pcsc-lite  1.8.16
sys_unix.c
Go to the documentation of this file.
1 /*
2  * This handles abstract system level calls.
3  *
4  * MUSCLE SmartCard Development ( http://pcsclite.alioth.debian.org/pcsclite.html )
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/types.h>
42 #include <sys/mman.h>
43 #include <sys/stat.h>
44 #include <sys/wait.h>
45 #include <sys/time.h>
46 #include <sys/file.h>
47 #include <fcntl.h>
48 #include <errno.h>
49 #include <unistd.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <signal.h>
54 #include <time.h>
55 
56 #include "misc.h"
57 #include "sys_generic.h"
58 #include "debuglog.h"
59 
65 INTERNAL int SYS_Sleep(int iTimeVal)
66 {
67 #ifdef HAVE_NANOSLEEP
68  struct timespec mrqtp;
69  mrqtp.tv_sec = iTimeVal;
70  mrqtp.tv_nsec = 0;
71 
72  return nanosleep(&mrqtp, NULL);
73 #else
74  return sleep(iTimeVal);
75 #endif
76 }
77 
83 INTERNAL int SYS_USleep(int iTimeVal)
84 {
85 #ifdef HAVE_NANOSLEEP
86  struct timespec mrqtp;
87  mrqtp.tv_sec = iTimeVal/1000000;
88  mrqtp.tv_nsec = (iTimeVal - (mrqtp.tv_sec * 1000000)) * 1000;
89 
90  return nanosleep(&mrqtp, NULL);
91 #else
92  struct timeval tv;
93  tv.tv_sec = iTimeVal/1000000;
94  tv.tv_usec = iTimeVal - (tv.tv_sec * 1000000);
95  return select(0, NULL, NULL, NULL, &tv);
96 #endif
97 }
98 
99 INTERNAL int SYS_RandomInt(int fStart, int fEnd)
100 {
101  static int iInitialized = 0;
102  int iRandNum = 0;
103 
104  if (0 == iInitialized)
105  {
106  srand(SYS_GetSeed());
107  iInitialized = 1;
108  }
109 
110  if (-1 == fEnd)
111  /* full int range */
112  iRandNum = rand();
113  else
114  iRandNum = ((rand()+0.0)/RAND_MAX * (fEnd - fStart)) + fStart;
115 
116  return iRandNum;
117 }
118 
119 INTERNAL int SYS_GetSeed(void)
120 {
121  struct timeval tv;
122  struct timezone tz;
123  long myseed = 0;
124 
125  tz.tz_minuteswest = 0;
126  tz.tz_dsttime = 0;
127  if (gettimeofday(&tv, &tz) == 0)
128  {
129  myseed = tv.tv_usec;
130  } else
131  {
132  myseed = (long) time(NULL);
133  }
134  return myseed;
135 }
136 
This handles abstract system level calls.
int SYS_Sleep(int)
Makes the current process sleep for some seconds.
Definition: sys_unix.c:65
int SYS_USleep(int)
Makes the current process sleep for some microseconds.
Definition: sys_unix.c:83
This handles debugging.