1 /* 2 * Copyright (c) Wipro Technologies Ltd, 2003. All Rights Reserved. 3 * Copyright (c) 2011 Cyril Hrubis <chrubis (at) suse.cz> 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 14 * with this program; if not, write the Free Software Foundation, Inc., 15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 16 * 17 */ 18 19 #include <errno.h> 20 #include <time.h> 21 #include <pwd.h> 22 #include <unistd.h> 23 24 #include "test.h" 25 #include "safe_macros.h" 26 #include "common_timers.h" 27 28 static void setup(void); 29 static void cleanup(void); 30 static int setup_test(int option); 31 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 42 }; 43 44 int testcases[] = { 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 */ 52 EINVAL, /* PROCESS_CPUTIME_ID */ 53 EINVAL, /* THREAD_CPUTIME_ID */ 54 }; 55 56 char *TCID = "clock_settime03"; 57 int TST_TOTAL = ARRAY_SIZE(testcases); 58 59 char nobody_uid[] = "nobody"; 60 struct passwd *ltpuser; 61 static struct timespec spec, *temp, saved; 62 63 int main(int ac, char **av) 64 { 65 int lc, i; 66 67 tst_parse_opts(ac, av, NULL, NULL); 68 69 setup(); 70 71 for (lc = 0; TEST_LOOPING(lc); lc++) { 72 73 tst_count = 0; 74 75 for (i = 0; i < TST_TOTAL; i++) { 76 77 if (setup_test(i) < 0) 78 continue; 79 80 TEST(ltp_syscall(__NR_clock_settime, clocks[i], temp)); 81 82 /* Change the UID back to root */ 83 if (i == TST_TOTAL - 1) { 84 SAFE_SETEUID(cleanup, 0); 85 } 86 87 /* check return code */ 88 if (TEST_RETURN == -1 && TEST_ERRNO == testcases[i]) { 89 tst_resm(TPASS | TTERRNO, 90 "clock_settime(2) got expected " 91 "failure."); 92 } else { 93 tst_resm(TFAIL | TTERRNO, 94 "clock_settime(2) failed to produce " 95 "expected error (return code = %ld)", 96 TEST_RETURN); 97 /* Restore the clock to its previous state. */ 98 if (TEST_RETURN == 0) { 99 if (ltp_syscall(__NR_clock_settime, 100 CLOCK_REALTIME, 101 &saved) < 0) { 102 tst_resm(TWARN | TERRNO, 103 "FATAL: could not set " 104 "the clock!"); 105 } 106 } 107 } 108 109 } 110 111 } 112 113 cleanup(); 114 tst_exit(); 115 } 116 117 static int setup_test(int option) 118 { 119 /* valid timespec */ 120 spec = saved; 121 temp = &spec; 122 123 /* error sceanrios */ 124 switch (option) { 125 case 0: 126 /* Make tp argument bad pointer */ 127 temp = (struct timespec *)-1; 128 break; 129 case 4: 130 /* Make the parameter of timespec invalid */ 131 spec.tv_nsec = -1; 132 break; 133 case 5: 134 /* Make the parameter of timespec invalid */ 135 spec.tv_nsec = NSEC_PER_SEC + 1; 136 break; 137 case 6: 138 /* change the User to non-root */ 139 spec.tv_nsec = 0; 140 if ((ltpuser = getpwnam(nobody_uid)) == NULL) { 141 tst_resm(TWARN, "user \"nobody\" not present; " 142 "skipping test"); 143 return -1; 144 } 145 if (seteuid(ltpuser->pw_uid) == -1) { 146 tst_resm(TWARN | TERRNO, 147 "seteuid failed to set the effective " 148 "uid to %d (nobody)", ltpuser->pw_uid); 149 return -1; 150 } 151 break; 152 } 153 return 0; 154 } 155 156 static void setup(void) 157 { 158 tst_sig(NOFORK, DEF_HANDLER, cleanup); 159 160 tst_require_root(); 161 162 if (ltp_syscall(__NR_clock_gettime, CLOCK_REALTIME, &saved) < 0) 163 tst_brkm(TBROK, NULL, "Clock gettime failed"); 164 165 spec.tv_sec = 1; 166 spec.tv_nsec = 0; 167 168 TEST_PAUSE; 169 } 170 171 static void cleanup(void) 172 { 173 } 174