• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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
16  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  *
19  * NAME
20  *	fork12.c
21  *
22  * DESCRIPTION
23  *	Check that all children inherit parent's file descriptor
24  *
25  * ALGORITHM
26  *	Parent forks processes until -1 is returned.$
27  *
28  * USAGE
29  *	fork12
30  *	** CAUTION ** Can hang your machine, esp prior to 2.4.19
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
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.
40  */
41 
42 #include <stdio.h>
43 #include <sys/wait.h>
44 #include <errno.h>
45 #include <string.h>
46 #include "test.h"
47 
48 char *TCID = "fork12";
49 int TST_TOTAL = 1;
50 
51 static void setup(void);
52 static void cleanup(void);
53 static void fork12_sigs(int signum);
54 
main(int ac,char ** av)55 int main(int ac, char **av)
56 {
57 	int forks, pid1, fork_errno, waitstatus;
58 	int ret, status;
59 	int lc;
60 
61 	tst_parse_opts(ac, av, NULL, NULL);
62 
63 	setup();
64 
65 	for (lc = 0; TEST_LOOPING(lc); lc++) {
66 		tst_count = 0;
67 
68 		tst_resm(TINFO, "Forking as many kids as possible");
69 		forks = 0;
70 		while ((pid1 = fork()) != -1) {
71 			if (pid1 == 0) {	/* child */
72 				pause();
73 				exit(0);
74 			}
75 			forks++;
76 			ret = waitpid(-1, &status, WNOHANG);
77 			if (ret < 0)
78 				tst_brkm(TBROK, cleanup,
79 					 "waitpid failed %d: %s\n", errno,
80 					 strerror(errno));
81 			if (ret > 0) {
82 				/* a child may be killed by OOM killer */
83 				if (WTERMSIG(status) == SIGKILL)
84 					break;
85 				tst_brkm(TBROK, cleanup,
86 					 "child exit with error code %d or signal %d",
87 					 WEXITSTATUS(status), WTERMSIG(status));
88 			}
89 		}
90 		fork_errno = errno;
91 
92 		/* parent */
93 		tst_resm(TINFO, "Number of processes forked is %d", forks);
94 		tst_resm(TPASS, "fork() eventually failed with %d: %s",
95 			 fork_errno, strerror(fork_errno));
96 		/* collect our kids */
97 		/*
98 		 * Introducing a sleep(3) to make sure all children are
99 		 * at pause() when SIGQUIT is sent to them
100 		 */
101 		sleep(3);
102 		kill(0, SIGQUIT);
103 		while (wait(&waitstatus) > 0) ;
104 
105 	}
106 
107 	cleanup();
108 	tst_exit();
109 }
110 
setup(void)111 static void setup(void)
112 {
113 	tst_sig(FORK, fork12_sigs, cleanup);
114 	TEST_PAUSE;
115 }
116 
cleanup(void)117 static void cleanup(void)
118 {
119 	int waitstatus;
120 
121 	/* collect our kids */
122 	kill(0, SIGQUIT);
123 	while (wait(&waitstatus) > 0) ;
124 }
125 
fork12_sigs(int signum)126 static void fork12_sigs(int signum)
127 {
128 	if (signum == SIGQUIT) {
129 		/* Children will continue, parent will ignore */
130 	} else {
131 		tst_brkm(TBROK, cleanup,
132 			 "Unexpected signal %d received.", signum);
133 	}
134 }
135