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 * Ported to LTP: Wayne Boyer
5*49cdfc7eSAndroid Build Coastguard Worker * 06/2017 Modified by Guangwen Feng <[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 * Open a file with oflag = O_CREAT set, does it set the sticky bit off?
11*49cdfc7eSAndroid Build Coastguard Worker * Open a dir with O_DIRECTORY, does it set the S_IFDIR bit on?
12*49cdfc7eSAndroid Build Coastguard Worker *
13*49cdfc7eSAndroid Build Coastguard Worker * ALGORITHM
14*49cdfc7eSAndroid Build Coastguard Worker * 1. open a new file with O_CREAT, fstat.st_mode should not have the
15*49cdfc7eSAndroid Build Coastguard Worker * 01000 bit on. In Linux, the save text bit is *NOT* cleared.
16*49cdfc7eSAndroid Build Coastguard Worker * 2. open a new dir with O_DIRECTORY, fstat.st_mode should have the
17*49cdfc7eSAndroid Build Coastguard Worker * 040000 bit on.
18*49cdfc7eSAndroid Build Coastguard Worker */
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE /* for O_DIRECTORY */
21*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE "testfile"
28*49cdfc7eSAndroid Build Coastguard Worker #define TEST_DIR "testdir"
29*49cdfc7eSAndroid Build Coastguard Worker
30*49cdfc7eSAndroid Build Coastguard Worker static int fd;
31*49cdfc7eSAndroid Build Coastguard Worker
32*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
33*49cdfc7eSAndroid Build Coastguard Worker char *filename;
34*49cdfc7eSAndroid Build Coastguard Worker int flag;
35*49cdfc7eSAndroid Build Coastguard Worker mode_t mode;
36*49cdfc7eSAndroid Build Coastguard Worker unsigned short tst_bit;
37*49cdfc7eSAndroid Build Coastguard Worker char *desc;
38*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
39*49cdfc7eSAndroid Build Coastguard Worker {TEST_FILE, O_RDWR | O_CREAT, 01444, S_ISVTX, "sticky bit"},
40*49cdfc7eSAndroid Build Coastguard Worker {TEST_DIR, O_DIRECTORY, 0, S_IFDIR, "sirectory bit"}
41*49cdfc7eSAndroid Build Coastguard Worker };
42*49cdfc7eSAndroid Build Coastguard Worker
verify_open(unsigned int n)43*49cdfc7eSAndroid Build Coastguard Worker static void verify_open(unsigned int n)
44*49cdfc7eSAndroid Build Coastguard Worker {
45*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[n];
46*49cdfc7eSAndroid Build Coastguard Worker struct stat buf;
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_FD_SILENT(open(tc->filename, tc->flag, tc->mode),
49*49cdfc7eSAndroid Build Coastguard Worker "open() with %s", tc->desc);
50*49cdfc7eSAndroid Build Coastguard Worker if (!TST_PASS)
51*49cdfc7eSAndroid Build Coastguard Worker return;
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker fd = TST_RET;
54*49cdfc7eSAndroid Build Coastguard Worker
55*49cdfc7eSAndroid Build Coastguard Worker SAFE_FSTAT(fd, &buf);
56*49cdfc7eSAndroid Build Coastguard Worker if (!(buf.st_mode & tc->tst_bit))
57*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "%s is cleared unexpectedly", tc->desc);
58*49cdfc7eSAndroid Build Coastguard Worker else
59*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "%s is set as expected", tc->desc);
60*49cdfc7eSAndroid Build Coastguard Worker
61*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
62*49cdfc7eSAndroid Build Coastguard Worker if (S_ISREG(buf.st_mode))
63*49cdfc7eSAndroid Build Coastguard Worker SAFE_UNLINK(tc->filename);
64*49cdfc7eSAndroid Build Coastguard Worker }
65*49cdfc7eSAndroid Build Coastguard Worker
setup(void)66*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
67*49cdfc7eSAndroid Build Coastguard Worker {
68*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR(TEST_DIR, 0755);
69*49cdfc7eSAndroid Build Coastguard Worker }
70*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)71*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
72*49cdfc7eSAndroid Build Coastguard Worker {
73*49cdfc7eSAndroid Build Coastguard Worker if (fd > 0)
74*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
75*49cdfc7eSAndroid Build Coastguard Worker }
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
78*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
79*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
80*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
81*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
82*49cdfc7eSAndroid Build Coastguard Worker .test = verify_open,
83*49cdfc7eSAndroid Build Coastguard Worker };
84