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 |
| 22 | * creat04.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * testcase to check creat(2) fails with EACCES |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * 1. A parent spawns a child, which creates a test-directory, and |
| 29 | * makes it 700. Then the parent spawns another child, does a |
| 30 | * setreuid to ltpuser1, and attempts to creat() a file (which |
| 31 | * does not exist) in the directory created by the first child. |
| 32 | * 2. Repeat 1, for a file (to be created by ltpuser1) which exists. |
| 33 | * That is the first child should creat() a file before exiting. |
| 34 | * |
| 35 | * USAGE: <for command-line> |
| 36 | * creat04 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 37 | * where, -c n : Run n copies concurrently. |
| 38 | * -e : Turn on errno logging. |
| 39 | * -i n : Execute test n times. |
| 40 | * -I x : Execute test for x seconds. |
| 41 | * -P x : Pause for x seconds between iterations. |
| 42 | * -t : Turn on syscall timing. |
| 43 | * |
| 44 | * HISTORY |
| 45 | * 07/2001 Ported by Wayne Boyer |
| 46 | * |
| 47 | * RESTRICTIONS |
| 48 | * Test must be run as root. |
| 49 | */ |
| 50 | |
| 51 | #include <stdio.h> |
| 52 | #include <errno.h> |
| 53 | #include <fcntl.h> |
| 54 | #include <pwd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | #include <sys/types.h> |
| 56 | #include <sys/stat.h> |
| 57 | #include <sys/wait.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | |
| 60 | char *TCID = "creat04"; |
| 61 | int TST_TOTAL = 2; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | |
| 63 | void setup(void); |
| 64 | void cleanup(void); |
| 65 | |
| 66 | #define FMODE 0444 |
| 67 | #define DMODE 00700 |
| 68 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | char user1name[] = "nobody"; |
| 70 | char good_dir[40] = "testdir"; |
| 71 | char fname[40], fname1[40]; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 72 | extern struct passwd *my_getpwnam(char *); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | struct passwd *ltpuser1; |
| 74 | |
| 75 | struct test_case_t { |
| 76 | char *fname; |
| 77 | } TC[] = { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 78 | { |
| 79 | fname}, { |
| 80 | fname1} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 83 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 85 | int lc; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 86 | int retval = 0; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 87 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | |
| 89 | pid_t pid, pid1; |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 90 | int i, status, fd; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 92 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 93 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | |
| 95 | setup(); |
| 96 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 98 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 99 | /* reset tst_count in case we are looping */ |
| 100 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 102 | if ((pid = FORK_OR_VFORK()) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 103 | tst_brkm(TBROK | TERRNO, cleanup, "fork() #1 failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | } |
| 105 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 106 | if (pid == 0) { /* first child */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | if (mkdir(good_dir, DMODE) != 0) { |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 108 | perror("mkdir() failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | exit(1); |
| 110 | } |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 111 | if ((fd = open(fname1, O_RDWR | O_CREAT, 0444)) == -1) { |
| 112 | perror("open failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | exit(1); |
| 114 | } |
| 115 | exit(0); |
| 116 | } |
| 117 | wait(&status); |
| 118 | |
| 119 | /* make sure the child returned a good exit status */ |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 120 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | tst_brkm(TBROK, cleanup, "child #1 failed"); |
| 122 | } |
| 123 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 124 | if ((pid1 = FORK_OR_VFORK()) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 125 | tst_brkm(TBROK | TERRNO, cleanup, "fork() #2 failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | } |
| 127 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 128 | if (pid1 == 0) { /* second child */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | |
| 130 | ltpuser1 = my_getpwnam(user1name); |
| 131 | |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 132 | if (ltpuser1 == NULL) { |
| 133 | perror("getpwnam"); |
| 134 | exit(1); |
| 135 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | if (seteuid(ltpuser1->pw_uid) == -1) { |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 137 | perror("seteuid"); |
| 138 | exit(1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /* loop through the test cases */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 142 | for (i = 0; i < TST_TOTAL; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | |
| 144 | TEST(creat(TC[i].fname, FMODE)); |
| 145 | |
| 146 | if (TEST_RETURN != -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 147 | retval = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | tst_resm(TFAIL, "call succeeded " |
| 149 | "unexpectedly"); |
| 150 | continue; |
| 151 | } |
| 152 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | if (TEST_ERRNO != EACCES) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 154 | retval = 1; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 155 | tst_resm(TFAIL | TTERRNO, |
| 156 | "Expected EACCES"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | } else { |
| 158 | tst_resm(TPASS, "call failed with " |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 159 | "EACCES as expected"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | seteuid(0); |
| 164 | |
| 165 | /* clean up things in case we are looping */ |
| 166 | unlink(fname); |
| 167 | unlink(fname1); |
| 168 | rmdir(good_dir); |
plars | 7286067 | 2001-09-12 16:16:33 +0000 | [diff] [blame] | 169 | exit(retval); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 170 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 171 | } else { /* parent */ |
plars | 7286067 | 2001-09-12 16:16:33 +0000 | [diff] [blame] | 172 | /* wait for the child to finish */ |
| 173 | wait(&status); |
| 174 | /* make sure the child returned a good exit status */ |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 175 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 176 | tst_resm(TFAIL, "see failures reported above"); |
plars | 7286067 | 2001-09-12 16:16:33 +0000 | [diff] [blame] | 177 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | cleanup(); |
| 181 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 182 | tst_exit(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 183 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 184 | |
| 185 | /* |
| 186 | * setup() - performs all ONE TIME setup for this test. |
| 187 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 188 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 189 | { |
Garrett Cooper | 87bc45c | 2010-12-17 01:56:08 -0800 | [diff] [blame] | 190 | tst_require_root(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 192 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 193 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 194 | TEST_PAUSE; |
| 195 | |
| 196 | /* make a temporary directory and cd to it */ |
| 197 | tst_tmpdir(); |
| 198 | |
| 199 | sprintf(good_dir, "%s.%d", good_dir, getpid()); |
| 200 | sprintf(fname1, "%s/file1.%d", good_dir, getpid()); |
| 201 | sprintf(fname, "%s/file.%d", good_dir, getpid()); |
| 202 | } |
| 203 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | /* |
| 205 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 206 | * completion or premature exit. |
| 207 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 208 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 209 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 210 | |
| 211 | /* delete the test directory created in setup() */ |
| 212 | tst_rmdir(); |
| 213 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 214 | } |