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: alarm06
22  *
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,
42  *   	Verify the Functionality of system call
43  *      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>
51  *  alarm06 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
52  *     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.
64  *
65  */
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"
75 
76 char *TCID = "alarm06";
77 int TST_TOTAL = 1;
78 int alarms_received = 0;
79 
80 void setup();
81 void cleanup();
82 void sigproc(int sig);
83 
main(int ac,char ** av)84 int main(int ac, char **av)
85 {
86 	int lc;
87 	int time_sec1 = 10;	/* time for which 1st alarm is set */
88 	int time_sec2 = 0;	/* time for which 2nd alarm is set */
89 	int ret_val1, ret_val2;	/* return values for alarm() calls */
90 	int sleep_time1 = 5;	/* waiting time for the 1st signal */
91 	int sleep_time2 = 10;	/* waiting time for the 2nd signal */
92 
93 	tst_parse_opts(ac, av, NULL, NULL);
94 
95 	setup();
96 
97 	for (lc = 0; TEST_LOOPING(lc); lc++) {
98 
99 		tst_count = 0;
100 
101 		/*
102 		 * Call First alarm() with non-zero time parameter
103 		 * 'time_sec1' to send SIGALRM to the calling process.
104 		 */
105 		TEST(alarm(time_sec1));
106 		ret_val1 = TEST_RETURN;
107 
108 		sleep(sleep_time1);
109 
110 		TEST(alarm(time_sec2));
111 		ret_val2 = TEST_RETURN;
112 
113 		/* Wait for signal SIGALRM */
114 		sleep(sleep_time2);
115 
116 		/*
117 		 * Check whether the second alarm() call returned
118 		 * the amount of time (seconds) previously remaining in the
119 		 * alarm clock of the calling process, and
120 		 * sigproc() never executed as SIGALRM was not received by the
121 		 * process, the variable alarms_received remains unset.
122 		 */
123 		if ((alarms_received == 0) &&
124 		    (ret_val2 == (time_sec1 - sleep_time1)))
125 			tst_resm(TPASS, "Functionality of alarm(%u) "
126 				 "successful", time_sec2);
127 		else
128 			tst_resm(TFAIL, "alarm(%u) fails, returned %d, "
129 				 "alarms_received:%d",
130 				 time_sec2, ret_val2, alarms_received);
131 	}
132 
133 	cleanup();
134 	tst_exit();
135 }
136 
setup(void)137 void setup(void)
138 {
139 
140 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
141 
142 	TEST_PAUSE;
143 
144 	/* Set the signal catching function */
145 	if (signal(SIGALRM, sigproc) == SIG_ERR)
146 		tst_brkm(TFAIL | TERRNO, cleanup, "signal(SIGALRM, ..) failed");
147 }
148 
sigproc(int sig)149 void sigproc(int sig)
150 {
151 	alarms_received++;
152 }
153 
cleanup(void)154 void cleanup(void)
155 {
156 }
157