plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
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 | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 21 | * Test Name: alarm06 |
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 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_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 42 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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> |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 51 | * alarm06 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 64 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
Garrett Cooper | 6b66d95 | 2010-12-17 20:42:35 -0800 | [diff] [blame] | 76 | char *TCID = "alarm06"; |
| 77 | int TST_TOTAL = 1; |
| 78 | int alarms_received = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 80 | void setup(); |
Garrett Cooper | 6b66d95 | 2010-12-17 20:42:35 -0800 | [diff] [blame] | 81 | void cleanup(); |
| 82 | void sigproc(int sig); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 84 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 86 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 87 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 93 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 94 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 95 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | setup(); |
| 98 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 100 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 101 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 103 | /* |
| 104 | * Call First alarm() with non-zero time parameter |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | * 'time_sec1' to send SIGALRM to the calling process. |
| 106 | */ |
| 107 | TEST(alarm(time_sec1)); |
| 108 | ret_val1 = TEST_RETURN; |
| 109 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | sleep(sleep_time1); |
| 111 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | 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 Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 123 | * process, the variable alarms_received remains unset. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 125 | 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 Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 133 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | cleanup(); |
Garrett Cooper | 6b66d95 | 2010-12-17 20:42:35 -0800 | [diff] [blame] | 136 | tst_exit(); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 137 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 139 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 141 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 143 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | TEST_PAUSE; |
| 145 | |
| 146 | /* Set the signal catching function */ |
Garrett Cooper | 6b66d95 | 2010-12-17 20:42:35 -0800 | [diff] [blame] | 147 | if (signal(SIGALRM, sigproc) == SIG_ERR) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 148 | tst_brkm(TFAIL | TERRNO, cleanup, "signal(SIGALRM, ..) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | } |
| 150 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 151 | void sigproc(int sig) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 153 | alarms_received++; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 156 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | { |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 158 | } |