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 |
| 22 | * chdir01.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Check proper operation of chdir(): tests whether the |
| 26 | * system call can it change the current, working directory, and find a |
| 27 | * file there? Will it fail on a non-directory entry ? |
| 28 | * |
| 29 | * ALGORITHM |
| 30 | * Make a directory "Testdirectory", and create a file in it. cd into |
| 31 | * the directory and read the entry. It should be the file just |
| 32 | * created. |
| 33 | * |
| 34 | * USAGE: <for command-line> |
| 35 | * chdir01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 36 | * where, -c n : Run n copies concurrently. |
| 37 | * -e : Turn on errno logging. |
| 38 | * -i n : Execute test n times. |
| 39 | * -I x : Execute test for x seconds. |
| 40 | * -P x : Pause for x seconds between iterations. |
| 41 | * -t : Turn on syscall timing. |
| 42 | * |
| 43 | * HISTORY |
| 44 | * 07/2001 Ported by Wayne Boyer |
| 45 | * |
| 46 | * RESTRICTIONS |
| 47 | * None |
| 48 | */ |
| 49 | |
| 50 | #include <stdio.h> |
| 51 | #include <sys/types.h> |
| 52 | #include <dirent.h> |
| 53 | #include <errno.h> |
robbiew | 15226cd | 2002-04-10 16:10:40 +0000 | [diff] [blame] | 54 | #include <string.h> |
robbiew | 2c94524 | 2002-11-11 19:01:25 +0000 | [diff] [blame] | 55 | #include <fcntl.h> |
| 56 | #include<sys/stat.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | #include "test.h" |
Garrett Cooper | fc62c2d | 2010-12-21 07:07:56 -0800 | [diff] [blame] | 58 | #include "safe_macros.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 60 | char *TCID = "chdir01"; |
Zeng Linggang | 4f30545 | 2013-10-28 09:32:06 +0800 | [diff] [blame] | 61 | int TST_TOTAL = 2; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | void setup(void); |
| 64 | void cleanup(void); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 65 | static void checknames(char **, int, DIR *); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | |
| 67 | char testdir[40] = ""; |
| 68 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 69 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | { |
| 71 | DIR *ddir, *opendir(); |
Garrett Cooper | fc62c2d | 2010-12-21 07:07:56 -0800 | [diff] [blame] | 72 | int fd; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | char *filname = "chdirtest"; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 74 | char *filenames[3]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 76 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 77 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
Garrett Cooper | 41bd8fd | 2010-12-17 11:15:00 -0800 | [diff] [blame] | 79 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 80 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | |
Garrett Cooper | 41bd8fd | 2010-12-17 11:15:00 -0800 | [diff] [blame] | 82 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 85 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 86 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | |
Garrett Cooper | fc62c2d | 2010-12-21 07:07:56 -0800 | [diff] [blame] | 88 | SAFE_CHDIR(cleanup, testdir); |
| 89 | |
| 90 | fd = SAFE_CREAT(cleanup, filname, 0000); |
| 91 | SAFE_CLOSE(cleanup, fd); |
| 92 | if ((ddir = opendir(".")) == NULL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 93 | tst_brkm(TBROK | TERRNO, cleanup, "opendir(.) failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 95 | filenames[0] = "."; |
| 96 | filenames[1] = ".."; |
| 97 | filenames[2] = filname; |
Garrett Cooper | 41bd8fd | 2010-12-17 11:15:00 -0800 | [diff] [blame] | 98 | checknames(filenames, sizeof(filenames) / sizeof(filenames[0]), |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 99 | ddir); |
Garrett Cooper | 41bd8fd | 2010-12-17 11:15:00 -0800 | [diff] [blame] | 100 | closedir(ddir); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | |
| 102 | TEST(chdir(filname)); |
| 103 | |
Garrett Cooper | 41bd8fd | 2010-12-17 11:15:00 -0800 | [diff] [blame] | 104 | if (TEST_RETURN != -1) |
| 105 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 106 | else if (TEST_ERRNO != ENOTDIR) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 107 | tst_resm(TFAIL | TTERRNO, |
| 108 | "failed unexpectedly; wanted ENOTDIR"); |
Garrett Cooper | 41bd8fd | 2010-12-17 11:15:00 -0800 | [diff] [blame] | 109 | else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 110 | tst_resm(TPASS, "failed as expected with ENOTDIR"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | |
Garrett Cooper | 41bd8fd | 2010-12-17 11:15:00 -0800 | [diff] [blame] | 112 | if (unlink(filname) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 113 | tst_brkm(TBROK | TERRNO, cleanup, |
| 114 | "Couldn't remove file"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | |
Garrett Cooper | fc62c2d | 2010-12-21 07:07:56 -0800 | [diff] [blame] | 116 | SAFE_CHDIR(cleanup, ".."); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | |
Zeng Linggang | 4f30545 | 2013-10-28 09:32:06 +0800 | [diff] [blame] | 118 | /* ELOOP */ |
| 119 | SAFE_SYMLINK(cleanup, "test_eloop1", "test_eloop2"); |
| 120 | SAFE_SYMLINK(cleanup, "test_eloop2", "test_eloop1"); |
| 121 | |
| 122 | TEST(chdir("test_eloop1")); |
| 123 | |
| 124 | if (TEST_RETURN != -1) { |
| 125 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 126 | } else if (TEST_ERRNO != ELOOP) { |
| 127 | tst_resm(TFAIL | TTERRNO, |
| 128 | "failed unexpectedly; wanted ELOOP"); |
| 129 | } else { |
| 130 | tst_resm(TPASS, "failed as expected with ELOOP"); |
| 131 | } |
| 132 | |
| 133 | SAFE_UNLINK(cleanup, "test_eloop1"); |
| 134 | SAFE_UNLINK(cleanup, "test_eloop2"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | } |
| 136 | cleanup(); |
| 137 | |
Garrett Cooper | 41bd8fd | 2010-12-17 11:15:00 -0800 | [diff] [blame] | 138 | tst_exit(); |
| 139 | |
| 140 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 142 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 144 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 146 | |
| 147 | umask(0); |
| 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(); |
| 152 | |
| 153 | sprintf(testdir, "Testdir.%d", getpid()); |
| 154 | |
Garrett Cooper | fc62c2d | 2010-12-21 07:07:56 -0800 | [diff] [blame] | 155 | SAFE_MKDIR(cleanup, testdir, 0700); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | } |
| 157 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 158 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | tst_rmdir(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 163 | void checknames(char **pfilnames, int fnamecount, DIR * ddir) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | { |
| 165 | struct dirent *dir; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 166 | int i, found; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 167 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 168 | found = 0; |
Garrett Cooper | fc62c2d | 2010-12-21 07:07:56 -0800 | [diff] [blame] | 169 | while ((dir = readdir(ddir)) != NULL) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 170 | for (i = 0; i < fnamecount; i++) { |
Chris Dearman | 37550cf | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 171 | /* |
Garrett Cooper | fc62c2d | 2010-12-21 07:07:56 -0800 | [diff] [blame] | 172 | * if dir->d_name is not null terminated it is a bug |
| 173 | * anyway |
| 174 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 175 | if (strcmp(pfilnames[i], dir->d_name) == 0) { |
| 176 | tst_resm(TINFO, "Found file %s", dir->d_name); |
| 177 | found++; |
| 178 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 179 | } |
| 180 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 181 | if (found < fnamecount) { |
| 182 | tst_brkm(TFAIL, cleanup, |
| 183 | "Some files do not exist, but they must exist"); |
| 184 | } |
Garrett Cooper | fc62c2d | 2010-12-21 07:07:56 -0800 | [diff] [blame] | 185 | } |