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 |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 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" |
| 59 | #include "usctest.h" |
| 60 | |
| 61 | char *TCID = "creat04"; |
| 62 | int TST_TOTAL = 2; |
| 63 | extern int Tst_count; |
| 64 | |
| 65 | void setup(void); |
| 66 | void cleanup(void); |
| 67 | |
| 68 | #define FMODE 0444 |
| 69 | #define DMODE 00700 |
| 70 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 71 | int exp_enos[] = { EACCES, 0 }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | |
| 73 | char user1name[] = "nobody"; |
| 74 | char good_dir[40] = "testdir"; |
| 75 | char fname[40], fname1[40]; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 76 | extern struct passwd *my_getpwnam(char *); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | struct passwd *ltpuser1; |
| 78 | |
| 79 | struct test_case_t { |
| 80 | char *fname; |
| 81 | } TC[] = { |
| 82 | /* comment */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 83 | { |
| 84 | fname}, |
| 85 | /* comment */ |
| 86 | { |
| 87 | fname1} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 90 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 92 | int lc; /* loop counter */ |
| 93 | int retval = 0; |
| 94 | char *msg; /* message returned from parse_opts */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | |
| 96 | pid_t pid, pid1; |
| 97 | int i, e_code, status, fd; |
| 98 | |
| 99 | /* parse standard options */ |
Garrett Cooper | e1f008e | 2010-12-14 00:21:59 -0800 | [diff] [blame^] | 100 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg); |
| 102 | } |
| 103 | |
| 104 | setup(); |
| 105 | |
| 106 | TEST_EXP_ENOS(exp_enos); |
| 107 | |
| 108 | /* check looping state if -i option given */ |
| 109 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 110 | |
| 111 | /* reset Tst_count in case we are looping */ |
| 112 | Tst_count = 0; |
| 113 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 114 | if ((pid = FORK_OR_VFORK()) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | tst_brkm(TBROK, cleanup, "fork() #1 failed"); |
| 116 | } |
| 117 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 118 | if (pid == 0) { /* first child */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | if (mkdir(good_dir, DMODE) != 0) { |
| 120 | tst_resm(TINFO, "mkdir() failed"); |
| 121 | exit(1); |
| 122 | } |
| 123 | if ((fd = open(fname1, O_RDWR | O_CREAT, 0444)) |
| 124 | == -1) { |
| 125 | tst_resm(TINFO, "open() failed"); |
| 126 | exit(1); |
| 127 | } |
| 128 | exit(0); |
| 129 | } |
| 130 | wait(&status); |
| 131 | |
| 132 | /* make sure the child returned a good exit status */ |
| 133 | e_code = status >> 8; |
| 134 | if (e_code != 0) { |
| 135 | tst_brkm(TBROK, cleanup, "child #1 failed"); |
| 136 | } |
| 137 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 138 | if ((pid1 = FORK_OR_VFORK()) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | tst_brkm(TBROK, cleanup, "fork() #2 failed"); |
| 140 | } |
| 141 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 142 | if (pid1 == 0) { /* second child */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | |
| 144 | ltpuser1 = my_getpwnam(user1name); |
| 145 | |
| 146 | if (seteuid(ltpuser1->pw_uid) == -1) { |
vapier | dac6873 | 2009-08-28 12:15:15 +0000 | [diff] [blame] | 147 | tst_resm(TINFO|TERRNO, "setreuid(%d) failed", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | ltpuser1->pw_uid); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | continue; |
| 150 | } |
| 151 | |
| 152 | /* loop through the test cases */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 153 | for (i = 0; i < TST_TOTAL; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | |
| 155 | TEST(creat(TC[i].fname, FMODE)); |
| 156 | |
| 157 | if (TEST_RETURN != -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 158 | retval = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | tst_resm(TFAIL, "call succeeded " |
| 160 | "unexpectedly"); |
| 161 | continue; |
| 162 | } |
| 163 | |
| 164 | TEST_ERROR_LOG(TEST_ERRNO); |
| 165 | |
| 166 | if (TEST_ERRNO != EACCES) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 167 | retval = 1; |
vapier | dac6873 | 2009-08-28 12:15:15 +0000 | [diff] [blame] | 168 | tst_resm(TFAIL|TTERRNO, "Expected EACCES"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | } else { |
| 170 | tst_resm(TPASS, "call failed with " |
| 171 | "expected EACCES error"); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /* reset our ID back to the saved ID - root */ |
| 176 | seteuid(0); |
| 177 | |
| 178 | /* clean up things in case we are looping */ |
| 179 | unlink(fname); |
| 180 | unlink(fname1); |
| 181 | rmdir(good_dir); |
plars | 7286067 | 2001-09-12 16:16:33 +0000 | [diff] [blame] | 182 | exit(retval); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 183 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 184 | } else { /* parent */ |
plars | 7286067 | 2001-09-12 16:16:33 +0000 | [diff] [blame] | 185 | /* wait for the child to finish */ |
| 186 | wait(&status); |
| 187 | /* make sure the child returned a good exit status */ |
| 188 | e_code = status >> 8; |
| 189 | if (e_code != 0) { |
| 190 | tst_resm(TFAIL, "Failures reported above"); |
| 191 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | cleanup(); |
| 195 | |
robbiew | 2c94524 | 2002-11-11 19:01:25 +0000 | [diff] [blame] | 196 | return 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 197 | /*NOTREACHED*/} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 198 | |
| 199 | /* |
| 200 | * setup() - performs all ONE TIME setup for this test. |
| 201 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 202 | void setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 203 | { |
| 204 | /* The test must be run as root */ |
| 205 | if (geteuid() != 0) { |
| 206 | tst_brkm(TBROK, tst_exit, "Test must be run as root"); |
| 207 | } |
| 208 | |
| 209 | /* capture signals */ |
| 210 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 211 | |
| 212 | /* Pause if that option was specified */ |
| 213 | TEST_PAUSE; |
| 214 | |
| 215 | /* make a temporary directory and cd to it */ |
| 216 | tst_tmpdir(); |
| 217 | |
| 218 | sprintf(good_dir, "%s.%d", good_dir, getpid()); |
| 219 | sprintf(fname1, "%s/file1.%d", good_dir, getpid()); |
| 220 | sprintf(fname, "%s/file.%d", good_dir, getpid()); |
| 221 | } |
| 222 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 223 | /* |
| 224 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 225 | * completion or premature exit. |
| 226 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 227 | void cleanup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 228 | { |
| 229 | /* |
| 230 | * print timing stats if that option was specified. |
| 231 | * print errno log if that option was specified. |
| 232 | */ |
| 233 | TEST_CLEANUP; |
| 234 | |
| 235 | /* delete the test directory created in setup() */ |
| 236 | tst_rmdir(); |
| 237 | |
| 238 | /* exit with return code appropriate for results */ |
| 239 | tst_exit(); |
| 240 | } |