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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
| 22 | * waitpid05.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Check that when a child kills itself with a kill statement after |
| 26 | * determining its process id by using getpid, the parent receives a |
| 27 | * correct report of the cause of its death. This also indirectly |
| 28 | * checks that getpid returns the correct process id. |
| 29 | * |
| 30 | * ALGORITHM |
| 31 | * For signals 1 - 15: fork a child that determines it's own process |
| 32 | * id, then sends the signal to itself. The parent waits to see if the |
| 33 | * demise of the child results in the signal number being returned to |
| 34 | * the parent. |
| 35 | * |
| 36 | * USAGE: <for command-line> |
| 37 | * waitpid05 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 38 | * where, -c n : Run n copies concurrently. |
| 39 | * -e : Turn on errno logging. |
| 40 | * -i n : Execute test n times. |
| 41 | * -I x : Execute test for x seconds. |
| 42 | * -P x : Pause for x seconds between iterations. |
| 43 | * -t : Turn on syscall timing. |
| 44 | * |
| 45 | * History |
| 46 | * 07/2001 John George |
| 47 | * -Ported |
robbiew | 4644c7e | 2002-04-26 14:33:32 +0000 | [diff] [blame] | 48 | * 04/2002 wjhuie sigset cleanups |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 49 | * |
| 50 | * Restrictions |
| 51 | * None |
| 52 | */ |
| 53 | |
| 54 | #include <sys/file.h> |
| 55 | #include <sys/signal.h> |
| 56 | #include <sys/types.h> |
| 57 | #include <sys/wait.h> |
plars | 4814b68 | 2001-10-25 15:32:43 +0000 | [diff] [blame] | 58 | #include <sys/time.h> |
| 59 | #include <sys/resource.h> |
| 60 | #include <unistd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | #include <errno.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 62 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | |
Wanlong Gao | 2c58f60 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 64 | static void do_child(int); |
| 65 | static void setup(void); |
| 66 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | |
| 68 | char *TCID = "waitpid05"; |
| 69 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 71 | #ifdef UCLINUX |
Wanlong Gao | 2c58f60 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 72 | static void do_child_uclinux(void); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 73 | static int sig_uclinux; |
| 74 | #endif |
| 75 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 76 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | { |
| 78 | int pid, npid, sig, nsig; |
| 79 | int exno, nexno, status; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 80 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 81 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | |
Wanlong Gao | 2c58f60 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 83 | msg = parse_opts(ac, av, NULL, NULL); |
| 84 | if (msg != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 86 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 87 | #ifdef UCLINUX |
| 88 | maybe_run_child(&do_child_uclinux, "d", &sig_uclinux); |
| 89 | #endif |
| 90 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | setup(); |
| 92 | |
| 93 | /* check for looping state if -i option is given */ |
| 94 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 95 | /* reset tst_count in case we are looping */ |
| 96 | tst_count = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 97 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | /* |
| 99 | * Set SIGTERM to SIG_DFL as test driver sets up to ignore |
| 100 | * SIGTERM |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | */ |
robbiew | 4644c7e | 2002-04-26 14:33:32 +0000 | [diff] [blame] | 102 | if (signal(SIGTERM, SIG_DFL) == SIG_ERR) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | tst_resm(TFAIL, "Sigset SIGTERM failed, errno = %d", |
| 104 | errno); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 105 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | exno = 1; |
| 109 | for (sig = 1; sig <= 15; sig++) { |
Wanlong Gao | 2c58f60 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 110 | if (sig == SIGUSR1 || sig == SIGUSR2 || sig == SIGBUS) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | continue; |
Wanlong Gao | 2c58f60 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 112 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 113 | /*Initialize signal to its default action */ |
| 114 | signal(sig, SIG_DFL); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 115 | pid = FORK_OR_VFORK(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | |
| 117 | if (pid == 0) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 118 | #ifdef UCLINUX |
| 119 | self_exec(av[0], "d", sig); |
| 120 | /* No fork() error check is done so don't */ |
| 121 | /* do an error check here */ |
| 122 | #else |
| 123 | do_child(sig); |
| 124 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | } else { |
| 126 | errno = 0; |
| 127 | while (((npid = waitpid(pid, &status, 0)) != |
| 128 | -1) || (errno == EINTR)) { |
Wanlong Gao | 2c58f60 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 129 | if (errno == EINTR) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | continue; |
Wanlong Gao | 2c58f60 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 131 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | if (npid != pid) { |
| 133 | tst_resm(TFAIL, "waitpid " |
| 134 | "error: unexpected " |
| 135 | "pid returned"); |
| 136 | } else { |
| 137 | tst_resm(TPASS, "received " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 138 | "expected pid."); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | nsig = status % 256; |
| 142 | |
| 143 | /* |
| 144 | * to check if the core dump bit has |
| 145 | * been set, bit #7 |
| 146 | */ |
| 147 | if (nsig >= 128) { |
| 148 | nsig -= 128; |
| 149 | if ((sig == 1) || (sig == 2) || |
Wanlong Gao | 2c58f60 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 150 | (sig == 9) || (sig == 13) || |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | (sig == 14) || |
| 152 | (sig == 15)) { |
| 153 | tst_resm(TFAIL, |
| 154 | "signal " |
| 155 | "error : " |
| 156 | "core dump " |
| 157 | "bit set for" |
| 158 | " exception " |
| 159 | "number %d", |
| 160 | sig); |
| 161 | } |
| 162 | } else if ((sig == 3) || (sig == 4) || |
| 163 | (sig == 5) || (sig == 6) || |
| 164 | (sig == 8) || (sig == 11)) { |
| 165 | tst_resm(TFAIL, |
| 166 | "signal error: " |
| 167 | "core dump bit not " |
| 168 | "set for exception " |
| 169 | "number %d", sig); |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * nsig is the signal number returned |
| 174 | * by waitpid |
| 175 | */ |
| 176 | if (nsig != sig) { |
| 177 | tst_resm(TFAIL, "waitpid " |
| 178 | "error: unexpected " |
| 179 | "signal returned"); |
| 180 | tst_resm(TINFO, "got signal " |
| 181 | "%d, expected " |
| 182 | "%d", nsig, sig); |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * nexno is the exit number returned |
| 187 | * by waitpid |
| 188 | */ |
| 189 | nexno = status / 256; |
| 190 | if (nexno != 0) { |
| 191 | tst_resm(TFAIL, "signal " |
| 192 | "error: unexpected " |
| 193 | "exit number " |
| 194 | "returned"); |
| 195 | } else { |
| 196 | tst_resm(TPASS, "received " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 197 | "expected exit number."); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
Wanlong Gao | 2c58f60 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 203 | if (access("core", F_OK) == 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | unlink("core"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 205 | } |
Wanlong Gao | 2c58f60 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 206 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 207 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 208 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Wanlong Gao | 2c58f60 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 211 | static void do_child(int sig) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 212 | { |
| 213 | int exno = 1; |
| 214 | int pid = getpid(); |
| 215 | |
| 216 | if (kill(pid, sig) == -1) { |
| 217 | tst_resm(TFAIL, "kill error: kill unsuccessful"); |
| 218 | exit(exno); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | #ifdef UCLINUX |
| 223 | /* |
| 224 | * do_child_uclinux() |
| 225 | * run do_child with the appropriate sig variable |
| 226 | */ |
Wanlong Gao | 2c58f60 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 227 | static void do_child_uclinux(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 228 | { |
| 229 | do_child(sig_uclinux); |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 230 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 231 | #endif |
| 232 | |
Wanlong Gao | 2c58f60 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 233 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 234 | { |
plars | 4814b68 | 2001-10-25 15:32:43 +0000 | [diff] [blame] | 235 | struct rlimit newlimit; |
| 236 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 237 | TEST_PAUSE; |
| 238 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 239 | tst_tmpdir(); |
plars | 4814b68 | 2001-10-25 15:32:43 +0000 | [diff] [blame] | 240 | |
vapier | d17884d | 2006-05-26 05:47:00 +0000 | [diff] [blame] | 241 | newlimit.rlim_max = newlimit.rlim_cur = RLIM_INFINITY; |
| 242 | if (setrlimit(RLIMIT_CORE, &newlimit) != 0) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 243 | tst_resm(TWARN, |
| 244 | "setrlimit(RLIMIT_CORE,RLIM_INFINITY) failed; this may cause some false core-dump test failures"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Wanlong Gao | 2c58f60 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 247 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 248 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 249 | tst_rmdir(); |
Wanlong Gao | 2c58f60 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 250 | } |