45 #include <sys/types.h> 57 #include <systemd/sd-daemon.h> 69 #include "configfile.h" 78 _Atomic
char AraKiri = FALSE;
79 static char Init = TRUE;
81 char SocketActivated = FALSE;
82 static int ExitValue = EXIT_FAILURE;
83 int HPForceReaderPolling = 0;
84 static int pipefd[] = {-1, -1};
85 static int signal_handler_fd[] = {-1, -1};
86 char Add_Serial_In_Name = TRUE;
87 char Add_Interface_In_Name = TRUE;
92 static void at_exit(
void);
93 static void clean_temp_files(
void);
94 static void signal_trap(
int);
95 static void print_version(
void);
96 static void print_usage(
char const *
const);
110 uint32_t dwClientID = 0;
118 (void)HPStopHotPluggables();
124 EHDeinitializeEventStructures();
125 ContextsDeinitialize();
133 Log2(PCSC_LOG_DEBUG,
"A new context thread creation is requested: %d", dwClientID);
137 Log1(PCSC_LOG_ERROR,
"Problem during the context thread creation");
149 Log1(PCSC_LOG_ERROR,
"Error in ProcessEventsServer");
163 Log2(PCSC_LOG_ERROR,
"ProcessEventsServer unknown retval: %d",
186 r = read(signal_handler_fd[0], &sig,
sizeof sig);
189 Log2(PCSC_LOG_ERROR,
"read failed: %s", strerror(errno));
193 Log2(PCSC_LOG_INFO,
"Received signal: %d", sig);
200 HPReCheckSerialReaders();
204 (void)signal(SIGUSR1, signal_trap);
213 Log1(PCSC_LOG_INFO,
"Direct suicide");
214 ExitValue = EXIT_SUCCESS;
221 ExitValue = EXIT_SUCCESS;
225 if (AraKiri == FALSE)
227 Log1(PCSC_LOG_INFO,
"Preparing for suicide");
235 Log1(PCSC_LOG_INFO,
"Suicide during init");
242 static int lives = 2;
248 Log1(PCSC_LOG_INFO,
"Forced suicide");
258 int main(
int argc,
char **argv)
261 char setToForeground;
264 char *newReaderConfig = NULL;
266 struct stat fStatBuf;
267 int customMaxThreadCounter = 0;
268 int customMaxReaderHandles = 0;
269 int customMaxThreadCardHandles = 0;
272 #ifdef HAVE_GETOPT_LONG 273 int option_index = 0;
274 static struct option long_options[] = {
275 {
"config", 1, NULL,
'c'},
276 {
"foreground", 0, NULL,
'f'},
277 {
"color", 0, NULL,
'T'},
278 {
"help", 0, NULL,
'h'},
279 {
"version", 0, NULL,
'v'},
280 {
"apdu", 0, NULL,
'a'},
281 {
"debug", 0, NULL,
'd'},
282 {
"info", 0, NULL,
'i'},
283 {
"error", 0, NULL,
'e'},
284 {
"critical", 0, NULL,
'C'},
285 {
"hotplug", 0, NULL,
'H'},
286 {
"force-reader-polling", optional_argument, NULL, 0},
287 {
"max-thread", 1, NULL,
't'},
288 {
"max-card-handle-per-thread", 1, NULL,
's'},
289 {
"max-card-handle-per-reader", 1, NULL,
'r'},
290 {
"auto-exit", 0, NULL,
'x'},
291 {
"reader-name-no-serial", 0, NULL,
'S'},
292 {
"reader-name-no-interface", 0, NULL,
'I'},
296 #define OPT_STRING "c:fTdhvaieCHt:r:s:xSI" 298 setToForeground = FALSE;
306 printf(
"BUILD ERROR: The release version number PCSCLITE_VERSION_NUMBER\n");
307 printf(
" in pcsclite.h (%s) does not match the release version number\n",
309 printf(
" generated in config.h (%s) (see configure.in).\n", VERSION);
321 DebugLogSetLogType(DEBUGLOG_SYSLOG_DEBUG);
326 #ifdef HAVE_GETOPT_LONG 327 while ((opt = getopt_long (argc, argv, OPT_STRING, long_options, &option_index)) != -1) {
329 while ((opt = getopt (argc, argv, OPT_STRING)) != -1) {
332 #ifdef HAVE_GETOPT_LONG 334 if (strcmp(long_options[option_index].name,
335 "force-reader-polling") == 0)
336 HPForceReaderPolling = optarg ? abs(atoi(optarg)) : 1;
341 Log2(PCSC_LOG_INFO,
"using new config directory: %s", optarg);
342 newReaderConfig = optarg;
347 setToForeground = TRUE;
349 DebugLogSetLogType(DEBUGLOG_STDOUT_DEBUG);
351 "pcscd set to foreground with debug send to stdout");
355 DebugLogSetLogType(DEBUGLOG_STDOUT_COLOR_DEBUG);
356 Log1(PCSC_LOG_INFO,
"Force colored logs");
360 DebugLogSetLevel(PCSC_LOG_DEBUG);
364 DebugLogSetLevel(PCSC_LOG_INFO);
368 DebugLogSetLevel(PCSC_LOG_ERROR);
372 DebugLogSetLevel(PCSC_LOG_CRITICAL);
376 print_usage (argv[0]);
384 DebugLogSetCategory(DEBUG_CATEGORY_APDU);
389 DebugLogSetLogType(DEBUGLOG_STDOUT_DEBUG);
394 customMaxThreadCounter = optarg ? atoi(optarg) : 0;
395 Log2(PCSC_LOG_INFO,
"setting customMaxThreadCounter to: %d",
396 customMaxThreadCounter);
400 customMaxReaderHandles = optarg ? atoi(optarg) : 0;
401 Log2(PCSC_LOG_INFO,
"setting customMaxReaderHandles to: %d",
402 customMaxReaderHandles);
406 customMaxThreadCardHandles = optarg ? atoi(optarg) : 0;
407 Log2(PCSC_LOG_INFO,
"setting customMaxThreadCardHandles to: %d",
408 customMaxThreadCardHandles);
413 Log2(PCSC_LOG_INFO,
"Auto exit after %d seconds of inactivity",
414 TIME_BEFORE_SUICIDE);
418 Add_Serial_In_Name = FALSE;
422 Add_Interface_In_Name = FALSE;
426 print_usage (argv[0]);
434 printf(
"Unknown option: %s\n", argv[optind]);
435 print_usage(argv[0]);
439 #ifdef USE_LIBSYSTEMD 443 rv = sd_listen_fds(0);
446 Log1(PCSC_LOG_CRITICAL,
"Too many file descriptors received");
453 SocketActivated = TRUE;
454 Log1(PCSC_LOG_INFO,
"Started by systemd");
457 SocketActivated = FALSE;
465 rv = stat(PCSCLITE_CSOCK_NAME, &fStatBuf);
468 if (rv == 0 && !SocketActivated)
475 pid = GetDaemonPid();
480 return SendHotplugSignal();
485 Log1(PCSC_LOG_CRITICAL,
486 "file " PCSCLITE_CSOCK_NAME
" already exists.");
487 Log2(PCSC_LOG_CRITICAL,
488 "Another pcscd (pid: %ld) seems to be running.", (
long)pid);
500 Log2(PCSC_LOG_CRITICAL,
"kill failed: %s", strerror(errno));
508 Log1(PCSC_LOG_CRITICAL,
"file " PCSCLITE_RUN_PID
" do not exist");
509 Log1(PCSC_LOG_CRITICAL,
"Hotplug failed");
517 Log1(PCSC_LOG_CRITICAL,
"Hotplug failed: pcscd is not running");
526 Log2(PCSC_LOG_CRITICAL,
"chdir() failed: %s", strerror(errno));
533 if (!setToForeground)
538 if (pipe(pipefd) == -1)
540 Log2(PCSC_LOG_CRITICAL,
"pipe() failed: %s", strerror(errno));
547 Log2(PCSC_LOG_CRITICAL,
"fork() failed: %s", strerror(errno));
553 fd = open(
"/dev/null", O_RDWR);
556 dup2(fd, STDIN_FILENO);
557 dup2(fd, STDOUT_FILENO);
558 dup2(fd, STDERR_FILENO);
575 ret = read(pipefd[0], &buf, 1);
596 (void)signal(SIGQUIT, signal_trap);
597 (void)signal(SIGTERM, signal_trap);
598 (void)signal(SIGINT, signal_trap);
601 (void)signal(SIGALRM, signal_trap);
603 if (pipe(signal_handler_fd) == -1)
605 Log2(PCSC_LOG_CRITICAL,
"pipe() failed: %s", strerror(errno));
609 pthread_t signal_handler_thread;
610 rv = pthread_create(&signal_handler_thread, NULL,
signal_thread, NULL);
613 Log2(PCSC_LOG_CRITICAL,
"pthread_create failed: %s", strerror(rv));
621 int mode = S_IROTH | S_IXOTH | S_IRGRP | S_IXGRP | S_IRWXU;
623 rv = mkdir(PCSCLITE_IPC_DIR, mode);
624 if ((rv != 0) && (errno != EEXIST))
626 Log2(PCSC_LOG_CRITICAL,
627 "cannot create " PCSCLITE_IPC_DIR
": %s", strerror(errno));
634 (void)chmod(PCSCLITE_IPC_DIR, mode);
640 rv = RFAllocateReaderSpace(customMaxReaderHandles);
650 rv = RFStartSerialReaders(newReaderConfig);
653 Log3(PCSC_LOG_CRITICAL,
"invalid directory %s: %s", newReaderConfig,
660 rv = RFStartSerialReaders(PCSCLITE_CONFIG_DIR);
666 Log1(PCSC_LOG_INFO,
"pcsc-lite " VERSION
" daemon ready.");
676 int mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
678 f = open(PCSCLITE_RUN_PID, O_RDWR | O_CREAT, mode);
681 char pid[PID_ASCII_SIZE];
684 (void)snprintf(pid,
sizeof(pid),
"%u\n", (unsigned) getpid());
685 rr = write(f, pid, strlen(pid) + 1);
688 Log2(PCSC_LOG_CRITICAL,
689 "writing " PCSCLITE_RUN_PID
" failed: %s",
696 (void)fchmod(f, mode);
701 Log2(PCSC_LOG_CRITICAL,
"cannot create " PCSCLITE_RUN_PID
": %s",
713 (void)signal(SIGUSR1, signal_trap);
718 #ifdef USE_LIBSYSTEMD 720 rv = ListenExistingSocket(SD_LISTEN_FDS_START + 0);
727 Log1(PCSC_LOG_CRITICAL,
"Error initializing pcscd.");
734 rv = ContextsInitialize(customMaxThreadCounter, customMaxThreadCardHandles);
738 Log1(PCSC_LOG_CRITICAL,
"Error initializing pcscd.");
742 (void)signal(SIGPIPE, SIG_IGN);
743 (void)signal(SIGHUP, SIG_IGN);
746 #if !defined(PCSCLITE_STATIC_DRIVER) && defined(USE_USB) 750 rv = HPSearchHotPluggables();
758 rv = HPRegisterForHotplugEvents();
761 Log1(PCSC_LOG_ERROR,
"HPRegisterForHotplugEvents failed");
765 RFWaitForReaderInit();
775 rr = write(pipefd[1], &buf, 1);
778 Log2(PCSC_LOG_ERROR,
"write() failed: %s", strerror(errno));
786 Log2(PCSC_LOG_DEBUG,
"Starting suicide alarm in %d seconds",
787 TIME_BEFORE_SUICIDE);
788 alarm(TIME_BEFORE_SUICIDE);
793 Log1(PCSC_LOG_ERROR,
"SVCServiceRunLoop returned");
797 static void at_exit(
void)
799 Log1(PCSC_LOG_INFO,
"cleaning " PCSCLITE_IPC_DIR);
810 r = write(pipefd[1], &buf, 1);
813 Log2(PCSC_LOG_ERROR,
"write() failed: %s", strerror(errno));
821 static void clean_temp_files(
void)
825 if (!SocketActivated)
827 rv =
remove(PCSCLITE_CSOCK_NAME);
829 Log2(PCSC_LOG_ERROR,
"Cannot remove " PCSCLITE_CSOCK_NAME
": %s",
833 rv =
remove(PCSCLITE_RUN_PID);
835 Log2(PCSC_LOG_ERROR,
"Cannot remove " PCSCLITE_RUN_PID
": %s",
839 static void signal_trap(
int sig)
843 r = write(signal_handler_fd[1], &sig,
sizeof sig);
845 Log2(PCSC_LOG_ERROR,
"write failed: %s", strerror(errno));
848 static void print_version(
void)
850 printf(
"%s version %s.\n", PACKAGE, VERSION);
851 printf(
"Copyright (C) 1999-2002 by David Corcoran <corcoran@musclecard.com>.\n");
852 printf(
"Copyright (C) 2001-2022 by Ludovic Rousseau <ludovic.rousseau@free.fr>.\n");
853 printf(
"Copyright (C) 2003-2004 by Damien Sauveron <sauveron@labri.fr>.\n");
854 printf(
"Report bugs to <pcsclite-muscle@lists.infradead.org>.\n");
856 printf(
"Enabled features:%s\n", PCSCLITE_FEATURES);
857 printf(
"MAX_READERNAME: %d, PCSCLITE_MAX_READERS_CONTEXTS: %d\n",
861 static void print_usage(
char const *
const progname)
863 printf(
"Usage: %s options\n", progname);
864 printf(
"Options:\n");
865 #ifdef HAVE_GETOPT_LONG 866 printf(
" -a, --apdu log APDU commands and results\n");
868 printf(
" -c, --config new reader.conf.d path\n");
870 printf(
" -f, --foreground run in foreground (no daemon),\n");
871 printf(
" send logs to stdout instead of syslog\n");
872 printf(
" -T, --color force use of colored logs\n");
873 printf(
" -h, --help display usage information\n");
874 printf(
" -H, --hotplug ask the daemon to rescan the available readers\n");
875 printf(
" -v, --version display the program version number\n");
876 printf(
" -d, --debug display lower level debug messages\n");
877 printf(
" -i, --info display info level debug messages\n");
878 printf(
" -e --error display error level debug messages (default level)\n");
879 printf(
" -C --critical display critical only level debug messages\n");
880 printf(
" --force-reader-polling ignore the IFD_GENERATE_HOTPLUG reader capability\n");
881 printf(
" -t, --max-thread maximum number of threads (default %d)\n", PCSC_MAX_CONTEXT_THREADS);
882 printf(
" -s, --max-card-handle-per-thread maximum number of card handle per thread (default: %d)\n", PCSC_MAX_CONTEXT_CARD_HANDLES);
883 printf(
" -r, --max-card-handle-per-reader maximum number of card handle per reader (default: %d)\n", PCSC_MAX_READER_HANDLES);
884 printf(
" -x, --auto-exit pcscd will quit after %d seconds of inactivity\n", TIME_BEFORE_SUICIDE);
885 printf(
" -S, --reader-name-no-serial do not include the USB serial number in the name\n");
886 printf(
" -I, --reader-name-no-interface do not include the USB interface name in the name\n");
888 printf(
" -a log APDU commands and results\n");
890 printf(
" -c new reader.conf.d path\n");
892 printf(
" -f run in foreground (no daemon), send logs to stdout instead of syslog\n");
893 printf(
" -T force use of colored logs\n");
894 printf(
" -d display debug messages.\n");
895 printf(
" -i display info messages.\n");
896 printf(
" -e display error messages (default level).\n");
897 printf(
" -C display critical messages.\n");
898 printf(
" -h display usage information\n");
899 printf(
" -H ask the daemon to rescan the available readers\n");
900 printf(
" -v display the program version number\n");
901 printf(
" -t maximum number of threads\n");
902 printf(
" -s maximum number of card handle per thread\n");
903 printf(
" -r maximum number of card handle per reader\n");
904 printf(
" -x pcscd will quit after %d seconds of inactivity\n", TIME_BEFORE_SUICIDE);
#define SCARD_S_SUCCESS
No error was encountered.
void SYS_InitRandom(void)
Initialize the random generator.
LONG CreateContextThread(uint32_t *pdwClientID)
Creates threads to handle messages received from Clients.
This handles abstract system level calls.
char AutoExit
Represents an Application Context on the Server side.
int SYS_Sleep(int)
Makes the current process sleep for some seconds.
This demarshalls functions over the message queue and keeps track of clients and their handles...
int SYS_USleep(int)
Makes the current process sleep for some microseconds.
#define PCSCLITE_MAX_READERS_CONTEXTS
Maximum readers context (a slot is count as a reader)
This defines some structures and #defines to be used over the transport layer.
INTERNAL int32_t InitializeSocket(void)
Prepares the communication channel used by the server to talk to the clients.
This handles card insertion/removal events, updates ATR, protocol, and status information.
static void SVCServiceRunLoop(void)
The Server's Message Queue Listener function.
This keeps a list of defines for pcsc-lite.
static void * signal_thread(void *arg)
thread dedicated to handle signals
This keeps a list of defines for pcsc-lite.
INTERNAL int32_t ProcessEventsServer(uint32_t *pdwClientID)
Looks for messages sent by clients.
This keeps track of a list of currently available reader structures.
This provides a search API for hot pluggble devices.
#define PCSCLITE_VERSION_NUMBER
Current version.