added more error debugging

git-svn-id: https://vsmartcard.svn.sourceforge.net/svnroot/vsmartcard@355 96b47cad-a561-4643-ad3b-153ac7d7599c
This commit is contained in:
frankmorgner
2010-11-11 09:51:48 +00:00
parent a012c02caf
commit c0f16a32a3
4 changed files with 40 additions and 27 deletions

View File

@@ -167,8 +167,7 @@ static int lnfc_receive_capdu(void *driver_data,
// Receive external reader command through target // Receive external reader command through target
if (!nfc_target_receive_bytes(data->pndTarget, data->abtCapdu, &data->szCapduLen)) { if (!nfc_target_receive_bytes(data->pndTarget, data->abtCapdu, &data->szCapduLen)) {
if (verbose >= 0) ERROR ("nfc_target_receive_bytes: %s\n", nfc_strerror(data->pndTarget));
nfc_perror (data->pndTarget, "nfc_target_receive_bytes");
return 0; return 0;
} }
@@ -196,8 +195,7 @@ static int lnfc_send_rapdu(void *driver_data,
if (!nfc_target_send_bytes(data->pndTarget, rapdu, len)) { if (!nfc_target_send_bytes(data->pndTarget, rapdu, len)) {
if (verbose >= 0) ERROR ("nfc_target_send_bytes: %s\n", nfc_strerror(data->pndTarget));
nfc_perror (data->pndTarget, "nfc_target_send_bytes");
return 0; return 0;
} }

View File

@@ -19,6 +19,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h>
#include "config.h" #include "config.h"
#include "pcsc-relay.h" #include "pcsc-relay.h"
@@ -129,7 +130,7 @@ static int picc_connect(void **driver_data)
data->fd = fopen(PICCDEV, "a+"); /*O_NOCTTY ?*/ data->fd = fopen(PICCDEV, "a+"); /*O_NOCTTY ?*/
if (!data->fd) { if (!data->fd) {
ERROR("Error opening %s\n", PICCDEV); ERROR("Error opening %s: %s\n", PICCDEV, strerror(errno));
return 0; return 0;
} }
@@ -166,18 +167,16 @@ static int picc_receive_capdu(void *driver_data,
{ {
ssize_t linelen; ssize_t linelen;
struct picc_data *data = driver_data; struct picc_data *data = driver_data;
size_t buflen = 0;
char *buf = NULL;
if (!data || !capdu || !len) if (!data || !capdu || !len)
return 0; return 0;
/* read C-APDU */ /* read C-APDU */
linelen = getline(&buf, &buflen, data->fd); linelen = getline(&data->buf, &data->bufmax, data->fd);
if (linelen < 0) { if (linelen < 0) {
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; return 0;
} }
} }
@@ -187,15 +186,11 @@ static int picc_receive_capdu(void *driver_data,
} }
fflush(data->fd); fflush(data->fd);
DEBUG("%s\n", buf); DEBUG("%s\n", data->buf);
/* decode C-APDU */ /* decode C-APDU */
if (!picc_decode_apdu(buf, linelen, capdu, len)) return picc_decode_apdu(data->buf, linelen, capdu, len);
return 0;
return 1;
} }
static int picc_send_rapdu(void *driver_data, static int picc_send_rapdu(void *driver_data,
@@ -218,8 +213,12 @@ static int picc_send_rapdu(void *driver_data,
/* write R-APDU */ /* write R-APDU */
DEBUG("INF: Writing R-APDU\r\n%s\r\n", data->buf); DEBUG("INF: Writing R-APDU\r\n%s\r\n", data->buf);
if (fprintf(data->fd,"%s\r\n", (char *) data->buf) < 0 if (fprintf(data->fd,"%s\r\n", (char *) data->buf) < 0) {
|| fflush(data->fd) != 0) { ERROR("fprintf: %s\n", strerror(errno));
return 0;
}
if (fflush(data->fd) != 0) {
ERROR("fflush: %s\n", strerror(errno));
return 0; return 0;
} }

View File

@@ -70,13 +70,16 @@ static void daemonize();
static void cleanup_exit(int signo); static void cleanup_exit(int signo);
static void cleanup(void); static void cleanup(void);
void daemonize() { void daemonize() {
pid_t pid, sid; pid_t pid, sid;
/*Fork and continue as child process*/ /* Fork and continue as child process */
pid = fork(); pid = fork();
if (pid < 0) if (pid < 0) {
ERROR("fork: %s\n", strerror(errno));
goto err; goto err;
}
if (pid > 0) /* Exit the parent */ if (pid > 0) /* Exit the parent */
exit(0); exit(0);
@@ -84,13 +87,17 @@ void daemonize() {
/* Create new session and set the process group ID */ /* Create new session and set the process group ID */
sid = setsid(); sid = setsid();
if (sid < 0) if (sid < 0) {
ERROR("setsid: %s\n", strerror(errno));
goto err; goto err;
}
/* Change the current working directory. This prevents the current /* Change the current working directory. This prevents the current
directory from being locked; hence not being able to remove it. */ * directory from being locked; hence not being able to remove it. */
if (chdir("/") < 0) if (chdir("/") < 0) {
ERROR("chdir: %s\n", strerror(errno));
goto err; goto err;
}
/* Redirect standard files to /dev/null */ /* Redirect standard files to /dev/null */
close(STDIN_FILENO); close(STDIN_FILENO);
@@ -190,8 +197,10 @@ int main (int argc, char **argv)
sigemptyset(&new_sig.sa_mask); sigemptyset(&new_sig.sa_mask);
new_sig.sa_flags = SA_RESTART; 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)) || (sigaction(SIGTERM, &new_sig, &old_sig) < 0)) {
ERROR("sigaction: %s\n", strerror(errno));
goto err; goto err;
}
/* Open the device */ /* Open the device */
@@ -218,7 +227,7 @@ int main (int argc, char **argv)
/* transmit APDU to card */ /* transmit APDU to card */
outputLength = MAX_BUFFER_SIZE; outputLength = sizeof outputBuffer;
r = pcsc_transmit(protocol, hCard, buf, buflen, outputBuffer, &outputLength); r = pcsc_transmit(protocol, hCard, buf, buflen, outputBuffer, &outputLength);
if (r != SCARD_S_SUCCESS) if (r != SCARD_S_SUCCESS)
goto err; goto err;
@@ -235,5 +244,10 @@ int main (int argc, char **argv)
err: err:
cleanup(); cleanup();
exit(r == SCARD_S_SUCCESS ? 0 : 1); if (r != SCARD_S_SUCCESS) {
ERROR(pcsc_stringify_error(r));
exit(1);
}
exit(0);
} }

View File

@@ -51,8 +51,10 @@ extern struct rf_driver driver_libnfc;
{if (verbose >= LEVEL_INFO) \ {if (verbose >= LEVEL_INFO) \
printf (__VA_ARGS__);} printf (__VA_ARGS__);}
#define ERROR(...) \ #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 #ifdef __cplusplus