xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/stime/stime02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * Test Description:
6*49cdfc7eSAndroid Build Coastguard Worker  *   Verify that the system call stime() fails to set the system's idea
7*49cdfc7eSAndroid Build Coastguard Worker  *   of data and time if invoked by "non-root" user.
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * Expected Result:
10*49cdfc7eSAndroid Build Coastguard Worker  *  stime() should fail with return value -1 and set errno to EPERM.
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * History
13*49cdfc7eSAndroid Build Coastguard Worker  *	07/2001 John George
14*49cdfc7eSAndroid Build Coastguard Worker  *		-Ported
15*49cdfc7eSAndroid Build Coastguard Worker  */
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <time.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
23*49cdfc7eSAndroid Build Coastguard Worker #include "stime_var.h"
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker static time_t new_time;
26*49cdfc7eSAndroid Build Coastguard Worker 
run(void)27*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
28*49cdfc7eSAndroid Build Coastguard Worker {
29*49cdfc7eSAndroid Build Coastguard Worker 	TEST(do_stime(&new_time));
30*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != -1) {
31*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL,
32*49cdfc7eSAndroid Build Coastguard Worker 			"stime() returned %ld, expected -1 EPERM", TST_RET);
33*49cdfc7eSAndroid Build Coastguard Worker 		return;
34*49cdfc7eSAndroid Build Coastguard Worker 	}
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_ERR == EPERM) {
37*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS | TTERRNO, "stime(2) fails, Caller not root");
38*49cdfc7eSAndroid Build Coastguard Worker 		return;
39*49cdfc7eSAndroid Build Coastguard Worker 	}
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TFAIL | TTERRNO,
42*49cdfc7eSAndroid Build Coastguard Worker 		"stime(2) fails, Caller not root, expected errno:%d", EPERM);
43*49cdfc7eSAndroid Build Coastguard Worker }
44*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)45*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
46*49cdfc7eSAndroid Build Coastguard Worker {
47*49cdfc7eSAndroid Build Coastguard Worker 	time_t curr_time;
48*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *ltpuser;
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	stime_info();
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker 	ltpuser = SAFE_GETPWNAM("nobody");
53*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETUID(ltpuser->pw_uid);
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	if ((curr_time = time(NULL)) < 0)
56*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "time() failed");
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker 	new_time = curr_time + 10;
59*49cdfc7eSAndroid Build Coastguard Worker }
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
62*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
63*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
64*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
65*49cdfc7eSAndroid Build Coastguard Worker 	.test_variants = TEST_VARIANTS,
66*49cdfc7eSAndroid Build Coastguard Worker };
67