pcsc-lite  1.8.20
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=%ld)",
98  (long)pid);
99  if (kill(pid, SIGUSR1) < 0)
100  {
101  Log3(PCSC_LOG_CRITICAL, "Can't signal pcscd (pid=%ld): %s",
102  (long)pid, strerror(errno));
103  return EXIT_FAILURE ;
104  }
105  (void)SYS_Sleep(1);
106  }
107 
108  return EXIT_SUCCESS;
109 } /* SendHotplugSignal */
110 
118 #define OPENCT_FILE "/var/run/openct/status"
119 int CheckForOpenCT(void)
120 {
121  struct stat buf;
122 
123  if (0 == stat(OPENCT_FILE, &buf))
124  {
125  Log1(PCSC_LOG_CRITICAL, "File " OPENCT_FILE " found. Remove OpenCT and try again");
126  return 1;
127  }
128 
129  return 0;
130 } /* CheckForOpenCT */
131 
136 long int time_sub(struct timeval *a, struct timeval *b)
137 {
138  struct timeval r;
139  r.tv_sec = a -> tv_sec - b -> tv_sec;
140  r.tv_usec = a -> tv_usec - b -> tv_usec;
141  if (r.tv_usec < 0)
142  {
143  r.tv_sec--;
144  r.tv_usec += 1000000;
145  }
146 
147  return r.tv_sec * 1000000 + r.tv_usec;
148 } /* time_sub */
149 
150 int ThreadCreate(pthread_t * pthThread, int attributes,
151  PCSCLITE_THREAD_FUNCTION(pvFunction), LPVOID pvArg)
152 {
153  pthread_attr_t attr;
154  size_t stack_size;
155  int ret;
156 
157  ret = pthread_attr_init(&attr);
158  if (ret)
159  return ret;
160 
161  ret = pthread_attr_setdetachstate(&attr,
162  attributes & THREAD_ATTR_DETACHED ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE);
163  if (ret)
164  goto error;
165 
166  /* stack size of 0x40000 (256 KB) bytes minimum for musl C lib */
167  ret = pthread_attr_getstacksize(&attr, &stack_size);
168  if (ret)
169  goto error;
170 
171  /* A stack_size of 0 indicates the default size on Solaris.
172  * The default size is more than 256 KB so do not shrink it. */
173  if ((stack_size != 0) && (stack_size < 0x40000))
174  {
175  stack_size = 0x40000;
176  ret = pthread_attr_setstacksize(&attr, stack_size);
177  if (ret)
178  goto error;
179  }
180 
181  ret = pthread_create(pthThread, &attr, pvFunction, pvArg);
182 
183 error:
184  pthread_attr_destroy(&attr);
185  return ret;
186 }
#define OPENCT_FILE
Check is OpenCT is running and display a critical message if it is.
Definition: utils.c:118
This handles abstract system level calls.
int SYS_Sleep(int)
Makes the current process sleep for some seconds.
Definition: sys_unix.c:53
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:136
This keeps a list of defines for pcsc-lite.
This handles debugging.