1 /*
2 
3  * Copyright (c) 2002-2003, Intel Corporation. All rights reserved.
4  * Created by:  rusty.lynch REMOVE-THIS AT intel DOT com
5  * This file is licensed under the GPL license.  For the full content
6  * of this license, see the COPYING file at the top level of this
7  * source tree.
8 
9   Test case for assertion #22 of the sigaction system call that verifies
10   that if the SA_NODEFER flag is set for a given signal, then when the
11   sa_sigaction signal-catching function is entered, then the signal that
12   was caught is not added to the signal mask by raising that signal in the
13   signal handler and verifying that the handler is reentered.
14 
15   Steps:
16   1. Fork a new process
17   2. (parent) wait for child
18   3. (child) Setup a signal handler for SIGTSTP with SA_NODEFER set
19      in the sa_flags field
20   4. (child) raise SIGTSTP
21   5. (child, signal handler) increment handler count
22   6. (child, signal handler) if count is 1 then raise SIGTSTP
23   7. (child, signal handler) if count is 1 then set error variable
24   8. (child) if error is set then return -1, else return 0
25   6. (parent - returning from wait) If child returned 0 then exit 0,
26      otherwise exit -1.
27 */
28 
29 
30 #include <signal.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <sys/wait.h>
34 #include <unistd.h>
35 #include "posixtest.h"
36 
37 int handler_count = 0;
38 
handler(int signo LTP_ATTRIBUTE_UNUSED)39 void handler(int signo LTP_ATTRIBUTE_UNUSED)
40 {
41 	static int inside_handler = 0;
42 
43 	printf("SIGTSTP caught\n");
44 	if (inside_handler) {
45 		printf("Signal caught while inside handler\n");
46 		exit(0);
47 	}
48 
49 	inside_handler++;
50 	handler_count++;
51 
52 	if (handler_count == 1) {
53 		printf("Raising SIGTSTP\n");
54 		raise(SIGTSTP);
55 		printf("Returning from raising SIGTSTP\n");
56 	}
57 
58 	inside_handler--;
59 }
60 
main(void)61 int main(void)
62 {
63 	if (fork() == 0) {
64 		/* child */
65 
66 		struct sigaction act;
67 
68 		act.sa_handler = handler;
69 		act.sa_flags = SA_NODEFER;
70 		sigemptyset(&act.sa_mask);
71 		if (sigaction(SIGTSTP, &act, 0) == -1) {
72 			perror("Unexpected error while attempting to "
73 			       "setup test pre-conditions");
74 			return PTS_UNRESOLVED;
75 		}
76 
77 		if (raise(SIGTSTP) == -1) {
78 			perror("Unexpected error while attempting to "
79 			       "setup test pre-conditions");
80 			return PTS_UNRESOLVED;
81 		}
82 
83 		return PTS_FAIL;
84 	} else {
85 		int s;
86 
87 		/* parent */
88 		if (wait(&s) == -1) {
89 			perror("Unexpected error while setting up test "
90 			       "pre-conditions");
91 			return PTS_UNRESOLVED;
92 		}
93 
94 		if (!WEXITSTATUS(s)) {
95 			printf("Test PASSED\n");
96 			return PTS_PASS;
97 		}
98 	}
99 
100 	printf("Test FAILED\n");
101 	return PTS_FAIL;
102 }
103