1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
/* $OpenBSD: nanosleep.c,v 1.2 2002/02/17 05:33:33 art Exp $ */
/*
* Written by Artur Grabowski <art@openbsd.org> 2002 Public Domain.
*/
#include <sys/types.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <err.h>
#include <signal.h>
int trivial(void);
int with_signal(void);
int time_elapsed(void);
int time_elapsed_with_signal(void);
int short_time(void);
void sighandler(int);
int
main(int argc, char **argv)
{
int ch, ret;
ret = 0;
while ((ch = getopt(argc, argv, "tseES")) != -1) {
switch (ch) {
case 't':
ret |= trivial();
break;
case 's':
ret |= with_signal();
break;
case 'e':
ret |= time_elapsed();
break;
case 'E':
ret |= time_elapsed_with_signal();
break;
case 'S':
ret |= short_time();
default:
fprintf(stderr, "Usage: nanosleep [-tse]\n");
exit(1);
}
}
return (ret);
}
void
sighandler(int signum)
{
}
int
trivial(void)
{
struct timespec ts, rts;
ts.tv_sec = 0;
ts.tv_nsec = 30000000;
rts.tv_sec = 4711; /* Just add to the confusion */
rts.tv_nsec = 4711;
if (nanosleep(&ts, &rts) < 0) {
warn("trivial: nanosleep");
return 1;
}
/*
* Just check that we don't get any leftover time if we sleep the
* amount of time we want to sleep.
* If we receive any signal, something is wrong anyway.
*/
if (rts.tv_sec != 0 || rts.tv_nsec != 0) {
warnx("trivial: non-zero time? %d/%d", rts.tv_sec,
rts.tv_nsec);
return 1;
}
return 0;
}
int
with_signal(void)
{
struct timespec ts, rts;
pid_t pid;
int status;
signal(SIGUSR1, sighandler);
pid = getpid();
switch(fork()) {
case -1:
err(1, "fork");
default:
ts.tv_sec = 1;
ts.tv_nsec = 0;
nanosleep(&ts, NULL);
kill(pid, SIGUSR1);
exit(0);
}
ts.tv_sec = 10;
ts.tv_nsec = 0;
rts.tv_sec = 0;
rts.tv_nsec = 0;
if (nanosleep(&ts, &rts) == 0) {
warn("with-signal: nanosleep");
return 1;
}
if (rts.tv_sec == 0 && rts.tv_nsec == 0) {
warnx("with-signal: zero time");
return 1;
}
if (wait(&status) < 0)
err(1, "wait");
return 0;
}
int
time_elapsed(void)
{
struct timespec ts;
struct timeval stv, etv;
ts.tv_sec = 0;
ts.tv_nsec = 500000000;
if (gettimeofday(&stv, NULL) < 0) {
warn("gettimeofday");
return 1;
}
if (nanosleep(&ts, NULL) < 0) {
warn("nanosleep");
return 1;
}
if (gettimeofday(&etv, NULL) < 0) {
warn("gettimeofday");
return 1;
}
timersub(&etv, &stv, &stv);
if (stv.tv_sec == 0 && stv.tv_usec < 500000) {
warnx("slept less than 0.5 sec");
return 1;
}
return 0;
}
int
time_elapsed_with_signal(void)
{
struct timespec ts, rts;
struct timeval stv, etv;
pid_t pid;
int status;
signal(SIGUSR1, sighandler);
pid = getpid();
switch(fork()) {
case -1:
err(1, "fork");
default:
ts.tv_sec = 1;
ts.tv_nsec = 0;
nanosleep(&ts, NULL);
kill(pid, SIGUSR1);
exit(0);
}
ts.tv_sec = 10;
ts.tv_nsec = 0;
rts.tv_sec = 0;
rts.tv_nsec = 0;
if (gettimeofday(&stv, NULL) < 0) {
warn("gettimeofday");
return 1;
}
if (nanosleep(&ts, &rts) == 0) {
warn("nanosleep");
return 1;
}
if (gettimeofday(&etv, NULL) < 0) {
warn("gettimeofday");
return 1;
}
timersub(&etv, &stv, &stv);
etv.tv_sec = rts.tv_sec;
etv.tv_usec = rts.tv_nsec / 1000 + 1; /* the '+ 1' is a "roundup" */
timeradd(&etv, &stv, &stv);
if (stv.tv_sec == 0 && stv.tv_usec < 500000) {
warnx("slept less than 0.5 sec");
return 1;
}
if (wait(&status) < 0)
err(1, "wait");
return 0;
}
int
short_time(void)
{
struct timespec ts, rts;
pid_t pid;
int status;
signal(SIGUSR1, sighandler);
pid = getpid();
switch(fork()) {
case -1:
err(1, "fork");
default:
/* Sleep two seconds, then shoot parent. */
ts.tv_sec = 2;
ts.tv_nsec = 0;
nanosleep(&ts, NULL);
kill(pid, SIGUSR1);
exit(0);
}
ts.tv_sec = 0;
ts.tv_nsec = 1;
if (nanosleep(&ts, NULL) <= 0) {
warn("short_time: nanosleep");
return 1;
}
if (wait(&status) < 0)
err(1, "wait");
return 0;
}
|