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 char disable_polkit = FALSE;
85 static int pipefd[] = {-1, -1};
86 static int signal_handler_fd[] = {-1, -1};
87 char Add_Serial_In_Name = TRUE;
88 char Add_Interface_In_Name = TRUE;
93 static void at_exit(
void);
94 static void clean_temp_files(
void);
95 static void signal_trap(
int);
96 static void print_version(
void);
97 static void print_usage(
char const *
const);
119 (void)HPStopHotPluggables();
125 EHDeinitializeEventStructures();
126 ContextsDeinitialize();
134 Log2(PCSC_LOG_DEBUG,
"A new context thread creation is requested: %d", dwClientID);
138 Log1(PCSC_LOG_ERROR,
"Problem during the context thread creation");
150 Log1(PCSC_LOG_ERROR,
"Error in ProcessEventsServer");
164 Log2(PCSC_LOG_ERROR,
"ProcessEventsServer unknown retval: %d",
187 r = read(signal_handler_fd[0], &sig,
sizeof sig);
190 Log2(PCSC_LOG_ERROR,
"read failed: %s", strerror(errno));
194 Log2(PCSC_LOG_INFO,
"Received signal: %d", sig);
201 HPReCheckSerialReaders();
205 (void)signal(SIGUSR1, signal_trap);
214 Log1(PCSC_LOG_INFO,
"Direct suicide");
215 ExitValue = EXIT_SUCCESS;
222 ExitValue = EXIT_SUCCESS;
226 if (AraKiri == FALSE)
228 Log1(PCSC_LOG_INFO,
"Preparing for suicide");
236 Log1(PCSC_LOG_INFO,
"Suicide during init");
243 static int lives = 2;
249 Log1(PCSC_LOG_INFO,
"Forced suicide");
259 int main(
int argc,
char **argv)
262 char setToForeground;
264 char *newReaderConfig;
265 struct stat fStatBuf;
266 int customMaxThreadCounter = 0;
267 int customMaxReaderHandles = 0;
268 int customMaxThreadCardHandles = 0;
271 #ifdef HAVE_GETOPT_LONG
272 int option_index = 0;
273 static struct option long_options[] = {
274 {
"config", 1, NULL,
'c'},
275 {
"foreground", 0, NULL,
'f'},
276 {
"color", 0, NULL,
'T'},
277 {
"help", 0, NULL,
'h'},
278 {
"version", 0, NULL,
'v'},
279 {
"apdu", 0, NULL,
'a'},
280 {
"debug", 0, NULL,
'd'},
281 {
"info", 0, NULL,
'i'},
282 {
"error", 0, NULL,
'e'},
283 {
"critical", 0, NULL,
'C'},
284 {
"hotplug", 0, NULL,
'H'},
285 {
"force-reader-polling", optional_argument, NULL, 0},
286 {
"max-thread", 1, NULL,
't'},
287 {
"max-card-handle-per-thread", 1, NULL,
's'},
288 {
"max-card-handle-per-reader", 1, NULL,
'r'},
289 {
"auto-exit", 0, NULL,
'x'},
290 {
"reader-name-no-serial", 0, NULL,
'S'},
291 {
"reader-name-no-interface", 0, NULL,
'I'},
292 {
"disable-polkit", 0, NULL, 1},
296 #define OPT_STRING "c:fTdhvaieCHt:r:s:xSI"
298 newReaderConfig = NULL;
299 setToForeground = FALSE;
307 printf(
"BUILD ERROR: The release version number PCSCLITE_VERSION_NUMBER\n");
308 printf(
" in pcsclite.h (%s) does not match the release version number\n",
310 printf(
" generated in config.h (%s) (see configure.in).\n", VERSION);
322 DebugLogSetLogType(DEBUGLOG_SYSLOG_DEBUG);
327 #ifdef HAVE_GETOPT_LONG
328 while ((opt = getopt_long (argc, argv, OPT_STRING, long_options, &option_index)) != -1) {
330 while ((opt = getopt (argc, argv, OPT_STRING)) != -1) {
333 #ifdef HAVE_GETOPT_LONG
335 if (strcmp(long_options[option_index].name,
336 "force-reader-polling") == 0)
337 HPForceReaderPolling = optarg ? abs(atoi(optarg)) : 1;
340 if (strcmp(long_options[option_index].name,
341 "disable-polkit") == 0)
342 disable_polkit = TRUE;
346 Log2(PCSC_LOG_INFO,
"using new config file: %s", optarg);
347 newReaderConfig = optarg;
351 setToForeground = TRUE;
353 DebugLogSetLogType(DEBUGLOG_STDOUT_DEBUG);
355 "pcscd set to foreground with debug send to stdout");
359 DebugLogSetLogType(DEBUGLOG_STDOUT_COLOR_DEBUG);
360 Log1(PCSC_LOG_INFO,
"Force colored logs");
364 DebugLogSetLevel(PCSC_LOG_DEBUG);
368 DebugLogSetLevel(PCSC_LOG_INFO);
372 DebugLogSetLevel(PCSC_LOG_ERROR);
376 DebugLogSetLevel(PCSC_LOG_CRITICAL);
380 print_usage (argv[0]);
388 DebugLogSetCategory(DEBUG_CATEGORY_APDU);
393 DebugLogSetLogType(DEBUGLOG_STDOUT_DEBUG);
398 customMaxThreadCounter = optarg ? atoi(optarg) : 0;
399 Log2(PCSC_LOG_INFO,
"setting customMaxThreadCounter to: %d",
400 customMaxThreadCounter);
404 customMaxReaderHandles = optarg ? atoi(optarg) : 0;
405 Log2(PCSC_LOG_INFO,
"setting customMaxReaderHandles to: %d",
406 customMaxReaderHandles);
410 customMaxThreadCardHandles = optarg ? atoi(optarg) : 0;
411 Log2(PCSC_LOG_INFO,
"setting customMaxThreadCardHandles to: %d",
412 customMaxThreadCardHandles);
417 Log2(PCSC_LOG_INFO,
"Auto exit after %d seconds of inactivity",
418 TIME_BEFORE_SUICIDE);
422 Add_Serial_In_Name = FALSE;
426 Add_Interface_In_Name = FALSE;
430 print_usage (argv[0]);
438 printf(
"Unknown option: %s\n", argv[optind]);
439 print_usage(argv[0]);
443 #ifdef USE_LIBSYSTEMD
447 rv = sd_listen_fds(0);
450 Log1(PCSC_LOG_CRITICAL,
"Too many file descriptors received");
457 SocketActivated = TRUE;
458 Log1(PCSC_LOG_INFO,
"Started by systemd");
461 SocketActivated = FALSE;
469 rv = stat(PCSCLITE_CSOCK_NAME, &fStatBuf);
472 if (rv == 0 && !SocketActivated)
479 pid = GetDaemonPid();
484 return SendHotplugSignal();
489 Log1(PCSC_LOG_CRITICAL,
490 "file " PCSCLITE_CSOCK_NAME
" already exists.");
491 Log2(PCSC_LOG_CRITICAL,
492 "Another pcscd (pid: %ld) seems to be running.", (
long)pid);
504 Log2(PCSC_LOG_CRITICAL,
"kill failed: %s", strerror(errno));
512 Log1(PCSC_LOG_CRITICAL,
"file " PCSCLITE_RUN_PID
" do not exist");
513 Log1(PCSC_LOG_CRITICAL,
"Hotplug failed");
521 Log1(PCSC_LOG_CRITICAL,
"Hotplug failed: pcscd is not running");
530 Log2(PCSC_LOG_CRITICAL,
"chdir() failed: %s", strerror(errno));
537 if (!setToForeground)
542 if (pipe(pipefd) == -1)
544 Log2(PCSC_LOG_CRITICAL,
"pipe() failed: %s", strerror(errno));
551 Log2(PCSC_LOG_CRITICAL,
"fork() failed: %s", strerror(errno));
557 fd = open(
"/dev/null", O_RDWR);
560 dup2(fd, STDIN_FILENO);
561 dup2(fd, STDOUT_FILENO);
562 dup2(fd, STDERR_FILENO);
579 ret = read(pipefd[0], &buf, 1);
600 (void)signal(SIGQUIT, signal_trap);
601 (void)signal(SIGTERM, signal_trap);
602 (void)signal(SIGINT, signal_trap);
605 (void)signal(SIGALRM, signal_trap);
607 if (pipe(signal_handler_fd) == -1)
609 Log2(PCSC_LOG_CRITICAL,
"pipe() failed: %s", strerror(errno));
613 pthread_t signal_handler_thread;
614 rv = pthread_create(&signal_handler_thread, NULL,
signal_thread, NULL);
617 Log2(PCSC_LOG_CRITICAL,
"pthread_create failed: %s", strerror(rv));
625 int mode = S_IROTH | S_IXOTH | S_IRGRP | S_IXGRP | S_IRWXU;
627 rv = mkdir(PCSCLITE_IPC_DIR, mode);
628 if ((rv != 0) && (errno != EEXIST))
630 Log2(PCSC_LOG_CRITICAL,
631 "cannot create " PCSCLITE_IPC_DIR
": %s", strerror(errno));
638 (void)chmod(PCSCLITE_IPC_DIR, mode);
644 rv = RFAllocateReaderSpace(customMaxReaderHandles);
654 rv = RFStartSerialReaders(newReaderConfig);
657 Log3(PCSC_LOG_CRITICAL,
"invalid file %s: %s", newReaderConfig,
664 rv = RFStartSerialReaders(PCSCLITE_CONFIG_DIR);
670 Log1(PCSC_LOG_INFO,
"pcsc-lite " VERSION
" daemon ready.");
680 int mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
682 f = open(PCSCLITE_RUN_PID, O_RDWR | O_CREAT, mode);
685 char pid[PID_ASCII_SIZE];
688 (void)snprintf(pid,
sizeof(pid),
"%u\n", (unsigned) getpid());
689 rr = write(f, pid, strlen(pid) + 1);
692 Log2(PCSC_LOG_CRITICAL,
693 "writing " PCSCLITE_RUN_PID
" failed: %s",
700 (void)fchmod(f, mode);
705 Log2(PCSC_LOG_CRITICAL,
"cannot create " PCSCLITE_RUN_PID
": %s",
717 (void)signal(SIGUSR1, signal_trap);
722 #ifdef USE_LIBSYSTEMD
724 rv = ListenExistingSocket(SD_LISTEN_FDS_START + 0);
731 Log1(PCSC_LOG_CRITICAL,
"Error initializing pcscd.");
738 rv = ContextsInitialize(customMaxThreadCounter, customMaxThreadCardHandles);
742 Log1(PCSC_LOG_CRITICAL,
"Error initializing pcscd.");
746 (void)signal(SIGPIPE, SIG_IGN);
747 (void)signal(SIGHUP, SIG_IGN);
750 #if !defined(PCSCLITE_STATIC_DRIVER) && defined(USE_USB)
754 rv = HPSearchHotPluggables();
760 rv = HPRegisterForHotplugEvents();
763 Log1(PCSC_LOG_ERROR,
"HPRegisterForHotplugEvents failed");
767 RFWaitForReaderInit();
777 rr = write(pipefd[1], &buf, 1);
780 Log2(PCSC_LOG_ERROR,
"write() failed: %s", strerror(errno));
788 Log1(PCSC_LOG_ERROR,
"SVCServiceRunLoop returned");
792 static void at_exit(
void)
794 Log1(PCSC_LOG_INFO,
"cleaning " PCSCLITE_IPC_DIR);
805 r = write(pipefd[1], &buf, 1);
808 Log2(PCSC_LOG_ERROR,
"write() failed: %s", strerror(errno));
816 static void clean_temp_files(
void)
820 if (!SocketActivated)
822 rv = remove(PCSCLITE_CSOCK_NAME);
824 Log2(PCSC_LOG_ERROR,
"Cannot remove " PCSCLITE_CSOCK_NAME
": %s",
828 rv = remove(PCSCLITE_RUN_PID);
830 Log2(PCSC_LOG_ERROR,
"Cannot remove " PCSCLITE_RUN_PID
": %s",
834 static void signal_trap(
int sig)
838 r = write(signal_handler_fd[1], &sig,
sizeof sig);
840 Log2(PCSC_LOG_ERROR,
"write failed: %s", strerror(errno));
843 static void print_version(
void)
845 printf(
"%s version %s.\n", PACKAGE, VERSION);
846 printf(
"Copyright (C) 1999-2002 by David Corcoran <corcoran@musclecard.com>.\n");
847 printf(
"Copyright (C) 2001-2018 by Ludovic Rousseau <ludovic.rousseau@free.fr>.\n");
848 printf(
"Copyright (C) 2003-2004 by Damien Sauveron <sauveron@labri.fr>.\n");
849 printf(
"Report bugs to <pcsclite-muscle@lists.infradead.org>.\n");
851 printf (
"Enabled features:%s\n", PCSCLITE_FEATURES);
854 static void print_usage(
char const *
const progname)
856 printf(
"Usage: %s options\n", progname);
857 printf(
"Options:\n");
858 #ifdef HAVE_GETOPT_LONG
859 printf(
" -a, --apdu log APDU commands and results\n");
860 printf(
" -c, --config path to reader.conf\n");
861 printf(
" -f, --foreground run in foreground (no daemon),\n");
862 printf(
" send logs to stdout instead of syslog\n");
863 printf(
" -T, --color force use of colored logs\n");
864 printf(
" -h, --help display usage information\n");
865 printf(
" -H, --hotplug ask the daemon to rescan the available readers\n");
866 printf(
" -v, --version display the program version number\n");
867 printf(
" -d, --debug display lower level debug messages\n");
868 printf(
" -i, --info display info level debug messages\n");
869 printf(
" -e --error display error level debug messages (default level)\n");
870 printf(
" -C --critical display critical only level debug messages\n");
871 printf(
" --force-reader-polling ignore the IFD_GENERATE_HOTPLUG reader capability\n");
872 printf(
" -t, --max-thread maximum number of threads (default %d)\n", PCSC_MAX_CONTEXT_THREADS);
873 printf(
" -s, --max-card-handle-per-thread maximum number of card handle per thread (default: %d)\n", PCSC_MAX_CONTEXT_CARD_HANDLES);
874 printf(
" -r, --max-card-handle-per-reader maximum number of card handle per reader (default: %d)\n", PCSC_MAX_READER_HANDLES);
875 printf(
" -x, --auto-exit pcscd will quit after %d seconds of inactivity\n", TIME_BEFORE_SUICIDE);
876 printf(
" -S, --reader-name-no-serial do not include the USB serial number in the name\n");
877 printf(
" -I, --reader-name-no-interface do not include the USB interface name in the name\n");
878 printf(
" --disable-polkit disable polkit support\n");
880 printf(
" -a log APDU commands and results\n");
881 printf(
" -c path to reader.conf\n");
882 printf(
" -f run in foreground (no daemon), send logs to stdout instead of syslog\n");
883 printf(
" -T force use of colored logs\n");
884 printf(
" -d display debug messages.\n");
885 printf(
" -i display info messages.\n");
886 printf(
" -e display error messages (default level).\n");
887 printf(
" -C display critical messages.\n");
888 printf(
" -h display usage information\n");
889 printf(
" -H ask the daemon to rescan the available readers\n");
890 printf(
" -v display the program version number\n");
891 printf(
" -t maximum number of threads\n");
892 printf(
" -s maximum number of card handle per thread\n");
893 printf(
" -r maximum number of card handle per reader\n");
894 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.
This keeps a list of defines for pcsc-lite.
static void SVCServiceRunLoop(void)
The Server's Message Queue Listener function.
char AutoExit
Represents an Application Context on the Server side.
static void * signal_thread(void *arg)
thread dedicated to handle signals
This keeps a list of defines for pcsc-lite.
#define PCSCLITE_VERSION_NUMBER
Current version.
This keeps track of a list of currently available reader structures.
This handles abstract system level calls.
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.