robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) Wipro Technologies Ltd, 2002. All Rights Reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of version 2 of the GNU General Public License as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it would be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License along |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 13 | * with this program; if not, write the Free Software Foundation, Inc., |
| 14 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 15 | * |
| 16 | */ |
| 17 | /********************************************************** |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 18 | * |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 19 | * TEST IDENTIFIER : adjtimex02 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 20 | * |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 21 | * EXECUTED BY : root / superuser |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 22 | * |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 23 | * TEST TITLE : Tests for error conditions |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 24 | * |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 25 | * TEST CASE TOTAL : 6 |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 26 | * |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 27 | * AUTHOR : Saji Kumar.V.R <saji.kumar@wipro.com> |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 28 | * |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 29 | * SIGNALS |
| 30 | * Uses SIGUSR1 to pause before test if option set. |
| 31 | * (See the parse_opts(3) man page). |
| 32 | * |
| 33 | * DESCRIPTION |
| 34 | * Verify that |
| 35 | * 1) adjtimex(2) fails with errno set to EFAULT if buf does |
| 36 | * not point to writable memory |
| 37 | * 2) adjtimex(2) fails with errno set to EINVAL if an attempt |
| 38 | * is made to set buf.tick to a value < 900000/HZ |
| 39 | * 3) adjtimex(2) fails with errno set to EINVAL if an attempt |
| 40 | * is made to set buf.tick to a value > 1100000/HZ |
| 41 | * 4) adjtimex(2) fails with errno set to EINVAL if an attempt |
| 42 | * is made to set buf.offset to a value > 512000L |
subrata_modak | 69bc3dc | 2008-05-23 08:36:04 +0000 | [diff] [blame] | 43 | * (This test case will be executed only if the kernel version |
| 44 | * is 2.6.25 or below) |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 45 | * 5) adjtimex(2) fails with errno set to EINVAL if an attempt |
| 46 | * is made to set buf.offset to a value < 512000L |
subrata_modak | 69bc3dc | 2008-05-23 08:36:04 +0000 | [diff] [blame] | 47 | * (This test case will be executed only if the kernel version |
| 48 | * is 2.6.25 or below) |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 49 | * 6) adjtimex(2) fails with errno set to EPERM if buf.mode is |
| 50 | * non-zero and the user is not super-user. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 51 | * |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 52 | * Setup: |
| 53 | * Setup signal handling. |
| 54 | * Pause for SIGUSR1 if option specified. |
| 55 | * Save current parameters in tim_save |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 56 | * |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 57 | * Test: |
| 58 | * Loop if the proper options are given. |
| 59 | * Call test case specific setup if needed |
| 60 | * call adjtimex with saved timex structure |
| 61 | * Check return value is between 0 & 5 |
| 62 | * Test passed |
| 63 | * Otherwise |
| 64 | * Test failed |
| 65 | * Call test case specific cleanup if needed |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 66 | * |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 67 | * Cleanup: |
| 68 | * Print errno log and/or timing stats if options given |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 69 | * |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 70 | * USAGE: <for command-line> |
| 71 | * adjtimex02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-h] [-f] [-p] |
| 72 | * where, -c n : Run n copies concurrently. |
| 73 | * -e : Turn on errno logging. |
| 74 | * -h : Show help screen |
| 75 | * -f : Turn off functional testing |
| 76 | * -i n : Execute test n times. |
| 77 | * -I x : Execute test for x seconds. |
| 78 | * -p : Pause for SIGUSR1 before starting |
| 79 | * -P x : Pause for x seconds between iterations. |
| 80 | * -t : Turn on syscall timing. |
| 81 | * |
| 82 | ****************************************************************/ |
| 83 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 84 | #if defined UCLINUX && !__THROW |
| 85 | /* workaround for libc bug causing failure in sys/timex.h */ |
| 86 | #define __THROW |
| 87 | #endif |
| 88 | |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 89 | #include <errno.h> |
| 90 | #include <sys/timex.h> |
mreed10 | c360edb | 2006-11-03 17:21:43 +0000 | [diff] [blame] | 91 | #include <unistd.h> |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 92 | #include <pwd.h> |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 93 | #include "test.h" |
| 94 | #include "usctest.h" |
| 95 | |
| 96 | #define SET_MODE ( ADJ_OFFSET | ADJ_FREQUENCY | ADJ_MAXERROR | ADJ_ESTERROR | \ |
| 97 | ADJ_STATUS | ADJ_TIMECONST | ADJ_TICK ) |
| 98 | |
| 99 | static void setup(); |
| 100 | static int setup2(); |
| 101 | static int setup3(); |
| 102 | static int setup4(); |
| 103 | static int setup5(); |
| 104 | static int setup6(); |
| 105 | static void cleanup(); |
| 106 | static void cleanup6(); |
| 107 | |
| 108 | char *TCID = "adjtimex02"; /* Test program identifier. */ |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 109 | |
mreed10 | c360edb | 2006-11-03 17:21:43 +0000 | [diff] [blame] | 110 | static int hz; /* HZ from sysconf */ |
| 111 | |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 112 | static struct timex tim_save; |
| 113 | static struct timex buff; |
| 114 | static int exp_enos[] = { EPERM, EINVAL, EFAULT, 0 }; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 115 | |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 116 | static char nobody_uid[] = "nobody"; |
| 117 | struct passwd *ltpuser; |
| 118 | |
| 119 | struct test_cases_t { |
| 120 | struct timex *buffp; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 121 | int (*setup) (); |
| 122 | void (*cleanup) (); |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 123 | int exp_errno; |
| 124 | } test_cases[] = { |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 125 | #ifndef UCLINUX |
| 126 | /* Skip since uClinux does not implement memory protection */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 127 | { |
| 128 | (struct timex *)-1, NULL, NULL, EFAULT}, |
vapier | 7ec19d9 | 2006-02-27 04:38:56 +0000 | [diff] [blame] | 129 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 130 | { |
| 131 | &buff, setup2, NULL, EINVAL}, { |
| 132 | &buff, setup3, NULL, EINVAL}, { |
| 133 | &buff, setup4, NULL, EINVAL}, { |
| 134 | &buff, setup5, NULL, EINVAL}, { |
| 135 | &buff, setup6, cleanup6, EPERM} |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | int TST_TOTAL = sizeof(test_cases) / sizeof(test_cases[0]); |
| 139 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 140 | int main(int ac, char **av) |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 141 | { |
| 142 | |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 143 | int lc, i; |
| 144 | char *msg; |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 145 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 146 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 147 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 148 | |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 149 | setup(); |
| 150 | |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 151 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 152 | |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 153 | Tst_count = 0; |
| 154 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 155 | for (i = 0; i < TST_TOTAL; ++i) { |
subrata_modak | 69bc3dc | 2008-05-23 08:36:04 +0000 | [diff] [blame] | 156 | /* |
| 157 | * since Linux 2.6.26, if buf.offset value is outside |
| 158 | * the acceptable range, it is simply normalized instead |
| 159 | * of letting the syscall fail. so just skip this test |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 160 | * case. |
subrata_modak | 69bc3dc | 2008-05-23 08:36:04 +0000 | [diff] [blame] | 161 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 162 | if ((i == 3 || i == 4) && tst_kvercmp(2, 6, 25) > 0) { |
subrata_modak | 69bc3dc | 2008-05-23 08:36:04 +0000 | [diff] [blame] | 163 | tst_resm(TCONF, "this kernel normalizes buf." |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 164 | "offset value if it is outside" |
| 165 | " the acceptable range."); |
subrata_modak | 69bc3dc | 2008-05-23 08:36:04 +0000 | [diff] [blame] | 166 | continue; |
| 167 | } |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 168 | |
| 169 | buff = tim_save; |
| 170 | buff.modes = SET_MODE; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 171 | if ((test_cases[i].setup) && (test_cases[i].setup())) { |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 172 | tst_resm(TWARN, "setup() failed, skipping" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 173 | " this test case"); |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 174 | continue; |
| 175 | } |
| 176 | |
| 177 | /* Call adjtimex(2) */ |
| 178 | TEST(adjtimex(test_cases[i].buffp)); |
| 179 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 180 | if ((TEST_RETURN == -1) && (TEST_ERRNO == |
| 181 | test_cases[i].exp_errno)) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 182 | tst_resm(TPASS | TTERRNO, |
vapier | 8dc646d | 2009-08-28 10:54:04 +0000 | [diff] [blame] | 183 | "Test Passed, adjtimex() returned -1"); |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 184 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 185 | tst_resm(TFAIL | TTERRNO, |
| 186 | "Test Failed, adjtimex() returned %ld", |
| 187 | TEST_RETURN); |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 188 | } |
| 189 | TEST_ERROR_LOG(TEST_ERRNO); |
| 190 | if (test_cases[i].cleanup) { |
| 191 | test_cases[i].cleanup(); |
| 192 | } |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 193 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 194 | } |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 195 | |
| 196 | /* cleanup and exit */ |
| 197 | cleanup(); |
| 198 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 199 | tst_exit(); |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 200 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 201 | } |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 202 | |
| 203 | /* setup() - performs all ONE TIME setup for this test */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 204 | void setup() |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 205 | { |
| 206 | |
| 207 | tim_save.modes = 0; |
| 208 | |
| 209 | /* Check whether we are root */ |
| 210 | if (geteuid() != 0) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 211 | tst_brkm(TBROK, NULL, "Test must be run as root"); |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 212 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 213 | |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 214 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 215 | |
| 216 | /* set the expected errnos... */ |
| 217 | TEST_EXP_ENOS(exp_enos); |
| 218 | |
mreed10 | c360edb | 2006-11-03 17:21:43 +0000 | [diff] [blame] | 219 | /* set the HZ from sysconf */ |
| 220 | hz = sysconf(_SC_CLK_TCK); |
| 221 | if (hz == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 222 | tst_brkm(TBROK, NULL, "Failed to read the HZ from sysconf\n"); |
mreed10 | c360edb | 2006-11-03 17:21:43 +0000 | [diff] [blame] | 223 | } |
| 224 | |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 225 | TEST_PAUSE; |
| 226 | |
| 227 | /* Save current parameters in tim_save */ |
| 228 | if ((adjtimex(&tim_save)) == -1) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 229 | tst_brkm(TBROK, NULL, "Failed to save current parameters"); |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 230 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 231 | } |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 232 | |
| 233 | /* |
| 234 | *cleanup() - performs all ONE TIME cleanup for this test at |
| 235 | * completion or premature exit. |
| 236 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 237 | void cleanup() |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 238 | { |
| 239 | |
| 240 | tim_save.modes = SET_MODE; |
| 241 | /* Restore saved parameters */ |
| 242 | if ((adjtimex(&tim_save)) == -1) { |
| 243 | tst_resm(TWARN, "Failed to restore saved parameters"); |
| 244 | } |
| 245 | /* |
| 246 | * print timing stats if that option was specified. |
| 247 | * print errno log if that option was specified. |
| 248 | */ |
| 249 | TEST_CLEANUP; |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 250 | } |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 251 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 252 | int setup2() |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 253 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 254 | buff.tick = 900000 / hz - 1; |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 258 | int setup3() |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 259 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 260 | buff.tick = 1100000 / hz + 1; |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 261 | return 0; |
| 262 | } |
| 263 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 264 | int setup4() |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 265 | { |
| 266 | buff.offset = 512000L + 1; |
| 267 | return 0; |
| 268 | } |
| 269 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 270 | int setup5() |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 271 | { |
| 272 | buff.offset = (-1) * (512000L) - 1; |
| 273 | return 0; |
| 274 | } |
| 275 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 276 | int setup6() |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 277 | { |
| 278 | /* Switch to nobody user for correct error code collection */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 279 | if ((ltpuser = getpwnam(nobody_uid)) == NULL) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 280 | tst_brkm(TBROK, NULL, "\"nobody\" user not present"); |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 281 | } |
| 282 | if (seteuid(ltpuser->pw_uid) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 283 | tst_resm(TWARN | TERRNO, "seteuid(%d) failed", ltpuser->pw_uid); |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 284 | return 1; |
| 285 | } |
| 286 | return 0; |
| 287 | } |
| 288 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 289 | void cleanup6() |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 290 | { |
| 291 | /* Set effective user id back to root */ |
| 292 | if (seteuid(0) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 293 | tst_brkm(TBROK | TERRNO, cleanup, "seteuid failed to set the" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 294 | " effective uid to root"); |
robbiew | ca765f1 | 2002-12-24 16:56:24 +0000 | [diff] [blame] | 295 | } |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 296 | } |