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: nanosleep01 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that nanosleep() will be successful to suspend the execution |
| 25 | * of a process for a specified time. |
| 26 | * |
| 27 | * Expected Result: |
| 28 | * nanosleep() should return with value 0 and the process should be |
| 29 | * suspended for time specified by timespec structure. |
| 30 | * |
| 31 | * Algorithm: |
| 32 | * Setup: |
| 33 | * Setup signal handling. |
| 34 | * Pause for SIGUSR1 if option specified. |
| 35 | * |
| 36 | * Test: |
| 37 | * Loop if the proper options are given. |
| 38 | * Execute system call |
| 39 | * Check return code, if system call failed (return=-1) |
robbiew | 2d950e4 | 2003-03-28 18:57:31 +0000 | [diff] [blame] | 40 | * Issue a FAIL message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 41 | * Otherwise, |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 42 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | * if successful, |
robbiew | 2d950e4 | 2003-03-28 18:57:31 +0000 | [diff] [blame] | 44 | * Issue Functionality-Pass message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | * Otherwise, |
robbiew | 2d950e4 | 2003-03-28 18:57:31 +0000 | [diff] [blame] | 46 | * Issue Functionality-Fail message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 47 | * Cleanup: |
| 48 | * Print errno log and/or timing stats if options given |
| 49 | * |
| 50 | * Usage: <for command-line> |
| 51 | * nanosleep01 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 52 | * where, -c n : Run n copies concurrently. |
| 53 | * -f : Turn off functionality Testing. |
robbiew | 2d950e4 | 2003-03-28 18:57:31 +0000 | [diff] [blame] | 54 | * -i n : Execute test n times. |
| 55 | * -I x : Execute test for x seconds. |
| 56 | * -P x : Pause for x seconds between iterations. |
| 57 | * -t : Turn on syscall timing. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | * |
| 59 | * HISTORY |
robbiew | 2d950e4 | 2003-03-28 18:57:31 +0000 | [diff] [blame] | 60 | * 07/2001 Ported by Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | * |
| 62 | * RESTRICTIONS: |
| 63 | * None. |
| 64 | * |
| 65 | */ |
| 66 | #include <errno.h> |
| 67 | #include <unistd.h> |
| 68 | #include <fcntl.h> |
| 69 | #include <time.h> |
robbiew | 2d950e4 | 2003-03-28 18:57:31 +0000 | [diff] [blame] | 70 | #include <sys/time.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | #include <sys/wait.h> |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 72 | #include <stdint.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | |
| 74 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 76 | char *TCID = "nanosleep01"; |
| 77 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 79 | struct timespec timereq; /* time struct. buffer for nanosleep() */ |
robbiew | 2d950e4 | 2003-03-28 18:57:31 +0000 | [diff] [blame] | 80 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | void setup(); /* Main setup function of test */ |
| 82 | void cleanup(); /* cleanup function for the test */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 86 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 87 | const char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | pid_t cpid; /* Child process id */ |
| 89 | struct timeval otime; /* time before child execution suspended */ |
| 90 | struct timeval ntime; /* time after child resumes execution */ |
| 91 | int retval = 0, e_code, status; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 93 | msg = parse_opts(ac, av, NULL, NULL); |
| 94 | if (msg != NULL) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 95 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 96 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 97 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 99 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 103 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 105 | /* |
| 106 | * Creat a child process and suspend it till |
| 107 | * time specified by timespec struct element |
| 108 | * time_t tv_sec. |
| 109 | */ |
| 110 | cpid = FORK_OR_VFORK(); |
| 111 | if (cpid == -1) { |
| 112 | tst_brkm(TBROK, cleanup, "fork() failed"); |
| 113 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 115 | if (cpid == 0) { /* Child process */ |
| 116 | /* Note down the current time */ |
| 117 | gettimeofday(&otime, 0); |
| 118 | /* |
| 119 | * Call nanosleep() to suspend child process |
| 120 | * for specified time. |
| 121 | */ |
| 122 | TEST(nanosleep(&timereq, NULL)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 124 | /* time after child resumes execution */ |
| 125 | gettimeofday(&ntime, 0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 127 | if (TEST_RETURN == -1) { |
| 128 | retval = 1; |
| 129 | tst_resm(TFAIL, |
| 130 | "nanosleep() failed, errno=%d : %s", |
| 131 | TEST_ERRNO, strerror(TEST_ERRNO)); |
| 132 | continue; |
| 133 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 135 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 136 | * Verify whether child execution was |
| 137 | * actually suspended to desired interval. |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 138 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 139 | long want_ms, got_ms; |
| 140 | want_ms = |
| 141 | timereq.tv_sec * 1000 + |
| 142 | timereq.tv_nsec / 1000000; |
| 143 | got_ms = |
| 144 | ntime.tv_sec * 1000 + ntime.tv_usec / 1000; |
| 145 | got_ms -= |
| 146 | otime.tv_sec * 1000 + otime.tv_usec / 1000; |
| 147 | if (got_ms < want_ms) { |
| 148 | retval = 1; |
| 149 | tst_resm(TFAIL, "Child execution not " |
| 150 | "suspended for %jd seconds. (Wanted %ld ms, got %ld ms)", |
| 151 | (intmax_t) timereq.tv_sec, |
| 152 | want_ms, got_ms); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 153 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 154 | tst_resm(TPASS, "nanosleep " |
| 155 | "functionality is correct"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 156 | } |
| 157 | exit(retval); |
| 158 | } else { /* parent process */ |
| 159 | /* wait for the child to finish */ |
| 160 | wait(&status); |
| 161 | /* make sure the child returned a good exit status */ |
| 162 | e_code = status >> 8; |
| 163 | if (e_code != 0) { |
| 164 | tst_resm(TFAIL, "Failures reported above"); |
| 165 | } |
| 166 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 167 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 168 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 169 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 170 | tst_exit(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 171 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 172 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | |
| 174 | /* |
| 175 | * setup() - performs all ONE TIME setup for this test. |
robbiew | 2d950e4 | 2003-03-28 18:57:31 +0000 | [diff] [blame] | 176 | * Initialize time structure elements. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 178 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 179 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 180 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 181 | tst_sig(FORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 183 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 184 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 185 | /* Initialise time variables which used to suspend child execution */ |
| 186 | timereq.tv_sec = 2; |
| 187 | timereq.tv_nsec = 9999; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 188 | } |
| 189 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 190 | /* |
| 191 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 192 | * completion or premature exit. |
| 193 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 194 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 195 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 196 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 197 | } |