plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 400b622 | 2015-02-19 12:59:27 +0100 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
| 3 | * 07/2001 Ported by Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 4 | * |
Cyril Hrubis | 400b622 | 2015-02-19 12:59:27 +0100 | [diff] [blame] | 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 9 | * |
Cyril Hrubis | 400b622 | 2015-02-19 12:59:27 +0100 | [diff] [blame] | 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 14 | * |
Cyril Hrubis | 400b622 | 2015-02-19 12:59:27 +0100 | [diff] [blame] | 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 Foundation, |
| 17 | * 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 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 21 | * Test Description: |
| 22 | * Verify that nanosleep() will fail to suspend the execution |
| 23 | * of a process for a specified time if interrupted by a non-blocked signal. |
| 24 | * |
| 25 | * Expected Result: |
| 26 | * nanosleep() should return with -1 value and sets errno to EINTR. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 27 | */ |
Cyril Hrubis | 400b622 | 2015-02-19 12:59:27 +0100 | [diff] [blame] | 28 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 29 | #include <errno.h> |
| 30 | #include <unistd.h> |
| 31 | #include <fcntl.h> |
robbiew | 01be333 | 2003-03-26 23:48:41 +0000 | [diff] [blame] | 32 | #include <time.h> |
| 33 | #include <wait.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 34 | |
| 35 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 36 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 37 | char *TCID = "nanosleep03"; |
| 38 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 39 | |
Cyril Hrubis | 400b622 | 2015-02-19 12:59:27 +0100 | [diff] [blame] | 40 | static void do_child(void); |
| 41 | static void setup(void); |
| 42 | static void sig_handler(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 44 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 46 | int lc; |
Cyril Hrubis | 400b622 | 2015-02-19 12:59:27 +0100 | [diff] [blame] | 47 | pid_t cpid; |
| 48 | int status; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 49 | |
Cyril Hrubis | d6d11d0 | 2015-03-09 17:35:43 +0100 | [diff] [blame] | 50 | tst_parse_opts(ac, av, NULL, NULL); |
Cyril Hrubis | 400b622 | 2015-02-19 12:59:27 +0100 | [diff] [blame] | 51 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 52 | #ifdef UCLINUX |
| 53 | maybe_run_child(&do_child, "dddd", &timereq.tv_sec, &timereq.tv_nsec, |
| 54 | &timerem.tv_sec, &timerem.tv_nsec); |
| 55 | #endif |
| 56 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | setup(); |
| 58 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 60 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | * Creat a child process and suspend its |
| 64 | * execution using nanosleep() |
| 65 | */ |
Cyril Hrubis | 400b622 | 2015-02-19 12:59:27 +0100 | [diff] [blame] | 66 | if ((cpid = FORK_OR_VFORK()) == -1) |
| 67 | tst_brkm(TBROK, NULL, "fork() failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | |
Cyril Hrubis | 400b622 | 2015-02-19 12:59:27 +0100 | [diff] [blame] | 69 | if (cpid == 0) { |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 70 | #ifdef UCLINUX |
| 71 | if (self_exec(av[0], "dddd", |
| 72 | timereq.tv_sec, timereq.tv_nsec, |
| 73 | timerem.tv_sec, timerem.tv_nsec) < 0) { |
Cyril Hrubis | 400b622 | 2015-02-19 12:59:27 +0100 | [diff] [blame] | 74 | tst_brkm(TBROK, NULL, "self_exec failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | } |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 76 | #else |
| 77 | do_child(); |
| 78 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | } |
| 80 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | sleep(1); |
| 82 | |
| 83 | /* Now send signal to child */ |
| 84 | if (kill(cpid, SIGINT) < 0) { |
Cyril Hrubis | 400b622 | 2015-02-19 12:59:27 +0100 | [diff] [blame] | 85 | tst_brkm(TBROK, NULL, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | "kill() fails send signal to child"); |
| 87 | } |
| 88 | |
| 89 | /* Wait for child to execute */ |
| 90 | wait(&status); |
yaberauneya | f00ed7a | 2009-12-22 17:35:13 +0000 | [diff] [blame] | 91 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
| 92 | tst_resm(TPASS, "nanosleep() failed, interrupted" |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 93 | " by signal (%d) as expected", EINTR); |
yaberauneya | f00ed7a | 2009-12-22 17:35:13 +0000 | [diff] [blame] | 94 | } else { |
| 95 | tst_resm(TFAIL, "child process exited abnormally; " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 96 | "status = %d", status); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 98 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 100 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 101 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | |
Cyril Hrubis | 400b622 | 2015-02-19 12:59:27 +0100 | [diff] [blame] | 103 | static void do_child(void) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 104 | { |
Cyril Hrubis | 400b622 | 2015-02-19 12:59:27 +0100 | [diff] [blame] | 105 | struct timespec timereq = {.tv_sec = 5, .tv_nsec = 9999}; |
| 106 | struct timespec timerem; |
| 107 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 108 | /* |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 109 | * Call nanosleep() to suspend child process |
| 110 | * for specified time 'tv_sec'. |
| 111 | * Call should return before suspending execution |
| 112 | * for the specified time due to receipt of signal |
| 113 | * from Parent. |
| 114 | */ |
| 115 | TEST(nanosleep(&timereq, &timerem)); |
| 116 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 117 | if (TEST_RETURN == -1) { |
| 118 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 119 | /* Check for expected errno is set */ |
| 120 | if (TEST_ERRNO != EINTR) { |
yaberauneya | f00ed7a | 2009-12-22 17:35:13 +0000 | [diff] [blame] | 121 | tst_resm(TFAIL | TTERRNO, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 122 | "nanosleep() failed; expected errno: %d", |
| 123 | EINTR); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 124 | exit(1); |
| 125 | } |
| 126 | } else { |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 127 | tst_resm(TFAIL, "nanosleep() returns %ld, " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 128 | "expected -1, errno:%d", TEST_RETURN, EINTR); |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 129 | exit(1); |
| 130 | } |
| 131 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 132 | exit(0); |
| 133 | } |
| 134 | |
Cyril Hrubis | 400b622 | 2015-02-19 12:59:27 +0100 | [diff] [blame] | 135 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | { |
Cyril Hrubis | 400b622 | 2015-02-19 12:59:27 +0100 | [diff] [blame] | 137 | tst_sig(FORK, DEF_HANDLER, NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | TEST_PAUSE; |
| 140 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | if (signal(SIGINT, sig_handler) == SIG_ERR) { |
Cyril Hrubis | 400b622 | 2015-02-19 12:59:27 +0100 | [diff] [blame] | 142 | tst_brkm(TBROK, NULL, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | "signal() fails to setup signal handler"); |
| 144 | } |
| 145 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Cyril Hrubis | 400b622 | 2015-02-19 12:59:27 +0100 | [diff] [blame] | 148 | static void sig_handler(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | { |
| 150 | } |