From 2bfeff8b3365740f678325b1b755fe34c10a83da Mon Sep 17 00:00:00 2001 From: oepen Date: Thu, 14 Oct 2010 12:34:40 +0000 Subject: [PATCH] picc_to_pcsc now runs as a daemon if the parameters verbose or debug are not defined git-svn-id: https://vsmartcard.svn.sourceforge.net/svnroot/vsmartcard@333 96b47cad-a561-4643-ad3b-153ac7d7599c --- picc_to_pcsc/src/picc_to_pcsc.c | 80 ++++++++++++++++++++++++++------- 1 file changed, 65 insertions(+), 15 deletions(-) diff --git a/picc_to_pcsc/src/picc_to_pcsc.c b/picc_to_pcsc/src/picc_to_pcsc.c index 3ff74a3..480c398 100644 --- a/picc_to_pcsc/src/picc_to_pcsc.c +++ b/picc_to_pcsc/src/picc_to_pcsc.c @@ -37,13 +37,49 @@ static FILE *picc_fd = NULL; /*filehandle used for PICCDEV*/ static LPSTR readers = NULL; static SCARDCONTEXT hContext = 0; static SCARDHANDLE hCard = 0; +static int verbose = 0, debug = 0; /* Forward declaration */ +static void daemonize(); static void cleanup_exit(int signo); static void cleanup(void); static size_t picc_decode_apdu(char *inbuf, size_t inlen, unsigned char **outbuf); static size_t picc_encode_rapdu(unsigned char *inbuf, size_t inlen, char **outbuf); +void daemonize() { + pid_t pid, sid; + + /*Fork and continue as child process*/ + pid = fork(); + if (pid < 0) + goto err; + if (pid > 0) /* Exit the parent */ + exit(0); + + umask(0); + + /* Create new session and set the process group ID */ + sid = setsid(); + if (sid < 0) + goto err; + + /* Change the current working directory. This prevents the current + directory from being locked; hence not being able to remove it. */ + if (chdir("/") < 0) + goto err; + + /* Redirect standard files to /dev/null */ + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); + + return; + +err: + fprintf(stderr, "Failed to start the daemon. Exiting.\n"); + exit(1); +} + void cleanup_exit(int signo){ cleanup(); exit(0); @@ -76,7 +112,8 @@ size_t picc_decode_apdu(char *inbuf, size_t inlen, unsigned char **outbuf) p = realloc(*outbuf, length); if (!p) { - fprintf(stderr, "Error allocating memory for decoded C-APDU\n"); + if (debug || verbose) + fprintf(stderr, "Error allocating memory for decoded C-APDU\n"); goto noapdu; } *outbuf = p; @@ -85,7 +122,8 @@ size_t picc_decode_apdu(char *inbuf, size_t inlen, unsigned char **outbuf) while(inbuf+inlen > end && length > pos) { b = strtoul(end, &end, 16); if (b > 0xff) { - fprintf(stderr, "%s:%u Error decoding C-APDU\n", __FILE__, __LINE__); + if (debug || verbose) + fprintf(stderr, "%s:%u Error decoding C-APDU\n", __FILE__, __LINE__); goto noapdu; } @@ -110,7 +148,8 @@ size_t picc_encode_rapdu(unsigned char *inbuf, size_t inlen, char **outbuf) length = 5+inlen*3+1; p = realloc(*outbuf, length); if (!p) { - fprintf(stderr, "Error allocating memory for encoded R-APDU\n"); + if (debug || verbose) + fprintf(stderr, "Error allocating memory for encoded R-APDU\n"); goto err; } *outbuf = p; @@ -149,7 +188,6 @@ int main (int argc, char **argv) ssize_t linelen; unsigned int readernum = 0; - int verbose = 0; struct sigaction new_sig, old_sig; @@ -157,31 +195,42 @@ int main (int argc, char **argv) if (argc > 1) { readernum = strtoul(argv[1], NULL, 10); if (argc > 2) { - if (0 != strcmp(argv[2], "verbose") || argc > 3) { + if (0 == strcmp(argv[2], "verbose")) + verbose++; + else if (0 == strcmp(argv[2], "debug")) + debug++; + else + goto parse_err; + if (argc > 3) { +parse_err: fprintf(stderr, "Usage: " - "%s [reader number] [verbose]\n", argv[0]); + "%s [reader number] [verbose | debug]\n", argv[0]); exit(2); } - verbose++; } } + if (!verbose && !debug) + daemonize(); /* Register signal handlers */ new_sig.sa_handler = cleanup_exit; sigemptyset(&new_sig.sa_mask); new_sig.sa_flags = SA_RESTART; - if (sigaction(SIGINT, &new_sig, &old_sig) < 0) + if ((sigaction(SIGINT, &new_sig, &old_sig) < 0) + || (sigaction(SIGTERM, &new_sig, &old_sig) < 0)) goto err; /* Open the device */ picc_fd = fopen(PICCDEV, "a+"); /*O_NOCTTY ?*/ if (!picc_fd) { - fprintf(stderr,"Error opening %s\n", PICCDEV); + if (debug || verbose) + fprintf(stderr,"Error opening %s\n", PICCDEV); goto err; } - printf("Connected to %s\n", PICCDEV); + if (debug || verbose) + printf("Connected to %s\n", PICCDEV); /* connect to reader and card */ @@ -196,7 +245,8 @@ int main (int argc, char **argv) linelen = getline(&read, &readlen, picc_fd); if (linelen < 0) { if (linelen < 0) { - fprintf(stderr,"Error reading from %s\n", PICCDEV); + if (debug || verbose) + fprintf(stderr,"Error reading from %s\n", PICCDEV); goto err; } } @@ -204,7 +254,7 @@ int main (int argc, char **argv) continue; fflush(picc_fd); - if (verbose) + if (debug) printf("%s\n", read); @@ -232,10 +282,10 @@ int main (int argc, char **argv) /* write R-APDU */ - if (verbose) - printf("INF: Writing R-APDU\n\n%s\n\n", buf); + if (debug) + printf("INF: Writing R-APDU\n\n%s\n\n", (char *) buf); - fprintf(picc_fd,"%s\r\n", buf); + fprintf(picc_fd,"%s\r\n", (char *) buf); fflush(picc_fd); }