vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) Wipro Technologies Ltd, 2003. All Rights Reserved. |
Cyril Hrubis | b7a9504 | 2011-10-27 14:39:09 +0200 | [diff] [blame] | 3 | * Copyright (c) 2011 Cyril Hrubis <chrubis@suse.cz> |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of version 2 of the GNU General Public License as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it would be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | * |
| 13 | * 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] | 14 | * with this program; if not, write the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 16 | * |
| 17 | */ |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 18 | |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 19 | #include <errno.h> |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 20 | #include <time.h> |
| 21 | #include <pwd.h> |
vapier | bc8a461 | 2006-08-22 05:43:15 +0000 | [diff] [blame] | 22 | #include <unistd.h> |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 23 | |
vapier | bc8a461 | 2006-08-22 05:43:15 +0000 | [diff] [blame] | 24 | #include "test.h" |
| 25 | #include "usctest.h" |
| 26 | #include "common_timers.h" |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 27 | |
Cyril Hrubis | b7a9504 | 2011-10-27 14:39:09 +0200 | [diff] [blame] | 28 | static void setup(void); |
| 29 | static void cleanup(void); |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 30 | static int setup_test(int option); |
| 31 | |
subrata_modak | e8af978 | 2008-02-26 11:14:44 +0000 | [diff] [blame] | 32 | clockid_t clocks[] = { |
| 33 | CLOCK_REALTIME, |
| 34 | CLOCK_MONOTONIC, |
| 35 | MAX_CLOCKS, |
| 36 | MAX_CLOCKS + 1, |
| 37 | CLOCK_REALTIME, |
| 38 | CLOCK_REALTIME, |
| 39 | CLOCK_REALTIME, |
| 40 | CLOCK_PROCESS_CPUTIME_ID, |
| 41 | CLOCK_THREAD_CPUTIME_ID |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
Cyril Hrubis | b7a9504 | 2011-10-27 14:39:09 +0200 | [diff] [blame] | 44 | int testcases[] = { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 45 | EFAULT, /* tp bad */ |
| 46 | EINVAL, /* CLOCK_MONOTONIC */ |
| 47 | EINVAL, /* MAX_CLOCKS */ |
| 48 | EINVAL, /* MAX_CLOCKS + 1 */ |
| 49 | EINVAL, /* Invalid timespec */ |
| 50 | EINVAL, /* NSEC_PER_SEC + 1 */ |
| 51 | EPERM, /* non-root user */ |
Cyril Hrubis | b7a9504 | 2011-10-27 14:39:09 +0200 | [diff] [blame] | 52 | 0, |
| 53 | 0, |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
Cyril Hrubis | b7a9504 | 2011-10-27 14:39:09 +0200 | [diff] [blame] | 56 | char *TCID = "clock_settime03"; |
| 57 | int TST_TOTAL = sizeof(testcases) / sizeof(*testcases); |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 58 | |
Cyril Hrubis | b7a9504 | 2011-10-27 14:39:09 +0200 | [diff] [blame] | 59 | static int exp_enos[] = { EINVAL, EFAULT, EPERM, 0 }; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 60 | |
Cyril Hrubis | b7a9504 | 2011-10-27 14:39:09 +0200 | [diff] [blame] | 61 | char nobody_uid[] = "nobody"; |
| 62 | struct passwd *ltpuser; |
| 63 | static struct timespec spec, *temp, saved; |
| 64 | |
| 65 | int main(int ac, char **av) |
| 66 | { |
| 67 | int lc, i; |
| 68 | char *msg; |
| 69 | |
Garrett Cooper | a9670cd | 2010-11-28 21:55:20 -0800 | [diff] [blame] | 70 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
| 71 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 72 | |
yaberauneya | 261d0fb | 2009-12-01 04:23:58 +0000 | [diff] [blame] | 73 | /* PROCESS_CPUTIME_ID & THREAD_CPUTIME_ID are not supported on |
Cyril Hrubis | b7a9504 | 2011-10-27 14:39:09 +0200 | [diff] [blame] | 74 | * kernel versions lower than 2.6.12 and changed back in 2.6.38 |
yaberauneya | 261d0fb | 2009-12-01 04:23:58 +0000 | [diff] [blame] | 75 | */ |
Wanlong Gao | ec544ef | 2011-10-26 17:25:06 +0800 | [diff] [blame] | 76 | if ((tst_kvercmp(2, 6, 12)) < 0 || (tst_kvercmp(2, 6, 38)) >= 0) { |
Cyril Hrubis | b7a9504 | 2011-10-27 14:39:09 +0200 | [diff] [blame] | 77 | testcases[7] = EINVAL; |
| 78 | testcases[8] = EINVAL; |
yaberauneya | 1a7f542 | 2009-12-06 20:53:42 +0000 | [diff] [blame] | 79 | } else { |
Cyril Hrubis | b7a9504 | 2011-10-27 14:39:09 +0200 | [diff] [blame] | 80 | testcases[7] = EFAULT; |
| 81 | testcases[8] = EFAULT; |
yaberauneya | 261d0fb | 2009-12-01 04:23:58 +0000 | [diff] [blame] | 82 | } |
subrata_modak | e8af978 | 2008-02-26 11:14:44 +0000 | [diff] [blame] | 83 | |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 84 | setup(); |
| 85 | |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 86 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 87 | |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 88 | Tst_count = 0; |
| 89 | |
| 90 | for (i = 0; i < TST_TOTAL; i++) { |
| 91 | |
Cyril Hrubis | b7a9504 | 2011-10-27 14:39:09 +0200 | [diff] [blame] | 92 | if (setup_test(i) < 0) |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 93 | continue; |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 94 | |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 95 | TEST(ltp_syscall(__NR_clock_settime, clocks[i], temp)); |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 96 | |
| 97 | /* Change the UID back to root */ |
| 98 | if (i == TST_TOTAL - 1) { |
| 99 | if (seteuid(0) == -1) { |
yaberauneya | 1a7f542 | 2009-12-06 20:53:42 +0000 | [diff] [blame] | 100 | tst_brkm(TBROK | TERRNO, cleanup, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 101 | "Failed to set the effective " |
| 102 | "uid to root"); |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | /* check return code */ |
Cyril Hrubis | b7a9504 | 2011-10-27 14:39:09 +0200 | [diff] [blame] | 107 | if (TEST_RETURN == -1 && TEST_ERRNO == testcases[i]) { |
yaberauneya | 261d0fb | 2009-12-01 04:23:58 +0000 | [diff] [blame] | 108 | tst_resm(TPASS | TTERRNO, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 109 | "clock_settime(2) got expected " |
| 110 | "failure."); |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 111 | } else { |
yaberauneya | 261d0fb | 2009-12-01 04:23:58 +0000 | [diff] [blame] | 112 | tst_resm(TFAIL | TTERRNO, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 113 | "clock_settime(2) failed to produce " |
| 114 | "expected error (return code = %ld)", |
| 115 | TEST_RETURN); |
yaberauneya | 1a7f542 | 2009-12-06 20:53:42 +0000 | [diff] [blame] | 116 | /* Restore the clock to its previous state. */ |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 117 | if (TEST_RETURN == 0) { |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 118 | if (ltp_syscall(__NR_clock_settime, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 119 | CLOCK_REALTIME, |
| 120 | &saved) < 0) { |
yaberauneya | 1a7f542 | 2009-12-06 20:53:42 +0000 | [diff] [blame] | 121 | tst_resm(TWARN | TERRNO, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 122 | "FATAL: could not set " |
| 123 | "the clock!"); |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
Cyril Hrubis | b7a9504 | 2011-10-27 14:39:09 +0200 | [diff] [blame] | 126 | } |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 127 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 128 | } |
yaberauneya | 1a7f542 | 2009-12-06 20:53:42 +0000 | [diff] [blame] | 129 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 130 | } |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 131 | |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 132 | cleanup(); |
yaberauneya | 1a7f542 | 2009-12-06 20:53:42 +0000 | [diff] [blame] | 133 | tst_exit(); |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Cyril Hrubis | b7a9504 | 2011-10-27 14:39:09 +0200 | [diff] [blame] | 136 | static int setup_test(int option) |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 137 | { |
subrata_modak | e8af978 | 2008-02-26 11:14:44 +0000 | [diff] [blame] | 138 | /* valid timespec */ |
| 139 | spec = saved; |
| 140 | temp = &spec; |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 141 | |
subrata_modak | e8af978 | 2008-02-26 11:14:44 +0000 | [diff] [blame] | 142 | /* error sceanrios */ |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 143 | switch (option) { |
yaberauneya | 1a7f542 | 2009-12-06 20:53:42 +0000 | [diff] [blame] | 144 | case 0: |
| 145 | /* Make tp argument bad pointer */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 146 | temp = (struct timespec *)-1; |
yaberauneya | 1a7f542 | 2009-12-06 20:53:42 +0000 | [diff] [blame] | 147 | break; |
| 148 | case 4: |
| 149 | /* Make the parameter of timespec invalid */ |
| 150 | spec.tv_nsec = -1; |
| 151 | break; |
| 152 | case 5: |
| 153 | /* Make the parameter of timespec invalid */ |
| 154 | spec.tv_nsec = NSEC_PER_SEC + 1; |
| 155 | break; |
| 156 | case 6: |
| 157 | /* change the User to non-root */ |
| 158 | spec.tv_nsec = 0; |
| 159 | if ((ltpuser = getpwnam(nobody_uid)) == NULL) { |
Garrett Cooper | a9670cd | 2010-11-28 21:55:20 -0800 | [diff] [blame] | 160 | tst_resm(TWARN, "user \"nobody\" not present; " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 161 | "skipping test"); |
yaberauneya | 1a7f542 | 2009-12-06 20:53:42 +0000 | [diff] [blame] | 162 | return -1; |
| 163 | } |
| 164 | if (seteuid(ltpuser->pw_uid) == -1) { |
| 165 | tst_resm(TWARN | TERRNO, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 166 | "seteuid failed to set the effective " |
| 167 | "uid to %d (nobody)", ltpuser->pw_uid); |
yaberauneya | 1a7f542 | 2009-12-06 20:53:42 +0000 | [diff] [blame] | 168 | return -1; |
| 169 | } |
| 170 | break; |
| 171 | case 7: |
| 172 | case 8: |
| 173 | /* Make tp argument bad pointer */ |
| 174 | if (tst_kvercmp(2, 6, 12) >= 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 175 | temp = (struct timespec *)-1; |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 176 | } |
| 177 | return 0; |
| 178 | } |
| 179 | |
Cyril Hrubis | b7a9504 | 2011-10-27 14:39:09 +0200 | [diff] [blame] | 180 | static void setup(void) |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 181 | { |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 182 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 183 | |
Cyril Hrubis | b7a9504 | 2011-10-27 14:39:09 +0200 | [diff] [blame] | 184 | tst_require_root(NULL); |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 185 | |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 186 | if (ltp_syscall(__NR_clock_gettime, CLOCK_REALTIME, &saved) < 0) |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 187 | tst_brkm(TBROK, NULL, "Clock gettime failed"); |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 188 | |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 189 | TEST_EXP_ENOS(exp_enos); |
| 190 | spec.tv_sec = 1; |
| 191 | spec.tv_nsec = 0; |
| 192 | |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 193 | TEST_PAUSE; |
Garrett Cooper | a9670cd | 2010-11-28 21:55:20 -0800 | [diff] [blame] | 194 | } |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 195 | |
Cyril Hrubis | b7a9504 | 2011-10-27 14:39:09 +0200 | [diff] [blame] | 196 | static void cleanup(void) |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 197 | { |
vapier | d13d74b | 2006-02-11 07:24:00 +0000 | [diff] [blame] | 198 | TEST_CLEANUP; |
Wanlong Gao | ec544ef | 2011-10-26 17:25:06 +0800 | [diff] [blame] | 199 | } |