Marios Makris | 60b1baa | 2012-08-09 20:36:46 +0300 | [diff] [blame] | 1 | /********************************************************** |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 2 | * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 3 | * |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 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. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 7 | * |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 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. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 11 | * |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 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. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 18 | * |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 19 | * You should have received a copy of the GNU General Public License along |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 20 | * with this program; if not, write the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 22 | * |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 24 | * Mountain View, CA 94043, or: |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 25 | * |
| 26 | * http://www.sgi.com |
| 27 | * |
| 28 | * For further information regarding this notice, see: |
| 29 | * |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 30 | * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
Marios Makris | 60b1baa | 2012-08-09 20:36:46 +0300 | [diff] [blame] | 31 | *********************************************************/ |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 32 | |
| 33 | /********************************************************** |
| 34 | * |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 35 | * OS Testing - Silicon Graphics, Inc. |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 36 | * |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 37 | * FUNCTION NAME : tst_tmpdir, tst_rmdir |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 38 | * |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 39 | * FUNCTION TITLE : Create/remove a testing temp dir |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 40 | * |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 41 | * SYNOPSIS: |
| 42 | * void tst_tmpdir(); |
| 43 | * void tst_rmdir(); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 44 | * |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 45 | * AUTHOR : Dave Fenner |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 46 | * |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 47 | * INITIAL RELEASE : UNICOS 8.0 |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 48 | * |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 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(). |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 54 | * |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 55 | * RETURN VALUE |
| 56 | * Neither tst_tmpdir() or tst_rmdir() has a return value. |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 57 | * |
Marios Makris | 60b1baa | 2012-08-09 20:36:46 +0300 | [diff] [blame] | 58 | *********************************************************/ |
| 59 | |
Stanislav Kholmanskikh | 5702765 | 2015-05-26 13:21:10 +0300 | [diff] [blame] | 60 | #include <sys/mman.h> |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 61 | #include <sys/types.h> |
| 62 | #include <sys/stat.h> |
Garrett Cooper | b1c7c6b | 2010-12-20 07:09:47 -0800 | [diff] [blame] | 63 | #include <assert.h> |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 64 | #include <errno.h> |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 65 | #include <libgen.h> |
| 66 | #include <stdio.h> |
| 67 | #include <stdlib.h> |
| 68 | #include <string.h> |
| 69 | #include <unistd.h> |
Marios Makris | 60b1baa | 2012-08-09 20:36:46 +0300 | [diff] [blame] | 70 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 71 | #include "test.h" |
| 72 | #include "rmobj.h" |
Cyril Hrubis | ed69cd5 | 2013-06-24 17:51:00 +0200 | [diff] [blame] | 73 | #include "ltp_priv.h" |
Stanislav Kholmanskikh | 5702765 | 2015-05-26 13:21:10 +0300 | [diff] [blame] | 74 | #include "lapi/futex.h" |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 75 | |
| 76 | /* |
| 77 | * Define some useful macros. |
| 78 | */ |
Garrett Cooper | 1f3c236 | 2010-12-16 22:05:45 -0800 | [diff] [blame] | 79 | #define DIR_MODE (S_IRWXU|S_IRWXG|S_IRWXO) |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 80 | |
| 81 | #ifndef PATH_MAX |
| 82 | #ifdef MAXPATHLEN |
Garrett Cooper | 1f3c236 | 2010-12-16 22:05:45 -0800 | [diff] [blame] | 83 | #define PATH_MAX MAXPATHLEN |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 84 | #else |
Garrett Cooper | 1f3c236 | 2010-12-16 22:05:45 -0800 | [diff] [blame] | 85 | #define PATH_MAX 1024 |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 86 | #endif |
| 87 | #endif |
| 88 | |
| 89 | /* |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 90 | * Define global variables. |
| 91 | */ |
Garrett Cooper | db97680 | 2010-12-16 15:56:25 -0800 | [diff] [blame] | 92 | extern char *TCID; /* defined/initialized in main() */ |
| 93 | static char *TESTDIR = NULL; /* the directory created */ |
| 94 | |
Cyril Hrubis | 69c2ab0 | 2012-12-12 17:22:29 +0100 | [diff] [blame] | 95 | static char test_start_work_dir[PATH_MAX]; |
| 96 | |
Stanislav Kholmanskikh | 5702765 | 2015-05-26 13:21:10 +0300 | [diff] [blame] | 97 | /* lib/tst_checkpoint.c */ |
| 98 | extern futex_t *tst_futexes; |
Cyril Hrubis | 69c2ab0 | 2012-12-12 17:22:29 +0100 | [diff] [blame] | 99 | |
Cyril Hrubis | ab8388c | 2012-11-28 15:42:17 +0100 | [diff] [blame] | 100 | int tst_tmpdir_created(void) |
| 101 | { |
| 102 | return TESTDIR != NULL; |
| 103 | } |
| 104 | |
Cyril Hrubis | 9c31ad2 | 2014-05-14 17:15:39 +0200 | [diff] [blame] | 105 | char *tst_get_tmpdir(void) |
Garrett Cooper | db97680 | 2010-12-16 15:56:25 -0800 | [diff] [blame] | 106 | { |
Garrett Cooper | b1c7c6b | 2010-12-20 07:09:47 -0800 | [diff] [blame] | 107 | /* 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 Cooper | db97680 | 2010-12-16 15:56:25 -0800 | [diff] [blame] | 110 | return strdup(TESTDIR); |
| 111 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 112 | |
Cyril Hrubis | 69c2ab0 | 2012-12-12 17:22:29 +0100 | [diff] [blame] | 113 | const char *tst_get_startwd(void) |
| 114 | { |
| 115 | return test_start_work_dir; |
| 116 | } |
| 117 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 118 | void tst_tmpdir(void) |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 119 | { |
Garrett Cooper | db97680 | 2010-12-16 15:56:25 -0800 | [diff] [blame] | 120 | char template[PATH_MAX]; |
Xiaoguang Wang | 33a4adf | 2014-07-30 12:09:40 +0800 | [diff] [blame] | 121 | char *env_tmpdir; |
| 122 | char *errmsg, *c; |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 123 | |
Garrett Cooper | db97680 | 2010-12-16 15:56:25 -0800 | [diff] [blame] | 124 | /* |
Marios Makris | c128c45 | 2012-08-09 20:36:50 +0300 | [diff] [blame] | 125 | * Create a template for the temporary directory. Use the |
| 126 | * environment variable TMPDIR if it is available, otherwise |
| 127 | * use our default TEMPDIR. |
nstraz | ffe35e2 | 2001-03-13 16:33:54 +0000 | [diff] [blame] | 128 | */ |
Xiaoguang Wang | 33a4adf | 2014-07-30 12:09:40 +0800 | [diff] [blame] | 129 | 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 Kodanev | 15634ea | 2016-06-20 17:53:03 +0300 | [diff] [blame] | 138 | tst_brkm(TBROK, NULL, "You must specify an absolute " |
| 139 | "pathname for environment variable TMPDIR"); |
Xiaoguang Wang | 33a4adf | 2014-07-30 12:09:40 +0800 | [diff] [blame] | 140 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 141 | snprintf(template, PATH_MAX, "%s/%.3sXXXXXX", env_tmpdir, TCID); |
Xiaoguang Wang | 33a4adf | 2014-07-30 12:09:40 +0800 | [diff] [blame] | 142 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 143 | snprintf(template, PATH_MAX, "%s/%.3sXXXXXX", TEMPDIR, TCID); |
Xiaoguang Wang | 33a4adf | 2014-07-30 12:09:40 +0800 | [diff] [blame] | 144 | } |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 145 | |
Marios Makris | c128c45 | 2012-08-09 20:36:50 +0300 | [diff] [blame] | 146 | /* Make the temporary directory in one shot using mkdtemp. */ |
| 147 | if (mkdtemp(template) == NULL) |
Alexey Kodanev | 15634ea | 2016-06-20 17:53:03 +0300 | [diff] [blame] | 148 | tst_brkm(TBROK | TERRNO, NULL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 149 | "%s: mkdtemp(%s) failed", __func__, template); |
Marios Makris | c128c45 | 2012-08-09 20:36:50 +0300 | [diff] [blame] | 150 | if ((TESTDIR = strdup(template)) == NULL) |
Alexey Kodanev | 15634ea | 2016-06-20 17:53:03 +0300 | [diff] [blame] | 151 | tst_brkm(TBROK | TERRNO, NULL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 152 | "%s: strdup(%s) failed", __func__, template); |
nstraz | ffe35e2 | 2001-03-13 16:33:54 +0000 | [diff] [blame] | 153 | |
Marios Makris | c128c45 | 2012-08-09 20:36:50 +0300 | [diff] [blame] | 154 | if (chown(TESTDIR, -1, getgid()) == -1) |
Alexey Kodanev | 15634ea | 2016-06-20 17:53:03 +0300 | [diff] [blame] | 155 | tst_brkm(TBROK | TERRNO, NULL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 156 | "chown(%s, -1, %d) failed", TESTDIR, getgid()); |
Marios Makris | c128c45 | 2012-08-09 20:36:50 +0300 | [diff] [blame] | 157 | if (chmod(TESTDIR, DIR_MODE) == -1) |
Alexey Kodanev | 15634ea | 2016-06-20 17:53:03 +0300 | [diff] [blame] | 158 | tst_brkm(TBROK | TERRNO, NULL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 159 | "chmod(%s, %#o) failed", TESTDIR, DIR_MODE); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 160 | |
Cyril Hrubis | 69c2ab0 | 2012-12-12 17:22:29 +0100 | [diff] [blame] | 161 | 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 Makris | 60b1baa | 2012-08-09 20:36:46 +0300 | [diff] [blame] | 166 | /* |
| 167 | * Change to the temporary directory. If the chdir() fails, issue |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 168 | * TBROK messages for all test cases, attempt to remove the |
nstraz | ffe35e2 | 2001-03-13 16:33:54 +0000 | [diff] [blame] | 169 | * directory (if it was created), and exit. If the removal also |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 170 | * fails, also issue a TWARN message. |
nstraz | ffe35e2 | 2001-03-13 16:33:54 +0000 | [diff] [blame] | 171 | */ |
Garrett Cooper | 903910d | 2010-11-23 09:27:44 -0800 | [diff] [blame] | 172 | if (chdir(TESTDIR) == -1) { |
Xiaoguang Wang | 33a4adf | 2014-07-30 12:09:40 +0800 | [diff] [blame] | 173 | tst_resm(TERRNO, "%s: chdir(%s) failed", __func__, TESTDIR); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 174 | |
nstraz | ffe35e2 | 2001-03-13 16:33:54 +0000 | [diff] [blame] | 175 | /* Try to remove the directory */ |
Xiaoguang Wang | 33a4adf | 2014-07-30 12:09:40 +0800 | [diff] [blame] | 176 | if (rmobj(TESTDIR, &errmsg) == -1) { |
Marios Makris | c128c45 | 2012-08-09 20:36:50 +0300 | [diff] [blame] | 177 | tst_resm(TWARN, "%s: rmobj(%s) failed: %s", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 178 | __func__, TESTDIR, errmsg); |
Xiaoguang Wang | 33a4adf | 2014-07-30 12:09:40 +0800 | [diff] [blame] | 179 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 180 | |
Xiaoguang Wang | 33a4adf | 2014-07-30 12:09:40 +0800 | [diff] [blame] | 181 | tst_exit(); |
nstraz | ffe35e2 | 2001-03-13 16:33:54 +0000 | [diff] [blame] | 182 | } |
Marios Makris | 60b1baa | 2012-08-09 20:36:46 +0300 | [diff] [blame] | 183 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 184 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 185 | void tst_rmdir(void) |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 186 | { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 187 | char *errmsg; |
vapier | 45a8ba0 | 2009-07-20 10:59:32 +0000 | [diff] [blame] | 188 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 189 | /* |
| 190 | * Check that TESTDIR is not NULL. |
| 191 | */ |
| 192 | if (TESTDIR == NULL) { |
Garrett Cooper | db97680 | 2010-12-16 15:56:25 -0800 | [diff] [blame] | 193 | tst_resm(TWARN, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 194 | "%s: TESTDIR was NULL; no removal attempted", |
| 195 | __func__); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 196 | return; |
| 197 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 198 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 199 | /* |
Stanislav Kholmanskikh | 5702765 | 2015-05-26 13:21:10 +0300 | [diff] [blame] | 200 | * Unmap the backend file. |
| 201 | * This is needed to overcome the NFS "silly rename" feature. |
| 202 | */ |
Stanislav Kholmanskikh | 401d0cb | 2015-05-28 11:58:10 +0300 | [diff] [blame] | 203 | if (tst_futexes) { |
| 204 | msync((void *)tst_futexes, getpagesize(), MS_SYNC); |
Stanislav Kholmanskikh | 5702765 | 2015-05-26 13:21:10 +0300 | [diff] [blame] | 205 | munmap((void *)tst_futexes, getpagesize()); |
Stanislav Kholmanskikh | 401d0cb | 2015-05-28 11:58:10 +0300 | [diff] [blame] | 206 | } |
Stanislav Kholmanskikh | 5702765 | 2015-05-26 13:21:10 +0300 | [diff] [blame] | 207 | |
| 208 | /* |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 209 | * Attempt to remove the "TESTDIR" directory, using rmobj(). |
| 210 | */ |
Xiaoguang Wang | 33a4adf | 2014-07-30 12:09:40 +0800 | [diff] [blame] | 211 | if (rmobj(TESTDIR, &errmsg) == -1) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 212 | tst_resm(TWARN, "%s: rmobj(%s) failed: %s", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 213 | __func__, TESTDIR, errmsg); |
Xiaoguang Wang | 33a4adf | 2014-07-30 12:09:40 +0800 | [diff] [blame] | 214 | } |
Marios Makris | 60b1baa | 2012-08-09 20:36:46 +0300 | [diff] [blame] | 215 | } |