pcsc-lite  1.8.15
utils.c
Go to the documentation of this file.
1 /*
2  * MUSCLE SmartCard Development ( http://pcsclite.alioth.debian.org/pcsclite.html )
3  *
4  * Copyright (C) 2006-2011
5  * Ludovic Rousseau <ludovic.rousseau@free.fr>
6  *
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10 
11 1. Redistributions of source code must retain the above copyright
12  notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14  notice, this list of conditions and the following disclaimer in the
15  documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17  derived from this software without specific prior written permission.
18 
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
36 #include <stdio.h>
37 #include <sys/types.h>
38 #include <unistd.h>
39 #include <errno.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <signal.h>
43 #include <dirent.h>
44 #include <fcntl.h>
45 #include <pthread.h>
46 
47 #include "config.h"
48 #include "debuglog.h"
49 #include "utils.h"
50 #include "pcscd.h"
51 #include "sys_generic.h"
52 
53 pid_t GetDaemonPid(void)
54 {
55  int fd;
56  pid_t pid;
57 
58  /* pids are only 15 bits but 4294967296
59  * (32 bits in case of a new system use it) is on 10 bytes
60  */
61  fd = open(PCSCLITE_RUN_PID, O_RDONLY);
62  if (fd >= 0)
63  {
64  char pid_ascii[PID_ASCII_SIZE];
65  ssize_t r;
66 
67  r = read(fd, pid_ascii, sizeof pid_ascii);
68  if (r < 0)
69  {
70  Log2(PCSC_LOG_CRITICAL, "Reading " PCSCLITE_RUN_PID " failed: %s",
71  strerror(errno));
72  pid = -1;
73  }
74  else
75  pid = atoi(pid_ascii);
76  (void)close(fd);
77 
78  }
79  else
80  {
81  Log2(PCSC_LOG_CRITICAL, "Can't open " PCSCLITE_RUN_PID ": %s",
82  strerror(errno));
83  return -1;
84  }
85 
86  return pid;
87 } /* GetDaemonPid */
88 
89 int SendHotplugSignal(void)
90 {
91  pid_t pid;
92 
93  pid = GetDaemonPid();
94 
95  if (pid != -1)
96  {
97  Log2(PCSC_LOG_INFO, "Send hotplug signal to pcscd (pid=%d)", pid);
98  if (kill(pid, SIGUSR1) < 0)
99  {
100  Log3(PCSC_LOG_CRITICAL, "Can't signal pcscd (pid=%d): %s",
101  pid, strerror(errno));
102  return EXIT_FAILURE ;
103  }
104  (void)SYS_Sleep(1);
105  }
106 
107  return EXIT_SUCCESS;
108 } /* SendHotplugSignal */
109 
117 #define OPENCT_FILE "/var/run/openct/status"
118 int CheckForOpenCT(void)
119 {
120  struct stat buf;
121 
122  if (0 == stat(OPENCT_FILE, &buf))
123  {
124  Log1(PCSC_LOG_CRITICAL, "File " OPENCT_FILE " found. Remove OpenCT and try again");
125  return 1;
126  }
127 
128  return 0;
129 } /* CheckForOpenCT */
130 
135 long int time_sub(struct timeval *a, struct timeval *b)
136 {
137  struct timeval r;
138  r.tv_sec = a -> tv_sec - b -> tv_sec;
139  r.tv_usec = a -> tv_usec - b -> tv_usec;
140  if (r.tv_usec < 0)
141  {
142  r.tv_sec--;
143  r.tv_usec += 1000000;
144  }
145 
146  return r.tv_sec * 1000000 + r.tv_usec;
147 } /* time_sub */
148 
149 int ThreadCreate(pthread_t * pthThread, int attributes,
150  PCSCLITE_THREAD_FUNCTION(pvFunction), LPVOID pvArg)
151 {
152  pthread_attr_t attr;
153  size_t stack_size;
154  int ret;
155 
156  ret = pthread_attr_init(&attr);
157  if (ret)
158  return ret;
159 
160  ret = pthread_attr_setdetachstate(&attr,
161  attributes & THREAD_ATTR_DETACHED ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE);
162  if (ret)
163  goto error;
164 
165  /* stack size of 0x40000 (256 KB) bytes minimum for musl C lib */
166  ret = pthread_attr_getstacksize(&attr, &stack_size);
167  if (ret)
168  goto error;
169 
170  if (stack_size < 0x40000)
171  {
172  stack_size = 0x40000;
173  ret = pthread_attr_setstacksize(&attr, stack_size);
174  if (ret)
175  goto error;
176  }
177 
178  ret = pthread_create(pthThread, &attr, pvFunction, pvArg);
179 
180 error:
181  pthread_attr_destroy(&attr);
182  return ret;
183 }
#define OPENCT_FILE
Check is OpenCT is running and display a critical message if it is.
Definition: utils.c:117
This handles abstract system level calls.
int SYS_Sleep(int)
Makes the current process sleep for some seconds.
Definition: sys_unix.c:65
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:135
This keeps a list of defines for pcsc-lite.
This handles debugging.