1 /*
2 * Copyright (c) International Business Machines Corp., 2001
3 * 07/2001 Ported by Wayne Boyer
4 * 06/2017 Modified by Guangwen Feng <fenggw-fnst@cn.fujitsu.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /*
22 * DESCRIPTION
23 * Open a file with oflag = O_CREAT set, does it set the sticky bit off?
24 * Open a dir with O_DIRECTORY, does it set the S_IFDIR bit on?
25 *
26 * ALGORITHM
27 * 1. open a new file with O_CREAT, fstat.st_mode should not have the
28 * 01000 bit on. In Linux, the save text bit is *NOT* cleared.
29 * 2. open a new dir with O_DIRECTORY, fstat.st_mode should have the
30 * 040000 bit on.
31 */
32
33 #define _GNU_SOURCE /* for O_DIRECTORY */
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <errno.h>
38 #include "tst_test.h"
39
40 #define TEST_FILE "testfile"
41 #define TEST_DIR "testdir"
42
43 static int fd;
44
45 static struct tcase {
46 char *filename;
47 int flag;
48 mode_t mode;
49 unsigned short tst_bit;
50 char *desc;
51 } tcases[] = {
52 {TEST_FILE, O_RDWR | O_CREAT, 01444, S_ISVTX, "Sticky bit"},
53 {TEST_DIR, O_DIRECTORY, 0, S_IFDIR, "Directory bit"}
54 };
55
verify_open(unsigned int n)56 static void verify_open(unsigned int n)
57 {
58 struct tcase *tc = &tcases[n];
59 struct stat buf;
60
61 TEST(open(tc->filename, tc->flag, tc->mode));
62 fd = TST_RET;
63 if (fd == -1) {
64 tst_res(TFAIL, "Cannot open a file");
65 return;
66 }
67
68 SAFE_FSTAT(fd, &buf);
69 if (!(buf.st_mode & tc->tst_bit))
70 tst_res(TFAIL, "%s is cleared unexpectedly", tc->desc);
71 else
72 tst_res(TPASS, "%s is set as expected", tc->desc);
73
74 SAFE_CLOSE(fd);
75 if (S_ISREG(buf.st_mode))
76 SAFE_UNLINK(tc->filename);
77 }
78
setup(void)79 static void setup(void)
80 {
81 SAFE_MKDIR(TEST_DIR, 0755);
82 }
83
cleanup(void)84 static void cleanup(void)
85 {
86 if (fd > 0)
87 SAFE_CLOSE(fd);
88 }
89
90 static struct tst_test test = {
91 .tcnt = ARRAY_SIZE(tcases),
92 .needs_tmpdir = 1,
93 .setup = setup,
94 .cleanup = cleanup,
95 .test = verify_open,
96 };
97