nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 1 | /* |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 12 | * the GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
Wanlong Gao | 9ae5cba | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 17 | * |
| 18 | * |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 19 | * NAME |
Wanlong Gao | 9ae5cba | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 20 | * fork12.c |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 21 | * |
| 22 | * DESCRIPTION |
| 23 | * Check that all children inherit parent's file descriptor |
| 24 | * |
| 25 | * ALGORITHM |
Wanlong Gao | 9ae5cba | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 26 | * Parent forks processes until -1 is returned.$ |
| 27 | * |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 28 | * USAGE |
Wanlong Gao | 9ae5cba | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 29 | * fork12 |
| 30 | * ** CAUTION ** Can hang your machine, esp prior to 2.4.19 |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 31 | * |
| 32 | * HISTORY |
| 33 | * 07/2001 Ported by Wayne Boyer |
| 34 | * 07/2002 Split from fork07 as a test case to exhaust available pids. |
| 35 | * |
| 36 | * RESTRICTIONS |
Wanlong Gao | 9ae5cba | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 37 | * Should be run as root to avoid resource limits.$ |
| 38 | * Should not be run with other test programs because it tries to |
| 39 | * use all available pids. |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 40 | */ |
| 41 | |
| 42 | #include <stdio.h> |
| 43 | #include <sys/wait.h> |
| 44 | #include <errno.h> |
| 45 | #include <string.h> |
| 46 | #include "test.h" |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 47 | |
| 48 | char *TCID = "fork12"; |
| 49 | int TST_TOTAL = 1; |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 50 | |
Wanlong Gao | 9ae5cba | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 51 | static void setup(void); |
| 52 | static void cleanup(void); |
| 53 | static void fork12_sigs(int signum); |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 54 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 55 | int main(int ac, char **av) |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 56 | { |
| 57 | int forks, pid1, fork_errno, waitstatus; |
subrata_modak | 44bbe6b | 2008-06-08 13:34:16 +0000 | [diff] [blame] | 58 | int ret, status; |
Wanlong Gao | 9ae5cba | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 59 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 60 | const char *msg; |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 61 | |
Wanlong Gao | 9ae5cba | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 62 | msg = parse_opts(ac, av, NULL, NULL); |
| 63 | if (msg != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 64 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 65 | |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 66 | setup(); |
| 67 | |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 68 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 69 | tst_count = 0; |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 70 | |
| 71 | tst_resm(TINFO, "Forking as many kids as possible"); |
| 72 | forks = 0; |
| 73 | while ((pid1 = fork()) != -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 74 | if (pid1 == 0) { /* child */ |
| 75 | pause(); |
| 76 | exit(0); |
| 77 | } |
| 78 | forks++; |
| 79 | ret = waitpid(-1, &status, WNOHANG); |
| 80 | if (ret < 0) |
| 81 | tst_brkm(TBROK, cleanup, |
| 82 | "waitpid failed %d: %s\n", errno, |
| 83 | strerror(errno)); |
| 84 | if (ret > 0) { |
| 85 | /* a child may be killed by OOM killer */ |
| 86 | if (WTERMSIG(status) == SIGKILL) |
| 87 | break; |
| 88 | tst_brkm(TBROK, cleanup, |
| 89 | "child exit with error code %d or signal %d", |
| 90 | WEXITSTATUS(status), WTERMSIG(status)); |
| 91 | } |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 92 | } |
| 93 | fork_errno = errno; |
| 94 | |
| 95 | /* parent */ |
| 96 | tst_resm(TINFO, "Number of processes forked is %d", forks); |
| 97 | tst_resm(TPASS, "fork() eventually failed with %d: %s", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 98 | fork_errno, strerror(fork_errno)); |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 99 | /* collect our kids */ |
Wanlong Gao | 9ae5cba | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 100 | /* |
| 101 | * Introducing a sleep(3) to make sure all children are |
| 102 | * at pause() when SIGQUIT is sent to them |
| 103 | */ |
| 104 | sleep(3); |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 105 | kill(0, SIGQUIT); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 106 | while (wait(&waitstatus) > 0) ; |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 107 | |
| 108 | } |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 109 | |
Wanlong Gao | 9ae5cba | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 110 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 111 | tst_exit(); |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 114 | static void setup(void) |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 115 | { |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 116 | tst_sig(FORK, fork12_sigs, cleanup); |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 117 | TEST_PAUSE; |
| 118 | } |
| 119 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 120 | static void cleanup(void) |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 121 | { |
| 122 | int waitstatus; |
| 123 | |
| 124 | /* collect our kids */ |
| 125 | kill(0, SIGQUIT); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 126 | while (wait(&waitstatus) > 0) ; |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Wanlong Gao | 9ae5cba | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 129 | static void fork12_sigs(int signum) |
nstraz | cdae3fc | 2002-07-19 15:59:19 +0000 | [diff] [blame] | 130 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 131 | if (signum == SIGQUIT) { |
| 132 | /* Children will continue, parent will ignore */ |
Wanlong Gao | 9ae5cba | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 133 | } else { |
Garrett Cooper | 5262667 | 2010-12-20 15:03:21 -0800 | [diff] [blame] | 134 | tst_brkm(TBROK, cleanup, |
Wanlong Gao | 9ae5cba | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 135 | "Unexpected signal %d received.", signum); |
| 136 | } |
Garrett Cooper | 5262667 | 2010-12-20 15:03:21 -0800 | [diff] [blame] | 137 | } |