blob: f5bad039b9984ba1985296e8d42bed01c4024603 [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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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"
plars865695b2001-08-27 22:15:12 +000059
60#include <signal.h>
61#include <errno.h>
62#include <sys/wait.h>
63
64void cleanup(void);
65void setup(void);
robbiewd34d5812005-07-11 22:28:09 +000066void do_child(void);
plars865695b2001-08-27 22:15:12 +000067
subrata_modak56207ce2009-03-23 13:35:39 +000068char *TCID = "kill04";
plars865695b2001-08-27 22:15:12 +000069int TST_TOTAL = 1;
70
plars865695b2001-08-27 22:15:12 +000071#define TEST_SIG SIGKILL
72
robbiewb8360ee2003-03-26 22:04:58 +000073int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000074{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020075 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020076 const char *msg;
Stanislav Kholmanskikh23b37f32014-06-30 14:48:24 +040077 pid_t fake_pid;
plars865695b2001-08-27 22:15:12 +000078
Garrett Cooper45e285d2010-11-22 12:19:25 -080079 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080080 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000081 }
robbiewd34d5812005-07-11 22:28:09 +000082
Cyril Hrubis605fa332015-02-04 13:11:20 +010083 setup();
plars865695b2001-08-27 22:15:12 +000084
85 /* The following loop checks looping state if -i option given */
86 for (lc = 0; TEST_LOOPING(lc); lc++) {
87
Caspar Zhangd59a6592013-03-07 14:59:12 +080088 /* reset tst_count in case we are looping */
89 tst_count = 0;
Stanislav Kholmanskikh23b37f32014-06-30 14:48:24 +040090
91 fake_pid = tst_get_unused_pid(cleanup);
92 TEST(kill(fake_pid, TEST_SIG));
plars865695b2001-08-27 22:15:12 +000093
94 if (TEST_RETURN != -1) {
95 tst_brkm(TFAIL, cleanup, "%s failed - errno = %d : %s "
vapierae542852009-08-28 13:22:51 +000096 "Expected a return value of -1 got %ld",
subrata_modak56207ce2009-03-23 13:35:39 +000097 TCID, TEST_ERRNO, strerror(TEST_ERRNO),
98 TEST_RETURN);
Wanlong Gao354ebb42012-12-07 10:10:04 +080099 }
plars865695b2001-08-27 22:15:12 +0000100
Cyril Hrubise38b9612014-06-02 17:20:57 +0200101 /*
102 * Check to see if the errno was set to the expected
103 * value of 3 : ESRCH
104 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200105 if (TEST_ERRNO == ESRCH) {
106 tst_resm(TPASS, "errno set to %d : %s, as "
107 "expected", TEST_ERRNO,
108 strerror(TEST_ERRNO));
109 } else {
110 tst_resm(TFAIL, "errno set to %d : %s expected "
111 "%d : %s", TEST_ERRNO,
112 strerror(TEST_ERRNO), 3, strerror(3));
plars865695b2001-08-27 22:15:12 +0000113 }
114 }
plars865695b2001-08-27 22:15:12 +0000115
Cyril Hrubise38b9612014-06-02 17:20:57 +0200116 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800117 tst_exit();
plars865695b2001-08-27 22:15:12 +0000118}
119
robbiewd34d5812005-07-11 22:28:09 +0000120/*
plars865695b2001-08-27 22:15:12 +0000121 * setup() - performs all ONE TIME setup for this test
122 */
subrata_modak56207ce2009-03-23 13:35:39 +0000123void setup(void)
plars865695b2001-08-27 22:15:12 +0000124{
Garrett Cooper2c282152010-12-16 00:55:50 -0800125
plars865695b2001-08-27 22:15:12 +0000126 TEST_PAUSE;
127}
128
129/*
130 * cleanup() - performs all the ONE TIME cleanup for this test at completion
131 * or premature exit.
132 */
subrata_modak56207ce2009-03-23 13:35:39 +0000133void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000134{
plars865695b2001-08-27 22:15:12 +0000135
Chris Dearmanec6edca2012-10-17 19:54:01 -0700136}