blob: 9501191fc2491206cf2dd1dae7c40ba082465846 [file] [log] [blame]
robbiewca765f12002-12-24 16:56:24 +00001/*
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 Gaofed96412012-10-24 10:10:29 +080013 * with this program; if not, write the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
robbiewca765f12002-12-24 16:56:24 +000015 *
16 */
17/**********************************************************
subrata_modak4bb656a2009-02-26 12:02:09 +000018 *
robbiewca765f12002-12-24 16:56:24 +000019 * TEST IDENTIFIER : adjtimex02
subrata_modak4bb656a2009-02-26 12:02:09 +000020 *
robbiewca765f12002-12-24 16:56:24 +000021 * EXECUTED BY : root / superuser
subrata_modak4bb656a2009-02-26 12:02:09 +000022 *
robbiewca765f12002-12-24 16:56:24 +000023 * TEST TITLE : Tests for error conditions
subrata_modak4bb656a2009-02-26 12:02:09 +000024 *
robbiewca765f12002-12-24 16:56:24 +000025 * TEST CASE TOTAL : 6
subrata_modak4bb656a2009-02-26 12:02:09 +000026 *
robbiewca765f12002-12-24 16:56:24 +000027 * AUTHOR : Saji Kumar.V.R <saji.kumar@wipro.com>
subrata_modak4bb656a2009-02-26 12:02:09 +000028 *
robbiewca765f12002-12-24 16:56:24 +000029 * 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_modak69bc3dc2008-05-23 08:36:04 +000043 * (This test case will be executed only if the kernel version
44 * is 2.6.25 or below)
robbiewca765f12002-12-24 16:56:24 +000045 * 5) adjtimex(2) fails with errno set to EINVAL if an attempt
46 * is made to set buf.offset to a value < 512000L
subrata_modak69bc3dc2008-05-23 08:36:04 +000047 * (This test case will be executed only if the kernel version
48 * is 2.6.25 or below)
robbiewca765f12002-12-24 16:56:24 +000049 * 6) adjtimex(2) fails with errno set to EPERM if buf.mode is
50 * non-zero and the user is not super-user.
subrata_modak4bb656a2009-02-26 12:02:09 +000051 *
robbiewca765f12002-12-24 16:56:24 +000052 * Setup:
53 * Setup signal handling.
54 * Pause for SIGUSR1 if option specified.
55 * Save current parameters in tim_save
subrata_modak4bb656a2009-02-26 12:02:09 +000056 *
robbiewca765f12002-12-24 16:56:24 +000057 * 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_modak4bb656a2009-02-26 12:02:09 +000066 *
robbiewca765f12002-12-24 16:56:24 +000067 * Cleanup:
68 * Print errno log and/or timing stats if options given
subrata_modak4bb656a2009-02-26 12:02:09 +000069 *
robbiewca765f12002-12-24 16:56:24 +000070 * 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
robbiewd34d5812005-07-11 22:28:09 +000084#if defined UCLINUX && !__THROW
85/* workaround for libc bug causing failure in sys/timex.h */
86#define __THROW
87#endif
88
robbiewca765f12002-12-24 16:56:24 +000089#include <errno.h>
90#include <sys/timex.h>
mreed10c360edb2006-11-03 17:21:43 +000091#include <unistd.h>
subrata_modak4bb656a2009-02-26 12:02:09 +000092#include <pwd.h>
robbiewca765f12002-12-24 16:56:24 +000093#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
99static void setup();
100static int setup2();
101static int setup3();
102static int setup4();
103static int setup5();
104static int setup6();
105static void cleanup();
106static void cleanup6();
107
108char *TCID = "adjtimex02"; /* Test program identifier. */
robbiewca765f12002-12-24 16:56:24 +0000109
mreed10c360edb2006-11-03 17:21:43 +0000110static int hz; /* HZ from sysconf */
111
robbiewca765f12002-12-24 16:56:24 +0000112static struct timex tim_save;
113static struct timex buff;
114static int exp_enos[] = { EPERM, EINVAL, EFAULT, 0 };
Wanlong Gao354ebb42012-12-07 10:10:04 +0800115
robbiewca765f12002-12-24 16:56:24 +0000116static char nobody_uid[] = "nobody";
117struct passwd *ltpuser;
118
119struct test_cases_t {
120 struct timex *buffp;
subrata_modak56207ce2009-03-23 13:35:39 +0000121 int (*setup) ();
122 void (*cleanup) ();
robbiewca765f12002-12-24 16:56:24 +0000123 int exp_errno;
124} test_cases[] = {
vapier7ec19d92006-02-27 04:38:56 +0000125#ifndef UCLINUX
126 /* Skip since uClinux does not implement memory protection */
subrata_modak56207ce2009-03-23 13:35:39 +0000127 {
128 (struct timex *)-1, NULL, NULL, EFAULT},
vapier7ec19d92006-02-27 04:38:56 +0000129#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000130 {
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}
robbiewca765f12002-12-24 16:56:24 +0000136};
137
138int TST_TOTAL = sizeof(test_cases) / sizeof(test_cases[0]);
139
subrata_modak56207ce2009-03-23 13:35:39 +0000140int main(int ac, char **av)
robbiewca765f12002-12-24 16:56:24 +0000141{
142
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200143 int lc, i;
144 char *msg;
robbiewca765f12002-12-24 16:56:24 +0000145
Garrett Cooper53740502010-12-16 00:04:01 -0800146 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800147 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewca765f12002-12-24 16:56:24 +0000148
robbiewca765f12002-12-24 16:56:24 +0000149 setup();
150
robbiewca765f12002-12-24 16:56:24 +0000151 for (lc = 0; TEST_LOOPING(lc); lc++) {
152
robbiewca765f12002-12-24 16:56:24 +0000153 Tst_count = 0;
154
subrata_modak56207ce2009-03-23 13:35:39 +0000155 for (i = 0; i < TST_TOTAL; ++i) {
subrata_modak69bc3dc2008-05-23 08:36:04 +0000156 /*
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_modak4bb656a2009-02-26 12:02:09 +0000160 * case.
subrata_modak69bc3dc2008-05-23 08:36:04 +0000161 */
subrata_modak56207ce2009-03-23 13:35:39 +0000162 if ((i == 3 || i == 4) && tst_kvercmp(2, 6, 25) > 0) {
subrata_modak69bc3dc2008-05-23 08:36:04 +0000163 tst_resm(TCONF, "this kernel normalizes buf."
subrata_modak56207ce2009-03-23 13:35:39 +0000164 "offset value if it is outside"
165 " the acceptable range.");
subrata_modak69bc3dc2008-05-23 08:36:04 +0000166 continue;
167 }
robbiewca765f12002-12-24 16:56:24 +0000168
169 buff = tim_save;
170 buff.modes = SET_MODE;
subrata_modak56207ce2009-03-23 13:35:39 +0000171 if ((test_cases[i].setup) && (test_cases[i].setup())) {
robbiewca765f12002-12-24 16:56:24 +0000172 tst_resm(TWARN, "setup() failed, skipping"
subrata_modak56207ce2009-03-23 13:35:39 +0000173 " this test case");
robbiewca765f12002-12-24 16:56:24 +0000174 continue;
175 }
176
177 /* Call adjtimex(2) */
178 TEST(adjtimex(test_cases[i].buffp));
179
subrata_modak56207ce2009-03-23 13:35:39 +0000180 if ((TEST_RETURN == -1) && (TEST_ERRNO ==
181 test_cases[i].exp_errno)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800182 tst_resm(TPASS | TTERRNO,
vapier8dc646d2009-08-28 10:54:04 +0000183 "Test Passed, adjtimex() returned -1");
robbiewca765f12002-12-24 16:56:24 +0000184 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800185 tst_resm(TFAIL | TTERRNO,
186 "Test Failed, adjtimex() returned %ld",
187 TEST_RETURN);
robbiewca765f12002-12-24 16:56:24 +0000188 }
189 TEST_ERROR_LOG(TEST_ERRNO);
190 if (test_cases[i].cleanup) {
191 test_cases[i].cleanup();
192 }
subrata_modak4bb656a2009-02-26 12:02:09 +0000193 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800194 }
robbiewca765f12002-12-24 16:56:24 +0000195
196 /* cleanup and exit */
197 cleanup();
198
Garrett Cooper53740502010-12-16 00:04:01 -0800199 tst_exit();
robbiewca765f12002-12-24 16:56:24 +0000200
Garrett Cooper2c282152010-12-16 00:55:50 -0800201}
robbiewca765f12002-12-24 16:56:24 +0000202
203/* setup() - performs all ONE TIME setup for this test */
subrata_modak56207ce2009-03-23 13:35:39 +0000204void setup()
robbiewca765f12002-12-24 16:56:24 +0000205{
206
207 tim_save.modes = 0;
208
209 /* Check whether we are root */
210 if (geteuid() != 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800211 tst_brkm(TBROK, NULL, "Test must be run as root");
robbiewca765f12002-12-24 16:56:24 +0000212 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000213
robbiewca765f12002-12-24 16:56:24 +0000214 tst_sig(NOFORK, DEF_HANDLER, cleanup);
215
216 /* set the expected errnos... */
217 TEST_EXP_ENOS(exp_enos);
218
mreed10c360edb2006-11-03 17:21:43 +0000219 /* set the HZ from sysconf */
220 hz = sysconf(_SC_CLK_TCK);
221 if (hz == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800222 tst_brkm(TBROK, NULL, "Failed to read the HZ from sysconf\n");
mreed10c360edb2006-11-03 17:21:43 +0000223 }
224
robbiewca765f12002-12-24 16:56:24 +0000225 TEST_PAUSE;
226
227 /* Save current parameters in tim_save */
228 if ((adjtimex(&tim_save)) == -1) {
Garrett Cooper53740502010-12-16 00:04:01 -0800229 tst_brkm(TBROK, NULL, "Failed to save current parameters");
robbiewca765f12002-12-24 16:56:24 +0000230 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800231}
robbiewca765f12002-12-24 16:56:24 +0000232
233/*
234 *cleanup() - performs all ONE TIME cleanup for this test at
235 * completion or premature exit.
236 */
subrata_modak56207ce2009-03-23 13:35:39 +0000237void cleanup()
robbiewca765f12002-12-24 16:56:24 +0000238{
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 Cooper2c282152010-12-16 00:55:50 -0800250}
robbiewca765f12002-12-24 16:56:24 +0000251
subrata_modak56207ce2009-03-23 13:35:39 +0000252int setup2()
robbiewca765f12002-12-24 16:56:24 +0000253{
subrata_modak56207ce2009-03-23 13:35:39 +0000254 buff.tick = 900000 / hz - 1;
robbiewca765f12002-12-24 16:56:24 +0000255 return 0;
256}
257
subrata_modak56207ce2009-03-23 13:35:39 +0000258int setup3()
robbiewca765f12002-12-24 16:56:24 +0000259{
subrata_modak56207ce2009-03-23 13:35:39 +0000260 buff.tick = 1100000 / hz + 1;
robbiewca765f12002-12-24 16:56:24 +0000261 return 0;
262}
263
subrata_modak56207ce2009-03-23 13:35:39 +0000264int setup4()
robbiewca765f12002-12-24 16:56:24 +0000265{
266 buff.offset = 512000L + 1;
267 return 0;
268}
269
subrata_modak56207ce2009-03-23 13:35:39 +0000270int setup5()
robbiewca765f12002-12-24 16:56:24 +0000271{
272 buff.offset = (-1) * (512000L) - 1;
273 return 0;
274}
275
subrata_modak56207ce2009-03-23 13:35:39 +0000276int setup6()
robbiewca765f12002-12-24 16:56:24 +0000277{
278 /* Switch to nobody user for correct error code collection */
subrata_modak56207ce2009-03-23 13:35:39 +0000279 if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -0800280 tst_brkm(TBROK, NULL, "\"nobody\" user not present");
robbiewca765f12002-12-24 16:56:24 +0000281 }
282 if (seteuid(ltpuser->pw_uid) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800283 tst_resm(TWARN | TERRNO, "seteuid(%d) failed", ltpuser->pw_uid);
robbiewca765f12002-12-24 16:56:24 +0000284 return 1;
285 }
286 return 0;
287}
288
subrata_modak56207ce2009-03-23 13:35:39 +0000289void cleanup6()
robbiewca765f12002-12-24 16:56:24 +0000290{
291 /* Set effective user id back to root */
292 if (seteuid(0) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800293 tst_brkm(TBROK | TERRNO, cleanup, "seteuid failed to set the"
subrata_modak56207ce2009-03-23 13:35:39 +0000294 " effective uid to root");
robbiewca765f12002-12-24 16:56:24 +0000295 }
Chris Dearmanec6edca2012-10-17 19:54:01 -0700296}