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");
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;
263 char *newReaderConfig;
264 struct stat fStatBuf;
265 int customMaxThreadCounter = 0;
266 int customMaxReaderHandles = 0;
267 int customMaxThreadCardHandles = 0;
270 #ifdef HAVE_GETOPT_LONG 271 int option_index = 0;
272 static struct option long_options[] = {
273 {
"config", 1, NULL,
'c'},
274 {
"foreground", 0, NULL,
'f'},
275 {
"color", 0, NULL,
'T'},
276 {
"help", 0, NULL,
'h'},
277 {
"version", 0, NULL,
'v'},
278 {
"apdu", 0, NULL,
'a'},
279 {
"debug", 0, NULL,
'd'},
280 {
"info", 0, NULL,
'i'},
281 {
"error", 0, NULL,
'e'},
282 {
"critical", 0, NULL,
'C'},
283 {
"hotplug", 0, NULL,
'H'},
284 {
"force-reader-polling", optional_argument, NULL, 0},
285 {
"max-thread", 1, NULL,
't'},
286 {
"max-card-handle-per-thread", 1, NULL,
's'},
287 {
"max-card-handle-per-reader", 1, NULL,
'r'},
288 {
"auto-exit", 0, NULL,
'x'},
289 {
"reader-name-no-serial", 0, NULL,
'S'},
290 {
"reader-name-no-interface", 0, NULL,
'I'},
294 #define OPT_STRING "c:fTdhvaieCHt:r:s:xSI" 296 newReaderConfig = NULL;
297 setToForeground = FALSE;
305 printf(
"BUILD ERROR: The release version number PCSCLITE_VERSION_NUMBER\n");
306 printf(
" in pcsclite.h (%s) does not match the release version number\n",
308 printf(
" generated in config.h (%s) (see configure.in).\n", VERSION);
320 DebugLogSetLogType(DEBUGLOG_SYSLOG_DEBUG);
325 #ifdef HAVE_GETOPT_LONG 326 while ((opt = getopt_long (argc, argv, OPT_STRING, long_options, &option_index)) != -1) {
328 while ((opt = getopt (argc, argv, OPT_STRING)) != -1) {
331 #ifdef HAVE_GETOPT_LONG 333 if (strcmp(long_options[option_index].name,
334 "force-reader-polling") == 0)
335 HPForceReaderPolling = optarg ? abs(atoi(optarg)) : 1;
339 Log2(PCSC_LOG_INFO,
"using new config file: %s", optarg);
340 newReaderConfig = optarg;
344 setToForeground = TRUE;
346 DebugLogSetLogType(DEBUGLOG_STDOUT_DEBUG);
348 "pcscd set to foreground with debug send to stdout");
352 DebugLogSetLogType(DEBUGLOG_STDOUT_COLOR_DEBUG);
353 Log1(PCSC_LOG_INFO,
"Force colored logs");
357 DebugLogSetLevel(PCSC_LOG_DEBUG);
361 DebugLogSetLevel(PCSC_LOG_INFO);
365 DebugLogSetLevel(PCSC_LOG_ERROR);
369 DebugLogSetLevel(PCSC_LOG_CRITICAL);
373 print_usage (argv[0]);
381 DebugLogSetCategory(DEBUG_CATEGORY_APDU);
386 DebugLogSetLogType(DEBUGLOG_STDOUT_DEBUG);
391 customMaxThreadCounter = optarg ? atoi(optarg) : 0;
392 Log2(PCSC_LOG_INFO,
"setting customMaxThreadCounter to: %d",
393 customMaxThreadCounter);
397 customMaxReaderHandles = optarg ? atoi(optarg) : 0;
398 Log2(PCSC_LOG_INFO,
"setting customMaxReaderHandles to: %d",
399 customMaxReaderHandles);
403 customMaxThreadCardHandles = optarg ? atoi(optarg) : 0;
404 Log2(PCSC_LOG_INFO,
"setting customMaxThreadCardHandles to: %d",
405 customMaxThreadCardHandles);
410 Log2(PCSC_LOG_INFO,
"Auto exit after %d seconds of inactivity",
411 TIME_BEFORE_SUICIDE);
415 Add_Serial_In_Name = FALSE;
419 Add_Interface_In_Name = FALSE;
423 print_usage (argv[0]);
431 printf(
"Unknown option: %s\n", argv[optind]);
432 print_usage(argv[0]);
436 #ifdef USE_LIBSYSTEMD 440 rv = sd_listen_fds(0);
443 Log1(PCSC_LOG_CRITICAL,
"Too many file descriptors received");
450 SocketActivated = TRUE;
451 Log1(PCSC_LOG_INFO,
"Started by systemd");
454 SocketActivated = FALSE;
462 rv = stat(PCSCLITE_CSOCK_NAME, &fStatBuf);
465 if (rv == 0 && !SocketActivated)
472 pid = GetDaemonPid();
477 return SendHotplugSignal();
482 Log1(PCSC_LOG_CRITICAL,
483 "file " PCSCLITE_CSOCK_NAME
" already exists.");
484 Log2(PCSC_LOG_CRITICAL,
485 "Another pcscd (pid: %ld) seems to be running.", (
long)pid);
497 Log2(PCSC_LOG_CRITICAL,
"kill failed: %s", strerror(errno));
505 Log1(PCSC_LOG_CRITICAL,
"file " PCSCLITE_RUN_PID
" do not exist");
506 Log1(PCSC_LOG_CRITICAL,
"Hotplug failed");
514 Log1(PCSC_LOG_CRITICAL,
"Hotplug failed: pcscd is not running");
523 Log2(PCSC_LOG_CRITICAL,
"chdir() failed: %s", strerror(errno));
530 if (!setToForeground)
535 if (pipe(pipefd) == -1)
537 Log2(PCSC_LOG_CRITICAL,
"pipe() failed: %s", strerror(errno));
544 Log2(PCSC_LOG_CRITICAL,
"fork() failed: %s", strerror(errno));
550 fd = open(
"/dev/null", O_RDWR);
553 dup2(fd, STDIN_FILENO);
554 dup2(fd, STDOUT_FILENO);
555 dup2(fd, STDERR_FILENO);
572 ret = read(pipefd[0], &buf, 1);
593 (void)signal(SIGQUIT, signal_trap);
594 (void)signal(SIGTERM, signal_trap);
595 (void)signal(SIGINT, signal_trap);
598 (void)signal(SIGALRM, signal_trap);
600 if (pipe(signal_handler_fd) == -1)
602 Log2(PCSC_LOG_CRITICAL,
"pipe() failed: %s", strerror(errno));
606 pthread_t signal_handler_thread;
607 rv = pthread_create(&signal_handler_thread, NULL,
signal_thread, NULL);
610 Log2(PCSC_LOG_CRITICAL,
"pthread_create failed: %s", strerror(rv));
618 int mode = S_IROTH | S_IXOTH | S_IRGRP | S_IXGRP | S_IRWXU;
620 rv = mkdir(PCSCLITE_IPC_DIR, mode);
621 if ((rv != 0) && (errno != EEXIST))
623 Log2(PCSC_LOG_CRITICAL,
624 "cannot create " PCSCLITE_IPC_DIR
": %s", strerror(errno));
631 (void)chmod(PCSCLITE_IPC_DIR, mode);
637 rv = RFAllocateReaderSpace(customMaxReaderHandles);
647 rv = RFStartSerialReaders(newReaderConfig);
650 Log3(PCSC_LOG_CRITICAL,
"invalid file %s: %s", newReaderConfig,
657 rv = RFStartSerialReaders(PCSCLITE_CONFIG_DIR);
663 Log1(PCSC_LOG_INFO,
"pcsc-lite " VERSION
" daemon ready.");
673 int mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
675 f = open(PCSCLITE_RUN_PID, O_RDWR | O_CREAT, mode);
678 char pid[PID_ASCII_SIZE];
681 (void)snprintf(pid,
sizeof(pid),
"%u\n", (unsigned) getpid());
682 rr = write(f, pid, strlen(pid) + 1);
685 Log2(PCSC_LOG_CRITICAL,
686 "writing " PCSCLITE_RUN_PID
" failed: %s",
693 (void)fchmod(f, mode);
698 Log2(PCSC_LOG_CRITICAL,
"cannot create " PCSCLITE_RUN_PID
": %s",
710 (void)signal(SIGUSR1, signal_trap);
715 #ifdef USE_LIBSYSTEMD 717 rv = ListenExistingSocket(SD_LISTEN_FDS_START + 0);
724 Log1(PCSC_LOG_CRITICAL,
"Error initializing pcscd.");
731 rv = ContextsInitialize(customMaxThreadCounter, customMaxThreadCardHandles);
735 Log1(PCSC_LOG_CRITICAL,
"Error initializing pcscd.");
739 (void)signal(SIGPIPE, SIG_IGN);
740 (void)signal(SIGHUP, SIG_IGN);
743 #if !defined(PCSCLITE_STATIC_DRIVER) && defined(USE_USB) 747 rv = HPSearchHotPluggables();
753 rv = HPRegisterForHotplugEvents();
756 Log1(PCSC_LOG_ERROR,
"HPRegisterForHotplugEvents failed");
760 RFWaitForReaderInit();
770 rr = write(pipefd[1], &buf, 1);
773 Log2(PCSC_LOG_ERROR,
"write() failed: %s", strerror(errno));
781 Log1(PCSC_LOG_ERROR,
"SVCServiceRunLoop returned");
785 static void at_exit(
void)
787 Log1(PCSC_LOG_INFO,
"cleaning " PCSCLITE_IPC_DIR);
798 r = write(pipefd[1], &buf, 1);
801 Log2(PCSC_LOG_ERROR,
"write() failed: %s", strerror(errno));
809 static void clean_temp_files(
void)
813 if (!SocketActivated)
815 rv =
remove(PCSCLITE_CSOCK_NAME);
817 Log2(PCSC_LOG_ERROR,
"Cannot remove " PCSCLITE_CSOCK_NAME
": %s",
821 rv =
remove(PCSCLITE_RUN_PID);
823 Log2(PCSC_LOG_ERROR,
"Cannot remove " PCSCLITE_RUN_PID
": %s",
827 static void signal_trap(
int sig)
831 r = write(signal_handler_fd[1], &sig,
sizeof sig);
833 Log2(PCSC_LOG_ERROR,
"write failed: %s", strerror(errno));
836 static void print_version(
void)
838 printf(
"%s version %s.\n", PACKAGE, VERSION);
839 printf(
"Copyright (C) 1999-2002 by David Corcoran <corcoran@musclecard.com>.\n");
840 printf(
"Copyright (C) 2001-2018 by Ludovic Rousseau <ludovic.rousseau@free.fr>.\n");
841 printf(
"Copyright (C) 2003-2004 by Damien Sauveron <sauveron@labri.fr>.\n");
842 printf(
"Report bugs to <pcsclite-muscle@lists.alioth.debian.org>.\n");
844 printf (
"Enabled features:%s\n", PCSCLITE_FEATURES);
847 static void print_usage(
char const *
const progname)
849 printf(
"Usage: %s options\n", progname);
850 printf(
"Options:\n");
851 #ifdef HAVE_GETOPT_LONG 852 printf(
" -a, --apdu log APDU commands and results\n");
853 printf(
" -c, --config path to reader.conf\n");
854 printf(
" -f, --foreground run in foreground (no daemon),\n");
855 printf(
" send logs to stdout instead of syslog\n");
856 printf(
" -T, --color force use of colored logs\n");
857 printf(
" -h, --help display usage information\n");
858 printf(
" -H, --hotplug ask the daemon to rescan the available readers\n");
859 printf(
" -v, --version display the program version number\n");
860 printf(
" -d, --debug display lower level debug messages\n");
861 printf(
" -i, --info display info level debug messages\n");
862 printf(
" -e --error display error level debug messages (default level)\n");
863 printf(
" -C --critical display critical only level debug messages\n");
864 printf(
" --force-reader-polling ignore the IFD_GENERATE_HOTPLUG reader capability\n");
865 printf(
" -t, --max-thread maximum number of threads (default %d)\n", PCSC_MAX_CONTEXT_THREADS);
866 printf(
" -s, --max-card-handle-per-thread maximum number of card handle per thread (default: %d)\n", PCSC_MAX_CONTEXT_CARD_HANDLES);
867 printf(
" -r, --max-card-handle-per-reader maximum number of card handle per reader (default: %d)\n", PCSC_MAX_READER_HANDLES);
868 printf(
" -x, --auto-exit pcscd will quit after %d seconds of inactivity\n", TIME_BEFORE_SUICIDE);
869 printf(
" -S, --reader-name-no-serial do not include the USB serial number in the name\n");
870 printf(
" -I, --reader-name-no-interface do not include the USB interface name in the name\n");
872 printf(
" -a log APDU commands and results\n");
873 printf(
" -c path to reader.conf\n");
874 printf(
" -f run in foreground (no daemon), send logs to stdout instead of syslog\n");
875 printf(
" -T force use of colored logs\n");
876 printf(
" -d display debug messages.\n");
877 printf(
" -i display info messages.\n");
878 printf(
" -e display error messages (default level).\n");
879 printf(
" -C display critical messages.\n");
880 printf(
" -h display usage information\n");
881 printf(
" -H ask the daemon to rescan the available readers\n");
882 printf(
" -v display the program version number\n");
883 printf(
" -t maximum number of threads\n");
884 printf(
" -s maximum number of card handle per thread\n");
885 printf(
" -r maximum number of card handle per reader\n");
886 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)
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.