$OpenBSD: patch-src-pcscdaemon_c,v 1.2 2004/01/02 22:53:09 espie Exp $ --- src/pcscdaemon.c.orig 2001-11-07 23:54:30.000000000 +0100 +++ src/pcscdaemon.c 2004-01-02 23:47:08.000000000 +0100 @@ -26,9 +26,17 @@ #include #include +#ifdef USE_DAEMON +#ifndef USE_SYSLOG +#error You must use '--enable-syslog' when also using '--enable-daemon' or you will not get any message +#endif +#endif + /* Some internal functions */ void SVCServiceRunLoop(); void SVCClientCleanup( psharedSegmentMsg ); +void at_exit(void); +void at_exit_signal(int); extern int errno; @@ -55,9 +63,9 @@ void SVCServiceRunLoop() { rsp = SHMInitializeCommonSegment(); if ( rsp == -1 ) { - DebugLogA("SVCServiceRunLoop: Error initializing pcscd.\n", + DebugLogA("SVCServiceRunLoop: Error initializing pcscd.", __FILE__, __LINE__); - SYS_Exit(-1); + exit(-1); } /* Solaris sends a SIGALRM and it is annoying */ @@ -82,8 +90,7 @@ void SVCServiceRunLoop() { MSGCleanupClient( &msgStruct ); SYS_MutexUnLock(&usbNotifierMutex); snprintf(errMessage, sizeof(errMessage), "%s%d%s", - "SVCServiceRun: Client ", msgStruct.request_id, - " has disappeared.\n" ); + "SVCServiceRun: Client ", msgStruct.request_id, " has disappeared." ); #ifdef PCSC_DEBUG DebugLogA(errMessage, __FILE__, __LINE__); #endif @@ -108,8 +115,7 @@ void SVCServiceRunLoop() { case -1: #ifdef PCSC_DEBUG - DebugLogA("SVCServiceRun: Error in ProcessEvents.\n", - __FILE__, __LINE__); + DebugLogA("SVCServiceRun: Error in ProcessEvents.", __FILE__, __LINE__); #endif break; @@ -137,8 +143,7 @@ int main(int argc, char **argv) { printf("pcscd -help - This help menu\n"); return 0; } else if ( argc == 3 && (strcmp(argv[1], "-c") == 0) ) { - DebugLogC("main: Using new config file: %s\n", argv[2], - __FILE__, __LINE__); + DebugLogC("main: Using new config file: %s", argv[2], __FILE__, __LINE__); newReaderConfig = argv[2]; } else if ( argc == 1 ) { /* All OK Here */ @@ -150,7 +155,7 @@ int main(int argc, char **argv) { #ifdef USE_DAEMON /* standard daemonizing actions */ #ifndef HAVE_DAEMON - switch (fork()) { + switch (SYS_Fork()) { case -1: return (-1); case 0: @@ -167,22 +172,47 @@ int main(int argc, char **argv) { #endif #endif - /* Create the /tmp/pcsc directory and chmod it */ - rv = SYS_Mkdir("/tmp/pcsc", S_ISVTX | S_IRWXO | S_IRWXG | S_IRWXU ); + /* test the presence of /tmp/pcsc */ + { + struct stat buf; + + rv = SYS_Stat(PCSCLITE_IPC_DIR, &buf); + if (rv == 0) + { #ifdef PCSC_DEBUG + DebugLogA("main: directory " PCSCLITE_IPC_DIR " already exists", + __FILE__, __LINE__); + return 1; +#endif + } + + } + + /* cleanly remove /tmp/pcsc when exiting */ + atexit(at_exit); + signal(SIGQUIT, at_exit_signal); + signal(SIGTERM, at_exit_signal); + signal(SIGINT, at_exit_signal); + signal(SIGHUP, at_exit_signal); + + /* Create the /tmp/pcsc directory and chmod it */ + rv = SYS_Mkdir(PCSCLITE_IPC_DIR, S_ISVTX | S_IRWXO | S_IRWXG | S_IRWXU ); if ( rv != 0 ) { - DebugLogC("main: Cannot create /tmp/pcsc: %s\n", strerror(errno), +#ifdef PCSC_DEBUG + DebugLogC("main: Cannot create " PCSCLITE_IPC_DIR ": %s", strerror(errno), __FILE__, __LINE__); - } #endif + return 1; + } - rv = SYS_Chmod("/tmp/pcsc", S_ISVTX | S_IRWXO | S_IRWXG | S_IRWXU ); -#ifdef PCSC_DEBUG + rv = SYS_Chmod(PCSCLITE_IPC_DIR, S_ISVTX | S_IRWXO | S_IRWXG | S_IRWXU ); if ( rv != 0 ) { - DebugLogC("main: Cannot chmod /tmp/pcsc: %s\n", strerror(errno), +#ifdef PCSC_DEBUG + DebugLogC("main: Cannot chmod " PCSCLITE_IPC_DIR ": %s", strerror(errno), __FILE__, __LINE__); - } #endif + return 1; + } /* Allocate memory for reader structures */ RFAllocateReaderSpace( PCSCLITE_MAX_CONTEXTS ); @@ -199,13 +229,58 @@ int main(int argc, char **argv) { g_rgSCardT1Pci.dwProtocol = SCARD_PROTOCOL_T1; g_rgSCardRawPci.dwProtocol = SCARD_PROTOCOL_RAW; - DebugLogA("main: PC/SC Lite Daemon Ready.\n", __FILE__, __LINE__); + DebugLogA("main: PC/SC Lite Daemon Ready.", __FILE__, __LINE__); SVCServiceRunLoop(); + #ifdef USE_SYSLOG - syslog( LOG_ERR, "pcscdaemon.c: main: SVCServiceRunLoop returned\n" ); + syslog( LOG_ERR, "pcscdaemon.c: main: SVCServiceRunLoop returned" ); #else - fprintf(stderr, "pcscdaemon.c: main: SVCServiceRunLoop returned\n"); + fprintf(stderr, "pcscdaemon.c: main: SVCServiceRunLoop returned"); #endif return 1; } + +void at_exit(void) +{ + int rv; + + DebugLogA("at_exit: cleaning " PCSCLITE_IPC_DIR, __FILE__, __LINE__); + + rv = SYS_Unlink(PCSCLITE_SHM_FILE); +#ifdef PCSC_DEBUG + if ( rv != 0 ) + DebugLogC("main: Cannot unlink " PCSCLITE_SHM_FILE ": %s", + strerror(errno), __FILE__, __LINE__); +#endif + + rv = SYS_Unlink(PCSCLITE_PUBSHM_FILE); +#ifdef PCSC_DEBUG + if ( rv != 0 ) + DebugLogC("main: Cannot unlink " PCSCLITE_PUBSHM_FILE ": %s", + strerror(errno), __FILE__, __LINE__); +#endif + + rv = SYS_Unlink(PCSCLITE_CSOCK_NAME); +#ifdef PCSC_DEBUG + if ( rv != 0 ) + DebugLogC("main: Cannot unlink " PCSCLITE_CSOCK_NAME ": %s", + strerror(errno), __FILE__, __LINE__); +#endif + + rv = SYS_Rmdir(PCSCLITE_IPC_DIR); +#ifdef PCSC_DEBUG + if ( rv != 0 ) + DebugLogC("main: Cannot rmdir " PCSCLITE_IPC_DIR ": %s", + strerror(errno), __FILE__, __LINE__); +#endif + + SYS_Exit(1); +} + +void at_exit_signal(int sig) +{ + /* will call at_exit() */ + exit(0); +} +