blob: eb84e39196bd1db695f70e7d1d0b6cf0dba1f55a [file] [log] [blame]
robbiewc1f652a2002-04-25 15:29:56 +00001/*
2 *
3 * Copyright (C) Bull S.A. 2001
4 * Copyright (c) International Business Machines Corp., 2001
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiewc1f652a2002-04-25 15:29:56 +000019 */
20
21/*
22 * NAME
23 * fchdir03
24 *
25 * DESCRIPTION
26 * Testcase for testing that fchdir(2) sets EACCES errno
27 *
28 * ALGORITHM
29 * 1. create a child process, sets its uid to ltpuser1
subrata_modak4bb656a2009-02-26 12:02:09 +000030 * 2. this child creates a directory with perm 400,
robbiewc1f652a2002-04-25 15:29:56 +000031 * 3. this child opens the directory and gets a file descriptor
32 * 4. this child attempts to fchdir(2) to the directory created in 2.
33 * and expects to get an EACCES.
34 * 5. finally this child checks the return code,
subrata_modak4bb656a2009-02-26 12:02:09 +000035 * resets the process ID to root and calls cleanup.
robbiewc1f652a2002-04-25 15:29:56 +000036 *
37 * USAGE: <for command-line>
38 * fchdir03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
39 * where, -c n : Run n copies concurrently.
40 * -e : Turn on errno logging.
41 * -i n : Execute test n times.
42 * -I x : Execute test for x seconds.
43 * -P x : Pause for x seconds between iterations.
44 * -t : Turn on syscall timing.
45 *
46 * HISTORY
47 * 04/2002 Ported by Jacky Malcles
48 *
49 * RESTRICTIONS
50 * This test must be run as root.
51 */
52
53#include "test.h"
robbiewc1f652a2002-04-25 15:29:56 +000054
robbiewc1f652a2002-04-25 15:29:56 +000055#include <sys/types.h>
Garrett Cooper10c925c2010-12-18 21:35:01 -080056#include <sys/stat.h>
robbiewc1f652a2002-04-25 15:29:56 +000057#include <sys/wait.h>
Garrett Cooper10c925c2010-12-18 21:35:01 -080058#include <errno.h>
59#include <fcntl.h>
60#include <pwd.h>
61#include <stdio.h>
62#include <string.h>
63#include <unistd.h>
robbiewc1f652a2002-04-25 15:29:56 +000064
65char *TCID = "fchdir03";
66int TST_TOTAL = 1;
robbiewc1f652a2002-04-25 15:29:56 +000067
68void setup(void);
69void cleanup(void);
70
71char user1name[] = "nobody";
72
robbiewc1f652a2002-04-25 15:29:56 +000073char good_dir[100];
74int fd;
75
subrata_modak4bb656a2009-02-26 12:02:09 +000076struct passwd *ltpuser1;
robbiewc1f652a2002-04-25 15:29:56 +000077
78extern struct passwd *my_getpwnam(char *);
79
plars74948ad2002-11-14 16:16:14 +000080int main(int ac, char **av)
robbiewc1f652a2002-04-25 15:29:56 +000081{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020082 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020083 const char *msg;
robbiewc1f652a2002-04-25 15:29:56 +000084
subrata_modak4bb656a2009-02-26 12:02:09 +000085 pid_t pid;
robbiewf9cb9162003-03-25 18:49:24 +000086 int status;
robbiewc1f652a2002-04-25 15:29:56 +000087
Garrett Cooper53740502010-12-16 00:04:01 -080088 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080089 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiewc1f652a2002-04-25 15:29:56 +000090
91 setup();
92
robbiewc1f652a2002-04-25 15:29:56 +000093 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080094 tst_count = 0;
robbiewc1f652a2002-04-25 15:29:56 +000095
Garrett Cooper10c925c2010-12-18 21:35:01 -080096 if ((pid = FORK_OR_VFORK()) == -1)
robbiewc1f652a2002-04-25 15:29:56 +000097 tst_brkm(TBROK, cleanup, "fork failed");
robbiewc1f652a2002-04-25 15:29:56 +000098
Garrett Cooper10c925c2010-12-18 21:35:01 -080099 if (pid == 0) {
robbiewc1f652a2002-04-25 15:29:56 +0000100 /*
101 * set the child's ID to ltpuser1 using seteuid()
102 * so that the ID can be changed back after the
103 * TEST call is made.
104 */
105 if (seteuid(ltpuser1->pw_uid) != 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800106 perror("setreuid failed in child #1");
robbiewc1f652a2002-04-25 15:29:56 +0000107 exit(1);
108 }
109 if (mkdir(good_dir, 00400) != 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800110 perror("mkdir failed in child #1");
robbiewc1f652a2002-04-25 15:29:56 +0000111 exit(1);
112 }
subrata_modak56207ce2009-03-23 13:35:39 +0000113 if ((fd = open(good_dir, O_RDONLY)) == -1) {
Garrett Cooper53740502010-12-16 00:04:01 -0800114 perror("opening directory failed");
robbiewc1f652a2002-04-25 15:29:56 +0000115 }
116
robbiewc1f652a2002-04-25 15:29:56 +0000117 TEST(fchdir(fd));
118
robbiewc1f652a2002-04-25 15:29:56 +0000119 if (TEST_RETURN != -1) {
Garrett Cooper53740502010-12-16 00:04:01 -0800120 printf("Call succeeded unexpectedly\n");
121 exit(1);
robbiewc1f652a2002-04-25 15:29:56 +0000122 } else if (TEST_ERRNO != EACCES) {
Garrett Cooper53740502010-12-16 00:04:01 -0800123 printf("Expected %d - got %d\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800124 EACCES, TEST_ERRNO);
Garrett Cooper53740502010-12-16 00:04:01 -0800125 exit(1);
Garrett Cooper10c925c2010-12-18 21:35:01 -0800126 } else
127 printf("Got EACCES as expected\n");
robbiewc1f652a2002-04-25 15:29:56 +0000128
Garrett Cooper10c925c2010-12-18 21:35:01 -0800129 /* reset the UID to root */
130 if (setuid(0) == -1)
Garrett Cooper53740502010-12-16 00:04:01 -0800131 perror("setuid(0) failed");
robbiewc1f652a2002-04-25 15:29:56 +0000132
Garrett Cooper53740502010-12-16 00:04:01 -0800133 } else {
134 if (wait(&status) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800135 tst_brkm(TBROK | TERRNO, cleanup,
136 "wait failed");
Garrett Cooper53740502010-12-16 00:04:01 -0800137 else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
138 tst_brkm(TBROK, cleanup,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800139 "child exited abnormally (wait status = "
140 "%d", status);
Garrett Cooper53740502010-12-16 00:04:01 -0800141 else {
142 /* let the child carry on */
143 exit(0);
144 }
robbiewc1f652a2002-04-25 15:29:56 +0000145 }
146
Garrett Cooper10c925c2010-12-18 21:35:01 -0800147 if (rmdir(good_dir) == -1)
148 tst_brkm(TBROK, cleanup, "rmdir failed");
robbiewc1f652a2002-04-25 15:29:56 +0000149
150 }
151 cleanup();
152
Garrett Cooper10c925c2010-12-18 21:35:01 -0800153 tst_exit();
robbiewc1f652a2002-04-25 15:29:56 +0000154}
155
Mike Frysingerc57fba52014-04-09 18:56:30 -0400156void setup(void)
robbiewc1f652a2002-04-25 15:29:56 +0000157{
158 char *cur_dir = NULL;
159
Garrett Cooper10c925c2010-12-18 21:35:01 -0800160 tst_require_root(NULL);
robbiewc1f652a2002-04-25 15:29:56 +0000161
robbiewc1f652a2002-04-25 15:29:56 +0000162 tst_sig(FORK, DEF_HANDLER, cleanup);
163
robbiewc1f652a2002-04-25 15:29:56 +0000164 TEST_PAUSE;
165
robbiewc1f652a2002-04-25 15:29:56 +0000166 tst_tmpdir();
167
Garrett Cooper10c925c2010-12-18 21:35:01 -0800168 if ((cur_dir = getcwd(cur_dir, 0)) == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800169 tst_brkm(TBROK | TERRNO, cleanup, "getcwd failed");
robbiewc1f652a2002-04-25 15:29:56 +0000170
subrata_modak56207ce2009-03-23 13:35:39 +0000171 sprintf(good_dir, "%s.%d", cur_dir, getpid());
robbiewc1f652a2002-04-25 15:29:56 +0000172
173 ltpuser1 = my_getpwnam(user1name);
174}
175
Mike Frysingerc57fba52014-04-09 18:56:30 -0400176void cleanup(void)
robbiewc1f652a2002-04-25 15:29:56 +0000177{
robbiewc1f652a2002-04-25 15:29:56 +0000178 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700179}