[4/5] syscalls/mkdir04: Cleanup && Convert to new API

Message ID 1521804892-5783-4-git-send-email-xuyang.jy@cn.fujitsu.com
State Accepted
Headers show
Series
  • [1/5] SAFE_MACROS: Add SAFE_SETREUID()/SAFE_SETREGID()
Related show

Commit Message

yang xu March 23, 2018, 11:34 a.m.
1) Take use of some safe macros
2  Remove temporary directory in loop

Signed-off-by: yang xu <xuyang.jy@cn.fujitsu.com>
---
 testcases/kernel/syscalls/mkdir/mkdir04.c | 275 +++++++++---------------------
 1 file changed, 78 insertions(+), 197 deletions(-)

Comments

Cyril Hrubis April 18, 2018, 1:43 p.m. | #1
Hi!
Same as mkdir02, further cleaned up and simplified the test and pushed,
thanks.

Patch

diff --git a/testcases/kernel/syscalls/mkdir/mkdir04.c b/testcases/kernel/syscalls/mkdir/mkdir04.c
index 3e0d199..562f784 100644
--- a/testcases/kernel/syscalls/mkdir/mkdir04.c
+++ b/testcases/kernel/syscalls/mkdir/mkdir04.c
@@ -1,241 +1,122 @@ 
-/*
+/* Copyright (c) International Business Machines  Corp., 2001
  *
- *   Copyright (c) International Business Machines  Corp., 2001
+ * This program is free software;  you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
- *   This program is free software;  you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License as published by
- *   the Free Software Foundation; either version 2 of the License, or
- *   (at your option) any later version.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY;  without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+ * the GNU General Public License for more details.
  *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
- *   the GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program;  if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 
-/*
- * NAME
- *	mkdir04
- *
- * DESCRIPTION
- *
- * ALGORITHM
- *	Setup:
- *		Setup signal handling.
- *		Pause for SIGUSR1 if option specified.
- *		Create temporary directory.
- *
- *	Test:
- *		Loop if the proper options are given.
- *              fork the first child
- *                      set to be ltpuser1
- *                      create a dirctory tstdir1 with 0700 permission
- *              fork the second child
- *                      set to ltpuser2
- *                      try to create a subdirectory tstdir2 under tstdir1
- *                      check the returnvalue, if succeeded (return=0)
- *			       Log the errno and Issue a FAIL message.
- *		        Otherwise,
- *			       Verify the errno
- *			       if equals to EACCES,
- *				       Issue Pass message.
- *			       Otherwise,
- *				       Issue Fail message.
- *	Cleanup:
- *		Print errno log and/or timing stats if options given
- *		Delete the temporary directory created.
- * USAGE
- *	mkdir04 [-c n] [-e] [-f] [-i n] [-I x] [-P x] [-t]
- *	where,  -c n : Run n copies concurrently.
- *		-e   : Turn on errno logging.
- *		-f   : Turn off functionality Testing.
- *		-i n : Execute test n times.
- *		-I x : Execute test for x seconds.
- *		-P x : Pause for x seconds between iterations.
- *		-t   : Turn on syscall timing.
- *
- * HISTORY
- *	07/2001 Ported by Wayne Boyer
- *
- * RESTRICTIONS
- *	None.
- *
+/*  DESCRIPTION
+ *   fork the first child
+ *	set to be ltpuser1
+ *	create a dirctory TESTDIR with 0700 permission
+ *   fork the second child
+ *	set to ltpuser2
+ *	try to create a subdirectory TESTDIR1 under tstdir1
+ *	check the returnvalue, if succeeded (return=0)
+ *		Log the errno and Issue a FAIL message.
+ *			Otherwise,
+ *		Verify the errno
+ *			if equals to EACCES,
+ *		Issue Pass message.
+ *			Otherwise,
+ *		Issue Fail message.
  */
 
 #include <errno.h>
-#include <string.h>
-#include <signal.h>
 #include <sys/stat.h>
 #include <sys/types.h>
-#include <fcntl.h>
 #include <pwd.h>
 #include <sys/wait.h>
 #include <unistd.h>
-
-#include "test.h"
-#include "safe_macros.h"
-
-void setup();
-void cleanup();
-int fail;
+#include <stdlib.h>
+#include "tst_test.h"
 
 #define PERMS		0700
+#define TESTDIR		"testdir"
+#define TESTDIR1    "testdir/testfile"
 
 static uid_t nobody_uid, bin_uid;
 
