blob: d5e334158fe024bd38260b0a2d85f09a30bac242 [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 * NAME
22 * settimeofday02.c
23 *
24 * DESCRIPTION
25 * Testcase to check that settimeofday() sets errnos correctly.
26 *
27 * ALGORITHM
28 * Setup:
29 * Setup signal handling.
30 * Check that we are not root.
31 * Setup expected errnos.
32 * Pause for SIGUSER1 if option specified.
33 * Save the current time values.
34 * Loop if the proper options are given.
35 * Call settimeofday with an invalid "buf" address and verify that
36 * errno is set to EFAULT.
37 * Call settimeofday as a non-root user. Verify that the call fails
38 * and errno is set to EPERM.
39 * Cleanup:
40 * Print errno log and/or timing stats if options given.
41 *
42 * USAGE: <for command-line>
43 * settimeofday02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
44 * where, -c n : Run n copies concurrently.
45 * -e : Turn on errno logging.
46 * -i n : Execute test n times.
47 * -I x : Execute test for x seconds.
48 * -P x : Pause for x seconds between iterations.
49 * -t : Turn on syscall timing.
50 *
51 * History
52 * 07/2001 John George
53 * -Ported
54 *
55 * Restrictions
subrata_modak4bb656a2009-02-26 12:02:09 +000056 * Must not be run as root
plars865695b2001-08-27 22:15:12 +000057 */
58
59#include <stdio.h>
60#include <sys/time.h>
61#include <errno.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080062#include "test.h"
robbiew5901ff42001-08-28 15:11:41 +000063#include <pwd.h>
plars865695b2001-08-27 22:15:12 +000064
65#define VAL_SEC 100
66#define VAL_MSEC 100
67
68char *TCID = "settimeofday02";
69int TST_TOTAL = 1;
Wanlong Gao354ebb42012-12-07 10:10:04 +080070
plars865695b2001-08-27 22:15:12 +000071struct timeval tp;
72time_t save_tv_sec, save_tv_usec;
73
robbiew5901ff42001-08-28 15:11:41 +000074char nobody_uid[] = "nobody";
75struct passwd *ltpuser;
76
plars865695b2001-08-27 22:15:12 +000077void setup(void);
78void cleanup(void);
79void restore_time(void);
80
vapiercc0d4502006-02-27 04:36:28 +000081#if !defined(UCLINUX)
82
plars74948ad2002-11-14 16:16:14 +000083int main(int argc, char **argv)
plars865695b2001-08-27 22:15:12 +000084{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020085 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020086 const char *msg;
plars865695b2001-08-27 22:15:12 +000087
Wanlong Gao354ebb42012-12-07 10:10:04 +080088 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) {
plars865695b2001-08-27 22:15:12 +000089 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
90 }
91
92 setup();
93
plars865695b2001-08-27 22:15:12 +000094 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080095 /* reset tst_count in case we are looping */
96 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000097
98 TEST(settimeofday((void *)-1, NULL));
99 if (TEST_RETURN != -1) {
100 tst_resm(TFAIL, "settimeofday(2) failed to FAIL");
101 restore_time();
102 } else {
plars865695b2001-08-27 22:15:12 +0000103 if (TEST_ERRNO != EFAULT) {
104 tst_resm(TFAIL, "Expected EFAULT got %d",
subrata_modak56207ce2009-03-23 13:35:39 +0000105 TEST_ERRNO);
plars865695b2001-08-27 22:15:12 +0000106 } else {
107 tst_resm(TPASS, "Received expected errno");
108 }
109 }
110
111 tp.tv_sec = VAL_SEC;
112 tp.tv_usec = VAL_MSEC;
113 TEST(settimeofday(&tp, NULL));
114 if (TEST_RETURN != -1) {
115 tst_resm(TFAIL, "settimeofday(2) failed to FAIL");
116 restore_time();
117 } else {
plars865695b2001-08-27 22:15:12 +0000118 if (TEST_ERRNO != EPERM) {
119 tst_resm(TFAIL, "Expected EPERM got %d",
subrata_modak56207ce2009-03-23 13:35:39 +0000120 TEST_ERRNO);
plars865695b2001-08-27 22:15:12 +0000121 } else {
122 tst_resm(TPASS, "Received expected errno");
123 }
124 }
125 }
126 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800127 tst_exit();
robbiewfa451a12003-03-27 20:52:36 +0000128
plars865695b2001-08-27 22:15:12 +0000129}
130
vapiercc0d4502006-02-27 04:36:28 +0000131#else
132
Mike Frysingerc57fba52014-04-09 18:56:30 -0400133int main(void)
vapiercc0d4502006-02-27 04:36:28 +0000134{
135 tst_resm(TINFO, "test is not available on uClinux");
Garrett Cooper2c282152010-12-16 00:55:50 -0800136 tst_exit();
vapiercc0d4502006-02-27 04:36:28 +0000137}
138
139#endif /* if !defined(UCLINUX) */
140
plars865695b2001-08-27 22:15:12 +0000141/*
142 * setup()
143 * performs all ONE TIME setup for this test
144 */
subrata_modak56207ce2009-03-23 13:35:39 +0000145void setup(void)
plars865695b2001-08-27 22:15:12 +0000146{
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200147 tst_require_root(NULL);
Garrett Cooper2c282152010-12-16 00:55:50 -0800148
plars865695b2001-08-27 22:15:12 +0000149 tst_sig(FORK, DEF_HANDLER, cleanup);
150
subrata_modak56207ce2009-03-23 13:35:39 +0000151 /* Switch to nobody user for correct error code collection */
152 ltpuser = getpwnam(nobody_uid);
153 if (setuid(ltpuser->pw_uid) == -1) {
154 tst_resm(TINFO, "setuid failed to "
155 "to set the effective uid to %d", ltpuser->pw_uid);
156 perror("setuid");
plars865695b2001-08-27 22:15:12 +0000157 }
158
plars865695b2001-08-27 22:15:12 +0000159 /* Pause if that option was specified
160 * TEST_PAUSE contains the code to fork the test with the -c option.
161 */
162 TEST_PAUSE;
163
164 /* Save the current time values */
165 if ((gettimeofday(&tp, (struct timezone *)&tp)) == -1) {
166 tst_brkm(TBROK, cleanup, "gettimeofday failed. "
subrata_modak56207ce2009-03-23 13:35:39 +0000167 "errno=%d", errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800168 }
plars865695b2001-08-27 22:15:12 +0000169 save_tv_sec = tp.tv_sec;
170 save_tv_usec = tp.tv_usec;
171}
172
173/*
174 * cleanup()
175 * performs all ONE TIME cleanup for this test at
176 * completion or premature exit
177 */
subrata_modak56207ce2009-03-23 13:35:39 +0000178void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000179{
plars865695b2001-08-27 22:15:12 +0000180
Wanlong Gao354ebb42012-12-07 10:10:04 +0800181}
plars865695b2001-08-27 22:15:12 +0000182
subrata_modak56207ce2009-03-23 13:35:39 +0000183void restore_time(void)
plars865695b2001-08-27 22:15:12 +0000184{
185 /* restore the original time values. */
186 tp.tv_sec = save_tv_sec;
187 tp.tv_usec = save_tv_usec;
188 if ((settimeofday(&tp, NULL)) == -1) {
189 tst_resm(TWARN, "FATAL COULD NOT RESET THE CLOCK");
subrata_modak56207ce2009-03-23 13:35:39 +0000190 tst_resm(TFAIL, "Error Setting Time, errno=%d", errno);
plars865695b2001-08-27 22:15:12 +0000191 }
Chris Dearmanec6edca2012-10-17 19:54:01 -0700192}