1 /**********************************************************
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 *********************************************************/
32
33 /**********************************************************
34 *
35 * OS Testing - Silicon Graphics, Inc.
36 *
37 * FUNCTION NAME : tst_tmpdir, tst_rmdir
38 *
39 * FUNCTION TITLE : Create/remove a testing temp dir
40 *
41 * SYNOPSIS:
42 * void tst_tmpdir();
43 * void tst_rmdir();
44 *
45 * AUTHOR : Dave Fenner
46 *
47 * INITIAL RELEASE : UNICOS 8.0
48 *
49 * DESCRIPTION
50 * tst_tmpdir() is used to create a unique, temporary testing
51 * directory, and make it the current working directory.
52 * tst_rmdir() is used to remove the directory created by
53 * tst_tmpdir().
54 *
55 * RETURN VALUE
56 * Neither tst_tmpdir() or tst_rmdir() has a return value.
57 *
58 *********************************************************/
59 #define _GNU_SOURCE
60 #include <sys/mman.h>
61 #include <sys/types.h>
62 #include <sys/stat.h>
63 #include <assert.h>
64 #include <errno.h>
65 #include <libgen.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <unistd.h>
70 #include <dirent.h>
71 #include <fcntl.h>
72
73 #include "test.h"
74 #include "safe_macros.h"
75 #include "ltp_priv.h"
76 #include "lapi/futex.h"
77
78 /*
79 * Define some useful macros.
80 */
81 #define DIR_MODE (S_IRWXU|S_IRWXG|S_IRWXO)
82
83 #ifndef PATH_MAX
84 #ifdef MAXPATHLEN
85 #define PATH_MAX MAXPATHLEN
86 #else
87 #define PATH_MAX 1024
88 #endif
89 #endif
90
91 /*
92 * Define global variables.
93 */
94 extern char *TCID; /* defined/initialized in main() */
95 static char *TESTDIR = NULL; /* the directory created */
96
97 static char test_start_work_dir[PATH_MAX];
98
99 /* lib/tst_checkpoint.c */
100 extern futex_t *tst_futexes;
101
102 static int rmobj(const char *obj, char **errmsg);
103
tst_tmpdir_created(void)104 int tst_tmpdir_created(void)
105 {
106 return TESTDIR != NULL;
107 }
108
tst_get_tmpdir(void)109 char *tst_get_tmpdir(void)
110 {
111 if (TESTDIR == NULL) {
112 tst_brkm(TBROK, NULL, "you must call tst_tmpdir() first");
113 return NULL;
114 }
115
116 return strdup(TESTDIR);
117 }
118
tst_get_startwd(void)119 const char *tst_get_startwd(void)
120 {
121 return test_start_work_dir;
122 }
123
purge_dir(const char * path,char ** errptr)124 static int purge_dir(const char *path, char **errptr)
125 {
126 int ret_val = 0;
127 DIR *dir;
128 struct dirent *dir_ent;
129 char dirobj[PATH_MAX];
130 static char err_msg[PATH_MAX + 1280];
131
132 /* Do NOT perform the request if the directory is "/" */
133 if (!strcmp(path, "/")) {
134 if (errptr) {
135 strcpy(err_msg, "Cannot purge system root directory");
136 *errptr = err_msg;
137 }
138
139 return -1;
140 }
141
142 errno = 0;
143
144 /* Open the directory to get access to what is in it */
145 if (!(dir = opendir(path))) {
146 if (errptr) {
147 sprintf(err_msg,
148 "Cannot open directory %s; errno=%d: %s",
149 path, errno, tst_strerrno(errno));
150 *errptr = err_msg;
151 }
152 return -1;
153 }
154
155 /* Loop through the entries in the directory, removing each one */
156 for (dir_ent = readdir(dir); dir_ent; dir_ent = readdir(dir)) {
157 /* Don't remove "." or ".." */
158 if (!strcmp(dir_ent->d_name, ".")
159 || !strcmp(dir_ent->d_name, ".."))
160 continue;
161
162 /* Recursively remove the current entry */
163 sprintf(dirobj, "%s/%s", path, dir_ent->d_name);
164 if (rmobj(dirobj, errptr) != 0)
165 ret_val = -1;
166 }
167
168 closedir(dir);
169 return ret_val;
170 }
171
rmobj(const char * obj,char ** errmsg)172 static int rmobj(const char *obj, char **errmsg)
173 {
174 int ret_val = 0;
175 struct stat statbuf;
176 static char err_msg[PATH_MAX + 1280];
177 int fd;
178
179 fd = open(obj, O_DIRECTORY | O_NOFOLLOW);
180 if (fd >= 0) {
181 close(fd);
182 ret_val = purge_dir(obj, errmsg);
183
184 /* If there were problems removing an entry, don't attempt to
185 remove the directory itself */
186 if (ret_val == -1)
187 return -1;
188
189 /* Get the link count, now that all the entries have been removed */
190 if (lstat(obj, &statbuf) < 0) {
191 if (errmsg != NULL) {
192 sprintf(err_msg,
193 "lstat(%s) failed; errno=%d: %s", obj,
194 errno, tst_strerrno(errno));
195 *errmsg = err_msg;
196 }
197 return -1;
198 }
199
200 /* Remove the directory itself */
201 if (statbuf.st_nlink >= 3) {
202 /* The directory is linked; unlink() must be used */
203 if (unlink(obj) < 0) {
204 if (errmsg != NULL) {
205 sprintf(err_msg,
206 "unlink(%s) failed; errno=%d: %s",
207 obj, errno, tst_strerrno(errno));
208 *errmsg = err_msg;
209 }
210 return -1;
211 }
212 } else {
213 /* The directory is not linked; remove() can be used */
214 if (remove(obj) < 0) {
215 if (errmsg != NULL) {
216 sprintf(err_msg,
217 "remove(%s) failed; errno=%d: %s",
218 obj, errno, tst_strerrno(errno));
219 *errmsg = err_msg;
220 }
221 return -1;
222 }
223 }
224 } else {
225 if (unlink(obj) < 0) {
226 if (errmsg != NULL) {
227 sprintf(err_msg,
228 "unlink(%s) failed; errno=%d: %s", obj,
229 errno, tst_strerrno(errno));
230 *errmsg = err_msg;
231 }
232 return -1;
233 }
234 }
235
236 return 0;
237 }
238
tst_tmpdir(void)239 void tst_tmpdir(void)
240 {
241 char template[PATH_MAX];
242 char *env_tmpdir;
243 char *errmsg, *c;
244
245 /*
246 * Create a template for the temporary directory. Use the
247 * environment variable TMPDIR if it is available, otherwise
248 * use our default TEMPDIR.
249 */
250 env_tmpdir = getenv("TMPDIR");
251 if (env_tmpdir) {
252 c = strchr(env_tmpdir, '/');
253 /*
254 * Now we force environment variable TMPDIR to be an absolute
255 * pathname, which dose not make much sense, but it will
256 * greatly simplify code in tst_rmdir().
257 */
258 if (c != env_tmpdir) {
259 tst_brkm(TBROK, NULL, "You must specify an absolute "
260 "pathname for environment variable TMPDIR");
261 return;
262 }
263 snprintf(template, PATH_MAX, "%s/%.3sXXXXXX", env_tmpdir, TCID);
264 } else {
265 snprintf(template, PATH_MAX, "%s/%.3sXXXXXX", TEMPDIR, TCID);
266 }
267
268 /* Make the temporary directory in one shot using mkdtemp. */
269 if (mkdtemp(template) == NULL) {
270 tst_brkm(TBROK | TERRNO, NULL,
271 "%s: mkdtemp(%s) failed", __func__, template);
272 return;
273 }
274
275 if ((TESTDIR = strdup(template)) == NULL) {
276 tst_brkm(TBROK | TERRNO, NULL,
277 "%s: strdup(%s) failed", __func__, template);
278 return;
279 }
280
281 SAFE_CHOWN(NULL, TESTDIR, -1, getgid());
282
283 SAFE_CHMOD(NULL, TESTDIR, DIR_MODE);
284
285 if (getcwd(test_start_work_dir, sizeof(test_start_work_dir)) == NULL) {
286 tst_resm(TINFO, "Failed to record test working dir");
287 test_start_work_dir[0] = '\0';
288 }
289
290 /*
291 * Change to the temporary directory. If the chdir() fails, issue
292 * TBROK messages for all test cases, attempt to remove the
293 * directory (if it was created), and exit. If the removal also
294 * fails, also issue a TWARN message.
295 */
296 if (chdir(TESTDIR) == -1) {
297 tst_resm(TERRNO, "%s: chdir(%s) failed", __func__, TESTDIR);
298
299 /* Try to remove the directory */
300 if (rmobj(TESTDIR, &errmsg) == -1) {
301 tst_resm(TWARN, "%s: rmobj(%s) failed: %s",
302 __func__, TESTDIR, errmsg);
303 }
304
305 tst_exit();
306 }
307 }
308
tst_rmdir(void)309 void tst_rmdir(void)
310 {
311 char *errmsg;
312
313 /*
314 * Check that TESTDIR is not NULL.
315 */
316 if (TESTDIR == NULL) {
317 tst_resm(TWARN,
318 "%s: TESTDIR was NULL; no removal attempted",
319 __func__);
320 return;
321 }
322
323 /*
324 * Unmap the backend file.
325 * This is needed to overcome the NFS "silly rename" feature.
326 */
327 if (tst_futexes) {
328 msync((void *)tst_futexes, getpagesize(), MS_SYNC);
329 munmap((void *)tst_futexes, getpagesize());
330 }
331
332 /*
333 * Attempt to remove the "TESTDIR" directory, using rmobj().
334 */
335 if (rmobj(TESTDIR, &errmsg) == -1) {
336 tst_resm(TWARN, "%s: rmobj(%s) failed: %s",
337 __func__, TESTDIR, errmsg);
338 }
339 }
340
tst_purge_dir(const char * path)341 void tst_purge_dir(const char *path)
342 {
343 char *err;
344
345 if (purge_dir(path, &err))
346 tst_brkm(TBROK, NULL, "%s: %s", __func__, err);
347 }
348