pcsc-lite  1.8.11
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 Changes to this license can be made only by the copyright author with
20 explicit written consent.
21 
22 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * $Id: utils.c 6851 2014-02-14 15:43:32Z rousseau $
34  */
35 
41 #include <stdio.h>
42 #include <sys/types.h>
43 #include <unistd.h>
44 #include <errno.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <signal.h>
48 #include <dirent.h>
49 #include <fcntl.h>
50 #include <pthread.h>
51 
52 #include "config.h"
53 #include "debuglog.h"
54 #include "utils.h"
55 #include "pcscd.h"
56 #include "sys_generic.h"
57 
58 pid_t GetDaemonPid(void)
59 {
60  int fd;
61  pid_t pid;
62 
63  /* pids are only 15 bits but 4294967296
64  * (32 bits in case of a new system use it) is on 10 bytes
65  */
66  fd = open(PCSCLITE_RUN_PID, O_RDONLY);
67  if (fd >= 0)
68  {
69  char pid_ascii[PID_ASCII_SIZE];
70  ssize_t r;
71 
72  r = read(fd, pid_ascii, sizeof pid_ascii);
73  if (r < 0)
74  {
75  Log2(PCSC_LOG_CRITICAL, "Reading " PCSCLITE_RUN_PID " failed: %s",
76  strerror(errno));
77  pid = -1;
78  }
79  else
80  pid = atoi(pid_ascii);
81  (void)close(fd);
82 
83  }
84  else
85  {
86  Log2(PCSC_LOG_CRITICAL, "Can't open " PCSCLITE_RUN_PID ": %s",
87  strerror(errno));
88  return -1;
89  }
90 
91  return pid;
92 } /* GetDaemonPid */
93 
94 int SendHotplugSignal(void)
95 {
96  pid_t pid;
97 
98  pid = GetDaemonPid();
99 
100  if (pid != -1)
101  {
102  Log2(PCSC_LOG_INFO, "Send hotplug signal to pcscd (pid=%d)", pid);
103  if (kill(pid, SIGUSR1) < 0)
104  {
105  Log3(PCSC_LOG_CRITICAL, "Can't signal pcscd (pid=%d): %s",
106  pid, strerror(errno));
107  return EXIT_FAILURE ;
108  }
109  (void)SYS_Sleep(1);
110  }
111 
112  return EXIT_SUCCESS;
113 } /* SendHotplugSignal */
114 
122 #define OPENCT_FILE "/var/run/openct/status"
123 int CheckForOpenCT(void)
124 {
125  struct stat buf;
126 
127  if (0 == stat(OPENCT_FILE, &buf))
128  {
129  Log1(PCSC_LOG_CRITICAL, "File " OPENCT_FILE " found. Remove OpenCT and try again");
130  return 1;
131  }
132 
133  return 0;
134 } /* CheckForOpenCT */
135 
140 long int time_sub(struct timeval *a, struct timeval *b)
141 {
142  struct timeval r;
143  r.tv_sec = a -> tv_sec - b -> tv_sec;
144  r.tv_usec = a -> tv_usec - b -> tv_usec;
145  if (r.tv_usec < 0)
146  {
147  r.tv_sec--;
148  r.tv_usec += 1000000;
149  }
150 
151  return r.tv_sec * 1000000 + r.tv_usec;
152 } /* time_sub */
153 
154 int ThreadCreate(pthread_t * pthThread, int attributes,
155  PCSCLITE_THREAD_FUNCTION(pvFunction), LPVOID pvArg)
156 {
157  pthread_attr_t attr;
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  {
168  (void)pthread_attr_destroy(&attr);
169  return ret;
170  }
171 
172  ret = pthread_create(pthThread, &attr, pvFunction, pvArg);
173  if (ret)
174  return ret;
175 
176  ret = pthread_attr_destroy(&attr);
177  return ret;
178 }
#define OPENCT_FILE
Check is OpenCT is running and display a critical message if it is.
Definition: utils.c:122
This handles abstract system level calls.
int SYS_Sleep(int)
Makes the current process sleep for some seconds.
Definition: sys_unix.c:70
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:140
This keeps a list of defines for pcsc-lite.
This handles debugging.