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
This commit is contained in:
@@ -37,13 +37,49 @@ static FILE *picc_fd = NULL; /*filehandle used for PICCDEV*/
|
|||||||
static LPSTR readers = NULL;
|
static LPSTR readers = NULL;
|
||||||
static SCARDCONTEXT hContext = 0;
|
static SCARDCONTEXT hContext = 0;
|
||||||
static SCARDHANDLE hCard = 0;
|
static SCARDHANDLE hCard = 0;
|
||||||
|
static int verbose = 0, debug = 0;
|
||||||
|
|
||||||
/* Forward declaration */
|
/* Forward declaration */
|
||||||
|
static void daemonize();
|
||||||
static void cleanup_exit(int signo);
|
static void cleanup_exit(int signo);
|
||||||
static void cleanup(void);
|
static void cleanup(void);
|
||||||
static size_t picc_decode_apdu(char *inbuf, size_t inlen, unsigned char **outbuf);
|
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);
|
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){
|
void cleanup_exit(int signo){
|
||||||
cleanup();
|
cleanup();
|
||||||
exit(0);
|
exit(0);
|
||||||
@@ -76,7 +112,8 @@ size_t picc_decode_apdu(char *inbuf, size_t inlen, unsigned char **outbuf)
|
|||||||
|
|
||||||
p = realloc(*outbuf, length);
|
p = realloc(*outbuf, length);
|
||||||
if (!p) {
|
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;
|
goto noapdu;
|
||||||
}
|
}
|
||||||
*outbuf = p;
|
*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) {
|
while(inbuf+inlen > end && length > pos) {
|
||||||
b = strtoul(end, &end, 16);
|
b = strtoul(end, &end, 16);
|
||||||
if (b > 0xff) {
|
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;
|
goto noapdu;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,7 +148,8 @@ size_t picc_encode_rapdu(unsigned char *inbuf, size_t inlen, char **outbuf)
|
|||||||
length = 5+inlen*3+1;
|
length = 5+inlen*3+1;
|
||||||
p = realloc(*outbuf, length);
|
p = realloc(*outbuf, length);
|
||||||
if (!p) {
|
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;
|
goto err;
|
||||||
}
|
}
|
||||||
*outbuf = p;
|
*outbuf = p;
|
||||||
@@ -149,7 +188,6 @@ int main (int argc, char **argv)
|
|||||||
ssize_t linelen;
|
ssize_t linelen;
|
||||||
|
|
||||||
unsigned int readernum = 0;
|
unsigned int readernum = 0;
|
||||||
int verbose = 0;
|
|
||||||
|
|
||||||
struct sigaction new_sig, old_sig;
|
struct sigaction new_sig, old_sig;
|
||||||
|
|
||||||
@@ -157,31 +195,42 @@ int main (int argc, char **argv)
|
|||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
readernum = strtoul(argv[1], NULL, 10);
|
readernum = strtoul(argv[1], NULL, 10);
|
||||||
if (argc > 2) {
|
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: "
|
fprintf(stderr, "Usage: "
|
||||||
"%s [reader number] [verbose]\n", argv[0]);
|
"%s [reader number] [verbose | debug]\n", argv[0]);
|
||||||
exit(2);
|
exit(2);
|
||||||
}
|
}
|
||||||
verbose++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!verbose && !debug)
|
||||||
|
daemonize();
|
||||||
|
|
||||||
/* Register signal handlers */
|
/* Register signal handlers */
|
||||||
new_sig.sa_handler = cleanup_exit;
|
new_sig.sa_handler = cleanup_exit;
|
||||||
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))
|
||||||
goto err;
|
goto err;
|
||||||
|
|
||||||
|
|
||||||
/* Open the device */
|
/* Open the device */
|
||||||
picc_fd = fopen(PICCDEV, "a+"); /*O_NOCTTY ?*/
|
picc_fd = fopen(PICCDEV, "a+"); /*O_NOCTTY ?*/
|
||||||
if (!picc_fd) {
|
if (!picc_fd) {
|
||||||
fprintf(stderr,"Error opening %s\n", PICCDEV);
|
if (debug || verbose)
|
||||||
|
fprintf(stderr,"Error opening %s\n", PICCDEV);
|
||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
printf("Connected to %s\n", PICCDEV);
|
if (debug || verbose)
|
||||||
|
printf("Connected to %s\n", PICCDEV);
|
||||||
|
|
||||||
|
|
||||||
/* connect to reader and card */
|
/* connect to reader and card */
|
||||||
@@ -196,7 +245,8 @@ int main (int argc, char **argv)
|
|||||||
linelen = getline(&read, &readlen, picc_fd);
|
linelen = getline(&read, &readlen, picc_fd);
|
||||||
if (linelen < 0) {
|
if (linelen < 0) {
|
||||||
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;
|
goto err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -204,7 +254,7 @@ int main (int argc, char **argv)
|
|||||||
continue;
|
continue;
|
||||||
fflush(picc_fd);
|
fflush(picc_fd);
|
||||||
|
|
||||||
if (verbose)
|
if (debug)
|
||||||
printf("%s\n", read);
|
printf("%s\n", read);
|
||||||
|
|
||||||
|
|
||||||
@@ -232,10 +282,10 @@ int main (int argc, char **argv)
|
|||||||
|
|
||||||
|
|
||||||
/* write R-APDU */
|
/* write R-APDU */
|
||||||
if (verbose)
|
if (debug)
|
||||||
printf("INF: Writing R-APDU\n\n%s\n\n", buf);
|
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);
|
fflush(picc_fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user