blob: be601520cff7feae225e033686974ab08926734b [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"
robbiewca765f12002-12-24 16:56:24 +000094
95#define SET_MODE ( ADJ_OFFSET | ADJ_FREQUENCY | ADJ_MAXERROR | ADJ_ESTERROR | \
96 ADJ_STATUS | ADJ_TIMECONST | ADJ_TICK )
97
Mike Frysingerc57fba52014-04-09 18:56:30 -040098static void setup(void);
99static int setup2(void);
100static int setup3(void);
101static int setup4(void);
102static int setup5(void);
103static int setup6(void);
104static void cleanup(void);
105static void cleanup6(void);
robbiewca765f12002-12-24 16:56:24 +0000106
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200107char *TCID = "adjtimex02";
robbiewca765f12002-12-24 16:56:24 +0000108
mreed10c360edb2006-11-03 17:21:43 +0000109static int hz; /* HZ from sysconf */
110
robbiewca765f12002-12-24 16:56:24 +0000111static struct timex tim_save;
112static struct timex buff;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800113
robbiewca765f12002-12-24 16:56:24 +0000114static char nobody_uid[] = "nobody";
115struct passwd *ltpuser;
116
117struct test_cases_t {
118 struct timex *buffp;
subrata_modak56207ce2009-03-23 13:35:39 +0000119 int (*setup) ();
120 void (*cleanup) ();
robbiewca765f12002-12-24 16:56:24 +0000121 int exp_errno;
122} test_cases[] = {
vapier7ec19d92006-02-27 04:38:56 +0000123#ifndef UCLINUX
124 /* Skip since uClinux does not implement memory protection */
subrata_modak56207ce2009-03-23 13:35:39 +0000125 {
126 (struct timex *)-1, NULL, NULL, EFAULT},
vapier7ec19d92006-02-27 04:38:56 +0000127#endif
subrata_modak56207ce2009-03-23 13:35:39 +0000128 {
129 &buff, setup2, NULL, EINVAL}, {
130 &buff, setup3, NULL, EINVAL}, {
131 &buff, setup4, NULL, EINVAL}, {
132 &buff, setup5, NULL, EINVAL}, {
133 &buff, setup6, cleanup6, EPERM}
robbiewca765f12002-12-24 16:56:24 +0000134};
135
136int TST_TOTAL = sizeof(test_cases) / sizeof(test_cases[0]);
137
subrata_modak56207ce2009-03-23 13:35:39 +0000138int main(int ac, char **av)
robbiewca765f12002-12-24 16:56:24 +0000139{
140
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200141 int lc, i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200142 const char *msg;
robbiewca765f12002-12-24 16:56:24 +0000143
Garrett Cooper53740502010-12-16 00:04:01 -0800144 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800145 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewca765f12002-12-24 16:56:24 +0000146
robbiewca765f12002-12-24 16:56:24 +0000147 setup();
148
robbiewca765f12002-12-24 16:56:24 +0000149 for (lc = 0; TEST_LOOPING(lc); lc++) {
150
Caspar Zhangd59a6592013-03-07 14:59:12 +0800151 tst_count = 0;
robbiewca765f12002-12-24 16:56:24 +0000152
subrata_modak56207ce2009-03-23 13:35:39 +0000153 for (i = 0; i < TST_TOTAL; ++i) {
subrata_modak69bc3dc2008-05-23 08:36:04 +0000154 /*
155 * since Linux 2.6.26, if buf.offset value is outside
156 * the acceptable range, it is simply normalized instead
157 * of letting the syscall fail. so just skip this test
subrata_modak4bb656a2009-02-26 12:02:09 +0000158 * case.
subrata_modak69bc3dc2008-05-23 08:36:04 +0000159 */
subrata_modak56207ce2009-03-23 13:35:39 +0000160 if ((i == 3 || i == 4) && tst_kvercmp(2, 6, 25) > 0) {
subrata_modak69bc3dc2008-05-23 08:36:04 +0000161 tst_resm(TCONF, "this kernel normalizes buf."
subrata_modak56207ce2009-03-23 13:35:39 +0000162 "offset value if it is outside"
163 " the acceptable range.");
subrata_modak69bc3dc2008-05-23 08:36:04 +0000164 continue;
165 }
robbiewca765f12002-12-24 16:56:24 +0000166
167 buff = tim_save;
168 buff.modes = SET_MODE;
subrata_modak56207ce2009-03-23 13:35:39 +0000169 if ((test_cases[i].setup) && (test_cases[i].setup())) {
robbiewca765f12002-12-24 16:56:24 +0000170 tst_resm(TWARN, "setup() failed, skipping"
subrata_modak56207ce2009-03-23 13:35:39 +0000171 " this test case");
robbiewca765f12002-12-24 16:56:24 +0000172 continue;
173 }
174
175 /* Call adjtimex(2) */
176 TEST(adjtimex(test_cases[i].buffp));
177
subrata_modak56207ce2009-03-23 13:35:39 +0000178 if ((TEST_RETURN == -1) && (TEST_ERRNO ==
179 test_cases[i].exp_errno)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800180 tst_resm(TPASS | TTERRNO,
vapier8dc646d2009-08-28 10:54:04 +0000181 "Test Passed, adjtimex() returned -1");
robbiewca765f12002-12-24 16:56:24 +0000182 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800183 tst_resm(TFAIL | TTERRNO,
184 "Test Failed, adjtimex() returned %ld",
185 TEST_RETURN);
robbiewca765f12002-12-24 16:56:24 +0000186 }
robbiewca765f12002-12-24 16:56:24 +0000187 if (test_cases[i].cleanup) {
188 test_cases[i].cleanup();
189 }
subrata_modak4bb656a2009-02-26 12:02:09 +0000190 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800191 }
robbiewca765f12002-12-24 16:56:24 +0000192
193 /* cleanup and exit */
194 cleanup();
195
Garrett Cooper53740502010-12-16 00:04:01 -0800196 tst_exit();
robbiewca765f12002-12-24 16:56:24 +0000197
Garrett Cooper2c282152010-12-16 00:55:50 -0800198}
robbiewca765f12002-12-24 16:56:24 +0000199
200/* setup() - performs all ONE TIME setup for this test */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400201void setup(void)
robbiewca765f12002-12-24 16:56:24 +0000202{
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200203 tst_require_root(NULL);
robbiewca765f12002-12-24 16:56:24 +0000204
205 tim_save.modes = 0;
206
robbiewca765f12002-12-24 16:56:24 +0000207 tst_sig(NOFORK, DEF_HANDLER, cleanup);
208
mreed10c360edb2006-11-03 17:21:43 +0000209 /* set the HZ from sysconf */
210 hz = sysconf(_SC_CLK_TCK);
211 if (hz == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800212 tst_brkm(TBROK, NULL, "Failed to read the HZ from sysconf\n");
mreed10c360edb2006-11-03 17:21:43 +0000213 }
214
robbiewca765f12002-12-24 16:56:24 +0000215 TEST_PAUSE;
216
217 /* Save current parameters in tim_save */
218 if ((adjtimex(&tim_save)) == -1) {
Garrett Cooper53740502010-12-16 00:04:01 -0800219 tst_brkm(TBROK, NULL, "Failed to save current parameters");
robbiewca765f12002-12-24 16:56:24 +0000220 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800221}
robbiewca765f12002-12-24 16:56:24 +0000222
223/*
224 *cleanup() - performs all ONE TIME cleanup for this test at
225 * completion or premature exit.
226 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400227void cleanup(void)
robbiewca765f12002-12-24 16:56:24 +0000228{
229
230 tim_save.modes = SET_MODE;
231 /* Restore saved parameters */
232 if ((adjtimex(&tim_save)) == -1) {
233 tst_resm(TWARN, "Failed to restore saved parameters");
234 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800235}
robbiewca765f12002-12-24 16:56:24 +0000236
Mike Frysingerc57fba52014-04-09 18:56:30 -0400237int setup2(void)
robbiewca765f12002-12-24 16:56:24 +0000238{
subrata_modak56207ce2009-03-23 13:35:39 +0000239 buff.tick = 900000 / hz - 1;
robbiewca765f12002-12-24 16:56:24 +0000240 return 0;
241}
242
Mike Frysingerc57fba52014-04-09 18:56:30 -0400243int setup3(void)
robbiewca765f12002-12-24 16:56:24 +0000244{
subrata_modak56207ce2009-03-23 13:35:39 +0000245 buff.tick = 1100000 / hz + 1;
robbiewca765f12002-12-24 16:56:24 +0000246 return 0;
247}
248
Mike Frysingerc57fba52014-04-09 18:56:30 -0400249int setup4(void)
robbiewca765f12002-12-24 16:56:24 +0000250{
251 buff.offset = 512000L + 1;
252 return 0;
253}
254
Mike Frysingerc57fba52014-04-09 18:56:30 -0400255int setup5(void)
robbiewca765f12002-12-24 16:56:24 +0000256{
257 buff.offset = (-1) * (512000L) - 1;
258 return 0;
259}
260
Mike Frysingerc57fba52014-04-09 18:56:30 -0400261int setup6(void)
robbiewca765f12002-12-24 16:56:24 +0000262{
263 /* Switch to nobody user for correct error code collection */
subrata_modak56207ce2009-03-23 13:35:39 +0000264 if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
Garrett Cooper53740502010-12-16 00:04:01 -0800265 tst_brkm(TBROK, NULL, "\"nobody\" user not present");
robbiewca765f12002-12-24 16:56:24 +0000266 }
267 if (seteuid(ltpuser->pw_uid) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800268 tst_resm(TWARN | TERRNO, "seteuid(%d) failed", ltpuser->pw_uid);
robbiewca765f12002-12-24 16:56:24 +0000269 return 1;
270 }
271 return 0;
272}
273
Mike Frysingerc57fba52014-04-09 18:56:30 -0400274void cleanup6(void)
robbiewca765f12002-12-24 16:56:24 +0000275{
276 /* Set effective user id back to root */
277 if (seteuid(0) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800278 tst_brkm(TBROK | TERRNO, cleanup, "seteuid failed to set the"
subrata_modak56207ce2009-03-23 13:35:39 +0000279 " effective uid to root");
robbiewca765f12002-12-24 16:56:24 +0000280 }
Chris Dearmanec6edca2012-10-17 19:54:01 -0700281}