xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/utime/utime04.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 values specified by
13*49cdfc7eSAndroid Build Coastguard Worker  * times argument, under the following constraints:
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  * - The times argument is not NULL.
16*49cdfc7eSAndroid Build Coastguard Worker  * - The user ID of the process is "root".
17*49cdfc7eSAndroid Build Coastguard Worker  */
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #include <utime.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker #define MNT_POINT	"mntpoint"
23*49cdfc7eSAndroid Build Coastguard Worker #define TEMP_FILE	MNT_POINT"/tmp_file"
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker #define FILE_MODE	0444
26*49cdfc7eSAndroid Build Coastguard Worker #define NEW_MODF_TIME	10000
27*49cdfc7eSAndroid Build Coastguard Worker #define NEW_ACCESS_TIME	20000
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker static struct utimbuf times = {
30*49cdfc7eSAndroid Build Coastguard Worker 	.modtime = NEW_MODF_TIME,
31*49cdfc7eSAndroid Build Coastguard Worker 	.actime = NEW_ACCESS_TIME
32*49cdfc7eSAndroid Build Coastguard Worker };
33*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)34*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
35*49cdfc7eSAndroid Build Coastguard Worker {
36*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(TEMP_FILE, FILE_MODE, NULL);
37*49cdfc7eSAndroid Build Coastguard Worker }
38*49cdfc7eSAndroid Build Coastguard Worker 
run(void)39*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
40*49cdfc7eSAndroid Build Coastguard Worker {
41*49cdfc7eSAndroid Build Coastguard Worker 	struct stat stat_buf;
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(utime(TEMP_FILE, &times), "utime(%s, &times)", TEMP_FILE);
44*49cdfc7eSAndroid Build Coastguard Worker 	if (!TST_PASS)
45*49cdfc7eSAndroid Build Coastguard Worker 		return;
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(TEMP_FILE, &stat_buf);
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LI(stat_buf.st_mtime, NEW_MODF_TIME);
50*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LI(stat_buf.st_atime, NEW_ACCESS_TIME);
51*49cdfc7eSAndroid Build Coastguard Worker }
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
54*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
55*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
56*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
57*49cdfc7eSAndroid Build Coastguard Worker 	.mount_device = 1,
58*49cdfc7eSAndroid Build Coastguard Worker 	.mntpoint = MNT_POINT,
59*49cdfc7eSAndroid Build Coastguard Worker 	.all_filesystems = 1,
60*49cdfc7eSAndroid Build Coastguard Worker 	.skip_filesystems = (const char *const[]) {
61*49cdfc7eSAndroid Build Coastguard Worker 		"vfat",
62*49cdfc7eSAndroid Build Coastguard Worker 		"exfat",
63*49cdfc7eSAndroid Build Coastguard Worker 		NULL
64*49cdfc7eSAndroid Build Coastguard Worker 	}
65*49cdfc7eSAndroid Build Coastguard Worker };
66