• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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>
54 #include <string.h>
55 #include <fcntl.h>
56 #include<sys/stat.h>
57 #include "test.h"
58 #include "safe_macros.h"
59 
60 char *TCID = "chdir01";
61 int TST_TOTAL = 2;
62 
63 void setup(void);
64 void cleanup(void);
65 static void checknames(char **, int, DIR *);
66 
67 char testdir[40] = "";
68 
main(int ac,char ** av)69 int main(int ac, char **av)
70 {
71 	DIR *ddir, *opendir();
72 	int fd;
73 	char *filname = "chdirtest";
74 	char *filenames[3];
75 
76 	int lc;
77 
78 	tst_parse_opts(ac, av, NULL, NULL);
79 
80 	setup();
81 
82 	for (lc = 0; TEST_LOOPING(lc); lc++) {
83 
84 		tst_count = 0;
85 
86 		SAFE_CHDIR(cleanup, testdir);
87 
88 		fd = SAFE_CREAT(cleanup, filname, 0000);
89 		SAFE_CLOSE(cleanup, fd);
90 		if ((ddir = opendir(".")) == NULL)
91 			tst_brkm(TBROK | TERRNO, cleanup, "opendir(.) failed");
92 
93 		filenames[0] = ".";
94 		filenames[1] = "..";
95 		filenames[2] = filname;
96 		checknames(filenames, sizeof(filenames) / sizeof(filenames[0]),
97 			   ddir);
98 		closedir(ddir);
99 
100 		TEST(chdir(filname));
101 
102 		if (TEST_RETURN != -1)
103 			tst_resm(TFAIL, "call succeeded unexpectedly");
104 		else if (TEST_ERRNO != ENOTDIR)
105 			tst_resm(TFAIL | TTERRNO,
106 				 "failed unexpectedly; wanted ENOTDIR");
107 		else
108 			tst_resm(TPASS, "failed as expected with ENOTDIR");
109 
110 		SAFE_UNLINK(cleanup, filname);
111 
112 		SAFE_CHDIR(cleanup, "..");
113 
114 		/* ELOOP */
115 		SAFE_SYMLINK(cleanup, "test_eloop1", "test_eloop2");
116 		SAFE_SYMLINK(cleanup, "test_eloop2", "test_eloop1");
117 
118 		TEST(chdir("test_eloop1"));
119 
120 		if (TEST_RETURN != -1) {
121 			tst_resm(TFAIL, "call succeeded unexpectedly");
122 		} else if (TEST_ERRNO != ELOOP) {
123 			tst_resm(TFAIL | TTERRNO,
124 				 "failed unexpectedly; wanted ELOOP");
125 		} else {
126 			tst_resm(TPASS, "failed as expected with ELOOP");
127 		}
128 
129 		SAFE_UNLINK(cleanup, "test_eloop1");
130 		SAFE_UNLINK(cleanup, "test_eloop2");
131 	}
132 	cleanup();
133 
134 	tst_exit();
135 
136 }
137 
setup(void)138 void setup(void)
139 {
140 
141 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
142 
143 	umask(0);
144 
145 	TEST_PAUSE;
146 
147 	tst_tmpdir();
148 
149 	sprintf(testdir, "Testdir.%d", getpid());
150 
151 	SAFE_MKDIR(cleanup, testdir, 0700);
152 }
153 
cleanup(void)154 void cleanup(void)
155 {
156 	tst_rmdir();
157 }
158 
checknames(char ** pfilnames,int fnamecount,DIR * ddir)159 void checknames(char **pfilnames, int fnamecount, DIR * ddir)
160 {
161 	struct dirent *dir;
162 	int i, found;
163 
164 	found = 0;
165 	while ((dir = readdir(ddir)) != NULL) {
166 		for (i = 0; i < fnamecount; i++) {
167 			/*
168 			 * if dir->d_name is not null terminated it is a bug
169 			 * anyway
170 			 */
171 			if (strcmp(pfilnames[i], dir->d_name) == 0) {
172 				tst_resm(TINFO, "Found file %s", dir->d_name);
173 				found++;
174 			}
175 		}
176 	}
177 	if (found < fnamecount) {
178 		tst_brkm(TFAIL, cleanup,
179 			 "Some files do not exist, but they must exist");
180 	}
181 }
182