xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/utime/utime02.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  *		07/2001 ported by John George
5*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) 2022 SUSE LLC Avinesh Kumar <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * Verify that the system call utime() successfully changes the last
12*49cdfc7eSAndroid Build Coastguard Worker  * access and modification times of a file to the current time,
13*49cdfc7eSAndroid Build Coastguard Worker  * under the following constraints:
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  * - The times argument is NULL.
16*49cdfc7eSAndroid Build Coastguard Worker  * - The user ID of the process is not "root".
17*49cdfc7eSAndroid Build Coastguard Worker  * - The file is owned by the user ID of the process.
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #include <utime.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
24*49cdfc7eSAndroid Build Coastguard Worker #include "tst_clocks.h"
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker #define MNT_POINT	"mntpoint"
27*49cdfc7eSAndroid Build Coastguard Worker #define TEMP_FILE	MNT_POINT"/tmp_file"
28*49cdfc7eSAndroid Build Coastguard Worker #define FILE_MODE	0444
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker #define TEST_USERNAME "nobody"
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)33*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
34*49cdfc7eSAndroid Build Coastguard Worker {
35*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *pw;
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker 	pw = SAFE_GETPWNAM(TEST_USERNAME);
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(TEMP_FILE, FILE_MODE, NULL);
40*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHOWN(TEMP_FILE, pw->pw_uid, pw->pw_gid);
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Switching effective user ID to user: %s", pw->pw_name);
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEUID(pw->pw_uid);
45*49cdfc7eSAndroid Build Coastguard Worker }
46*49cdfc7eSAndroid Build Coastguard Worker 
run(void)47*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
48*49cdfc7eSAndroid Build Coastguard Worker {
49*49cdfc7eSAndroid Build Coastguard Worker 	struct utimbuf utbuf;
50*49cdfc7eSAndroid Build Coastguard Worker 	struct stat stat_buf;
51*49cdfc7eSAndroid Build Coastguard Worker 	time_t pre_time, post_time;
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	utbuf.modtime = tst_get_fs_timestamp() - 5;
54*49cdfc7eSAndroid Build Coastguard Worker 	utbuf.actime = utbuf.modtime + 1;
55*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS_SILENT(utime(TEMP_FILE, &utbuf));
56*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(TEMP_FILE, &stat_buf);
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LI(stat_buf.st_atime, utbuf.actime);
59*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LI(stat_buf.st_mtime, utbuf.modtime);
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker 	pre_time = tst_get_fs_timestamp();
62*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(utime(TEMP_FILE, NULL), "utime(%s, NULL)", TEMP_FILE);
63*49cdfc7eSAndroid Build Coastguard Worker 	if (!TST_PASS)
64*49cdfc7eSAndroid Build Coastguard Worker 		return;
65*49cdfc7eSAndroid Build Coastguard Worker 	post_time = tst_get_fs_timestamp();
66*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(TEMP_FILE, &stat_buf);
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker 	if (stat_buf.st_mtime < pre_time || stat_buf.st_mtime > post_time)
69*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "utime() did not set expected mtime, "
70*49cdfc7eSAndroid Build Coastguard Worker 				"pre_time: %ld, post_time: %ld, st_mtime: %ld",
71*49cdfc7eSAndroid Build Coastguard Worker 				pre_time, post_time, stat_buf.st_mtime);
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 	if (stat_buf.st_atime < pre_time || stat_buf.st_atime > post_time)
74*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "utime() did not set expected atime, "
75*49cdfc7eSAndroid Build Coastguard Worker 				"pre_time: %ld, post_time: %ld, st_atime: %ld",
76*49cdfc7eSAndroid Build Coastguard Worker 				pre_time, post_time, stat_buf.st_atime);
77*49cdfc7eSAndroid Build Coastguard Worker }
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
80*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
81*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
82*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
83*49cdfc7eSAndroid Build Coastguard Worker 	.mntpoint = MNT_POINT,
84*49cdfc7eSAndroid Build Coastguard Worker 	.mount_device = 1,
85*49cdfc7eSAndroid Build Coastguard Worker 	.all_filesystems = 1,
86*49cdfc7eSAndroid Build Coastguard Worker 	.skip_filesystems = (const char *const[]) {
87*49cdfc7eSAndroid Build Coastguard Worker 		"vfat",
88*49cdfc7eSAndroid Build Coastguard Worker 		"exfat",
89*49cdfc7eSAndroid Build Coastguard Worker 		NULL
90*49cdfc7eSAndroid Build Coastguard Worker 	}
91*49cdfc7eSAndroid Build Coastguard Worker };
92