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: nanosleep04 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that nanosleep() will fail to suspend the execution |
| 25 | * of a process if the specified pause time is invalid. |
| 26 | * |
| 27 | * Expected Result: |
| 28 | * nanosleep() should return with -1 value and sets errno to EINVAL. |
| 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 | * nanosleep04 [-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 <wait.h> |
| 68 | #include <time.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 = "nanosleep04"; |
| 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; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | |
| 77 | void setup(); /* Main setup function of test */ |
| 78 | void cleanup(); /* cleanup function for the test */ |
| 79 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 80 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 82 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 83 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | pid_t cpid; /* Child process id */ |
| 85 | int status; /* child exit status */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 86 | |
Garrett Cooper | a9e49f1 | 2010-12-16 10:54:03 -0800 | [diff] [blame] | 87 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 88 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | setup(); |
| 91 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 93 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 94 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | |
| 96 | /* |
| 97 | * Creat a child process and suspend its |
| 98 | * execution using nanosleep() |
| 99 | */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 100 | if ((cpid = FORK_OR_VFORK()) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | tst_brkm(TBROK, cleanup, |
| 102 | "fork() fails to create child process"); |
| 103 | } |
| 104 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 105 | if (cpid == 0) { /* Child process */ |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 106 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | * Call nanosleep() to suspend child process |
| 108 | * for specified time 'tv_sec'. |
| 109 | */ |
| 110 | TEST(nanosleep(&timereq, NULL)); |
| 111 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | if (TEST_RETURN == -1) { |
| 113 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | /* Check for expected errno is set */ |
| 115 | if (TEST_ERRNO != EINVAL) { |
| 116 | tst_resm(TFAIL, "nanosleep() failed, " |
| 117 | "errno:%d, expected:%d", |
| 118 | TEST_ERRNO, EINVAL); |
| 119 | exit(1); |
| 120 | } |
| 121 | } else { |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 122 | tst_resm(TFAIL, "nanosleep() returns %ld, " |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | "expected -1, errno:%d", |
| 124 | TEST_RETURN, EINVAL); |
| 125 | exit(1); |
| 126 | } |
| 127 | |
| 128 | /* Everything is fine, exit normally */ |
| 129 | exit(0); |
| 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, when provided " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 136 | "invalid pause time, with expected " |
| 137 | "errno: %d", EINVAL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | } else if (WEXITSTATUS(status) == 1) { |
yaberauneya | f00ed7a | 2009-12-22 17:35:13 +0000 | [diff] [blame] | 139 | tst_resm(TFAIL, "child process exited abnormally; " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 140 | "status = %d", status); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 142 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | cleanup(); |
| 145 | |
Garrett Cooper | a9e49f1 | 2010-12-16 10:54:03 -0800 | [diff] [blame] | 146 | tst_exit(); |
| 147 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 148 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | |
| 150 | /* |
| 151 | * setup() - performs all ONE TIME setup for this test. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 152 | * Initialise time structure elements, such that pause time |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | * points to -ve value. |
| 154 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 155 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 157 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 159 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | TEST_PAUSE; |
| 161 | |
| 162 | /* Initialise time variables which used to suspend child execution */ |
| 163 | timereq.tv_sec = -5; |
| 164 | timereq.tv_nsec = 9999; |
| 165 | |
| 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 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 174 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 175 | } |