pcsc-lite  2.0.3
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-2022
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 #define _GNU_SOURCE /* for secure_getenv(3) */
42 #include <sys/time.h>
43 #include <limits.h>
44 #include <stdlib.h>
45 #include <time.h>
46 #ifdef HAVE_GETRANDOM
47 #include <sys/random.h>
48 #endif /* HAVE_GETRANDOM */
49 #include <errno.h>
50 #include <string.h>
51 #include <unistd.h>
52 
53 #include "misc.h"
54 #include "sys_generic.h"
55 #include "debuglog.h"
56 
62 INTERNAL int SYS_Sleep(int iTimeVal)
63 {
64 #ifdef HAVE_NANOSLEEP
65  struct timespec mrqtp;
66  mrqtp.tv_sec = iTimeVal;
67  mrqtp.tv_nsec = 0;
68 
69  return nanosleep(&mrqtp, NULL);
70 #else
71  return sleep(iTimeVal);
72 #endif
73 }
74 
80 INTERNAL int SYS_USleep(int iTimeVal)
81 {
82 #ifdef HAVE_NANOSLEEP
83  struct timespec mrqtp;
84  mrqtp.tv_sec = iTimeVal/1000000;
85  mrqtp.tv_nsec = (iTimeVal - (mrqtp.tv_sec * 1000000)) * 1000;
86 
87  return nanosleep(&mrqtp, NULL);
88 #else
89  struct timeval tv;
90  tv.tv_sec = iTimeVal/1000000;
91  tv.tv_usec = iTimeVal - (tv.tv_sec * 1000000);
92  return select(0, NULL, NULL, NULL, &tv);
93 #endif
94 }
95 
108 INTERNAL int SYS_RandomInt(void)
109 {
110 #ifdef HAVE_GETRANDOM
111  unsigned int ui = 0;
112  unsigned char c[sizeof ui] = {0};
113  size_t i;
114  ssize_t ret;
115 
116  ret = getrandom(c, sizeof c, 0);
117  if (-1 == ret)
118  {
119  Log2(PCSC_LOG_ERROR, "getrandom() failed: %s", strerror(errno));
120  return lrand48();
121  }
122  // this loop avoids trap representations that may occur in the naive solution
123  for(i = 0; i < sizeof ui; i++) {
124  ui <<= CHAR_BIT;
125  ui |= c[i];
126  }
127  // the casts are for the sake of clarity
128  return (int)(ui & (unsigned int)INT_MAX);
129 #else
130  int r = lrand48(); // this is not thread-safe
131  return r;
132 #endif /* HAVE_GETRANDOM */
133 }
134 
138 INTERNAL void SYS_InitRandom(void)
139 {
140 #ifndef HAVE_GETRANDOM
141  struct timeval tv;
142  struct timezone tz;
143  long myseed = 0;
144 
145  tz.tz_minuteswest = 0;
146  tz.tz_dsttime = 0;
147  if (gettimeofday(&tv, &tz) == 0)
148  {
149  myseed = tv.tv_usec;
150  } else
151  {
152  myseed = (long) time(NULL);
153  }
154 
155  srand48(myseed);
156 #endif /* HAVE_GETRANDOM */
157 }
158 
166 INTERNAL const char * SYS_GetEnv(const char *name)
167 {
168 #ifdef HAVE_SECURE_GETENV
169  return secure_getenv(name);
170 #else
171  /* Otherwise, make sure current process is not tainted by uid or gid
172  * changes */
173 #ifdef HAVE_issetugid
174  if (issetugid())
175  return NULL;
176 #endif
177  return getenv(name);
178 #endif
179 }
180 
INTERNAL int SYS_RandomInt(void)
Generate a pseudo random number.
Definition: sys_unix.c:108
INTERNAL void SYS_InitRandom(void)
Initialize the random generator.
Definition: sys_unix.c:138
This handles abstract system level calls.
INTERNAL const char * SYS_GetEnv(const char *name)
(More) secure version of getenv(3)
Definition: sys_unix.c:166
INTERNAL int SYS_Sleep(int iTimeVal)
Makes the current process sleep for some seconds.
Definition: sys_unix.c:62
INTERNAL int SYS_USleep(int iTimeVal)
Makes the current process sleep for some microseconds.
Definition: sys_unix.c:80
This handles debugging.