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 |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 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" |
| 77 | #include "usctest.h" |
| 78 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 79 | char *TCID = "alarm05"; /* Test program identifier. */ |
| 80 | int TST_TOTAL = 1; /* Total number of test cases. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | extern int Tst_count; /* Test Case counter for tst_* routines */ |
| 82 | int almreceived = 0; /* flag to indicate SIGALRM received or not */ |
| 83 | |
| 84 | void setup(); /* Main setup function of test */ |
| 85 | void cleanup(); /* cleanup function for the test */ |
| 86 | void sigproc(int sig); /* signal catching function */ |
| 87 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 88 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | { |
| 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 */ |
robbiew | b1dc305 | 2005-06-07 14:11:44 +0000 | [diff] [blame] | 95 | int ret_val3; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | int sleep_time1 = 3; /* waiting time for the signal */ |
| 97 | int sleep_time2 = 6; /* waiting time for the signal */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 98 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | /* Parse standard options given to run the test. */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 100 | msg = parse_opts(ac, av, (option_t *) NULL, NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | if (msg != (char *)NULL) { |
| 102 | tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg); |
| 103 | } |
| 104 | |
| 105 | /* Perform global setup for test */ |
| 106 | setup(); |
| 107 | |
| 108 | /* Check looping state if -i option given */ |
| 109 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 110 | /* Reset Tst_count in case we are looping. */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 111 | Tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | |
mreed10 | d709235 | 2006-07-10 15:50:38 +0000 | [diff] [blame] | 113 | /* Reset almreceived for every iteration, since it has |
| 114 | * old values from previous iterations (if any) and not |
| 115 | * a value of zero */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 116 | almreceived = 0; |
mreed10 | d709235 | 2006-07-10 15:50:38 +0000 | [diff] [blame] | 117 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 118 | /* |
| 119 | * Call First alarm() with non-zero time parameter |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | * 'time_sec1' to send SIGALRM to the calling process. |
| 121 | */ |
| 122 | TEST(alarm(time_sec1)); |
| 123 | ret_val1 = TEST_RETURN; |
| 124 | |
| 125 | /* Wait for signal SIGALARM */ |
| 126 | sleep(sleep_time1); |
| 127 | |
| 128 | /* |
| 129 | * Call Second alarm() with non-zero time parameter |
| 130 | * 'time_sec2' to send SIGALRM to the calling process. |
| 131 | */ |
| 132 | TEST(alarm(time_sec2)); |
| 133 | ret_val2 = TEST_RETURN; |
| 134 | |
| 135 | /* Wait for signal SIGALRM */ |
| 136 | sleep(sleep_time2); |
| 137 | |
| 138 | /* |
| 139 | * Check whether the second alarm() call returned |
| 140 | * the amount of time previously remaining in the |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 141 | * alarm clock of the calling process, and |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | * sigproc() executed when SIGALRM received by the |
| 143 | * process, the variable almreceived is set. |
| 144 | */ |
| 145 | if (STD_FUNCTIONAL_TEST) { |
| 146 | if ((almreceived == 1) && |
| 147 | (ret_val2 == (time_sec1 - sleep_time1))) { |
robbiew | b1dc305 | 2005-06-07 14:11:44 +0000 | [diff] [blame] | 148 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 149 | /* |
| 150 | * Make sure the system cleaned up the alarm |
robbiew | b1dc305 | 2005-06-07 14:11:44 +0000 | [diff] [blame] | 151 | * after it delivered it. |
| 152 | */ |
| 153 | TEST(alarm(0)); |
| 154 | ret_val3 = TEST_RETURN; |
| 155 | |
| 156 | if (ret_val3 != 0) { |
| 157 | tst_resm(TFAIL, "System did not " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 158 | "clean up delivered " "alarm"); |
robbiew | b1dc305 | 2005-06-07 14:11:44 +0000 | [diff] [blame] | 159 | } else { |
| 160 | tst_resm(TPASS, "Functionality of " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 161 | "alarm(%u) successful", |
| 162 | time_sec2); |
robbiew | b1dc305 | 2005-06-07 14:11:44 +0000 | [diff] [blame] | 163 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | } else { |
| 165 | tst_resm(TFAIL, "alarm(%u) fails, returned %d, " |
| 166 | "almreceived:%d", |
| 167 | time_sec2, ret_val2, almreceived); |
| 168 | } |
| 169 | } else { |
| 170 | tst_resm(TPASS, "call succeeded"); |
| 171 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 172 | } /* End for TEST_LOOPING */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | |
| 174 | /* Call cleanup() to undo setup done for the test. */ |
| 175 | cleanup(); |
| 176 | |
robbiew | 2c94524 | 2002-11-11 19:01:25 +0000 | [diff] [blame] | 177 | return 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 178 | /*NOTREACHED*/} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 179 | |
| 180 | /* |
| 181 | * setup() - performs all ONE TIME setup for this test. |
| 182 | * Setup the signal handler to catch SIGALRM. |
| 183 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 184 | void setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 185 | { |
| 186 | /* capture signals */ |
| 187 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 188 | |
| 189 | /* Pause if that option was specified */ |
| 190 | TEST_PAUSE; |
| 191 | |
| 192 | /* Set the signal catching function */ |
| 193 | if (signal(SIGALRM, sigproc) == SIG_ERR) { |
| 194 | tst_brkm(TFAIL, cleanup, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 195 | "signal() fails to catch SIGALARM, errno=%d", errno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 199 | /* |
| 200 | * sigproc(int) - This function defines the action that has to be taken |
| 201 | * when the SIGALRM signal is caught. |
| 202 | * It also sets the variable which is used to check whether the |
| 203 | * alarm system call was successful. |
| 204 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 205 | void sigproc(int sig) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 206 | { |
| 207 | almreceived = almreceived + 1; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * void |
| 212 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 213 | * completion or premature exit. |
| 214 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame^] | 215 | void cleanup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 216 | { |
| 217 | /* |
| 218 | * print timing stats if that option was specified. |
| 219 | * print errno log if that option was specified. |
| 220 | */ |
| 221 | TEST_CLEANUP; |
| 222 | |
| 223 | /* exit with return code appropriate for results */ |
| 224 | tst_exit(); |
| 225 | } |