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 values specified by
13f08c3bdfSopenharmony_ci * times argument, under the following constraints:
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci * - The times argument is not 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
25f08c3bdfSopenharmony_ci#define MNT_POINT	"mntpoint"
26f08c3bdfSopenharmony_ci#define TEMP_FILE	MNT_POINT"/tmp_file"
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_ci#define FILE_MODE	0444
29f08c3bdfSopenharmony_ci#define MODE_RWX	0777
30f08c3bdfSopenharmony_ci#define NEW_MODF_TIME	10000
31f08c3bdfSopenharmony_ci#define NEW_ACCESS_TIME	20000
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci#define TEST_USERNAME "nobody"
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_cistatic struct utimbuf times = {
36f08c3bdfSopenharmony_ci	.modtime = NEW_MODF_TIME,
37f08c3bdfSopenharmony_ci	.actime = NEW_ACCESS_TIME
38f08c3bdfSopenharmony_ci};
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_cistatic void setup(void)
41f08c3bdfSopenharmony_ci{
42f08c3bdfSopenharmony_ci	struct passwd *pw;
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci	SAFE_CHMOD(MNT_POINT, MODE_RWX);
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ci	pw = SAFE_GETPWNAM(TEST_USERNAME);
47f08c3bdfSopenharmony_ci	tst_res(TINFO, "Switching effective user ID to user: %s", pw->pw_name);
48f08c3bdfSopenharmony_ci	SAFE_SETEUID(pw->pw_uid);
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci	SAFE_TOUCH(TEMP_FILE, FILE_MODE, NULL);
51f08c3bdfSopenharmony_ci}
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_cistatic void run(void)
54f08c3bdfSopenharmony_ci{
55f08c3bdfSopenharmony_ci	struct stat stat_buf;
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	TST_EXP_PASS(utime(TEMP_FILE, &times), "utime(%s, &times)", TEMP_FILE);
58f08c3bdfSopenharmony_ci	if (!TST_PASS)
59f08c3bdfSopenharmony_ci		return;
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci	SAFE_STAT(TEMP_FILE, &stat_buf);
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(stat_buf.st_mtime, NEW_MODF_TIME);
64f08c3bdfSopenharmony_ci	TST_EXP_EQ_LI(stat_buf.st_atime, NEW_ACCESS_TIME);
65f08c3bdfSopenharmony_ci}
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_cistatic struct tst_test test = {
68f08c3bdfSopenharmony_ci	.test_all = run,
69f08c3bdfSopenharmony_ci	.setup = setup,
70f08c3bdfSopenharmony_ci	.needs_root = 1,
71f08c3bdfSopenharmony_ci	.mount_device = 1,
72f08c3bdfSopenharmony_ci	.mntpoint = MNT_POINT,
73f08c3bdfSopenharmony_ci	.all_filesystems = 1,
74f08c3bdfSopenharmony_ci	.skip_filesystems = (const char *const[]) {
75f08c3bdfSopenharmony_ci		"vfat",
76f08c3bdfSopenharmony_ci		"exfat",
77f08c3bdfSopenharmony_ci		NULL
78f08c3bdfSopenharmony_ci	}
79f08c3bdfSopenharmony_ci};
80