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: alarm07 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 22 | * |
| 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_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 41 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | * 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> |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 50 | * alarm07 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | * 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" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | |
Garrett Cooper | 6b66d95 | 2010-12-17 20:42:35 -0800 | [diff] [blame] | 75 | char *TCID = "alarm07"; |
| 76 | int TST_TOTAL = 1; |
| 77 | int alarms_received = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
Garrett Cooper | 6b66d95 | 2010-12-17 20:42:35 -0800 | [diff] [blame] | 79 | void setup(); |
| 80 | void cleanup(); |
| 81 | void sigproc(int sig); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 83 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | { |
Garrett Cooper | 6b66d95 | 2010-12-17 20:42:35 -0800 | [diff] [blame] | 85 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 86 | const char *msg; |
Garrett Cooper | 6b66d95 | 2010-12-17 20:42:35 -0800 | [diff] [blame] | 87 | int sleep_time = 5; |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 88 | int status; |
Garrett Cooper | 6b66d95 | 2010-12-17 20:42:35 -0800 | [diff] [blame] | 89 | int time_sec = 3; |
| 90 | pid_t cpid; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 91 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 92 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 93 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | setup(); |
| 96 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 98 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 99 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 101 | /* |
| 102 | * Call First alarm() with non-zero time parameter |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | * 'time_sec' to send SIGALRM to the process. |
| 104 | */ |
| 105 | TEST(alarm(time_sec)); |
| 106 | |
| 107 | /* Now, fork a child process */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 108 | cpid = FORK_OR_VFORK(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | if (cpid < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 110 | tst_resm(TFAIL | TERRNO, "fork() failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | } |
| 112 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | sleep(sleep_time); |
| 114 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 115 | 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); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 124 | } 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 Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 134 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | cleanup(); |
Garrett Cooper | 6b66d95 | 2010-12-17 20:42:35 -0800 | [diff] [blame] | 137 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 138 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * setup() - performs all ONE TIME setup for this test. |
| 142 | * Setup signal handler to catch SIGALRM signal. |
| 143 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 144 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 146 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 148 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | TEST_PAUSE; |
| 150 | |
| 151 | /* Set the signal catching function */ |
| 152 | if (signal(SIGALRM, sigproc) == SIG_ERR) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 153 | tst_brkm(TFAIL | TERRNO, cleanup, "signal(SIGALRM, ..) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | /* |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 163 | void sigproc(int sig) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 165 | alarms_received++; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* |
| 169 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 170 | * completion or premature exit. |
| 171 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 172 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | { |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 174 | } |