blob: 267d42a8d7c7fe0b47fd9d8bc6030b8138a7d98a [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
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/*
21 * NAME
22 * fchdir01.c
23 *
24 * DESCRIPTION
25 * fchdir01 - create a directory and cd into it.
26 *
27 * ALGORITHM
28 * create a new directory
29 * open the directory and get a file descriptor
30 * loop if that option was specified
31 * fchdir() into the directory
32 * check the return code
33 * if failure, issue a FAIL message.
34 * otherwise,
35 * if doing functionality testing, call check_functionality()
36 * if correct,
37 * issue a PASS message
38 * otherwise
39 * issue a FAIL message
40 * call cleanup
41 *
42 * USAGE: <for command-line>
43 * fchdir01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
44 * where, -c n : Run n copies concurrently.
45 * -f : Turn off functionality Testing.
46 * -i n : Execute test n times.
47 * -I x : Execute test for x seconds.
48 * -P x : Pause for x seconds between iterations.
49 * -t : Turn on syscall timing.
50 *
51 * HISTORY
52 * 03/2001 - Written by Wayne Boyer
53 *
54 * RESTRICTIONS
55 * none
56 */
57
58#include "test.h"
59#include "usctest.h"
60
61#include <errno.h>
62#include <sys/stat.h>
63#include <fcntl.h>
64#include <string.h>
65
66void cleanup(void);
67void setup(void);
68
69char *TCID = "fchdir01";
70int TST_TOTAL = 1;
71extern int Tst_count;
72
73int fd;
74char *temp_dir;
75const char *TEST_DIR = "alpha";
76
77#define MODES S_IRWXU
78
plars74948ad2002-11-14 16:16:14 +000079int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000080{
81 int lc; /* loop counter */
82 char *msg; /* message returned from parse_opts */
83 void check_functionality(void);
84 int r_val;
85
86 /* parse standard options */
87 if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){
88 tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
89 }
90
91 setup(); /* global setup */
92
93 /* The following loop checks looping state if -i option given */
94
95 for (lc = 0; TEST_LOOPING(lc); lc++) {
96 /* reset Tst_count in case we are looping */
97 Tst_count = 0;
98
99 /* get the name of the test dirctory */
100 if ((temp_dir = (getcwd(temp_dir, 0))) == NULL) {
101 tst_brkm(TBROK, cleanup, "%s - getcwd() in main() "
102 "failed", TCID);
103 }
104
105 /*
106 * create a new directory and open it
107 */
108
109 if ((r_val = mkdir(TEST_DIR, MODES)) == -1){
110 tst_brkm(TBROK, cleanup, "%s - mkdir() in main() "
111 "failed", TCID);
112 }
113
114 if ((fd = open(TEST_DIR, O_RDONLY)) == -1) {
115 tst_brkm(TBROK, cleanup, "open of directory failed");
116 }
117
118 /*
119 * Use TEST macro to make the call
120 */
121
122 TEST(fchdir(fd));
123
124 if (TEST_RETURN == -1) {
125 tst_brkm(TFAIL, cleanup, "%s call failed - errno = %d :"
126 " %s", TCID, TEST_ERRNO, strerror(TEST_ERRNO));
127 } else {
128 if (STD_FUNCTIONAL_TEST) {
129 check_functionality();
130 } else {
131 tst_resm(TPASS, "call succeeded");
132 }
133 }
134
135 /*
136 * clean up things in case we are looping
137 */
138
139 /*
140 * NOTE: in case of failure here, we need to use "tst_resm()"
141 * and not "tst_brkm()". This is because if we get to this
142 * point, we have already set a PASS or FAIL for the test
143 * and "tst_brkm()" won't report as we might expect.
144 */
145
146 /* chdir back to our temporary work directory */
147 if ((r_val = chdir("..")) == -1){
148 tst_resm(TBROK, "fchdir failed - errno = %d : %s",
149 errno, strerror(errno));
150 }
151
152 if ((r_val = rmdir(TEST_DIR)) == -1){
153 tst_resm(TBROK, "rmdir failed - errno = %d : %s",
154 errno, strerror(errno));
155 }
156
157 /*
158 * clean up things in case we are looping
159 */
160 free(temp_dir);
161 temp_dir = NULL;
162 }
163
164 cleanup();
165
166 /*NOTREACHED*/
subrata_modak43337a32009-02-26 11:43:51 +0000167 return 0;
plars865695b2001-08-27 22:15:12 +0000168}
169
170/*
171 * check_functionality() - check that we are in the correct directory.
172 */
173void
174check_functionality(void)
175{
176 char *buf = NULL;
177 char **bufptr = &buf;
178 char *dir;
179
180 /*
181 * Get the current directory path.
182 */
183 if ((buf = (getcwd(buf, 0))) == NULL) {
184 tst_brkm(TBROK, cleanup, "%s - getcwd() in "
185 "check_functionality() failed", TCID);
186 }
187
188 /*
189 * strip off all but the last directory name in the
190 * current working directory.
191 */
192 do {
193 if ((dir = strsep(bufptr, "/")) == NULL) {
194 tst_brkm(TBROK, cleanup, "%s - strsep() in "
195 "check_functionality() failed", TCID);
196 }
197 } while(*bufptr != NULL);
198
199 /*
200 * Make sure we are in the right place.
201 */
202 if (strcmp(TEST_DIR, dir) == 0) {
203 tst_resm(TPASS, "%s call succeeded", TCID);
204 } else {
205 tst_resm(TFAIL, "%s functionality test failed", TCID);
206 }
207}
208
209/*
210 * setup() - performs all the ONE TIME setup for this test.
211 */
212void
213setup(void)
214{
215 /* capture signals */
216 tst_sig(NOFORK, DEF_HANDLER, cleanup);
217
218 /* Pause if that option was specified */
219 TEST_PAUSE;
220
221 /* create a test directory and cd into it */
222 tst_tmpdir();
223}
224
225/*
226 * cleanup() - performs all the ONE TIME cleanup for this test at completion
227 * or premature exit.
228 */
229void
230cleanup(void)
231{
232 /* remove the test directory */
233 tst_rmdir();
234
235 /*
236 * print timing stats if that option was specified.
237 * print errno log if that option was specified.
238 */
239 TEST_CLEANUP;
240
241 /* exit with return code appropriate for results */
242 tst_exit();
243}
244