Skip to contentSkip to conversation

fchdir/fchdir01: Convert to new API

PATCH In review tangmeng 2 participants 4 messages Nov 8, 2021

lore
Share & tools
lore

tangmeng

author
#0 lore raw

PATCH 1/2 fchdir/fchdir01: Convert to new API

---
 testcases/kernel/syscalls/fchdir/fchdir01.c | 166 ++------------------
 1 file changed, 15 insertions(+), 151 deletions(-)
testcases/kernel/syscalls/fchdir/fchdir01.c +15 -151
index 526dfcaa2..4d07dd603 100644
@@ -1,171 +1,35 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
- *
  *   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 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
  */
 
 /*
- * NAME
- *	fchdir01.c
- *
  * DESCRIPTION
  *	fchdir01 - create a directory and cd into it.
- *
- * ALGORITHM
- *	create a new directory
- *	open the directory and get a file descriptor
- *	loop if that option was specified
- *	fchdir() into the directory
- *	check the return code
- *	  if failure, issue a FAIL message.
- *	otherwise,
- *	  if doing functionality testing, call check_functionality()
- *	  	if correct,
- *			issue a PASS message
- *		otherwise
- *			issue a FAIL message
- *	call cleanup
- *
- * USAGE:  <for command-line>
- *  fchdir01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
- *     where,  -c n : Run n copies concurrently.
- *             -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
- *	03/2001 - Written by Wayne Boyer
- *
- * RESTRICTIONS
- *	none
  */
 
-#include "test.h"
+#include "tst_test.h"
 
-#include <sys/stat.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <libgen.h>
-#include <string.h>
-
-void cleanup(void);
-void setup(void);
-
-char *TCID = "fchdir01";
-int TST_TOTAL = 1;
-
-int fd;
-char *temp_dir;
-const char *TEST_DIR = "alpha";
+static int fd;
+static const char *TEST_DIR = "alpha";
 
 #define MODES	S_IRWXU
 
-int main(int ac, char **av)
-{
-	int lc;
-	void check_functionality(void);
-	int r_val;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();		/* global setup */
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		/* get the name of the test dirctory */
-		if ((temp_dir = (getcwd(temp_dir, 0))) == NULL)
-			tst_brkm(TBROK, cleanup, "getcwd failed");
-
-		/*
-		 * create a new directory and open it
-		 */
-
-		if ((r_val = mkdir(TEST_DIR, MODES)) == -1)
-			tst_brkm(TBROK, cleanup, "mkdir failed");
-
-		if ((fd = open(TEST_DIR, O_RDONLY)) == -1)
-			tst_brkm(TBROK, cleanup, "open of directory failed");
-
-		TEST(fchdir(fd));
-
-		if (TEST_RETURN == -1) {
-			tst_brkm(TFAIL | TTERRNO, cleanup,
-				 "fchdir call failed");
-		} else {
-				check_functionality();
-		}
-
-		/*
-		 * clean up things in case we are looping
-		 */
-
-		/*
-		 * NOTE: in case of failure here, we need to use "tst_resm()"
-		 * and not "tst_brkm()".  This is because if we get to this
-		 * point, we have already set a PASS or FAIL for the test
-		 * and "tst_brkm()" won't report as we might expect.
-		 */
-
-		/* chdir back to our temporary work directory */
-		if ((r_val = chdir("..")) == -1)
-			tst_resm(TBROK | TERRNO, "chdir failed");
-
-		if ((r_val = rmdir(TEST_DIR)) == -1)
-			tst_resm(TBROK | TERRNO, "rmdir failed");
-
-		free(temp_dir);
-		temp_dir = NULL;
-	}
-
-	cleanup();
-	tst_exit();
-}
-
-void check_functionality(void)
+static void verify_fchdir(void)
 {
-	char *buf = NULL;
-	char *dir;
+	if ((fd = open(TEST_DIR, O_RDONLY)) == -1)
+		tst_brk(TBROK, "open of directory failed");
 
-	if ((buf = (getcwd(buf, 0))) == NULL) {
-		tst_brkm(TBROK, cleanup, "getcwd failed");
-	}
-
-	if ((dir = basename(buf)) == NULL)
-		tst_brkm(TBROK, cleanup, "basename failed");
-
-	if (strcmp(TEST_DIR, dir) == 0)
-		tst_resm(TPASS, "fchdir call succeeded");
-	else
-		tst_resm(TFAIL, "fchdir call failed");
+	TST_EXP_PASS(fchdir(fd));
 }
 
-void setup(void)
+static void setup(void)
 {
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
+	SAFE_MKDIR(TEST_DIR, MODES);
 }
 
