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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
nstraz | 64d7fe6 | 2001-09-26 21:34:54 +0000 | [diff] [blame] | 22 | * chdir04.c |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Testcase to test whether chdir(2) sets errno correctly. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 26 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 27 | * ALGORITHM |
| 28 | * 1. Test for ENAMETOOLONG: |
| 29 | * Create a bad directory name with length more than |
| 30 | * |
| 31 | * VFS_MAXNAMELEN (Linux kernel variable), and attempt to |
| 32 | * chdir(2) to it. |
| 33 | * |
| 34 | * 2. Test for ENOENT: |
| 35 | * Attempt to chdir(2) on a non-existent directory |
| 36 | * |
| 37 | * 3. Test for EFAULT: |
| 38 | * Pass an address which lies outside the address space of the |
| 39 | * process, and expect an EFAULT. |
| 40 | * |
| 41 | * USAGE: <for command-line> |
nstraz | 64d7fe6 | 2001-09-26 21:34:54 +0000 | [diff] [blame] | 42 | * chdir04 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | * where, -c n : Run n copies concurrently. |
| 44 | * -e : Turn on errno logging. |
| 45 | * -i n : Execute test n times. |
| 46 | * -I x : Execute test for x seconds. |
| 47 | * -P x : Pause for x seconds between iterations. |
| 48 | * -t : Turn on syscall timing. |
| 49 | * |
| 50 | * HISTORY |
| 51 | * 07/2001 Ported by Wayne Boyer |
| 52 | * |
| 53 | * RESTRICTIONS |
| 54 | * NONE |
| 55 | */ |
| 56 | |
| 57 | #include <stdio.h> |
| 58 | #include <errno.h> |
| 59 | #include <sys/stat.h> |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 60 | #include <sys/mman.h> |
Garrett Cooper | 71ff32c | 2010-12-17 11:30:53 -0800 | [diff] [blame] | 61 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | |
nstraz | 64d7fe6 | 2001-09-26 21:34:54 +0000 | [diff] [blame] | 63 | char *TCID = "chdir04"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 65 | char bad_dir[] = |
| 66 | "abcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyzabcdefghijklmnopqrstmnopqrstuvwxyz"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | |
| 68 | char noexist_dir[] = "/tmp/noexistdir"; |
| 69 | |
| 70 | struct test_case_t { |
| 71 | char *dname; |
| 72 | int error; |
| 73 | } TC[] = { |
| 74 | /* |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 75 | * to test whether chdir() is setting ENAMETOOLONG if the |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | * directory is more than VFS_MAXNAMELEN |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 78 | { |
| 79 | bad_dir, ENAMETOOLONG}, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 80 | /* |
| 81 | * to test whether chdir() is setting ENOENT if the |
| 82 | * directory is not existing. |
| 83 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 84 | { |
| 85 | noexist_dir, ENOENT}, |
Garrett Cooper | 71ff32c | 2010-12-17 11:30:53 -0800 | [diff] [blame] | 86 | #ifndef UCLINUX |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 87 | /* |
| 88 | * to test whether chdir() is setting EFAULT if the |
| 89 | * directory is an invalid address. |
| 90 | */ |
| 91 | { |
| 92 | (void *)-1, EFAULT} |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 93 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
Cyril Hrubis | b863a0b | 2014-09-24 13:15:29 +0200 | [diff] [blame] | 96 | int TST_TOTAL = ARRAY_SIZE(TC); |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 97 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | int flag; |
| 99 | #define FAILED 1 |
| 100 | |
| 101 | void setup(void); |
| 102 | void cleanup(void); |
| 103 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 104 | char *bad_addr = 0; |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 105 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 106 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | { |
Garrett Cooper | 71ff32c | 2010-12-17 11:30:53 -0800 | [diff] [blame] | 108 | int lc; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | int i; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 110 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | |
Garrett Cooper | 71ff32c | 2010-12-17 11:30:53 -0800 | [diff] [blame] | 112 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 113 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | |
| 115 | setup(); |
| 116 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 118 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 120 | for (i = 0; i < TST_TOTAL; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | |
| 122 | TEST(chdir(TC[i].dname)); |
| 123 | |
| 124 | if (TEST_RETURN != -1) { |
| 125 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 126 | continue; |
| 127 | } |
| 128 | |
Garrett Cooper | 71ff32c | 2010-12-17 11:30:53 -0800 | [diff] [blame] | 129 | if (TEST_ERRNO == TC[i].error) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 130 | tst_resm(TPASS | TTERRNO, "failed as expected"); |
Garrett Cooper | 71ff32c | 2010-12-17 11:30:53 -0800 | [diff] [blame] | 131 | else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 132 | tst_resm(TFAIL | TTERRNO, |
| 133 | "didn't fail as expected (expected %d)", |
| 134 | TC[i].error); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | } |
| 138 | cleanup(); |
| 139 | |
Garrett Cooper | 71ff32c | 2010-12-17 11:30:53 -0800 | [diff] [blame] | 140 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | |
Garrett Cooper | 71ff32c | 2010-12-17 11:30:53 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 144 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 146 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 148 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | TEST_PAUSE; |
| 150 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | tst_tmpdir(); |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 152 | |
Garrett Cooper | 71ff32c | 2010-12-17 11:30:53 -0800 | [diff] [blame] | 153 | #ifdef UCLINUX |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 154 | bad_addr = mmap(0, 1, PROT_NONE, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 155 | MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); |
Garrett Cooper | 71ff32c | 2010-12-17 11:30:53 -0800 | [diff] [blame] | 156 | if (bad_addr == MAP_FAILED) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 157 | tst_brkm(TBROK | TERRNO, cleanup, "mmap() failed"); |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 158 | TC[2].dname = bad_addr; |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 159 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 162 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | tst_rmdir(); |
| 165 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 166 | } |