1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) Crackerjack Project., 2007, Hitachi, Ltd
4 * Author(s): Takahiro Yasui <takahiro.yasui.mp@hitachi.com>
5 * Copyright (c) Linux Test Project, 2014-2018
6 * Ported to LTP: Manas Kumar Nayak maknayak@in.ibm.com>
7 */
8
9 /*\
10 * [Description]
11 *
12 * Verify that, utimes(2) returns -1 and sets errno to
13 *
14 * 1. EACCES if times is NULL, the caller's effective user ID does not match
15 * the owner of the file, the caller does not have write access to the file,
16 * and the caller is not privileged
17 * 2. ENOENT if filename does not exist
18 * 3. EFAULT if filename is NULL
19 * 4. EPERM if times is not NULL, the caller's effective UID does not match
20 * the owner of the file, and the caller is not privileged
21 * 5. EROFS if path resides on a read-only file system
22 */
23
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <pwd.h>
27 #include "tst_test.h"
28 #include "lapi/syscalls.h"
29
30 #define MNT_POINT "mntpoint"
31 #define TESTFILE1 "testfile1"
32 #define TESTFILE2 "testfile2"
33 #define TESTFILE3 "mntpoint/file"
34 #define FILE_MODE (S_IRWXU | S_IRGRP | S_IXGRP | \
35 S_IROTH | S_IXOTH)
36 #define DIR_MODE (S_IRWXU | S_IRWXG | S_IRWXO)
37
38 static struct timeval a_tv[2] = { {0, 0}, {1000, 0} };
39 static struct timeval m_tv[2] = { {1000, 0}, {0, 0} };
40 static struct timeval tv[2] = { {1000, 0}, {2000, 0} };
41
42 static struct tcase {
43 char *pathname;
44 struct timeval *times;
45 int exp_errno;
46 } tcases[] = {
47 { TESTFILE1, a_tv, 0 },
48 { TESTFILE1, m_tv, 0 },
49 { TESTFILE2, NULL, EACCES },
50 { "notexistfile", tv, ENOENT },
51 { NULL, tv, EFAULT },
52 { TESTFILE2, tv, EPERM },
53 { TESTFILE3, tv, EROFS },
54 };
55
setup(void)56 static void setup(void)
57 {
58 struct passwd *ltpuser = SAFE_GETPWNAM("nobody");
59
60 SAFE_TOUCH(TESTFILE2, FILE_MODE, NULL);
61 SAFE_SETEUID(ltpuser->pw_uid);
62 SAFE_TOUCH(TESTFILE1, FILE_MODE, NULL);
63 }
64
utimes_verify(unsigned int i)65 static void utimes_verify(unsigned int i)
66 {
67 struct stat st;
68 struct timeval tmp_tv[2];
69 struct tcase *tc = &tcases[i];
70
71 if (tc->exp_errno == 0) {
72 SAFE_STAT(tc->pathname, &st);
73
74 tmp_tv[0].tv_sec = st.st_atime;
75 tmp_tv[0].tv_usec = 0;
76 tmp_tv[1].tv_sec = st.st_mtime;
77 tmp_tv[1].tv_usec = 0;
78 }
79
80 TEST(utimes(tc->pathname, tc->times));
81
82 if (TST_ERR == tc->exp_errno) {
83 tst_res(TPASS | TTERRNO, "utimes() worked as expected");
84 } else {
85 tst_res(TFAIL | TTERRNO,
86 "utimes() failed unexpectedly; expected: %d - %s",
87 tc->exp_errno, tst_strerrno(tc->exp_errno));
88 }
89
90 if (TST_ERR == 0 && utimes(tc->pathname, tmp_tv) == -1)
91 tst_brk(TBROK | TERRNO, "utimes() failed.");
92 }
93
94 static struct tst_test test = {
95 .setup = setup,
96 .test = utimes_verify,
97 .tcnt = ARRAY_SIZE(tcases),
98 .needs_root = 1,
99 .needs_rofs = 1,
100 .mntpoint = MNT_POINT,
101 };
102