pcsc-lite  1.9.9
sys_unix.c
Go to the documentation of this file.
1 /*
2  * This handles abstract system level calls.
3  *
4  * MUSCLE SmartCard Development ( https://pcsclite.apdu.fr/ )
5  *
6  * Copyright (C) 1999
7  * David Corcoran <corcoran@musclecard.com>
8  * Copyright (C) 2002-2010
9  * Ludovic Rousseau <ludovic.rousseau@free.fr>
10  *
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions
13 are met:
14 
15 1. Redistributions of source code must retain the above copyright
16  notice, this list of conditions and the following disclaimer.
17 2. Redistributions in binary form must reproduce the above copyright
18  notice, this list of conditions and the following disclaimer in the
19  documentation and/or other materials provided with the distribution.
20 3. The name of the author may not be used to endorse or promote products
21  derived from this software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
40 #include "config.h"
41 #include <sys/time.h>
42 #include <limits.h>
43 #include <stdlib.h>
44 #include <time.h>
45 #ifdef HAVE_GETRANDOM
46 #include <sys/random.h>
47 #endif /* HAVE_GETRANDOM */
48 #include <errno.h>
49 #include <string.h>
50 
51 #include "misc.h"
52 #include "sys_generic.h"
53 #include "PCSC/debuglog.h"
54 
60 INTERNAL int SYS_Sleep(int iTimeVal)
61 {
62 #ifdef HAVE_NANOSLEEP
63  struct timespec mrqtp;
64  mrqtp.tv_sec = iTimeVal;
65  mrqtp.tv_nsec = 0;
66 
67  return nanosleep(&mrqtp, NULL);
68 #else
69  return sleep(iTimeVal);
70 #endif
71 }
72 
78 INTERNAL int SYS_USleep(int iTimeVal)
79 {
80 #ifdef HAVE_NANOSLEEP
81  struct timespec mrqtp;
82  mrqtp.tv_sec = iTimeVal/1000000;
83  mrqtp.tv_nsec = (iTimeVal - (mrqtp.tv_sec * 1000000)) * 1000;
84 
85  return nanosleep(&mrqtp, NULL);
86 #else
87  struct timeval tv;
88  tv.tv_sec = iTimeVal/1000000;
89  tv.tv_usec = iTimeVal - (tv.tv_sec * 1000000);
90  return select(0, NULL, NULL, NULL, &tv);
91 #endif
92 }
93 
106 INTERNAL int SYS_RandomInt(void)
107 {
108 #ifdef HAVE_GETRANDOM
109  unsigned int ui = 0;
110  unsigned char c[sizeof ui] = {0};
111  size_t i;
112  ssize_t ret;
113 
114  ret = getrandom(c, sizeof c, 0);
115  if (-1 == ret)
116  {
117  Log2(PCSC_LOG_ERROR, "getrandom() failed: %s", strerror(errno));
118  return lrand48();
119  }
120  // this loop avoids trap representations that may occur in the naive solution
121  for(i = 0; i < sizeof ui; i++) {
122  ui <<= CHAR_BIT;
123  ui |= c[i];
124  }
125  // the casts are for the sake of clarity
126  return (int)(ui & (unsigned int)INT_MAX);
127 #else
128  int r = lrand48(); // this is not thread-safe
129  return r;
130 #endif /* HAVE_GETRANDOM */
131 }
132 
136 INTERNAL void SYS_InitRandom(void)
137 {
138 #ifndef HAVE_GETRANDOM
139  struct timeval tv;
140  struct timezone tz;
141  long myseed = 0;
142 
143  tz.tz_minuteswest = 0;
144  tz.tz_dsttime = 0;
145  if (gettimeofday(&tv, &tz) == 0)
146  {
147  myseed = tv.tv_usec;
148  } else
149  {
150  myseed = (long) time(NULL);
151  }
152 
153  srand48(myseed);
154 #endif /* HAVE_GETRANDOM */
155 }
156 
This handles debugging.
This handles abstract system level calls.
INTERNAL int SYS_RandomInt(void)
Generate a pseudo random number.
Definition: sys_unix.c:106
INTERNAL int SYS_Sleep(int iTimeVal)
Makes the current process sleep for some seconds.
Definition: sys_unix.c:60
INTERNAL int SYS_USleep(int iTimeVal)
Makes the current process sleep for some microseconds.
Definition: sys_unix.c:78
INTERNAL void SYS_InitRandom(void)
Initialize the random generator.
Definition: sys_unix.c:136