• 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  * Test that nanosleep() sets errno to EINTR if it is interrupted by a signal.
9  * Test by sending a signal to a child doing nanosleep().  If nanosleep
10  * errno = EINTR, return success from the child.
11  */
12 #include <stdio.h>
13 #include <time.h>
14 #include <signal.h>
15 #include <unistd.h>
16 #include <sys/wait.h>
17 #include <errno.h>
18 #include "posixtest.h"
19 
20 #define CHILDSUCCESS 1
21 #define CHILDFAILURE 0
22 
handler(int signo LTP_ATTRIBUTE_UNUSED)23 void handler(int signo LTP_ATTRIBUTE_UNUSED)
24 {
25 	printf("In handler\n");
26 }
27 
main(void)28 int main(void)
29 {
30 	struct timespec tssleepfor, tsstorage;
31 	int sleepsec = 30;
32 	int pid;
33 	struct sigaction act;
34 
35 	if ((pid = fork()) == 0) {
36 		/* child here */
37 
38 		act.sa_handler = handler;
39 		act.sa_flags = 0;
40 		if (sigemptyset(&act.sa_mask) == -1) {
41 			perror("Error calling sigemptyset\n");
42 			return CHILDFAILURE;
43 		}
44 		if (sigaction(SIGABRT, &act, 0) == -1) {
45 			perror("Error calling sigaction\n");
46 			return CHILDFAILURE;
47 		}
48 		tssleepfor.tv_sec = sleepsec;
49 		tssleepfor.tv_nsec = 0;
50 		if (nanosleep(&tssleepfor, &tsstorage) == -1) {
51 			if (EINTR == errno) {
52 				printf("errno == EINTR\n");
53 				return CHILDSUCCESS;
54 			} else {
55 				printf("errno != EINTR\n");
56 				return CHILDFAILURE;
57 			}
58 		} else {
59 			printf("nanosleep did not return -1\n");
60 			return CHILDFAILURE;
61 		}
62 
63 		return CHILDFAILURE;
64 	} else {
65 		/* parent here */
66 		int i;
67 
68 		sleep(1);
69 
70 		if (kill(pid, SIGABRT) != 0) {
71 			printf("Could not raise signal being tested\n");
72 			return PTS_UNRESOLVED;
73 		}
74 
75 		if (wait(&i) == -1) {
76 			perror("Error waiting for child to exit\n");
77 			return PTS_UNRESOLVED;
78 		}
79 
80 		if (WIFEXITED(i) && WEXITSTATUS(i)) {
81 			printf("Test PASSED\n");
82 			return PTS_PASS;
83 		} else {
84 			printf("Child did not exit normally.\n");
85 			printf("Test FAILED\n");
86 			return PTS_FAIL;
87 		}
88 	}
89 	return PTS_UNRESOLVED;
90 }
91