-char *TCID = "mkdir04";
-int TST_TOTAL = 1;
-int fail;
-
-char tstdir1[100];
-char tstdir2[100];
-
-int main(int ac, char **av)
+static void verify_mkdir(void)
 {
-	int lc;
-	int rval;
-	pid_t pid, pid1;
 	int status;
+	pid_t pid, pid1;
 
-	/*
-	 * parse standard options
-	 */
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	/*
-	 * perform global setup for test
-	 */
-	setup();
-
-	/*
-	 * check looping state if -i option given
-	 */
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
+	pid = SAFE_FORK();
+	if (pid == 0) {
+		SAFE_SETREUID(nobody_uid, nobody_uid);
 
-		/* Initialize the test directories name */
-		sprintf(tstdir1, "tstdir1.%d", getpid());
-		if ((pid = FORK_OR_VFORK()) < 0) {
-			tst_brkm(TBROK, cleanup, "fork #1 failed");
+		if (mkdir(TESTDIR, PERMS) == -1) {
+			tst_res(TFAIL | TERRNO, "mkdir(%s, %#o) Failed",
+				 TESTDIR, PERMS);
+			exit(1);
 		}
 
-		if (pid == 0) {	/* first child */
-			rval = setreuid(nobody_uid, nobody_uid);
-			if (rval < 0) {
-				tst_resm(TFAIL | TERRNO, "setreuid failed to "
-					 "to set the real uid to %d and "
-					 "effective uid to %d",
-					 nobody_uid, nobody_uid);
-				exit(1);
-			}
-			/* create the parent directory with 0700 permits */
-			if (mkdir(tstdir1, PERMS) == -1) {
-				tst_resm(TFAIL | TERRNO,
-					 "mkdir(%s, %#o) Failed",
-					 tstdir1, PERMS);
-				exit(1);
-			}
-			/* create tstdir1 succeeded */
-			exit(0);
-		}
-		wait(&status);
-		if (WEXITSTATUS(status) != 0) {
-			tst_brkm(TFAIL, cleanup,
-				 "Test to check mkdir EACCES failed"
-				 "in create parent directory");
-		}
+		exit(0);
+	}
 
-		sprintf(tstdir2, "%s/tst", tstdir1);
+	SAFE_WAITPID(pid, &status, 0);
+	if (WEXITSTATUS(status) != 0) {
+		tst_brk(TFAIL, "Test to check mkdir EACCES failed"
+			"in create parent directory");
+	}
 
-		if ((pid1 = FORK_OR_VFORK()) < 0) {
-			tst_brkm(TBROK, cleanup, "fork #2 failed");
-		}
+	pid1 = SAFE_FORK();
+	if (pid1 == 0) {
+		SAFE_SETREUID(bin_uid, bin_uid);
 
-		if (pid1 == 0) {	/* second child */
-			rval = setreuid(bin_uid, bin_uid);
-			if (rval < 0) {
-				tst_resm(TFAIL | TERRNO, "setreuid failed to "
-					 "to set the real uid to %d and "
-					 "effective uid to %d",
-					 bin_uid, bin_uid);
-				exit(1);
-			}
-			if (mkdir(tstdir2, PERMS) != -1) {
-				tst_resm(TFAIL, "mkdir(%s, %#o) unexpected "
-					 "succeeded", tstdir2, PERMS);
-				exit(1);
-			}
-			if (errno != EACCES) {
-				tst_resm(TFAIL, "Expected EACCES got %d",
-					 errno);
-				exit(1);
-			}
-			/* PASS */
-			exit(0);
+		if (mkdir(TESTDIR1, PERMS) != -1) {
+			tst_res(TFAIL, "mkdir(%s, %#o) unexpected "
+				"succeeded", TESTDIR1, PERMS);
+			exit(1);
 		}
-		waitpid(pid1, &status, 0);
-		if (WEXITSTATUS(status) == 0) {
-			tst_resm(TPASS, "Test to attempt to creat a directory "
-				 "in a directory having no permissions "
-				 "SUCCEEDED in setting errno to EACCES");
-		} else {
-			tst_resm(TFAIL, "Test to attempt to creat a directory "
-				 "in a directory having no permissions FAILED");
-			cleanup();
+		if (errno != EACCES) {
+			tst_res(TFAIL, "Expected EACCES got %d", errno);
+			exit(1);
 		}
+
+		exit(0);
 	}
 
-	/*
-	 * cleanup and exit
-	 */
-	cleanup();
-	tst_exit();
+	SAFE_WAITPID(pid1, &status, 0);
+	if (WEXITSTATUS(status) == 0) {
+		tst_res(TPASS, "Test to attempt to creat a directory "
+			"in a directory having no permissions "
+			"SUCCEEDED in setting errno to EACCES");
+	} else {
+		tst_brk(TFAIL, "Test to attempt to creat a directory "
+			"in a directory having no permissions FAILED");
+	}
 
+	  /* rmdir() TESTDIR for loop test*/
+	SAFE_RMDIR(TESTDIR);
 }
 
-/*
- * setup() - performs all ONE TIME setup for this test.
- */
 void setup(void)
 {
 	struct passwd *pw;
 
-	tst_require_root();
-
-	pw = SAFE_GETPWNAM(NULL, "nobody");
+	pw = SAFE_GETPWNAM("nobody");
 	nobody_uid = pw->pw_uid;
-	pw = SAFE_GETPWNAM(NULL, "bin");
+	pw = SAFE_GETPWNAM("bin");
 	bin_uid = pw->pw_uid;
 
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	/* Create a temporary directory and make it current. */
-	tst_tmpdir();
-
 	umask(0);
 }
 
-/*
- * cleanup() - performs all ONE TIME cleanup for this test at
- *             completion or premature exit.
- */
-void cleanup(void)
-{
-
-	/*
-	 * Remove the temporary directory.
-	 */
-	tst_rmdir();
-
-	/*
-	 * Exit with return code appropriate for results.
-	 */
-
-}
+static struct tst_test test = {
+	.test_all = verify_mkdir,
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+	.setup = setup,
+	.forks_child = 1,
+};