plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 22 | * open01.c |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | * |
| 24 | * DESCRIPTION |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 25 | * Open a file with oflag = O_CREAT set, does it set the sticky bit off? |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 26 | * |
| 27 | * Open "/tmp" with O_DIRECTORY, does it set the S_IFDIR bit on? |
| 28 | * |
| 29 | * ALGORITHM |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 49 | * None |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 50 | */ |
plars | 7b6a067 | 2002-06-05 12:51:39 +0000 | [diff] [blame] | 51 | #define _GNU_SOURCE /* for O_DIRECTORY */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | #include <sys/types.h> |
| 53 | #include <sys/stat.h> |
plars | 7b6a067 | 2002-06-05 12:51:39 +0000 | [diff] [blame] | 54 | #include <fcntl.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | #include <errno.h> |
| 56 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | |
| 58 | char *TCID = "open01"; |
| 59 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | |
Wanlong Gao | b2c141f | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 61 | static char pfilname[40] = ""; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | |
Wanlong Gao | b2c141f | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 63 | static void cleanup(void); |
| 64 | static void setup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 66 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 68 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 69 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | |
| 71 | struct stat statbuf; |
| 72 | int fildes; |
| 73 | unsigned short filmode; |
| 74 | |
| 75 | /* |
| 76 | * parse standard command line options |
| 77 | */ |
Wanlong Gao | b2c141f | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 78 | msg = parse_opts(ac, av, NULL, NULL); |
| 79 | if (msg != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 80 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | |
Wanlong Gao | b2c141f | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 82 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | |
| 84 | /* |
| 85 | * check looping state if -i option given on the command line |
| 86 | */ |
| 87 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 88 | tst_count = 0; /* reset tst_count while looping. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | |
| 90 | /* test #1 */ |
| 91 | TEST(open(pfilname, O_RDWR | O_CREAT, 01444)); |
| 92 | |
Wanlong Gao | b2c141f | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 93 | fildes = TEST_RETURN; |
| 94 | if (fildes == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | tst_resm(TFAIL, "Cannot open %s", pfilname); |
| 96 | continue; |
| 97 | } |
| 98 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 99 | 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"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 105 | tst_resm(TPASS, "Save text bit not cleared " |
| 106 | "as expected"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /* test #2 */ |
| 110 | TEST(open("/tmp", O_DIRECTORY)); |
| 111 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 112 | if (TEST_RETURN == -1) { |
| 113 | tst_resm(TFAIL, "open of /tmp failed, errno: %d", |
| 114 | TEST_ERRNO); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | continue; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 116 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 118 | 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"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 124 | tst_resm(TPASS, "directory bit is set " |
| 125 | "as expected"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /* clean up things is case we are looping */ |
Wanlong Gao | b2c141f | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 129 | if (close(fildes) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | tst_brkm(TBROK, cleanup, "close #1 failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | |
Wanlong Gao | b2c141f | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 132 | if (unlink(pfilname) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | tst_brkm(TBROK, cleanup, "can't remove file"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | |
Wanlong Gao | b2c141f | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 135 | if (close(TEST_RETURN) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | tst_brkm(TBROK, cleanup, "close #2 failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 138 | |
Wanlong Gao | b2c141f | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 139 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 140 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Wanlong Gao | b2c141f | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 143 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | { |
| 145 | umask(0); |
| 146 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 148 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | TEST_PAUSE; |
| 150 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | tst_tmpdir(); |
| 152 | |
| 153 | sprintf(pfilname, "open3.%d", getpid()); |
| 154 | } |
| 155 | |
Wanlong Gao | b2c141f | 2013-02-04 17:11:18 +0800 | [diff] [blame] | 156 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 159 | } |