1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 * Ported to LTP: Wayne Boyer
5 * 06/2017 Modified by Guangwen Feng <fenggw-fnst@cn.fujitsu.com>
6 */
7 /*
8 * DESCRIPTION
9 * 1. open a new file without O_CREAT, ENOENT should be returned.
10 * 2. open a file with O_RDONLY | O_NOATIME and the caller was not
11 * privileged, EPERM should be returned.
12 */
13
14 #define _GNU_SOURCE
15
16 #include <stdio.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <pwd.h>
22 #include "tst_test.h"
23
24 #define TEST_FILE "test_file"
25 #define TEST_FILE2 "test_file2"
26
27 static struct tcase {
28 const char *filename;
29 int flag;
30 int exp_errno;
31 const char *desc;
32 } tcases[] = {
33 {TEST_FILE, O_RDWR, ENOENT, "new file without O_CREAT"},
34 {TEST_FILE2, O_RDONLY | O_NOATIME, EPERM, "unpriviledget O_RDONLY | O_NOATIME"},
35 };
36
setup(void)37 void setup(void)
38 {
39 struct passwd *ltpuser;
40
41 SAFE_TOUCH(TEST_FILE2, 0644, NULL);
42
43 ltpuser = SAFE_GETPWNAM("nobody");
44
45 SAFE_SETEUID(ltpuser->pw_uid);
46 }
47
verify_open(unsigned int n)48 static void verify_open(unsigned int n)
49 {
50 struct tcase *tc = &tcases[n];
51
52 TST_EXP_FAIL2(open(tc->filename, tc->flag, 0444),
53 tc->exp_errno, "open() %s", tc->desc);
54 }
55
cleanup(void)56 void cleanup(void)
57 {
58 SAFE_SETEUID(0);
59 }
60
61 static struct tst_test test = {
62 .tcnt = ARRAY_SIZE(tcases),
63 .needs_root = 1,
64 .needs_tmpdir = 1,
65 .setup = setup,
66 .cleanup = cleanup,
67 .test = verify_open,
68 };
69