pcsc-lite  2.0.3
utils.c
Go to the documentation of this file.
1 /*
2  * MUSCLE SmartCard Development ( https://pcsclite.apdu.fr/ )
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 #ifndef LIBPCSCLITE
54 pid_t GetDaemonPid(void)
55 {
56  int fd;
57  pid_t pid;
58 
59  /* pids are only 15 bits but 4294967296
60  * (32 bits in case of a new system use it) is on 10 bytes
61  */
62  fd = open(PCSCLITE_RUN_PID, O_RDONLY);
63  if (fd >= 0)
64  {
65  char pid_ascii[PID_ASCII_SIZE];
66  ssize_t r;
67 
68  r = read(fd, pid_ascii, sizeof pid_ascii);
69  if (r < 0)
70  {
71  Log2(PCSC_LOG_CRITICAL, "Reading " PCSCLITE_RUN_PID " failed: %s",
72  strerror(errno));
73  pid = -1;
74  }
75  else
76  pid = atoi(pid_ascii);
77  (void)close(fd);
78 
79  }
80  else
81  {
82  Log2(PCSC_LOG_CRITICAL, "Can't open " PCSCLITE_RUN_PID ": %s",
83  strerror(errno));
84  return -1;
85  }
86 
87  return pid;
88 } /* GetDaemonPid */
89 
90 int SendHotplugSignal(void)
91 {
92  pid_t pid;
93 
94  pid = GetDaemonPid();
95 
96  if (pid != -1)
97  {
98  Log2(PCSC_LOG_INFO, "Send hotplug signal to pcscd (pid=%ld)",
99  (long)pid);
100  if (kill(pid, SIGUSR1) < 0)
101  {
102  Log3(PCSC_LOG_CRITICAL, "Can't signal pcscd (pid=%ld): %s",
103  (long)pid, strerror(errno));
104  return EXIT_FAILURE ;
105  }
106  (void)SYS_Sleep(1);
107  }
108 
109  return EXIT_SUCCESS;
110 } /* SendHotplugSignal */
111 
119 #define OPENCT_FILE "/var/run/openct/status"
120 int CheckForOpenCT(void)
121 {
122  struct stat buf;
123 
124  if (0 == stat(OPENCT_FILE, &buf))
125  {
126  Log1(PCSC_LOG_CRITICAL, "File " OPENCT_FILE " found. Remove OpenCT and try again");
127  return 1;
128  }
129 
130  return 0;
131 } /* CheckForOpenCT */
132 #endif
133 
138 long int time_sub(struct timeval *a, struct timeval *b)
139 {
140  struct timeval r;
141  r.tv_sec = a -> tv_sec - b -> tv_sec;
142  r.tv_usec = a -> tv_usec - b -> tv_usec;
143  if (r.tv_usec < 0)
144  {
145  r.tv_sec--;
146  r.tv_usec += 1000000;
147  }
148 
149  return r.tv_sec * 1000000 + r.tv_usec;
150 } /* time_sub */
151 
152 #ifndef LIBPCSCLITE
153 int ThreadCreate(pthread_t * pthThread, int attributes,
154  PCSCLITE_THREAD_FUNCTION(pvFunction), LPVOID pvArg)
155 {
156  pthread_attr_t attr;
157  size_t stack_size;
158  int ret;
159 
160  ret = pthread_attr_init(&attr);
161  if (ret)
162  return ret;
163 
164  ret = pthread_attr_setdetachstate(&attr,
165  attributes & THREAD_ATTR_DETACHED ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE);
166  if (ret)
167  goto error;
168 
169  /* stack size of 0x40000 (256 KB) bytes minimum for musl C lib */
170  ret = pthread_attr_getstacksize(&attr, &stack_size);
171  if (ret)
172  goto error;
173 
174  /* A stack_size of 0 indicates the default size on Solaris.
175  * The default size is more than 256 KB so do not shrink it. */
176  if ((stack_size != 0) && (stack_size < 0x40000))
177  {
178  stack_size = 0x40000;
179  ret = pthread_attr_setstacksize(&attr, stack_size);
180  if (ret)
181  goto error;
182  }
183 
184  ret = pthread_create(pthThread, &attr, pvFunction, pvArg);
185 
186 error:
187  pthread_attr_destroy(&attr);
188  return ret;
189 }
190 #endif
#define OPENCT_FILE
Check is OpenCT is running and display a critical message if it is.
Definition: utils.c:119
This handles abstract system level calls.
int SYS_Sleep(int)
Makes the current process sleep for some seconds.
Definition: sys_unix.c:62
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:138
This keeps a list of defines for pcsc-lite.
This handles debugging.