45 #include <sys/types.h> 61 #include "sd-daemon.h" 67 #include "configfile.h" 77 static char Init = TRUE;
79 char SocketActivated = FALSE;
80 static int ExitValue = EXIT_FAILURE;
81 int HPForceReaderPolling = 0;
82 static int pipefd[] = {-1, -1};
83 static int signal_handler_fd[] = {-1, -1};
84 char Add_Serial_In_Name = TRUE;
85 char Add_Interface_In_Name = TRUE;
90 static void at_exit(
void);
91 static void clean_temp_files(
void);
92 static void signal_trap(
int);
93 static void print_version(
void);
94 static void print_usage(
char const *
const);
116 (void)HPStopHotPluggables();
122 EHDeinitializeEventStructures();
123 ContextsDeinitialize();
131 Log2(PCSC_LOG_DEBUG,
"A new context thread creation is requested: %d", dwClientID);
135 Log1(PCSC_LOG_ERROR,
"Problem during the context thread creation");
147 Log1(PCSC_LOG_ERROR,
"Error in ProcessEventsServer");
161 Log2(PCSC_LOG_ERROR,
"ProcessEventsServer unknown retval: %d",
184 r = read(signal_handler_fd[0], &sig,
sizeof sig);
187 Log2(PCSC_LOG_ERROR,
"read failed: %s", strerror(errno));
191 Log2(PCSC_LOG_INFO,
"Received signal: %d", sig);
198 HPReCheckSerialReaders();
202 (void)signal(SIGUSR1, signal_trap);
211 Log1(PCSC_LOG_INFO,
"Direct suicide");
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");
255 int main(
int argc,
char **argv)
258 char setToForeground;
260 char *newReaderConfig;
261 struct stat fStatBuf;
262 int customMaxThreadCounter = 0;
263 int customMaxReaderHandles = 0;
264 int customMaxThreadCardHandles = 0;
267 #ifdef HAVE_GETOPT_LONG 268 int option_index = 0;
269 static struct option long_options[] = {
270 {
"config", 1, NULL,
'c'},
271 {
"foreground", 0, NULL,
'f'},
272 {
"color", 0, NULL,
'T'},
273 {
"help", 0, NULL,
'h'},
274 {
"version", 0, NULL,
'v'},
275 {
"apdu", 0, NULL,
'a'},
276 {
"debug", 0, NULL,
'd'},
277 {
"info", 0, NULL,
'i'},
278 {
"error", 0, NULL,
'e'},
279 {
"critical", 0, NULL,
'C'},
280 {
"hotplug", 0, NULL,
'H'},
281 {
"force-reader-polling", optional_argument, NULL, 0},
282 {
"max-thread", 1, NULL,
't'},
283 {
"max-card-handle-per-thread", 1, NULL,
's'},
284 {
"max-card-handle-per-reader", 1, NULL,
'r'},
285 {
"auto-exit", 0, NULL,
'x'},
286 {
"reader-name-no-serial", 0, NULL,
'S'},
287 {
"reader-name-no-interface", 0, NULL,
'I'},
291 #define OPT_STRING "c:fTdhvaieCHt:r:s:xSI" 293 newReaderConfig = NULL;
294 setToForeground = FALSE;
302 printf(
"BUILD ERROR: The release version number PCSCLITE_VERSION_NUMBER\n");
303 printf(
" in pcsclite.h (%s) does not match the release version number\n",
305 printf(
" generated in config.h (%s) (see configure.in).\n", VERSION);
317 DebugLogSetLogType(DEBUGLOG_SYSLOG_DEBUG);
322 #ifdef HAVE_GETOPT_LONG 323 while ((opt = getopt_long (argc, argv, OPT_STRING, long_options, &option_index)) != -1) {
325 while ((opt = getopt (argc, argv, OPT_STRING)) != -1) {
328 #ifdef HAVE_GETOPT_LONG 330 if (strcmp(long_options[option_index].name,
331 "force-reader-polling") == 0)
332 HPForceReaderPolling = optarg ? abs(atoi(optarg)) : 1;
336 Log2(PCSC_LOG_INFO,
"using new config file: %s", optarg);
337 newReaderConfig = optarg;
341 setToForeground = TRUE;
343 DebugLogSetLogType(DEBUGLOG_STDOUT_DEBUG);
345 "pcscd set to foreground with debug send to stdout");
349 DebugLogSetLogType(DEBUGLOG_STDOUT_COLOR_DEBUG);
350 Log1(PCSC_LOG_INFO,
"Force colored logs");
354 DebugLogSetLevel(PCSC_LOG_DEBUG);
358 DebugLogSetLevel(PCSC_LOG_INFO);
362 DebugLogSetLevel(PCSC_LOG_ERROR);
366 DebugLogSetLevel(PCSC_LOG_CRITICAL);
370 print_usage (argv[0]);
378 DebugLogSetCategory(DEBUG_CATEGORY_APDU);
383 DebugLogSetLogType(DEBUGLOG_STDOUT_DEBUG);
388 customMaxThreadCounter = optarg ? atoi(optarg) : 0;
389 Log2(PCSC_LOG_INFO,
"setting customMaxThreadCounter to: %d",
390 customMaxThreadCounter);
394 customMaxReaderHandles = optarg ? atoi(optarg) : 0;
395 Log2(PCSC_LOG_INFO,
"setting customMaxReaderHandles to: %d",
396 customMaxReaderHandles);
400 customMaxThreadCardHandles = optarg ? atoi(optarg) : 0;
401 Log2(PCSC_LOG_INFO,
"setting customMaxThreadCardHandles to: %d",
402 customMaxThreadCardHandles);
407 Log2(PCSC_LOG_INFO,
"Auto exit after %d seconds of inactivity",
408 TIME_BEFORE_SUICIDE);
412 Add_Serial_In_Name = FALSE;
416 Add_Interface_In_Name = FALSE;
420 print_usage (argv[0]);
428 printf(
"Unknown option: %s\n", argv[optind]);
429 print_usage(argv[0]);
436 rv = sd_listen_fds(0);
439 Log1(PCSC_LOG_CRITICAL,
"Too many file descriptors received");
446 SocketActivated = TRUE;
447 Log1(PCSC_LOG_INFO,
"Started by systemd");
450 SocketActivated = FALSE;
457 rv = stat(PCSCLITE_CSOCK_NAME, &fStatBuf);
460 if (rv == 0 && !SocketActivated)
467 pid = GetDaemonPid();
472 return SendHotplugSignal();
477 Log1(PCSC_LOG_CRITICAL,
478 "file " PCSCLITE_CSOCK_NAME
" already exists.");
479 Log2(PCSC_LOG_CRITICAL,
480 "Another pcscd (pid: %ld) seems to be running.", (
long)pid);
492 Log2(PCSC_LOG_CRITICAL,
"kill failed: %s", strerror(errno));
500 Log1(PCSC_LOG_CRITICAL,
"file " PCSCLITE_RUN_PID
" do not exist");
501 Log1(PCSC_LOG_CRITICAL,
"Hotplug failed");
509 Log1(PCSC_LOG_CRITICAL,
"Hotplug failed: pcscd is not running");
518 Log2(PCSC_LOG_CRITICAL,
"chdir() failed: %s", strerror(errno));
525 if (!setToForeground)
530 if (pipe(pipefd) == -1)
532 Log2(PCSC_LOG_CRITICAL,
"pipe() failed: %s", strerror(errno));
539 Log2(PCSC_LOG_CRITICAL,
"fork() failed: %s", strerror(errno));
545 fd = open(
"/dev/null", O_RDWR);
548 dup2(fd, STDIN_FILENO);
549 dup2(fd, STDOUT_FILENO);
550 dup2(fd, STDERR_FILENO);
567 ret = read(pipefd[0], &buf, 1);
588 (void)signal(SIGQUIT, signal_trap);
589 (void)signal(SIGTERM, signal_trap);
590 (void)signal(SIGINT, signal_trap);
593 (void)signal(SIGALRM, signal_trap);
595 if (pipe(signal_handler_fd) == -1)
597 Log2(PCSC_LOG_CRITICAL,
"pipe() failed: %s", strerror(errno));
601 pthread_t signal_handler_thread;
602 rv = pthread_create(&signal_handler_thread, NULL,
signal_thread, NULL);
605 Log2(PCSC_LOG_CRITICAL,
"pthread_create failed: %s", strerror(rv));
613 int mode = S_IROTH | S_IXOTH | S_IRGRP | S_IXGRP | S_IRWXU;
615 rv = mkdir(PCSCLITE_IPC_DIR, mode);
616 if ((rv != 0) && (errno != EEXIST))
618 Log2(PCSC_LOG_CRITICAL,
619 "cannot create " PCSCLITE_IPC_DIR
": %s", strerror(errno));
626 (void)chmod(PCSCLITE_IPC_DIR, mode);
632 rv = RFAllocateReaderSpace(customMaxReaderHandles);
642 rv = RFStartSerialReaders(newReaderConfig);
645 Log3(PCSC_LOG_CRITICAL,
"invalid file %s: %s", newReaderConfig,
652 rv = RFStartSerialReaders(PCSCLITE_CONFIG_DIR);
658 Log1(PCSC_LOG_INFO,
"pcsc-lite " VERSION
" daemon ready.");
668 int mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
670 f = open(PCSCLITE_RUN_PID, O_RDWR | O_CREAT, mode);
673 char pid[PID_ASCII_SIZE];
676 (void)snprintf(pid,
sizeof(pid),
"%u\n", (unsigned) getpid());
677 rr = write(f, pid, strlen(pid) + 1);
680 Log2(PCSC_LOG_CRITICAL,
681 "writing " PCSCLITE_RUN_PID
" failed: %s",
689 (void)chmod(PCSCLITE_RUN_PID, mode);
692 Log2(PCSC_LOG_CRITICAL,
"cannot create " PCSCLITE_RUN_PID
": %s",
704 (void)signal(SIGUSR1, signal_trap);
716 Log1(PCSC_LOG_CRITICAL,
"Error initializing pcscd.");
723 rv = ContextsInitialize(customMaxThreadCounter, customMaxThreadCardHandles);
727 Log1(PCSC_LOG_CRITICAL,
"Error initializing pcscd.");
731 (void)signal(SIGPIPE, SIG_IGN);
732 (void)signal(SIGHUP, SIG_IGN);
735 #if !defined(PCSCLITE_STATIC_DRIVER) && defined(USE_USB) 739 rv = HPSearchHotPluggables();
745 rv = HPRegisterForHotplugEvents();
748 Log1(PCSC_LOG_ERROR,
"HPRegisterForHotplugEvents failed");
752 RFWaitForReaderInit();
762 rr = write(pipefd[1], &buf, 1);
765 Log2(PCSC_LOG_ERROR,
"write() failed: %s", strerror(errno));
773 Log1(PCSC_LOG_ERROR,
"SVCServiceRunLoop returned");
777 static void at_exit(
void)
779 Log1(PCSC_LOG_INFO,
"cleaning " PCSCLITE_IPC_DIR);
790 r = write(pipefd[1], &buf, 1);
793 Log2(PCSC_LOG_ERROR,
"write() failed: %s", strerror(errno));
801 static void clean_temp_files(
void)
805 if (!SocketActivated)
807 rv =
remove(PCSCLITE_CSOCK_NAME);
809 Log2(PCSC_LOG_ERROR,
"Cannot remove " PCSCLITE_CSOCK_NAME
": %s",
813 rv =
remove(PCSCLITE_RUN_PID);
815 Log2(PCSC_LOG_ERROR,
"Cannot remove " PCSCLITE_RUN_PID
": %s",
819 static void signal_trap(
int sig)
823 r = write(signal_handler_fd[1], &sig,
sizeof sig);
825 Log2(PCSC_LOG_ERROR,
"write failed: %s", strerror(errno));
828 static void print_version(
void)
830 printf(
"%s version %s.\n", PACKAGE, VERSION);
831 printf(
"Copyright (C) 1999-2002 by David Corcoran <corcoran@musclecard.com>.\n");
832 printf(
"Copyright (C) 2001-2015 by Ludovic Rousseau <ludovic.rousseau@free.fr>.\n");
833 printf(
"Copyright (C) 2003-2004 by Damien Sauveron <sauveron@labri.fr>.\n");
834 printf(
"Report bugs to <pcsclite-muscle@lists.alioth.debian.org>.\n");
836 printf (
"Enabled features:%s\n", PCSCLITE_FEATURES);
839 static void print_usage(
char const *
const progname)
841 printf(
"Usage: %s options\n", progname);
842 printf(
"Options:\n");
843 #ifdef HAVE_GETOPT_LONG 844 printf(
" -a, --apdu log APDU commands and results\n");
845 printf(
" -c, --config path to reader.conf\n");
846 printf(
" -f, --foreground run in foreground (no daemon),\n");
847 printf(
" send logs to stdout instead of syslog\n");
848 printf(
" -T, --color force use of colored logs\n");
849 printf(
" -h, --help display usage information\n");
850 printf(
" -H, --hotplug ask the daemon to rescan the available readers\n");
851 printf(
" -v, --version display the program version number\n");
852 printf(
" -d, --debug display lower level debug messages\n");
853 printf(
" -i, --info display info level debug messages\n");
854 printf(
" -e --error display error level debug messages (default level)\n");
855 printf(
" -C --critical display critical only level debug messages\n");
856 printf(
" --force-reader-polling ignore the IFD_GENERATE_HOTPLUG reader capability\n");
857 printf(
" -t, --max-thread maximum number of threads (default %d)\n", PCSC_MAX_CONTEXT_THREADS);
858 printf(
" -s, --max-card-handle-per-thread maximum number of card handle per thread (default: %d)\n", PCSC_MAX_CONTEXT_CARD_HANDLES);
859 printf(
" -r, --max-card-handle-per-reader maximum number of card handle per reader (default: %d)\n", PCSC_MAX_READER_HANDLES);
860 printf(
" -x, --auto-exit pcscd will quit after %d seconds of inactivity\n", TIME_BEFORE_SUICIDE);
861 printf(
" -S, --reader-name-no-serial do not include the USB serial number in the name\n");
862 printf(
" -I, --reader-name-no-interface do not include the USB interface name in the name\n");
864 printf(
" -a log APDU commands and results\n");
865 printf(
" -c path to reader.conf\n");
866 printf(
" -f run in foreground (no daemon), send logs to stdout instead of syslog\n");
867 printf(
" -T force use of colored logs\n");
868 printf(
" -d display debug messages.\n");
869 printf(
" -i display info messages.\n");
870 printf(
" -e display error messages (default level).\n");
871 printf(
" -C display critical messages.\n");
872 printf(
" -h display usage information\n");
873 printf(
" -H ask the daemon to rescan the available readers\n");
874 printf(
" -v display the program version number\n");
875 printf(
" -t maximum number of threads\n");
876 printf(
" -s maximum number of card handle per thread\n");
877 printf(
" -r maximum number of card handle per reader\n");
878 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.
INTERNAL int32_t ListenExistingSocket(int fd)
Acquires a socket passed in from systemd.
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.