1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker * 07/2001 John George
5*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2022 SUSE LLC Avinesh Kumar <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker */
7*49cdfc7eSAndroid Build Coastguard Worker
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker *
11*49cdfc7eSAndroid Build Coastguard Worker * Verify that system call utime() fails with
12*49cdfc7eSAndroid Build Coastguard Worker *
13*49cdfc7eSAndroid Build Coastguard Worker * - EACCES when times argument is NULL and user does not have rights to modify
14*49cdfc7eSAndroid Build Coastguard Worker * the file.
15*49cdfc7eSAndroid Build Coastguard Worker * - ENOENT when specified file does not exist.
16*49cdfc7eSAndroid Build Coastguard Worker * - EPERM when times argument is not NULL and user does not have rights to
17*49cdfc7eSAndroid Build Coastguard Worker * modify the file.
18*49cdfc7eSAndroid Build Coastguard Worker * - EROFS when the path resides on a read-only filesystem.
19*49cdfc7eSAndroid Build Coastguard Worker */
20*49cdfc7eSAndroid Build Coastguard Worker
21*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <utime.h>
23*49cdfc7eSAndroid Build Coastguard Worker
24*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
25*49cdfc7eSAndroid Build Coastguard Worker
26*49cdfc7eSAndroid Build Coastguard Worker #define TEMP_FILE "tmp_file"
27*49cdfc7eSAndroid Build Coastguard Worker #define MNT_POINT "mntpoint"
28*49cdfc7eSAndroid Build Coastguard Worker #define FILE_MODE 0644
29*49cdfc7eSAndroid Build Coastguard Worker #define TEST_USERNAME "nobody"
30*49cdfc7eSAndroid Build Coastguard Worker
31*49cdfc7eSAndroid Build Coastguard Worker static const struct utimbuf times;
32*49cdfc7eSAndroid Build Coastguard Worker
33*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
34*49cdfc7eSAndroid Build Coastguard Worker char *pathname;
35*49cdfc7eSAndroid Build Coastguard Worker int exp_errno;
36*49cdfc7eSAndroid Build Coastguard Worker const struct utimbuf *utimbuf;
37*49cdfc7eSAndroid Build Coastguard Worker char *err_desc;
38*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
39*49cdfc7eSAndroid Build Coastguard Worker {TEMP_FILE, EACCES, NULL, "No write access"},
40*49cdfc7eSAndroid Build Coastguard Worker {"", ENOENT, NULL, "File not exist"},
41*49cdfc7eSAndroid Build Coastguard Worker {TEMP_FILE, EPERM, ×, "Not file owner"},
42*49cdfc7eSAndroid Build Coastguard Worker {MNT_POINT, EROFS, NULL, "Read-only filesystem"}
43*49cdfc7eSAndroid Build Coastguard Worker };
44*49cdfc7eSAndroid Build Coastguard Worker
45*49cdfc7eSAndroid Build Coastguard Worker
setup(void)46*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
47*49cdfc7eSAndroid Build Coastguard Worker {
48*49cdfc7eSAndroid Build Coastguard Worker struct passwd *pw;
49*49cdfc7eSAndroid Build Coastguard Worker
50*49cdfc7eSAndroid Build Coastguard Worker SAFE_TOUCH(TEMP_FILE, FILE_MODE, NULL);
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard Worker pw = SAFE_GETPWNAM(TEST_USERNAME);
53*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Switching effective user ID to user: %s", pw->pw_name);
54*49cdfc7eSAndroid Build Coastguard Worker SAFE_SETEUID(pw->pw_uid);
55*49cdfc7eSAndroid Build Coastguard Worker }
56*49cdfc7eSAndroid Build Coastguard Worker
run(unsigned int i)57*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int i)
58*49cdfc7eSAndroid Build Coastguard Worker {
59*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[i];
60*49cdfc7eSAndroid Build Coastguard Worker
61*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FAIL(utime(tc->pathname, tc->utimbuf),
62*49cdfc7eSAndroid Build Coastguard Worker tc->exp_errno, "%s", tc->err_desc);
63*49cdfc7eSAndroid Build Coastguard Worker }
64*49cdfc7eSAndroid Build Coastguard Worker
65*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
66*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
67*49cdfc7eSAndroid Build Coastguard Worker .test = run,
68*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
69*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
70*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
71*49cdfc7eSAndroid Build Coastguard Worker .mntpoint = MNT_POINT,
72*49cdfc7eSAndroid Build Coastguard Worker .needs_rofs = 1
73*49cdfc7eSAndroid Build Coastguard Worker };
74