blob: f1a3f9da7f9c0053b00c971d4dade7076393b306 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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>
robbiew15226cd2002-04-10 16:10:40 +000054#include <string.h>
robbiew2c945242002-11-11 19:01:25 +000055#include <fcntl.h>
56#include<sys/stat.h>
plars865695b2001-08-27 22:15:12 +000057#include "test.h"
Garrett Cooperfc62c2d2010-12-21 07:07:56 -080058#include "safe_macros.h"
plars865695b2001-08-27 22:15:12 +000059
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020060char *TCID = "chdir01";
Zeng Linggang4f305452013-10-28 09:32:06 +080061int TST_TOTAL = 2;
plars865695b2001-08-27 22:15:12 +000062
plars865695b2001-08-27 22:15:12 +000063void setup(void);
64void cleanup(void);
subrata_modak56207ce2009-03-23 13:35:39 +000065static void checknames(char **, int, DIR *);
plars865695b2001-08-27 22:15:12 +000066
67char testdir[40] = "";
68
subrata_modak56207ce2009-03-23 13:35:39 +000069int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000070{
71 DIR *ddir, *opendir();
Garrett Cooperfc62c2d2010-12-21 07:07:56 -080072 int fd;
plars865695b2001-08-27 22:15:12 +000073 char *filname = "chdirtest";
subrata_modak56207ce2009-03-23 13:35:39 +000074 char *filenames[3];
plars865695b2001-08-27 22:15:12 +000075
Cyril Hrubis89af32a2012-10-24 16:39:11 +020076 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020077 const char *msg;
plars865695b2001-08-27 22:15:12 +000078
Garrett Cooper41bd8fd2010-12-17 11:15:00 -080079 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080080 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000081
Garrett Cooper41bd8fd2010-12-17 11:15:00 -080082 setup();
plars865695b2001-08-27 22:15:12 +000083
plars865695b2001-08-27 22:15:12 +000084 for (lc = 0; TEST_LOOPING(lc); lc++) {
85
Caspar Zhangd59a6592013-03-07 14:59:12 +080086 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000087
Garrett Cooperfc62c2d2010-12-21 07:07:56 -080088 SAFE_CHDIR(cleanup, testdir);
89
90 fd = SAFE_CREAT(cleanup, filname, 0000);
91 SAFE_CLOSE(cleanup, fd);
92 if ((ddir = opendir(".")) == NULL)
Wanlong Gao354ebb42012-12-07 10:10:04 +080093 tst_brkm(TBROK | TERRNO, cleanup, "opendir(.) failed");
plars865695b2001-08-27 22:15:12 +000094
subrata_modak56207ce2009-03-23 13:35:39 +000095 filenames[0] = ".";
96 filenames[1] = "..";
97 filenames[2] = filname;
Garrett Cooper41bd8fd2010-12-17 11:15:00 -080098 checknames(filenames, sizeof(filenames) / sizeof(filenames[0]),
Wanlong Gao354ebb42012-12-07 10:10:04 +080099 ddir);
Garrett Cooper41bd8fd2010-12-17 11:15:00 -0800100 closedir(ddir);
plars865695b2001-08-27 22:15:12 +0000101
102 TEST(chdir(filname));
103
Garrett Cooper41bd8fd2010-12-17 11:15:00 -0800104 if (TEST_RETURN != -1)
105 tst_resm(TFAIL, "call succeeded unexpectedly");
106 else if (TEST_ERRNO != ENOTDIR)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800107 tst_resm(TFAIL | TTERRNO,
108 "failed unexpectedly; wanted ENOTDIR");
Garrett Cooper41bd8fd2010-12-17 11:15:00 -0800109 else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800110 tst_resm(TPASS, "failed as expected with ENOTDIR");
plars865695b2001-08-27 22:15:12 +0000111
Garrett Cooper41bd8fd2010-12-17 11:15:00 -0800112 if (unlink(filname) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800113 tst_brkm(TBROK | TERRNO, cleanup,
114 "Couldn't remove file");
plars865695b2001-08-27 22:15:12 +0000115
Garrett Cooperfc62c2d2010-12-21 07:07:56 -0800116 SAFE_CHDIR(cleanup, "..");
plars865695b2001-08-27 22:15:12 +0000117
Zeng Linggang4f305452013-10-28 09:32:06 +0800118 /* 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");
plars865695b2001-08-27 22:15:12 +0000135 }
136 cleanup();
137
Garrett Cooper41bd8fd2010-12-17 11:15:00 -0800138 tst_exit();
139
140}
plars865695b2001-08-27 22:15:12 +0000141
subrata_modak56207ce2009-03-23 13:35:39 +0000142void setup(void)
plars865695b2001-08-27 22:15:12 +0000143{
Garrett Cooper2c282152010-12-16 00:55:50 -0800144
plars865695b2001-08-27 22:15:12 +0000145 tst_sig(NOFORK, DEF_HANDLER, cleanup);
146
147 umask(0);
148
plars865695b2001-08-27 22:15:12 +0000149 TEST_PAUSE;
150
plars865695b2001-08-27 22:15:12 +0000151 tst_tmpdir();
152
153 sprintf(testdir, "Testdir.%d", getpid());
154
Garrett Cooperfc62c2d2010-12-21 07:07:56 -0800155 SAFE_MKDIR(cleanup, testdir, 0700);
plars865695b2001-08-27 22:15:12 +0000156}
157
subrata_modak56207ce2009-03-23 13:35:39 +0000158void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000159{
plars865695b2001-08-27 22:15:12 +0000160 tst_rmdir();
plars865695b2001-08-27 22:15:12 +0000161}
162
Wanlong Gao354ebb42012-12-07 10:10:04 +0800163void checknames(char **pfilnames, int fnamecount, DIR * ddir)
plars865695b2001-08-27 22:15:12 +0000164{
165 struct dirent *dir;
subrata_modak56207ce2009-03-23 13:35:39 +0000166 int i, found;
plars865695b2001-08-27 22:15:12 +0000167
subrata_modak56207ce2009-03-23 13:35:39 +0000168 found = 0;
Garrett Cooperfc62c2d2010-12-21 07:07:56 -0800169 while ((dir = readdir(ddir)) != NULL) {
subrata_modak56207ce2009-03-23 13:35:39 +0000170 for (i = 0; i < fnamecount; i++) {
Chris Dearman37550cf2012-10-17 19:54:01 -0700171 /*
Garrett Cooperfc62c2d2010-12-21 07:07:56 -0800172 * if dir->d_name is not null terminated it is a bug
173 * anyway
174 */
subrata_modak56207ce2009-03-23 13:35:39 +0000175 if (strcmp(pfilnames[i], dir->d_name) == 0) {
176 tst_resm(TINFO, "Found file %s", dir->d_name);
177 found++;
178 }
plars865695b2001-08-27 22:15:12 +0000179 }
180 }
subrata_modak56207ce2009-03-23 13:35:39 +0000181 if (found < fnamecount) {
182 tst_brkm(TFAIL, cleanup,
183 "Some files do not exist, but they must exist");
184 }
Garrett Cooperfc62c2d2010-12-21 07:07:56 -0800185}