blob: c10d4992021dcc8b6a64e3a753ecc3738b8d416a [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
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
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 };
plars865695b2001-08-27 22:15:12 +000084time_t curr_time; /* system's current time in seconds */
85time_t new_time; /* system's new time */
86time_t tloc; /* argument var. for time() */
robbiewb1c11a12001-08-28 15:47:02 +000087char nobody_uid[] = "nobody";
88struct passwd *ltpuser;
89
plars865695b2001-08-27 22:15:12 +000090void setup(); /* Main setup function of test */
91void cleanup(); /* cleanup function for the test */
92
subrata_modak56207ce2009-03-23 13:35:39 +000093int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000094{
95 int lc; /* loop counter */
96 char *msg; /* message returned from parse_opts */
subrata_modak56207ce2009-03-23 13:35:39 +000097
plars865695b2001-08-27 22:15:12 +000098 /* Parse standard options given to run the test. */
Garrett Cooper45e285d2010-11-22 12:19:25 -080099 msg = parse_opts(ac, av, NULL, NULL);
100 if (msg != NULL) {
plars865695b2001-08-27 22:15:12 +0000101 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper2c282152010-12-16 00:55:50 -0800102
plars865695b2001-08-27 22:15:12 +0000103 }
104
plars865695b2001-08-27 22:15:12 +0000105 setup();
106
107 /* set the expected errnos... */
108 TEST_EXP_ENOS(exp_enos);
109
plars865695b2001-08-27 22:15:12 +0000110 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800111
subrata_modak56207ce2009-03-23 13:35:39 +0000112 Tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000113
subrata_modak4bb656a2009-02-26 12:02:09 +0000114 /*
plars865695b2001-08-27 22:15:12 +0000115 * Invoke stime(2) to set the system's time
116 * to the specified new_time as non-root user.
117 */
118 TEST(stime(&new_time));
119
plars865695b2001-08-27 22:15:12 +0000120 if (TEST_RETURN == -1) {
121 TEST_ERROR_LOG(TEST_ERRNO);
122 if (TEST_ERRNO == EPERM) {
123 tst_resm(TPASS, "stime(2) fails, Caller not "
subrata_modak56207ce2009-03-23 13:35:39 +0000124 "root, errno:%d", TEST_ERRNO);
plars865695b2001-08-27 22:15:12 +0000125 } else {
126 tst_resm(TFAIL, "stime(2) fails, Caller not "
subrata_modak56207ce2009-03-23 13:35:39 +0000127 "root, errno:%d, expected errno:%d",
128 TEST_ERRNO, EPERM);
plars865695b2001-08-27 22:15:12 +0000129 }
130 } else {
subrata_modak923b23f2009-11-02 13:57:16 +0000131 tst_resm(TFAIL, "stime(2) returned %ld, expected -1, "
subrata_modak56207ce2009-03-23 13:35:39 +0000132 "errno:%d", TEST_RETURN, EPERM);
plars865695b2001-08-27 22:15:12 +0000133 }
subrata_modak56207ce2009-03-23 13:35:39 +0000134 Tst_count++; /* incr TEST_LOOP counter */
Garrett Cooper2c282152010-12-16 00:55:50 -0800135 }
plars865695b2001-08-27 22:15:12 +0000136
plars865695b2001-08-27 22:15:12 +0000137 cleanup();
plars865695b2001-08-27 22:15:12 +0000138
Garrett Cooper2c282152010-12-16 00:55:50 -0800139}
plars865695b2001-08-27 22:15:12 +0000140
141/*
142 * void
143 * setup() - performs all ONE TIME setup for this test.
144 * Get the current time and system's new time.
145 */
subrata_modak56207ce2009-03-23 13:35:39 +0000146void setup()
plars865695b2001-08-27 22:15:12 +0000147{
Garrett Cooper2c282152010-12-16 00:55:50 -0800148
plars865695b2001-08-27 22:15:12 +0000149 tst_sig(NOFORK, DEF_HANDLER, cleanup);
150
robbiewb1c11a12001-08-28 15:47:02 +0000151 /* Switch to nobody user for correct error code collection */
subrata_modak56207ce2009-03-23 13:35:39 +0000152 if (geteuid() != 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800153 tst_brkm(TBROK, NULL, "Test must be run as root");
subrata_modak56207ce2009-03-23 13:35:39 +0000154 }
155 ltpuser = getpwnam(nobody_uid);
156 if (setuid(ltpuser->pw_uid) == -1) {
157 tst_resm(TINFO, "setuid failed to "
158 "to set the effective uid to %d", ltpuser->pw_uid);
159 perror("setuid");
160 }
plars865695b2001-08-27 22:15:12 +0000161
plars865695b2001-08-27 22:15:12 +0000162 TEST_PAUSE;
163
164 /* Get the current time */
165 if ((curr_time = time(&tloc)) < 0) {
166 tst_brkm(TBROK, cleanup,
subrata_modak56207ce2009-03-23 13:35:39 +0000167 "time() failed to get current time, errno=%d", errno);
Garrett Cooper53740502010-12-16 00:04:01 -0800168 }
plars865695b2001-08-27 22:15:12 +0000169
170 /* Get the system's new time */
171 new_time = curr_time + INCR_TIME;
Garrett Cooper2c282152010-12-16 00:55:50 -0800172}
plars865695b2001-08-27 22:15:12 +0000173
174/*
175 * void
176 * cleanup() - performs all ONE TIME cleanup for this test at
177 * completion or premature exit.
178 */
subrata_modak56207ce2009-03-23 13:35:39 +0000179void cleanup()
plars865695b2001-08-27 22:15:12 +0000180{
181 /*
182 * print timing stats if that option was specified.
183 * print errno log if that option was specified.
184 */
185 TEST_CLEANUP;
186
Garrett Cooper2c282152010-12-16 00:55:50 -0800187}