1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci *
3f08c3bdfSopenharmony_ci *   Copyright (c) International Business Machines  Corp., 2001
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci *   This program is free software;  you can redistribute it and/or modify
6f08c3bdfSopenharmony_ci *   it under the terms of the GNU General Public License as published by
7f08c3bdfSopenharmony_ci *   the Free Software Foundation; either version 2 of the License, or
8f08c3bdfSopenharmony_ci *   (at your option) any later version.
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci *   This program is distributed in the hope that it will be useful,
11f08c3bdfSopenharmony_ci *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12f08c3bdfSopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13f08c3bdfSopenharmony_ci *   the GNU General Public License for more details.
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci *   You should have received a copy of the GNU General Public License
16f08c3bdfSopenharmony_ci *   along with this program;  if not, write to the Free Software
17f08c3bdfSopenharmony_ci *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18f08c3bdfSopenharmony_ci */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci/*
21f08c3bdfSopenharmony_ci * NAME
22f08c3bdfSopenharmony_ci *	kill08.c
23f08c3bdfSopenharmony_ci *
24f08c3bdfSopenharmony_ci * DESCRIPTION
25f08c3bdfSopenharmony_ci *	Test case to check the basic functionality of kill() when kill an
26f08c3bdfSopenharmony_ci *	entire process group.
27f08c3bdfSopenharmony_ci *
28f08c3bdfSopenharmony_ci * ALGORITHM
29f08c3bdfSopenharmony_ci *	call setup
30f08c3bdfSopenharmony_ci *	loop if the -i option was given
31f08c3bdfSopenharmony_ci *	fork 5 childeren
32f08c3bdfSopenharmony_ci *	execute the kill system call
33f08c3bdfSopenharmony_ci *	check the return value
34f08c3bdfSopenharmony_ci *	if return value is -1
35f08c3bdfSopenharmony_ci *		issue a FAIL message, break remaining tests and cleanup
36f08c3bdfSopenharmony_ci *	if we are doing functional testing
37f08c3bdfSopenharmony_ci *		if the processes were terminated with the expected signal.
38f08c3bdfSopenharmony_ci *			issue a PASS message
39f08c3bdfSopenharmony_ci *		otherwise
40f08c3bdfSopenharmony_ci *			issue a FAIL message
41f08c3bdfSopenharmony_ci *	call cleanup
42f08c3bdfSopenharmony_ci *
43f08c3bdfSopenharmony_ci * USAGE
44f08c3bdfSopenharmony_ci *  kill08 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
45f08c3bdfSopenharmony_ci *     where,  -c n : Run n copies concurrently.
46f08c3bdfSopenharmony_ci *             -f   : Turn off functionality Testing.
47f08c3bdfSopenharmony_ci *             -i n : Execute test n times.
48f08c3bdfSopenharmony_ci *             -I x : Execute test for x seconds.
49f08c3bdfSopenharmony_ci *             -P x : Pause for x seconds between iterations.
50f08c3bdfSopenharmony_ci *             -t   : Turn on syscall timing.
51f08c3bdfSopenharmony_ci *
52f08c3bdfSopenharmony_ci * HISTORY
53f08c3bdfSopenharmony_ci *	07/2001 Ported by Wayne Boyer
54f08c3bdfSopenharmony_ci *
55f08c3bdfSopenharmony_ci * RESTRICTIONS
56f08c3bdfSopenharmony_ci *	This test should be run as a non-root user.
57f08c3bdfSopenharmony_ci */
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci#include "test.h"
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci#include <signal.h>
62f08c3bdfSopenharmony_ci#include <errno.h>
63f08c3bdfSopenharmony_ci#include <sys/wait.h>
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_civoid cleanup(void);
66f08c3bdfSopenharmony_civoid setup(void);
67f08c3bdfSopenharmony_civoid do_child(void);
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_cichar *TCID = "kill08";
70f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci#define TEST_SIG SIGKILL
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ciint main(int ac, char **av)
75f08c3bdfSopenharmony_ci{
76f08c3bdfSopenharmony_ci	int lc;
77f08c3bdfSopenharmony_ci	pid_t pid1, pid2;
78f08c3bdfSopenharmony_ci	int exno, status, nsig, i;
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
81f08c3bdfSopenharmony_ci#ifdef UCLINUX
82f08c3bdfSopenharmony_ci	maybe_run_child(&do_child, "");
83f08c3bdfSopenharmony_ci#endif
84f08c3bdfSopenharmony_ci
85f08c3bdfSopenharmony_ci	setup();		/* global setup */
86f08c3bdfSopenharmony_ci
87f08c3bdfSopenharmony_ci	/* The following loop checks looping state if -i option given */
88f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_ci		/* reset tst_count in case we are looping */
91f08c3bdfSopenharmony_ci		tst_count = 0;
92f08c3bdfSopenharmony_ci		status = 1;
93f08c3bdfSopenharmony_ci		exno = 1;
94f08c3bdfSopenharmony_ci
95f08c3bdfSopenharmony_ci		/* Fork a process and set the process group so that */
96f08c3bdfSopenharmony_ci		/* it is different from this one.  Fork 5 more children. */
97f08c3bdfSopenharmony_ci
98f08c3bdfSopenharmony_ci		pid1 = FORK_OR_VFORK();
99f08c3bdfSopenharmony_ci		if (pid1 < 0) {
100f08c3bdfSopenharmony_ci			tst_brkm(TBROK, cleanup, "Fork of first child failed");
101f08c3bdfSopenharmony_ci		} else if (pid1 == 0) {
102f08c3bdfSopenharmony_ci			setpgrp();
103f08c3bdfSopenharmony_ci			for (i = 0; i < 5; i++) {
104f08c3bdfSopenharmony_ci				pid2 = FORK_OR_VFORK();
105f08c3bdfSopenharmony_ci				if (pid2 < 0) {
106f08c3bdfSopenharmony_ci					tst_brkm(TBROK, cleanup, "Fork failed");
107f08c3bdfSopenharmony_ci				} else if (pid2 == 0) {
108f08c3bdfSopenharmony_ci#ifdef UCLINUX
109f08c3bdfSopenharmony_ci					if (self_exec(av[0], "") < 0) {
110f08c3bdfSopenharmony_ci						tst_brkm(TBROK, cleanup,
111f08c3bdfSopenharmony_ci							 "self_exec of "
112f08c3bdfSopenharmony_ci							 "child failed");
113f08c3bdfSopenharmony_ci					}
114f08c3bdfSopenharmony_ci#else
115f08c3bdfSopenharmony_ci					do_child();
116f08c3bdfSopenharmony_ci#endif
117f08c3bdfSopenharmony_ci				}
118f08c3bdfSopenharmony_ci			}
119f08c3bdfSopenharmony_ci			/* Kill all processes in this process group */
120f08c3bdfSopenharmony_ci			TEST(kill(0, TEST_SIG));
121f08c3bdfSopenharmony_ci			pause();
122f08c3bdfSopenharmony_ci			exit(exno);
123f08c3bdfSopenharmony_ci		} else {
124f08c3bdfSopenharmony_ci			waitpid(pid1, &status, 0);
125f08c3bdfSopenharmony_ci			if (TEST_RETURN != 0) {
126f08c3bdfSopenharmony_ci				tst_brkm(TFAIL, cleanup, "%s failed - errno = "
127f08c3bdfSopenharmony_ci					 "%d : %s", TCID, TEST_ERRNO,
128f08c3bdfSopenharmony_ci					 strerror(TEST_ERRNO));
129f08c3bdfSopenharmony_ci			}
130f08c3bdfSopenharmony_ci		}
131f08c3bdfSopenharmony_ci
132f08c3bdfSopenharmony_ci		/*
133f08c3bdfSopenharmony_ci		 * Check to see if the process was terminated with the
134f08c3bdfSopenharmony_ci		 * expected signal.
135f08c3bdfSopenharmony_ci		 */
136f08c3bdfSopenharmony_ci		nsig = WTERMSIG(status);
137f08c3bdfSopenharmony_ci		if (nsig == TEST_SIG) {
138f08c3bdfSopenharmony_ci			tst_resm(TPASS, "received expected signal %d",
139f08c3bdfSopenharmony_ci				 nsig);
140f08c3bdfSopenharmony_ci		} else {
141f08c3bdfSopenharmony_ci			tst_resm(TFAIL,
142f08c3bdfSopenharmony_ci				 "expected signal %d received %d",
143f08c3bdfSopenharmony_ci				 TEST_SIG, nsig);
144f08c3bdfSopenharmony_ci		}
145f08c3bdfSopenharmony_ci	}
146f08c3bdfSopenharmony_ci
147f08c3bdfSopenharmony_ci	cleanup();
148f08c3bdfSopenharmony_ci	tst_exit();
149f08c3bdfSopenharmony_ci}
150f08c3bdfSopenharmony_ci
151f08c3bdfSopenharmony_ci/*
152f08c3bdfSopenharmony_ci * do_child()
153f08c3bdfSopenharmony_ci */
154f08c3bdfSopenharmony_civoid do_child(void)
155f08c3bdfSopenharmony_ci{
156f08c3bdfSopenharmony_ci	int exno = 1;
157f08c3bdfSopenharmony_ci
158f08c3bdfSopenharmony_ci	pause();
159f08c3bdfSopenharmony_ci	exit(exno);
160f08c3bdfSopenharmony_ci}
161f08c3bdfSopenharmony_ci
162f08c3bdfSopenharmony_ci/*
163f08c3bdfSopenharmony_ci * setup() - performs all ONE TIME setup for this test
164f08c3bdfSopenharmony_ci */
165f08c3bdfSopenharmony_civoid setup(void)
166f08c3bdfSopenharmony_ci{
167f08c3bdfSopenharmony_ci
168f08c3bdfSopenharmony_ci	TEST_PAUSE;
169f08c3bdfSopenharmony_ci}
170f08c3bdfSopenharmony_ci
171f08c3bdfSopenharmony_ci/*
172f08c3bdfSopenharmony_ci * cleanup() - performs all the ONE TIME cleanup for this test at completion
173f08c3bdfSopenharmony_ci * or premature exit.
174f08c3bdfSopenharmony_ci */
175f08c3bdfSopenharmony_civoid cleanup(void)
176f08c3bdfSopenharmony_ci{
177f08c3bdfSopenharmony_ci
178f08c3bdfSopenharmony_ci}
179