56#include <systemd/sd-daemon.h>
68#include "configfile.h"
72_Atomic
bool AraKiri =
false;
73static bool Init =
true;
75bool SocketActivated =
false;
76static int ExitValue = EXIT_FAILURE;
77int HPForceReaderPolling = 0;
78bool disable_polkit =
false;
79static int pipefd[] = {-1, -1};
80static int signal_handler_fd[] = {-1, -1};
81bool Add_Serial_In_Name =
true;
82bool Add_Interface_In_Name =
true;
87static void at_exit(
void);
88static void clean_temp_files(
void);
89static void signal_trap(
int);
90static void print_version(
void);
91static void print_usage(
char const *
const);
105 uint32_t dwClientID = 0;
113 (void)HPStopHotPluggables();
118 ContextsDeinitialize();
122 EHDeinitializeEventStructures();
130 Log2(PCSC_LOG_DEBUG,
"A new context thread creation is requested: %d", dwClientID);
134 Log1(PCSC_LOG_ERROR,
"Problem during the context thread creation");
146 Log1(PCSC_LOG_ERROR,
"Error in ProcessEventsServer");
160 Log2(PCSC_LOG_ERROR,
"ProcessEventsServer unknown retval: %d",
183 r = read(signal_handler_fd[0], &sig,
sizeof sig);
186 Log2(PCSC_LOG_ERROR,
"read failed: %s", strerror(errno));
190 Log2(PCSC_LOG_INFO,
"Received signal: %d", sig);
197 HPReCheckSerialReaders();
201 (void)signal(SIGUSR1, signal_trap);
210 Log1(PCSC_LOG_INFO,
"Direct suicide");
211 ExitValue = EXIT_SUCCESS;
218 ExitValue = EXIT_SUCCESS;
222 if (AraKiri ==
false)
224 Log1(PCSC_LOG_INFO,
"Preparing for suicide");
232 Log1(PCSC_LOG_INFO,
"Suicide during init");
239 static int lives = 2;
245 Log1(PCSC_LOG_INFO,
"Forced suicide");
255int main(
int argc,
char **argv)
258 bool setToForeground;
261 char *newReaderConfig = NULL;
263 struct stat fStatBuf;
264 int customMaxThreadCounter = 0;
265 int customMaxReaderHandles = 0;
266 int customMaxThreadCardHandles = 0;
269#ifdef HAVE_GETOPT_LONG
270 int option_index = 0;
271 static struct option long_options[] = {
272 {
"config", 1, NULL,
'c'},
273 {
"foreground", 0, NULL,
'f'},
274 {
"color", 0, NULL,
'T'},
275 {
"help", 0, NULL,
'h'},
276 {
"version", 0, NULL,
'v'},
277 {
"apdu", 0, NULL,
'a'},
278 {
"debug", 0, NULL,
'd'},
279 {
"info", 0, NULL,
'i'},
280 {
"error", 0, NULL,
'e'},
281 {
"critical", 0, NULL,
'C'},
282 {
"hotplug", 0, NULL,
'H'},
283 {
"force-reader-polling", optional_argument, NULL, 0},
284 {
"max-thread", 1, NULL,
't'},
285 {
"max-card-handle-per-thread", 1, NULL,
's'},
286 {
"max-card-handle-per-reader", 1, NULL,
'r'},
287 {
"auto-exit", 0, NULL,
'x'},
288 {
"reader-name-no-serial", 0, NULL,
'S'},
289 {
"reader-name-no-interface", 0, NULL,
'I'},
290 {
"disable-polkit", 0, NULL, 1},
294#define OPT_STRING "c:fTdhvaieCHt:r:s:xSI"
296 setToForeground =
false;
304 printf(
"BUILD ERROR: The release version number PCSCLITE_VERSION_NUMBER\n");
305 printf(
" in pcsclite.h (%s) does not match the release version number\n",
307 printf(
" generated in config.h (%s) (see configure.in).\n", VERSION);
319 DebugLogSetLogType(DEBUGLOG_SYSLOG_DEBUG);
324#ifdef HAVE_GETOPT_LONG
325 while ((opt = getopt_long (argc, argv, OPT_STRING, long_options, &option_index)) != -1) {
327 while ((opt = getopt (argc, argv, OPT_STRING)) != -1) {
330#ifdef HAVE_GETOPT_LONG
332 if (strcmp(long_options[option_index].name,
333 "force-reader-polling") == 0)
334 HPForceReaderPolling = optarg ? abs(atoi(optarg)) : 1;
337 if (strcmp(long_options[option_index].name,
338 "disable-polkit") == 0)
339 disable_polkit =
true;
344 Log2(PCSC_LOG_INFO,
"using new config directory: %s", optarg);
345 newReaderConfig = optarg;
350 setToForeground =
true;
352 DebugLogSetLogType(DEBUGLOG_STDOUT_DEBUG);
354 "pcscd set to foreground with debug send to stdout");
358 DebugLogSetLogType(DEBUGLOG_STDOUT_COLOR_DEBUG);
359 Log1(PCSC_LOG_INFO,
"Force colored logs");
363 DebugLogSetLevel(PCSC_LOG_DEBUG);
367 DebugLogSetLevel(PCSC_LOG_INFO);
371 DebugLogSetLevel(PCSC_LOG_ERROR);
375 DebugLogSetLevel(PCSC_LOG_CRITICAL);
379 print_usage (argv[0]);
387 DebugLogSetCategory(DEBUG_CATEGORY_APDU);
392 DebugLogSetLogType(DEBUGLOG_STDOUT_DEBUG);
397 customMaxThreadCounter = optarg ? atoi(optarg) : 0;
398 Log2(PCSC_LOG_INFO,
"setting customMaxThreadCounter to: %d",
399 customMaxThreadCounter);
403 customMaxReaderHandles = optarg ? atoi(optarg) : 0;
404 Log2(PCSC_LOG_INFO,
"setting customMaxReaderHandles to: %d",
405 customMaxReaderHandles);
409 customMaxThreadCardHandles = optarg ? atoi(optarg) : 0;
410 Log2(PCSC_LOG_INFO,
"setting customMaxThreadCardHandles to: %d",
411 customMaxThreadCardHandles);
416 Log2(PCSC_LOG_INFO,
"Auto exit after %d seconds of inactivity",
417 TIME_BEFORE_SUICIDE);
421 Add_Serial_In_Name =
false;
425 Add_Interface_In_Name =
false;
429 print_usage (argv[0]);
437 printf(
"Unknown option: %s\n", argv[optind]);
438 print_usage(argv[0]);
446 rv = sd_listen_fds(0);
449 Log1(PCSC_LOG_CRITICAL,
"Too many file descriptors received");
456 SocketActivated =
true;
457 Log1(PCSC_LOG_INFO,
"Started by systemd");
460 SocketActivated =
false;
468 rv = stat(PCSCLITE_CSOCK_NAME, &fStatBuf);
471 if (rv == 0 && !SocketActivated)
478 pid = GetDaemonPid();
483 return SendHotplugSignal();
488 Log1(PCSC_LOG_CRITICAL,
489 "file " PCSCLITE_CSOCK_NAME
" already exists.");
490 Log2(PCSC_LOG_CRITICAL,
491 "Another pcscd (pid: %ld) seems to be running.", (
long)pid);
503 Log2(PCSC_LOG_CRITICAL,
"kill failed: %s", strerror(errno));
511 Log1(PCSC_LOG_CRITICAL,
"file " PCSCLITE_RUN_PID
" do not exist");
512 Log1(PCSC_LOG_CRITICAL,
"Hotplug failed");
520 Log1(PCSC_LOG_CRITICAL,
"Hotplug failed: pcscd is not running");
529 Log2(PCSC_LOG_CRITICAL,
"chdir() failed: %s", strerror(errno));
536 if (!setToForeground)
541 if (pipe(pipefd) == -1)
543 Log2(PCSC_LOG_CRITICAL,
"pipe() failed: %s", strerror(errno));
550 Log2(PCSC_LOG_CRITICAL,
"fork() failed: %s", strerror(errno));
556 fd = open(
"/dev/null", O_RDWR);
559 dup2(fd, STDIN_FILENO);
560 dup2(fd, STDOUT_FILENO);
561 dup2(fd, STDERR_FILENO);
578 ret = read(pipefd[0], &buf, 1);
599 (void)signal(SIGQUIT, signal_trap);
600 (void)signal(SIGTERM, signal_trap);
601 (void)signal(SIGINT, signal_trap);
604 (void)signal(SIGALRM, signal_trap);
606 if (pipe(signal_handler_fd) == -1)
608 Log2(PCSC_LOG_CRITICAL,
"pipe() failed: %s", strerror(errno));
612 pthread_t signal_handler_thread;
613 rv = pthread_create(&signal_handler_thread, NULL,
signal_thread, NULL);
616 Log2(PCSC_LOG_CRITICAL,
"pthread_create failed: %s", strerror(rv));
624 int mode = S_IROTH | S_IXOTH | S_IRGRP | S_IXGRP | S_IRWXU;
626 rv = mkdir(PCSCLITE_IPC_DIR, mode);
627 if ((rv != 0) && (errno != EEXIST))
629 Log2(PCSC_LOG_CRITICAL,
630 "cannot create " PCSCLITE_IPC_DIR
": %s", strerror(errno));
637 (void)chmod(PCSCLITE_IPC_DIR, mode);
643 rv = RFAllocateReaderSpace(customMaxReaderHandles);
653 rv = RFStartSerialReaders(newReaderConfig);
656 Log3(PCSC_LOG_CRITICAL,
"invalid directory %s: %s", newReaderConfig,
663 rv = RFStartSerialReaders(PCSCLITE_CONFIG_DIR);
669 Log1(PCSC_LOG_INFO,
"pcsc-lite " VERSION
" daemon ready.");
679 int mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
681 f = open(PCSCLITE_RUN_PID, O_RDWR | O_CREAT, mode);
684 char pid[PID_ASCII_SIZE];
687 (void)snprintf(pid,
sizeof(pid),
"%u\n", (unsigned) getpid());
688 rr = write(f, pid, strlen(pid) + 1);
691 Log2(PCSC_LOG_CRITICAL,
692 "writing " PCSCLITE_RUN_PID
" failed: %s",
699 (void)fchmod(f, mode);
704 Log2(PCSC_LOG_CRITICAL,
"cannot create " PCSCLITE_RUN_PID
": %s",
716 (void)signal(SIGUSR1, signal_trap);
723 rv = ListenExistingSocket(SD_LISTEN_FDS_START + 0);
730 Log1(PCSC_LOG_CRITICAL,
"Error initializing pcscd.");
737 rv = ContextsInitialize(customMaxThreadCounter, customMaxThreadCardHandles);
741 Log1(PCSC_LOG_CRITICAL,
"Error initializing pcscd.");
745 (void)signal(SIGPIPE, SIG_IGN);
746 (void)signal(SIGHUP, SIG_IGN);
749 const char *hpDirPath =
SYS_GetEnv(
"PCSCLITE_HP_DROPDIR");
750 if (NULL == hpDirPath)
751 hpDirPath = PCSCLITE_HP_DROPDIR;
752 Log2(PCSC_LOG_INFO,
"Using drivers directory: %s", hpDirPath);
754#if !defined(PCSCLITE_STATIC_DRIVER) && defined(USE_USB)
758 rv = HPSearchHotPluggables(hpDirPath);
766 rv = HPRegisterForHotplugEvents(hpDirPath);
769 Log1(PCSC_LOG_ERROR,
"HPRegisterForHotplugEvents failed");
773 RFWaitForReaderInit();
783 rr = write(pipefd[1], &buf, 1);
786 Log2(PCSC_LOG_ERROR,
"write() failed: %s", strerror(errno));
794 Log2(PCSC_LOG_DEBUG,
"Starting suicide alarm in %d seconds",
795 TIME_BEFORE_SUICIDE);
796 alarm(TIME_BEFORE_SUICIDE);
801 Log1(PCSC_LOG_ERROR,
"SVCServiceRunLoop returned");
805static void at_exit(
void)
807 Log1(PCSC_LOG_INFO,
"cleaning " PCSCLITE_IPC_DIR);
818 r = write(pipefd[1], &buf, 1);
821 Log2(PCSC_LOG_ERROR,
"write() failed: %s", strerror(errno));
829static void clean_temp_files(
void)
833 if (!SocketActivated)
835 rv = remove(PCSCLITE_CSOCK_NAME);
837 Log2(PCSC_LOG_ERROR,
"Cannot remove " PCSCLITE_CSOCK_NAME
": %s",
841 rv = remove(PCSCLITE_RUN_PID);
843 Log2(PCSC_LOG_ERROR,
"Cannot remove " PCSCLITE_RUN_PID
": %s",
847static void signal_trap(
int sig)
851 r = write(signal_handler_fd[1], &sig,
sizeof sig);
853 Log2(PCSC_LOG_ERROR,
"write failed: %s", strerror(errno));
856static void print_version(
void)
858 printf(
"pcsc-lite version " VERSION
"\n");
859 printf(
"Copyright (C) 1999-2002 by David Corcoran <corcoran@musclecard.com>.\n");
860 printf(
"Copyright (C) 2001-2024 by Ludovic Rousseau <ludovic.rousseau@free.fr>.\n");
861 printf(
"Copyright (C) 2003-2004 by Damien Sauveron <sauveron@labri.fr>.\n");
862 printf(
"Report bugs to <pcsclite-muscle@lists.infradead.org>.\n");
864 printf(
"Enabled features: " PCSCLITE_FEATURES
"\n");
865 printf(
"MAX_READERNAME: %d, PCSCLITE_MAX_READERS_CONTEXTS: %d\n",
869static void print_usage(
char const *
const progname)
871 printf(
"Usage: %s options\n", progname);
872 printf(
"Options:\n");
873#ifdef HAVE_GETOPT_LONG
874 printf(
" -a, --apdu log APDU commands and results\n");
876 printf(
" -c, --config new reader.conf.d path\n");
878 printf(
" -f, --foreground run in foreground (no daemon),\n");
879 printf(
" send logs to stdout instead of syslog\n");
880 printf(
" -T, --color force use of colored logs\n");
881 printf(
" -h, --help display usage information\n");
882 printf(
" -H, --hotplug ask the daemon to rescan the available readers\n");
883 printf(
" -v, --version display the program version number\n");
884 printf(
" -d, --debug display lower level debug messages\n");
885 printf(
" -i, --info display info level debug messages\n");
886 printf(
" -e --error display error level debug messages (default level)\n");
887 printf(
" -C --critical display critical only level debug messages\n");
888 printf(
" --force-reader-polling ignore the IFD_GENERATE_HOTPLUG reader capability\n");
889 printf(
" -t, --max-thread maximum number of threads (default %d)\n", PCSC_MAX_CONTEXT_THREADS);
890 printf(
" -s, --max-card-handle-per-thread maximum number of card handle per thread (default: %d)\n", PCSC_MAX_CONTEXT_CARD_HANDLES);
891 printf(
" -r, --max-card-handle-per-reader maximum number of card handle per reader (default: %d)\n", PCSC_MAX_READER_HANDLES);
892 printf(
" -x, --auto-exit pcscd will quit after %d seconds of inactivity\n", TIME_BEFORE_SUICIDE);
893 printf(
" -S, --reader-name-no-serial do not include the USB serial number in the name\n");
894 printf(
" -I, --reader-name-no-interface do not include the USB interface name in the name\n");
895 printf(
" --disable-polkit disable polkit support\n");
897 printf(
" -a log APDU commands and results\n");
899 printf(
" -c new reader.conf.d path\n");
901 printf(
" -f run in foreground (no daemon), send logs to stdout instead of syslog\n");
902 printf(
" -T force use of colored logs\n");
903 printf(
" -d display debug messages.\n");
904 printf(
" -i display info messages.\n");
905 printf(
" -e display error messages (default level).\n");
906 printf(
" -C display critical messages.\n");
907 printf(
" -h display usage information\n");
908 printf(
" -H ask the daemon to rescan the available readers\n");
909 printf(
" -v display the program version number\n");
910 printf(
" -t maximum number of threads\n");
911 printf(
" -s maximum number of card handle per thread\n");
912 printf(
" -r maximum number of card handle per reader\n");
913 printf(
" -x pcscd will quit after %d seconds of inactivity\n", TIME_BEFORE_SUICIDE);
This handles card insertion/removal events, updates ATR, protocol, and status information.
#define SCARD_S_SUCCESS
No error was encountered.
This provides a search API for hot pluggble devices.
bool AutoExit
Represents an Application Context on the Server side.
static void * signal_thread(void *arg)
thread dedicated to handle signals
static void SVCServiceRunLoop(void)
The Server's Message Queue Listener function.
This keeps a list of defines for pcsc-lite.
#define PCSCLITE_VERSION_NUMBER
Current version.
#define PCSCLITE_MAX_READERS_CONTEXTS
Maximum readers context (a slot is count as a reader)
This keeps track of a list of currently available reader structures.
This handles abstract system level calls.
const char * SYS_GetEnv(const char *name)
(More) secure version of getenv(3)
int SYS_USleep(int)
Makes the current process sleep for some microseconds.
void SYS_InitRandom(void)
Initialize the random generator.
int SYS_Sleep(int)
Makes the current process sleep for some seconds.
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.
INTERNAL int32_t ProcessEventsServer(uint32_t *pdwClientID)
Looks for messages sent by clients.
LONG CreateContextThread(uint32_t *pdwClientID)
Creates threads to handle messages received from Clients.
This demarshalls functions over the message queue and keeps track of clients and their handles.