-void cleanup(void)
-{
-	tst_rmdir();
-}
+static struct tst_test test = {
+	.test_all = verify_fchdir,
+	.setup = setup,
+	.needs_tmpdir = 1,
+};

--
2.20.1

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#1 lore raw

PATCH 2/2 fchdir/fchdir02: Convert to new API

---
 testcases/kernel/syscalls/fchdir/fchdir02.c | 103 ++------------------
 1 file changed, 8 insertions(+), 95 deletions(-)
testcases/kernel/syscalls/fchdir/fchdir02.c +8 -95
index cf11bf6b9..b0ba9ad58 100644
@@ -1,110 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
- *
  *   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 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
  */
 
 /*
- * NAME
- *	fchdir02.c
- *
  * DESCRIPTION
  *	fchdir02 - try to cd into a bad directory (bad fd).
- *
- * CALLS
- *	fchdir()
- *
- * ALGORITHM
- *	loop if that option was specified
- *	call fchdir() with an invalid file descriptor
- *	check the errno value
- *	  issue a PASS message if we get EBADF - errno 9
- *	otherwise, the tests fails
- *	  issue a FAIL message
- *	  break any remaining tests
- *	  call cleanup
- *
- * USAGE:  <for command-line>
- *  fchdir02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
- *     where,  -c n : Run n copies concurrently.
- *             -e   : Turn on errno logging.
- *	       -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
- *	03/2001 - Written by Wayne Boyer
- *
- * RESTRICTIONS
- *	none
  */
 
-#include "test.h"
+#include "tst_test.h"
 
-#include <errno.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-
-void cleanup(void);
-void setup(void);
-
-char *TCID = "fchdir02";
-int TST_TOTAL = 1;
-
-int main(int ac, char **av)
+static void verify_fchdir(void)
 {
 	const int bad_fd = -5;
-	int lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();		/* global setup */
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		TEST(fchdir(bad_fd));
-
-		if (TEST_RETURN != -1)
-			tst_brkm(TFAIL, cleanup, "call succeeded unexpectedly");
-
-		if (TEST_ERRNO == EBADF)
-			tst_resm(TPASS, "failed as expected with EBADF");
-		else
-			tst_brkm(TFAIL | TTERRNO, cleanup,
-				 "call failed unexpectedly");
-	}
 
-	cleanup();
-
-	tst_exit();
+	TST_EXP_FAIL(fchdir(bad_fd), EBADF);
 }
 
-void setup(void)
-{
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-}
-
-void cleanup(void)
-{
-	tst_rmdir();
-}
+static struct tst_test test = {
+	.test_all = verify_fchdir,
+	.needs_tmpdir = 1,
+};

--
2.20.1

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Re: [LTP] [PATCH 2/2] fchdir/fchdir02: Convert to new API

testcases/kernel/syscalls/fchdir/fchdir02.c line 22

Quoted 4 lines
testcases/kernel/syscalls/fchdir/fchdir02.c +3 -0
+static struct tst_test test = {
+       .test_all = verify_fchdir,
+       .needs_tmpdir = 1,

There is no need to use tmpdir since it is just a fake test for error checking.

I removed that line and pushed. Thanks~

Signature · 2 lines
Regards,
Li Wang
Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw
In reply to tangmeng

Review

Quoted 7 lines
(quoted diff) +2 -3
-void check_functionality(void)
+static void verify_fchdir(void)
 {
-       char *buf = NULL;
-       char *dir;
+       if ((fd = open(TEST_DIR, O_RDONLY)) == -1)

If making use of SAFE_OPEN, then we can save time for the error checking over and over again.

And better move this into setup() to make "-i x" happy.

Btw, plz remember to close the fd in cleanup().

I made some improvement work as below and pushed:

testcases/kernel/syscalls/fchdir/fchdir01.c +7 -3
@@ -17,19 +17,23 @@ static const char *TEST_DIR = "alpha";
 
 static void verify_fchdir(void)
 {
-       if ((fd = open(TEST_DIR, O_RDONLY)) == -1)
-               tst_brk(TBROK, "open of directory failed");
-
        TST_EXP_PASS(fchdir(fd));
 }
 
 static void setup(void)
 {
        SAFE_MKDIR(TEST_DIR, MODES);
+       fd = SAFE_OPEN(TEST_DIR, O_RDONLY);
+}
+
+static void cleanup(void)
+{
+       SAFE_CLOSE(fd);
 }
 
 static struct tst_test test = {
        .test_all = verify_fchdir,
        .setup = setup,
+       .cleanup = cleanup,
        .needs_tmpdir = 1,
 };
Signature · 2 lines
Regards,
Li Wang
Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp