pcsc-lite  2.5.0
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-2024
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 #ifdef PCSCD
120  Log2(PCSC_LOG_ERROR, "getrandom() failed: %s", strerror(errno));
121 #endif
122  return lrand48();
123  }
124  // this loop avoids trap representations that may occur in the naive solution
125  for(i = 0; i < sizeof ui; i++) {
126  ui <<= CHAR_BIT;
127  ui |= c[i];
128  }
129  // the casts are for the sake of clarity
130  return (int)(ui & (unsigned int)INT_MAX);
131 #else
132  int r = (int)lrand48(); // this is not thread-safe
133  return r;
134 #endif /* HAVE_GETRANDOM */
135 }
136 
140 INTERNAL void SYS_InitRandom(void)
141 {
142 #ifndef HAVE_GETRANDOM
143  struct timeval tv;
144  struct timezone tz;
145  long myseed = 0;
146 
147  tz.tz_minuteswest = 0;
148  tz.tz_dsttime = 0;
149  if (gettimeofday(&tv, &tz) == 0)
150  {
151  myseed = tv.tv_usec;
152  } else
153  {
154  myseed = (long) time(NULL);
155  }
156 
157  srand48(myseed);
158 #endif /* HAVE_GETRANDOM */
159 }
160 
168 INTERNAL const char * SYS_GetEnv(const char *name)
169 {
170 #ifdef HAVE_SECURE_GETENV
171  return secure_getenv(name);
172 #else
173  /* Otherwise, make sure current process is not tainted by uid or gid
174  * changes */
175  if (issetugid())
176  return NULL;
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:140
This handles abstract system level calls.
INTERNAL const char * SYS_GetEnv(const char *name)
(More) secure version of getenv(3)
Definition: sys_unix.c:168
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.