blob: d692e7ee4f34305c2ae01c3496c88a4d56127f79 [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: alarm07
plars865695b2001-08-27 22:15:12 +000022 *
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,
subrata_modakbdbaec52009-02-26 12:14:51 +000041 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000042 * 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>
nstrazfa31d552002-05-14 16:50:06 +000050 * alarm07 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
plars865695b2001-08-27 22:15:12 +000051 * 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"
plars865695b2001-08-27 22:15:12 +000074
Garrett Cooper6b66d952010-12-17 20:42:35 -080075char *TCID = "alarm07";
76int TST_TOTAL = 1;
77int alarms_received = 0;
plars865695b2001-08-27 22:15:12 +000078
Garrett Cooper6b66d952010-12-17 20:42:35 -080079void setup();
80void cleanup();
81void sigproc(int sig);
plars865695b2001-08-27 22:15:12 +000082
subrata_modak56207ce2009-03-23 13:35:39 +000083int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000084{
Garrett Cooper6b66d952010-12-17 20:42:35 -080085 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020086 const char *msg;
Garrett Cooper6b66d952010-12-17 20:42:35 -080087 int sleep_time = 5;
Garrett Cooper53740502010-12-16 00:04:01 -080088 int status;
Garrett Cooper6b66d952010-12-17 20:42:35 -080089 int time_sec = 3;
90 pid_t cpid;
subrata_modak56207ce2009-03-23 13:35:39 +000091
Garrett Cooper53740502010-12-16 00:04:01 -080092 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080093 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000094
plars865695b2001-08-27 22:15:12 +000095 setup();
96
plars865695b2001-08-27 22:15:12 +000097 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -080098
Caspar Zhangd59a6592013-03-07 14:59:12 +080099 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000100
subrata_modak4bb656a2009-02-26 12:02:09 +0000101 /*
102 * Call First alarm() with non-zero time parameter
plars865695b2001-08-27 22:15:12 +0000103 * 'time_sec' to send SIGALRM to the process.
104 */
105 TEST(alarm(time_sec));
106
107 /* Now, fork a child process */
robbiewd34d5812005-07-11 22:28:09 +0000108 cpid = FORK_OR_VFORK();
plars865695b2001-08-27 22:15:12 +0000109 if (cpid < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800110 tst_resm(TFAIL | TERRNO, "fork() failed");
plars865695b2001-08-27 22:15:12 +0000111 }
112
plars865695b2001-08-27 22:15:12 +0000113 sleep(sleep_time);
114
Cyril Hrubise38b9612014-06-02 17:20:57 +0200115 if (cpid == 0) {
116 if (alarms_received == 0)
117 exit(0);
118 else {
119 printf("alarm request not cleared in "
120 "child; alarms received:%d\n",
121 alarms_received);
122 exit(1);
plars865695b2001-08-27 22:15:12 +0000123 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200124 } else {
125 /* Wait for child to complete execution */
126 if (wait(&status) == -1)
127 tst_brkm(TBROK | TERRNO, cleanup,
128 "wait failed");
129 if (!WIFEXITED(status) ||
130 WEXITSTATUS(status) != 0)
131 tst_brkm(TBROK | TERRNO, cleanup,
132 "child exited abnormally");
133 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800134 }
plars865695b2001-08-27 22:15:12 +0000135
plars865695b2001-08-27 22:15:12 +0000136 cleanup();
Garrett Cooper6b66d952010-12-17 20:42:35 -0800137 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800138}
plars865695b2001-08-27 22:15:12 +0000139
140/*
141 * setup() - performs all ONE TIME setup for this test.
142 * Setup signal handler to catch SIGALRM signal.
143 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400144void setup(void)
plars865695b2001-08-27 22:15:12 +0000145{
Garrett Cooper2c282152010-12-16 00:55:50 -0800146
plars865695b2001-08-27 22:15:12 +0000147 tst_sig(FORK, DEF_HANDLER, cleanup);
148
plars865695b2001-08-27 22:15:12 +0000149 TEST_PAUSE;
150
151 /* Set the signal catching function */
152 if (signal(SIGALRM, sigproc) == SIG_ERR) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800153 tst_brkm(TFAIL | TERRNO, cleanup, "signal(SIGALRM, ..) failed");
plars865695b2001-08-27 22:15:12 +0000154 }
155}
156
plars865695b2001-08-27 22:15:12 +0000157/*
158 * sigproc(int) - This function defines the action that has to be taken
159 * when the SIGALRM signal is caught.
160 * It also sets the variable which is used to check whether the
161 * alarm system call was successful.
162 */
subrata_modak56207ce2009-03-23 13:35:39 +0000163void sigproc(int sig)
plars865695b2001-08-27 22:15:12 +0000164{
Garrett Cooper53740502010-12-16 00:04:01 -0800165 alarms_received++;
plars865695b2001-08-27 22:15:12 +0000166}
167
168/*
169 * cleanup() - performs all ONE TIME cleanup for this test at
170 * completion or premature exit.
171 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400172void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000173{
Chris Dearmanec6edca2012-10-17 19:54:01 -0700174}