blob: e93e0b27c8a74fb7c9be08499f72a37711235658 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
nstrazfa31d552002-05-14 16:50:06 +000021 * Test Name: alarm06
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 zero.
26 *
27 * Expected Result:
28 * The previously specified alarm request should be cancelled and the
29 * SIGALRM should not be received.
30 *
31 * Algorithm:
32 * Setup:
33 * Setup signal handling.
34 * Pause for SIGUSR1 if option specified.
35 *
36 * Test:
37 * Loop if the proper options are given.
38 * Execute system call
39 * Check return code, if system call failed (return=-1)
40 * Log the errno and Issue a FAIL message.
41 * Otherwise,
subrata_modakbdbaec52009-02-26 12:14:51 +000042 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000043 * if successful,
44 * Issue Functionality-Pass message.
45 * Otherwise,
46 * Issue Functionality-Fail message.
47 * Cleanup:
48 * Print errno log and/or timing stats if options given
49 *
50 * Usage: <for command-line>
nstrazfa31d552002-05-14 16:50:06 +000051 * alarm06 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
plars865695b2001-08-27 22:15:12 +000052 * where, -c n : Run n copies concurrently.
53 * -f : Turn off functionality Testing.
54 * -i n : Execute test n times.
55 * -I x : Execute test for x seconds.
56 * -P x : Pause for x seconds between iterations.
57 * -t : Turn on syscall timing.
58 *
59 * HISTORY
60 * 07/2001 Ported by Wayne Boyer
61 *
62 * RESTRICTIONS:
63 * None.
subrata_modak4bb656a2009-02-26 12:02:09 +000064 *
plars865695b2001-08-27 22:15:12 +000065 */
66
67#include <stdio.h>
68#include <unistd.h>
69#include <sys/types.h>
70#include <errno.h>
71#include <string.h>
72#include <signal.h>
73
74#include "test.h"
plars865695b2001-08-27 22:15:12 +000075
Garrett Cooper6b66d952010-12-17 20:42:35 -080076char *TCID = "alarm06";
77int TST_TOTAL = 1;
78int alarms_received = 0;
plars865695b2001-08-27 22:15:12 +000079
Garrett Cooper1e6f5a62010-12-19 09:58:10 -080080void setup();
Garrett Cooper6b66d952010-12-17 20:42:35 -080081void cleanup();
82void sigproc(int sig);
plars865695b2001-08-27 22:15:12 +000083
subrata_modak56207ce2009-03-23 13:35:39 +000084int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000085{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020086 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020087 const char *msg;
plars865695b2001-08-27 22:15:12 +000088 int time_sec1 = 10; /* time for which 1st alarm is set */
89 int time_sec2 = 0; /* time for which 2nd alarm is set */
90 int ret_val1, ret_val2; /* return values for alarm() calls */
91 int sleep_time1 = 5; /* waiting time for the 1st signal */
92 int sleep_time2 = 10; /* waiting time for the 2nd signal */
subrata_modak56207ce2009-03-23 13:35:39 +000093
Garrett Cooper53740502010-12-16 00:04:01 -080094 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080095 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000096
plars865695b2001-08-27 22:15:12 +000097 setup();
98
plars865695b2001-08-27 22:15:12 +000099 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800100
Caspar Zhangd59a6592013-03-07 14:59:12 +0800101 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000102
subrata_modak4bb656a2009-02-26 12:02:09 +0000103 /*
104 * Call First alarm() with non-zero time parameter
plars865695b2001-08-27 22:15:12 +0000105 * 'time_sec1' to send SIGALRM to the calling process.
106 */
107 TEST(alarm(time_sec1));
108 ret_val1 = TEST_RETURN;
109
plars865695b2001-08-27 22:15:12 +0000110 sleep(sleep_time1);
111
plars865695b2001-08-27 22:15:12 +0000112 TEST(alarm(time_sec2));
113 ret_val2 = TEST_RETURN;
114
115 /* Wait for signal SIGALRM */
116 sleep(sleep_time2);
117
118 /*
119 * Check whether the second alarm() call returned
120 * the amount of time (seconds) previously remaining in the
121 * alarm clock of the calling process, and
122 * sigproc() never executed as SIGALRM was not received by the
Garrett Cooper53740502010-12-16 00:04:01 -0800123 * process, the variable alarms_received remains unset.
plars865695b2001-08-27 22:15:12 +0000124 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200125 if ((alarms_received == 0) &&
126 (ret_val2 == (time_sec1 - sleep_time1)))
127 tst_resm(TPASS, "Functionality of alarm(%u) "
128 "successful", time_sec2);
129 else
130 tst_resm(TFAIL, "alarm(%u) fails, returned %d, "
131 "alarms_received:%d",
132 time_sec2, ret_val2, alarms_received);
Garrett Cooper2c282152010-12-16 00:55:50 -0800133 }
plars865695b2001-08-27 22:15:12 +0000134
plars865695b2001-08-27 22:15:12 +0000135 cleanup();
Garrett Cooper6b66d952010-12-17 20:42:35 -0800136 tst_exit();
Garrett Cooper53740502010-12-16 00:04:01 -0800137}
plars865695b2001-08-27 22:15:12 +0000138
Mike Frysingerc57fba52014-04-09 18:56:30 -0400139void setup(void)
plars865695b2001-08-27 22:15:12 +0000140{
Garrett Cooper2c282152010-12-16 00:55:50 -0800141
plars865695b2001-08-27 22:15:12 +0000142 tst_sig(NOFORK, DEF_HANDLER, cleanup);
143
plars865695b2001-08-27 22:15:12 +0000144 TEST_PAUSE;
145
146 /* Set the signal catching function */
Garrett Cooper6b66d952010-12-17 20:42:35 -0800147 if (signal(SIGALRM, sigproc) == SIG_ERR)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800148 tst_brkm(TFAIL | TERRNO, cleanup, "signal(SIGALRM, ..) failed");
plars865695b2001-08-27 22:15:12 +0000149}
150
subrata_modak56207ce2009-03-23 13:35:39 +0000151void sigproc(int sig)
plars865695b2001-08-27 22:15:12 +0000152{
Garrett Cooper53740502010-12-16 00:04:01 -0800153 alarms_received++;
plars865695b2001-08-27 22:15:12 +0000154}
155
Mike Frysingerc57fba52014-04-09 18:56:30 -0400156void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000157{
Chris Dearmanec6edca2012-10-17 19:54:01 -0700158}