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