blob: 5474634b6977cd948a4bceb724a75c28ab8fdcac [file] [log] [blame]
Marios Makris60b1baa2012-08-09 20:36:46 +03001/**********************************************************
alaffincc2e5552000-07-27 17:13:18 +00002 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
vapier45a8ba02009-07-20 10:59:32 +00003 *
alaffincc2e5552000-07-27 17:13:18 +00004 * 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.
vapier45a8ba02009-07-20 10:59:32 +00007 *
alaffincc2e5552000-07-27 17:13:18 +00008 * 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.
vapier45a8ba02009-07-20 10:59:32 +000011 *
alaffincc2e5552000-07-27 17:13:18 +000012 * 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.
vapier45a8ba02009-07-20 10:59:32 +000018 *
alaffincc2e5552000-07-27 17:13:18 +000019 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080020 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
vapier45a8ba02009-07-20 10:59:32 +000022 *
alaffincc2e5552000-07-27 17:13:18 +000023 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
vapier45a8ba02009-07-20 10:59:32 +000025 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
alaffincc2e5552000-07-27 17:13:18 +000030 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
Marios Makris60b1baa2012-08-09 20:36:46 +030031 *********************************************************/
alaffincc2e5552000-07-27 17:13:18 +000032
33/**********************************************************
34 *
Garrett Cooper53740502010-12-16 00:04:01 -080035 * OS Testing - Silicon Graphics, Inc.
alaffincc2e5552000-07-27 17:13:18 +000036 *
Garrett Cooper53740502010-12-16 00:04:01 -080037 * FUNCTION NAME : tst_tmpdir, tst_rmdir
alaffincc2e5552000-07-27 17:13:18 +000038 *
Garrett Cooper53740502010-12-16 00:04:01 -080039 * FUNCTION TITLE : Create/remove a testing temp dir
alaffincc2e5552000-07-27 17:13:18 +000040 *
Garrett Cooper53740502010-12-16 00:04:01 -080041 * SYNOPSIS:
42 * void tst_tmpdir();
43 * void tst_rmdir();
alaffincc2e5552000-07-27 17:13:18 +000044 *
Garrett Cooper53740502010-12-16 00:04:01 -080045 * AUTHOR : Dave Fenner
alaffincc2e5552000-07-27 17:13:18 +000046 *
Garrett Cooper53740502010-12-16 00:04:01 -080047 * INITIAL RELEASE : UNICOS 8.0
alaffincc2e5552000-07-27 17:13:18 +000048 *
Garrett Cooper53740502010-12-16 00:04:01 -080049 * 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().
alaffincc2e5552000-07-27 17:13:18 +000054 *
Garrett Cooper53740502010-12-16 00:04:01 -080055 * RETURN VALUE
56 * Neither tst_tmpdir() or tst_rmdir() has a return value.
alaffincc2e5552000-07-27 17:13:18 +000057 *
Marios Makris60b1baa2012-08-09 20:36:46 +030058 *********************************************************/
59
Stanislav Kholmanskikh57027652015-05-26 13:21:10 +030060#include <sys/mman.h>
Garrett Cooper53740502010-12-16 00:04:01 -080061#include <sys/types.h>
62#include <sys/stat.h>
Garrett Cooperb1c7c6b2010-12-20 07:09:47 -080063#include <assert.h>
alaffincc2e5552000-07-27 17:13:18 +000064#include <errno.h>
Garrett Cooper53740502010-12-16 00:04:01 -080065#include <libgen.h>
66#include <stdio.h>
67#include <stdlib.h>
68#include <string.h>
69#include <unistd.h>
Marios Makris60b1baa2012-08-09 20:36:46 +030070
alaffincc2e5552000-07-27 17:13:18 +000071#include "test.h"
72#include "rmobj.h"
Cyril Hrubised69cd52013-06-24 17:51:00 +020073#include "ltp_priv.h"
Stanislav Kholmanskikh57027652015-05-26 13:21:10 +030074#include "lapi/futex.h"
alaffincc2e5552000-07-27 17:13:18 +000075
76/*
77 * Define some useful macros.
78 */
Garrett Cooper1f3c2362010-12-16 22:05:45 -080079#define DIR_MODE (S_IRWXU|S_IRWXG|S_IRWXO)
alaffincc2e5552000-07-27 17:13:18 +000080
81#ifndef PATH_MAX
82#ifdef MAXPATHLEN
Garrett Cooper1f3c2362010-12-16 22:05:45 -080083#define PATH_MAX MAXPATHLEN
alaffincc2e5552000-07-27 17:13:18 +000084#else
Garrett Cooper1f3c2362010-12-16 22:05:45 -080085#define PATH_MAX 1024
alaffincc2e5552000-07-27 17:13:18 +000086#endif
87#endif
88
89/*
alaffincc2e5552000-07-27 17:13:18 +000090 * Define global variables.
91 */
Garrett Cooperdb976802010-12-16 15:56:25 -080092extern char *TCID; /* defined/initialized in main() */
93static char *TESTDIR = NULL; /* the directory created */
94
Cyril Hrubis69c2ab02012-12-12 17:22:29 +010095static char test_start_work_dir[PATH_MAX];
96
Stanislav Kholmanskikh57027652015-05-26 13:21:10 +030097/* lib/tst_checkpoint.c */
98extern futex_t *tst_futexes;
Cyril Hrubis69c2ab02012-12-12 17:22:29 +010099
Cyril Hrubisab8388c2012-11-28 15:42:17 +0100100int tst_tmpdir_created(void)
101{
102 return TESTDIR != NULL;
103}
104
Cyril Hrubis9c31ad22014-05-14 17:15:39 +0200105char *tst_get_tmpdir(void)
Garrett Cooperdb976802010-12-16 15:56:25 -0800106{
Garrett Cooperb1c7c6b2010-12-20 07:09:47 -0800107 /* Smack the user for calling things out of order. */
108 if (TESTDIR == NULL)
109 tst_brkm(TBROK, NULL, "you must call tst_tmpdir() first");
Garrett Cooperdb976802010-12-16 15:56:25 -0800110 return strdup(TESTDIR);
111}
alaffincc2e5552000-07-27 17:13:18 +0000112
Cyril Hrubis69c2ab02012-12-12 17:22:29 +0100113const char *tst_get_startwd(void)
114{
115 return test_start_work_dir;
116}
117
subrata_modak88c166c2009-06-09 16:01:20 +0000118void tst_tmpdir(void)
alaffincc2e5552000-07-27 17:13:18 +0000119{
Garrett Cooperdb976802010-12-16 15:56:25 -0800120 char template[PATH_MAX];
Xiaoguang Wang33a4adf2014-07-30 12:09:40 +0800121 char *env_tmpdir;
122 char *errmsg, *c;
Garrett Cooper53740502010-12-16 00:04:01 -0800123
Garrett Cooperdb976802010-12-16 15:56:25 -0800124 /*
Marios Makrisc128c452012-08-09 20:36:50 +0300125 * Create a template for the temporary directory. Use the
126 * environment variable TMPDIR if it is available, otherwise
127 * use our default TEMPDIR.
nstrazffe35e22001-03-13 16:33:54 +0000128 */
Xiaoguang Wang33a4adf2014-07-30 12:09:40 +0800129 env_tmpdir = getenv("TMPDIR");
130 if (env_tmpdir) {
131 c = strchr(env_tmpdir, '/');
132 /*
133 * Now we force environment variable TMPDIR to be an absolute
134 * pathname, which dose not make much sense, but it will
135 * greatly simplify code in tst_rmdir().
136 */
137 if (c != env_tmpdir) {
Alexey Kodanev15634ea2016-06-20 17:53:03 +0300138 tst_brkm(TBROK, NULL, "You must specify an absolute "
139 "pathname for environment variable TMPDIR");
Xiaoguang Wang33a4adf2014-07-30 12:09:40 +0800140 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800141 snprintf(template, PATH_MAX, "%s/%.3sXXXXXX", env_tmpdir, TCID);
Xiaoguang Wang33a4adf2014-07-30 12:09:40 +0800142 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800143 snprintf(template, PATH_MAX, "%s/%.3sXXXXXX", TEMPDIR, TCID);
Xiaoguang Wang33a4adf2014-07-30 12:09:40 +0800144 }
vapier45a8ba02009-07-20 10:59:32 +0000145
Marios Makrisc128c452012-08-09 20:36:50 +0300146 /* Make the temporary directory in one shot using mkdtemp. */
147 if (mkdtemp(template) == NULL)
Alexey Kodanev15634ea2016-06-20 17:53:03 +0300148 tst_brkm(TBROK | TERRNO, NULL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800149 "%s: mkdtemp(%s) failed", __func__, template);
Marios Makrisc128c452012-08-09 20:36:50 +0300150 if ((TESTDIR = strdup(template)) == NULL)
Alexey Kodanev15634ea2016-06-20 17:53:03 +0300151 tst_brkm(TBROK | TERRNO, NULL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800152 "%s: strdup(%s) failed", __func__, template);
nstrazffe35e22001-03-13 16:33:54 +0000153
Marios Makrisc128c452012-08-09 20:36:50 +0300154 if (chown(TESTDIR, -1, getgid()) == -1)
Alexey Kodanev15634ea2016-06-20 17:53:03 +0300155 tst_brkm(TBROK | TERRNO, NULL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800156 "chown(%s, -1, %d) failed", TESTDIR, getgid());
Marios Makrisc128c452012-08-09 20:36:50 +0300157 if (chmod(TESTDIR, DIR_MODE) == -1)
Alexey Kodanev15634ea2016-06-20 17:53:03 +0300158 tst_brkm(TBROK | TERRNO, NULL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800159 "chmod(%s, %#o) failed", TESTDIR, DIR_MODE);
alaffincc2e5552000-07-27 17:13:18 +0000160
Cyril Hrubis69c2ab02012-12-12 17:22:29 +0100161 if (getcwd(test_start_work_dir, sizeof(test_start_work_dir)) == NULL) {
162 tst_resm(TINFO, "Failed to record test working dir");
163 test_start_work_dir[0] = '\0';
164 }
165
Marios Makris60b1baa2012-08-09 20:36:46 +0300166 /*
167 * Change to the temporary directory. If the chdir() fails, issue
Garrett Cooper53740502010-12-16 00:04:01 -0800168 * TBROK messages for all test cases, attempt to remove the
nstrazffe35e22001-03-13 16:33:54 +0000169 * directory (if it was created), and exit. If the removal also
vapier45a8ba02009-07-20 10:59:32 +0000170 * fails, also issue a TWARN message.
nstrazffe35e22001-03-13 16:33:54 +0000171 */
Garrett Cooper903910d2010-11-23 09:27:44 -0800172 if (chdir(TESTDIR) == -1) {
Xiaoguang Wang33a4adf2014-07-30 12:09:40 +0800173 tst_resm(TERRNO, "%s: chdir(%s) failed", __func__, TESTDIR);
alaffincc2e5552000-07-27 17:13:18 +0000174
nstrazffe35e22001-03-13 16:33:54 +0000175 /* Try to remove the directory */
Xiaoguang Wang33a4adf2014-07-30 12:09:40 +0800176 if (rmobj(TESTDIR, &errmsg) == -1) {
Marios Makrisc128c452012-08-09 20:36:50 +0300177 tst_resm(TWARN, "%s: rmobj(%s) failed: %s",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800178 __func__, TESTDIR, errmsg);
Xiaoguang Wang33a4adf2014-07-30 12:09:40 +0800179 }
alaffincc2e5552000-07-27 17:13:18 +0000180
Xiaoguang Wang33a4adf2014-07-30 12:09:40 +0800181 tst_exit();
nstrazffe35e22001-03-13 16:33:54 +0000182 }
Marios Makris60b1baa2012-08-09 20:36:46 +0300183}
alaffincc2e5552000-07-27 17:13:18 +0000184
subrata_modak88c166c2009-06-09 16:01:20 +0000185void tst_rmdir(void)
alaffincc2e5552000-07-27 17:13:18 +0000186{
Garrett Cooper53740502010-12-16 00:04:01 -0800187 char *errmsg;
vapier45a8ba02009-07-20 10:59:32 +0000188
Garrett Cooper53740502010-12-16 00:04:01 -0800189 /*
190 * Check that TESTDIR is not NULL.
191 */
192 if (TESTDIR == NULL) {
Garrett Cooperdb976802010-12-16 15:56:25 -0800193 tst_resm(TWARN,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800194 "%s: TESTDIR was NULL; no removal attempted",
195 __func__);
Garrett Cooper53740502010-12-16 00:04:01 -0800196 return;
197 }
alaffincc2e5552000-07-27 17:13:18 +0000198
Garrett Cooper53740502010-12-16 00:04:01 -0800199 /*
Stanislav Kholmanskikh57027652015-05-26 13:21:10 +0300200 * Unmap the backend file.
201 * This is needed to overcome the NFS "silly rename" feature.
202 */
Stanislav Kholmanskikh401d0cb2015-05-28 11:58:10 +0300203 if (tst_futexes) {
204 msync((void *)tst_futexes, getpagesize(), MS_SYNC);
Stanislav Kholmanskikh57027652015-05-26 13:21:10 +0300205 munmap((void *)tst_futexes, getpagesize());
Stanislav Kholmanskikh401d0cb2015-05-28 11:58:10 +0300206 }
Stanislav Kholmanskikh57027652015-05-26 13:21:10 +0300207
208 /*
Garrett Cooper53740502010-12-16 00:04:01 -0800209 * Attempt to remove the "TESTDIR" directory, using rmobj().
210 */
Xiaoguang Wang33a4adf2014-07-30 12:09:40 +0800211 if (rmobj(TESTDIR, &errmsg) == -1) {
Garrett Cooper53740502010-12-16 00:04:01 -0800212 tst_resm(TWARN, "%s: rmobj(%s) failed: %s",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800213 __func__, TESTDIR, errmsg);
Xiaoguang Wang33a4adf2014-07-30 12:09:40 +0800214 }
Marios Makris60b1baa2012-08-09 20:36:46 +0300215}