1 /*
2 * Copyright (c) 2002, Intel Corporation. All rights reserved.
3 * Created by: julie.n.fleischer REMOVE-THIS AT intel DOT com
4 * This file is licensed under the GPL license. For the full content
5 * of this license, see the COPYING file at the top level of this
6 * source tree.
7 *
8 * Regression test motivated by an LKML discussion. Test that nanosleep()
9 * can be interrupted and then continue.
10 */
11 #include <stdio.h>
12 #include <time.h>
13 #include <signal.h>
14 #include <unistd.h>
15 #include <sys/wait.h>
16 #include <stdlib.h>
17 #include "posixtest.h"
18
19 #define SLEEPSEC 5
20
21 #define CHILDPASS 0 //if interrupted, child will return 0
22 #define CHILDFAIL 1
23
main(void)24 int main(void)
25 {
26 int pid, slepts;
27 struct timespec tsbefore, tsafter;
28
29 if (clock_gettime(CLOCK_REALTIME, &tsbefore) != 0) {
30 perror("clock_gettime() did not return success\n");
31 return PTS_UNRESOLVED;
32 }
33
34 if ((pid = fork()) < 0) {
35 printf("fork() did not return success\n");
36 return PTS_UNRESOLVED;
37 } else if (pid == 0) {
38 /* child here */
39 struct timespec tssleep;
40
41 tssleep.tv_sec = SLEEPSEC;
42 tssleep.tv_nsec = 0;
43 if (nanosleep(&tssleep, NULL) == 0) {
44 printf("nanosleep() returned success\n");
45 return CHILDPASS;
46 } else {
47 printf("nanosleep() did not return success\n");
48 return CHILDFAIL;
49 }
50 return CHILDFAIL;
51 } else {
52 /* parent here */
53 int i;
54
55 sleep(1);
56
57 if (kill(pid, SIGSTOP) != 0) {
58 printf("Could not raise SIGSTOP\n");
59 return PTS_UNRESOLVED;
60 }
61
62 if (kill(pid, SIGCONT) != 0) {
63 printf("Could not raise SIGCONT\n");
64 return PTS_UNRESOLVED;
65 }
66
67 if (wait(&i) == -1) {
68 perror("Error waiting for child to exit\n");
69 return PTS_UNRESOLVED;
70 }
71
72 if (!WIFEXITED(i)) {
73 printf("nanosleep() did not return 0\n");
74 return PTS_FAIL;
75 }
76
77 if (clock_gettime(CLOCK_REALTIME, &tsafter) == -1) {
78 perror("Error in clock_gettime()\n");
79 return PTS_UNRESOLVED;
80 }
81
82 slepts = tsafter.tv_sec - tsbefore.tv_sec;
83
84 printf("Start %d sec; End %d sec\n", (int)tsbefore.tv_sec,
85 (int)tsafter.tv_sec);
86 if (slepts >= SLEEPSEC) {
87 printf("Test PASSED\n");
88 return PTS_PASS;
89 } else {
90 printf("nanosleep() did not sleep long enough\n");
91 return PTS_FAIL;
92 }
93
94 } //end fork
95
96 return PTS_UNRESOLVED;
97 }
98