blob: 6a479687b50c540ba6dbdd4ce433023024e146c8 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
robbiewb1dc3052005-06-07 14:11:44 +00003 * Copyright (c) International Business Machines Corp., 2001,2005
plars865695b2001-08-27 22:15:12 +00004 *
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/*
nstraz6a10c822001-11-13 18:06:36 +000021 * Test Name: alarm05
plars865695b2001-08-27 22:15:12 +000022 *
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:
subrata_modak4bb656a2009-02-26 12:02:09 +000028 * The return value of the alarm system call should be equal to the
plars865695b2001-08-27 22:15:12 +000029 * 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,
subrata_modakbdbaec52009-02-26 12:14:51 +000044 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000045 * 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>
nstraz6a10c822001-11-13 18:06:36 +000053 * alarm05 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
plars865695b2001-08-27 22:15:12 +000054 * 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
robbiewb1dc3052005-06-07 14:11:44 +000062 * 06/2005 Test for alarm cleanup by Amos Waterland
plars865695b2001-08-27 22:15:12 +000063 * 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#include "usctest.h"
78
subrata_modak56207ce2009-03-23 13:35:39 +000079char *TCID = "alarm05"; /* Test program identifier. */
80int TST_TOTAL = 1; /* Total number of test cases. */
plars865695b2001-08-27 22:15:12 +000081extern int Tst_count; /* Test Case counter for tst_* routines */
Garrett Cooper53740502010-12-16 00:04:01 -080082int alarms_received = 0; /* flag to indicate SIGALRM received or not */
plars865695b2001-08-27 22:15:12 +000083
84void setup(); /* Main setup function of test */
85void cleanup(); /* cleanup function for the test */
86void sigproc(int sig); /* signal catching function */
87
subrata_modak56207ce2009-03-23 13:35:39 +000088int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000089{
90 int lc; /* loop counter */
91 char *msg; /* message returned from parse_opts */
92 int time_sec1 = 10; /* time for which 1st alarm is set */
93 int time_sec2 = 5; /* time for which 2st alarm is set */
94 int ret_val1, ret_val2; /* return values for alarm() calls */
robbiewb1dc3052005-06-07 14:11:44 +000095 int ret_val3;
plars865695b2001-08-27 22:15:12 +000096 int sleep_time1 = 3; /* waiting time for the signal */
97 int sleep_time2 = 6; /* waiting time for the signal */
subrata_modak56207ce2009-03-23 13:35:39 +000098
plars865695b2001-08-27 22:15:12 +000099 /* Parse standard options given to run the test. */
Garrett Cooper53740502010-12-16 00:04:01 -0800100 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800101 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000102
plars865695b2001-08-27 22:15:12 +0000103 setup();
104
plars865695b2001-08-27 22:15:12 +0000105 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800106
subrata_modak56207ce2009-03-23 13:35:39 +0000107 Tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000108
Garrett Cooper53740502010-12-16 00:04:01 -0800109 /* Reset alarms_received for every iteration, since it has
mreed10d7092352006-07-10 15:50:38 +0000110 * old values from previous iterations (if any) and not
111 * a value of zero */
Garrett Cooper53740502010-12-16 00:04:01 -0800112 alarms_received = 0;
mreed10d7092352006-07-10 15:50:38 +0000113
subrata_modak4bb656a2009-02-26 12:02:09 +0000114 /*
115 * Call First alarm() with non-zero time parameter
plars865695b2001-08-27 22:15:12 +0000116 * 'time_sec1' to send SIGALRM to the calling process.
117 */
118 TEST(alarm(time_sec1));
119 ret_val1 = TEST_RETURN;
120
121 /* Wait for signal SIGALARM */
122 sleep(sleep_time1);
123
124 /*
125 * Call Second alarm() with non-zero time parameter
126 * 'time_sec2' to send SIGALRM to the calling process.
127 */
128 TEST(alarm(time_sec2));
129 ret_val2 = TEST_RETURN;
130
131 /* Wait for signal SIGALRM */
132 sleep(sleep_time2);
133
134 /*
135 * Check whether the second alarm() call returned
136 * the amount of time previously remaining in the
subrata_modak4bb656a2009-02-26 12:02:09 +0000137 * alarm clock of the calling process, and
plars865695b2001-08-27 22:15:12 +0000138 * sigproc() executed when SIGALRM received by the
Garrett Cooper53740502010-12-16 00:04:01 -0800139 * process, the variable alarms_received is set.
plars865695b2001-08-27 22:15:12 +0000140 */
141 if (STD_FUNCTIONAL_TEST) {
Garrett Cooper53740502010-12-16 00:04:01 -0800142 if ((alarms_received == 1) &&
plars865695b2001-08-27 22:15:12 +0000143 (ret_val2 == (time_sec1 - sleep_time1))) {
robbiewb1dc3052005-06-07 14:11:44 +0000144
subrata_modak4bb656a2009-02-26 12:02:09 +0000145 /*
146 * Make sure the system cleaned up the alarm
robbiewb1dc3052005-06-07 14:11:44 +0000147 * after it delivered it.
148 */
149 TEST(alarm(0));
150 ret_val3 = TEST_RETURN;
151
152 if (ret_val3 != 0) {
153 tst_resm(TFAIL, "System did not "
subrata_modak56207ce2009-03-23 13:35:39 +0000154 "clean up delivered " "alarm");
robbiewb1dc3052005-06-07 14:11:44 +0000155 } else {
156 tst_resm(TPASS, "Functionality of "
subrata_modak56207ce2009-03-23 13:35:39 +0000157 "alarm(%u) successful",
158 time_sec2);
robbiewb1dc3052005-06-07 14:11:44 +0000159 }
plars865695b2001-08-27 22:15:12 +0000160 } else {
161 tst_resm(TFAIL, "alarm(%u) fails, returned %d, "
Garrett Cooper53740502010-12-16 00:04:01 -0800162 "alarms_received:%d",
163 time_sec2, ret_val2, alarms_received);
plars865695b2001-08-27 22:15:12 +0000164 }
165 } else {
166 tst_resm(TPASS, "call succeeded");
167 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800168 }
plars865695b2001-08-27 22:15:12 +0000169
plars865695b2001-08-27 22:15:12 +0000170 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800171
Garrett Cooper53740502010-12-16 00:04:01 -0800172}
plars865695b2001-08-27 22:15:12 +0000173
174/*
175 * setup() - performs all ONE TIME setup for this test.
176 * Setup the signal handler to catch SIGALRM.
177 */
subrata_modak56207ce2009-03-23 13:35:39 +0000178void setup()
plars865695b2001-08-27 22:15:12 +0000179{
Garrett Cooper2c282152010-12-16 00:55:50 -0800180
plars865695b2001-08-27 22:15:12 +0000181 tst_sig(NOFORK, DEF_HANDLER, cleanup);
182
plars865695b2001-08-27 22:15:12 +0000183 TEST_PAUSE;
184
185 /* Set the signal catching function */
186 if (signal(SIGALRM, sigproc) == SIG_ERR) {
187 tst_brkm(TFAIL, cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000188 "signal() fails to catch SIGALARM, errno=%d", errno);
plars865695b2001-08-27 22:15:12 +0000189 }
190}
191
plars865695b2001-08-27 22:15:12 +0000192/*
193 * sigproc(int) - This function defines the action that has to be taken
194 * when the SIGALRM signal is caught.
195 * It also sets the variable which is used to check whether the
196 * alarm system call was successful.
197 */
subrata_modak56207ce2009-03-23 13:35:39 +0000198void sigproc(int sig)
plars865695b2001-08-27 22:15:12 +0000199{
Garrett Cooper53740502010-12-16 00:04:01 -0800200 alarms_received++;
plars865695b2001-08-27 22:15:12 +0000201}
202
203/*
204 * void
205 * cleanup() - performs all ONE TIME cleanup for this test at
206 * completion or premature exit.
207 */
subrata_modak56207ce2009-03-23 13:35:39 +0000208void cleanup()
plars865695b2001-08-27 22:15:12 +0000209{
210 /*
211 * print timing stats if that option was specified.
212 * print errno log if that option was specified.
213 */
214 TEST_CLEANUP;
Garrett Cooper2c282152010-12-16 00:55:50 -0800215}