xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/stime/stime01.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() successfully sets the system's idea
7*49cdfc7eSAndroid Build Coastguard Worker  *  of date and time if invoked by "root" user.
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * Expected Result:
10*49cdfc7eSAndroid Build Coastguard Worker  *  stime() should succeed to set the system data/time to the specified time.
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 <time.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <sys/time.h>
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker #include "stime_var.h"
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker static struct timeval real_time_tv;
24*49cdfc7eSAndroid Build Coastguard Worker 
run(void)25*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
26*49cdfc7eSAndroid Build Coastguard Worker {
27*49cdfc7eSAndroid Build Coastguard Worker 	time_t new_time;
28*49cdfc7eSAndroid Build Coastguard Worker 	struct timeval pres_time_tv;
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker 	if (gettimeofday(&real_time_tv, NULL) < 0)
31*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "gettimeofday() failed");
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker 	new_time = real_time_tv.tv_sec + 30;
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker 	if (do_stime(&new_time) < 0) {
36*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TERRNO, "stime(%ld) failed", new_time);
37*49cdfc7eSAndroid Build Coastguard Worker 		return;
38*49cdfc7eSAndroid Build Coastguard Worker 	}
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker 	if (gettimeofday(&pres_time_tv, NULL) < 0)
41*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TERRNO, "gettimeofday() failed");
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 	switch (pres_time_tv.tv_sec - new_time) {
44*49cdfc7eSAndroid Build Coastguard Worker 	case 0:
45*49cdfc7eSAndroid Build Coastguard Worker 	case 1:
46*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "pt.tv_sec: %ld", pres_time_tv.tv_sec);
47*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "system time was set to %ld", new_time);
48*49cdfc7eSAndroid Build Coastguard Worker 	break;
49*49cdfc7eSAndroid Build Coastguard Worker 	default:
50*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "system time not set to %ld (got: %ld)",
51*49cdfc7eSAndroid Build Coastguard Worker 			new_time, pres_time_tv.tv_sec);
52*49cdfc7eSAndroid Build Coastguard Worker 	}
53*49cdfc7eSAndroid Build Coastguard Worker }
54*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)55*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
56*49cdfc7eSAndroid Build Coastguard Worker {
57*49cdfc7eSAndroid Build Coastguard Worker 	stime_info();
58*49cdfc7eSAndroid Build Coastguard Worker }
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
61*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
62*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
63*49cdfc7eSAndroid Build Coastguard Worker 	.restore_wallclock = 1,
64*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
65*49cdfc7eSAndroid Build Coastguard Worker 	.test_variants = TEST_VARIANTS,
66*49cdfc7eSAndroid Build Coastguard Worker };
67