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 | * waitpid02.c |
| 23 | * |
| 24 | * DESCRIPTION |
plars | a775b98 | 2002-10-31 18:55:02 +0000 | [diff] [blame] | 25 | * Check that when a child gets killed by an integer zero |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 26 | * divide exception, the waiting parent is correctly notified. |
| 27 | * |
| 28 | * ALGORITHM |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 29 | * Fork a child and send a SIGFPE to it. The parent waits for the |
plars | a775b98 | 2002-10-31 18:55:02 +0000 | [diff] [blame] | 30 | * death of the child and checks that SIGFPE was returned. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 31 | * |
| 32 | * USAGE: <for command-line> |
| 33 | * waitpid02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 34 | * where, -c n : Run n copies concurrently. |
| 35 | * -e : Turn on errno logging. |
| 36 | * -i n : Execute test n times. |
| 37 | * -I x : Execute test for x seconds. |
| 38 | * -P x : Pause for x seconds between iterations. |
| 39 | * -t : Turn on syscall timing. |
| 40 | * |
| 41 | * History |
| 42 | * 07/2001 John George |
| 43 | * -Ported |
plars | a775b98 | 2002-10-31 18:55:02 +0000 | [diff] [blame] | 44 | * 10/2002 Paul Larson |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 45 | * Div by zero doesn't cause SIGFPE on some archs, fixed |
plars | a775b98 | 2002-10-31 18:55:02 +0000 | [diff] [blame] | 46 | * to send the signal with kill |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 47 | * |
| 48 | * Restrictions |
| 49 | * None |
| 50 | */ |
| 51 | |
| 52 | #include <sys/file.h> |
Jan Stancek | d0bebd6 | 2012-11-14 10:36:33 +0100 | [diff] [blame] | 53 | #include <sys/resource.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | #include <sys/signal.h> |
| 55 | #include <sys/types.h> |
| 56 | #include <sys/wait.h> |
| 57 | #include <errno.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 58 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | |
Wanlong Gao | 9554de5 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 60 | static void do_child(void); |
| 61 | static void setup(void); |
| 62 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | |
| 64 | char *TCID = "waitpid02"; |
| 65 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 67 | int main(int argc, char **argv) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 69 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 70 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | |
| 72 | int pid, npid, sig, nsig; |
| 73 | int exno, nexno, status; |
| 74 | |
Wanlong Gao | 9554de5 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 75 | msg = parse_opts(argc, argv, NULL, NULL); |
| 76 | if (msg != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 78 | #ifdef UCLINUX |
| 79 | maybe_run_child(&do_child, ""); |
| 80 | #endif |
| 81 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | setup(); |
| 83 | |
| 84 | /* check for looping state if -i option is given */ |
| 85 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 86 | /* reset tst_count in case we are looping */ |
| 87 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | |
| 89 | exno = 1; |
plars | a775b98 | 2002-10-31 18:55:02 +0000 | [diff] [blame] | 90 | sig = SIGFPE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 92 | pid = FORK_OR_VFORK(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | |
| 94 | if (pid == 0) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 95 | #ifdef UCLINUX |
| 96 | self_exec(argv[0], ""); |
| 97 | /* No fork() error check is done so don't check here */ |
| 98 | #else |
| 99 | do_child(); |
| 100 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | } else { |
plars | a775b98 | 2002-10-31 18:55:02 +0000 | [diff] [blame] | 102 | kill(pid, sig); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | errno = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 104 | while (((npid = waitpid(pid, &status, 0)) != -1) || |
| 105 | (errno == EINTR)) { |
Wanlong Gao | 9554de5 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 106 | if (errno == EINTR) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | continue; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | |
| 109 | if (npid != pid) { |
| 110 | tst_resm(TFAIL, "waitpid error: " |
| 111 | "unexpected pid returned"); |
| 112 | } else { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 113 | tst_resm(TPASS, |
| 114 | "recieved expected pid"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | nsig = WTERMSIG(status); |
| 118 | |
| 119 | /* |
| 120 | * nsig is the signal number returned by |
| 121 | * waitpid |
| 122 | */ |
| 123 | if (nsig != sig) { |
| 124 | tst_resm(TFAIL, "waitpid error: " |
| 125 | "unexpected signal returned"); |
| 126 | } else { |
| 127 | tst_resm(TPASS, "recieved expected " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 128 | "signal"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* |
| 132 | * nexno is the exit number returned by |
| 133 | * waitpid |
| 134 | */ |
| 135 | nexno = WEXITSTATUS(status); |
| 136 | if (nexno != 0) { |
| 137 | tst_resm(TFAIL, "signal error: " |
| 138 | "unexpected exit number " |
| 139 | "returned"); |
| 140 | } else { |
| 141 | tst_resm(TPASS, "recieved expected " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 142 | "exit value"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | } |
Wanlong Gao | 9554de5 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 147 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 149 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Wanlong Gao | 9554de5 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 152 | static void do_child(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 153 | { |
| 154 | int exno = 1; |
| 155 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 156 | while (1) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 157 | usleep(10); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 158 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 159 | exit(exno); |
| 160 | } |
| 161 | |
Wanlong Gao | 9554de5 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 162 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | { |
Jan Stancek | d0bebd6 | 2012-11-14 10:36:33 +0100 | [diff] [blame] | 164 | /* SIGFPE is expected signal, so avoid creating any corefile. |
| 165 | * '1' is a special value, that will also avoid dumping via pipe. */ |
| 166 | struct rlimit r; |
| 167 | r.rlim_cur = 1; |
| 168 | r.rlim_max = 1; |
| 169 | setrlimit(RLIMIT_CORE, &r); |
| 170 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | TEST_PAUSE; |
| 172 | } |
| 173 | |
Wanlong Gao | 9554de5 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 174 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 175 | { |
Wanlong Gao | 9554de5 | 2012-11-20 10:32:33 +0800 | [diff] [blame] | 176 | } |