• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 		/* child here */
36 		struct timespec tssleep;
37 
38 		tssleep.tv_sec = SLEEPSEC;
39 		tssleep.tv_nsec = 0;
40 		if (nanosleep(&tssleep, NULL) == 0) {
41 			printf("nanosleep() returned success\n");
42 			return CHILDPASS;
43 		} else {
44 			printf("nanosleep() did not return success\n");
45 			return CHILDFAIL;
46 		}
47 		return CHILDFAIL;
48 	} else {
49 		/* parent here */
50 		int i;
51 
52 		sleep(1);
53 
54 		if (kill(pid, SIGSTOP) != 0) {
55 			printf("Could not raise SIGSTOP\n");
56 			return PTS_UNRESOLVED;
57 		}
58 
59 		if (kill(pid, SIGCONT) != 0) {
60 			printf("Could not raise SIGCONT\n");
61 			return PTS_UNRESOLVED;
62 		}
63 
64 		if (wait(&i) == -1) {
65 			perror("Error waiting for child to exit\n");
66 			return PTS_UNRESOLVED;
67 		}
68 
69 		if (!WIFEXITED(i)) {
70 			printf("nanosleep() did not return 0\n");
71 			return PTS_FAIL;
72 		}
73 
74 		if (clock_gettime(CLOCK_REALTIME, &tsafter) == -1) {
75 			perror("Error in clock_gettime()\n");
76 			return PTS_UNRESOLVED;
77 		}
78 
79 		slepts = tsafter.tv_sec - tsbefore.tv_sec;
80 
81 		printf("Start %d sec; End %d sec\n", (int)tsbefore.tv_sec,
82 		       (int)tsafter.tv_sec);
83 		if (slepts >= SLEEPSEC) {
84 			printf("Test PASSED\n");
85 			return PTS_PASS;
86 		} else {
87 			printf("nanosleep() did not sleep long enough\n");
88 			return PTS_FAIL;
89 		}
90 
91 	}			//end fork
92 
93 	return PTS_UNRESOLVED;
94 }
95