1 /*
2 *
3 * Copyright (c) International Business Machines Corp., 2001,2005
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: alarm05
22 *
23 * Test Description:
24 * Check the functionality of the Alarm system call when the time input
25 * parameter is non zero.
26 *
27 * Expected Result:
28 * The return value of the alarm system call should be equal to the
29 * amount previously remaining in the alarm clock.
30 * A SIGALRM signal should be received after the specified amount of
31 * time has elapsed.
32 *
33 * Algorithm:
34 * Setup:
35 * Setup signal handling.
36 * Pause for SIGUSR1 if option specified.
37 *
38 * Test:
39 * Loop if the proper options are given.
40 * Execute system call
41 * Check return code, if system call failed (return=-1)
42 * Log the errno and Issue a FAIL message.
43 * Otherwise,
44 * Verify the Functionality of system call
45 * if successful,
46 * Issue Functionality-Pass message.
47 * Otherwise,
48 * Issue Functionality-Fail message.
49 * Cleanup:
50 * Print errno log and/or timing stats if options given
51 *
52 * Usage: <for command-line>
53 * alarm05 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
54 * where, -c n : Run n copies concurrently.
55 * -f : Turn off functionality Testing.
56 * -i n : Execute test n times.
57 * -I x : Execute test for x seconds.
58 * -P x : Pause for x seconds between iterations.
59 * -t : Turn on syscall timing.
60 *
61 * HISTORY
62 * 06/2005 Test for alarm cleanup by Amos Waterland
63 * 07/2001 Ported by Wayne Boyer
64 *
65 * RESTRICTIONS:
66 * None.
67 */
68
69 #include <stdio.h>
70 #include <unistd.h>
71 #include <sys/types.h>
72 #include <errno.h>
73 #include <string.h>
74 #include <signal.h>
75
76 #include "test.h"
77
78 char *TCID = "alarm05";
79 int TST_TOTAL = 1;
80 int alarms_received = 0; /* flag to indicate SIGALRM received or not */
81
82 void setup(); /* Main setup function of test */
83 void cleanup(); /* cleanup function for the test */
84 void sigproc(int sig); /* signal catching function */
85
main(int ac,char ** av)86 int main(int ac, char **av)
87 {
88 int lc;
89 int time_sec1 = 10; /* time for which 1st alarm is set */
90 int time_sec2 = 5; /* time for which 2st alarm is set */
91 int ret_val1, ret_val2; /* return values for alarm() calls */
92 int ret_val3;
93 int sleep_time1 = 3; /* waiting time for the signal */
94 int sleep_time2 = 6; /* waiting time for the 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 * Reset alarms_received for every iteration, since it has
106 * old values from previous iterations (if any) and not
107 * a value of zero
108 */
109 alarms_received = 0;
110
111 /*
112 * Call First alarm() with non-zero time parameter
113 * 'time_sec1' to send SIGALRM to the calling process.
114 */
115 TEST(alarm(time_sec1));
116 ret_val1 = TEST_RETURN;
117
118 /* Wait for signal SIGALARM */
119 sleep(sleep_time1);
120
121 /*
122 * Call Second alarm() with non-zero time parameter
123 * 'time_sec2' to send SIGALRM to the calling process.
124 */
125 TEST(alarm(time_sec2));
126 ret_val2 = TEST_RETURN;
127
128 /* Wait for signal SIGALRM */
129 sleep(sleep_time2);
130
131 /*
132 * Check whether the second alarm() call returned
133 * the amount of time previously remaining in the
134 * alarm clock of the calling process, and
135 * sigproc() executed when SIGALRM received by the
136 * process, the variable alarms_received is set.
137 */
138 if ((alarms_received == 1) &&
139 (ret_val2 == (time_sec1 - sleep_time1))) {
140
141 /*
142 * Make sure the system cleaned up the alarm
143 * after it delivered it.
144 */
145 TEST(alarm(0));
146 ret_val3 = TEST_RETURN;
147
148 if (ret_val3 != 0)
149 tst_resm(TFAIL, "System did not "
150 "clean up delivered " "alarm");
151 else {
152 tst_resm(TPASS, "Functionality of "
153 "alarm(%u) successful",
154 time_sec2);
155 }
156 } else {
157 tst_resm(TFAIL, "alarm(%u) fails, returned %d, "
158 "alarms_received:%d",
159 time_sec2, ret_val2, alarms_received);
160 }
161 }
162
163 cleanup();
164 tst_exit();
165 }
166
167 /*
168 * setup() - performs all ONE TIME setup for this test.
169 * Setup the signal handler to catch SIGALRM.
170 */
setup(void)171 void setup(void)
172 {
173
174 tst_sig(NOFORK, DEF_HANDLER, cleanup);
175
176 TEST_PAUSE;
177
178 /* Set the signal catching function */
179 if (signal(SIGALRM, sigproc) == SIG_ERR) {
180 tst_brkm(TFAIL, cleanup,
181 "signal() fails to catch SIGALARM, errno=%d", errno);
182 }
183 }
184
185 /*
186 * sigproc(int) - This function defines the action that has to be taken
187 * when the SIGALRM signal is caught.
188 * It also sets the variable which is used to check whether the
189 * alarm system call was successful.
190 */
sigproc(int sig)191 void sigproc(int sig)
192 {
193 alarms_received++;
194 }
195
196 /*
197 * void
198 * cleanup() - performs all ONE TIME cleanup for this test at
199 * completion or premature exit.
200 */
cleanup(void)201 void cleanup(void)
202 {
203 }
204