robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 19 | */ |
| 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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 30 | * 2. this child creates a directory with perm 400, |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 31 | * 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_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 35 | * resets the process ID to root and calls cleanup. |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 36 | * |
| 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" |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 54 | |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 55 | #include <sys/types.h> |
Garrett Cooper | 10c925c | 2010-12-18 21:35:01 -0800 | [diff] [blame] | 56 | #include <sys/stat.h> |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 57 | #include <sys/wait.h> |
Garrett Cooper | 10c925c | 2010-12-18 21:35:01 -0800 | [diff] [blame] | 58 | #include <errno.h> |
| 59 | #include <fcntl.h> |
| 60 | #include <pwd.h> |
| 61 | #include <stdio.h> |
| 62 | #include <string.h> |
| 63 | #include <unistd.h> |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 64 | |
| 65 | char *TCID = "fchdir03"; |
| 66 | int TST_TOTAL = 1; |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 67 | |
| 68 | void setup(void); |
| 69 | void cleanup(void); |
| 70 | |
| 71 | char user1name[] = "nobody"; |
| 72 | |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 73 | char good_dir[100]; |
| 74 | int fd; |
| 75 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 76 | struct passwd *ltpuser1; |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 77 | |
| 78 | extern struct passwd *my_getpwnam(char *); |
| 79 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 80 | int main(int ac, char **av) |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 81 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 82 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 83 | const char *msg; |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 84 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 85 | pid_t pid; |
robbiew | f9cb916 | 2003-03-25 18:49:24 +0000 | [diff] [blame] | 86 | int status; |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 87 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 88 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 89 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 90 | |
| 91 | setup(); |
| 92 | |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 93 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 94 | tst_count = 0; |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 95 | |
Garrett Cooper | 10c925c | 2010-12-18 21:35:01 -0800 | [diff] [blame] | 96 | if ((pid = FORK_OR_VFORK()) == -1) |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 97 | tst_brkm(TBROK, cleanup, "fork failed"); |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 98 | |
Garrett Cooper | 10c925c | 2010-12-18 21:35:01 -0800 | [diff] [blame] | 99 | if (pid == 0) { |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 100 | /* |
| 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 Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 106 | perror("setreuid failed in child #1"); |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 107 | exit(1); |
| 108 | } |
| 109 | if (mkdir(good_dir, 00400) != 0) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 110 | perror("mkdir failed in child #1"); |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 111 | exit(1); |
| 112 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 113 | if ((fd = open(good_dir, O_RDONLY)) == -1) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 114 | perror("opening directory failed"); |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 115 | } |
| 116 | |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 117 | TEST(fchdir(fd)); |
| 118 | |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 119 | if (TEST_RETURN != -1) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 120 | printf("Call succeeded unexpectedly\n"); |
| 121 | exit(1); |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 122 | } else if (TEST_ERRNO != EACCES) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 123 | printf("Expected %d - got %d\n", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 124 | EACCES, TEST_ERRNO); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 125 | exit(1); |
Garrett Cooper | 10c925c | 2010-12-18 21:35:01 -0800 | [diff] [blame] | 126 | } else |
| 127 | printf("Got EACCES as expected\n"); |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 128 | |
Garrett Cooper | 10c925c | 2010-12-18 21:35:01 -0800 | [diff] [blame] | 129 | /* reset the UID to root */ |
| 130 | if (setuid(0) == -1) |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 131 | perror("setuid(0) failed"); |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 132 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 133 | } else { |
| 134 | if (wait(&status) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 135 | tst_brkm(TBROK | TERRNO, cleanup, |
| 136 | "wait failed"); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 137 | else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) |
| 138 | tst_brkm(TBROK, cleanup, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 139 | "child exited abnormally (wait status = " |
| 140 | "%d", status); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 141 | else { |
| 142 | /* let the child carry on */ |
| 143 | exit(0); |
| 144 | } |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Garrett Cooper | 10c925c | 2010-12-18 21:35:01 -0800 | [diff] [blame] | 147 | if (rmdir(good_dir) == -1) |
| 148 | tst_brkm(TBROK, cleanup, "rmdir failed"); |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 149 | |
| 150 | } |
| 151 | cleanup(); |
| 152 | |
Garrett Cooper | 10c925c | 2010-12-18 21:35:01 -0800 | [diff] [blame] | 153 | tst_exit(); |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 156 | void setup(void) |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 157 | { |
| 158 | char *cur_dir = NULL; |
| 159 | |
Garrett Cooper | 10c925c | 2010-12-18 21:35:01 -0800 | [diff] [blame] | 160 | tst_require_root(NULL); |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 161 | |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 162 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 163 | |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 164 | TEST_PAUSE; |
| 165 | |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 166 | tst_tmpdir(); |
| 167 | |
Garrett Cooper | 10c925c | 2010-12-18 21:35:01 -0800 | [diff] [blame] | 168 | if ((cur_dir = getcwd(cur_dir, 0)) == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 169 | tst_brkm(TBROK | TERRNO, cleanup, "getcwd failed"); |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 170 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 171 | sprintf(good_dir, "%s.%d", cur_dir, getpid()); |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 172 | |
| 173 | ltpuser1 = my_getpwnam(user1name); |
| 174 | } |
| 175 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 176 | void cleanup(void) |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 177 | { |
robbiew | c1f652a | 2002-04-25 15:29:56 +0000 | [diff] [blame] | 178 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 179 | } |