pcsc-lite  1.8.8
utils.c
Go to the documentation of this file.
1 /*
2  * MUSCLE SmartCard Development ( http://www.linuxnet.com )
3  *
4  * Copyright (C) 2006-2011
5  * Ludovic Rousseau <ludovic.rousseau@free.fr>
6  *
7  * $Id: utils.c 5711 2011-05-05 09:02:08Z rousseau $
8  */
9 
15 #include <stdio.h>
16 #include <sys/types.h>
17 #include <unistd.h>
18 #include <errno.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <signal.h>
22 #include <dirent.h>
23 #include <fcntl.h>
24 #include <pthread.h>
25 
26 #include "config.h"
27 #include "debuglog.h"
28 #include "utils.h"
29 #include "pcscd.h"
30 #include "sys_generic.h"
31 
32 pid_t GetDaemonPid(void)
33 {
34  int fd;
35  pid_t pid;
36 
37  /* pids are only 15 bits but 4294967296
38  * (32 bits in case of a new system use it) is on 10 bytes
39  */
40  fd = open(PCSCLITE_RUN_PID, O_RDONLY);
41  if (fd >= 0)
42  {
43  char pid_ascii[PID_ASCII_SIZE];
44 
45  (void)read(fd, pid_ascii, PID_ASCII_SIZE);
46  (void)close(fd);
47 
48  pid = atoi(pid_ascii);
49  }
50  else
51  {
52  Log2(PCSC_LOG_CRITICAL, "Can't open " PCSCLITE_RUN_PID ": %s",
53  strerror(errno));
54  return -1;
55  }
56 
57  return pid;
58 } /* GetDaemonPid */
59 
60 int SendHotplugSignal(void)
61 {
62  pid_t pid;
63 
64  pid = GetDaemonPid();
65 
66  if (pid != -1)
67  {
68  Log2(PCSC_LOG_INFO, "Send hotplug signal to pcscd (pid=%d)", pid);
69  if (kill(pid, SIGUSR1) < 0)
70  {
71  Log3(PCSC_LOG_CRITICAL, "Can't signal pcscd (pid=%d): %s",
72  pid, strerror(errno));
73  return EXIT_FAILURE ;
74  }
75  (void)SYS_Sleep(1);
76  }
77 
78  return EXIT_SUCCESS;
79 } /* SendHotplugSignal */
80 
88 #define OPENCT_FILE "/var/run/openct/status"
89 int CheckForOpenCT(void)
90 {
91  struct stat buf;
92 
93  if (0 == stat(OPENCT_FILE, &buf))
94  {
95  Log1(PCSC_LOG_CRITICAL, "File " OPENCT_FILE " found. Remove OpenCT and try again");
96  return 1;
97  }
98 
99  return 0;
100 } /* CheckForOpenCT */
101 
106 long int time_sub(struct timeval *a, struct timeval *b)
107 {
108  struct timeval r;
109  r.tv_sec = a -> tv_sec - b -> tv_sec;
110  r.tv_usec = a -> tv_usec - b -> tv_usec;
111  if (r.tv_usec < 0)
112  {
113  r.tv_sec--;
114  r.tv_usec += 1000000;
115  }
116 
117  return r.tv_sec * 1000000 + r.tv_usec;
118 } /* time_sub */
119 
120 int ThreadCreate(pthread_t * pthThread, int attributes,
121  PCSCLITE_THREAD_FUNCTION(pvFunction), LPVOID pvArg)
122 {
123  pthread_attr_t attr;
124  int ret;
125 
126  ret = pthread_attr_init(&attr);
127  if (ret)
128  return ret;
129 
130  ret = pthread_attr_setdetachstate(&attr,
131  attributes & THREAD_ATTR_DETACHED ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE);
132  if (ret)
133  {
134  (void)pthread_attr_destroy(&attr);
135  return ret;
136  }
137 
138  ret = pthread_create(pthThread, &attr, pvFunction, pvArg);
139  if (ret)
140  return ret;
141 
142  ret = pthread_attr_destroy(&attr);
143  return ret;
144 }
#define OPENCT_FILE
Check is OpenCT is running and display a critical message if it is.
Definition: utils.c:88
This handles abstract system level calls.
int SYS_Sleep(int)
Makes the current process sleep for some seconds.
Definition: sys_unix.c:44
long int time_sub(struct timeval *a, struct timeval *b)
return the difference (as long int) in µs between 2 struct timeval r = a - b
Definition: utils.c:106
This keeps a list of defines for pcsc-lite.
This handles debugging.