blob: 6ae0389ab2bd3e22c2e86544b08469f3355d9a15 [file] [log] [blame]
vapierd13d74b2006-02-11 07:24:00 +00001/*
2 * Copyright (c) Wipro Technologies Ltd, 2003. All Rights Reserved.
Cyril Hrubisb7a95042011-10-27 14:39:09 +02003 * Copyright (c) 2011 Cyril Hrubis <chrubis@suse.cz>
vapierd13d74b2006-02-11 07:24:00 +00004 *
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 Gaofed96412012-10-24 10:10:29 +080014 * with this program; if not, write the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
vapierd13d74b2006-02-11 07:24:00 +000016 *
17 */
vapierd13d74b2006-02-11 07:24:00 +000018
vapierd13d74b2006-02-11 07:24:00 +000019#include <errno.h>
vapierd13d74b2006-02-11 07:24:00 +000020#include <time.h>
21#include <pwd.h>
vapierbc8a4612006-08-22 05:43:15 +000022#include <unistd.h>
vapierd13d74b2006-02-11 07:24:00 +000023
vapierbc8a4612006-08-22 05:43:15 +000024#include "test.h"
25#include "usctest.h"
26#include "common_timers.h"
vapierd13d74b2006-02-11 07:24:00 +000027
Cyril Hrubisb7a95042011-10-27 14:39:09 +020028static void setup(void);
29static void cleanup(void);
vapierd13d74b2006-02-11 07:24:00 +000030static int setup_test(int option);
31
subrata_modake8af9782008-02-26 11:14:44 +000032clockid_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
vapierd13d74b2006-02-11 07:24:00 +000042};
43
Cyril Hrubisb7a95042011-10-27 14:39:09 +020044int testcases[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +080045 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 Hrubisb7a95042011-10-27 14:39:09 +020052 0,
53 0,
vapierd13d74b2006-02-11 07:24:00 +000054};
55
Cyril Hrubisb7a95042011-10-27 14:39:09 +020056char *TCID = "clock_settime03";
57int TST_TOTAL = sizeof(testcases) / sizeof(*testcases);
vapierd13d74b2006-02-11 07:24:00 +000058
Cyril Hrubisb7a95042011-10-27 14:39:09 +020059static int exp_enos[] = { EINVAL, EFAULT, EPERM, 0 };
Wanlong Gao354ebb42012-12-07 10:10:04 +080060
Cyril Hrubisb7a95042011-10-27 14:39:09 +020061char nobody_uid[] = "nobody";
62struct passwd *ltpuser;
63static struct timespec spec, *temp, saved;
64
65int main(int ac, char **av)
66{
67 int lc, i;
68 char *msg;
69
Garrett Coopera9670cd2010-11-28 21:55:20 -080070 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
71 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
vapierd13d74b2006-02-11 07:24:00 +000072
yaberauneya261d0fb2009-12-01 04:23:58 +000073 /* PROCESS_CPUTIME_ID & THREAD_CPUTIME_ID are not supported on
Cyril Hrubisb7a95042011-10-27 14:39:09 +020074 * kernel versions lower than 2.6.12 and changed back in 2.6.38
yaberauneya261d0fb2009-12-01 04:23:58 +000075 */
Wanlong Gaoec544ef2011-10-26 17:25:06 +080076 if ((tst_kvercmp(2, 6, 12)) < 0 || (tst_kvercmp(2, 6, 38)) >= 0) {
Cyril Hrubisb7a95042011-10-27 14:39:09 +020077 testcases[7] = EINVAL;
78 testcases[8] = EINVAL;
yaberauneya1a7f5422009-12-06 20:53:42 +000079 } else {
Cyril Hrubisb7a95042011-10-27 14:39:09 +020080 testcases[7] = EFAULT;
81 testcases[8] = EFAULT;
yaberauneya261d0fb2009-12-01 04:23:58 +000082 }
subrata_modake8af9782008-02-26 11:14:44 +000083
vapierd13d74b2006-02-11 07:24:00 +000084 setup();
85
vapierd13d74b2006-02-11 07:24:00 +000086 for (lc = 0; TEST_LOOPING(lc); lc++) {
87
vapierd13d74b2006-02-11 07:24:00 +000088 Tst_count = 0;
89
90 for (i = 0; i < TST_TOTAL; i++) {
91
Cyril Hrubisb7a95042011-10-27 14:39:09 +020092 if (setup_test(i) < 0)
vapierd13d74b2006-02-11 07:24:00 +000093 continue;
vapierd13d74b2006-02-11 07:24:00 +000094
Jan Stancek359980f2013-02-15 10:16:05 +010095 TEST(ltp_syscall(__NR_clock_settime, clocks[i], temp));
vapierd13d74b2006-02-11 07:24:00 +000096
97 /* Change the UID back to root */
98 if (i == TST_TOTAL - 1) {
99 if (seteuid(0) == -1) {
yaberauneya1a7f5422009-12-06 20:53:42 +0000100 tst_brkm(TBROK | TERRNO, cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800101 "Failed to set the effective "
102 "uid to root");
vapierd13d74b2006-02-11 07:24:00 +0000103 }
104 }
105
106 /* check return code */
Cyril Hrubisb7a95042011-10-27 14:39:09 +0200107 if (TEST_RETURN == -1 && TEST_ERRNO == testcases[i]) {
yaberauneya261d0fb2009-12-01 04:23:58 +0000108 tst_resm(TPASS | TTERRNO,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800109 "clock_settime(2) got expected "
110 "failure.");
vapierd13d74b2006-02-11 07:24:00 +0000111 } else {
yaberauneya261d0fb2009-12-01 04:23:58 +0000112 tst_resm(TFAIL | TTERRNO,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800113 "clock_settime(2) failed to produce "
114 "expected error (return code = %ld)",
115 TEST_RETURN);
yaberauneya1a7f5422009-12-06 20:53:42 +0000116 /* Restore the clock to its previous state. */
vapierd13d74b2006-02-11 07:24:00 +0000117 if (TEST_RETURN == 0) {
Jan Stancek359980f2013-02-15 10:16:05 +0100118 if (ltp_syscall(__NR_clock_settime,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800119 CLOCK_REALTIME,
120 &saved) < 0) {
yaberauneya1a7f5422009-12-06 20:53:42 +0000121 tst_resm(TWARN | TERRNO,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800122 "FATAL: could not set "
123 "the clock!");
vapierd13d74b2006-02-11 07:24:00 +0000124 }
125 }
Cyril Hrubisb7a95042011-10-27 14:39:09 +0200126 }
vapierd13d74b2006-02-11 07:24:00 +0000127
Garrett Cooper2c282152010-12-16 00:55:50 -0800128 }
yaberauneya1a7f5422009-12-06 20:53:42 +0000129
Garrett Cooper2c282152010-12-16 00:55:50 -0800130 }
vapierd13d74b2006-02-11 07:24:00 +0000131
vapierd13d74b2006-02-11 07:24:00 +0000132 cleanup();
yaberauneya1a7f5422009-12-06 20:53:42 +0000133 tst_exit();
vapierd13d74b2006-02-11 07:24:00 +0000134}
135
Cyril Hrubisb7a95042011-10-27 14:39:09 +0200136static int setup_test(int option)
vapierd13d74b2006-02-11 07:24:00 +0000137{
subrata_modake8af9782008-02-26 11:14:44 +0000138 /* valid timespec */
139 spec = saved;
140 temp = &spec;
vapierd13d74b2006-02-11 07:24:00 +0000141
subrata_modake8af9782008-02-26 11:14:44 +0000142 /* error sceanrios */
vapierd13d74b2006-02-11 07:24:00 +0000143 switch (option) {
yaberauneya1a7f5422009-12-06 20:53:42 +0000144 case 0:
145 /* Make tp argument bad pointer */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800146 temp = (struct timespec *)-1;
yaberauneya1a7f5422009-12-06 20:53:42 +0000147 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 Coopera9670cd2010-11-28 21:55:20 -0800160 tst_resm(TWARN, "user \"nobody\" not present; "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800161 "skipping test");
yaberauneya1a7f5422009-12-06 20:53:42 +0000162 return -1;
163 }
164 if (seteuid(ltpuser->pw_uid) == -1) {
165 tst_resm(TWARN | TERRNO,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800166 "seteuid failed to set the effective "
167 "uid to %d (nobody)", ltpuser->pw_uid);
yaberauneya1a7f5422009-12-06 20:53:42 +0000168 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 Gao354ebb42012-12-07 10:10:04 +0800175 temp = (struct timespec *)-1;
vapierd13d74b2006-02-11 07:24:00 +0000176 }
177 return 0;
178}
179
Cyril Hrubisb7a95042011-10-27 14:39:09 +0200180static void setup(void)
vapierd13d74b2006-02-11 07:24:00 +0000181{
vapierd13d74b2006-02-11 07:24:00 +0000182 tst_sig(NOFORK, DEF_HANDLER, cleanup);
183
Cyril Hrubisb7a95042011-10-27 14:39:09 +0200184 tst_require_root(NULL);
vapierd13d74b2006-02-11 07:24:00 +0000185
Jan Stancek359980f2013-02-15 10:16:05 +0100186 if (ltp_syscall(__NR_clock_gettime, CLOCK_REALTIME, &saved) < 0)
Garrett Cooper53740502010-12-16 00:04:01 -0800187 tst_brkm(TBROK, NULL, "Clock gettime failed");
vapierd13d74b2006-02-11 07:24:00 +0000188
vapierd13d74b2006-02-11 07:24:00 +0000189 TEST_EXP_ENOS(exp_enos);
190 spec.tv_sec = 1;
191 spec.tv_nsec = 0;
192
vapierd13d74b2006-02-11 07:24:00 +0000193 TEST_PAUSE;
Garrett Coopera9670cd2010-11-28 21:55:20 -0800194}
vapierd13d74b2006-02-11 07:24:00 +0000195
Cyril Hrubisb7a95042011-10-27 14:39:09 +0200196static void cleanup(void)
vapierd13d74b2006-02-11 07:24:00 +0000197{
vapierd13d74b2006-02-11 07:24:00 +0000198 TEST_CLEANUP;
Wanlong Gaoec544ef2011-10-26 17:25:06 +0800199}