xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/utime/utime03.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) 2021 SUSE LLC <[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 sets the modification
12*49cdfc7eSAndroid Build Coastguard Worker  * and access times of a file to the current time, under the following
13*49cdfc7eSAndroid Build Coastguard Worker  * 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 not owned by the user ID of the process.
18*49cdfc7eSAndroid Build Coastguard Worker  * - The user ID of the process has write access to the file.
19*49cdfc7eSAndroid Build Coastguard Worker  */
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <utime.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <time.h>
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
26*49cdfc7eSAndroid Build Coastguard Worker #include "tst_uid.h"
27*49cdfc7eSAndroid Build Coastguard Worker #include "tst_clocks.h"
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker #define MNTPOINT	"mntpoint"
30*49cdfc7eSAndroid Build Coastguard Worker #define TEMP_FILE	MNTPOINT"/tmp_file"
31*49cdfc7eSAndroid Build Coastguard Worker #define FILE_MODE	0766
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker static uid_t root_uid, user_uid;
34*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)35*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
36*49cdfc7eSAndroid Build Coastguard Worker {
37*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *pw;
38*49cdfc7eSAndroid Build Coastguard Worker 	uid_t test_users[2];
39*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	root_uid = getuid();
42*49cdfc7eSAndroid Build Coastguard Worker 	pw = SAFE_GETPWNAM("nobody");
43*49cdfc7eSAndroid Build Coastguard Worker 	test_users[0] = pw->pw_uid;
44*49cdfc7eSAndroid Build Coastguard Worker 	tst_get_uids(test_users, 1, 2);
45*49cdfc7eSAndroid Build Coastguard Worker 	user_uid = test_users[1];
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_CREAT(TEMP_FILE, FILE_MODE);
48*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	/* Override umask */
51*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHMOD(TEMP_FILE, FILE_MODE);
52*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CHOWN(TEMP_FILE, pw->pw_uid, pw->pw_gid);
53*49cdfc7eSAndroid Build Coastguard Worker }
54*49cdfc7eSAndroid Build Coastguard Worker 
run(void)55*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
56*49cdfc7eSAndroid Build Coastguard Worker {
57*49cdfc7eSAndroid Build Coastguard Worker 	struct utimbuf utbuf;
58*49cdfc7eSAndroid Build Coastguard Worker 	struct stat statbuf;
59*49cdfc7eSAndroid Build Coastguard Worker 	time_t mintime, maxtime;
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker 	utbuf.modtime = time(0) - 5;
62*49cdfc7eSAndroid Build Coastguard Worker 	utbuf.actime = utbuf.modtime + 1;
63*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS_SILENT(utime(TEMP_FILE, &utbuf));
64*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(TEMP_FILE, &statbuf);
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 	if (statbuf.st_atime != utbuf.actime ||
67*49cdfc7eSAndroid Build Coastguard Worker 		statbuf.st_mtime != utbuf.modtime) {
68*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Could not set initial file times");
69*49cdfc7eSAndroid Build Coastguard Worker 		return;
70*49cdfc7eSAndroid Build Coastguard Worker 	}
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEUID(user_uid);
73*49cdfc7eSAndroid Build Coastguard Worker 	mintime = tst_get_fs_timestamp();
74*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(utime(TEMP_FILE, NULL));
75*49cdfc7eSAndroid Build Coastguard Worker 	maxtime = tst_get_fs_timestamp();
76*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SETEUID(root_uid);
77*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(TEMP_FILE, &statbuf);
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker 	if (statbuf.st_atime < mintime || statbuf.st_atime > maxtime)
80*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "utime() did not set expected atime, "
81*49cdfc7eSAndroid Build Coastguard Worker 			"mintime: %ld, maxtime: %ld, st_atime: %ld",
82*49cdfc7eSAndroid Build Coastguard Worker 			mintime, maxtime, statbuf.st_atime);
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker 	if (statbuf.st_mtime < mintime || statbuf.st_mtime > maxtime)
85*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "utime() did not set expected mtime, "
86*49cdfc7eSAndroid Build Coastguard Worker 			"mintime: %ld, maxtime: %ld, st_mtime: %ld",
87*49cdfc7eSAndroid Build Coastguard Worker 			mintime, maxtime, statbuf.st_mtime);
88*49cdfc7eSAndroid Build Coastguard Worker }
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
91*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
92*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
93*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
94*49cdfc7eSAndroid Build Coastguard Worker 	.mount_device = 1,
95*49cdfc7eSAndroid Build Coastguard Worker 	.mntpoint = MNTPOINT,
96*49cdfc7eSAndroid Build Coastguard Worker 	.all_filesystems = 1,
97*49cdfc7eSAndroid Build Coastguard Worker 	.skip_filesystems = (const char *const[]) {
98*49cdfc7eSAndroid Build Coastguard Worker 		"vfat",
99*49cdfc7eSAndroid Build Coastguard Worker 		"exfat",
100*49cdfc7eSAndroid Build Coastguard Worker 		NULL
101*49cdfc7eSAndroid Build Coastguard Worker 	}
102*49cdfc7eSAndroid Build Coastguard Worker };
103