plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
robbiew | b1dc305 | 2005-06-07 14:11:44 +0000 | [diff] [blame] | 3 | * Copyright (c) International Business Machines Corp., 2001,2005 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
nstraz | 6a10c82 | 2001-11-13 18:06:36 +0000 | [diff] [blame] | 21 | * Test Name: alarm05 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 28 | * The return value of the alarm system call should be equal to the |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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, |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 44 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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> |
nstraz | 6a10c82 | 2001-11-13 18:06:36 +0000 | [diff] [blame] | 53 | * alarm05 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
robbiew | b1dc305 | 2005-06-07 14:11:44 +0000 | [diff] [blame] | 62 | * 06/2005 Test for alarm cleanup by Amos Waterland |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 78 | char *TCID = "alarm05"; |
| 79 | int TST_TOTAL = 1; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 80 | int alarms_received = 0; /* flag to indicate SIGALRM received or not */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 86 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 88 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 89 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | int time_sec1 = 10; /* time for which 1st alarm is set */ |
| 91 | int time_sec2 = 5; /* time for which 2st alarm is set */ |
| 92 | int ret_val1, ret_val2; /* return values for alarm() calls */ |
robbiew | b1dc305 | 2005-06-07 14:11:44 +0000 | [diff] [blame] | 93 | int ret_val3; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | int sleep_time1 = 3; /* waiting time for the signal */ |
| 95 | int sleep_time2 = 6; /* waiting time for the signal */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 96 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 97 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 98 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | setup(); |
| 101 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 103 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 104 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 106 | /* |
Garrett Cooper | 6b66d95 | 2010-12-17 20:42:35 -0800 | [diff] [blame] | 107 | * Reset alarms_received for every iteration, since it has |
mreed10 | d709235 | 2006-07-10 15:50:38 +0000 | [diff] [blame] | 108 | * old values from previous iterations (if any) and not |
Garrett Cooper | 6b66d95 | 2010-12-17 20:42:35 -0800 | [diff] [blame] | 109 | * a value of zero |
| 110 | */ |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 111 | alarms_received = 0; |
mreed10 | d709235 | 2006-07-10 15:50:38 +0000 | [diff] [blame] | 112 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 113 | /* |
| 114 | * Call First alarm() with non-zero time parameter |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | * 'time_sec1' to send SIGALRM to the calling process. |
| 116 | */ |
| 117 | TEST(alarm(time_sec1)); |
| 118 | ret_val1 = TEST_RETURN; |
| 119 | |
| 120 | /* Wait for signal SIGALARM */ |
| 121 | sleep(sleep_time1); |
| 122 | |
| 123 | /* |
| 124 | * Call Second alarm() with non-zero time parameter |
| 125 | * 'time_sec2' to send SIGALRM to the calling process. |
| 126 | */ |
| 127 | TEST(alarm(time_sec2)); |
| 128 | ret_val2 = TEST_RETURN; |
| 129 | |
| 130 | /* Wait for signal SIGALRM */ |
| 131 | sleep(sleep_time2); |
| 132 | |
| 133 | /* |
| 134 | * Check whether the second alarm() call returned |
| 135 | * the amount of time previously remaining in the |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 136 | * alarm clock of the calling process, and |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | * sigproc() executed when SIGALRM received by the |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 138 | * process, the variable alarms_received is set. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 140 | if ((alarms_received == 1) && |
| 141 | (ret_val2 == (time_sec1 - sleep_time1))) { |
robbiew | b1dc305 | 2005-06-07 14:11:44 +0000 | [diff] [blame] | 142 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 143 | /* |
| 144 | * Make sure the system cleaned up the alarm |
| 145 | * after it delivered it. |
| 146 | */ |
| 147 | TEST(alarm(0)); |
| 148 | ret_val3 = TEST_RETURN; |
robbiew | b1dc305 | 2005-06-07 14:11:44 +0000 | [diff] [blame] | 149 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 150 | if (ret_val3 != 0) |
| 151 | tst_resm(TFAIL, "System did not " |
| 152 | "clean up delivered " "alarm"); |
| 153 | else { |
| 154 | tst_resm(TPASS, "Functionality of " |
| 155 | "alarm(%u) successful", |
| 156 | time_sec2); |
| 157 | } |
| 158 | } else { |
| 159 | tst_resm(TFAIL, "alarm(%u) fails, returned %d, " |
| 160 | "alarms_received:%d", |
| 161 | time_sec2, ret_val2, alarms_received); |
| 162 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 163 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 165 | cleanup(); |
Garrett Cooper | 6b66d95 | 2010-12-17 20:42:35 -0800 | [diff] [blame] | 166 | tst_exit(); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 167 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 168 | |
| 169 | /* |
| 170 | * setup() - performs all ONE TIME setup for this test. |
| 171 | * Setup the signal handler to catch SIGALRM. |
| 172 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 173 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 174 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 175 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 177 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 178 | TEST_PAUSE; |
| 179 | |
| 180 | /* Set the signal catching function */ |
| 181 | if (signal(SIGALRM, sigproc) == SIG_ERR) { |
| 182 | tst_brkm(TFAIL, cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 183 | "signal() fails to catch SIGALARM, errno=%d", errno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 187 | /* |
| 188 | * sigproc(int) - This function defines the action that has to be taken |
| 189 | * when the SIGALRM signal is caught. |
| 190 | * It also sets the variable which is used to check whether the |
| 191 | * alarm system call was successful. |
| 192 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 193 | void sigproc(int sig) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 194 | { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 195 | alarms_received++; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* |
| 199 | * void |
| 200 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 201 | * completion or premature exit. |
| 202 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 203 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | { |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 205 | } |