• 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 the raise(<signal>) function shall send the signal
9  *  to the executing process when the executing process is a child
10  *  process.
11  *  1) Set up a signal handler for a signal in the parent process.
12  *     This handler returns failure if called.
13  *  2) Fork a child process.
14  *  2) In the child process:
15  *     3) Set up a signal handler for the same signal as in the parent process.
16  *        This handler returns success if called.
17  *     4) Raise the signal.
18  *     5) If signal handler was called, return 1 (so WEXITSTATUS can test
19  *        for success).
20  *  6) In parent, if 1 was received, test passed.
21  *  This test is only performed on one signal.  All other signals are
22  *  considered to be in the same equivalence class.
23  */
24 
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30 #include <sys/wait.h>
31 #include "posixtest.h"
32 
33 #define SIGTOTEST SIGABRT
34 
parenthandler(int signo PTS_ATTRIBUTE_UNUSED)35 static void parenthandler(int signo PTS_ATTRIBUTE_UNUSED)
36 {
37 	printf("Caught signal from parent!\n");
38 	exit(-1);
39 }
40 
childhandler(int signo PTS_ATTRIBUTE_UNUSED)41 static void childhandler(int signo PTS_ATTRIBUTE_UNUSED)
42 {
43 	printf("Caught signal from child!\n");
44 	exit(0);
45 }
46 
main(void)47 int main(void)
48 {
49 	struct sigaction parentact;
50 
51 	parentact.sa_handler = parenthandler;
52 	parentact.sa_flags = 0;
53 	if (sigemptyset(&parentact.sa_mask) == -1) {
54 		perror("Error calling sigemptyset\n");
55 		return PTS_UNRESOLVED;
56 	}
57 	if (sigaction(SIGTOTEST, &parentact, 0) == -1) {
58 		perror("Error calling sigaction\n");
59 		return PTS_UNRESOLVED;
60 	}
61 
62 	if (fork() == 0) {
63 		/* child here */
64 		struct sigaction childact;
65 
66 		childact.sa_handler = childhandler;
67 		childact.sa_flags = 0;
68 		if (sigemptyset(&childact.sa_mask) == -1) {
69 			perror("Error calling sigemptyset\n");
70 			return PTS_UNRESOLVED;
71 		}
72 		if (sigaction(SIGTOTEST, &childact, 0) == -1) {
73 			perror("Error calling sigaction\n");
74 			return PTS_UNRESOLVED;
75 		}
76 		if (raise(SIGTOTEST) != 0) {
77 			printf("Could not raise signal being tested\n");
78 			return PTS_FAIL;
79 		}
80 
81 		printf("Should have exited from signal handler\n");
82 		return PTS_FAIL;
83 	} else {
84 		/* parent here */
85 		int i;
86 
87 		if (wait(&i) == -1) {
88 			perror("Error waiting for child to exit\n");
89 			return PTS_UNRESOLVED;
90 		}
91 		if (!WEXITSTATUS(i)) {
92 			printf("Child exited normally\n");
93 			printf("Test PASSED\n");
94 			return PTS_PASS;
95 		} else {
96 			printf("Child did not exit normally.\n");
97 			printf("Test FAILED\n");
98 			return PTS_FAIL;
99 		}
100 	}
101 
102 	printf("Should not make it here.\n");
103 	return PTS_UNRESOLVED;
104 }
105