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 | * NAME |
| 22 | * settimeofday01.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Testcase to check the basic functionality of settimeofday(). |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * Setup: |
| 29 | * Setup signal handling. |
| 30 | * Check that we are the proper user. |
| 31 | * Setup expected errnos. |
| 32 | * Pause for SIGUSER1 if option specified. |
| 33 | * Save the current time values. |
| 34 | * Loop if the proper options are given. |
| 35 | * Call settimeofday and verify the time was changed. |
| 36 | * Call settimeofday with invalid Args and verify that the call fails. |
| 37 | * Cleanup: |
| 38 | * Restore the original time values. |
| 39 | * Print errno log and/or timing stats if options given. |
| 40 | * |
| 41 | * USAGE: <for command-line> |
| 42 | * settimeofday01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 43 | * where, -c n : Run n copies concurrently. |
| 44 | * -e : Turn on errno logging. |
| 45 | * -i n : Execute test n times. |
| 46 | * -I x : Execute test for x seconds. |
| 47 | * -P x : Pause for x seconds between iterations. |
| 48 | * -t : Turn on syscall timing. |
| 49 | * |
| 50 | * History |
| 51 | * 07/2001 John George |
| 52 | * -Ported |
| 53 | * |
| 54 | * Restrictions |
| 55 | * Must be run as root. |
| 56 | */ |
| 57 | |
| 58 | #include <sys/time.h> |
| 59 | #include <errno.h> |
| 60 | #include <unistd.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 61 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | |
| 63 | #define FAILED 1 |
| 64 | #define VAL_SEC 100 |
| 65 | #define VAL_MSEC 100 |
robbiew | 4e8556e | 2004-11-22 17:08:16 +0000 | [diff] [blame] | 66 | #define ACCEPTABLE_DELTA 500 /* in milli-seconds */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | |
| 68 | char *TCID = "settimeofday01"; |
| 69 | int TST_TOTAL = 1; |
| 70 | time_t save_tv_sec, save_tv_usec; |
robbiew | 4e8556e | 2004-11-22 17:08:16 +0000 | [diff] [blame] | 71 | struct timeval tp, tp1, tp2; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | |
| 73 | void setup(void); |
| 74 | void cleanup(void); |
| 75 | |
vapier | cc0d450 | 2006-02-27 04:36:28 +0000 | [diff] [blame] | 76 | #if !defined(UCLINUX) |
| 77 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 78 | int main(int argc, char **argv) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 80 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 81 | const char *msg; |
robbiew | 4e8556e | 2004-11-22 17:08:16 +0000 | [diff] [blame] | 82 | suseconds_t delta; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 84 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 86 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 87 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | |
| 89 | setup(); |
| 90 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 92 | int condition_number = 1; |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 93 | /* reset tst_count in case we are looping */ |
| 94 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 96 | tp.tv_sec = VAL_SEC; |
| 97 | tp.tv_usec = VAL_MSEC; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | |
| 99 | TEST(settimeofday(&tp, NULL)); |
| 100 | if (TEST_RETURN == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | tst_resm(TFAIL, "Error Setting Time, errno=%d", |
| 102 | TEST_ERRNO); |
| 103 | } |
| 104 | |
robbiew | 4e8556e | 2004-11-22 17:08:16 +0000 | [diff] [blame] | 105 | if ((gettimeofday(&tp2, (struct timezone *)&tp1)) == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 106 | tst_resm(TBROK, "Error Getting Time, errno=%d", errno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | } |
| 108 | |
robbiew | 4e8556e | 2004-11-22 17:08:16 +0000 | [diff] [blame] | 109 | if (tp2.tv_sec > tp.tv_sec) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 110 | delta = |
| 111 | (suseconds_t) (tp2.tv_sec - tp.tv_sec) * 1000 + |
| 112 | (tp2.tv_usec - tp.tv_usec) / 1000; |
robbiew | 4e8556e | 2004-11-22 17:08:16 +0000 | [diff] [blame] | 113 | } else { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 114 | delta = |
| 115 | (suseconds_t) (tp.tv_sec - tp2.tv_sec) * 1000 + |
| 116 | (tp.tv_usec - tp2.tv_usec) / 1000; |
robbiew | 4e8556e | 2004-11-22 17:08:16 +0000 | [diff] [blame] | 117 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 118 | |
| 119 | if (delta > -ACCEPTABLE_DELTA && delta < ACCEPTABLE_DELTA) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | tst_resm(TPASS, "Test condition %d successful", |
| 121 | condition_number++); |
| 122 | } else { |
| 123 | tst_resm(TFAIL, "Test condition %d failed", |
| 124 | condition_number++); |
| 125 | } |
| 126 | |
| 127 | /* Invalid Args : Error Condition where tp = NULL */ |
| 128 | TEST(settimeofday((struct timeval *)-1, NULL)); |
| 129 | if (TEST_RETURN == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | tst_resm(TPASS, "Test condition %d successful", |
| 131 | condition_number++); |
| 132 | } else { |
| 133 | tst_resm(TFAIL, "Test condition %d failed", |
| 134 | condition_number); |
| 135 | } |
| 136 | |
| 137 | } |
| 138 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 139 | tst_exit(); |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 140 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | } |
| 142 | |
vapier | cc0d450 | 2006-02-27 04:36:28 +0000 | [diff] [blame] | 143 | #else |
| 144 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 145 | int main(void) |
vapier | cc0d450 | 2006-02-27 04:36:28 +0000 | [diff] [blame] | 146 | { |
| 147 | tst_resm(TINFO, "test is not available on uClinux"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 148 | tst_exit(); |
vapier | cc0d450 | 2006-02-27 04:36:28 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | #endif /* if !defined(UCLINUX) */ |
| 152 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | /* |
| 154 | * setup() |
| 155 | * performs all ONE TIME setup for this test |
| 156 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 157 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | { |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 159 | tst_require_root(NULL); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 160 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 162 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | /* Pause if that option was specified |
| 164 | * TEST_PAUSE contains the code to fork the test with the -c option. |
| 165 | */ |
| 166 | TEST_PAUSE; |
| 167 | |
| 168 | /* Save the current time values */ |
| 169 | if ((gettimeofday(&tp, (struct timezone *)&tp1)) == -1) { |
| 170 | tst_brkm(TBROK, cleanup, "gettimeofday failed. " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 171 | "errno=%d", errno); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 172 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | save_tv_sec = tp.tv_sec; |
| 174 | save_tv_usec = tp.tv_usec; |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * cleanup() |
| 179 | * performs all ONE TIME cleanup for this test at |
| 180 | * completion or premature exit |
| 181 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 182 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 183 | { |
| 184 | /* restore the original time values. */ |
| 185 | tp.tv_sec = save_tv_sec; |
| 186 | tp.tv_usec = save_tv_usec; |
| 187 | if ((settimeofday(&tp, NULL)) == -1) { |
| 188 | tst_resm(TWARN, "FATAL COULD NOT RESET THE CLOCK"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 189 | tst_resm(TFAIL, "Error Setting Time, errno=%d", errno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 192 | } |