robbiew | 2d1b089 | 2002-06-05 15:20:29 +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 | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * NAME |
| 23 | * statfs03.c |
| 24 | * |
| 25 | * DESCRIPTION |
| 26 | * Testcase to check that statfs(2) sets errno to EACCES when |
| 27 | * search permission is denied for a component of the path prefix of path. |
| 28 | * |
| 29 | * ALGORITHM |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 30 | * Use a component of the pathname, where search permission |
| 31 | * is denied for a component of the path prefix of path. |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 32 | * |
| 33 | * USAGE: <for command-line> |
| 34 | * statfs03 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 35 | * where, -c n : Run n copies concurrently. |
| 36 | * -e : Turn on errno logging. |
| 37 | * -i n : Execute test n times. |
| 38 | * -I x : Execute test for x seconds. |
| 39 | * -P x : Pause for x seconds between iterations. |
| 40 | * -t : Turn on syscall timing. |
| 41 | * |
| 42 | * HISTORY |
| 43 | * 05/2002 Ported by Jacky Malcles |
| 44 | * |
| 45 | * RESTRICTIONS |
| 46 | * NONE |
| 47 | * |
| 48 | */ |
| 49 | #include <sys/types.h> |
| 50 | #include <sys/statfs.h> |
| 51 | #include <sys/stat.h> |
| 52 | #include <sys/vfs.h> |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 53 | #include <fcntl.h> |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 54 | #include <errno.h> |
| 55 | #include <stdio.h> |
| 56 | #include "test.h" |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 57 | #include <pwd.h> |
| 58 | |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 59 | char *TCID = "statfs03"; |
| 60 | int TST_TOTAL = 1; |
mridge | 3db3f03 | 2004-08-25 16:26:11 +0000 | [diff] [blame] | 61 | int fileHandle = 0; |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 62 | |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 63 | char nobody_uid[] = "nobody"; |
| 64 | struct passwd *ltpuser; |
| 65 | |
| 66 | char fname[30] = "testfile"; |
| 67 | char path[50]; |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 68 | struct statfs buf; |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 69 | |
| 70 | void setup(void); |
| 71 | void cleanup(void); |
| 72 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 73 | int main(int ac, char **av) |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 74 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 75 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 76 | const char *msg; |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 77 | |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 78 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 79 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 80 | |
| 81 | setup(); |
| 82 | |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 83 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 84 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 85 | tst_count = 0; |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 86 | |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 87 | TEST(statfs(path, &buf)); |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 88 | |
| 89 | if (TEST_RETURN != -1) { |
| 90 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 91 | |
| 92 | } else { |
| 93 | |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 94 | if (TEST_ERRNO == EACCES) { |
| 95 | tst_resm(TPASS, "expected failure - " |
| 96 | "errno = %d : %s", TEST_ERRNO, |
| 97 | strerror(TEST_ERRNO)); |
| 98 | } else { |
| 99 | tst_resm(TFAIL, "unexpected error - %d : %s - " |
| 100 | "expected %d", TEST_ERRNO, |
Cyril Hrubis | 605fa33 | 2015-02-04 13:11:20 +0100 | [diff] [blame] | 101 | strerror(TEST_ERRNO), EACCES); |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | } |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 105 | |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 106 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 107 | tst_exit(); |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 108 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 109 | |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 110 | /* |
| 111 | * setup() - performs all ONE TIME setup for this test. |
| 112 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 113 | void setup(void) |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 114 | { |
| 115 | |
Garrett Cooper | 2384bd4 | 2010-11-22 14:53:11 -0800 | [diff] [blame] | 116 | tst_require_root(NULL); |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 117 | |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 118 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 119 | |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 120 | TEST_PAUSE; |
| 121 | |
| 122 | /* make a temporary directory and cd to it */ |
| 123 | tst_tmpdir(); |
Cyril Hrubis | 9c31ad2 | 2014-05-14 17:15:39 +0200 | [diff] [blame] | 124 | if (chmod(tst_get_tmpdir(), S_IRWXU) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 125 | tst_brkm(TBROK | TERRNO, cleanup, "chmod(%s, 700) failed", |
Cyril Hrubis | 9c31ad2 | 2014-05-14 17:15:39 +0200 | [diff] [blame] | 126 | tst_get_tmpdir()); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 127 | |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 128 | /* create a test file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 129 | sprintf(fname, "%s.%d", fname, getpid()); |
mridge | 800b8bf | 2004-09-27 22:10:27 +0000 | [diff] [blame] | 130 | if (mkdir(fname, 0444) == -1) { |
| 131 | tst_resm(TFAIL, "creat(2) FAILED to creat temp file"); |
| 132 | } else { |
| 133 | sprintf(path, "%s/%s", fname, fname); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 134 | if ((fileHandle = creat(path, 0444)) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 135 | tst_brkm(TFAIL | TERRNO, cleanup, "creat failed"); |
mridge | 800b8bf | 2004-09-27 22:10:27 +0000 | [diff] [blame] | 136 | } |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 137 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 138 | ltpuser = getpwnam(nobody_uid); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 139 | if (ltpuser == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 140 | tst_brkm(TBROK | TERRNO, cleanup, "getpwnam failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 141 | if (seteuid(ltpuser->pw_uid) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 142 | tst_resm(TINFO | TERRNO, "seteuid failed to " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 143 | "to set the effective uid to %d", ltpuser->pw_uid); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 144 | } |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 145 | |
| 146 | } |
| 147 | |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 148 | /* |
| 149 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 150 | * completion or premature exit. |
| 151 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 152 | void cleanup(void) |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 153 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 154 | /* reset the process ID to the saved ID (root) */ |
| 155 | if (setuid(0) == -1) { |
| 156 | tst_resm(TINFO, "setuid(0) failed"); |
| 157 | } |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 158 | |
| 159 | /* |
| 160 | * print timing stats if that option was specified. |
| 161 | * print errno log if that option was specified. |
| 162 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 163 | close(fileHandle); |
mridge | 3db3f03 | 2004-08-25 16:26:11 +0000 | [diff] [blame] | 164 | |
robbiew | 2d1b089 | 2002-06-05 15:20:29 +0000 | [diff] [blame] | 165 | /* delete the test directory created in setup() */ |
| 166 | tst_rmdir(); |
| 167 | |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 168 | } |