From c0f16a32a31c460dc507ffca0334bbb4f5c413db Mon Sep 17 00:00:00 2001 From: frankmorgner Date: Thu, 11 Nov 2010 09:51:48 +0000 Subject: [PATCH] added more error debugging git-svn-id: https://vsmartcard.svn.sourceforge.net/svnroot/vsmartcard@355 96b47cad-a561-4643-ad3b-153ac7d7599c --- pcsc-relay/src/lnfc.c | 6 ++---- pcsc-relay/src/opicc.c | 25 ++++++++++++------------- pcsc-relay/src/pcsc-relay.c | 30 ++++++++++++++++++++++-------- pcsc-relay/src/pcsc-relay.h | 6 ++++-- 4 files changed, 40 insertions(+), 27 deletions(-) diff --git a/pcsc-relay/src/lnfc.c b/pcsc-relay/src/lnfc.c index 6a75e20..3b7793f 100644 --- a/pcsc-relay/src/lnfc.c +++ b/pcsc-relay/src/lnfc.c @@ -167,8 +167,7 @@ static int lnfc_receive_capdu(void *driver_data, // Receive external reader command through target if (!nfc_target_receive_bytes(data->pndTarget, data->abtCapdu, &data->szCapduLen)) { - if (verbose >= 0) - nfc_perror (data->pndTarget, "nfc_target_receive_bytes"); + ERROR ("nfc_target_receive_bytes: %s\n", nfc_strerror(data->pndTarget)); return 0; } @@ -196,8 +195,7 @@ static int lnfc_send_rapdu(void *driver_data, if (!nfc_target_send_bytes(data->pndTarget, rapdu, len)) { - if (verbose >= 0) - nfc_perror (data->pndTarget, "nfc_target_send_bytes"); + ERROR ("nfc_target_send_bytes: %s\n", nfc_strerror(data->pndTarget)); return 0; } diff --git a/pcsc-relay/src/opicc.c b/pcsc-relay/src/opicc.c index e423a9f..9b8e9a6 100644 --- a/pcsc-relay/src/opicc.c +++ b/pcsc-relay/src/opicc.c @@ -19,6 +19,7 @@ #include #include #include +#include #include "config.h" #include "pcsc-relay.h" @@ -129,7 +130,7 @@ static int picc_connect(void **driver_data) data->fd = fopen(PICCDEV, "a+"); /*O_NOCTTY ?*/ if (!data->fd) { - ERROR("Error opening %s\n", PICCDEV); + ERROR("Error opening %s: %s\n", PICCDEV, strerror(errno)); return 0; } @@ -166,18 +167,16 @@ static int picc_receive_capdu(void *driver_data, { ssize_t linelen; struct picc_data *data = driver_data; - size_t buflen = 0; - char *buf = NULL; if (!data || !capdu || !len) return 0; /* read C-APDU */ - linelen = getline(&buf, &buflen, data->fd); + linelen = getline(&data->buf, &data->bufmax, data->fd); if (linelen < 0) { if (linelen < 0) { - ERROR("Error reading from %s\n", PICCDEV); + ERROR("Error reading from %s: %s\n", PICCDEV, strerror(errno)); return 0; } } @@ -187,15 +186,11 @@ static int picc_receive_capdu(void *driver_data, } fflush(data->fd); - DEBUG("%s\n", buf); + DEBUG("%s\n", data->buf); /* decode C-APDU */ - if (!picc_decode_apdu(buf, linelen, capdu, len)) - return 0; - - - return 1; + return picc_decode_apdu(data->buf, linelen, capdu, len); } static int picc_send_rapdu(void *driver_data, @@ -218,8 +213,12 @@ static int picc_send_rapdu(void *driver_data, /* write R-APDU */ DEBUG("INF: Writing R-APDU\r\n%s\r\n", data->buf); - if (fprintf(data->fd,"%s\r\n", (char *) data->buf) < 0 - || fflush(data->fd) != 0) { + if (fprintf(data->fd,"%s\r\n", (char *) data->buf) < 0) { + ERROR("fprintf: %s\n", strerror(errno)); + return 0; + } + if (fflush(data->fd) != 0) { + ERROR("fflush: %s\n", strerror(errno)); return 0; } diff --git a/pcsc-relay/src/pcsc-relay.c b/pcsc-relay/src/pcsc-relay.c index f4cdeb6..eec1e30 100644 --- a/pcsc-relay/src/pcsc-relay.c +++ b/pcsc-relay/src/pcsc-relay.c @@ -70,13 +70,16 @@ static void daemonize(); static void cleanup_exit(int signo); static void cleanup(void); + void daemonize() { pid_t pid, sid; - /*Fork and continue as child process*/ + /* Fork and continue as child process */ pid = fork(); - if (pid < 0) + if (pid < 0) { + ERROR("fork: %s\n", strerror(errno)); goto err; + } if (pid > 0) /* Exit the parent */ exit(0); @@ -84,13 +87,17 @@ void daemonize() { /* Create new session and set the process group ID */ sid = setsid(); - if (sid < 0) + if (sid < 0) { + ERROR("setsid: %s\n", strerror(errno)); 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) + * directory from being locked; hence not being able to remove it. */ + if (chdir("/") < 0) { + ERROR("chdir: %s\n", strerror(errno)); goto err; + } /* Redirect standard files to /dev/null */ close(STDIN_FILENO); @@ -190,8 +197,10 @@ int main (int argc, char **argv) sigemptyset(&new_sig.sa_mask); new_sig.sa_flags = SA_RESTART; if ((sigaction(SIGINT, &new_sig, &old_sig) < 0) - || (sigaction(SIGTERM, &new_sig, &old_sig) < 0)) + || (sigaction(SIGTERM, &new_sig, &old_sig) < 0)) { + ERROR("sigaction: %s\n", strerror(errno)); goto err; + } /* Open the device */ @@ -218,7 +227,7 @@ int main (int argc, char **argv) /* transmit APDU to card */ - outputLength = MAX_BUFFER_SIZE; + outputLength = sizeof outputBuffer; r = pcsc_transmit(protocol, hCard, buf, buflen, outputBuffer, &outputLength); if (r != SCARD_S_SUCCESS) goto err; @@ -235,5 +244,10 @@ int main (int argc, char **argv) err: cleanup(); - exit(r == SCARD_S_SUCCESS ? 0 : 1); + if (r != SCARD_S_SUCCESS) { + ERROR(pcsc_stringify_error(r)); + exit(1); + } + + exit(0); } diff --git a/pcsc-relay/src/pcsc-relay.h b/pcsc-relay/src/pcsc-relay.h index b9c689a..74e39f6 100644 --- a/pcsc-relay/src/pcsc-relay.h +++ b/pcsc-relay/src/pcsc-relay.h @@ -51,8 +51,10 @@ extern struct rf_driver driver_libnfc; {if (verbose >= LEVEL_INFO) \ printf (__VA_ARGS__);} #define ERROR(...) \ - {if (verbose >= 0) \ - fprintf (stderr, __VA_ARGS__);} + { \ + if (verbose >= LEVEL_DEBUG) fprintf (stderr, "%s:%u\t", __FILE__, __LINE__); \ + if (verbose >= 0) fprintf (stderr, __VA_ARGS__); \ + } #ifdef __cplusplus