xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/utime/utime05.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *   Copyright (c) International Business Machines  Corp., 2001
4  *		07/2001 ported by John George
5  *   Copyright (c) 2022 SUSE LLC Avinesh Kumar <[email protected]>
6  */
7 
8 /*\
9  * [Description]
10  *
11  * Verify that the system call utime() successfully changes the last
12  * access and modification times of a file to the values specified by
13  * times argument, under the following constraints:
14  *
15  * - The times argument is not NULL.
16  * - The user ID of the process is not "root".
17  * - The file is owned by the user ID of the process.
18  */
19 
20 #include <utime.h>
21 #include <pwd.h>
22 
23 #include "tst_test.h"
24 
25 #define MNT_POINT	"mntpoint"
26 #define TEMP_FILE	MNT_POINT"/tmp_file"
27 
28 #define FILE_MODE	0444
29 #define MODE_RWX	0777
30 #define NEW_MODF_TIME	10000
31 #define NEW_ACCESS_TIME	20000
32 
33 #define TEST_USERNAME "nobody"
34 
35 static struct utimbuf times = {
36 	.modtime = NEW_MODF_TIME,
37 	.actime = NEW_ACCESS_TIME
38 };
39 
setup(void)40 static void setup(void)
41 {
42 	struct passwd *pw;
43 
44 	SAFE_CHMOD(MNT_POINT, MODE_RWX);
45 
46 	pw = SAFE_GETPWNAM(TEST_USERNAME);
47 	tst_res(TINFO, "Switching effective user ID to user: %s", pw->pw_name);
48 	SAFE_SETEUID(pw->pw_uid);
49 
50 	SAFE_TOUCH(TEMP_FILE, FILE_MODE, NULL);
51 }
52 
run(void)53 static void run(void)
54 {
55 	struct stat stat_buf;
56 
57 	TST_EXP_PASS(utime(TEMP_FILE, &times), "utime(%s, &times)", TEMP_FILE);
58 	if (!TST_PASS)
59 		return;
60 
61 	SAFE_STAT(TEMP_FILE, &stat_buf);
62 
63 	TST_EXP_EQ_LI(stat_buf.st_mtime, NEW_MODF_TIME);
64 	TST_EXP_EQ_LI(stat_buf.st_atime, NEW_ACCESS_TIME);
65 }
66 
67 static struct tst_test test = {
68 	.test_all = run,
69 	.setup = setup,
70 	.needs_root = 1,
71 	.mount_device = 1,
72 	.mntpoint = MNT_POINT,
73 	.all_filesystems = 1,
74 	.skip_filesystems = (const char *const[]) {
75 		"vfat",
76 		"exfat",
77 		NULL
78 	}
79 };
80