Skip to content

Commit 33a4adf

Browse files
wangxiaoguangmetan-ucw
authored andcommitted
lib/tst_tmpdir.c: force environment variable TMPDIR using an absolute pathname
In lib/tst_tmpdir.c, when TESTDIR is not an absolute patchname beginning with '/', The corresponding code logic would be below: if (TESTDIR[0] != '/') { if (getcwd(current_dir, PATH_MAX) == NULL) strncpy(parent_dir, TESTDIR, PATH_MAX); else sprintf(parent_dir, "%s/%s", current_dir, TESTDIR); } else { ... } The statement "sprintf(parent_dir, "%s/%s", current_dir, TESTDIR);" is wrong, because we can change to any directory in test case, which TESTDIR may no be child directory of current_dir, we should fix this. Now we decide to use absolute pathname for TMPDIR, this will greatly simplify the code in tst_rmdir(). If we still want to use relative path in TMPDIR, we may change the code in tst_rmdir() to chdir(test_start_work_dir) first, but this chdir() operation still will fail sometimes, this is because some components' search permission in test_start_work_dir may be denied( some test cases are run by root, but then may call setuid(), see access05.c). So I think we should obey the rule 'fail fast', just using absolute pathname instead. Though this may be not friendly to to usrs, users rarely export a different TMPDIR other than /tmp, so it's OK. Signed-off-by: Xiaoguang Wang <wangxg.fnst@cn.fujitsu.com>
1 parent 089c787 commit 33a4adf

File tree

1 file changed

+23
-44
lines changed

1 file changed

+23
-44
lines changed

lib/tst_tmpdir.c

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,31 @@ const char *tst_get_startwd(void)
119119
void tst_tmpdir(void)
120120
{
121121
char template[PATH_MAX];
122-
char *env_tmpdir; /* temporary storage for TMPDIR env var */
123-
char *errmsg;
122+
char *env_tmpdir;
123+
char *errmsg, *c;
124124

125125
/*
126126
* Create a template for the temporary directory. Use the
127127
* environment variable TMPDIR if it is available, otherwise
128128
* use our default TEMPDIR.
129129
*/
130-
if ((env_tmpdir = getenv("TMPDIR")))
130+
env_tmpdir = getenv("TMPDIR");
131+
if (env_tmpdir) {
132+
c = strchr(env_tmpdir, '/');
133+
/*
134+
* Now we force environment variable TMPDIR to be an absolute
135+
* pathname, which dose not make much sense, but it will
136+
* greatly simplify code in tst_rmdir().
137+
*/
138+
if (c != env_tmpdir) {
139+
tst_brkm(TBROK, tmpdir_cleanup, "You must specify "
140+
"an absolute pathname for environment "
141+
"variable TMPDIR");
142+
}
131143
snprintf(template, PATH_MAX, "%s/%.3sXXXXXX", env_tmpdir, TCID);
132-
else
144+
} else {
133145
snprintf(template, PATH_MAX, "%s/%.3sXXXXXX", TEMPDIR, TCID);
146+
}
134147

135148
/* Make the temporary directory in one shot using mkdtemp. */
136149
if (mkdtemp(template) == NULL)
@@ -159,23 +172,21 @@ void tst_tmpdir(void)
159172
* fails, also issue a TWARN message.
160173
*/
161174
if (chdir(TESTDIR) == -1) {
162-
tst_brkm(TBROK | TERRNO, NULL, "%s: chdir(%s) failed",
163-
__func__, TESTDIR);
175+
tst_resm(TERRNO, "%s: chdir(%s) failed", __func__, TESTDIR);
164176

165177
/* Try to remove the directory */
166-
if (rmobj(TESTDIR, &errmsg) == -1)
178+
if (rmobj(TESTDIR, &errmsg) == -1) {
167179
tst_resm(TWARN, "%s: rmobj(%s) failed: %s",
168180
__func__, TESTDIR, errmsg);
181+
}
169182

170-
tmpdir_cleanup();
183+
tst_exit();
171184
}
172185
}
173186

174187
void tst_rmdir(void)
175188
{
176-
char current_dir[PATH_MAX];
177189
char *errmsg;
178-
char *parent_dir;
179190

180191
/*
181192
* Check that TESTDIR is not NULL.
@@ -187,45 +198,13 @@ void tst_rmdir(void)
187198
return;
188199
}
189200

190-
if ((parent_dir = malloc(PATH_MAX)) == NULL) {
191-
/* Make sure that we exit quickly and noisily. */
192-
tst_brkm(TBROK | TERRNO, NULL,
193-
"%s: malloc(%d) failed", __func__, PATH_MAX);
194-
}
195-
196-
/*
197-
* Get the directory name of TESTDIR. If TESTDIR is a relative path,
198-
* get full path.
199-
*/
200-
if (TESTDIR[0] != '/') {
201-
if (getcwd(current_dir, PATH_MAX) == NULL)
202-
strncpy(parent_dir, TESTDIR, PATH_MAX);
203-
else
204-
sprintf(parent_dir, "%s/%s", current_dir, TESTDIR);
205-
} else {
206-
strcpy(parent_dir, TESTDIR);
207-
}
208-
209-
if ((parent_dir = dirname(parent_dir)) == NULL) {
210-
tst_resm(TWARN | TERRNO, "%s: dirname failed", __func__);
211-
return;
212-
}
213-
214-
/*
215-
* Change directory to parent_dir (The dir above TESTDIR).
216-
*/
217-
if (chdir(parent_dir) != 0) {
218-
tst_resm(TWARN | TERRNO,
219-
"%s: chdir(%s) failed\nAttempting to remove temp dir "
220-
"anyway", __func__, parent_dir);
221-
}
222-
223201
/*
224202
* Attempt to remove the "TESTDIR" directory, using rmobj().
225203
*/
226-
if (rmobj(TESTDIR, &errmsg) == -1)
204+
if (rmobj(TESTDIR, &errmsg) == -1) {
227205
tst_resm(TWARN, "%s: rmobj(%s) failed: %s",
228206
__func__, TESTDIR, errmsg);
207+
}
229208
}
230209

231210
/*

0 commit comments

Comments
 (0)