blob: eb9f86b394da4732876008523871d4a2dc6b1487 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * NAME
subrata_modak56207ce2009-03-23 13:35:39 +000022 * open01.c
plars865695b2001-08-27 22:15:12 +000023 *
24 * DESCRIPTION
subrata_modak56207ce2009-03-23 13:35:39 +000025 * Open a file with oflag = O_CREAT set, does it set the sticky bit off?
plars865695b2001-08-27 22:15:12 +000026 *
27 * Open "/tmp" with O_DIRECTORY, does it set the S_IFDIR bit on?
28 *
29 * ALGORITHM
subrata_modak56207ce2009-03-23 13:35:39 +000030 * 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.
plars865695b2001-08-27 22:15:12 +000032 *
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
subrata_modak56207ce2009-03-23 13:35:39 +000049 * None
plars865695b2001-08-27 22:15:12 +000050 */
plars7b6a0672002-06-05 12:51:39 +000051#define _GNU_SOURCE /* for O_DIRECTORY */
plars865695b2001-08-27 22:15:12 +000052#include <sys/types.h>
53#include <sys/stat.h>
plars7b6a0672002-06-05 12:51:39 +000054#include <fcntl.h>
plars865695b2001-08-27 22:15:12 +000055#include <errno.h>
56#include "test.h"
plars865695b2001-08-27 22:15:12 +000057
58char *TCID = "open01";
59int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000060
Wanlong Gaob2c141f2013-02-04 17:11:18 +080061static char pfilname[40] = "";
plars865695b2001-08-27 22:15:12 +000062
Wanlong Gaob2c141f2013-02-04 17:11:18 +080063static void cleanup(void);
64static void setup(void);
plars865695b2001-08-27 22:15:12 +000065
plars74948ad2002-11-14 16:16:14 +000066int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000067{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020068 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020069 const char *msg;
plars865695b2001-08-27 22:15:12 +000070
71 struct stat statbuf;
72 int fildes;
73 unsigned short filmode;
74
75 /*
76 * parse standard command line options
77 */
Wanlong Gaob2c141f2013-02-04 17:11:18 +080078 msg = parse_opts(ac, av, NULL, NULL);
79 if (msg != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080080 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000081
Wanlong Gaob2c141f2013-02-04 17:11:18 +080082 setup();
plars865695b2001-08-27 22:15:12 +000083
84 /*
85 * check looping state if -i option given on the command line
86 */
87 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080088 tst_count = 0; /* reset tst_count while looping. */
plars865695b2001-08-27 22:15:12 +000089
90 /* test #1 */
91 TEST(open(pfilname, O_RDWR | O_CREAT, 01444));
92
Wanlong Gaob2c141f2013-02-04 17:11:18 +080093 fildes = TEST_RETURN;
94 if (fildes == -1) {
plars865695b2001-08-27 22:15:12 +000095 tst_resm(TFAIL, "Cannot open %s", pfilname);
96 continue;
97 }
98
Cyril Hrubise38b9612014-06-02 17:20:57 +020099 fstat(fildes, &statbuf);
100 filmode = statbuf.st_mode;
101 if (!(filmode & S_ISVTX)) {
102 tst_resm(TFAIL, "Save test bit cleared, but "
103 "should not have been");
plars865695b2001-08-27 22:15:12 +0000104 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200105 tst_resm(TPASS, "Save text bit not cleared "
106 "as expected");
plars865695b2001-08-27 22:15:12 +0000107 }
108
109 /* test #2 */
110 TEST(open("/tmp", O_DIRECTORY));
111
subrata_modak56207ce2009-03-23 13:35:39 +0000112 if (TEST_RETURN == -1) {
113 tst_resm(TFAIL, "open of /tmp failed, errno: %d",
114 TEST_ERRNO);
plars865695b2001-08-27 22:15:12 +0000115 continue;
subrata_modak56207ce2009-03-23 13:35:39 +0000116 }
plars865695b2001-08-27 22:15:12 +0000117
Cyril Hrubise38b9612014-06-02 17:20:57 +0200118 fstat(TEST_RETURN, &statbuf);
119 filmode = statbuf.st_mode;
120 if (!(filmode & S_IFDIR)) {
121 tst_resm(TFAIL, "directory bit cleared, but "
122 "should not have been");
plars865695b2001-08-27 22:15:12 +0000123 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200124 tst_resm(TPASS, "directory bit is set "
125 "as expected");
plars865695b2001-08-27 22:15:12 +0000126 }
127
128 /* clean up things is case we are looping */
Wanlong Gaob2c141f2013-02-04 17:11:18 +0800129 if (close(fildes) == -1)
plars865695b2001-08-27 22:15:12 +0000130 tst_brkm(TBROK, cleanup, "close #1 failed");
plars865695b2001-08-27 22:15:12 +0000131
Wanlong Gaob2c141f2013-02-04 17:11:18 +0800132 if (unlink(pfilname) == -1)
plars865695b2001-08-27 22:15:12 +0000133 tst_brkm(TBROK, cleanup, "can't remove file");
plars865695b2001-08-27 22:15:12 +0000134
Wanlong Gaob2c141f2013-02-04 17:11:18 +0800135 if (close(TEST_RETURN) == -1)
plars865695b2001-08-27 22:15:12 +0000136 tst_brkm(TBROK, cleanup, "close #2 failed");
plars865695b2001-08-27 22:15:12 +0000137 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000138
Wanlong Gaob2c141f2013-02-04 17:11:18 +0800139 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800140 tst_exit();
plars865695b2001-08-27 22:15:12 +0000141}
142
Wanlong Gaob2c141f2013-02-04 17:11:18 +0800143static void setup(void)
plars865695b2001-08-27 22:15:12 +0000144{
145 umask(0);
146
plars865695b2001-08-27 22:15:12 +0000147 tst_sig(NOFORK, DEF_HANDLER, cleanup);
148
plars865695b2001-08-27 22:15:12 +0000149 TEST_PAUSE;
150
plars865695b2001-08-27 22:15:12 +0000151 tst_tmpdir();
152
153 sprintf(pfilname, "open3.%d", getpid());
154}
155
Wanlong Gaob2c141f2013-02-04 17:11:18 +0800156static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000157{
plars865695b2001-08-27 22:15:12 +0000158 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700159}