plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 65 | void cleanup(void); |
| 66 | void setup(void); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 67 | void do_child(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 69 | char *TCID = "kill04"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | int TST_TOTAL = 1; |
| 71 | |
| 72 | extern int Tst_count; |
| 73 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 74 | int exp_enos[] = { ESRCH, 0 }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
| 76 | #define TEST_SIG SIGKILL |
| 77 | |
robbiew | b8360ee | 2003-03-26 22:04:58 +0000 | [diff] [blame] | 78 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 80 | int lc; /* loop counter */ |
| 81 | char *msg; /* message returned from parse_opts */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | pid_t pid, fake_pid; |
| 83 | int exno, status, fake_status, nsig; |
| 84 | |
| 85 | /* parse standard options */ |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 86 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 87 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 89 | #ifdef UCLINUX |
| 90 | maybe_run_child(&do_child, ""); |
| 91 | #endif |
| 92 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 93 | setup(); /* global setup */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | |
| 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; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 104 | pid = FORK_OR_VFORK(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | if (pid < 0) { |
| 106 | tst_brkm(TBROK, cleanup, "Fork failed"); |
| 107 | } else if (pid == 0) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 108 | #ifdef UCLINUX |
| 109 | if (self_exec(av[0], "") < 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 110 | tst_brkm(TBROK, cleanup, |
| 111 | "self_exec of child failed"); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 112 | } |
| 113 | #else |
| 114 | do_child(); |
| 115 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | } else { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 117 | fake_pid = FORK_OR_VFORK(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | if (fake_pid < 0) { |
| 119 | tst_brkm(TBROK, cleanup, "Second fork failed"); |
| 120 | } else if (fake_pid == 0) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 121 | #ifdef UCLINUX |
| 122 | if (self_exec(av[0], "") < 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 123 | tst_brkm(TBROK, cleanup, |
| 124 | "second self_exec " |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 125 | "of child failed"); |
| 126 | } |
| 127 | #else |
| 128 | do_child(); |
| 129 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | } |
| 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 " |
vapier | ae54285 | 2009-08-28 13:22:51 +0000 | [diff] [blame] | 140 | "Expected a return value of -1 got %ld", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 141 | TCID, TEST_ERRNO, strerror(TEST_ERRNO), |
| 142 | TEST_RETURN); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 143 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 154 | "expected", TEST_ERRNO, |
| 155 | strerror(TEST_ERRNO)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | } else { |
| 157 | tst_resm(TFAIL, "errno set to %d : %s expected " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 158 | "%d : %s", TEST_ERRNO, |
| 159 | strerror(TEST_ERRNO), 3, strerror(3)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | } |
| 163 | cleanup(); |
| 164 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame^] | 165 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | } |
| 167 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 168 | /* |
| 169 | * do_child() |
| 170 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 171 | void do_child() |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 172 | { |
| 173 | int exno = 1; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 174 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 175 | pause(); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 176 | exit(exno); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 177 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 178 | |
| 179 | /* |
| 180 | * setup() - performs all ONE TIME setup for this test |
| 181 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 182 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 183 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 184 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 185 | TEST_PAUSE; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * cleanup() - performs all the ONE TIME cleanup for this test at completion |
| 190 | * or premature exit. |
| 191 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 192 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 193 | { |
| 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 Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 200 | } |