pcsc-lite  1.7.4
utils.c
Go to the documentation of this file.
00001 /*
00002  * MUSCLE SmartCard Development ( http://www.linuxnet.com )
00003  *
00004  * Copyright (C) 2006-2011
00005  *  Ludovic Rousseau <ludovic.rousseau@free.fr>
00006  *
00007  * $Id: utils.c 5711 2011-05-05 09:02:08Z rousseau $
00008  */
00009 
00015 #include <stdio.h>
00016 #include <sys/types.h>
00017 #include <unistd.h>
00018 #include <errno.h>
00019 #include <stdlib.h>
00020 #include <string.h>
00021 #include <signal.h>
00022 #include <dirent.h>
00023 #include <fcntl.h>
00024 #include <pthread.h>
00025 
00026 #include "config.h"
00027 #include "debuglog.h"
00028 #include "utils.h"
00029 #include "pcscd.h"
00030 #include "sys_generic.h"
00031 
00032 pid_t GetDaemonPid(void)
00033 {
00034     int fd;
00035     pid_t pid;
00036 
00037     /* pids are only 15 bits but 4294967296
00038      * (32 bits in case of a new system use it) is on 10 bytes
00039      */
00040     fd = open(PCSCLITE_RUN_PID, O_RDONLY);
00041     if (fd >= 0)
00042     {
00043         char pid_ascii[PID_ASCII_SIZE];
00044 
00045         (void)read(fd, pid_ascii, PID_ASCII_SIZE);
00046         (void)close(fd);
00047 
00048         pid = atoi(pid_ascii);
00049     }
00050     else
00051     {
00052         Log2(PCSC_LOG_CRITICAL, "Can't open " PCSCLITE_RUN_PID ": %s",
00053             strerror(errno));
00054         return -1;
00055     }
00056 
00057     return pid;
00058 } /* GetDaemonPid */
00059 
00060 int SendHotplugSignal(void)
00061 {
00062     pid_t pid;
00063 
00064     pid = GetDaemonPid();
00065 
00066     if (pid != -1)
00067     {
00068         Log2(PCSC_LOG_INFO, "Send hotplug signal to pcscd (pid=%d)", pid);
00069         if (kill(pid, SIGUSR1) < 0)
00070         {
00071             Log3(PCSC_LOG_CRITICAL, "Can't signal pcscd (pid=%d): %s",
00072                 pid, strerror(errno));
00073             return EXIT_FAILURE ;
00074         }
00075         (void)SYS_Sleep(1);
00076     }
00077 
00078     return EXIT_SUCCESS;
00079 } /* SendHotplugSignal */
00080 
00088 #define OPENCT_FILE "/var/run/openct/status"
00089 int CheckForOpenCT(void)
00090 {
00091     struct stat buf;
00092 
00093     if (0 == stat(OPENCT_FILE, &buf))
00094     {
00095         Log1(PCSC_LOG_CRITICAL, "File " OPENCT_FILE " found. Remove OpenCT and try again");
00096         return 1;
00097     }
00098 
00099     return 0;
00100 } /* CheckForOpenCT */
00101 
00106 long int time_sub(struct timeval *a, struct timeval *b)
00107 {
00108     struct timeval r;
00109     r.tv_sec = a -> tv_sec - b -> tv_sec;
00110     r.tv_usec = a -> tv_usec - b -> tv_usec;
00111     if (r.tv_usec < 0)
00112     {
00113         r.tv_sec--;
00114         r.tv_usec += 1000000;
00115     }
00116 
00117     return r.tv_sec * 1000000 + r.tv_usec;
00118 } /* time_sub */
00119 
00120 int ThreadCreate(pthread_t * pthThread, int attributes,
00121     PCSCLITE_THREAD_FUNCTION(pvFunction), LPVOID pvArg)
00122 {
00123     pthread_attr_t attr;
00124     int ret;
00125 
00126     ret = pthread_attr_init(&attr);
00127     if (ret)
00128         return ret;
00129 
00130     ret = pthread_attr_setdetachstate(&attr,
00131         attributes & THREAD_ATTR_DETACHED ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE);
00132     if (ret)
00133     {
00134         (void)pthread_attr_destroy(&attr);
00135         return ret;
00136     }
00137 
00138     ret = pthread_create(pthThread, &attr, pvFunction, pvArg);
00139     if (ret)
00140         return ret;
00141 
00142     ret = pthread_attr_destroy(&attr);
00143     return ret;
00144 }