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
75 char *TCID = "alarm07";
76 int TST_TOTAL = 1;
77 int alarms_received = 0;
78
79 void setup();
80 void cleanup();
81 void sigproc(int sig);
82
main(int ac,char ** av)83 int main(int ac, char **av)
84 {
85 int lc;
86 int sleep_time = 5;
87 int status;
88 int time_sec = 3;
89 pid_t cpid;
90
91 tst_parse_opts(ac, av, NULL, NULL);
92
93 setup();
94
95 for (lc = 0; TEST_LOOPING(lc); lc++) {
96
97 tst_count = 0;
98
99 /*
100 * Call First alarm() with non-zero time parameter
101 * 'time_sec' to send SIGALRM to the process.
102 */
103 TEST(alarm(time_sec));
104
105 /* Now, fork a child process */
106 cpid = FORK_OR_VFORK();
107 if (cpid < 0) {
108 tst_resm(TFAIL | TERRNO, "fork() failed");
109 }
110
111 sleep(sleep_time);
112
113 if (cpid == 0) {
114 if (alarms_received == 0)
115 exit(0);
116 else {
117 printf("alarm request not cleared in "
118 "child; alarms received:%d\n",
119 alarms_received);
120 exit(1);
121 }
122 } else {
123 /* Wait for child to complete execution */
124 if (wait(&status) == -1)
125 tst_brkm(TBROK | TERRNO, cleanup,
126 "wait failed");
127 if (!WIFEXITED(status) ||
128 WEXITSTATUS(status) != 0)
129 tst_brkm(TBROK | TERRNO, cleanup,
130 "child exited abnormally");
131 }
132 }
133
134 cleanup();
135 tst_exit();
136 }
137
138 /*
139 * setup() - performs all ONE TIME setup for this test.
140 * Setup signal handler to catch SIGALRM signal.
141 */
setup(void)142 void setup(void)
143 {
144
145 tst_sig(FORK, DEF_HANDLER, cleanup);
146
147 TEST_PAUSE;
148
149 /* Set the signal catching function */
150 if (signal(SIGALRM, sigproc) == SIG_ERR) {
151 tst_brkm(TFAIL | TERRNO, cleanup, "signal(SIGALRM, ..) failed");
152 }
153 }
154
155 /*
156 * sigproc(int) - This function defines the action that has to be taken
157 * when the SIGALRM signal is caught.
158 * It also sets the variable which is used to check whether the
159 * alarm system call was successful.
160 */
sigproc(int sig)161 void sigproc(int sig)
162 {
163 alarms_received++;
164 }
165
166 /*
167 * cleanup() - performs all ONE TIME cleanup for this test at
168 * completion or premature exit.
169 */
cleanup(void)170 void cleanup(void)
171 {
172 }
173