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,
13f08c3bdfSopenharmony_ci * under the following constraints:
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci * - The times argument is NULL.
16f08c3bdfSopenharmony_ci * - The user ID of the process is not "root".
17f08c3bdfSopenharmony_ci * - The file is owned by the user ID of the process.
18f08c3bdfSopenharmony_ci */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci#include <utime.h>
21f08c3bdfSopenharmony_ci#include <pwd.h>
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_ci#include "tst_test.h"
24f08c3bdfSopenharmony_ci#include "tst_clocks.h"
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ci#define MNT_POINT	"mntpoint"
27f08c3bdfSopenharmony_ci#define TEMP_FILE	MNT_POINT"/tmp_file"
28f08c3bdfSopenharmony_ci#define FILE_MODE	0444
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci#define TEST_USERNAME "nobody"
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_cistatic void setup(void)
34f08c3bdfSopenharmony_ci{
35f08c3bdfSopenharmony_ci	struct passwd *pw;
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_ci	pw = SAFE_GETPWNAM(TEST_USERNAME);
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	SAFE_TOUCH(TEMP_FILE, FILE_MODE, NULL);
40f08c3bdfSopenharmony_ci	SAFE_CHOWN(TEMP_FILE, pw->pw_uid, pw->pw_gid);
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci	tst_res(TINFO, "Switching effective user ID to user: %s", pw->pw_name);
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci	SAFE_SETEUID(pw->pw_uid);
45f08c3bdfSopenharmony_ci}
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_cistatic void run(void)
48f08c3bdfSopenharmony_ci{
49f08c3bdfSopenharmony_ci	struct utimbuf utbuf;
50f08c3bdfSopenharmony_ci	struct stat stat_buf;
51f08c3bdfSopenharmony_ci	time_t pre_time, post_time;
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci	utbuf.modtime = tst_get_fs_timestamp() - 5;
54f08c3bdfSopenharmony_ci	utbuf.actime = utbuf.modtime + 1;
55f08c3bdfSopenharmony_ci	TST_EXP_PASS_SILENT(utime(TEMP_FILE, &utbuf));
56f08c3bdfSopenharmony_ci	SAFE_STAT(TEMP_FILE, &stat_buf);
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(stat_buf.st_atime, utbuf.actime);
59f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(stat_buf.st_mtime, utbuf.modtime);
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci	pre_time = tst_get_fs_timestamp();
62f08c3bdfSopenharmony_ci	TST_EXP_PASS(utime(TEMP_FILE, NULL), "utime(%s, NULL)", TEMP_FILE);
63f08c3bdfSopenharmony_ci	if (!TST_PASS)
64f08c3bdfSopenharmony_ci		return;
65f08c3bdfSopenharmony_ci	post_time = tst_get_fs_timestamp();
66f08c3bdfSopenharmony_ci	SAFE_STAT(TEMP_FILE, &stat_buf);
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_ci	if (stat_buf.st_mtime < pre_time || stat_buf.st_mtime > post_time)
69f08c3bdfSopenharmony_ci		tst_res(TFAIL, "utime() did not set expected mtime, "
70f08c3bdfSopenharmony_ci				"pre_time: %ld, post_time: %ld, st_mtime: %ld",
71f08c3bdfSopenharmony_ci				pre_time, post_time, stat_buf.st_mtime);
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_ci	if (stat_buf.st_atime < pre_time || stat_buf.st_atime > post_time)
74f08c3bdfSopenharmony_ci		tst_res(TFAIL, "utime() did not set expected atime, "
75f08c3bdfSopenharmony_ci				"pre_time: %ld, post_time: %ld, st_atime: %ld",
76f08c3bdfSopenharmony_ci				pre_time, post_time, stat_buf.st_atime);
77f08c3bdfSopenharmony_ci}
78f08c3bdfSopenharmony_ci
79f08c3bdfSopenharmony_cistatic struct tst_test test = {
80f08c3bdfSopenharmony_ci	.test_all = run,
81f08c3bdfSopenharmony_ci	.setup = setup,
82f08c3bdfSopenharmony_ci	.needs_root = 1,
83f08c3bdfSopenharmony_ci	.mntpoint = MNT_POINT,
84f08c3bdfSopenharmony_ci	.mount_device = 1,
85f08c3bdfSopenharmony_ci	.all_filesystems = 1,
86f08c3bdfSopenharmony_ci	.skip_filesystems = (const char *const[]) {
87f08c3bdfSopenharmony_ci		"vfat",
88f08c3bdfSopenharmony_ci		"exfat",
89f08c3bdfSopenharmony_ci		NULL
90f08c3bdfSopenharmony_ci	}
91f08c3bdfSopenharmony_ci};
92