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 | * 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> |
| 61 | #include <test.h> |
| 62 | #include <usctest.h> |
| 63 | |
| 64 | #define FAILED 1 |
| 65 | #define VAL_SEC 100 |
| 66 | #define VAL_MSEC 100 |
robbiew | 4e8556e | 2004-11-22 17:08:16 +0000 | [diff] [blame] | 67 | #define ACCEPTABLE_DELTA 500 /* in milli-seconds */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | |
| 69 | char *TCID = "settimeofday01"; |
| 70 | int TST_TOTAL = 1; |
| 71 | time_t save_tv_sec, save_tv_usec; |
robbiew | 4e8556e | 2004-11-22 17:08:16 +0000 | [diff] [blame] | 72 | struct timeval tp, tp1, tp2; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | int exp_enos[]={EFAULT, 0}; |
| 74 | extern int Tst_count; |
| 75 | |
| 76 | void setup(void); |
| 77 | void cleanup(void); |
| 78 | |
vapier | cc0d450 | 2006-02-27 04:36:28 +0000 | [diff] [blame] | 79 | #if !defined(UCLINUX) |
| 80 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 81 | int main(int argc, char **argv) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | { |
| 83 | int lc; /* loop counter */ |
| 84 | char *msg; /* message returned from parse_opts */ |
robbiew | 4e8556e | 2004-11-22 17:08:16 +0000 | [diff] [blame] | 85 | suseconds_t delta; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | |
| 87 | /* parse standard options */ |
| 88 | if ((msg = parse_opts(argc, argv, (option_t *)NULL, NULL)) != |
| 89 | (char *)NULL) { |
| 90 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 91 | tst_exit(); |
| 92 | /*NOTREACHED*/ |
| 93 | } |
| 94 | |
| 95 | setup(); |
| 96 | |
| 97 | /* check looping state if -i option is given */ |
| 98 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 99 | int condition_number = 1; |
| 100 | /* reset Tst_count in case we are looping */ |
| 101 | Tst_count = 0; |
| 102 | |
| 103 | tp.tv_sec = VAL_SEC; |
| 104 | tp.tv_usec = VAL_MSEC; |
| 105 | |
| 106 | TEST(settimeofday(&tp, NULL)); |
| 107 | if (TEST_RETURN == -1) { |
| 108 | TEST_ERROR_LOG(TEST_ERRNO); |
| 109 | tst_resm(TFAIL, "Error Setting Time, errno=%d", |
| 110 | TEST_ERRNO); |
| 111 | } |
| 112 | |
robbiew | 4e8556e | 2004-11-22 17:08:16 +0000 | [diff] [blame] | 113 | if ((gettimeofday(&tp2, (struct timezone *)&tp1)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | tst_resm(TBROK, "Error Getting Time, errno=%d", |
| 115 | errno); |
| 116 | } |
| 117 | |
robbiew | 4e8556e | 2004-11-22 17:08:16 +0000 | [diff] [blame] | 118 | if (tp2.tv_sec > tp.tv_sec) { |
| 119 | delta=(suseconds_t)(tp2.tv_sec-tp.tv_sec)*1000+(tp2.tv_usec-tp.tv_usec)/1000; |
| 120 | } else { |
| 121 | delta=(suseconds_t)(tp.tv_sec-tp2.tv_sec)*1000+(tp.tv_usec-tp2.tv_usec)/1000; |
| 122 | } |
| 123 | |
| 124 | if(delta>-ACCEPTABLE_DELTA && delta < ACCEPTABLE_DELTA) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | tst_resm(TPASS, "Test condition %d successful", |
| 126 | condition_number++); |
| 127 | } else { |
| 128 | tst_resm(TFAIL, "Test condition %d failed", |
| 129 | condition_number++); |
| 130 | } |
| 131 | |
| 132 | /* Invalid Args : Error Condition where tp = NULL */ |
| 133 | TEST(settimeofday((struct timeval *)-1, NULL)); |
| 134 | if (TEST_RETURN == -1) { |
| 135 | TEST_ERROR_LOG(TEST_ERRNO); |
| 136 | tst_resm(TPASS, "Test condition %d successful", |
| 137 | condition_number++); |
| 138 | } else { |
| 139 | tst_resm(TFAIL, "Test condition %d failed", |
| 140 | condition_number); |
| 141 | } |
| 142 | |
| 143 | } |
| 144 | cleanup(); |
| 145 | /*NOTREACHED*/ |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 146 | |
subrata_modak | 43337a3 | 2009-02-26 11:43:51 +0000 | [diff] [blame^] | 147 | return 0; |
robbiew | fa451a1 | 2003-03-27 20:52:36 +0000 | [diff] [blame] | 148 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | } |
| 150 | |
vapier | cc0d450 | 2006-02-27 04:36:28 +0000 | [diff] [blame] | 151 | #else |
| 152 | |
| 153 | int main() |
| 154 | { |
| 155 | tst_resm(TINFO, "test is not available on uClinux"); |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | #endif /* if !defined(UCLINUX) */ |
| 160 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | /* |
| 162 | * setup() |
| 163 | * performs all ONE TIME setup for this test |
| 164 | */ |
| 165 | void |
| 166 | setup(void) |
| 167 | { |
| 168 | /* capture signals */ |
| 169 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 170 | |
| 171 | /* Check that the test process id is root */ |
| 172 | if (geteuid() != 0) { |
| 173 | tst_brkm(TBROK, NULL, "Must be root for this test!"); |
| 174 | tst_exit(); |
| 175 | /*NOTREACHED*/ |
| 176 | } |
| 177 | |
| 178 | /* set the expected errnos... */ |
| 179 | TEST_EXP_ENOS(exp_enos); |
| 180 | |
| 181 | /* Pause if that option was specified |
| 182 | * TEST_PAUSE contains the code to fork the test with the -c option. |
| 183 | */ |
| 184 | TEST_PAUSE; |
| 185 | |
| 186 | /* Save the current time values */ |
| 187 | if ((gettimeofday(&tp, (struct timezone *)&tp1)) == -1) { |
| 188 | tst_brkm(TBROK, cleanup, "gettimeofday failed. " |
| 189 | "errno=%d", errno); |
| 190 | /*NOTREACHED*/ |
| 191 | } |
| 192 | save_tv_sec = tp.tv_sec; |
| 193 | save_tv_usec = tp.tv_usec; |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * cleanup() |
| 198 | * performs all ONE TIME cleanup for this test at |
| 199 | * completion or premature exit |
| 200 | */ |
| 201 | void |
| 202 | cleanup(void) |
| 203 | { |
| 204 | /* restore the original time values. */ |
| 205 | tp.tv_sec = save_tv_sec; |
| 206 | tp.tv_usec = save_tv_usec; |
| 207 | if ((settimeofday(&tp, NULL)) == -1) { |
| 208 | tst_resm(TWARN, "FATAL COULD NOT RESET THE CLOCK"); |
| 209 | tst_resm(TFAIL, "Error Setting Time, errno=%d", |
| 210 | errno); |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * print timing stats if that option was specified. |
| 215 | * print errno log if that option was specified. |
| 216 | */ |
| 217 | TEST_CLEANUP; |
| 218 | |
| 219 | /* exit with return code appropriate for results */ |
| 220 | tst_exit(); |
| 221 | /*NOTREACHED*/ |
| 222 | } |