• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /*
21  * Test Name: vfork02
22  *
23  * Test Description:
24  *  Fork a process using vfork() and verify that, the pending signals in
25  *  the parent are not pending in the child process.
26  * $
27  * Expected Result:
28  *  The signal which is pending in the parent should not be pending in the
29  *  child process.
30  *
31  * Algorithm:
32  *  Setup:
33  *   Setup signal handling.
34  *   Pause for SIGUSR1 if option specified.
35  *
36  *  Test:
37  *   Loop if the proper options are given.
38  *   Execute system call
39  *   Check return code, if system call failed (return=-1)
40  *   	Log the errno and Issue a FAIL message.
41  *   Otherwise,
42  *   	Verify the Functionality of system call
43  *      if successful,
44  *      	Issue Functionality-Pass message.
45  *      Otherwise,
46  *		Issue Functionality-Fail message.
47  *  Cleanup:
48  *   Print errno log and/or timing stats if options given
49  *
50  * Usage:  <for command-line>
51  *  vfork02 [-c n] [-e] [-f] [-i n] [-I x] [-p x] [-t]
52  *     where,  -c n : Run n copies concurrently.
53  *             -e   : Turn on errno logging.
54  *             -f   : Turn off functionality Testing.
55  *	       -i n : Execute test n times.
56  *	       -I x : Execute test for x seconds.
57  *	       -P x : Pause for x seconds between iterations.
58  *	       -t   : Turn on syscall timing.
59  *
60  * History
61  *	07/2001 John George
62  *		-Ported
63  *
64  * Restrictions:
65  *  None.
66  *
67  */
68 #define _GNU_SOURCE 1
69 
70 #include <stdio.h>
71 #include <sys/types.h>
72 #include <errno.h>
73 #include <unistd.h>
74 #include <fcntl.h>
75 #include <string.h>
76 #include <signal.h>
77 #include <sys/stat.h>
78 #include <sys/wait.h>
79 
80 #include "test.h"
81 #include "safe_macros.h"
82 
83 char *TCID = "vfork02";
84 int TST_TOTAL = 1;
85 
86 void setup();			/* Main setup function of test */
87 void cleanup();			/* cleanup function for the test */
88 void sig_handler();		/* signal catching function */
89 
main(int ac,char ** av)90 int main(int ac, char **av)
91 {
92 	int lc;
93 	pid_t cpid;		/* process id of the child process */
94 	int exit_status;	/* exit status of child process */
95 	sigset_t PendSig;	/* variable to hold pending signal */
96 
97 	tst_parse_opts(ac, av, NULL, NULL);
98 
99 	setup();
100 
101 	for (lc = 0; TEST_LOOPING(lc); lc++) {
102 
103 		tst_count = 0;
104 
105 		/*
106 		 * Call vfork(2) to create a child process without
107 		 * fully copying the address space of parent.
108 		 */
109 		TEST(vfork());
110 
111 		if ((cpid = TEST_RETURN) == -1) {
112 			tst_resm(TFAIL, "vfork() Failed, errno=%d : %s",
113 				 TEST_ERRNO, strerror(TEST_ERRNO));
114 		} else if (cpid == 0) {	/* Child process */
115 			/*
116 			 * Check whether the pending signal SIGUSR1
117 			 * in the parent is also pending in the child
118 			 * process by storing it in a variable.
119 			 */
120 			if (sigpending(&PendSig) == -1) {
121 				tst_resm(TFAIL, "sigpending function "
122 					 "failed in child");
123 				_exit(1);
124 			}
125 
126 			/* Check if SIGUSR1 is pending in child */
127 			if (sigismember(&PendSig, SIGUSR1) != 0) {
128 				tst_resm(TFAIL, "SIGUSR1 also pending "
129 					 "in child process");
130 				_exit(1);
131 			}
132 
133 			/*
134 			 * Exit with normal exit code if everything
135 			 * fine
136 			 */
137 			_exit(0);
138 		} else {	/* parent process */
139 			/*
140 			 * Let the parent process wait till child completes
141 			 * its execution.
142 			 */
143 			wait(&exit_status);
144 
145 			/* Check for the exit status of child process */
146 			if (WEXITSTATUS(exit_status) == 0) {
147 				tst_resm(TPASS, "Call to vfork() "
148 					 "successful");
149 			} else if (WEXITSTATUS(exit_status) == 1) {
150 				tst_resm(TFAIL,
151 					 "Child process exited abnormally");
152 			}
153 		}
154 		tst_count++;	/* incr. TEST_LOOP counter */
155 	}
156 
157 	cleanup();
158 	tst_exit();
159 
160 }
161 
162 /*
163  * void
164  * setup() - performs all ONE TIME setup for this test.
165  *   This function installs signal handler for SIGUSR1, puts signal SIGUSR1
166  *   on hold and then sends the signal SIGUSR1 to itself so that it is in
167  *   pending state.
168  */
setup(void)169 void setup(void)
170 {
171 	sigset_t PendSig;	/* variable to hold pending signal */
172 
173 	tst_sig(FORK, DEF_HANDLER, cleanup);
174 
175 	TEST_PAUSE;
176 
177 	/* Install the signal handler */
178 	if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
179 		tst_brkm(TBROK, cleanup, "Fails to catch the signal SIGUSR1");
180 	}
181 
182 	/* Hold the signal SIGUSR1 */
183 	if (sighold(SIGUSR1) == -1) {
184 		tst_brkm(TBROK, cleanup,
185 			 "sighold failed to hold the signal SIGUSR1");
186 	}
187 
188 	/* Send the signal SIGUSR1 to itself so that SIGUSR1 is pending */
189 	SAFE_KILL(cleanup, getpid(), SIGUSR1);
190 
191 	/* If SIGUSR1 is not pending in the parent, fail */
192 	if (sigpending(&PendSig) == -1) {
193 		tst_brkm(TBROK, cleanup,
194 			 "sigpending function failed in parent");
195 	}
196 
197 	/* Check if SIGUSR1 is pending in parent */
198 	if (sigismember(&PendSig, SIGUSR1) != 1) {
199 		tst_brkm(TBROK, cleanup,
200 			 "SIGUSR1 signal is not pending in parent");
201 	}
202 }
203 
204 /*
205  * void
206  * sig_handler() - signal catching function for 'SIGUSR1' signal.
207  *		 $
208  *   This is a null function and used only to catch the above signal
209  *   generated in parent process.
210  */
sig_handler(void)211 void sig_handler(void)
212 {
213 }
214 
215 /*
216  * void
217  * cleanup() - performs all ONE TIME cleanup for this test at
218  *             completion or premature exit.
219  *  Release the signal 'SIGUSR1'  if still in pending state.
220  */
cleanup(void)221 void cleanup(void)
222 {
223 
224 	/* Release the signal 'SIGUSR1' if in pending state */
225 	if (sigrelse(SIGUSR1) == -1) {
226 		tst_brkm(TBROK, NULL, "Failed to release 'SIGUSR1' in cleanup");
227 	}
228 
229 }
230