statfs03.c revision d59a659cd639ca2780b00049d102acd2a783d585
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 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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 30 * Use a component of the pathname, where search permission 31 * is denied for a component of the path prefix of path. 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> 53#include <fcntl.h> 54#include <errno.h> 55#include <stdio.h> 56#include "test.h" 57#include "usctest.h" 58#include <pwd.h> 59 60char *TCID = "statfs03"; 61int TST_TOTAL = 1; 62int fileHandle = 0; 63 64int exp_enos[] = { EACCES, 0 }; 65 66char nobody_uid[] = "nobody"; 67struct passwd *ltpuser; 68 69char fname[30] = "testfile"; 70char path[50]; 71struct statfs buf; 72 73void setup(void); 74void cleanup(void); 75 76int main(int ac, char **av) 77{ 78 int lc; 79 char *msg; 80 81 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) 82 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); 83 84 setup(); 85 86 /* set up the expected errnos */ 87 TEST_EXP_ENOS(exp_enos); 88 89 for (lc = 0; TEST_LOOPING(lc); lc++) { 90 91 tst_count = 0; 92 93 TEST(statfs(path, &buf)); 94 95 if (TEST_RETURN != -1) { 96 tst_resm(TFAIL, "call succeeded unexpectedly"); 97 98 } else { 99 100 TEST_ERROR_LOG(TEST_ERRNO); 101 102 if (TEST_ERRNO == EACCES) { 103 tst_resm(TPASS, "expected failure - " 104 "errno = %d : %s", TEST_ERRNO, 105 strerror(TEST_ERRNO)); 106 } else { 107 tst_resm(TFAIL, "unexpected error - %d : %s - " 108 "expected %d", TEST_ERRNO, 109 strerror(TEST_ERRNO), exp_enos[0]); 110 } 111 } 112 } 113 114 cleanup(); 115 tst_exit(); 116} 117 118/* 119 * setup() - performs all ONE TIME setup for this test. 120 */ 121void setup() 122{ 123 124 tst_require_root(NULL); 125 126 tst_sig(NOFORK, DEF_HANDLER, cleanup); 127 128 TEST_PAUSE; 129 130 /* make a temporary directory and cd to it */ 131 tst_tmpdir(); 132 if (chmod(get_tst_tmpdir(), S_IRWXU) == -1) 133 tst_brkm(TBROK | TERRNO, cleanup, "chmod(%s, 700) failed", 134 get_tst_tmpdir()); 135 136 /* create a test file */ 137 sprintf(fname, "%s.%d", fname, getpid()); 138 if (mkdir(fname, 0444) == -1) { 139 tst_resm(TFAIL, "creat(2) FAILED to creat temp file"); 140 } else { 141 sprintf(path, "%s/%s", fname, fname); 142 if ((fileHandle = creat(path, 0444)) == -1) 143 tst_brkm(TFAIL | TERRNO, cleanup, "creat failed"); 144 } 145 146 ltpuser = getpwnam(nobody_uid); 147 if (ltpuser == NULL) 148 tst_brkm(TBROK | TERRNO, cleanup, "getpwnam failed"); 149 if (seteuid(ltpuser->pw_uid) == -1) { 150 tst_resm(TINFO | TERRNO, "seteuid failed to " 151 "to set the effective uid to %d", ltpuser->pw_uid); 152 } 153 154} 155 156/* 157 * cleanup() - performs all ONE TIME cleanup for this test at 158 * completion or premature exit. 159 */ 160void cleanup() 161{ 162 /* reset the process ID to the saved ID (root) */ 163 if (setuid(0) == -1) { 164 tst_resm(TINFO, "setuid(0) failed"); 165 } 166 167 /* 168 * print timing stats if that option was specified. 169 * print errno log if that option was specified. 170 */ 171 close(fileHandle); 172 173 TEST_CLEANUP; 174 175 /* delete the test directory created in setup() */ 176 tst_rmdir(); 177 178} 179