Skip to contentSkip to conversation

lib/tst_tmpdir.c: use tst_resm() instead of tst_brkm() in tmpdir_cleanup()

PATCH In review Xiaoguang Wang 2 participants 3 messages Jul 30, 2014 – Aug 19, 2014 1 message not in the archive Library code

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH v2 1/2 lib/tst_tmpdir.c: use tst_resm() instead of tst_brkm() in tmpdir_cleanup()

In tst_tmpdir(), it will call tst_brkm() with tmpdir_cleanup() as cleanup func, but tmpdir_cleanup also() calls tst_brkm(), this will result in stack overflow finally, and also print too much noise about SIGABRT is caught(see abort() in tst_brkm_()), fix this.

Steps to reproduce this issue:
1. cd testcases/kernel/syscalls/access
2. mkdir testdir
3. su another_user
4. export TMPDIR=testdir
5. ./access05

---
 lib/tst_tmpdir.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/tst_tmpdir.c +1 -1
index 600d178..8cbd89c 100644
@@ -236,7 +236,7 @@ void tst_rmdir(void)
  */
 static void tmpdir_cleanup(void)
 {
-	tst_brkm(TWARN, NULL,
+	tst_resm(TWARN,
 		 "%s: no user cleanup function called before exiting",
 		 __func__);
 }
Signature · 13 lines
1.8.2.1


------------------------------------------------------------------------------
Infragistics Professional
Build stunning WinForms apps today!
Reboot your WinForms applications with our WinForms controls. 
Build a bridge from your legacy apps to the future.
http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list
#2 lore raw

PATCH v2 2/2 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.

---
 lib/tst_tmpdir.c | 65 +++++++++++++++++++-------------------------------------
 1 file changed, 22 insertions(+), 43 deletions(-)
lib/tst_tmpdir.c +22 -43
index 8cbd89c..ede8f89 100644
@@ -120,17 +120,30 @@ void tst_tmpdir(void)
 {
 	char template[PATH_MAX];
 	char *env_tmpdir;	/* temporary storage for TMPDIR env var */
-	char *errmsg;
+	char *errmsg, *c;
 
 	/*
 	 * Create a template for the temporary directory.  Use the
 	 * environment variable TMPDIR if it is available, otherwise
 	 * use our default TEMPDIR.
 	 */
-	if ((env_tmpdir = getenv("TMPDIR")))
+	env_tmpdir = getenv("TMPDIR");
+	if (env_tmpdir) {
+		c = strchr(env_tmpdir, '/');
+		/*
+		 * Now we force environment variable TMPDIR to be an absolute
+		 * pathname, which dose not make much sense, but it will
+		 * greatly simplify code in tst_rmdir().
+		 */
+		if (c != env_tmpdir) {
+			tst_brkm(TBROK, tmpdir_cleanup, "You must specify "
+				 "an absolute pathname for environment "
+				 "variable TMPDIR");
+		}
 		snprintf(template, PATH_MAX, "%s/%.3sXXXXXX", env_tmpdir, TCID);
-	else
+	} else {
 		snprintf(template, PATH_MAX, "%s/%.3sXXXXXX", TEMPDIR, TCID);
+	}
 
 	/* Make the temporary directory in one shot using mkdtemp. */
 	if (mkdtemp(template) == NULL)
@@ -159,23 +172,21 @@ void tst_tmpdir(void)
 	 * fails, also issue a TWARN message.
 	 */
 	if (chdir(TESTDIR) == -1) {
-		tst_brkm(TBROK | TERRNO, NULL, "%s: chdir(%s) failed",
-			 __func__, TESTDIR);
+		tst_resm(TERRNO, "%s: chdir(%s) failed", __func__, TESTDIR);
 
 		/* Try to remove the directory */
-		if (rmobj(TESTDIR, &errmsg) == -1)
+		if (rmobj(TESTDIR, &errmsg) == -1) {
 			tst_resm(TWARN, "%s: rmobj(%s) failed: %s",
 				 __func__, TESTDIR, errmsg);
+		}
 
-		tmpdir_cleanup();
+		tst_exit();
 	}
 }
 
 void tst_rmdir(void)
 {
-	char current_dir[PATH_MAX];
 	char *errmsg;
-	char *parent_dir;
 
 	/*
 	 * Check that TESTDIR is not NULL.
@@ -187,45 +198,13 @@ void tst_rmdir(void)
 		return;
 	}
 
-	if ((parent_dir = malloc(PATH_MAX)) == NULL) {
-		/* Make sure that we exit quickly and noisily. */
-		tst_brkm(TBROK | TERRNO, NULL,
-			 "%s: malloc(%d) failed", __func__, PATH_MAX);
-	}
-
-	/*
-	 * Get the directory name of TESTDIR.  If TESTDIR is a relative path,
-	 * get full path.
-	 */
-	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 {
-		strcpy(parent_dir, TESTDIR);
-	}
-
-	if ((parent_dir = dirname(parent_dir)) == NULL) {
-		tst_resm(TWARN | TERRNO, "%s: dirname failed", __func__);
-		return;
-	}
-
-	/*
-	 * Change directory to parent_dir (The dir above TESTDIR).
-	 */
-	if (chdir(parent_dir) != 0) {
-		tst_resm(TWARN | TERRNO,
-			 "%s: chdir(%s) failed\nAttempting to remove temp dir "
-			 "anyway", __func__, parent_dir);
-	}
-
 	/*
 	 * Attempt to remove the "TESTDIR" directory, using rmobj().
 	 */
-	if (rmobj(TESTDIR, &errmsg) == -1)
+	if (rmobj(TESTDIR, &errmsg) == -1) {
 		tst_resm(TWARN, "%s: rmobj(%s) failed: %s",
 			 __func__, TESTDIR, errmsg);
+	}
 }
 
 /*
Signature · 13 lines
1.8.2.1


------------------------------------------------------------------------------
Infragistics Professional
Build stunning WinForms apps today!
Reboot your WinForms applications with our WinForms controls. 
Build a bridge from your legacy apps to the future.
http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list