1 /*
2 * Copyright (c) 2002-2003, Intel Corporation. All rights reserved.
3 * Created by: salwan.searty 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 * Steps:
9 * 1) Fork a child process.
10 * 2) In the parent process, call killpg with signal SIGTOTEST for the
11 * process group id of the child. Have the parent ignore such a signal
12 * incase the process group id of the parent is the same as process
13 * group id of the child.
14 * In the child,
15 * 3) Wait for signal SIGTOTEST.
16 * 4) Return 1 if SIGTOTEST is found. Return 0 otherwise.
17 * 5) In the parent, return success if 1 was returned from child.
18 *
19 */
20
21 #define SIGTOTEST SIGUSR1
22
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <sys/wait.h>
28 #include "posixtest.h"
29
myhandler(int signo)30 static void myhandler(int signo)
31 {
32 (void) signo;
33 _exit(1);
34 }
35
main(void)36 int main(void)
37 {
38 int child_pid, child_pgid;
39
40 if ((child_pid = fork()) == 0) {
41 /* child here */
42 struct sigaction act;
43 act.sa_handler = myhandler;
44 act.sa_flags = 0;
45 sigemptyset(&act.sa_mask);
46 sigaction(SIGTOTEST, &act, 0);
47
48 /* change child's process group id */
49
50 /*
51 * XXX: POSIX 1003.1-2001 added setpgrp(2) to BASE, but
52 * unfortunately BSD has had their own implementations for
53 * ages for compatibility reasons.
54 */
55 #if __FreeBSD__ || __NetBSD__ || __OpenBSD__
56 setpgrp(0, 0);
57 #else
58 setpgrp();
59 #endif
60
61 sigpause(SIGABRT);
62
63 return 0;
64 } else {
65 /* parent here */
66 int i;
67 sigignore(SIGTOTEST);
68
69 sleep(1);
70 if ((child_pgid = getpgid(child_pid)) == -1) {
71 printf("Could not get pgid of child\n");
72 return PTS_UNRESOLVED;
73 }
74
75 if (killpg(child_pgid, SIGTOTEST) != 0) {
76 printf("Could not raise signal being tested\n");
77 return PTS_UNRESOLVED;
78 }
79
80 if (wait(&i) == -1) {
81 perror("Error waiting for child to exit\n");
82 return PTS_UNRESOLVED;
83 }
84
85 if (WEXITSTATUS(i)) {
86 printf("Child exited normally\n");
87 printf("Test PASSED\n");
88 return PTS_PASS;
89 } else {
90 printf("Child did not exit normally.\n");
91 printf("Test FAILED\n");
92 return PTS_FAIL;
93 }
94 }
95
96 printf("Should have exited from parent\n");
97 printf("Test FAILED\n");
98 return PTS_FAIL;
99 }
100