pcsc-lite  1.8.14
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  * $Id$
31  */
32 
38 #include <stdio.h>
39 #include <sys/types.h>
40 #include <unistd.h>
41 #include <errno.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <signal.h>
45 #include <dirent.h>
46 #include <fcntl.h>
47 #include <pthread.h>
48 
49 #include "config.h"
50 #include "debuglog.h"
51 #include "utils.h"
52 #include "pcscd.h"
53 #include "sys_generic.h"
54 
55 pid_t GetDaemonPid(void)
56 {
57  int fd;
58  pid_t pid;
59 
60  /* pids are only 15 bits but 4294967296
61  * (32 bits in case of a new system use it) is on 10 bytes
62  */
63  fd = open(PCSCLITE_RUN_PID, O_RDONLY);
64  if (fd >= 0)
65  {
66  char pid_ascii[PID_ASCII_SIZE];
67  ssize_t r;
68 
69  r = read(fd, pid_ascii, sizeof pid_ascii);
70  if (r < 0)
71  {
72  Log2(PCSC_LOG_CRITICAL, "Reading " PCSCLITE_RUN_PID " failed: %s",
73  strerror(errno));
74  pid = -1;
75  }
76  else
77  pid = atoi(pid_ascii);
78  (void)close(fd);
79 
80  }
81  else
82  {
83  Log2(PCSC_LOG_CRITICAL, "Can't open " PCSCLITE_RUN_PID ": %s",
84  strerror(errno));
85  return -1;
86  }
87 
88  return pid;
89 } /* GetDaemonPid */
90 
91 int SendHotplugSignal(void)
92 {
93  pid_t pid;
94 
95  pid = GetDaemonPid();
96 
97  if (pid != -1)
98  {
99  Log2(PCSC_LOG_INFO, "Send hotplug signal to pcscd (pid=%d)", pid);
100  if (kill(pid, SIGUSR1) < 0)
101  {
102  Log3(PCSC_LOG_CRITICAL, "Can't signal pcscd (pid=%d): %s",
103  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 
137 long int time_sub(struct timeval *a, struct timeval *b)
138 {
139  struct timeval r;
140  r.tv_sec = a -> tv_sec - b -> tv_sec;
141  r.tv_usec = a -> tv_usec - b -> tv_usec;
142  if (r.tv_usec < 0)
143  {
144  r.tv_sec--;
145  r.tv_usec += 1000000;
146  }
147 
148  return r.tv_sec * 1000000 + r.tv_usec;
149 } /* time_sub */
150 
151 int ThreadCreate(pthread_t * pthThread, int attributes,
152  PCSCLITE_THREAD_FUNCTION(pvFunction), LPVOID pvArg)
153 {
154  pthread_attr_t attr;
155  size_t stack_size;
156  int ret;
157 
158  ret = pthread_attr_init(&attr);
159  if (ret)
160  return ret;
161 
162  ret = pthread_attr_setdetachstate(&attr,
163  attributes & THREAD_ATTR_DETACHED ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE);
164  if (ret)
165  goto error;
166 
167  /* stack size of 0x40000 (256 KB) bytes minimum for musl C lib */
168  ret = pthread_attr_getstacksize(&attr, &stack_size);
169  if (ret)
170  goto error;
171 
172  if (stack_size < 0x40000)
173  {
174  stack_size = 0x40000;
175  ret = pthread_attr_setstacksize(&attr, stack_size);
176  if (ret)
177  goto error;
178  }
179 
180  ret = pthread_create(pthThread, &attr, pvFunction, pvArg);
181 
182 error:
183  pthread_attr_destroy(&attr);
184  return ret;
185 }
#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:67
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:137
This keeps a list of defines for pcsc-lite.
This handles debugging.