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 |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 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" |
| 71 | #include "usctest.h" |
| 72 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 73 | char *TCID = "nanosleep04"; /* Test program identifier. */ |
| 74 | int TST_TOTAL = 1; /* Total number of test cases. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | extern int Tst_count; /* Test Case counter for tst_* routines */ |
| 76 | |
| 77 | struct timespec timereq; /* time struct. buffer for nanosleep() */ |
| 78 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 79 | int exp_enos[] = { EINVAL, 0 }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | |
| 81 | void setup(); /* Main setup function of test */ |
| 82 | void cleanup(); /* cleanup function for the test */ |
| 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 | { |
| 86 | int lc; /* loop counter */ |
| 87 | char *msg; /* message returned from parse_opts */ |
| 88 | pid_t cpid; /* Child process id */ |
| 89 | int status; /* child exit status */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 90 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | /* Parse standard options given to run the test. */ |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 92 | msg = parse_opts(ac, av, NULL, NULL); |
| 93 | if (msg != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame^] | 94 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* Perform global setup for test */ |
| 98 | setup(); |
| 99 | |
| 100 | /* set the expected errnos... */ |
| 101 | TEST_EXP_ENOS(exp_enos); |
| 102 | |
| 103 | /* Check looping state if -i option given */ |
| 104 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 105 | |
| 106 | /* Reset Tst_count in case we are looping. */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 107 | Tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | |
| 109 | /* |
| 110 | * Creat a child process and suspend its |
| 111 | * execution using nanosleep() |
| 112 | */ |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 113 | if ((cpid = FORK_OR_VFORK()) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | tst_brkm(TBROK, cleanup, |
| 115 | "fork() fails to create child process"); |
| 116 | } |
| 117 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 118 | if (cpid == 0) { /* Child process */ |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 119 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | * Call nanosleep() to suspend child process |
| 121 | * for specified time 'tv_sec'. |
| 122 | */ |
| 123 | TEST(nanosleep(&timereq, NULL)); |
| 124 | |
| 125 | /* check return code of nanosleep() */ |
| 126 | if (TEST_RETURN == -1) { |
| 127 | |
| 128 | TEST_ERROR_LOG(TEST_ERRNO); |
| 129 | |
| 130 | /* Check for expected errno is set */ |
| 131 | if (TEST_ERRNO != EINVAL) { |
| 132 | tst_resm(TFAIL, "nanosleep() failed, " |
| 133 | "errno:%d, expected:%d", |
| 134 | TEST_ERRNO, EINVAL); |
| 135 | exit(1); |
| 136 | } |
| 137 | } else { |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 138 | tst_resm(TFAIL, "nanosleep() returns %ld, " |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | "expected -1, errno:%d", |
| 140 | TEST_RETURN, EINVAL); |
| 141 | exit(1); |
| 142 | } |
| 143 | |
| 144 | /* Everything is fine, exit normally */ |
| 145 | exit(0); |
| 146 | } |
| 147 | |
| 148 | /* Wait for child to execute */ |
| 149 | wait(&status); |
yaberauneya | f00ed7a | 2009-12-22 17:35:13 +0000 | [diff] [blame] | 150 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
| 151 | tst_resm(TPASS, "nanosleep() failed, when provided " |
| 152 | "invalid pause time, with expected " |
| 153 | "errno: %d", EINVAL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | } else if (WEXITSTATUS(status) == 1) { |
yaberauneya | f00ed7a | 2009-12-22 17:35:13 +0000 | [diff] [blame] | 155 | tst_resm(TFAIL, "child process exited abnormally; " |
| 156 | "status = %d", status); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 158 | } /* End for TEST_LOOPING */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | |
| 160 | /* Call cleanup() to undo setup done for the test. */ |
| 161 | cleanup(); |
| 162 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 163 | /*NOTREACHED*/ return 0; |
| 164 | } /* End main */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 165 | |
| 166 | /* |
| 167 | * setup() - performs all ONE TIME setup for this test. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 168 | * Initialise time structure elements, such that pause time |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | * points to -ve value. |
| 170 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 171 | void setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 172 | { |
| 173 | /* capture signals */ |
| 174 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 175 | |
| 176 | /* Pause if that option was specified */ |
| 177 | TEST_PAUSE; |
| 178 | |
| 179 | /* Initialise time variables which used to suspend child execution */ |
| 180 | timereq.tv_sec = -5; |
| 181 | timereq.tv_nsec = 9999; |
| 182 | |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 187 | * completion or premature exit. |
| 188 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 189 | void cleanup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 190 | { |
| 191 | /* |
| 192 | * print timing stats if that option was specified. |
| 193 | * print errno log if that option was specified. |
| 194 | */ |
| 195 | TEST_CLEANUP; |
| 196 | |
| 197 | /* exit with return code appropriate for results */ |
| 198 | tst_exit(); |
| 199 | } |