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: alarm07
22  *
23  * Test Description:
24  *  Check the functionality of the alarm() when the time input
25  *  parameter is non-zero and the process does a fork.
26  *
27  * Expected Result:
28  *  The alarm request should be cleared in the child process.
29  *
30  * Algorithm:
31  *  Setup:
32  *   Setup signal handling.
33  *   Pause for SIGUSR1 if option specified.
34  *
35  *  Test:
36  *   Loop if the proper options are given.
37  *   Execute system call
38  *   Check return code, if system call failed (return=-1)
39  *   	Log the errno and Issue a FAIL message.
40  *   Otherwise,
41  *   	Verify the Functionality of system call
42  *      if successful,
43  *      	Issue Functionality-Pass message.
44  *      Otherwise,
45  *		Issue Functionality-Fail message.
46  *  Cleanup:
47  *   Print errno log and/or timing stats if options given
48  *
49  * Usage:  <for command-line>
50  *  alarm07 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
51  *     where,  -c n : Run n copies concurrently.
52  *             -f   : Turn off functionality Testing.
53  *	       -i n : Execute test n times.
54  *	       -I x : Execute test for x seconds.
55  *	       -P x : Pause for x seconds between iterations.
56  *	       -t   : Turn on syscall timing.
57  *
58  * HISTORY
59  *	07/2001 Ported by Wayne Boyer
60  *
61  * RESTRICTIONS:
62  *  None.
63  */
64 
65 #include <stdio.h>
66 #include <unistd.h>
67 #include <sys/types.h>
68 #include <errno.h>
69 #include <string.h>
70 #include <signal.h>
71 #include <sys/wait.h>
72 
73 #include "test.h"
74 #include "safe_macros.h"
75 
76 char *TCID = "alarm07";
77 int TST_TOTAL = 1;
78 int alarms_received = 0;
79 
80 void setup();
81 void cleanup();
82 void sigproc(int sig);
83 
84 int main(int ac, char **av)
85 {
86 	int lc;
87 	int sleep_time = 5;
88 	int status;
89 	int time_sec = 3;
90 	pid_t cpid;
91 
92 	tst_parse_opts(ac, av, NULL, NULL);
93 
94 	setup();
95 
96 	for (lc = 0; TEST_LOOPING(lc); lc++) {
97 
98 		tst_count = 0;
99 
100 		/*
101 		 * Call First alarm() with non-zero time parameter
102 		 * 'time_sec' to send SIGALRM to the process.
103 		 */
104 		TEST(alarm(time_sec));
105 
106 		/* Now, fork a child process */
107 		cpid = FORK_OR_VFORK();
108 		if (cpid < 0) {
109 			tst_resm(TFAIL | TERRNO, "fork() failed");
110 		}
111 
112 		sleep(sleep_time);
113 
114 		if (cpid == 0) {
115 			if (alarms_received == 0)
116 				exit(0);
117 			else {
118 				printf("alarm request not cleared in "
119 				       "child; alarms received:%d\n",
120 				       alarms_received);
121 				exit(1);
122 			}
123 		} else {
124 			/* Wait for child to complete execution */
125 			SAFE_WAIT(cleanup, &status);
126 			if (!WIFEXITED(status) ||
127 			    WEXITSTATUS(status) != 0)
128 				tst_brkm(TBROK | TERRNO, cleanup,
129 					 "child exited abnormally");
130 		}
131 	}
132 
133 	cleanup();
134 	tst_exit();
135 }
136 
137 /*
138  * setup() - performs all ONE TIME setup for this test.
139  *  Setup signal handler to catch SIGALRM signal.
140  */
141 void setup(void)
142 {
143 
144 	tst_sig(FORK, DEF_HANDLER, cleanup);
145 
146 	TEST_PAUSE;
147 
148 	/* Set the signal catching function */
149 	if (signal(SIGALRM, sigproc) == SIG_ERR) {
150 		tst_brkm(TFAIL | TERRNO, cleanup, "signal(SIGALRM, ..) failed");
151 	}
152 }
153 
154 /*
155  * sigproc(int) - This function defines the action that has to be taken
156  *	          when the SIGALRM signal is caught.
157  *   It also sets the variable which is used to check whether the
158  *   alarm system call was successful.
159  */
160 void sigproc(int sig)
161 {
162 	alarms_received++;
163 }
164 
165 /*
166  * cleanup() - performs all ONE TIME cleanup for this test at
167  *             completion or premature exit.
168  */
169 void cleanup(void)
170 {
171 }
172