blob: 5b9b1a76c56a5a9f87ab9ed4fb8ea1716c751a6f [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * Test Name: stime02
22 *
23 * Test Description:
24 * Verify that the system call stime() fails to set the system's idea
25 * of data and time if invoked by "non-root" user.
26 *
27 * Expected Result:
28 * stime() should fail with return value -1 and set errno to EPERM.
29 *
30 * Algorithm:
31 * Setup:
32 * Setup signal handling.
33 * Pause for SIGUSR1 if option specified.
34 *
35 * Test:
36 * Loop if the proper options are given.
37 * Execute system call
38 * Check return code, if system call failed (return=-1)
39 * if errno set == expected errno
40 * Issue sys call fails with expected return value and errno.
41 * Otherwise,
42 * Issue sys call fails with unexpected errno.
43 * Otherwise,
44 * Issue sys call returns unexpected value.
45 *
46 * Cleanup:
47 * Print errno log and/or timing stats if options given
48 *
49 * Usage: <for command-line>
50 * stime02 [-c n] [-e] [-i n] [-I x] [-p x] [-t]
51 * where, -c n : Run n copies concurrently.
52 * -e : Turn on errno logging.
53 * -i n : Execute test n times.
54 * -I x : Execute test for x seconds.
55 * -P x : Pause for x seconds between iterations.
56 * -t : Turn on syscall timing.
57 *
58 * History
59 * 07/2001 John George
60 * -Ported
61 *
62 * Restrictions:
plars865695b2001-08-27 22:15:12 +000063 */
64
65#include <stdio.h>
66#include <unistd.h>
67#include <sys/types.h>
68#include <errno.h>
69#include <fcntl.h>
70#include <time.h>
71#include <string.h>
72#include <sys/stat.h>
73#include <signal.h>
robbiewb1c11a12001-08-28 15:47:02 +000074#include <pwd.h>
plars865695b2001-08-27 22:15:12 +000075
76#include "test.h"
77#include "usctest.h"
78
79#define INCR_TIME 10 /* increment in the system's current time */
80
subrata_modak56207ce2009-03-23 13:35:39 +000081char *TCID = "stime02"; /* Test program identifier. */
82int TST_TOTAL = 1; /* Total number of test cases. */
subrata_modak56207ce2009-03-23 13:35:39 +000083int exp_enos[] = { EPERM, 0 };
Wanlong Gao354ebb42012-12-07 10:10:04 +080084
plars865695b2001-08-27 22:15:12 +000085time_t curr_time; /* system's current time in seconds */
86time_t new_time; /* system's new time */
87time_t tloc; /* argument var. for time() */
robbiewb1c11a12001-08-28 15:47:02 +000088char nobody_uid[] = "nobody";
89struct passwd *ltpuser;
90
plars865695b2001-08-27 22:15:12 +000091void setup(); /* Main setup function of test */
92void cleanup(); /* cleanup function for the test */
93
subrata_modak56207ce2009-03-23 13:35:39 +000094int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000095{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020096 int lc;
97 char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +000098
plars865695b2001-08-27 22:15:12 +000099 /* Parse standard options given to run the test. */
Garrett Cooper45e285d2010-11-22 12:19:25 -0800100 msg = parse_opts(ac, av, NULL, NULL);
101 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000102 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800103
plars865695b2001-08-27 22:15:12 +0000104 }
105
plars865695b2001-08-27 22:15:12 +0000106 setup();
107
108 /* set the expected errnos... */
109 TEST_EXP_ENOS(exp_enos);
110
plars865695b2001-08-27 22:15:12 +0000111 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800112
Caspar Zhangd59a6592013-03-07 14:59:12 +0800113 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000114
subrata_modak4bb656a2009-02-26 12:02:09 +0000115 /*
plars865695b2001-08-27 22:15:12 +0000116 * Invoke stime(2) to set the system's time
117 * to the specified new_time as non-root user.
118 */
119 TEST(stime(&new_time));
120
plars865695b2001-08-27 22:15:12 +0000121 if (TEST_RETURN == -1) {
122 TEST_ERROR_LOG(TEST_ERRNO);
123 if (TEST_ERRNO == EPERM) {
124 tst_resm(TPASS, "stime(2) fails, Caller not "
subrata_modak56207ce2009-03-23 13:35:39 +0000125 "root, errno:%d", TEST_ERRNO);
plars865695b2001-08-27 22:15:12 +0000126 } else {
127 tst_resm(TFAIL, "stime(2) fails, Caller not "
subrata_modak56207ce2009-03-23 13:35:39 +0000128 "root, errno:%d, expected errno:%d",
129 TEST_ERRNO, EPERM);
plars865695b2001-08-27 22:15:12 +0000130 }
131 } else {
subrata_modak923b23f2009-11-02 13:57:16 +0000132 tst_resm(TFAIL, "stime(2) returned %ld, expected -1, "
subrata_modak56207ce2009-03-23 13:35:39 +0000133 "errno:%d", TEST_RETURN, EPERM);
plars865695b2001-08-27 22:15:12 +0000134 }
Caspar Zhangd59a6592013-03-07 14:59:12 +0800135 tst_count++; /* incr TEST_LOOP counter */
Garrett Cooper2c282152010-12-16 00:55:50 -0800136 }
plars865695b2001-08-27 22:15:12 +0000137
plars865695b2001-08-27 22:15:12 +0000138 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800139 tst_exit();
plars865695b2001-08-27 22:15:12 +0000140
Garrett Cooper2c282152010-12-16 00:55:50 -0800141}
plars865695b2001-08-27 22:15:12 +0000142
143/*
144 * void
145 * setup() - performs all ONE TIME setup for this test.
146 * Get the current time and system's new time.
147 */
subrata_modak56207ce2009-03-23 13:35:39 +0000148void setup()
plars865695b2001-08-27 22:15:12 +0000149{
Garrett Cooper2c282152010-12-16 00:55:50 -0800150
plars865695b2001-08-27 22:15:12 +0000151 tst_sig(NOFORK, DEF_HANDLER, cleanup);
152
robbiewb1c11a12001-08-28 15:47:02 +0000153 /* Switch to nobody user for correct error code collection */
subrata_modak56207ce2009-03-23 13:35:39 +0000154 if (geteuid() != 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800155 tst_brkm(TBROK, NULL, "Test must be run as root");
subrata_modak56207ce2009-03-23 13:35:39 +0000156 }
157 ltpuser = getpwnam(nobody_uid);
158 if (setuid(ltpuser->pw_uid) == -1) {
159 tst_resm(TINFO, "setuid failed to "
160 "to set the effective uid to %d", ltpuser->pw_uid);
161 perror("setuid");
162 }
plars865695b2001-08-27 22:15:12 +0000163
plars865695b2001-08-27 22:15:12 +0000164 TEST_PAUSE;
165
166 /* Get the current time */
167 if ((curr_time = time(&tloc)) < 0) {
168 tst_brkm(TBROK, cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000169 "time() failed to get current time, errno=%d", errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800170 }
plars865695b2001-08-27 22:15:12 +0000171
172 /* Get the system's new time */
173 new_time = curr_time + INCR_TIME;
Garrett Cooper2c282152010-12-16 00:55:50 -0800174}
plars865695b2001-08-27 22:15:12 +0000175
176/*
177 * void
178 * cleanup() - performs all ONE TIME cleanup for this test at
179 * completion or premature exit.
180 */
subrata_modak56207ce2009-03-23 13:35:39 +0000181void cleanup()
plars865695b2001-08-27 22:15:12 +0000182{
183 /*
184 * print timing stats if that option was specified.
185 * print errno log if that option was specified.
186 */
187 TEST_CLEANUP;
188
Chris Dearmanec6edca2012-10-17 19:54:01 -0700189}