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 <wait.h>
79 
80 #include "test.h"
81 
82 char *TCID = "vfork02";
83 int TST_TOTAL = 1;
84 
85 void setup();			/* Main setup function of test */
86 void cleanup();			/* cleanup function for the test */
87 void sig_handler();		/* signal catching function */
88 
main(int ac,char ** av)89 int main(int ac, char **av)
90 {
91 	int lc;
92 	pid_t cpid;		/* process id of the child process */
93 	int exit_status;	/* exit status of child process */
94 	sigset_t PendSig;	/* variable to hold pending signal */
95 
96 	tst_parse_opts(ac, av, NULL, NULL);
97 
98 	setup();
99 
100 	for (lc = 0; TEST_LOOPING(lc); lc++) {
101 
102 		tst_count = 0;
103 
104 		/*
105 		 * Call vfork(2) to create a child process without
106 		 * fully copying the address space of parent.
107 		 */
108 		TEST(vfork());
109 
110 		if ((cpid = TEST_RETURN) == -1) {
111 			tst_resm(TFAIL, "vfork() Failed, errno=%d : %s",
112 				 TEST_ERRNO, strerror(TEST_ERRNO));
113 		} else if (cpid == 0) {	/* Child process */
114 			/*
115 			 * Check whether the pending signal SIGUSR1
116 			 * in the parent is also pending in the child
117 			 * process by storing it in a variable.
118 			 */
119 			if (sigpending(&PendSig) == -1) {
120 				tst_resm(TFAIL, "sigpending function "
121 					 "failed in child");
122 				_exit(1);
123 			}
124 
125 			/* Check if SIGUSR1 is pending in child */
126 			if (sigismember(&PendSig, SIGUSR1) != 0) {
127 				tst_resm(TFAIL, "SIGUSR1 also pending "
128 					 "in child process");
129 				_exit(1);
130 			}
131 
132 			/*
133 			 * Exit with normal exit code if everything
134 			 * fine
135 			 */
136 			_exit(0);
137 		} else {	/* parent process */
138 			/*
139 			 * Let the parent process wait till child completes
140 			 * its execution.
141 			 */
142 			wait(&exit_status);
143 
144 			/* Check for the exit status of child process */
145 			if (WEXITSTATUS(exit_status) == 0) {
146 				tst_resm(TPASS, "Call to vfork() "
147 					 "successful");
148 			} else if (WEXITSTATUS(exit_status) == 1) {
149 				tst_resm(TFAIL,
150 					 "Child process exited abnormally");
151 			}
152 		}
153 		tst_count++;	/* incr. TEST_LOOP counter */
154 	}
155 
156 	cleanup();
157 	tst_exit();
158 
159 }
160 
161 /*
162  * void
163  * setup() - performs all ONE TIME setup for this test.
164  *   This function installs signal handler for SIGUSR1, puts signal SIGUSR1
165  *   on hold and then sends the signal SIGUSR1 to itself so that it is in
166  *   pending state.
167  */
setup(void)168 void setup(void)
169 {
170 	sigset_t PendSig;	/* variable to hold pending signal */
171 
172 	tst_sig(FORK, DEF_HANDLER, cleanup);
173 
174 	TEST_PAUSE;
175 
176 	/* Install the signal handler */
177 	if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
178 		tst_brkm(TBROK, cleanup, "Fails to catch the signal SIGUSR1");
179 	}
180 
181 	/* Hold the signal SIGUSR1 */
182 	if (sighold(SIGUSR1) == -1) {
183 		tst_brkm(TBROK, cleanup,
184 			 "sighold failed to hold the signal SIGUSR1");
185 	}
186 
187 	/* Send the signal SIGUSR1 to itself so that SIGUSR1 is pending */
188 	if (kill(getpid(), SIGUSR1) == -1) {
189 		tst_brkm(TBROK, cleanup,
190 			 "Fails to send the signal to the parent process");
191 	}
192 
193 	/* If SIGUSR1 is not pending in the parent, fail */
194 	if (sigpending(&PendSig) == -1) {
195 		tst_brkm(TBROK, cleanup,
196 			 "sigpending function failed in parent");
197 	}
198 
199 	/* Check if SIGUSR1 is pending in parent */
200 	if (sigismember(&PendSig, SIGUSR1) != 1) {
201 		tst_brkm(TBROK, cleanup,
202 			 "SIGUSR1 signal is not pending in parent");
203 	}
204 }
205 
206 /*
207  * void
208  * sig_handler() - signal catching function for 'SIGUSR1' signal.
209  *		 $
210  *   This is a null function and used only to catch the above signal
211  *   generated in parent process.
212  */
sig_handler(void)213 void sig_handler(void)
214 {
215 }
216 
217 /*
218  * void
219  * cleanup() - performs all ONE TIME cleanup for this test at
220  *             completion or premature exit.
221  *  Release the signal 'SIGUSR1'  if still in pending state.
222  */
cleanup(void)223 void cleanup(void)
224 {
225 
226 	/* Release the signal 'SIGUSR1' if in pending state */
227 	if (sigrelse(SIGUSR1) == -1) {
228 		tst_brkm(TBROK, NULL, "Failed to release 'SIGUSR1' in cleanup");
229 	}
230 
231 }
232