1/*
2 * Copyright (c) 2002-2003, 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 kill() function shall send signal sig to the process
9 *  specified by pid when the process specified by pid is not the calling
10 *  process.
11 *  1) Fork a child process.
12 *  2) In the parent process, call kill with signal SIGTOTEST for the
13 *     pid 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 *  This test is only performed on one signal.  All other signals are
20 *  considered to be in the same equivalence class.
21 *
22 *  This test makes the assumption that 1 second of sleeping on the part
23 *  of the parent is enough to give the child time to start waiting for
24 *  the parent's signal.  If that is not the case, this test will fail.
25 */
26
27#include <signal.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <unistd.h>
31#include <sys/wait.h>
32#include "posixtest.h"
33
34#define SIGTOTEST SIGUSR1
35
36static void myhandler(int signo)
37{
38	(void) signo;
39	_exit(1);
40}
41
42int main(void)
43{
44	int pid;
45
46	int sig;
47	sigset_t set;
48
49	if (sigemptyset(&set) == -1) {
50		perror("Error calling sigemptyset\n");
51		return PTS_UNRESOLVED;
52	}
53
54	if (sigaddset(&set, SIGTOTEST) == -1) {
55		perror("Error calling sigaddset\n");
56		return PTS_UNRESOLVED;
57	}
58
59	pid = fork();
60	if (pid == 0) {
61		/* child here */
62		struct sigaction act;
63		act.sa_handler = myhandler;
64		act.sa_flags = 0;
65		sigemptyset(&act.sa_mask);
66		sigaction(SIGTOTEST, &act, 0);
67
68		if (0 != sigwait(&set, &sig)) {
69			printf("Sigwait did not return 0."
70				"Possible problem with sigwait function\n");
71			/* FAIL */
72			return 0;
73		}
74
75		if (sig != SIGTOTEST)
76			/* FAIL */
77			return 0;
78
79		return 1;
80	} else if (pid > 0) {
81		/* parent here */
82		int i;
83
84		sleep(1);
85		if (kill(pid, SIGTOTEST) != 0) {
86			printf("Could not raise signal being tested\n");
87			return PTS_UNRESOLVED;
88		}
89
90		if (wait(&i) == -1) {
91			perror("Error waiting for child to exit\n");
92			return PTS_UNRESOLVED;
93		}
94
95		if (WEXITSTATUS(i)) {
96			printf("Child exited normally\n");
97			printf("Test PASSED\n");
98			return PTS_PASS;
99		} else {
100			printf("Child did not exit normally.\n");
101			printf("Test FAILED\n");
102			return PTS_FAIL;
103		}
104
105	} else {
106		printf("Error fork() a child\n");
107		return PTS_UNRESOLVED;
108	}
109
110	printf("Should have exited from parent\n");
111	printf("Test FAILED\n");
112	return PTS_FAIL;
113}
114