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 | /* |
| 21 | * Test Name: nanosleep03 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that nanosleep() will fail to suspend the execution |
| 25 | * of a process for a specified time if interrupted by a non-blocked signal. |
| 26 | * |
| 27 | * Expected Result: |
| 28 | * nanosleep() should return with -1 value and sets errno to EINTR. |
| 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 | * if errno set == expected errno |
| 40 | * Issue sys call fails with expected return value and errno. |
| 41 | * Otherwise, |
| 42 | * Issue sys call fails with unexpected errno. |
| 43 | * Otherwise, |
| 44 | * Issue sys call returns unexpected value. |
| 45 | * |
| 46 | * Cleanup: |
| 47 | * Print errno log and/or timing stats if options given |
| 48 | * |
| 49 | * Usage: <for command-line> |
| 50 | * nanosleep03 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 51 | * where, -c n : Run n copies concurrently. |
| 52 | * -e : Turn on errno logging. |
| 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 | #include <errno.h> |
| 65 | #include <unistd.h> |
| 66 | #include <fcntl.h> |
robbiew | 01be333 | 2003-03-26 23:48:41 +0000 | [diff] [blame] | 67 | #include <time.h> |
| 68 | #include <wait.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | |
| 70 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 72 | char *TCID = "nanosleep03"; |
| 73 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | |
Cyril Hrubis | 605fa33 | 2015-02-04 13:11:20 +0100 | [diff] [blame] | 75 | struct timespec timereq; |
| 76 | struct timespec timerem; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 78 | void do_child(); /* Child process */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | void setup(); /* Main setup function of test */ |
| 80 | void cleanup(); /* cleanup function for the test */ |
| 81 | void sig_handler(); /* signal catching function */ |
| 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 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 85 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 86 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | pid_t cpid; /* Child process id */ |
| 88 | int status; /* child exit status */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 89 | |
Garrett Cooper | a9e49f1 | 2010-12-16 10:54:03 -0800 | [diff] [blame] | 90 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 91 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 92 | #ifdef UCLINUX |
| 93 | maybe_run_child(&do_child, "dddd", &timereq.tv_sec, &timereq.tv_nsec, |
| 94 | &timerem.tv_sec, &timerem.tv_nsec); |
| 95 | #endif |
| 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 | |
| 103 | /* |
| 104 | * Creat a child process and suspend its |
| 105 | * execution using nanosleep() |
| 106 | */ |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 107 | if ((cpid = FORK_OR_VFORK()) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | tst_brkm(TBROK, cleanup, "fork() failed"); |
| 109 | } |
| 110 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 111 | if (cpid == 0) { /* Child process */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 112 | #ifdef UCLINUX |
| 113 | if (self_exec(av[0], "dddd", |
| 114 | timereq.tv_sec, timereq.tv_nsec, |
| 115 | timerem.tv_sec, timerem.tv_nsec) < 0) { |
| 116 | tst_brkm(TBROK, cleanup, "self_exec failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 118 | #else |
| 119 | do_child(); |
| 120 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* wait for child to time slot for execution */ |
| 124 | sleep(1); |
| 125 | |
| 126 | /* Now send signal to child */ |
| 127 | if (kill(cpid, SIGINT) < 0) { |
| 128 | tst_brkm(TBROK, cleanup, |
| 129 | "kill() fails send signal to child"); |
| 130 | } |
| 131 | |
| 132 | /* Wait for child to execute */ |
| 133 | wait(&status); |
yaberauneya | f00ed7a | 2009-12-22 17:35:13 +0000 | [diff] [blame] | 134 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
| 135 | tst_resm(TPASS, "nanosleep() failed, interrupted" |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 136 | " by signal (%d) as expected", EINTR); |
yaberauneya | f00ed7a | 2009-12-22 17:35:13 +0000 | [diff] [blame] | 137 | } else { |
| 138 | tst_resm(TFAIL, "child process exited abnormally; " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 139 | "status = %d", status); |
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 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 144 | tst_exit(); |
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 | |
| 148 | /* |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 149 | * do_child() |
| 150 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 151 | void do_child(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 152 | { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 153 | /* |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 154 | * Call nanosleep() to suspend child process |
| 155 | * for specified time 'tv_sec'. |
| 156 | * Call should return before suspending execution |
| 157 | * for the specified time due to receipt of signal |
| 158 | * from Parent. |
| 159 | */ |
| 160 | TEST(nanosleep(&timereq, &timerem)); |
| 161 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 162 | if (TEST_RETURN == -1) { |
| 163 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 164 | /* Check for expected errno is set */ |
| 165 | if (TEST_ERRNO != EINTR) { |
yaberauneya | f00ed7a | 2009-12-22 17:35:13 +0000 | [diff] [blame] | 166 | tst_resm(TFAIL | TTERRNO, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 167 | "nanosleep() failed; expected errno: %d", |
| 168 | EINTR); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 169 | exit(1); |
| 170 | } |
| 171 | } else { |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 172 | tst_resm(TFAIL, "nanosleep() returns %ld, " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 173 | "expected -1, errno:%d", TEST_RETURN, EINTR); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 174 | exit(1); |
| 175 | } |
| 176 | |
| 177 | /* Everything is fine, exit normally */ |
| 178 | exit(0); |
| 179 | } |
| 180 | |
| 181 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | * setup() - performs all ONE TIME setup for this test. |
| 183 | * Setup signal handler to catch the interrupt signal sent by parent |
| 184 | * to child process. |
| 185 | * Initialise time structure elements. |
| 186 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 187 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 188 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 189 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 190 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 191 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 192 | TEST_PAUSE; |
| 193 | |
| 194 | /* Setup signal handler */ |
| 195 | if (signal(SIGINT, sig_handler) == SIG_ERR) { |
| 196 | tst_brkm(TBROK, cleanup, |
| 197 | "signal() fails to setup signal handler"); |
| 198 | } |
| 199 | |
| 200 | /* Initialise time variables which used to suspend child execution */ |
| 201 | timereq.tv_sec = 5; |
| 202 | timereq.tv_nsec = 9999; |
| 203 | |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * sig_handler() - signal catching function. |
| 208 | * This function gets executed when a parnet sends a signal 'SIGINT' |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 209 | * to child to awake it from sleep. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 210 | * This function just returns without doing anything. |
| 211 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 212 | void sig_handler(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 213 | { |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 218 | * completion or premature exit. |
| 219 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 220 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 221 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 222 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 223 | } |