45 #include <sys/types.h> 57 #include <systemd/sd-daemon.h> 69 #include "configfile.h" 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);
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");
220 ExitValue = EXIT_SUCCESS;
224 if (AraKiri == FALSE)
226 Log1(PCSC_LOG_INFO,
"Preparing for suicide");
234 Log1(PCSC_LOG_INFO,
"Suicide during init");
241 static int lives = 2;
247 Log1(PCSC_LOG_INFO,
"Forced suicide");
257 int main(
int argc,
char **argv)
260 char setToForeground;
262 char *newReaderConfig;
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'},
293 #define OPT_STRING "c:fTdhvaieCHt:r:s:xSI" 295 newReaderConfig = NULL;
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;
338 Log2(PCSC_LOG_INFO,
"using new config file: %s", optarg);
339 newReaderConfig = optarg;
343 setToForeground = TRUE;
345 DebugLogSetLogType(DEBUGLOG_STDOUT_DEBUG);
347 "pcscd set to foreground with debug send to stdout");
351 DebugLogSetLogType(DEBUGLOG_STDOUT_COLOR_DEBUG);
352 Log1(PCSC_LOG_INFO,
"Force colored logs");
356 DebugLogSetLevel(PCSC_LOG_DEBUG);
360 DebugLogSetLevel(PCSC_LOG_INFO);
364 DebugLogSetLevel(PCSC_LOG_ERROR);
368 DebugLogSetLevel(PCSC_LOG_CRITICAL);
372 print_usage (argv[0]);
380 DebugLogSetCategory(DEBUG_CATEGORY_APDU);
385 DebugLogSetLogType(DEBUGLOG_STDOUT_DEBUG);
390 customMaxThreadCounter = optarg ? atoi(optarg) : 0;
391 Log2(PCSC_LOG_INFO,
"setting customMaxThreadCounter to: %d",
392 customMaxThreadCounter);
396 customMaxReaderHandles = optarg ? atoi(optarg) : 0;
397 Log2(PCSC_LOG_INFO,
"setting customMaxReaderHandles to: %d",
398 customMaxReaderHandles);
402 customMaxThreadCardHandles = optarg ? atoi(optarg) : 0;
403 Log2(PCSC_LOG_INFO,
"setting customMaxThreadCardHandles to: %d",
404 customMaxThreadCardHandles);
409 Log2(PCSC_LOG_INFO,
"Auto exit after %d seconds of inactivity",
410 TIME_BEFORE_SUICIDE);
414 Add_Serial_In_Name = FALSE;
418 Add_Interface_In_Name = FALSE;
422 print_usage (argv[0]);
430 printf(
"Unknown option: %s\n", argv[optind]);
431 print_usage(argv[0]);
435 #ifdef USE_LIBSYSTEMD 439 rv = sd_listen_fds(0);
442 Log1(PCSC_LOG_CRITICAL,
"Too many file descriptors received");
449 SocketActivated = TRUE;
450 Log1(PCSC_LOG_INFO,
"Started by systemd");
453 SocketActivated = FALSE;
461 rv = stat(PCSCLITE_CSOCK_NAME, &fStatBuf);
464 if (rv == 0 && !SocketActivated)
471 pid = GetDaemonPid();
476 return SendHotplugSignal();
481 Log1(PCSC_LOG_CRITICAL,
482 "file " PCSCLITE_CSOCK_NAME
" already exists.");
483 Log2(PCSC_LOG_CRITICAL,
484 "Another pcscd (pid: %ld) seems to be running.", (
long)pid);
496 Log2(PCSC_LOG_CRITICAL,
"kill failed: %s", strerror(errno));
504 Log1(PCSC_LOG_CRITICAL,
"file " PCSCLITE_RUN_PID
" do not exist");
505 Log1(PCSC_LOG_CRITICAL,
"Hotplug failed");
513 Log1(PCSC_LOG_CRITICAL,
"Hotplug failed: pcscd is not running");
522 Log2(PCSC_LOG_CRITICAL,
"chdir() failed: %s", strerror(errno));
529 if (!setToForeground)
534 if (pipe(pipefd) == -1)
536 Log2(PCSC_LOG_CRITICAL,
"pipe() failed: %s", strerror(errno));
543 Log2(PCSC_LOG_CRITICAL,
"fork() failed: %s", strerror(errno));
549 fd = open(
"/dev/null", O_RDWR);
552 dup2(fd, STDIN_FILENO);
553 dup2(fd, STDOUT_FILENO);
554 dup2(fd, STDERR_FILENO);
571 ret = read(pipefd[0], &buf, 1);
592 (void)signal(SIGQUIT, signal_trap);
593 (void)signal(SIGTERM, signal_trap);
594 (void)signal(SIGINT, signal_trap);
597 (void)signal(SIGALRM, signal_trap);
599 if (pipe(signal_handler_fd) == -1)
601 Log2(PCSC_LOG_CRITICAL,
"pipe() failed: %s", strerror(errno));
605 pthread_t signal_handler_thread;
606 rv = pthread_create(&signal_handler_thread, NULL,
signal_thread, NULL);
609 Log2(PCSC_LOG_CRITICAL,
"pthread_create failed: %s", strerror(rv));
617 int mode = S_IROTH | S_IXOTH | S_IRGRP | S_IXGRP | S_IRWXU;
619 rv = mkdir(PCSCLITE_IPC_DIR, mode);
620 if ((rv != 0) && (errno != EEXIST))
622 Log2(PCSC_LOG_CRITICAL,
623 "cannot create " PCSCLITE_IPC_DIR
": %s", strerror(errno));
630 (void)chmod(PCSCLITE_IPC_DIR, mode);
636 rv = RFAllocateReaderSpace(customMaxReaderHandles);
646 rv = RFStartSerialReaders(newReaderConfig);
649 Log3(PCSC_LOG_CRITICAL,
"invalid file %s: %s", newReaderConfig,
656 rv = RFStartSerialReaders(PCSCLITE_CONFIG_DIR);
662 Log1(PCSC_LOG_INFO,
"pcsc-lite " VERSION
" daemon ready.");
672 int mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
674 f = open(PCSCLITE_RUN_PID, O_RDWR | O_CREAT, mode);
677 char pid[PID_ASCII_SIZE];
680 (void)snprintf(pid,
sizeof(pid),
"%u\n", (unsigned) getpid());
681 rr = write(f, pid, strlen(pid) + 1);
684 Log2(PCSC_LOG_CRITICAL,
685 "writing " PCSCLITE_RUN_PID
" failed: %s",
693 (void)chmod(PCSCLITE_RUN_PID, mode);
696 Log2(PCSC_LOG_CRITICAL,
"cannot create " PCSCLITE_RUN_PID
": %s",
708 (void)signal(SIGUSR1, signal_trap);
713 #ifdef USE_LIBSYSTEMD 715 rv = ListenExistingSocket(SD_LISTEN_FDS_START + 0);
722 Log1(PCSC_LOG_CRITICAL,
"Error initializing pcscd.");
729 rv = ContextsInitialize(customMaxThreadCounter, customMaxThreadCardHandles);
733 Log1(PCSC_LOG_CRITICAL,
"Error initializing pcscd.");
737 (void)signal(SIGPIPE, SIG_IGN);
738 (void)signal(SIGHUP, SIG_IGN);
741 #if !defined(PCSCLITE_STATIC_DRIVER) && defined(USE_USB) 745 rv = HPSearchHotPluggables();
751 rv = HPRegisterForHotplugEvents();
754 Log1(PCSC_LOG_ERROR,
"HPRegisterForHotplugEvents failed");
758 RFWaitForReaderInit();
768 rr = write(pipefd[1], &buf, 1);
771 Log2(PCSC_LOG_ERROR,
"write() failed: %s", strerror(errno));
779 Log1(PCSC_LOG_ERROR,
"SVCServiceRunLoop returned");
783 static void at_exit(
void)
785 Log1(PCSC_LOG_INFO,
"cleaning " PCSCLITE_IPC_DIR);
796 r = write(pipefd[1], &buf, 1);
799 Log2(PCSC_LOG_ERROR,
"write() failed: %s", strerror(errno));
807 static void clean_temp_files(
void)
811 if (!SocketActivated)
813 rv =
remove(PCSCLITE_CSOCK_NAME);
815 Log2(PCSC_LOG_ERROR,
"Cannot remove " PCSCLITE_CSOCK_NAME
": %s",
819 rv =
remove(PCSCLITE_RUN_PID);
821 Log2(PCSC_LOG_ERROR,
"Cannot remove " PCSCLITE_RUN_PID
": %s",
825 static void signal_trap(
int sig)
829 r = write(signal_handler_fd[1], &sig,
sizeof sig);
831 Log2(PCSC_LOG_ERROR,
"write failed: %s", strerror(errno));
834 static void print_version(
void)
836 printf(
"%s version %s.\n", PACKAGE, VERSION);
837 printf(
"Copyright (C) 1999-2002 by David Corcoran <corcoran@musclecard.com>.\n");
838 printf(
"Copyright (C) 2001-2015 by Ludovic Rousseau <ludovic.rousseau@free.fr>.\n");
839 printf(
"Copyright (C) 2003-2004 by Damien Sauveron <sauveron@labri.fr>.\n");
840 printf(
"Report bugs to <pcsclite-muscle@lists.alioth.debian.org>.\n");
842 printf (
"Enabled features:%s\n", PCSCLITE_FEATURES);
845 static void print_usage(
char const *
const progname)
847 printf(
"Usage: %s options\n", progname);
848 printf(
"Options:\n");
849 #ifdef HAVE_GETOPT_LONG 850 printf(
" -a, --apdu log APDU commands and results\n");
851 printf(
" -c, --config path to reader.conf\n");
852 printf(
" -f, --foreground run in foreground (no daemon),\n");
853 printf(
" send logs to stdout instead of syslog\n");
854 printf(
" -T, --color force use of colored logs\n");
855 printf(
" -h, --help display usage information\n");
856 printf(
" -H, --hotplug ask the daemon to rescan the available readers\n");
857 printf(
" -v, --version display the program version number\n");
858 printf(
" -d, --debug display lower level debug messages\n");
859 printf(
" -i, --info display info level debug messages\n");
860 printf(
" -e --error display error level debug messages (default level)\n");
861 printf(
" -C --critical display critical only level debug messages\n");
862 printf(
" --force-reader-polling ignore the IFD_GENERATE_HOTPLUG reader capability\n");
863 printf(
" -t, --max-thread maximum number of threads (default %d)\n", PCSC_MAX_CONTEXT_THREADS);
864 printf(
" -s, --max-card-handle-per-thread maximum number of card handle per thread (default: %d)\n", PCSC_MAX_CONTEXT_CARD_HANDLES);
865 printf(
" -r, --max-card-handle-per-reader maximum number of card handle per reader (default: %d)\n", PCSC_MAX_READER_HANDLES);
866 printf(
" -x, --auto-exit pcscd will quit after %d seconds of inactivity\n", TIME_BEFORE_SUICIDE);
867 printf(
" -S, --reader-name-no-serial do not include the USB serial number in the name\n");
868 printf(
" -I, --reader-name-no-interface do not include the USB interface name in the name\n");
870 printf(
" -a log APDU commands and results\n");
871 printf(
" -c path to reader.conf\n");
872 printf(
" -f run in foreground (no daemon), send logs to stdout instead of syslog\n");
873 printf(
" -T force use of colored logs\n");
874 printf(
" -d display debug messages.\n");
875 printf(
" -i display info messages.\n");
876 printf(
" -e display error messages (default level).\n");
877 printf(
" -C display critical messages.\n");
878 printf(
" -h display usage information\n");
879 printf(
" -H ask the daemon to rescan the available readers\n");
880 printf(
" -v display the program version number\n");
881 printf(
" -t maximum number of threads\n");
882 printf(
" -s maximum number of card handle per thread\n");
883 printf(
" -r maximum number of card handle per reader\n");
884 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.
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)
Acquires a socket passed in from systemd.
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.