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 <avinesh.kumar@suse.com>
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 current time if the
13 * times argument is NULL and the user ID of the process is "root".
14 */
15
16#include <utime.h>
17
18#include "tst_test.h"
19#include "tst_clocks.h"
20
21#define MNT_POINT	"mntpoint"
22#define TEMP_FILE	MNT_POINT"/tmp_file"
23#define FILE_MODE	0444
24
25static void setup(void)
26{
27	SAFE_TOUCH(TEMP_FILE, FILE_MODE, NULL);
28}
29
30static void run(void)
31{
32	struct utimbuf utbuf;
33	struct stat stat_buf;
34	time_t pre_time, post_time;
35
36	utbuf.modtime = tst_get_fs_timestamp() - 5;
37	utbuf.actime = utbuf.modtime + 1;
38	TST_EXP_PASS_SILENT(utime(TEMP_FILE, &utbuf));
39	SAFE_STAT(TEMP_FILE, &stat_buf);
40
41	TST_EXP_EQ_LI(stat_buf.st_atime, utbuf.actime);
42	TST_EXP_EQ_LI(stat_buf.st_mtime, utbuf.modtime);
43
44	pre_time = tst_get_fs_timestamp();
45	TST_EXP_PASS(utime(TEMP_FILE, NULL), "utime(%s, NULL)", TEMP_FILE);
46	if (!TST_PASS)
47		return;
48	post_time = tst_get_fs_timestamp();
49	SAFE_STAT(TEMP_FILE, &stat_buf);
50
51	if (stat_buf.st_mtime < pre_time || stat_buf.st_mtime > post_time)
52		tst_res(TFAIL, "utime() did not set expected mtime, "
53				"pre_time: %ld, post_time: %ld, st_mtime: %ld",
54				pre_time, post_time, stat_buf.st_mtime);
55
56	if (stat_buf.st_atime < pre_time || stat_buf.st_atime > post_time)
57		tst_res(TFAIL, "utime() did not set expected atime, "
58				"pre_time: %ld, post_time: %ld, st_atime: %ld",
59				pre_time, post_time, stat_buf.st_atime);
60
61}
62
63static struct tst_test test = {
64	.test_all = run,
65	.setup = setup,
66	.needs_root = 1,
67	.mntpoint = MNT_POINT,
68	.mount_device = 1,
69	.all_filesystems = 1,
70	.skip_filesystems = (const char *const[]) {
71		"vfat",
72		"exfat",
73		NULL
74	}
75};
76