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