1 /*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /*
21 * NAME
22 * open01.c
23 *
24 * DESCRIPTION
25 * Open a file with oflag = O_CREAT set, does it set the sticky bit off?
26 *
27 * Open "/tmp" with O_DIRECTORY, does it set the S_IFDIR bit on?
28 *
29 * ALGORITHM
30 * 1. open a new file with O_CREAT, fstat.st_mode should not have the
31 * 01000 bit on. In Linux, the save text bit is *NOT* cleared.
32 *
33 * 2. open "/tmp" with O_DIRECTORY. fstat.st_mode should have the
34 * 040000 bit on.
35 *
36 * USAGE: <for command-line>
37 * open01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
38 * where, -c n : Run n copies concurrently.
39 * -f : Turn off functionality Testing.
40 * -i n : Execute test n times.
41 * -I x : Execute test for x seconds.
42 * -P x : Pause for x seconds between iterations.
43 * -t : Turn on syscall timing.
44 *
45 * HISTORY
46 * 07/2001 Ported by Wayne Boyer
47 *
48 * RESTRICTIONS
49 * None
50 */
51 #define _GNU_SOURCE /* for O_DIRECTORY */
52 #include <sys/types.h>
53 #include <sys/stat.h>
54 #include <fcntl.h>
55 #include <errno.h>
56 #include "test.h"
57
58 char *TCID = "open01";
59 int TST_TOTAL = 1;
60
61 static char pfilname[40] = "";
62
63 static void cleanup(void);
64 static void setup(void);
65
main(int ac,char ** av)66 int main(int ac, char **av)
67 {
68 int lc;
69
70 struct stat statbuf;
71 int fildes;
72 unsigned short filmode;
73
74 /*
75 * parse standard command line options
76 */
77 tst_parse_opts(ac, av, NULL, NULL);
78
79 setup();
80
81 /*
82 * check looping state if -i option given on the command line
83 */
84 for (lc = 0; TEST_LOOPING(lc); lc++) {
85 tst_count = 0; /* reset tst_count while looping. */
86
87 /* test #1 */
88 TEST(open(pfilname, O_RDWR | O_CREAT, 01444));
89
90 fildes = TEST_RETURN;
91 if (fildes == -1) {
92 tst_resm(TFAIL, "Cannot open %s", pfilname);
93 continue;
94 }
95
96 fstat(fildes, &statbuf);
97 filmode = statbuf.st_mode;
98 if (!(filmode & S_ISVTX)) {
99 tst_resm(TFAIL, "Save test bit cleared, but "
100 "should not have been");
101 } else {
102 tst_resm(TPASS, "Save text bit not cleared "
103 "as expected");
104 }
105
106 /* test #2 */
107 TEST(open("/tmp", O_DIRECTORY));
108
109 if (TEST_RETURN == -1) {
110 tst_resm(TFAIL, "open of /tmp failed, errno: %d",
111 TEST_ERRNO);
112 continue;
113 }
114
115 fstat(TEST_RETURN, &statbuf);
116 filmode = statbuf.st_mode;
117 if (!(filmode & S_IFDIR)) {
118 tst_resm(TFAIL, "directory bit cleared, but "
119 "should not have been");
120 } else {
121 tst_resm(TPASS, "directory bit is set "
122 "as expected");
123 }
124
125 /* clean up things is case we are looping */
126 if (close(fildes) == -1)
127 tst_brkm(TBROK, cleanup, "close #1 failed");
128
129 if (unlink(pfilname) == -1)
130 tst_brkm(TBROK, cleanup, "can't remove file");
131
132 if (close(TEST_RETURN) == -1)
133 tst_brkm(TBROK, cleanup, "close #2 failed");
134 }
135
136 cleanup();
137 tst_exit();
138 }
139
setup(void)140 static void setup(void)
141 {
142 umask(0);
143
144 tst_sig(NOFORK, DEF_HANDLER, cleanup);
145
146 TEST_PAUSE;
147
148 tst_tmpdir();
149
150 sprintf(pfilname, "open3.%d", getpid());
151 }
152
cleanup(void)153 static void cleanup(void)
154 {
155 tst_rmdir();
156 }
157