utils.c

Go to the documentation of this file.
00001 /*
00002  * MUSCLE SmartCard Development ( http://www.linuxnet.com )
00003  *
00004  * Copyright (C) 2006-2009
00005  *  Ludovic Rousseau <ludovic.rousseau@free.fr>
00006  *
00007  * $Id: pcscdaemon.c 2377 2007-02-05 13:13:56Z 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     FILE *f;
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     if ((f = fopen(PCSCLITE_RUN_PID, "rb")) != NULL)
00041     {
00042         char pid_ascii[PID_ASCII_SIZE];
00043 
00044         (void)fgets(pid_ascii, PID_ASCII_SIZE, f);
00045         (void)fclose(f);
00046 
00047         pid = atoi(pid_ascii);
00048     }
00049     else
00050     {
00051         Log2(PCSC_LOG_CRITICAL, "Can't open " PCSCLITE_RUN_PID ": %s",
00052             strerror(errno));
00053         return -1;
00054     }
00055 
00056     return pid;
00057 } /* GetDaemonPid */
00058 
00059 int SendHotplugSignal(void)
00060 {
00061     pid_t pid;
00062 
00063     pid = GetDaemonPid();
00064 
00065     if (pid != -1)
00066     {
00067         Log2(PCSC_LOG_INFO, "Send hotplug signal to pcscd (pid=%d)", pid);
00068         if (kill(pid, SIGUSR1) < 0)
00069         {
00070             Log3(PCSC_LOG_CRITICAL, "Can't signal pcscd (pid=%d): %s",
00071                 pid, strerror(errno));
00072             return EXIT_FAILURE ;
00073         }
00074         (void)SYS_Sleep(1);
00075     }
00076 
00077     return EXIT_SUCCESS;
00078 } /* SendHotplugSignal */
00079 
00087 #define OPENCT_FILE "/var/run/openct/status"
00088 int CheckForOpenCT(void)
00089 {
00090     struct stat buf;
00091 
00092     if (0 == stat(OPENCT_FILE, &buf))
00093     {
00094         Log1(PCSC_LOG_CRITICAL, "Remove OpenCT and try again");
00095         return 1;
00096     }
00097 
00098     return 0;
00099 } /* CheckForOpenCT */
00100 
00105 long int time_sub(struct timeval *a, struct timeval *b)
00106 {
00107     struct timeval r;
00108     r.tv_sec = a -> tv_sec - b -> tv_sec;
00109     r.tv_usec = a -> tv_usec - b -> tv_usec;
00110     if (r.tv_usec < 0)
00111     {
00112         r.tv_sec--;
00113         r.tv_usec += 1000000;
00114     }
00115 
00116     return r.tv_sec * 1000000 + r.tv_usec;
00117 } /* time_sub */
00118 
00119 int ThreadCreate(pthread_t * pthThread, int attributes,
00120     PCSCLITE_THREAD_FUNCTION(pvFunction), LPVOID pvArg)
00121 {
00122     pthread_attr_t attr;
00123     int ret;
00124 
00125     ret = pthread_attr_init(&attr);
00126     if (ret)
00127         return ret;
00128 
00129     ret = pthread_attr_setdetachstate(&attr,
00130         attributes & THREAD_ATTR_DETACHED ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE);
00131     if (ret)
00132     {
00133         (void)pthread_attr_destroy(&attr);
00134         return ret;
00135     }
00136 
00137     ret = pthread_create(pthThread, &attr, pvFunction, pvArg);
00138     if (ret)
00139         return ret;
00140 
00141     ret = pthread_attr_destroy(&attr);
00142     return ret;
00143 }