1f08c3bdfSopenharmony_ci/* IBM Corporation */
2f08c3bdfSopenharmony_ci/* 01/02/2003	Port to LTP avenkat@us.ibm.com */
3f08c3bdfSopenharmony_ci/* 06/30/2001	Port to Linux	nsharoff@us.ibm.com */
4f08c3bdfSopenharmony_ci
5f08c3bdfSopenharmony_ci/*
6f08c3bdfSopenharmony_ci *
7f08c3bdfSopenharmony_ci *   Copyright (c) International Business Machines  Corp., 2002
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci *   This program is free software;  you can redistribute it and/or modify
10f08c3bdfSopenharmony_ci *   it under the terms of the GNU General Public License as published by
11f08c3bdfSopenharmony_ci *   the Free Software Foundation; either version 2 of the License, or
12f08c3bdfSopenharmony_ci *   (at your option) any later version.
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci *   This program is distributed in the hope that it will be useful,
15f08c3bdfSopenharmony_ci *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
16f08c3bdfSopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
17f08c3bdfSopenharmony_ci *   the GNU General Public License for more details.
18f08c3bdfSopenharmony_ci *
19f08c3bdfSopenharmony_ci *   You should have received a copy of the GNU General Public License
20f08c3bdfSopenharmony_ci *   along with this program;  if not, write to the Free Software
21f08c3bdfSopenharmony_ci *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22f08c3bdfSopenharmony_ci */
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci			   /*kill2.c */
25f08c3bdfSopenharmony_ci/*======================================================================
26f08c3bdfSopenharmony_ci>KEYS:  < kill(), wait(), signal()
27f08c3bdfSopenharmony_ci>WHAT:  < Check that when a child is killed by its parent, it returns the
28f08c3bdfSopenharmony_ci	< correct values to the waiting parent--the child sets signal to
29f08c3bdfSopenharmony_ci	< ignore the kill
30f08c3bdfSopenharmony_ci>HOW:   < For each signal: Send that signal to a child that has elected
31f08c3bdfSopenharmony_ci	< to catch the signal, check that the correct status was returned
32f08c3bdfSopenharmony_ci	< to the waiting parent.
33f08c3bdfSopenharmony_ci	< NOTE: Signal 9 (kill) is not catchable, and must be dealt with
34f08c3bdfSopenharmony_ci	< separately.
35f08c3bdfSopenharmony_ci>BUGS:  < None known
36f08c3bdfSopenharmony_ci======================================================================*/
37f08c3bdfSopenharmony_ci#ifndef _GNU_SOURCE
38f08c3bdfSopenharmony_ci#define _GNU_SOURCE 1
39f08c3bdfSopenharmony_ci#endif
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci#include <stdio.h>
42f08c3bdfSopenharmony_ci#include <sys/types.h>
43f08c3bdfSopenharmony_ci#include <signal.h>
44f08c3bdfSopenharmony_ci#include <stdlib.h>
45f08c3bdfSopenharmony_ci#include <unistd.h>
46f08c3bdfSopenharmony_ci#include <sys/wait.h>
47f08c3bdfSopenharmony_ci#include <errno.h>
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci#include "test.h"
50f08c3bdfSopenharmony_ci#define ITER    3
51f08c3bdfSopenharmony_ci#define FAILED 0
52f08c3bdfSopenharmony_ci#define PASSED 1
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_cichar *TCID = "kill12";
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ciint local_flag = PASSED;
57f08c3bdfSopenharmony_ciint block_number;
58f08c3bdfSopenharmony_ciFILE *temp;
59f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
60f08c3bdfSopenharmony_cistatic int sig;
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ciint anyfail();
63f08c3bdfSopenharmony_ciint blenter();
64f08c3bdfSopenharmony_ciint instress();
65f08c3bdfSopenharmony_civoid setup();
66f08c3bdfSopenharmony_civoid terror();
67f08c3bdfSopenharmony_civoid fail_exit();
68f08c3bdfSopenharmony_civoid ok_exit();
69f08c3bdfSopenharmony_ciint forkfail();
70f08c3bdfSopenharmony_civoid do_child();
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ciint chflag;
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ciint main(int argc, char **argv)
75f08c3bdfSopenharmony_ci{
76f08c3bdfSopenharmony_ci	int pid, npid;
77f08c3bdfSopenharmony_ci	int nsig, exno, nexno, status;
78f08c3bdfSopenharmony_ci	int ret_val = 0;
79f08c3bdfSopenharmony_ci	int core;
80f08c3bdfSopenharmony_ci	void chsig();
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_ci	tst_parse_opts(argc, argv, NULL, NULL);
83f08c3bdfSopenharmony_ci
84f08c3bdfSopenharmony_ci	setup();
85f08c3bdfSopenharmony_ci	blenter();
86f08c3bdfSopenharmony_ci
87f08c3bdfSopenharmony_ci	exno = 1;
88f08c3bdfSopenharmony_ci
89f08c3bdfSopenharmony_ci	if (sigset(SIGCHLD, chsig) == SIG_ERR) {
90f08c3bdfSopenharmony_ci		fprintf(temp, "\tsigset failed, errno = %d\n", errno);
91f08c3bdfSopenharmony_ci		fail_exit();
92f08c3bdfSopenharmony_ci	}
93f08c3bdfSopenharmony_ci
94f08c3bdfSopenharmony_ci	for (sig = 1; sig < 14; sig++) {
95f08c3bdfSopenharmony_ci		fflush(temp);
96f08c3bdfSopenharmony_ci		chflag = 0;
97f08c3bdfSopenharmony_ci
98f08c3bdfSopenharmony_ci		pid = FORK_OR_VFORK();
99f08c3bdfSopenharmony_ci		if (pid < 0) {
100f08c3bdfSopenharmony_ci			forkfail();
101f08c3bdfSopenharmony_ci		}
102f08c3bdfSopenharmony_ci
103f08c3bdfSopenharmony_ci		if (pid == 0) {
104f08c3bdfSopenharmony_ci			do_child();
105f08c3bdfSopenharmony_ci		} else {
106f08c3bdfSopenharmony_ci			//fprintf(temp, "Testing signal %d\n", sig);
107f08c3bdfSopenharmony_ci
108f08c3bdfSopenharmony_ci			while (!chflag)	/* wait for child */
109f08c3bdfSopenharmony_ci				sleep(1);
110f08c3bdfSopenharmony_ci
111f08c3bdfSopenharmony_ci			kill(pid, sig);	/* child should ignroe this sig */
112f08c3bdfSopenharmony_ci			kill(pid, SIGCHLD);	/* child should exit */
113f08c3bdfSopenharmony_ci
114f08c3bdfSopenharmony_ci#ifdef BCS
115f08c3bdfSopenharmony_ci			while ((npid = wait(&status)) != pid
116f08c3bdfSopenharmony_ci			       || (npid == -1 && errno == EINTR)) ;
117f08c3bdfSopenharmony_ci			if (npid != pid) {
118f08c3bdfSopenharmony_ci				fprintf(temp,
119f08c3bdfSopenharmony_ci					"wait error: wait returned wrong pid\n");
120f08c3bdfSopenharmony_ci				ret_val = 1;
121f08c3bdfSopenharmony_ci			}
122f08c3bdfSopenharmony_ci#else
123f08c3bdfSopenharmony_ci			while ((npid = waitpid(pid, &status, 0)) != -1
124f08c3bdfSopenharmony_ci			       || errno == EINTR) ;
125f08c3bdfSopenharmony_ci#endif
126f08c3bdfSopenharmony_ci
127f08c3bdfSopenharmony_ci			/*
128f08c3bdfSopenharmony_ci			   nsig = status & 0177;
129f08c3bdfSopenharmony_ci			   core = status & 0200;
130f08c3bdfSopenharmony_ci			   nexno = (status & 0xff00) >> 8;
131f08c3bdfSopenharmony_ci			 */
132f08c3bdfSopenharmony_ci			/*****  LTP Port        *****/
133f08c3bdfSopenharmony_ci			nsig = WTERMSIG(status);
134f08c3bdfSopenharmony_ci#ifdef WCOREDUMP
135f08c3bdfSopenharmony_ci			core = WCOREDUMP(status);
136f08c3bdfSopenharmony_ci#endif
137f08c3bdfSopenharmony_ci			nexno = WIFEXITED(status);
138f08c3bdfSopenharmony_ci			/*****  **      **      *****/
139f08c3bdfSopenharmony_ci
140f08c3bdfSopenharmony_ci			/* nsig is the signal number returned by wait
141f08c3bdfSopenharmony_ci			   it should be 0, except when sig = 9          */
142f08c3bdfSopenharmony_ci
143f08c3bdfSopenharmony_ci			if ((sig == 9) && (nsig != sig)) {
144f08c3bdfSopenharmony_ci				fprintf(temp, "wait error: unexpected signal"
145f08c3bdfSopenharmony_ci					" returned when the signal sent was 9"
146f08c3bdfSopenharmony_ci					" The status of the process is %d \n",
147f08c3bdfSopenharmony_ci					status);
148f08c3bdfSopenharmony_ci				ret_val = 1;
149f08c3bdfSopenharmony_ci			}
150f08c3bdfSopenharmony_ci			if ((sig != 9) && (nsig != 0)) {
151f08c3bdfSopenharmony_ci				fprintf(temp, "wait error: unexpected signal "
152f08c3bdfSopenharmony_ci					"returned, the status of the process is "
153f08c3bdfSopenharmony_ci					"%d  \n", status);
154f08c3bdfSopenharmony_ci				ret_val = 1;
155f08c3bdfSopenharmony_ci			}
156f08c3bdfSopenharmony_ci
157f08c3bdfSopenharmony_ci			/* nexno is the exit number returned by wait
158f08c3bdfSopenharmony_ci			   it should be 1, except when sig = 9          */
159f08c3bdfSopenharmony_ci
160f08c3bdfSopenharmony_ci			if (sig == 9)
161f08c3bdfSopenharmony_ci				if (nexno != 0) {
162f08c3bdfSopenharmony_ci					fprintf(temp, "signal error: unexpected"
163f08c3bdfSopenharmony_ci						" exit number returned when"
164f08c3bdfSopenharmony_ci						" signal sent was 9, the status"
165f08c3bdfSopenharmony_ci						" of the process is %d \n",
166f08c3bdfSopenharmony_ci						status);
167f08c3bdfSopenharmony_ci					ret_val = 1;
168f08c3bdfSopenharmony_ci				} else;
169f08c3bdfSopenharmony_ci			else if (nexno != 1) {
170f08c3bdfSopenharmony_ci				fprintf(temp, "signal error: unexpected exit "
171f08c3bdfSopenharmony_ci					"number returned,the status of the"
172f08c3bdfSopenharmony_ci					" process is %d\n", status);
173f08c3bdfSopenharmony_ci				ret_val = 1;
174f08c3bdfSopenharmony_ci			}
175f08c3bdfSopenharmony_ci		}
176f08c3bdfSopenharmony_ci	}
177f08c3bdfSopenharmony_ci	if (ret_val)
178f08c3bdfSopenharmony_ci		local_flag = FAILED;
179f08c3bdfSopenharmony_ci
180f08c3bdfSopenharmony_ci	anyfail();
181f08c3bdfSopenharmony_ci	tst_exit();
182f08c3bdfSopenharmony_ci}
183f08c3bdfSopenharmony_ci
184f08c3bdfSopenharmony_civoid chsig(void)
185f08c3bdfSopenharmony_ci{
186f08c3bdfSopenharmony_ci	chflag++;
187f08c3bdfSopenharmony_ci}
188f08c3bdfSopenharmony_ci
189f08c3bdfSopenharmony_ciint anyfail(void)
190f08c3bdfSopenharmony_ci{
191f08c3bdfSopenharmony_ci	(local_flag == FAILED) ? tst_resm(TFAIL,
192f08c3bdfSopenharmony_ci					  "Test failed") : tst_resm(TPASS,
193f08c3bdfSopenharmony_ci								    "Test passed");
194f08c3bdfSopenharmony_ci	tst_exit();
195f08c3bdfSopenharmony_ci}
196f08c3bdfSopenharmony_ci
197f08c3bdfSopenharmony_civoid do_child(void)
198f08c3bdfSopenharmony_ci{
199f08c3bdfSopenharmony_ci	int exno = 1;
200f08c3bdfSopenharmony_ci
201f08c3bdfSopenharmony_ci	sigset(sig, SIG_IGN);	/* set to ignore signal */
202f08c3bdfSopenharmony_ci	kill(getppid(), SIGCHLD);	/* tell parent we are ready */
203f08c3bdfSopenharmony_ci	while (!chflag)
204f08c3bdfSopenharmony_ci		sleep(1);	/* wait for parent */
205f08c3bdfSopenharmony_ci
206f08c3bdfSopenharmony_ci	exit(exno);
207f08c3bdfSopenharmony_ci}
208f08c3bdfSopenharmony_ci
209f08c3bdfSopenharmony_civoid setup(void)
210f08c3bdfSopenharmony_ci{
211f08c3bdfSopenharmony_ci	temp = stderr;
212f08c3bdfSopenharmony_ci}
213f08c3bdfSopenharmony_ci
214f08c3bdfSopenharmony_ciint blenter(void)
215f08c3bdfSopenharmony_ci{
216f08c3bdfSopenharmony_ci	//tst_resm(TINFO, "Enter block %d", block_number);
217f08c3bdfSopenharmony_ci	local_flag = PASSED;
218f08c3bdfSopenharmony_ci	return 0;
219f08c3bdfSopenharmony_ci}
220f08c3bdfSopenharmony_ci
221f08c3bdfSopenharmony_civoid terror(char *message)
222f08c3bdfSopenharmony_ci{
223f08c3bdfSopenharmony_ci	tst_resm(TBROK, "Reason: %s:%s", message, strerror(errno));
224f08c3bdfSopenharmony_ci}
225f08c3bdfSopenharmony_ci
226f08c3bdfSopenharmony_civoid fail_exit(void)
227f08c3bdfSopenharmony_ci{
228f08c3bdfSopenharmony_ci	local_flag = FAILED;
229f08c3bdfSopenharmony_ci	anyfail();
230f08c3bdfSopenharmony_ci
231f08c3bdfSopenharmony_ci}
232f08c3bdfSopenharmony_ci
233f08c3bdfSopenharmony_ciint forkfail(void)
234f08c3bdfSopenharmony_ci{
235f08c3bdfSopenharmony_ci	tst_brkm(TBROK, NULL, "FORK FAILED - terminating test.");
236f08c3bdfSopenharmony_ci}
237