blob: 9d94531050a8923cb608f62b41e063ab376f9c04 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/*
21 * NAME
22 * kill04.c
23 *
24 * DESCRIPTION
25 * Test case to check that kill() fails when passed a non-existant pid.
26 *
27 * ALGORITHM
28 * call setup
29 * loop if the -i option was given
30 * fork a child
31 * execute the kill system call with a non-existant PID
32 * check the return value
33 * if return value is not -1
34 * issue a FAIL message, break remaining tests and cleanup
35 * if we are doing functional testing
36 * if the errno was set to 3 (No such proccess)
37 * issue a PASS message
38 * otherwise
39 * issue a FAIL message
40 * call cleanup
41 *
42 * USAGE
43 * kill04 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
44 * where, -c n : Run n copies concurrently.
45 * -f : Turn off functionality Testing.
46 * -i n : Execute test n times.
47 * -I x : Execute test for x seconds.
48 * -P x : Pause for x seconds between iterations.
49 * -t : Turn on syscall timing.
50 *
51 * HISTORY
52 * 07/2001 Ported by Wayne Boyer
53 *
54 * RESTRICTIONS
55 * This test should be run by a non-root user
56 */
57
58#include "test.h"
59#include "usctest.h"
60
61#include <signal.h>
62#include <errno.h>
63#include <sys/wait.h>
64
65void cleanup(void);
66void setup(void);
robbiewd34d5812005-07-11 22:28:09 +000067void do_child(void);
plars865695b2001-08-27 22:15:12 +000068
subrata_modak56207ce2009-03-23 13:35:39 +000069char *TCID = "kill04";
plars865695b2001-08-27 22:15:12 +000070int TST_TOTAL = 1;
71
72extern int Tst_count;
73
subrata_modak56207ce2009-03-23 13:35:39 +000074int exp_enos[] = { ESRCH, 0 };
plars865695b2001-08-27 22:15:12 +000075
76#define TEST_SIG SIGKILL
77
robbiewb8360ee2003-03-26 22:04:58 +000078int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000079{
subrata_modak56207ce2009-03-23 13:35:39 +000080 int lc; /* loop counter */
81 char *msg; /* message returned from parse_opts */
plars865695b2001-08-27 22:15:12 +000082 pid_t pid, fake_pid;
83 int exno, status, fake_status, nsig;
84
85 /* parse standard options */
Garrett Cooper45e285d2010-11-22 12:19:25 -080086 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080087 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000088 }
robbiewd34d5812005-07-11 22:28:09 +000089#ifdef UCLINUX
90 maybe_run_child(&do_child, "");
91#endif
92
subrata_modak56207ce2009-03-23 13:35:39 +000093 setup(); /* global setup */
plars865695b2001-08-27 22:15:12 +000094
95 TEST_EXP_ENOS(exp_enos);
96
97 /* The following loop checks looping state if -i option given */
98 for (lc = 0; TEST_LOOPING(lc); lc++) {
99
100 /* reset Tst_count in case we are looping */
101 Tst_count = 0;
102 status = 1;
103 exno = 1;
robbiewd34d5812005-07-11 22:28:09 +0000104 pid = FORK_OR_VFORK();
plars865695b2001-08-27 22:15:12 +0000105 if (pid < 0) {
106 tst_brkm(TBROK, cleanup, "Fork failed");
107 } else if (pid == 0) {
robbiewd34d5812005-07-11 22:28:09 +0000108#ifdef UCLINUX
109 if (self_exec(av[0], "") < 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000110 tst_brkm(TBROK, cleanup,
111 "self_exec of child failed");
robbiewd34d5812005-07-11 22:28:09 +0000112 }
113#else
114 do_child();
115#endif
plars865695b2001-08-27 22:15:12 +0000116 } else {
robbiewd34d5812005-07-11 22:28:09 +0000117 fake_pid = FORK_OR_VFORK();
plars865695b2001-08-27 22:15:12 +0000118 if (fake_pid < 0) {
119 tst_brkm(TBROK, cleanup, "Second fork failed");
120 } else if (fake_pid == 0) {
robbiewd34d5812005-07-11 22:28:09 +0000121#ifdef UCLINUX
122 if (self_exec(av[0], "") < 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000123 tst_brkm(TBROK, cleanup,
124 "second self_exec "
robbiewd34d5812005-07-11 22:28:09 +0000125 "of child failed");
126 }
127#else
128 do_child();
129#endif
plars865695b2001-08-27 22:15:12 +0000130 }
131 kill(fake_pid, TEST_SIG);
132 waitpid(fake_pid, &fake_status, 0);
133 TEST(kill(fake_pid, TEST_SIG));
134 kill(pid, TEST_SIG);
135 waitpid(pid, &status, 0);
136 }
137
138 if (TEST_RETURN != -1) {
139 tst_brkm(TFAIL, cleanup, "%s failed - errno = %d : %s "
vapierae542852009-08-28 13:22:51 +0000140 "Expected a return value of -1 got %ld",
subrata_modak56207ce2009-03-23 13:35:39 +0000141 TCID, TEST_ERRNO, strerror(TEST_ERRNO),
142 TEST_RETURN);
Garrett Cooper53740502010-12-16 00:04:01 -0800143 }
plars865695b2001-08-27 22:15:12 +0000144
145 if (STD_FUNCTIONAL_TEST) {
146 /*
147 * Check to see if the errno was set to the expected
148 * value of 3 : ESRCH
149 */
150 nsig = WTERMSIG(status);
151 TEST_ERROR_LOG(TEST_ERRNO);
152 if (TEST_ERRNO == ESRCH) {
153 tst_resm(TPASS, "errno set to %d : %s, as "
subrata_modak56207ce2009-03-23 13:35:39 +0000154 "expected", TEST_ERRNO,
155 strerror(TEST_ERRNO));
plars865695b2001-08-27 22:15:12 +0000156 } else {
157 tst_resm(TFAIL, "errno set to %d : %s expected "
subrata_modak56207ce2009-03-23 13:35:39 +0000158 "%d : %s", TEST_ERRNO,
159 strerror(TEST_ERRNO), 3, strerror(3));
plars865695b2001-08-27 22:15:12 +0000160 }
161 }
162 }
163 cleanup();
164
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800165 tst_exit();
plars865695b2001-08-27 22:15:12 +0000166}
167
robbiewd34d5812005-07-11 22:28:09 +0000168/*
169 * do_child()
170 */
subrata_modak56207ce2009-03-23 13:35:39 +0000171void do_child()
robbiewd34d5812005-07-11 22:28:09 +0000172{
173 int exno = 1;
subrata_modakbdbaec52009-02-26 12:14:51 +0000174
robbiewd34d5812005-07-11 22:28:09 +0000175 pause();
Garrett Cooper53740502010-12-16 00:04:01 -0800176 exit(exno);
robbiewd34d5812005-07-11 22:28:09 +0000177}
plars865695b2001-08-27 22:15:12 +0000178
179/*
180 * setup() - performs all ONE TIME setup for this test
181 */
subrata_modak56207ce2009-03-23 13:35:39 +0000182void setup(void)
plars865695b2001-08-27 22:15:12 +0000183{
Garrett Cooper2c282152010-12-16 00:55:50 -0800184
plars865695b2001-08-27 22:15:12 +0000185 TEST_PAUSE;
186}
187
188/*
189 * cleanup() - performs all the ONE TIME cleanup for this test at completion
190 * or premature exit.
191 */
subrata_modak56207ce2009-03-23 13:35:39 +0000192void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000193{
194 /*
195 * print timing status if that option was specified.
196 * print errno log if that option was specified
197 */
198 TEST_CLEANUP;
199
Garrett Cooper2c282152010-12-16 00:55:50 -0800200}