plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 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 |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 22 | * statfs02.c |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Testcase to check that statfs(2) sets errno correctly. |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * 1. Use a component of the pathname, which is not a directory |
| 29 | * in the "path" parameter to statfs(). Expect ENOTDIR |
| 30 | * 2. Pass a filename which doesn't exist, and expect ENOENT. |
| 31 | * 3. Pass a pathname which is more than MAXNAMLEN, and expect |
| 32 | * ENAMETOOLONG. |
| 33 | * 4. Pass a pointer to the pathname outside the address space of |
| 34 | * the process, and expect EFAULT. |
| 35 | * 5. Pass a pointer to the buf paramter outside the address space |
| 36 | * of the process, and expect EFAULT. |
| 37 | * |
| 38 | * USAGE: <for command-line> |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 39 | * statfs02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 40 | * where, -c n : Run n copies concurrently. |
| 41 | * -e : Turn on errno logging. |
| 42 | * -i n : Execute test n times. |
| 43 | * -I x : Execute test for x seconds. |
| 44 | * -P x : Pause for x seconds between iterations. |
| 45 | * -t : Turn on syscall timing. |
| 46 | * |
| 47 | * HISTORY |
| 48 | * 07/2001 Ported by Wayne Boyer |
| 49 | * |
| 50 | * RESTRICTIONS |
| 51 | * NONE |
| 52 | * |
| 53 | */ |
| 54 | #include <sys/types.h> |
| 55 | #include <sys/statfs.h> |
| 56 | #include <sys/stat.h> |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame^] | 57 | #include <fcntl.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | #include <sys/vfs.h> |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 59 | #include <sys/mman.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | #include <errno.h> |
| 61 | #include "test.h" |
| 62 | #include "usctest.h" |
| 63 | |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 64 | char *TCID = "statfs02"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | int TST_TOTAL = 5; |
| 66 | extern int Tst_count; |
| 67 | |
| 68 | int exp_enos[]={ENOTDIR, ENOENT, ENAMETOOLONG, EFAULT, 0}; |
| 69 | |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 70 | char * bad_addr = 0; |
| 71 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | char bad_file[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyz"; |
| 73 | char good_dir[100] = "testdir"; |
| 74 | char fname[30] = "testfile"; |
| 75 | struct statfs fsbuf; |
| 76 | char fname1[30]; |
| 77 | |
| 78 | struct test_case_t { |
| 79 | char *path; |
| 80 | struct statfs *buf; |
| 81 | int error; |
| 82 | } TC[] = { |
| 83 | /* path has a component which is not a directory - ENOTDIR */ |
| 84 | {fname1, &fsbuf, ENOTDIR}, |
| 85 | |
| 86 | /* path does not exist - ENOENT */ |
| 87 | {good_dir, &fsbuf, ENOENT}, |
| 88 | |
| 89 | /* path is too long - ENAMETOOLONG */ |
| 90 | {bad_file, &fsbuf, ENAMETOOLONG}, |
| 91 | |
| 92 | /* path is an invalid address - EFAULT */ |
| 93 | {(char *)-1, &fsbuf, EFAULT}, |
| 94 | |
| 95 | /* buf is an invalid address - EFAULT */ |
| 96 | {fname, (struct statfs *)-1, EFAULT} |
| 97 | }; |
| 98 | |
| 99 | void setup(void); |
| 100 | void cleanup(void); |
| 101 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame^] | 102 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | { |
| 104 | int lc; /* loop counter */ |
| 105 | char *msg; /* message returned from parse_opts */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | int i; |
| 107 | |
| 108 | /* parse standard options */ |
| 109 | if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){ |
| 110 | tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg); |
| 111 | /*NOTREACHED*/ |
| 112 | } |
| 113 | |
| 114 | setup(); |
| 115 | |
| 116 | /* set up the expected errnos */ |
| 117 | TEST_EXP_ENOS(exp_enos); |
| 118 | |
| 119 | /* check looping state if -i option given */ |
| 120 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 121 | |
| 122 | /* reset Tst_count in case we are looping. */ |
| 123 | Tst_count = 0; |
| 124 | |
| 125 | |
| 126 | /* loop through the test cases */ |
| 127 | for (i = 0; i < TST_TOTAL; i++) { |
| 128 | |
| 129 | TEST(statfs(TC[i].path, TC[i].buf)); |
| 130 | |
| 131 | if (TEST_RETURN != -1) { |
| 132 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 133 | continue; |
| 134 | } |
| 135 | |
| 136 | TEST_ERROR_LOG(TEST_ERRNO); |
| 137 | |
| 138 | if (TEST_ERRNO == TC[i].error) { |
| 139 | tst_resm(TPASS, "expected failure - " |
| 140 | "errno = %d : %s", TEST_ERRNO, |
| 141 | strerror(TEST_ERRNO)); |
| 142 | } else { |
| 143 | tst_resm(TFAIL, "unexpected error - %d : %s - " |
| 144 | "expected %d", TEST_ERRNO, |
| 145 | strerror(TEST_ERRNO), TC[i].error); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | cleanup(); |
| 150 | |
| 151 | /*NOTREACHED*/ |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * setup() - performs all ONE TIME setup for this test. |
| 156 | */ |
| 157 | void |
| 158 | setup() |
| 159 | { |
| 160 | /* capture signals */ |
| 161 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 162 | |
| 163 | /* Pause if that option was specified */ |
| 164 | TEST_PAUSE; |
| 165 | |
| 166 | /* make a temporary directory and cd to it */ |
| 167 | tst_tmpdir(); |
| 168 | |
| 169 | sprintf(fname, "%s.%d", fname, getpid()); |
| 170 | if ((creat(fname, 0444)) == -1) { |
| 171 | tst_resm(TFAIL, "creat(2) FAILED to creat temp file"); |
| 172 | } |
| 173 | sprintf(fname1, "%s/%s", fname, fname); |
| 174 | |
| 175 | sprintf(good_dir, "%s.statfs.%d", good_dir, getpid()); |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 176 | |
| 177 | bad_addr = mmap(0, 1, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
| 178 | if (bad_addr <= 0) { |
| 179 | tst_brkm(TBROK, cleanup, "mmap failed"); |
| 180 | } |
| 181 | TC[3].path = bad_addr; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | |
| 185 | /* |
| 186 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 187 | * completion or premature exit. |
| 188 | */ |
| 189 | void |
| 190 | cleanup() |
| 191 | { |
| 192 | /* |
| 193 | * print timing stats if that option was specified. |
| 194 | * print errno log if that option was specified. |
| 195 | */ |
| 196 | TEST_CLEANUP; |
| 197 | |
| 198 | /* delete the test directory created in setup() */ |
| 199 | tst_rmdir(); |
| 200 | |
| 201 | /* exit with return code appropriate for results */ |
| 202 | tst_exit(); |
| 203 | } |
| 204 | |