blob: da8012c93354da3163afa9b5b46454665a6eb496 [file] [log] [blame]
robbiewc787d952002-12-30 19:47:40 +00001/*
robbiewc787d952002-12-30 19:47:40 +00002 * Copyright (c) International Business Machines Corp., 2002
Wanlong Gaod753c212013-04-24 14:03:21 +08003 * 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
robbiewc787d952002-12-30 19:47:40 +00006 *
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 Gao4548c6c2012-10-19 18:03:36 +080019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiewc787d952002-12-30 19:47:40 +000020 */
21
Wanlong Gaod753c212013-04-24 14:03:21 +080022 /* ALGORITHM
robbiewc787d952002-12-30 19:47:40 +000023 * Fork child. Have child abort, check return status.
24 *
25 * RESTRICTIONS
26 * The ulimit for core file size must be greater than 0.
robbiewc787d952002-12-30 19:47:40 +000027 */
28
robbiewc787d952002-12-30 19:47:40 +000029#include <sys/types.h>
30#include <sys/wait.h>
Garrett Cooper53740502010-12-16 00:04:01 -080031#include <errno.h>
32#include <signal.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <unistd.h>
Cyril Hrubis411f70d2014-08-12 17:05:11 +020036#include <sys/resource.h>
robbiewc787d952002-12-30 19:47:40 +000037
robbiewc787d952002-12-30 19:47:40 +000038#include "test.h"
Cyril Hrubis411f70d2014-08-12 17:05:11 +020039#include "safe_macros.h"
40
Wanlong Gaod753c212013-04-24 14:03:21 +080041#define NUM 3
robbiewc787d952002-12-30 19:47:40 +000042
robbiewc787d952002-12-30 19:47:40 +000043char *TCID = "abort01";
robbiewc787d952002-12-30 19:47:40 +000044int TST_TOTAL = 1;
robbiewc787d952002-12-30 19:47:40 +000045
Wanlong Gaod753c212013-04-24 14:03:21 +080046static void setup(void);
47static void cleanup(void);
48static void do_child();
49static int instress();
Garrett Cooper53740502010-12-16 00:04:01 -080050
subrata_modak56207ce2009-03-23 13:35:39 +000051int main(int argc, char *argv[])
robbiewc787d952002-12-30 19:47:40 +000052{
53 register int i;
54 int status, count, child, kidpid;
Garrett Cooper5bd58842010-12-20 11:32:44 -080055 int sig, ex;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020056 const char *msg;
robbiewd34d5812005-07-11 22:28:09 +000057
Garrett Cooper5bd58842010-12-20 11:32:44 -080058#ifdef WCOREDUMP
59 int core;
Garrett Cooper5bd58842010-12-20 11:32:44 -080060 core = 0;
61#endif
62 ex = sig = 0;
Garrett Cooper53740502010-12-16 00:04:01 -080063
Wanlong Gaod753c212013-04-24 14:03:21 +080064 msg = parse_opts(argc, argv, NULL, NULL);
65 if (msg != NULL)
robbiewd34d5812005-07-11 22:28:09 +000066 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewd34d5812005-07-11 22:28:09 +000067#ifdef UCLINUX
68 maybe_run_child(&do_child, "");
69#endif
robbiewc787d952002-12-30 19:47:40 +000070
Garrett Cooper53740502010-12-16 00:04:01 -080071 setup();
robbiewc787d952002-12-30 19:47:40 +000072
Wanlong Gaod753c212013-04-24 14:03:21 +080073 for (i = 0; i < NUM; i++) {
74 kidpid = FORK_OR_VFORK();
75 if (kidpid == 0) {
robbiewd34d5812005-07-11 22:28:09 +000076#ifdef UCLINUX
77 if (self_exec(argv[0], "")) {
Garrett Cooper7ed507b2011-01-21 09:38:24 -080078 if (!instress()) {
Garrett Cooper53740502010-12-16 00:04:01 -080079 perror("fork failed");
80 exit(1);
81 }
robbiewd34d5812005-07-11 22:28:09 +000082 }
83#else
84 do_child();
85#endif
robbiewc787d952002-12-30 19:47:40 +000086 }
Garrett Cooper5bd58842010-12-20 11:32:44 -080087 if (kidpid < 0)
Garrett Cooper53740502010-12-16 00:04:01 -080088 if (!instress())
Wanlong Gao354ebb42012-12-07 10:10:04 +080089 tst_brkm(TBROK | TERRNO, cleanup,
90 "fork failed");
robbiewc787d952002-12-30 19:47:40 +000091 count = 0;
Garrett Cooper5bd58842010-12-20 11:32:44 -080092 while ((child = wait(&status)) > 0)
robbiewc787d952002-12-30 19:47:40 +000093 count++;
robbiewc787d952002-12-30 19:47:40 +000094 if (count != 1) {
Garrett Cooper53740502010-12-16 00:04:01 -080095 tst_brkm(TBROK, cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +080096 "wrong # children waited on; got %d, expected 1",
97 count);
robbiewc787d952002-12-30 19:47:40 +000098 }
Garrett Cooper53740502010-12-16 00:04:01 -080099 if (WIFSIGNALED(status)) {
100
subrata_modak56207ce2009-03-23 13:35:39 +0000101#ifdef WCOREDUMP
Garrett Cooper53740502010-12-16 00:04:01 -0800102 core = WCOREDUMP(status);
subrata_modak56207ce2009-03-23 13:35:39 +0000103#endif
Garrett Cooper53740502010-12-16 00:04:01 -0800104 sig = WTERMSIG(status);
105
106 }
107 if (WIFEXITED(status))
Garrett Cooper5bd58842010-12-20 11:32:44 -0800108 ex = WEXITSTATUS(status);
Garrett Cooper53740502010-12-16 00:04:01 -0800109
Garrett Cooper5bd58842010-12-20 11:32:44 -0800110#ifdef WCOREDUMP
Garrett Cooper53740502010-12-16 00:04:01 -0800111 if (core == 0) {
112 tst_brkm(TFAIL, cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800113 "Child did not dump core; exit code = %d, "
114 "signal = %d", ex, sig);
Wanlong Gaod753c212013-04-24 14:03:21 +0800115 } else if (core != -1) {
Garrett Cooper53740502010-12-16 00:04:01 -0800116 tst_resm(TPASS, "abort dumped core");
Wanlong Gaod753c212013-04-24 14:03:21 +0800117 }
Garrett Cooper5bd58842010-12-20 11:32:44 -0800118#endif
Wanlong Gaod753c212013-04-24 14:03:21 +0800119 if (sig == SIGIOT) {
Garrett Cooper53740502010-12-16 00:04:01 -0800120 tst_resm(TPASS, "abort raised SIGIOT");
Wanlong Gaod753c212013-04-24 14:03:21 +0800121 } else {
Garrett Cooper53740502010-12-16 00:04:01 -0800122 tst_brkm(TFAIL, cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800123 "Child did not raise SIGIOT (%d); exit code = %d, "
124 "signal = %d", SIGIOT, ex, sig);
robbiewc787d952002-12-30 19:47:40 +0000125 }
Garrett Cooper53740502010-12-16 00:04:01 -0800126
robbiewc787d952002-12-30 19:47:40 +0000127 }
128
Garrett Cooper53740502010-12-16 00:04:01 -0800129 cleanup();
130 tst_exit();
robbiewc787d952002-12-30 19:47:40 +0000131}
subrata_modak56207ce2009-03-23 13:35:39 +0000132
Cyril Hrubis411f70d2014-08-12 17:05:11 +0200133/* 1024 GNU blocks */
134#define MIN_RLIMIT_CORE (1024 * 1024)
135
Wanlong Gaod753c212013-04-24 14:03:21 +0800136static void setup(void)
137{
Cyril Hrubis411f70d2014-08-12 17:05:11 +0200138 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 Gaod753c212013-04-24 14:03:21 +0800148 tst_tmpdir();
149}
150
151static void cleanup(void)
152{
153 unlink("core");
154 tst_rmdir();
155}
156
Mike Frysingerc57fba52014-04-09 18:56:30 -0400157static void do_child(void)
subrata_modak56207ce2009-03-23 13:35:39 +0000158{
robbiewd34d5812005-07-11 22:28:09 +0000159 abort();
Garrett Cooper53740502010-12-16 00:04:01 -0800160 fprintf(stderr, "\tchild - abort failed.\n");
161 exit(1);
robbiewc787d952002-12-30 19:47:40 +0000162}
163
Mike Frysingerc57fba52014-04-09 18:56:30 -0400164static int instress(void)
robbiewc787d952002-12-30 19:47:40 +0000165{
subrata_modak56207ce2009-03-23 13:35:39 +0000166 tst_resm(TINFO,
167 "System resources may be too low; fork(), select() etc are likely to fail.");
168 return 1;
Garrett Cooper5bd58842010-12-20 11:32:44 -0800169}