1 /******************************************************************************/
2 /* Copyright (c) Crackerjack Project., 2007-2008 ,Hitachi, Ltd */
3 /* Author(s): Takahiro Yasui <takahiro.yasui.mp@hitachi.com>, */
4 /* Yumiko Sugita <yumiko.sugita.yf@hitachi.com>, */
5 /* Satoshi Fujiwara <sa-fuji@sdl.hitachi.co.jp> */
6 /* */
7 /* This program is free software; you can redistribute it and/or modify */
8 /* it under the terms of the GNU General Public License as published by */
9 /* the Free Software Foundation; either version 2 of the License, or */
10 /* (at your option) any later version. */
11 /* */
12 /* This program is distributed in the hope that it will be useful, */
13 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
14 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */
15 /* the GNU General Public License for more details. */
16 /* */
17 /* You should have received a copy of the GNU General Public License */
18 /* along with this program; if not, write to the Free Software */
19 /* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
20 /* */
21 /******************************************************************************/
22 /******************************************************************************/
23 /* */
24 /* File: clock_nanosleep01.c */
25 /* */
26 /* Description: This tests the clock_nanosleep() syscall */
27 /* */
28 /* */
29 /* */
30 /* */
31 /* */
32 /* */
33 /* Usage: <for command-line> */
34 /* clock_nanosleep01 [-c n] [-e][-i n] [-I x] [-p x] [-t] */
35 /* where, -c n : Run n copies concurrently. */
36 /* -e : Turn on errno logging. */
37 /* -i n : Execute test n times. */
38 /* -I x : Execute test for x seconds. */
39 /* -P x : Pause for x seconds between iterations. */
40 /* -t : Turn on syscall timing. */
41 /* */
42 /* Total Tests: 1 */
43 /* */
44 /* Test Name: clock_nanosleep01 */
45 /* History: Porting from Crackerjack to LTP is done by */
46 /* Manas Kumar Nayak maknayak@in.ibm.com> */
47 /******************************************************************************/
48 #include <sys/syscall.h>
49 #include <sys/types.h>
50 #include <getopt.h>
51 #include <string.h>
52 #include <stdlib.h>
53 #include <errno.h>
54 #include <stdio.h>
55 #include <time.h>
56 #include <signal.h>
57 #include "../utils/common_j_h.c"
58 #include "../utils/include_j_h.h"
59
60 #include "test.h"
61 #include "linux_syscall_numbers.h"
62
63 char *TCID = "clock_nanosleep01";
64 int testno;
65 int TST_TOTAL = 1;
66 struct sigaction act;
67
68 /*
69 * sighandler()
70 */
sighandler(int sig)71 void sighandler(int sig)
72 {
73 if (sig == SIGINT)
74 return;
75
76 return;
77 }
78
79 /* Extern Global Functions */
80 /******************************************************************************/
81 /* */
82 /* Function: cleanup */
83 /* */
84 /* Description: Performs all one time clean up for this test on successful */
85 /* completion, premature exit or failure. Closes all temporary */
86 /* files, removes all temporary directories exits the test with */
87 /* appropriate return code by calling tst_exit() function. */
88 /* */
89 /* Input: None. */
90 /* */
91 /* Output: None. */
92 /* */
93 /* Return: On failure - Exits calling tst_exit(). Non '0' return code. */
94 /* On success - Exits calling tst_exit(). With '0' return code. */
95 /* */
96 /******************************************************************************/
cleanup(void)97 void cleanup(void)
98 {
99
100 tst_rmdir();
101
102 }
103
104 /* Local Functions */
105 /******************************************************************************/
106 /* */
107 /* Function: setup */
108 /* */
109 /* Description: Performs all one time setup for this test. This function is */
110 /* typically used to capture signals, create temporary dirs */
111 /* and temporary files that may be used in the course of this */
112 /* test. */
113 /* */
114 /* Input: None. */
115 /* */
116 /* Output: None. */
117 /* */
118 /* Return: On failure - Exits by calling cleanup(). */
119 /* On success - returns 0. */
120 /* */
121 /******************************************************************************/
setup(void)122 void setup(void)
123 {
124 /* Capture signals if any */
125 act.sa_handler = sighandler;
126 sigfillset(&act.sa_mask);
127 sigaction(SIGINT, &act, NULL);
128
129 /* Create temporary directories */
130 TEST_PAUSE;
131 tst_tmpdir();
132 }
133
134 /*
135 * Macros
136 */
137 #define SYSCALL_NAME "clock_nanosleep"
138
139 enum test_type {
140 NORMAL,
141 NULL_POINTER,
142 SEND_SIGINT,
143 };
144
145 /*
146 * Data Structure
147 */
148 struct test_case {
149 clockid_t clk_id;
150 int ttype;
151 int flags;
152 time_t sec;
153 long nsec;
154 int ret;
155 int err;
156 };
157
158 /* Test cases
159 *
160 * test status of errors on man page
161 *
162 * EINTR v (function was interrupted by a signal)
163 * EINVAL v (invalid tv_nsec, etc.)
164 * ENOTSUP can't check because not supported clk_id generates
165 * EINVAL
166 */
167
168 static struct test_case tcase[] = {
169 { // case00
170 .clk_id = CLOCK_REALTIME,
171 .ttype = NORMAL,
172 .flags = 0,
173 .sec = 0,
174 .nsec = 500000000, // 500msec
175 .ret = 0,
176 .err = 0,
177 },
178 { // case01
179 .clk_id = CLOCK_MONOTONIC,
180 .ttype = NORMAL,
181 .flags = 0,
182 .sec = 0,
183 .nsec = 500000000, // 500msec
184 .ret = 0,
185 .err = 0,
186 },
187 { // case02
188 .ttype = NORMAL,
189 .clk_id = CLOCK_REALTIME,
190 .flags = 0,
191 .sec = 0,
192 .nsec = -1, // invalid
193 .ret = EINVAL,
194 .err = 0,
195 },
196 { // case03
197 .ttype = NORMAL,
198 .clk_id = CLOCK_REALTIME,
199 .flags = 0,
200 .sec = 0,
201 .nsec = 1000000000, // invalid
202 .ret = EINVAL,
203 .err = 0,
204 },
205 { // case04
206 .ttype = NORMAL,
207 .clk_id = CLOCK_THREAD_CPUTIME_ID, // not supported
208 .flags = 0,
209 .sec = 0,
210 .nsec = 500000000, // 500msec
211 .ret = EINVAL, // RHEL4U1 + 2.6.18 returns EINVAL
212 .err = 0,
213 },
214 { // case05
215 .ttype = SEND_SIGINT,
216 .clk_id = CLOCK_REALTIME,
217 .flags = 0,
218 .sec = 10,
219 .nsec = 0,
220 .ret = EINTR,
221 .err = 0,
222 },
223 #if 0 // glibc generates SEGV error (RHEL4U1 + 2.6.18)
224 { // caseXX
225 .ttype = NULL_POINTER,
226 .clk_id = CLOCK_REALTIME,
227 .flags = 0,
228 .sec = 0,
229 .nsec = 500000000, // 500msec
230 .ret = EFAULT,
231 .err = 0,
232 },
233 #endif
234 };
235
236 /*
237 * chk_difftime()
238 * Return : OK(0), NG(-1)
239 */
240 #define MAX_MSEC_DIFF 20
241
chk_difftime(struct timespec * bef,struct timespec * aft,time_t sec,long nsec)242 static int chk_difftime(struct timespec *bef, struct timespec *aft,
243 time_t sec, long nsec)
244 {
245 struct timespec t;
246 time_t expect;
247 time_t result;
248
249 t.tv_sec = aft->tv_sec - bef->tv_sec;
250 t.tv_nsec = aft->tv_nsec - bef->tv_nsec;
251 if (t.tv_nsec < 0) {
252 t.tv_sec -= 1;
253 t.tv_nsec += 1000000000;
254 }
255 expect = (sec * 1000) + (nsec / 1000000);
256 result = (t.tv_sec * 1000) + (t.tv_nsec / 1000000);
257 tst_resm(TINFO, "check sleep time: (min:%ld) < %ld < (max:%ld) (msec)",
258 expect - MAX_MSEC_DIFF, result, expect + MAX_MSEC_DIFF);
259 if (result < expect - MAX_MSEC_DIFF || result > expect + MAX_MSEC_DIFF)
260 return -1;
261 return 0;
262 }
263
264 /*
265 * do_test()
266 *
267 * Input : TestCase Data
268 * Return : RESULT_OK(0), RESULT_NG(1)
269 *
270 */
do_test(struct test_case * tc)271 static int do_test(struct test_case *tc)
272 {
273 int sys_ret;
274 int sys_errno;
275 int result = RESULT_OK;
276 struct timespec beftp, afttp, rq, rm;
277 int rc, range_ok = 1, remain_ok = 1;
278 pid_t pid = 0;
279
280 /*
281 * Check before sleep time
282 */
283 if (tc->ttype == SEND_SIGINT) {
284 pid = create_sig_proc(500000, SIGINT, UINT_MAX);
285 if (pid < 0)
286 return 1;
287 }
288
289 /*
290 * Check before sleep time
291 */
292 TEST(rc = clock_gettime(tc->clk_id, &beftp));
293 if (rc < 0) {
294 tst_resm(TFAIL | TTERRNO, "clock_gettime failed");
295 result = 1;
296 goto EXIT;
297 }
298 /*
299 * Execute system call
300 */
301 rq.tv_sec = tc->sec;
302 rq.tv_nsec = tc->nsec;
303 // !!!CAUTION: 'clock_gettime' returns errno itself
304 errno = 0;
305 if (tc->ttype == NULL_POINTER)
306 TEST(sys_ret =
307 clock_nanosleep(tc->clk_id, tc->flags, NULL, &rm));
308 else
309 TEST(sys_ret =
310 clock_nanosleep(tc->clk_id, tc->flags, &rq, &rm));
311 sys_errno = errno;
312 if (sys_ret != 0)
313 goto TEST_END;
314
315 /*
316 * Check after sleep time
317 */
318 TEST(rc = clock_gettime(tc->clk_id, &afttp));
319 if (TEST_RETURN < 0) {
320 EPRINTF("clock_gettime failed.\n");
321 result = 1;
322 goto EXIT;
323 }
324 range_ok = chk_difftime(&beftp, &afttp, tc->sec, tc->nsec) == 0;
325 /*
326 * Check remaining time
327 */
328 TEST_END:
329 if (tc->ttype == NORMAL || tc->ttype == SEND_SIGINT) {
330 tst_resm(TINFO, "remain time: %ld %ld", rm.tv_sec, rm.tv_nsec);
331 if (tc->ttype == NORMAL)
332 remain_ok = 1;
333 else
334 remain_ok = rm.tv_sec != 0 || rm.tv_nsec != 0;
335 }
336
337 /*
338 * Check results
339 */
340 result |= (sys_ret != tc->ret) || !range_ok || !remain_ok;
341 if (!range_ok)
342 PRINT_RESULT_EXTRA(0, tc->ret, tc->err, sys_ret, sys_errno,
343 "time range check", range_ok);
344 else
345 PRINT_RESULT_EXTRA(0, tc->ret, tc->err, sys_ret, sys_errno,
346 "remain time check", remain_ok);
347 EXIT:
348 if (pid > 0) {
349 int st;
350 TEST(kill(pid, SIGTERM));
351 TEST(wait(&st));
352 }
353 return result;
354 }
355
356 /*
357 * main()
358 */
359
main(int ac,char ** av)360 int main(int ac, char **av)
361 {
362 int result = RESULT_OK;
363 int i;
364 int lc;
365
366 tst_parse_opts(ac, av, NULL, NULL);
367
368 setup();
369
370 for (lc = 0; TEST_LOOPING(lc); ++lc) {
371
372 tst_count = 0;
373
374 for (testno = 0; testno < TST_TOTAL; ++testno) {
375
376 for (i = 0; i < (int)(sizeof(tcase) / sizeof(tcase[0]));
377 i++) {
378 int ret;
379 tst_resm(TINFO, "(case%02d) START", i);
380 ret = do_test(&tcase[i]);
381 tst_resm(TINFO, "(case%02d) END => %s",
382 i, (ret == 0) ? "OK" : "NG");
383 result |= ret;
384 }
385
386 switch (result) {
387 case RESULT_OK:
388 tst_resm(TPASS,
389 "clock_nanosleep call succeeded");
390 break;
391
392 default:
393 tst_brkm(TFAIL | TERRNO, cleanup,
394 "clock_nanosleep failed");
395 break;
396 }
397
398 }
399
400 }
401
402 cleanup();
403 tst_exit();
404
405 }
406