1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci *   Copyright (c) International Business Machines  Corp., 2001
4f08c3bdfSopenharmony_ci *		07/2001 ported by John George
5f08c3bdfSopenharmony_ci *   Copyright (c) 2022 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci/*\
9f08c3bdfSopenharmony_ci * [Description]
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * Verify that the system call utime() successfully changes the last
12f08c3bdfSopenharmony_ci * access and modification times of a file to the current time if the
13f08c3bdfSopenharmony_ci * times argument is NULL and the user ID of the process is "root".
14f08c3bdfSopenharmony_ci */
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_ci#include <utime.h>
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci#include "tst_test.h"
19f08c3bdfSopenharmony_ci#include "tst_clocks.h"
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci#define MNT_POINT	"mntpoint"
22f08c3bdfSopenharmony_ci#define TEMP_FILE	MNT_POINT"/tmp_file"
23f08c3bdfSopenharmony_ci#define FILE_MODE	0444
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_cistatic void setup(void)
26f08c3bdfSopenharmony_ci{
27f08c3bdfSopenharmony_ci	SAFE_TOUCH(TEMP_FILE, FILE_MODE, NULL);
28f08c3bdfSopenharmony_ci}
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_cistatic void run(void)
31f08c3bdfSopenharmony_ci{
32f08c3bdfSopenharmony_ci	struct utimbuf utbuf;
33f08c3bdfSopenharmony_ci	struct stat stat_buf;
34f08c3bdfSopenharmony_ci	time_t pre_time, post_time;
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci	utbuf.modtime = tst_get_fs_timestamp() - 5;
37f08c3bdfSopenharmony_ci	utbuf.actime = utbuf.modtime + 1;
38f08c3bdfSopenharmony_ci	TST_EXP_PASS_SILENT(utime(TEMP_FILE, &utbuf));
39f08c3bdfSopenharmony_ci	SAFE_STAT(TEMP_FILE, &stat_buf);
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(stat_buf.st_atime, utbuf.actime);
42f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(stat_buf.st_mtime, utbuf.modtime);
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci	pre_time = tst_get_fs_timestamp();
45f08c3bdfSopenharmony_ci	TST_EXP_PASS(utime(TEMP_FILE, NULL), "utime(%s, NULL)", TEMP_FILE);
46f08c3bdfSopenharmony_ci	if (!TST_PASS)
47f08c3bdfSopenharmony_ci		return;
48f08c3bdfSopenharmony_ci	post_time = tst_get_fs_timestamp();
49f08c3bdfSopenharmony_ci	SAFE_STAT(TEMP_FILE, &stat_buf);
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci	if (stat_buf.st_mtime < pre_time || stat_buf.st_mtime > post_time)
52f08c3bdfSopenharmony_ci		tst_res(TFAIL, "utime() did not set expected mtime, "
53f08c3bdfSopenharmony_ci				"pre_time: %ld, post_time: %ld, st_mtime: %ld",
54f08c3bdfSopenharmony_ci				pre_time, post_time, stat_buf.st_mtime);
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci	if (stat_buf.st_atime < pre_time || stat_buf.st_atime > post_time)
57f08c3bdfSopenharmony_ci		tst_res(TFAIL, "utime() did not set expected atime, "
58f08c3bdfSopenharmony_ci				"pre_time: %ld, post_time: %ld, st_atime: %ld",
59f08c3bdfSopenharmony_ci				pre_time, post_time, stat_buf.st_atime);
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci}
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_cistatic struct tst_test test = {
64f08c3bdfSopenharmony_ci	.test_all = run,
65f08c3bdfSopenharmony_ci	.setup = setup,
66f08c3bdfSopenharmony_ci	.needs_root = 1,
67f08c3bdfSopenharmony_ci	.mntpoint = MNT_POINT,
68f08c3bdfSopenharmony_ci	.mount_device = 1,
69f08c3bdfSopenharmony_ci	.all_filesystems = 1,
70f08c3bdfSopenharmony_ci	.skip_filesystems = (const char *const[]) {
71f08c3bdfSopenharmony_ci		"vfat",
72f08c3bdfSopenharmony_ci		"exfat",
73f08c3bdfSopenharmony_ci		NULL
74f08c3bdfSopenharmony_ci	}
75f08c3bdfSopenharmony_ci};
76