robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 1 | /* |
robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2002 |
Wanlong Gao | d753c21 | 2013-04-24 14:03:21 +0800 | [diff] [blame] | 3 | * 01/02/2003 Port to LTP avenkat@us.ibm.com |
| 4 | * 11/11/2002: Ported to LTP Suite by Ananda |
| 5 | * 06/30/2001 Port to Linux nsharoff@us.ibm.com |
robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 15 | * the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Wanlong Gao | d753c21 | 2013-04-24 14:03:21 +0800 | [diff] [blame] | 22 | /* ALGORITHM |
robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 23 | * Fork child. Have child abort, check return status. |
| 24 | * |
| 25 | * RESTRICTIONS |
| 26 | * The ulimit for core file size must be greater than 0. |
robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 27 | */ |
| 28 | |
robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 29 | #include <sys/types.h> |
| 30 | #include <sys/wait.h> |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 31 | #include <errno.h> |
| 32 | #include <signal.h> |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <unistd.h> |
Cyril Hrubis | 411f70d | 2014-08-12 17:05:11 +0200 | [diff] [blame] | 36 | #include <sys/resource.h> |
robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 37 | |
robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 38 | #include "test.h" |
Cyril Hrubis | 411f70d | 2014-08-12 17:05:11 +0200 | [diff] [blame] | 39 | #include "safe_macros.h" |
| 40 | |
Wanlong Gao | d753c21 | 2013-04-24 14:03:21 +0800 | [diff] [blame] | 41 | #define NUM 3 |
robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 42 | |
robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 43 | char *TCID = "abort01"; |
robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 44 | int TST_TOTAL = 1; |
robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 45 | |
Wanlong Gao | d753c21 | 2013-04-24 14:03:21 +0800 | [diff] [blame] | 46 | static void setup(void); |
| 47 | static void cleanup(void); |
| 48 | static void do_child(); |
| 49 | static int instress(); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 50 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 51 | int main(int argc, char *argv[]) |
robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 52 | { |
| 53 | register int i; |
| 54 | int status, count, child, kidpid; |
Garrett Cooper | 5bd5884 | 2010-12-20 11:32:44 -0800 | [diff] [blame] | 55 | int sig, ex; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 56 | const char *msg; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 57 | |
Garrett Cooper | 5bd5884 | 2010-12-20 11:32:44 -0800 | [diff] [blame] | 58 | #ifdef WCOREDUMP |
| 59 | int core; |
Garrett Cooper | 5bd5884 | 2010-12-20 11:32:44 -0800 | [diff] [blame] | 60 | core = 0; |
| 61 | #endif |
| 62 | ex = sig = 0; |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 63 | |
Wanlong Gao | d753c21 | 2013-04-24 14:03:21 +0800 | [diff] [blame] | 64 | msg = parse_opts(argc, argv, NULL, NULL); |
| 65 | if (msg != NULL) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 66 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 67 | #ifdef UCLINUX |
| 68 | maybe_run_child(&do_child, ""); |
| 69 | #endif |
robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 70 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 71 | setup(); |
robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 72 | |
Wanlong Gao | d753c21 | 2013-04-24 14:03:21 +0800 | [diff] [blame] | 73 | for (i = 0; i < NUM; i++) { |
| 74 | kidpid = FORK_OR_VFORK(); |
| 75 | if (kidpid == 0) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 76 | #ifdef UCLINUX |
| 77 | if (self_exec(argv[0], "")) { |
Garrett Cooper | 7ed507b | 2011-01-21 09:38:24 -0800 | [diff] [blame] | 78 | if (!instress()) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 79 | perror("fork failed"); |
| 80 | exit(1); |
| 81 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 82 | } |
| 83 | #else |
| 84 | do_child(); |
| 85 | #endif |
robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 86 | } |
Garrett Cooper | 5bd5884 | 2010-12-20 11:32:44 -0800 | [diff] [blame] | 87 | if (kidpid < 0) |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 88 | if (!instress()) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 89 | tst_brkm(TBROK | TERRNO, cleanup, |
| 90 | "fork failed"); |
robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 91 | count = 0; |
Garrett Cooper | 5bd5884 | 2010-12-20 11:32:44 -0800 | [diff] [blame] | 92 | while ((child = wait(&status)) > 0) |
robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 93 | count++; |
robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 94 | if (count != 1) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 95 | tst_brkm(TBROK, cleanup, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 96 | "wrong # children waited on; got %d, expected 1", |
| 97 | count); |
robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 98 | } |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 99 | if (WIFSIGNALED(status)) { |
| 100 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | #ifdef WCOREDUMP |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 102 | core = WCOREDUMP(status); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 103 | #endif |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 104 | sig = WTERMSIG(status); |
| 105 | |
| 106 | } |
| 107 | if (WIFEXITED(status)) |
Garrett Cooper | 5bd5884 | 2010-12-20 11:32:44 -0800 | [diff] [blame] | 108 | ex = WEXITSTATUS(status); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 109 | |
Garrett Cooper | 5bd5884 | 2010-12-20 11:32:44 -0800 | [diff] [blame] | 110 | #ifdef WCOREDUMP |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 111 | if (core == 0) { |
| 112 | tst_brkm(TFAIL, cleanup, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 113 | "Child did not dump core; exit code = %d, " |
| 114 | "signal = %d", ex, sig); |
Wanlong Gao | d753c21 | 2013-04-24 14:03:21 +0800 | [diff] [blame] | 115 | } else if (core != -1) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 116 | tst_resm(TPASS, "abort dumped core"); |
Wanlong Gao | d753c21 | 2013-04-24 14:03:21 +0800 | [diff] [blame] | 117 | } |
Garrett Cooper | 5bd5884 | 2010-12-20 11:32:44 -0800 | [diff] [blame] | 118 | #endif |
Wanlong Gao | d753c21 | 2013-04-24 14:03:21 +0800 | [diff] [blame] | 119 | if (sig == SIGIOT) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 120 | tst_resm(TPASS, "abort raised SIGIOT"); |
Wanlong Gao | d753c21 | 2013-04-24 14:03:21 +0800 | [diff] [blame] | 121 | } else { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 122 | tst_brkm(TFAIL, cleanup, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 123 | "Child did not raise SIGIOT (%d); exit code = %d, " |
| 124 | "signal = %d", SIGIOT, ex, sig); |
robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 125 | } |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 126 | |
robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 129 | cleanup(); |
| 130 | tst_exit(); |
robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 131 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 132 | |
Cyril Hrubis | 411f70d | 2014-08-12 17:05:11 +0200 | [diff] [blame] | 133 | /* 1024 GNU blocks */ |
| 134 | #define MIN_RLIMIT_CORE (1024 * 1024) |
| 135 | |
Wanlong Gao | d753c21 | 2013-04-24 14:03:21 +0800 | [diff] [blame] | 136 | static void setup(void) |
| 137 | { |
Cyril Hrubis | 411f70d | 2014-08-12 17:05:11 +0200 | [diff] [blame] | 138 | struct rlimit rlim; |
| 139 | |
| 140 | SAFE_GETRLIMIT(NULL, RLIMIT_CORE, &rlim); |
| 141 | |
| 142 | if (rlim.rlim_cur < MIN_RLIMIT_CORE) { |
| 143 | tst_resm(TINFO, "Adjusting RLIMIT_CORE to %i", MIN_RLIMIT_CORE); |
| 144 | rlim.rlim_cur = MIN_RLIMIT_CORE; |
| 145 | SAFE_SETRLIMIT(NULL, RLIMIT_CORE, &rlim); |
| 146 | } |
| 147 | |
Wanlong Gao | d753c21 | 2013-04-24 14:03:21 +0800 | [diff] [blame] | 148 | tst_tmpdir(); |
| 149 | } |
| 150 | |
| 151 | static void cleanup(void) |
| 152 | { |
| 153 | unlink("core"); |
| 154 | tst_rmdir(); |
| 155 | } |
| 156 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 157 | static void do_child(void) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 158 | { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 159 | abort(); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 160 | fprintf(stderr, "\tchild - abort failed.\n"); |
| 161 | exit(1); |
robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 164 | static int instress(void) |
robbiew | c787d95 | 2002-12-30 19:47:40 +0000 | [diff] [blame] | 165 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 166 | tst_resm(TINFO, |
| 167 | "System resources may be too low; fork(), select() etc are likely to fail."); |
| 168 | return 1; |
Garrett Cooper | 5bd5884 | 2010-12-20 11:32:44 -0800 | [diff] [blame] | 169 | } |