1 /******************************************************************************/
2 /*                                                                            */
3 /* Copyright (c) 2009 FUJITSU LIMITED                                         */
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 /* Author: Shi Weihua <shiwh@cn.fujitsu.com>                                  */
20 /*                                                                            */
21 /******************************************************************************/
22 
23 #include <sys/types.h>
24 #include <sys/wait.h>
25 #include <err.h>
26 #include <errno.h>
27 #include <signal.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 
32 #define UNUSED __attribute__ ((unused))
33 
34 static int test_switch = 0;
35 
sighandler(UNUSED int signo)36 void sighandler(UNUSED int signo)
37 {
38 	test_switch = !test_switch;
39 }
40 
main(void)41 int main(void)
42 {
43 	sigset_t signalset;
44 	struct sigaction sa;
45 	pid_t pid;
46 	int status;
47 	int count = 0;
48 
49 	sa.sa_handler = sighandler;
50 	if (sigemptyset(&sa.sa_mask) < 0)
51 		err(1, "sigemptyset()");
52 
53 	sa.sa_flags = 0;
54 	if (sigaction(SIGUSR1, &sa, NULL) < 0)
55 		err(1, "sigaction()");
56 
57 	if (sigemptyset(&signalset) < 0)
58 		err(1, "sigemptyset()");
59 
60 	/* wait for the signal SIGUSR1 to start testing */
61 	sigsuspend(&signalset);
62 	if (errno != EINTR)
63 		err(1, "sigsuspend()");
64 
65 	do {
66 		count++;
67 		pid = fork();
68 		if (pid == -1)
69 			err(1, "fork()");
70 		else if (pid == 0) {
71 			return 0;
72 		} else {
73 			wait(&status);
74 		}
75 	} while (test_switch);
76 
77 	return 0;
78 }
79