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