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  * Test that nanosleep() sets rmtp to the time remaining if
9  * it is interrupted by a signal.
10  * If time remaining is within OKDELTA difference, the test is a pass.
11  */
12 #include <stdio.h>
13 #include <time.h>
14 #include <signal.h>
15 #include <unistd.h>
16 #include <sys/wait.h>
17 #include <stdlib.h>
18 #include "posixtest.h"
19 
20 #define CHILDSUCCESS 1
21 #define CHILDFAILURE 0
22 
23 #define OKDELTA 1
24 
handler(int signo LTP_ATTRIBUTE_UNUSED)25 void handler(int signo LTP_ATTRIBUTE_UNUSED)
26 {
27 	printf("In handler\n");
28 }
29 
main(void)30 int main(void)
31 {
32 	struct timespec tssleepfor, tsstorage, tsbefore, tsafter;
33 	int sleepsec = 30;
34 	int pid;
35 	struct sigaction act;
36 
37 	if (clock_gettime(CLOCK_REALTIME, &tsbefore) == -1) {
38 		perror("Error in clock_gettime()\n");
39 		return PTS_UNRESOLVED;
40 	}
41 
42 	if ((pid = fork()) == 0) {
43 		/* child here */
44 		int sleptplusremaining;
45 
46 		act.sa_handler = handler;
47 		act.sa_flags = 0;
48 		if (sigemptyset(&act.sa_mask) == -1) {
49 			perror("Error calling sigemptyset\n");
50 			return CHILDFAILURE;
51 		}
52 		if (sigaction(SIGABRT, &act, 0) == -1) {
53 			perror("Error calling sigaction\n");
54 			return CHILDFAILURE;
55 		}
56 		tssleepfor.tv_sec = sleepsec;
57 		tssleepfor.tv_nsec = 0;
58 		if (nanosleep(&tssleepfor, &tsstorage) != -1) {
59 			printf("nanosleep() was not interrupted\n");
60 			return CHILDFAILURE;
61 		}
62 
63 		if (clock_gettime(CLOCK_REALTIME, &tsafter) == -1) {
64 			perror("Error in clock_gettime()\n");
65 			return CHILDFAILURE;
66 		}
67 
68 		sleptplusremaining = (tsafter.tv_sec - tsbefore.tv_sec) +
69 		    tsstorage.tv_sec;
70 
71 		if (abs(sleptplusremaining - sleepsec) <= OKDELTA) {
72 			printf("PASS - within %d difference\n",
73 			       abs(sleptplusremaining - sleepsec));
74 			return CHILDSUCCESS;
75 		} else {
76 			printf("FAIL - within %d difference\n",
77 			       abs(sleptplusremaining - sleepsec));
78 			return CHILDFAILURE;
79 		}
80 
81 	} else {
82 		/* parent here */
83 		int i;
84 
85 		sleep(1);
86 
87 		if (kill(pid, SIGABRT) != 0) {
88 			printf("Could not raise signal being tested\n");
89 			return PTS_UNRESOLVED;
90 		}
91 
92 		if (wait(&i) == -1) {
93 			perror("Error waiting for child to exit\n");
94 			return PTS_UNRESOLVED;
95 		}
96 
97 		if (WIFEXITED(i) && WEXITSTATUS(i)) {
98 			printf("Child exited normally\n");
99 			printf("Test PASSED\n");
100 			return PTS_PASS;
101 		} else {
102 			printf("Child did not exit normally.\n");
103 			printf("Test FAILED\n");
104 			return PTS_FAIL;
105 		}
106 	}
107 	return PTS_UNRESOLVED;
108 }
109