blob: 5c5c68d307d7cac061db70773e870092ee902efa [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
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>
plars865695b2001-08-27 22:15:12 +000055#include <sys/types.h>
56#include <sys/stat.h>
57#include <sys/wait.h>
plars865695b2001-08-27 22:15:12 +000058#include "test.h"
59#include "usctest.h"
60
61char *TCID = "creat04";
62int TST_TOTAL = 2;
63extern int Tst_count;
64
65void setup(void);
66void cleanup(void);
67
68#define FMODE 0444
69#define DMODE 00700
70
subrata_modak56207ce2009-03-23 13:35:39 +000071int exp_enos[] = { EACCES, 0 };
plars865695b2001-08-27 22:15:12 +000072
73char user1name[] = "nobody";
74char good_dir[40] = "testdir";
75char fname[40], fname1[40];
subrata_modak56207ce2009-03-23 13:35:39 +000076extern struct passwd *my_getpwnam(char *);
plars865695b2001-08-27 22:15:12 +000077struct passwd *ltpuser1;
78
79struct test_case_t {
80 char *fname;
81} TC[] = {
82 /* comment */
subrata_modak56207ce2009-03-23 13:35:39 +000083 {
84 fname},
85 /* comment */
86 {
87 fname1}
plars865695b2001-08-27 22:15:12 +000088};
89
subrata_modak56207ce2009-03-23 13:35:39 +000090int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000091{
subrata_modak56207ce2009-03-23 13:35:39 +000092 int lc; /* loop counter */
93 int retval = 0;
94 char *msg; /* message returned from parse_opts */
plars865695b2001-08-27 22:15:12 +000095
96 pid_t pid, pid1;
97 int i, e_code, status, fd;
98
99 /* parse standard options */
Garrett Coopere1f008e2010-12-14 00:21:59 -0800100 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
plars865695b2001-08-27 22:15:12 +0000101 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
robbiewd34d5812005-07-11 22:28:09 +0000114 if ((pid = FORK_OR_VFORK()) == -1) {
plars865695b2001-08-27 22:15:12 +0000115 tst_brkm(TBROK, cleanup, "fork() #1 failed");
116 }
117
subrata_modak56207ce2009-03-23 13:35:39 +0000118 if (pid == 0) { /* first child */
plars865695b2001-08-27 22:15:12 +0000119 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
robbiewd34d5812005-07-11 22:28:09 +0000138 if ((pid1 = FORK_OR_VFORK()) == -1) {
plars865695b2001-08-27 22:15:12 +0000139 tst_brkm(TBROK, cleanup, "fork() #2 failed");
140 }
141
subrata_modak56207ce2009-03-23 13:35:39 +0000142 if (pid1 == 0) { /* second child */
plars865695b2001-08-27 22:15:12 +0000143
144 ltpuser1 = my_getpwnam(user1name);
145
146 if (seteuid(ltpuser1->pw_uid) == -1) {
vapierdac68732009-08-28 12:15:15 +0000147 tst_resm(TINFO|TERRNO, "setreuid(%d) failed",
plars865695b2001-08-27 22:15:12 +0000148 ltpuser1->pw_uid);
plars865695b2001-08-27 22:15:12 +0000149 continue;
150 }
151
152 /* loop through the test cases */
subrata_modak56207ce2009-03-23 13:35:39 +0000153 for (i = 0; i < TST_TOTAL; i++) {
plars865695b2001-08-27 22:15:12 +0000154
155 TEST(creat(TC[i].fname, FMODE));
156
157 if (TEST_RETURN != -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000158 retval = 1;
plars865695b2001-08-27 22:15:12 +0000159 tst_resm(TFAIL, "call succeeded "
160 "unexpectedly");
161 continue;
162 }
163
164 TEST_ERROR_LOG(TEST_ERRNO);
165
166 if (TEST_ERRNO != EACCES) {
subrata_modak56207ce2009-03-23 13:35:39 +0000167 retval = 1;
vapierdac68732009-08-28 12:15:15 +0000168 tst_resm(TFAIL|TTERRNO, "Expected EACCES");
plars865695b2001-08-27 22:15:12 +0000169 } 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);
plars72860672001-09-12 16:16:33 +0000182 exit(retval);
plars865695b2001-08-27 22:15:12 +0000183
subrata_modak56207ce2009-03-23 13:35:39 +0000184 } else { /* parent */
plars72860672001-09-12 16:16:33 +0000185 /* 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 }
plars865695b2001-08-27 22:15:12 +0000192 }
193 }
194 cleanup();
195
robbiew2c945242002-11-11 19:01:25 +0000196 return 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000197 /*NOTREACHED*/}
plars865695b2001-08-27 22:15:12 +0000198
199/*
200 * setup() - performs all ONE TIME setup for this test.
201 */
subrata_modak56207ce2009-03-23 13:35:39 +0000202void setup()
plars865695b2001-08-27 22:15:12 +0000203{
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
plars865695b2001-08-27 22:15:12 +0000223/*
224 * cleanup() - performs all ONE TIME cleanup for this test at
225 * completion or premature exit.
226 */
subrata_modak56207ce2009-03-23 13:35:39 +0000227void cleanup()
plars865695b2001-08-27 22:15:12 +0000228{
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}