blob: dd87406cf9c35e115136f001f40e01d42db880de [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
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"
plars865695b2001-08-27 22:15:12 +000059
60char *TCID = "creat04";
61int TST_TOTAL = 2;
plars865695b2001-08-27 22:15:12 +000062
63void setup(void);
64void cleanup(void);
65
66#define FMODE 0444
67#define DMODE 00700
68
plars865695b2001-08-27 22:15:12 +000069char user1name[] = "nobody";
70char good_dir[40] = "testdir";
71char fname[40], fname1[40];
subrata_modak56207ce2009-03-23 13:35:39 +000072extern struct passwd *my_getpwnam(char *);
plars865695b2001-08-27 22:15:12 +000073struct passwd *ltpuser1;
74
75struct test_case_t {
76 char *fname;
77} TC[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +080078 {
79 fname}, {
80 fname1}
plars865695b2001-08-27 22:15:12 +000081};
82
subrata_modak56207ce2009-03-23 13:35:39 +000083int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000084{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020085 int lc;
subrata_modak56207ce2009-03-23 13:35:39 +000086 int retval = 0;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020087 const char *msg;
plars865695b2001-08-27 22:15:12 +000088
89 pid_t pid, pid1;
Garrett Cooper87bc45c2010-12-17 01:56:08 -080090 int i, status, fd;
plars865695b2001-08-27 22:15:12 +000091
Garrett Cooper87bc45c2010-12-17 01:56:08 -080092 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080093 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000094
95 setup();
96
plars865695b2001-08-27 22:15:12 +000097 for (lc = 0; TEST_LOOPING(lc); lc++) {
98
Caspar Zhangd59a6592013-03-07 14:59:12 +080099 /* reset tst_count in case we are looping */
100 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000101
robbiewd34d5812005-07-11 22:28:09 +0000102 if ((pid = FORK_OR_VFORK()) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800103 tst_brkm(TBROK | TERRNO, cleanup, "fork() #1 failed");
plars865695b2001-08-27 22:15:12 +0000104 }
105
subrata_modak56207ce2009-03-23 13:35:39 +0000106 if (pid == 0) { /* first child */
plars865695b2001-08-27 22:15:12 +0000107 if (mkdir(good_dir, DMODE) != 0) {
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800108 perror("mkdir() failed");
plars865695b2001-08-27 22:15:12 +0000109 exit(1);
110 }
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800111 if ((fd = open(fname1, O_RDWR | O_CREAT, 0444)) == -1) {
112 perror("open failed");
plars865695b2001-08-27 22:15:12 +0000113 exit(1);
114 }
115 exit(0);
116 }
117 wait(&status);
118
119 /* make sure the child returned a good exit status */
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800120 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
plars865695b2001-08-27 22:15:12 +0000121 tst_brkm(TBROK, cleanup, "child #1 failed");
122 }
123
robbiewd34d5812005-07-11 22:28:09 +0000124 if ((pid1 = FORK_OR_VFORK()) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800125 tst_brkm(TBROK | TERRNO, cleanup, "fork() #2 failed");
plars865695b2001-08-27 22:15:12 +0000126 }
127
subrata_modak56207ce2009-03-23 13:35:39 +0000128 if (pid1 == 0) { /* second child */
plars865695b2001-08-27 22:15:12 +0000129
130 ltpuser1 = my_getpwnam(user1name);
131
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800132 if (ltpuser1 == NULL) {
133 perror("getpwnam");
134 exit(1);
135 }
plars865695b2001-08-27 22:15:12 +0000136 if (seteuid(ltpuser1->pw_uid) == -1) {
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800137 perror("seteuid");
138 exit(1);
plars865695b2001-08-27 22:15:12 +0000139 }
140
141 /* loop through the test cases */
subrata_modak56207ce2009-03-23 13:35:39 +0000142 for (i = 0; i < TST_TOTAL; i++) {
plars865695b2001-08-27 22:15:12 +0000143
144 TEST(creat(TC[i].fname, FMODE));
145
146 if (TEST_RETURN != -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000147 retval = 1;
plars865695b2001-08-27 22:15:12 +0000148 tst_resm(TFAIL, "call succeeded "
149 "unexpectedly");
150 continue;
151 }
152
plars865695b2001-08-27 22:15:12 +0000153 if (TEST_ERRNO != EACCES) {
subrata_modak56207ce2009-03-23 13:35:39 +0000154 retval = 1;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800155 tst_resm(TFAIL | TTERRNO,
156 "Expected EACCES");
plars865695b2001-08-27 22:15:12 +0000157 } else {
158 tst_resm(TPASS, "call failed with "
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800159 "EACCES as expected");
plars865695b2001-08-27 22:15:12 +0000160 }
161 }
162
plars865695b2001-08-27 22:15:12 +0000163 seteuid(0);
164
165 /* clean up things in case we are looping */
166 unlink(fname);
167 unlink(fname1);
168 rmdir(good_dir);
plars72860672001-09-12 16:16:33 +0000169 exit(retval);
plars865695b2001-08-27 22:15:12 +0000170
subrata_modak56207ce2009-03-23 13:35:39 +0000171 } else { /* parent */
plars72860672001-09-12 16:16:33 +0000172 /* wait for the child to finish */
173 wait(&status);
174 /* make sure the child returned a good exit status */
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800175 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
176 tst_resm(TFAIL, "see failures reported above");
plars72860672001-09-12 16:16:33 +0000177 }
plars865695b2001-08-27 22:15:12 +0000178 }
179 }
180 cleanup();
181
Garrett Cooper2c282152010-12-16 00:55:50 -0800182 tst_exit();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800183}
plars865695b2001-08-27 22:15:12 +0000184
185/*
186 * setup() - performs all ONE TIME setup for this test.
187 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400188void setup(void)
plars865695b2001-08-27 22:15:12 +0000189{
Garrett Cooper87bc45c2010-12-17 01:56:08 -0800190 tst_require_root(NULL);
plars865695b2001-08-27 22:15:12 +0000191
plars865695b2001-08-27 22:15:12 +0000192 tst_sig(FORK, DEF_HANDLER, cleanup);
193
plars865695b2001-08-27 22:15:12 +0000194 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
plars865695b2001-08-27 22:15:12 +0000204/*
205 * cleanup() - performs all ONE TIME cleanup for this test at
206 * completion or premature exit.
207 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400208void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000209{
plars865695b2001-08-27 22:15:12 +0000210
211 /* delete the test directory created in setup() */
212 tst_rmdir();
213
Chris Dearmanec6edca2012-10-17 19:54:01 -0700214}