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