Skip to contentSkip to conversation

Convert syscalls/dup2/dup2{01...05} to the new API

PATCH Superseded QI Fuli 4 participants 14 messages Sep 2, 2021 – Sep 7, 2021

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

QI Fuli

author
#0 lore raw

cover Convert syscalls/dup2/dup2{01...05} to the new API

From: QI Fuli <[email protected]>

Convert syscalls syscalls/dup2/dup2{01...05} to the new API

QI Fuli (5):
syscalls/dup2/dup201: Convert dup201 to the new API syscalls/dup2/dup202: Convert dup202 to the new API syscalls/dup2/dup203: Convert dup203 to the new API syscalls/dup2/dup204: Convert dup204 to the new API syscalls/dup2/dup205: Convert dup205 to the new API

 testcases/kernel/syscalls/dup2/dup201.c | 161 +++----------
 testcases/kernel/syscalls/dup2/dup202.c | 175 ++++----------
 testcases/kernel/syscalls/dup2/dup203.c | 291 +++++++++---------------
 testcases/kernel/syscalls/dup2/dup204.c | 135 +++--------
 testcases/kernel/syscalls/dup2/dup205.c | 165 +++++---------
 5 files changed, 291 insertions(+), 636 deletions(-)
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH 1/5 syscalls/dup2/dup201: Convert dup201 to the new API

From: QI Fuli <[email protected]>

---
 testcases/kernel/syscalls/dup2/dup201.c | 161 +++++-------------------
 1 file changed, 34 insertions(+), 127 deletions(-)
testcases/kernel/syscalls/dup2/dup201.c +34 -127
index 4fa34466a..231c262bf 100644
@@ -1,87 +1,30 @@
-/*
- *
- *   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
- */
+// SPDX-License-Identifier: GPL-2.0-or-later
 
 /*
- * NAME
- *	dup201.c
+ * Copyright (c) International Business Machines  Corp., 2001
  *
- * DESCRIPTION
+ * DESCRIPTION:
  *	Negative tests for dup2() with bad fd (EBADF)
  *
- * ALGORITHM
- * 	Setup:
- *	a.	Setup signal handling.
- *	b.	Pause for SIGUSR1 if option specified.
- *
- * 	Test:
- *	a.	Loop if the proper options are given.
- *	b.	Loop through the test cases
- * 	c.	Execute dup2() system call
- *	d.	Check return code, if system call failed (return=-1), test
- *		for EBADF.
- *
- * 	Cleanup:
- * 	a.	Print errno log and/or timing stats if options given
- *
- * USAGE:  <for command-line>
- *  dup201 [-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
- *	07/2001 Ported by Wayne Boyer
- *	01/2002 Removed EMFILE test - Paul Larson
- *
- * RESTRICTIONS
- * 	NONE
+ * HISTORY:
+ * 	07/2001 Ported by Wayne Boyer
+ * 	01/2002 Removed EMFILE test - Paul Larson
  */
 
-#include <sys/types.h>
-#include <fcntl.h>
 #include <errno.h>
-#include <sys/time.h>
-#include <sys/resource.h>
-#include <unistd.h>
-#include <signal.h>
-#include "test.h"
-
-void setup(void);
-void cleanup(void);
-
-char *TCID = "dup201";
-int TST_TOTAL = 4;
+#include "tst_test.h"
 
 int maxfd;
 int goodfd = 5;
 int badfd = -1;
 int mystdout = 0;
 
-struct test_case_t {
+static struct tcase {
 	int *ofd;
 	int *nfd;
 	int error;
 	void (*setupfunc) ();
-} TC[] = {
+} tcases[] = {
 	/* First fd argument is less than 0 - EBADF */
 	{&badfd, &goodfd, EBADF, NULL},
 	    /* First fd argument is getdtablesize() - EBADF */
@@ -92,72 +35,36 @@ struct test_case_t {
 	{&mystdout, &maxfd, EBADF, NULL},
 };
 
-int main(int ac, char **av)
-{
-	int lc;
-	int i;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		/* loop through the test cases */
-
-		for (i = 0; i < TST_TOTAL; i++) {
-
-			/* call the test case setup routine if necessary */
-			if (TC[i].setupfunc != NULL)
-				(*TC[i].setupfunc) ();
-
-			TEST(dup2(*TC[i].ofd, *TC[i].nfd));
-
-			if (TEST_RETURN != -1) {
-				tst_resm(TFAIL, "call succeeded unexpectedly");
-				continue;
-			}
-
-			if (TEST_ERRNO == TC[i].error) {
-				tst_resm(TPASS,
-					 "failed as expected - errno = %d : %s",
-					 TEST_ERRNO, strerror(TEST_ERRNO));
-			} else {
-				tst_resm(TFAIL | TTERRNO,
-					 "failed unexpectedly; "
-					 "expected %d: %s", TC[i].error,
-					 strerror(TC[i].error));
-			}
-		}
-	}
-	cleanup();
-
-	tst_exit();
-}
-
-/*
- * setup() - performs all ONE TIME setup for this test.
- */
 void setup(void)
 {
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-
 	/* get some test specific values */
 	maxfd = getdtablesize();
 }
 
-/*
- * cleanup() - performs all ONE TIME cleanup for this test at
- *	       completion or premature exit.
- */
-void cleanup(void)
+static void run(unsigned int i)
 {
-	tst_rmdir();
+	struct tcase *tc = tcases + i;
+
+	if (tc->setupfunc)
+		tc->setupfunc();
+
+	TEST(dup2(*tc->ofd, *tc->nfd));
+
+	if (TST_RET == -1) {
+		if (TST_ERR == tc->error)
+			tst_res(TPASS, "failed as expected - errno = %d : %s",
+				TST_ERR, strerror(TST_ERR));
+		else
+			tst_res(TFAIL | TTERRNO, "failed unexpectedly; "
+				"expected %d: %s", tc->error,
+				strerror(tc->error));
+	} else
+		tst_res(TFAIL, "call succeeded unexpectedly");
 }
+
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = run,
+	.setup = setup,
+};
Signature · 1 line
2.31.1
#2 lore raw

PATCH 1/5 syscalls/dup2/dup201: Convert dup201 to the new API

Quoted 72 lines · QI Fuli

From: QI Fuli <[email protected]>

---
  testcases/kernel/syscalls/dup2/dup201.c | 161 +++++-------------------
  1 file changed, 34 insertions(+), 127 deletions(-)
testcases/kernel/syscalls/dup2/dup201.c +4 -51
index 4fa34466a..231c262bf 100644
@@ -1,87 +1,30 @@
-/*
- *
- *   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
- */
+// SPDX-License-Identifier: GPL-2.0-or-later
 
  /*
- * NAME
- *	dup201.c
+ * Copyright (c) International Business Machines  Corp., 2001
   *
- * DESCRIPTION
+ * DESCRIPTION:
   *	Negative tests for dup2() with bad fd (EBADF)
   *
- * ALGORITHM
- * 	Setup:
- *	a.	Setup signal handling.
- *	b.	Pause for SIGUSR1 if option specified.
- *
- * 	Test:
- *	a.	Loop if the proper options are given.
- *	b.	Loop through the test cases
- * 	c.	Execute dup2() system call
- *	d.	Check return code, if system call failed (return=-1), test
- *		for EBADF.
- *
- * 	Cleanup:
- * 	a.	Print errno log and/or timing stats if options given
- *
- * USAGE:  <for command-line>
- *  dup201 [-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
- *	07/2001 Ported by Wayne Boyer
- *	01/2002 Removed EMFILE test - Paul Larson
- *
- * RESTRICTIONS
- * 	NONE
+ * HISTORY:

testcases/kernel/syscalls/dup2/dup201.c line 64

Quoted 10 lines
testcases/kernel/syscalls/dup2/dup201.c +2 -5
+ * 	07/2001 Ported by Wayne Boyer
+ * 	01/2002 Removed EMFILE test - Paul Larson
   */
 
-#include <sys/types.h>
-#include <fcntl.h>
  #include <errno.h>
-#include <sys/time.h>
-#include <sys/resource.h>
-#include <unistd.h>

Hi Qi,

Please keep <unistd.h> for dup2() and getdtablesize().

Quoted 4 lines
testcases/kernel/syscalls/dup2/dup201.c +0 -4
-#include <signal.h>
-#include "test.h"
-
-void setup(void);

testcases/kernel/syscalls/dup2/dup201.c line 20

Quoted 10 lines
testcases/kernel/syscalls/dup2/dup201.c +1 -4
-void cleanup(void);
-
-char *TCID = "dup201";
-int TST_TOTAL = 4;
+#include "tst_test.h"
 
  int maxfd;
  int goodfd = 5;
  int badfd = -1;
  int mystdout = 0;

Please add static prefix.

Quoted 56 lines
testcases/kernel/syscalls/dup2/dup201.c +2 -42
-struct test_case_t {
+static struct tcase {
  	int *ofd;
  	int *nfd;
  	int error;
  	void (*setupfunc) ();
-} TC[] = {
+} tcases[] = {
  	/* First fd argument is less than 0 - EBADF */
  	{&badfd, &goodfd, EBADF, NULL},
  	    /* First fd argument is getdtablesize() - EBADF */
@@ -92,72 +35,36 @@ struct test_case_t {
  	{&mystdout, &maxfd, EBADF, NULL},
  };
 
-int main(int ac, char **av)
-{
-	int lc;
-	int i;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		/* loop through the test cases */
-
-		for (i = 0; i < TST_TOTAL; i++) {
-
-			/* call the test case setup routine if necessary */
-			if (TC[i].setupfunc != NULL)
-				(*TC[i].setupfunc) ();
-
-			TEST(dup2(*TC[i].ofd, *TC[i].nfd));
-
-			if (TEST_RETURN != -1) {
-				tst_resm(TFAIL, "call succeeded unexpectedly");
-				continue;
-			}
-
-			if (TEST_ERRNO == TC[i].error) {
-				tst_resm(TPASS,
-					 "failed as expected - errno = %d : %s",
-					 TEST_ERRNO, strerror(TEST_ERRNO));
-			} else {
-				tst_resm(TFAIL | TTERRNO,
-					 "failed unexpectedly; "
-					 "expected %d: %s", TC[i].error,
-					 strerror(TC[i].error));
-			}
-		}
-	}

testcases/kernel/syscalls/dup2/dup201.c struct test_case_t { · line 39

Quoted 10 lines
testcases/kernel/syscalls/dup2/dup201.c +0 -8
-	cleanup();
-
-	tst_exit();
-}
-
-/*
- * setup() - performs all ONE TIME setup for this test.
- */
  void setup(void)
  {

Please add static prefix.

Quoted 13 lines
testcases/kernel/syscalls/dup2/dup201.c +0 -9
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-
  	/* get some test specific values */
  	maxfd = getdtablesize();
  }
 
-/*
- * cleanup() - performs all ONE TIME cleanup for this test at

testcases/kernel/syscalls/dup2/dup201.c struct test_case_t { · line 49

Quoted 10 lines
testcases/kernel/syscalls/dup2/dup201.c +5 -4
- *	       completion or premature exit.
- */
-void cleanup(void)
+static void run(unsigned int i)
  {
-	tst_rmdir();
+	struct tcase *tc = tcases + i;
+
+	if (tc->setupfunc)
+		tc->setupfunc();

tc->setupfunc always NULL so you can remove it directly.

testcases/kernel/syscalls/dup2/dup201.c struct test_case_t { · line 62

Quoted 13 lines
testcases/kernel/syscalls/dup2/dup201.c +13 -0
+
+	TEST(dup2(*tc->ofd, *tc->nfd));
+
+	if (TST_RET == -1) {
+		if (TST_ERR == tc->error)
+			tst_res(TPASS, "failed as expected - errno = %d : %s",
+				TST_ERR, strerror(TST_ERR));
+		else
+			tst_res(TFAIL | TTERRNO, "failed unexpectedly; "
+				"expected %d: %s", tc->error,
+				strerror(tc->error));
+	} else
+		tst_res(TFAIL, "call succeeded unexpectedly");

It is simpler to call TST_EXP_FAIL2() here.

testcases/kernel/syscalls/dup2/dup201.c struct test_case_t { · line 66

Quoted 4 lines
testcases/kernel/syscalls/dup2/dup201.c +3 -0
  }
+
+static struct tst_test test = {
+	.needs_tmpdir = 1,

It seems that we don't need any temp file.

Best Regards,

Xiao Yang

Quoted 4 lines
testcases/kernel/syscalls/dup2/dup201.c +4 -0
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = run,
+	.setup = setup,
+};
#3 lore raw

PATCH 1/5 syscalls/dup2/dup201: Convert dup201 to the new API

Quoted 225 lines

Subject: Re: [LTP] [PATCH 1/5] syscalls/dup2/dup201: Convert dup201 to the new API

Review

Quoted 8 lines · QI Fuli

From: QI Fuli <[email protected]>

---
  testcases/kernel/syscalls/dup2/dup201.c | 161 +++++-------------------
  1 file changed, 34 insertions(+), 127 deletions(-)
(quoted diff) +0 -0

b/testcases/kernel/syscalls/dup2/dup201.c

Quoted 11 lines

index 4fa34466a..231c262bf 100644

testcases/kernel/syscalls/dup2/dup201.c +0 -7
@@ -1,87 +1,30 @@
-/*
- *
- *   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

testcases/kernel/syscalls/dup2/dup201.c line 17

Quoted 10 lines
testcases/kernel/syscalls/dup2/dup201.c +0 -10
- *   (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

Quoted 43 lines
testcases/kernel/syscalls/dup2/dup201.c +4 -34
- */
+// SPDX-License-Identifier: GPL-2.0-or-later
 
  /*
- * NAME
- *	dup201.c
+ * Copyright (c) International Business Machines  Corp., 2001
   *
- * DESCRIPTION
+ * DESCRIPTION:
   *	Negative tests for dup2() with bad fd (EBADF)
   *
- * ALGORITHM
- * 	Setup:
- *	a.	Setup signal handling.
- *	b.	Pause for SIGUSR1 if option specified.
- *
- * 	Test:
- *	a.	Loop if the proper options are given.
- *	b.	Loop through the test cases
- * 	c.	Execute dup2() system call
- *	d.	Check return code, if system call failed (return=-1), test
- *		for EBADF.
- *
- * 	Cleanup:
- * 	a.	Print errno log and/or timing stats if options given
- *
- * USAGE:  <for command-line>
- *  dup201 [-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
- *	07/2001 Ported by Wayne Boyer
- *	01/2002 Removed EMFILE test - Paul Larson
- *
- * RESTRICTIONS
- * 	NONE
+ * HISTORY:

testcases/kernel/syscalls/dup2/dup201.c line 64

Quoted 10 lines
testcases/kernel/syscalls/dup2/dup201.c +2 -5
+ * 	07/2001 Ported by Wayne Boyer
+ * 	01/2002 Removed EMFILE test - Paul Larson
   */
 
-#include <sys/types.h>
-#include <fcntl.h>
  #include <errno.h>
-#include <sys/time.h>
-#include <sys/resource.h>
-#include <unistd.h>

Hi Qi,

Please keep <unistd.h> for dup2() and getdtablesize().

Quoted 4 lines
testcases/kernel/syscalls/dup2/dup201.c +0 -4
-#include <signal.h>
-#include "test.h"
-
-void setup(void);

testcases/kernel/syscalls/dup2/dup201.c line 20

Quoted 10 lines
testcases/kernel/syscalls/dup2/dup201.c +1 -4
-void cleanup(void);
-
-char *TCID = "dup201";
-int TST_TOTAL = 4;
+#include "tst_test.h"
 
  int maxfd;
  int goodfd = 5;
  int badfd = -1;
  int mystdout = 0;

Please add static prefix.

Quoted 31 lines
testcases/kernel/syscalls/dup2/dup201.c +2 -17
-struct test_case_t {
+static struct tcase {
  	int *ofd;
  	int *nfd;
  	int error;
  	void (*setupfunc) ();
-} TC[] = {
+} tcases[] = {
  	/* First fd argument is less than 0 - EBADF */
  	{&badfd, &goodfd, EBADF, NULL},
  	    /* First fd argument is getdtablesize() - EBADF */
@@ -92,72 +35,36 @@ struct test_case_t {
  	{&mystdout, &maxfd, EBADF, NULL},
  };
 
-int main(int ac, char **av)
-{
-	int lc;
-	int i;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		/* loop through the test cases */
-

testcases/kernel/syscalls/dup2/dup201.c struct test_case_t { · line 119

Quoted 10 lines
testcases/kernel/syscalls/dup2/dup201.c +0 -10
-		for (i = 0; i < TST_TOTAL; i++) {
-
-			/* call the test case setup routine if necessary */
-			if (TC[i].setupfunc != NULL)
-				(*TC[i].setupfunc) ();
-
-			TEST(dup2(*TC[i].ofd, *TC[i].nfd));
-
-			if (TEST_RETURN != -1) {
-				tst_resm(TFAIL, "call succeeded

unexpectedly");

testcases/kernel/syscalls/dup2/dup201.c struct test_case_t {

Quoted 6 lines
testcases/kernel/syscalls/dup2/dup201.c +0 -6
-				continue;
-			}
-
-			if (TEST_ERRNO == TC[i].error) {
-				tst_resm(TPASS,
-					 "failed as expected - errno

= %d : %s",

testcases/kernel/syscalls/dup2/dup201.c struct test_case_t { · line 126

Quoted 1 line
testcases/kernel/syscalls/dup2/dup201.c +0 -1
-					 TEST_ERRNO,

strerror(TEST_ERRNO));

Quoted 8 lines
testcases/kernel/syscalls/dup2/dup201.c +0 -8
-			} else {
-				tst_resm(TFAIL | TTERRNO,
-					 "failed unexpectedly; "
-					 "expected %d: %s", TC[i].error,
-					 strerror(TC[i].error));
-			}
-		}
-	}

testcases/kernel/syscalls/dup2/dup201.c struct test_case_t { · line 39

Quoted 10 lines
testcases/kernel/syscalls/dup2/dup201.c +0 -8
-	cleanup();
-
-	tst_exit();
-}
-
-/*
- * setup() - performs all ONE TIME setup for this test.
- */
  void setup(void)
  {

Please add static prefix.

Quoted 13 lines
testcases/kernel/syscalls/dup2/dup201.c +0 -9
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-
  	/* get some test specific values */
  	maxfd = getdtablesize();
  }
 
-/*
- * cleanup() - performs all ONE TIME cleanup for this test at

testcases/kernel/syscalls/dup2/dup201.c struct test_case_t { · line 49

Quoted 10 lines
testcases/kernel/syscalls/dup2/dup201.c +5 -4
- *	       completion or premature exit.
- */
-void cleanup(void)
+static void run(unsigned int i)
  {
-	tst_rmdir();
+	struct tcase *tc = tcases + i;
+
+	if (tc->setupfunc)
+		tc->setupfunc();

tc->setupfunc always NULL so you can remove it directly.

testcases/kernel/syscalls/dup2/dup201.c struct test_case_t { · line 62

Quoted 13 lines
testcases/kernel/syscalls/dup2/dup201.c +13 -0
+
+	TEST(dup2(*tc->ofd, *tc->nfd));
+
+	if (TST_RET == -1) {
+		if (TST_ERR == tc->error)
+			tst_res(TPASS, "failed as expected - errno = %d : %s",
+				TST_ERR, strerror(TST_ERR));
+		else
+			tst_res(TFAIL | TTERRNO, "failed unexpectedly; "
+				"expected %d: %s", tc->error,
+				strerror(tc->error));
+	} else
+		tst_res(TFAIL, "call succeeded unexpectedly");

It is simpler to call TST_EXP_FAIL2() here.

testcases/kernel/syscalls/dup2/dup201.c struct test_case_t { · line 66

Quoted 4 lines
testcases/kernel/syscalls/dup2/dup201.c +3 -0
  }
+
+static struct tst_test test = {
+	.needs_tmpdir = 1,

It seems that we don't need any temp file.

Quoted 8 lines

Best Regards,

Xiao Yang

testcases/kernel/syscalls/dup2/dup201.c struct test_case_t { · line 70

Quoted 4 lines
testcases/kernel/syscalls/dup2/dup201.c +4 -0
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = run,
+	.setup = setup,
+};

Thank you for the comments.
I will make a v2 patch.

Qi

#4 lore raw

PATCH 1/5 syscalls/dup2/dup201: Convert dup201 to the new API

In reply to QI Fuli

Hi Qi

Quoted 96 lines

From: QI Fuli<[email protected]>

---
  testcases/kernel/syscalls/dup2/dup201.c | 161 +++++-------------------
  1 file changed, 34 insertions(+), 127 deletions(-)
testcases/kernel/syscalls/dup2/dup201.c +7 -64
index 4fa34466a..231c262bf 100644
@@ -1,87 +1,30 @@
-/*
- *
- *   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
- */
+// SPDX-License-Identifier: GPL-2.0-or-later
 
  /*
- * NAME
- *	dup201.c
+ * Copyright (c) International Business Machines  Corp., 2001
   *
- * DESCRIPTION
+ * DESCRIPTION:
   *	Negative tests for dup2() with bad fd (EBADF)
   *
- * ALGORITHM
- * 	Setup:
- *	a.	Setup signal handling.
- *	b.	Pause for SIGUSR1 if option specified.
- *
- * 	Test:
- *	a.	Loop if the proper options are given.
- *	b.	Loop through the test cases
- * 	c.	Execute dup2() system call
- *	d.	Check return code, if system call failed (return=-1), test
- *		for EBADF.
- *
- * 	Cleanup:
- * 	a.	Print errno log and/or timing stats if options given
- *
- * USAGE:<for command-line>
- *  dup201 [-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
- *	07/2001 Ported by Wayne Boyer
- *	01/2002 Removed EMFILE test - Paul Larson
- *
- * RESTRICTIONS
- * 	NONE
+ * HISTORY:
+ * 	07/2001 Ported by Wayne Boyer
+ * 	01/2002 Removed EMFILE test - Paul Larson
   */
 
-#include<sys/types.h>
-#include<fcntl.h>
  #include<errno.h>
-#include<sys/time.h>
-#include<sys/resource.h>
-#include<unistd.h>
-#include<signal.h>
-#include "test.h"
-
-void setup(void);
-void cleanup(void);
-
-char *TCID = "dup201";
-int TST_TOTAL = 4;
+#include "tst_test.h"
 
  int maxfd;
  int goodfd = 5;
  int badfd = -1;
  int mystdout = 0;

testcases/kernel/syscalls/dup2/dup201.c line 28

Quoted 10 lines
testcases/kernel/syscalls/dup2/dup201.c +2 -2
 
-struct test_case_t {
+static struct tcase {
  	int *ofd;
  	int *nfd;
  	int error;
  	void (*setupfunc) ();
-} TC[] = {
+} tcases[] = {
  	/* First fd argument is less than 0 - EBADF */

I prefer to put these messages in the top commet, so we can see more useful information by using doc parse. Maybe as below:

/*\
* [Description]
*
* Test for EBADF error.
*

  * EBADF -  First fd argument is less than 0.
  * EBADF -  First fd argument is getdtablesize().
  * EBADF -  Second fd argument is less than 0.
  * EBADF -  Second fd argument is getdtablesize().

*/

Quoted 74 lines
testcases/kernel/syscalls/dup2/dup201.c +1 -60
  	{&badfd,&goodfd, EBADF, NULL},
  	    /* First fd argument is getdtablesize() - EBADF */
@@ -92,72 +35,36 @@ struct test_case_t {
  	{&mystdout,&maxfd, EBADF, NULL},
  };
 
-int main(int ac, char **av)
-{
-	int lc;
-	int i;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		/* loop through the test cases */
-
-		for (i = 0; i<  TST_TOTAL; i++) {
-
-			/* call the test case setup routine if necessary */
-			if (TC[i].setupfunc != NULL)
-				(*TC[i].setupfunc) ();
-
-			TEST(dup2(*TC[i].ofd, *TC[i].nfd));
-
-			if (TEST_RETURN != -1) {
-				tst_resm(TFAIL, "call succeeded unexpectedly");
-				continue;
-			}
-
-			if (TEST_ERRNO == TC[i].error) {
-				tst_resm(TPASS,
-					 "failed as expected - errno = %d : %s",
-					 TEST_ERRNO, strerror(TEST_ERRNO));
-			} else {
-				tst_resm(TFAIL | TTERRNO,
-					 "failed unexpectedly; "
-					 "expected %d: %s", TC[i].error,
-					 strerror(TC[i].error));
-			}
-		}
-	}
-	cleanup();
-
-	tst_exit();
-}
-
-/*
- * setup() - performs all ONE TIME setup for this test.
- */
  void setup(void)
  {
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-
  	/* get some test specific values */
  	maxfd = getdtablesize();
  }
 
-/*
- * cleanup() - performs all ONE TIME cleanup for this test at
- *	       completion or premature exit.
- */
-void cleanup(void)
+static void run(unsigned int i)
  {

testcases/kernel/syscalls/dup2/dup201.c struct test_case_t { · line 54

Quoted 10 lines
testcases/kernel/syscalls/dup2/dup201.c +9 -1
-	tst_rmdir();
+	struct tcase *tc = tcases + i;
+
+	if (tc->setupfunc)
+		tc->setupfunc();
+
+	TEST(dup2(*tc->ofd, *tc->nfd));
+
+	if (TST_RET == -1) {
+		if (TST_ERR == tc->error)

If dup201 only tests EBADF error, I think we can use EBADF directly instead of tc->error.

Best Regards
Yang Xu

Quoted 16 lines
testcases/kernel/syscalls/dup2/dup201.c +15 -0
+			tst_res(TPASS, "failed as expected - errno = %d : %s",
+				TST_ERR, strerror(TST_ERR));
+		else
+			tst_res(TFAIL | TTERRNO, "failed unexpectedly; "
+				"expected %d: %s", tc->error,
+				strerror(tc->error));
+	} else
+		tst_res(TFAIL, "call succeeded unexpectedly");
  }
+
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = run,
+	.setup = setup,
+};
author
#5 lore raw

PATCH 2/5 syscalls/dup2/dup202: Convert dup202 to the new API

In reply to QI Fuli

From: QI Fuli <[email protected]>

---
 testcases/kernel/syscalls/dup2/dup202.c | 175 +++++++-----------------
 1 file changed, 48 insertions(+), 127 deletions(-)
testcases/kernel/syscalls/dup2/dup202.c +48 -127
index c87769fa9..16f8b2add 100644
@@ -1,65 +1,22 @@
-/*
- *
- *   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
- */
+// SPDX-License-Identifier: GPL-2.0-or-later
 
 /*
- * NAME
- *	dup202.c
+ * Copyright (c) International Business Machines  Corp., 2001
  *
- * DESCRIPTION
+ * DESCRIPTION:
  *	Is the access mode the same for both file descriptors?
  *		0: read only ?	"0444"
  *		1: write only ? "0222"
  *		2: read/write ? "0666"
  *
- * ALGORITHM
- *	Creat a file with each access mode; dup each file descriptor;
- *	stat each file descriptor and compare modes of each pair
- *
- * USAGE:  <for command-line>
- *  dup202 [-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
+ * HISTORY:
  *	07/2001 Ported by Wayne Boyer
- *
- * RESTRICTIONS
- *	None
  */
 
-#include <sys/types.h>
-#include <sys/stat.h>
 #include <errno.h>
-#include <fcntl.h>
 #include <stdio.h>
-#include "test.h"
-#include "safe_macros.h"
-
-char *TCID = "dup202";
-int TST_TOTAL = 3;
-
-void setup(void);
-void cleanup(void);
+#include "tst_test.h"
+#include "tst_safe_macros.h"
 
 char testfile[40];
 int fail;
@@ -68,100 +25,64 @@ int newfd;
 /* set these to a known index into our local file descriptor table */
 int duprdo = 10, dupwro = 20, duprdwr = 30;
 
-struct test_case_t {
+static struct tcase {
 	int *nfd;
 	mode_t mode;
-} TC[] = {
+} tcases[]= {
 	/* The first test creat(es) a file with mode 0444 */
-	{
-	&duprdo, (S_IRUSR | S_IRGRP | S_IROTH)},
+	{&duprdo, (S_IRUSR | S_IRGRP | S_IROTH)},
 	    /* The second test creat(es) a file with mode 0222 */
-	{
-	&dupwro, (S_IWUSR | S_IWGRP | S_IWOTH)},
+	{&dupwro, (S_IWUSR | S_IWGRP | S_IWOTH)},
 	    /* The third test creat(es) a file with mode 0666 */
-	{
-	&duprdwr,
-		    (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH)}
+	{&duprdwr, (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH)},
 };
 
-int main(int ac, char **av)
+void setup(void)
 {
-	int lc;
-	int i, ofd;
-	struct stat oldbuf, newbuf;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		/* loop through the test cases */
-		for (i = 0; i < TST_TOTAL; i++) {
-
-			if ((ofd = creat(testfile, TC[i].mode)) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "creat failed");
-
-			TEST(dup2(ofd, *TC[i].nfd));
-
-			if (TEST_RETURN == -1) {
-				tst_resm(TFAIL | TTERRNO,
-					 "call failed unexpectedly");
-				continue;
-			}
+	(void)umask(0);
+	sprintf(testfile, "dup202.%d", getpid());
+}
 
-			/* stat the original file */
-			SAFE_FSTAT(cleanup, ofd, &oldbuf);
+static void run(unsigned int i)
+{
+	int ofd;
+	struct stat oldbuf, newbuf;
+	struct tcase *tc = tcases + i;
 
-			/* stat the duped file */
-			SAFE_FSTAT(cleanup, *TC[i].nfd, &newbuf);
+	ofd = creat(testfile, tc->mode);
+	if (ofd == -1)
+		tst_brk(TBROK | TERRNO, "creat failed");
 
-			if (oldbuf.st_mode != newbuf.st_mode)
-				tst_resm(TFAIL, "original and dup "
-					 "modes do not match");
-			else
-				tst_resm(TPASS, "fstat shows new and "
-					 "old modes are the same");
+	TEST(dup2(ofd, *tc->nfd));
 
-			/* remove the file so that we can use it again */
-			if (close(*TC[i].nfd) == -1)
-				perror("close failed");
-			if (close(ofd) == -1)
-				perror("close failed");
-			if (unlink(testfile) == -1)
-				perror("unlink failed");
-		}
+	if (TST_RET == -1) {
+		tst_res(TFAIL | TTERRNO, "call failed unexpectedly");
+		return;
 	}
 
-	cleanup();
-	tst_exit();
-}
-
-/*
- * setup() - performs all ONE TIME setup for this test.
- */
-void setup(void)
-{
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
+	/* stat the original file */
+	SAFE_FSTAT(ofd, &oldbuf);
 
-	TEST_PAUSE;
+	/* stat the duped file */
+	SAFE_FSTAT(*tc->nfd, &newbuf);
 
-	tst_tmpdir();
+	if (oldbuf.st_mode != newbuf.st_mode)
+		tst_res(TFAIL, "original and dup modes do not match");
+	else
+		tst_res(TPASS, "fstat shows new and old modes are the same");
 
-	(void)umask(0);
-
-	sprintf(testfile, "dup202.%d", getpid());
+	/* remove the file so that we can use it again */
+	if (close(*tc->nfd) == -1)
+		perror("close failed");
+	if (close(ofd) == -1)
+		perror("close failed");
+	if (unlink(testfile) == -1)
+		perror("unlink failed");
 }
 
-/*
- * cleanup() - performs all ONE TIME cleanup for this test at
- *	       completion or premature exit.
- */
-void cleanup(void)
-{
-	tst_rmdir();
-}
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = run,
+	.setup = setup,
+};
Signature · 1 line
2.31.1
#6 lore raw

PATCH 2/5 syscalls/dup2/dup202: Convert dup202 to the new API

Quoted 29 lines · QI Fuli

From: QI Fuli <[email protected]>

---
  testcases/kernel/syscalls/dup2/dup202.c | 175 +++++++-----------------
  1 file changed, 48 insertions(+), 127 deletions(-)
testcases/kernel/syscalls/dup2/dup202.c +0 -17
index c87769fa9..16f8b2add 100644
@@ -1,65 +1,22 @@
-/*
- *
- *   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

testcases/kernel/syscalls/dup2/dup202.c line 6

Quoted 10 lines
testcases/kernel/syscalls/dup2/dup202.c +3 -4
- */
+// SPDX-License-Identifier: GPL-2.0-or-later
 
  /*
- * NAME
- *	dup202.c
+ * Copyright (c) International Business Machines  Corp., 2001
   *
- * DESCRIPTION
+ * DESCRIPTION:

Hi Qi,

Please use the new format of description so that it can be caught by doc parse:

---------------------------------------------

/*\

[Description]

---------------------------------------------

Quoted 11 lines
testcases/kernel/syscalls/dup2/dup202.c +0 -6
   *	Is the access mode the same for both file descriptors?
   *		0: read only ?	"0444"
   *		1: write only ? "0222"
   *		2: read/write ? "0666"
   *
- * ALGORITHM
- *	Creat a file with each access mode; dup each file descriptor;
- *	stat each file descriptor and compare modes of each pair
- *
- * USAGE:  <for command-line>
- *  dup202 [-c n] [-f] [-i n] [-I x] [-P x] [-t]

testcases/kernel/syscalls/dup2/dup202.c line 13

Quoted 10 lines
testcases/kernel/syscalls/dup2/dup202.c +1 -8
- *     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
+ * HISTORY:
   *	07/2001 Ported by Wayne Boyer

Remove it.

Quoted 84 lines
testcases/kernel/syscalls/dup2/dup202.c +12 -52
- *
- * RESTRICTIONS
- *	None
   */
 
-#include <sys/types.h>
-#include <sys/stat.h>
  #include <errno.h>
-#include <fcntl.h>
  #include <stdio.h>
-#include "test.h"
-#include "safe_macros.h"
-
-char *TCID = "dup202";
-int TST_TOTAL = 3;
-
-void setup(void);
-void cleanup(void);
+#include "tst_test.h"
+#include "tst_safe_macros.h"
 
  char testfile[40];
  int fail;
@@ -68,100 +25,64 @@ int newfd;
  /* set these to a known index into our local file descriptor table */
  int duprdo = 10, dupwro = 20, duprdwr = 30;
 
-struct test_case_t {
+static struct tcase {
  	int *nfd;
  	mode_t mode;
-} TC[] = {
+} tcases[]= {
  	/* The first test creat(es) a file with mode 0444 */
-	{
-	&duprdo, (S_IRUSR | S_IRGRP | S_IROTH)},
+	{&duprdo, (S_IRUSR | S_IRGRP | S_IROTH)},
  	    /* The second test creat(es) a file with mode 0222 */
-	{
-	&dupwro, (S_IWUSR | S_IWGRP | S_IWOTH)},
+	{&dupwro, (S_IWUSR | S_IWGRP | S_IWOTH)},
  	    /* The third test creat(es) a file with mode 0666 */
-	{
-	&duprdwr,
-		    (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH)}
+	{&duprdwr, (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH)},
  };
 
-int main(int ac, char **av)
+void setup(void)
  {
-	int lc;
-	int i, ofd;
-	struct stat oldbuf, newbuf;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		/* loop through the test cases */
-		for (i = 0; i < TST_TOTAL; i++) {
-
-			if ((ofd = creat(testfile, TC[i].mode)) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,
-					 "creat failed");
-
-			TEST(dup2(ofd, *TC[i].nfd));
-
-			if (TEST_RETURN == -1) {
-				tst_resm(TFAIL | TTERRNO,
-					 "call failed unexpectedly");
-				continue;
-			}
+	(void)umask(0);
+	sprintf(testfile, "dup202.%d", getpid());
+}
 
-			/* stat the original file */
-			SAFE_FSTAT(cleanup, ofd, &oldbuf);
+static void run(unsigned int i)

testcases/kernel/syscalls/dup2/dup202.c int newfd; · line 54

Quoted 10 lines
testcases/kernel/syscalls/dup2/dup202.c +7 -2
+{
+	int ofd;
+	struct stat oldbuf, newbuf;
+	struct tcase *tc = tcases + i;
 
-			/* stat the duped file */
-			SAFE_FSTAT(cleanup, *TC[i].nfd, &newbuf);
+	ofd = creat(testfile, tc->mode);
+	if (ofd == -1)
+		tst_brk(TBROK | TERRNO, "creat failed");

Call SAFE_OPEN() directly.

Quoted 42 lines
testcases/kernel/syscalls/dup2/dup202.c +9 -27
-			if (oldbuf.st_mode != newbuf.st_mode)
-				tst_resm(TFAIL, "original and dup "
-					 "modes do not match");
-			else
-				tst_resm(TPASS, "fstat shows new and "
-					 "old modes are the same");
+	TEST(dup2(ofd, *tc->nfd));
 
-			/* remove the file so that we can use it again */
-			if (close(*TC[i].nfd) == -1)
-				perror("close failed");
-			if (close(ofd) == -1)
-				perror("close failed");
-			if (unlink(testfile) == -1)
-				perror("unlink failed");
-		}
+	if (TST_RET == -1) {
+		tst_res(TFAIL | TTERRNO, "call failed unexpectedly");
+		return;
  	}
 
-	cleanup();
-	tst_exit();
-}
-
-/*
- * setup() - performs all ONE TIME setup for this test.
- */
-void setup(void)
-{
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
+	/* stat the original file */
+	SAFE_FSTAT(ofd, &oldbuf);
 
-	TEST_PAUSE;
+	/* stat the duped file */
+	SAFE_FSTAT(*tc->nfd, &newbuf);
 
-	tst_tmpdir();
+	if (oldbuf.st_mode != newbuf.st_mode)

testcases/kernel/syscalls/dup2/dup202.c int newfd; · line 76

Quoted 10 lines
testcases/kernel/syscalls/dup2/dup202.c +6 -3
+		tst_res(TFAIL, "original and dup modes do not match");
+	else
+		tst_res(TPASS, "fstat shows new and old modes are the same");
 
-	(void)umask(0);
-
-	sprintf(testfile, "dup202.%d", getpid());
+	/* remove the file so that we can use it again */
+	if (close(*tc->nfd) == -1)
+		perror("close failed");

It is safe to call SAFE_CLOSE().

testcases/kernel/syscalls/dup2/dup202.c int newfd; · line 78

Quoted 2 lines
testcases/kernel/syscalls/dup2/dup202.c +2 -0
+	if (close(ofd) == -1)
+		perror("close failed");

It is safe to call SAFE_CLOSE().

testcases/kernel/syscalls/dup2/dup202.c int newfd; · line 80

Quoted 2 lines
testcases/kernel/syscalls/dup2/dup202.c +2 -0
+	if (unlink(testfile) == -1)
+		perror("unlink failed");

unlink() is unnecessary if you call SAFE_OPEN() before.

Quoted 16 lines
testcases/kernel/syscalls/dup2/dup202.c +6 -8
  }
 
-/*
- * cleanup() - performs all ONE TIME cleanup for this test at
- *	       completion or premature exit.
- */
-void cleanup(void)
-{
-	tst_rmdir();
-}
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = run,
+	.setup = setup,
+};
#7 lore raw

PATCH 2/5 syscalls/dup2/dup202: Convert dup202 to the new API

In reply to QI Fuli

Hi Qi

Quoted 84 lines

From: QI Fuli<[email protected]>

---
  testcases/kernel/syscalls/dup2/dup202.c | 175 +++++++-----------------
  1 file changed, 48 insertions(+), 127 deletions(-)
testcases/kernel/syscalls/dup2/dup202.c +6 -49
index c87769fa9..16f8b2add 100644
@@ -1,65 +1,22 @@
-/*
- *
- *   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
- */
+// SPDX-License-Identifier: GPL-2.0-or-later
 
  /*
- * NAME
- *	dup202.c
+ * Copyright (c) International Business Machines  Corp., 2001
   *
- * DESCRIPTION
+ * DESCRIPTION:
   *	Is the access mode the same for both file descriptors?
   *		0: read only ?	"0444"
   *		1: write only ? "0222"
   *		2: read/write ? "0666"
   *
- * ALGORITHM
- *	Creat a file with each access mode; dup each file descriptor;
- *	stat each file descriptor and compare modes of each pair
- *
- * USAGE:<for command-line>
- *  dup202 [-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
+ * HISTORY:
   *	07/2001 Ported by Wayne Boyer
- *
- * RESTRICTIONS
- *	None
   */
 
-#include<sys/types.h>
-#include<sys/stat.h>
  #include<errno.h>
-#include<fcntl.h>
  #include<stdio.h>
-#include "test.h"
-#include "safe_macros.h"
-
-char *TCID = "dup202";
-int TST_TOTAL = 3;
-
-void setup(void);
-void cleanup(void);
+#include "tst_test.h"
+#include "tst_safe_macros.h"
 
  char testfile[40];
  int fail;

testcases/kernel/syscalls/dup2/dup202.c int newfd; · line 26

Quoted 2 lines
testcases/kernel/syscalls/dup2/dup202.c +0 -0
@@ -68,100 +25,64 @@ int newfd;
  /* set these to a known index into our local file descriptor table */
  int duprdo = 10, dupwro = 20, duprdwr = 30;

please use static prefix.

testcases/kernel/syscalls/dup2/dup202.c int newfd; · line 32

Quoted 8 lines
testcases/kernel/syscalls/dup2/dup202.c +2 -2
-struct test_case_t {
+static struct tcase {
  	int *nfd;
  	mode_t mode;
-} TC[] = {
+} tcases[]= {
  	/* The first test creat(es) a file with mode 0444 */

These commets are useless because we have said it in the top commet.

Quoted 34 lines
testcases/kernel/syscalls/dup2/dup202.c +4 -25
-	{
-	&duprdo, (S_IRUSR | S_IRGRP | S_IROTH)},
+	{&duprdo, (S_IRUSR | S_IRGRP | S_IROTH)},
  	    /* The second test creat(es) a file with mode 0222 */
-	{
-	&dupwro, (S_IWUSR | S_IWGRP | S_IWOTH)},
+	{&dupwro, (S_IWUSR | S_IWGRP | S_IWOTH)},
  	    /* The third test creat(es) a file with mode 0666 */
-	{
-	&duprdwr,
-		    (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH)}
+	{&duprdwr, (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH)},
  };
 
-int main(int ac, char **av)
+void setup(void)
  {
-	int lc;
-	int i, ofd;
-	struct stat oldbuf, newbuf;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		/* loop through the test cases */
-		for (i = 0; i<  TST_TOTAL; i++) {
-
-			if ((ofd = creat(testfile, TC[i].mode)) == -1)
-				tst_brkm(TBROK | TERRNO, cleanup,

testcases/kernel/syscalls/dup2/dup202.c int newfd; · line 42

Quoted 10 lines
testcases/kernel/syscalls/dup2/dup202.c +1 -9
-					 "creat failed");
-
-			TEST(dup2(ofd, *TC[i].nfd));
-
-			if (TEST_RETURN == -1) {
-				tst_resm(TFAIL | TTERRNO,
-					 "call failed unexpectedly");
-				continue;
-			}
+	(void)umask(0);

umask(0) is enough.

Quoted 27 lines
testcases/kernel/syscalls/dup2/dup202.c +11 -12
+	sprintf(testfile, "dup202.%d", getpid());
+}
 
-			/* stat the original file */
-			SAFE_FSTAT(cleanup, ofd,&oldbuf);
+static void run(unsigned int i)
+{
+	int ofd;
+	struct stat oldbuf, newbuf;
+	struct tcase *tc = tcases + i;
 
-			/* stat the duped file */
-			SAFE_FSTAT(cleanup, *TC[i].nfd,&newbuf);
+	ofd = creat(testfile, tc->mode);
+	if (ofd == -1)
+		tst_brk(TBROK | TERRNO, "creat failed");
 
-			if (oldbuf.st_mode != newbuf.st_mode)
-				tst_resm(TFAIL, "original and dup "
-					 "modes do not match");
-			else
-				tst_resm(TPASS, "fstat shows new and "
-					 "old modes are the same");
+	TEST(dup2(ofd, *tc->nfd));
 
-			/* remove the file so that we can use it again */
-			if (close(*TC[i].nfd) == -1)

testcases/kernel/syscalls/dup2/dup202.c int newfd; · line 61

Quoted 10 lines
testcases/kernel/syscalls/dup2/dup202.c +3 -6
-				perror("close failed");
-			if (close(ofd) == -1)
-				perror("close failed");
-			if (unlink(testfile) == -1)
-				perror("unlink failed");
-		}
+	if (TST_RET == -1) {
+		tst_res(TFAIL | TTERRNO, "call failed unexpectedly");
+		return;
  	}

We can use SAFE_DUP2 here.

Best Regards
Yang Xu

Quoted 51 lines
testcases/kernel/syscalls/dup2/dup202.c +21 -24
-	cleanup();
-	tst_exit();
-}
-
-/*
- * setup() - performs all ONE TIME setup for this test.
- */
-void setup(void)
-{
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
+	/* stat the original file */
+	SAFE_FSTAT(ofd,&oldbuf);
 
-	TEST_PAUSE;
+	/* stat the duped file */
+	SAFE_FSTAT(*tc->nfd,&newbuf);
 
-	tst_tmpdir();
+	if (oldbuf.st_mode != newbuf.st_mode)
+		tst_res(TFAIL, "original and dup modes do not match");
+	else
+		tst_res(TPASS, "fstat shows new and old modes are the same");
 
-	(void)umask(0);
-
-	sprintf(testfile, "dup202.%d", getpid());
+	/* remove the file so that we can use it again */
+	if (close(*tc->nfd) == -1)
+		perror("close failed");
+	if (close(ofd) == -1)
+		perror("close failed");
+	if (unlink(testfile) == -1)
+		perror("unlink failed");
  }
 
-/*
- * cleanup() - performs all ONE TIME cleanup for this test at
- *	       completion or premature exit.
- */
-void cleanup(void)
-{
-	tst_rmdir();
-}
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = run,
+	.setup = setup,
+};
author
#8 lore raw

PATCH 3/5 syscalls/dup2/dup203: Convert dup203 to the new API

In reply to QI Fuli

From: QI Fuli <[email protected]>

---
 testcases/kernel/syscalls/dup2/dup203.c | 291 +++++++++---------------
 1 file changed, 112 insertions(+), 179 deletions(-)
testcases/kernel/syscalls/dup2/dup203.c +112 -179
index e6f281adf..a2cbcc8f9 100644
@@ -1,208 +1,141 @@
-/*
- *
- *   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
- */
+// SPDX-License-Identifier: GPL-2.0-or-later
 
 /*
- * NAME
- *	dup203.c
+ * Copyright (c) International Business Machines  Corp., 2001
  *
- * DESCRIPTION
+ * DESCRIPTION:
  *	Testcase to check the basic functionality of dup2().
+ *	1. Attempt to dup2() on an open file descriptor.
+ *	2. Attempt to dup2() on a close file descriptor.
  *
- * ALGORITHM
- *	1.	Attempt to dup2() on an open file descriptor.
- *	2.	Attempt to dup2() on a close file descriptor.
- *
- * USAGE:  <for command-line>
- *  dup203 [-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
+ * HISTORY:
  *	07/2001 Ported by Wayne Boyer
- *
- * RESTRICTIONS
- *	NONE
  */
 
-#include <fcntl.h>
-#include <sys/param.h>
 #include <errno.h>
-#include <string.h>
-#include "test.h"
-#include "safe_macros.h"
+#include <stdio.h>
+#include "tst_test.h"
+#include "tst_safe_macros.h"
 
-void setup(void);
-void cleanup(void);
-
-char *TCID = "dup203";
-int TST_TOTAL = 1;
-
-int main(int ac, char **av)
+static void run(void)
 {
 	int fd0, fd1, fd2, rval;
 	char filename0[40], filename1[40];
 	char buf[40];
 
-	int lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
 //block1:
-		tst_resm(TINFO, "Enter block 1");
-		tst_resm(TINFO, "Test duping over an open fd");
-
-		sprintf(filename0, "dup202.file0.%d\n", getpid());
-		sprintf(filename1, "dup202.file1.%d\n", getpid());
-		unlink(filename0);
-		unlink(filename1);
-
-		if ((fd0 = creat(filename0, 0666)) == -1)
-			tst_brkm(TBROK, cleanup, "cannot create first file");
-		if (write(fd0, filename0, strlen(filename0)) == -1)
-			tst_brkm(TBROK, cleanup, "filename0: write(2) failed");
-
-		if ((fd1 = creat(filename1, 0666)) == -1)
-			tst_brkm(TBROK, cleanup, "Cannot create second file");
-		if (write(fd1, filename1, strlen(filename1)) == -1)
-			tst_brkm(TBROK, cleanup, "filename1: write(2) failed");
-
-		SAFE_CLOSE(cleanup, fd0);
-		if ((fd0 = open(filename0, O_RDONLY)) == -1)
-			tst_brkm(TBROK, cleanup, "open(2) on filename0 failed");
-
-		SAFE_CLOSE(cleanup, fd1);
-		if ((fd1 = open(filename1, O_RDONLY)) == -1)
-			tst_brkm(TBROK, cleanup, "open(2) on filename1 failed");
-
-		TEST(dup2(fd0, fd1));
-
-		if ((fd2 = TEST_RETURN) == -1) {
-			tst_resm(TFAIL, "call failed unexpectedly");
-		} else {
-			if (fd1 != fd2) {
-				tst_resm(TFAIL, "file descriptors don't match");
-				break;
-			}
-
-			memset(buf, 0, sizeof(buf));
-			if (read(fd2, buf, sizeof(buf)) == -1)
-				tst_brkm(TBROK, cleanup, "read(2) failed");
-			if (strcmp(buf, filename0) != 0)
-				tst_resm(TFAIL, "read from file got bad data");
-			tst_resm(TPASS, "dup2 test 1 functionality is correct");
+	tst_res(TINFO, "Enter block 1");
+	tst_res(TINFO, "Test duping over an open fd");
+
+	sprintf(filename0, "dup202.file0.%d\n", getpid());
+	sprintf(filename1, "dup202.file1.%d\n", getpid());
+	unlink(filename0);
+	unlink(filename1);
+
+	fd0 = creat(filename0, 0666);
+	if (fd0 == -1)
+		tst_brk(TBROK, "cannot create first file");
+	if (write(fd0, filename0, strlen(filename0)) == -1)
+		tst_brk(TBROK, "filename0: write(2) failed");
+
+	fd1 = creat(filename1, 0666);
+	if (fd1 == -1)
+		tst_brk(TBROK, "Cannot create second file");
+	if (write(fd1, filename1, strlen(filename1)) == -1)
+		tst_brk(TBROK, "filename1: write(2) failed");
+
+	SAFE_CLOSE(fd0);
+	fd0 = open(filename0, O_RDONLY);
+	if (fd0 == -1)
+		tst_brk(TBROK, "open(2) on filename0 failed");
+
+	SAFE_CLOSE(fd1);
+	fd1 = open(filename1, O_RDONLY);
+	if (fd1 == -1)
+		tst_brk(TBROK, "open(2) on filename1 failed");
+
+	TEST(dup2(fd0, fd1));
+
+	fd2 = TST_RET;
+	if (fd2 == -1)
+		tst_res(TFAIL, "call failed unexpectedly");
+	else {
+		if (fd1 != fd2) {
+			tst_res(TFAIL, "file descriptors don't match");
+			return;
 		}
 
-		close(fd0);
-		close(fd1);
-		close(fd2);
-		unlink(filename0);
-		unlink(filename1);
-
-		tst_resm(TINFO, "Exit block 1");
-
-//block2:
-		tst_resm(TINFO, "Enter block 2");
-		tst_resm(TINFO, "Test close on exec flag");
+		memset(buf, 0, sizeof(buf));
+		if (read(fd2, buf, sizeof(buf)) == -1)
+			tst_brk(TBROK, "read(2) failed");
+		if (strcmp(buf, filename0) != 0)
+			tst_res(TFAIL, "read from file got bad data");
+		tst_res(TPASS, "dup2 test 1 functionality is correct");
+	}
 
-		sprintf(filename0, "dup02.%d\n", getpid());
-		unlink(filename0);
+	close(fd0);
+	close(fd1);
+	close(fd2);
+	unlink(filename0);
+	unlink(filename1);
 
-		if ((fd0 = creat(filename0, 0666)) == -1) {
-			tst_brkm(TBROK, cleanup, "Cannot create first file");
-		}
-		if (fcntl(fd0, F_SETFD, 1) == -1) {
-			tst_brkm(TBROK, cleanup, "setting close on exec flag "
-				 "on fd0 failed");
-		}
+	tst_res(TINFO, "Exit block 1");
 
-		if ((fd2 = creat(filename1, 0666)) == -1) {
-			tst_brkm(TBROK, cleanup, "Cannot create second file");
+//block2:
+	tst_res(TINFO, "Enter block 2");
+	tst_res(TINFO, "Test close on exec flag");
+
+	sprintf(filename0, "dup02.%d\n", getpid());
+	unlink(filename0);
+
+	fd0 = creat(filename0, 0666);
+	if (fd0 == -1)
+		tst_brk(TBROK, "Cannot create first file");
+	if (fcntl(fd0, F_SETFD, 1) == -1)
+		tst_brk(TBROK, "setting close on exec flag on fd0 failed");
+
+	fd2 = creat(filename1, 0666);
+	if (fd2 == -1)
+		tst_brk(TBROK, "Cannot create second file");
+
+	/* SAFE_CLOSE() sets the fd to -1 avoid it here */
+	rval = fd2;
+	SAFE_CLOSE(rval);
+
+	TEST(dup2(fd0, fd2));
+
+	fd1 = TST_RET;
+	if (fd1 == -1)
+		tst_res(TFAIL, "call failed unexpectedly");
+	else {
+		if (fd1 != fd2) {
+			tst_res(TFAIL, "bad dup2 descriptor %d", fd1);
+			return;
 		}
 
-		/* SAFE_CLOSE() sets the fd to -1 avoid it here */
-		rval = fd2;
-		SAFE_CLOSE(cleanup, rval);
-
-		TEST(dup2(fd0, fd2));
-
-		if ((fd1 = TEST_RETURN) == -1) {
-			tst_resm(TFAIL, "call failed unexpectedly");
-		} else {
-			if (fd1 != fd2) {
-				tst_resm(TFAIL, "bad dup2 descriptor %d", fd1);
-				break;
-			}
-
-			if ((rval = fcntl(fd1, F_GETFD, 0)) != 0) {
-				tst_resm(TBROK | TERRNO,
-					 "fcntl F_GETFD on fd1 failed; expected a "
-					 "return value of 0x0, got %#x", rval);
-				break;
-			}
-			if ((rval = (fcntl(fd0, F_GETFL, 0) & O_ACCMODE)) !=
-			    O_WRONLY) {
-				tst_resm(TFAIL, "fctnl F_GETFL bad rval on fd0 "
-					 "Expected %#x got %#x", O_WRONLY,
-					 rval);
-			}
-			tst_resm(TPASS, "dup2 test 2 functionality is correct");
+		rval = fcntl(fd1, F_GETFD, 0);
+		if (rval != 0) {
+			tst_res(TFAIL | TERRNO,
+				"fcntl F_GETFD on fd1 failed; expected a "
+				"return value of 0x0, got %#x", rval);
+			return;
 		}
-
-		close(fd0);
-		close(fd1);
-
-		unlink(filename0);
-		unlink(filename1);
-		tst_resm(TINFO, "Exit block 2");
+		rval = fcntl(fd0, F_GETFL, 0);
+		if ((rval & O_ACCMODE) != O_WRONLY)
+			tst_res(TFAIL, "fctnl F_GETFL bad rval on fd0 "
+				"Expected %#x got %#x", O_WRONLY, rval);
+		tst_res(TPASS, "dup2 test 2 functionality is correct");
 	}
 
-	cleanup();
-	tst_exit();
-}
+	close(fd0);
+	close(fd1);
 
-/*
- * setup() - performs all ONE TIME setup for this test.
- */
-void setup(void)
-{
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
+	unlink(filename0);
+	unlink(filename1);
+	tst_res(TINFO, "Exit block 2");
 }
 
-/*
- * cleanup() - performs all ONE TIME cleanup for this test at
- *	       completion or premature exit.
- */
-void cleanup(void)
-{
-	tst_rmdir();
-}
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.test_all = run,
+};
Signature · 1 line
2.31.1
#9 lore raw

PATCH 3/5 syscalls/dup2/dup203: Convert dup203 to the new API

Hi Qi

Please use some safe macros.
And the run function is too long and I think we can divide it into two function(may be in a verify function with differnt arguments like dup202.c).

Best Regards
Yang Xu

Quoted 332 lines

From: QI Fuli<[email protected]>

---
  testcases/kernel/syscalls/dup2/dup203.c | 291 +++++++++---------------
  1 file changed, 112 insertions(+), 179 deletions(-)
testcases/kernel/syscalls/dup2/dup203.c +112 -179
index e6f281adf..a2cbcc8f9 100644
@@ -1,208 +1,141 @@
-/*
- *
- *   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
- */
+// SPDX-License-Identifier: GPL-2.0-or-later
 
  /*
- * NAME
- *	dup203.c
+ * Copyright (c) International Business Machines  Corp., 2001
   *
- * DESCRIPTION
+ * DESCRIPTION:
   *	Testcase to check the basic functionality of dup2().
+ *	1. Attempt to dup2() on an open file descriptor.
+ *	2. Attempt to dup2() on a close file descriptor.
   *
- * ALGORITHM
- *	1.	Attempt to dup2() on an open file descriptor.
- *	2.	Attempt to dup2() on a close file descriptor.
- *
- * USAGE:<for command-line>
- *  dup203 [-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
+ * HISTORY:
   *	07/2001 Ported by Wayne Boyer
- *
- * RESTRICTIONS
- *	NONE
   */
 
-#include<fcntl.h>
-#include<sys/param.h>
  #include<errno.h>
-#include<string.h>
-#include "test.h"
-#include "safe_macros.h"
+#include<stdio.h>
+#include "tst_test.h"
+#include "tst_safe_macros.h"
 
-void setup(void);
-void cleanup(void);
-
-char *TCID = "dup203";
-int TST_TOTAL = 1;
-
-int main(int ac, char **av)
+static void run(void)
  {
  	int fd0, fd1, fd2, rval;
  	char filename0[40], filename1[40];
  	char buf[40];
 
-	int lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
  //block1:
-		tst_resm(TINFO, "Enter block 1");
-		tst_resm(TINFO, "Test duping over an open fd");
-
-		sprintf(filename0, "dup202.file0.%d\n", getpid());
-		sprintf(filename1, "dup202.file1.%d\n", getpid());
-		unlink(filename0);
-		unlink(filename1);
-
-		if ((fd0 = creat(filename0, 0666)) == -1)
-			tst_brkm(TBROK, cleanup, "cannot create first file");
-		if (write(fd0, filename0, strlen(filename0)) == -1)
-			tst_brkm(TBROK, cleanup, "filename0: write(2) failed");
-
-		if ((fd1 = creat(filename1, 0666)) == -1)
-			tst_brkm(TBROK, cleanup, "Cannot create second file");
-		if (write(fd1, filename1, strlen(filename1)) == -1)
-			tst_brkm(TBROK, cleanup, "filename1: write(2) failed");
-
-		SAFE_CLOSE(cleanup, fd0);
-		if ((fd0 = open(filename0, O_RDONLY)) == -1)
-			tst_brkm(TBROK, cleanup, "open(2) on filename0 failed");
-
-		SAFE_CLOSE(cleanup, fd1);
-		if ((fd1 = open(filename1, O_RDONLY)) == -1)
-			tst_brkm(TBROK, cleanup, "open(2) on filename1 failed");
-
-		TEST(dup2(fd0, fd1));
-
-		if ((fd2 = TEST_RETURN) == -1) {
-			tst_resm(TFAIL, "call failed unexpectedly");
-		} else {
-			if (fd1 != fd2) {
-				tst_resm(TFAIL, "file descriptors don't match");
-				break;
-			}
-
-			memset(buf, 0, sizeof(buf));
-			if (read(fd2, buf, sizeof(buf)) == -1)
-				tst_brkm(TBROK, cleanup, "read(2) failed");
-			if (strcmp(buf, filename0) != 0)
-				tst_resm(TFAIL, "read from file got bad data");
-			tst_resm(TPASS, "dup2 test 1 functionality is correct");
+	tst_res(TINFO, "Enter block 1");
+	tst_res(TINFO, "Test duping over an open fd");
+
+	sprintf(filename0, "dup202.file0.%d\n", getpid());
+	sprintf(filename1, "dup202.file1.%d\n", getpid());
+	unlink(filename0);
+	unlink(filename1);
+
+	fd0 = creat(filename0, 0666);
+	if (fd0 == -1)
+		tst_brk(TBROK, "cannot create first file");
+	if (write(fd0, filename0, strlen(filename0)) == -1)
+		tst_brk(TBROK, "filename0: write(2) failed");
+
+	fd1 = creat(filename1, 0666);
+	if (fd1 == -1)
+		tst_brk(TBROK, "Cannot create second file");
+	if (write(fd1, filename1, strlen(filename1)) == -1)
+		tst_brk(TBROK, "filename1: write(2) failed");
+
+	SAFE_CLOSE(fd0);
+	fd0 = open(filename0, O_RDONLY);
+	if (fd0 == -1)
+		tst_brk(TBROK, "open(2) on filename0 failed");
+
+	SAFE_CLOSE(fd1);
+	fd1 = open(filename1, O_RDONLY);
+	if (fd1 == -1)
+		tst_brk(TBROK, "open(2) on filename1 failed");
+
+	TEST(dup2(fd0, fd1));
+
+	fd2 = TST_RET;
+	if (fd2 == -1)
+		tst_res(TFAIL, "call failed unexpectedly");
+	else {
+		if (fd1 != fd2) {
+			tst_res(TFAIL, "file descriptors don't match");
+			return;
  		}
 
-		close(fd0);
-		close(fd1);
-		close(fd2);
-		unlink(filename0);
-		unlink(filename1);
-
-		tst_resm(TINFO, "Exit block 1");
-
-//block2:
-		tst_resm(TINFO, "Enter block 2");
-		tst_resm(TINFO, "Test close on exec flag");
+		memset(buf, 0, sizeof(buf));
+		if (read(fd2, buf, sizeof(buf)) == -1)
+			tst_brk(TBROK, "read(2) failed");
+		if (strcmp(buf, filename0) != 0)
+			tst_res(TFAIL, "read from file got bad data");
+		tst_res(TPASS, "dup2 test 1 functionality is correct");
+	}
 
-		sprintf(filename0, "dup02.%d\n", getpid());
-		unlink(filename0);
+	close(fd0);
+	close(fd1);
+	close(fd2);
+	unlink(filename0);
+	unlink(filename1);
 
-		if ((fd0 = creat(filename0, 0666)) == -1) {
-			tst_brkm(TBROK, cleanup, "Cannot create first file");
-		}
-		if (fcntl(fd0, F_SETFD, 1) == -1) {
-			tst_brkm(TBROK, cleanup, "setting close on exec flag "
-				 "on fd0 failed");
-		}
+	tst_res(TINFO, "Exit block 1");
 
-		if ((fd2 = creat(filename1, 0666)) == -1) {
-			tst_brkm(TBROK, cleanup, "Cannot create second file");
+//block2:
+	tst_res(TINFO, "Enter block 2");
+	tst_res(TINFO, "Test close on exec flag");
+
+	sprintf(filename0, "dup02.%d\n", getpid());
+	unlink(filename0);
+
+	fd0 = creat(filename0, 0666);
+	if (fd0 == -1)
+		tst_brk(TBROK, "Cannot create first file");
+	if (fcntl(fd0, F_SETFD, 1) == -1)
+		tst_brk(TBROK, "setting close on exec flag on fd0 failed");
+
+	fd2 = creat(filename1, 0666);
+	if (fd2 == -1)
+		tst_brk(TBROK, "Cannot create second file");
+
+	/* SAFE_CLOSE() sets the fd to -1 avoid it here */
+	rval = fd2;
+	SAFE_CLOSE(rval);
+
+	TEST(dup2(fd0, fd2));
+
+	fd1 = TST_RET;
+	if (fd1 == -1)
+		tst_res(TFAIL, "call failed unexpectedly");
+	else {
+		if (fd1 != fd2) {
+			tst_res(TFAIL, "bad dup2 descriptor %d", fd1);
+			return;
  		}
 
-		/* SAFE_CLOSE() sets the fd to -1 avoid it here */
-		rval = fd2;
-		SAFE_CLOSE(cleanup, rval);
-
-		TEST(dup2(fd0, fd2));
-
-		if ((fd1 = TEST_RETURN) == -1) {
-			tst_resm(TFAIL, "call failed unexpectedly");
-		} else {
-			if (fd1 != fd2) {
-				tst_resm(TFAIL, "bad dup2 descriptor %d", fd1);
-				break;
-			}
-
-			if ((rval = fcntl(fd1, F_GETFD, 0)) != 0) {
-				tst_resm(TBROK | TERRNO,
-					 "fcntl F_GETFD on fd1 failed; expected a "
-					 "return value of 0x0, got %#x", rval);
-				break;
-			}
-			if ((rval = (fcntl(fd0, F_GETFL, 0)&  O_ACCMODE)) !=
-			    O_WRONLY) {
-				tst_resm(TFAIL, "fctnl F_GETFL bad rval on fd0 "
-					 "Expected %#x got %#x", O_WRONLY,
-					 rval);
-			}
-			tst_resm(TPASS, "dup2 test 2 functionality is correct");
+		rval = fcntl(fd1, F_GETFD, 0);
+		if (rval != 0) {
+			tst_res(TFAIL | TERRNO,
+				"fcntl F_GETFD on fd1 failed; expected a "
+				"return value of 0x0, got %#x", rval);
+			return;
  		}
-
-		close(fd0);
-		close(fd1);
-
-		unlink(filename0);
-		unlink(filename1);
-		tst_resm(TINFO, "Exit block 2");
+		rval = fcntl(fd0, F_GETFL, 0);
+		if ((rval&  O_ACCMODE) != O_WRONLY)
+			tst_res(TFAIL, "fctnl F_GETFL bad rval on fd0 "
+				"Expected %#x got %#x", O_WRONLY, rval);
+		tst_res(TPASS, "dup2 test 2 functionality is correct");
  	}
 
-	cleanup();
-	tst_exit();
-}
+	close(fd0);
+	close(fd1);
 
-/*
- * setup() - performs all ONE TIME setup for this test.
- */
-void setup(void)
-{
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
+	unlink(filename0);
+	unlink(filename1);
+	tst_res(TINFO, "Exit block 2");
  }
 
-/*
- * cleanup() - performs all ONE TIME cleanup for this test at
- *	       completion or premature exit.
- */
-void cleanup(void)
-{
-	tst_rmdir();
-}
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.test_all = run,
+};
author
#10 lore raw

PATCH 4/5 syscalls/dup2/dup204: Convert dup204 to the new API

In reply to QI Fuli

From: QI Fuli <[email protected]>

---
 testcases/kernel/syscalls/dup2/dup204.c | 135 ++++++------------------
 1 file changed, 35 insertions(+), 100 deletions(-)
testcases/kernel/syscalls/dup2/dup204.c +35 -100
index a357bc17e..3fd6b0832 100644
@@ -1,118 +1,25 @@
-/*
- *
- *   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
- */
+// SPDX-License-Identifier: GPL-2.0-or-later
 
 /*
- * NAME
- *	dup204.c
+ * Copyright (c) International Business Machines  Corp., 2001
  *
- * DESCRIPTION
+ * DESCRIPTION:
  *	Testcase to check the basic functionality of dup2(2).
- *
- * ALGORITHM
- *	attempt to call dup2() on read/write ends of a pipe
- *
- * USAGE:  <for command-line>
- *  dup204 [-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.
- *
- * RESTRICTION
- *	NONE
  */
 
 #ifndef _GNU_SOURCE
 #define _GNU_SOURCE
 #endif
-#include <sys/types.h>
-#include <fcntl.h>
-#include <sys/stat.h>
-#include <errno.h>
-#include <signal.h>
-#include <string.h>
-#include "test.h"
-#include "safe_macros.h"
-
-void setup();
-void cleanup();
-
-char *TCID = "dup204";
-int TST_TOTAL = 2;
+#include "tst_test.h"
+#include "tst_safe_macros.h"
 
 int fd[2];
 int nfd[2];
 
-int main(int ac, char **av)
-{
-	int lc;
-	int i;
-	struct stat oldbuf, newbuf;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		/* loop through the test cases */
-		for (i = 0; i < TST_TOTAL; i++) {
-			TEST(dup2(fd[i], nfd[i]));
-
-			if (TEST_RETURN == -1) {
-				tst_resm(TFAIL, "call failed unexpectedly");
-				continue;
-			}
-
-			SAFE_FSTAT(cleanup, fd[i], &oldbuf);
-			SAFE_FSTAT(cleanup, nfd[i], &newbuf);
-
-			if (oldbuf.st_ino != newbuf.st_ino)
-				tst_resm(TFAIL, "original and duped "
-					 "inodes do not match");
-			else
-				tst_resm(TPASS, "original and duped "
-					 "inodes are the same");
-
-			SAFE_CLOSE(cleanup, TEST_RETURN);
-		}
-	}
-
-	cleanup();
-	tst_exit();
-}
-
 void setup(void)
 {
 	fd[0] = -1;
-
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-
-	SAFE_PIPE(cleanup, fd);
+	SAFE_PIPE(fd);
 }
 
 void cleanup(void)
@@ -123,6 +30,34 @@ void cleanup(void)
 		close(fd[i]);
 		close(nfd[i]);
 	}
+}
+
+static void run(unsigned int i)
+{
+	struct stat oldbuf, newbuf;
+
+	TEST(dup2(fd[i], nfd[i]));
 
-	tst_rmdir();
+	if (TST_RET == -1) {
+		tst_res(TFAIL, "call failed unexpectedly");
+		return;
+	}
+
+	SAFE_FSTAT(fd[i], &oldbuf);
+	SAFE_FSTAT(nfd[i], &newbuf);
+
+	if (oldbuf.st_ino != newbuf.st_ino)
+		tst_res(TFAIL, "original and duped inodes do not match");
+	else
+		tst_res(TPASS, "original and duped inodes are the same");
+
+	SAFE_CLOSE(TST_RET);
 }
+
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.tcnt = 2,
+	.test = run,
+	.setup = setup,
+	.cleanup = cleanup,
+};
Signature · 1 line
2.31.1
#11 lore raw

PATCH 4/5 syscalls/dup2/dup204: Convert dup204 to the new API

Hi Qi

I think we can merge this case into dup202 directly.
So dup202 not only check modes doesn't change but also check inode number doesn't change.

Best Regards
Yang Xu

Quoted 172 lines

From: QI Fuli<[email protected]>

---
  testcases/kernel/syscalls/dup2/dup204.c | 135 ++++++------------------
  1 file changed, 35 insertions(+), 100 deletions(-)
testcases/kernel/syscalls/dup2/dup204.c +35 -100
index a357bc17e..3fd6b0832 100644
@@ -1,118 +1,25 @@
-/*
- *
- *   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
- */
+// SPDX-License-Identifier: GPL-2.0-or-later
 
  /*
- * NAME
- *	dup204.c
+ * Copyright (c) International Business Machines  Corp., 2001
   *
- * DESCRIPTION
+ * DESCRIPTION:
   *	Testcase to check the basic functionality of dup2(2).
- *
- * ALGORITHM
- *	attempt to call dup2() on read/write ends of a pipe
- *
- * USAGE:<for command-line>
- *  dup204 [-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.
- *
- * RESTRICTION
- *	NONE
   */
 
  #ifndef _GNU_SOURCE
  #define _GNU_SOURCE
  #endif
-#include<sys/types.h>
-#include<fcntl.h>
-#include<sys/stat.h>
-#include<errno.h>
-#include<signal.h>
-#include<string.h>
-#include "test.h"
-#include "safe_macros.h"
-
-void setup();
-void cleanup();
-
-char *TCID = "dup204";
-int TST_TOTAL = 2;
+#include "tst_test.h"
+#include "tst_safe_macros.h"
 
  int fd[2];
  int nfd[2];
 
-int main(int ac, char **av)
-{
-	int lc;
-	int i;
-	struct stat oldbuf, newbuf;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		/* loop through the test cases */
-		for (i = 0; i<  TST_TOTAL; i++) {
-			TEST(dup2(fd[i], nfd[i]));
-
-			if (TEST_RETURN == -1) {
-				tst_resm(TFAIL, "call failed unexpectedly");
-				continue;
-			}
-
-			SAFE_FSTAT(cleanup, fd[i],&oldbuf);
-			SAFE_FSTAT(cleanup, nfd[i],&newbuf);
-
-			if (oldbuf.st_ino != newbuf.st_ino)
-				tst_resm(TFAIL, "original and duped "
-					 "inodes do not match");
-			else
-				tst_resm(TPASS, "original and duped "
-					 "inodes are the same");
-
-			SAFE_CLOSE(cleanup, TEST_RETURN);
-		}
-	}
-
-	cleanup();
-	tst_exit();
-}
-
  void setup(void)
  {
  	fd[0] = -1;
-
-	tst_sig(FORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-
-	SAFE_PIPE(cleanup, fd);
+	SAFE_PIPE(fd);
  }
 
  void cleanup(void)
@@ -123,6 +30,34 @@ void cleanup(void)
  		close(fd[i]);
  		close(nfd[i]);
  	}
+}
+
+static void run(unsigned int i)
+{
+	struct stat oldbuf, newbuf;
+
+	TEST(dup2(fd[i], nfd[i]));
 
-	tst_rmdir();
+	if (TST_RET == -1) {
+		tst_res(TFAIL, "call failed unexpectedly");
+		return;
+	}
+
+	SAFE_FSTAT(fd[i],&oldbuf);
+	SAFE_FSTAT(nfd[i],&newbuf);
+
+	if (oldbuf.st_ino != newbuf.st_ino)
+		tst_res(TFAIL, "original and duped inodes do not match");
+	else
+		tst_res(TPASS, "original and duped inodes are the same");
+
+	SAFE_CLOSE(TST_RET);
  }
+
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.tcnt = 2,
+	.test = run,
+	.setup = setup,
+	.cleanup = cleanup,
+};
author
#12 lore raw

PATCH 5/5 syscalls/dup2/dup205: Convert dup205 to the new API

In reply to QI Fuli

From: QI Fuli <[email protected]>

---
 testcases/kernel/syscalls/dup2/dup205.c | 165 +++++++++---------------
 1 file changed, 62 insertions(+), 103 deletions(-)
testcases/kernel/syscalls/dup2/dup205.c +62 -103
index 0b324531f..30526fda6 100644
@@ -1,45 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
 /*
+ * Copyright (c) International Business Machines  Corp., 2002
  *
- *   Copyright (c) International Business Machines  Corp., 2002
- *
- *   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
  */
 
 /* Ported from SPIE, section2/iosuite/dup6.c, by Airong Zhang */
 
-/*======================================================================
-	=================== TESTPLAN SEGMENT ===================
->KEYS:  < dup2()
->WHAT:  < Does dup return -1 on the 21st file?
->HOW:   < Create up to _NFILE files and check for -1 return on the
-	< next attempt
-	< Should check NOFILE as well as _NFILE.  19-Jun-84 Dale.
->BUGS:  <
-======================================================================*/
-
-#include <sys/param.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <errno.h>
-#include <fcntl.h>
+#include <stdlib.h>
 #include <stdio.h>
-#include <unistd.h>
-#include "test.h"
+#include "tst_test.h"
 
-char *TCID = "dup205";
-int TST_TOTAL = 1;
 int *fildes;
 int min;
 int local_flag;
@@ -47,88 +18,76 @@ int local_flag;
 #define PASSED 1
 #define FAILED 0
 
-static void setup(void);
-static void cleanup(void);
+static void setup(void)
+{
+	min = getdtablesize();	/* get number of files allowed open */
+	fildes = malloc((min + 10) * sizeof(int));
+	if (fildes == NULL)
+		tst_brk(TBROK | TERRNO, "malloc error");
+}
+
+static void cleanup(void)
+{
+	if (fildes != NULL)
+		free(fildes);
+}
 
-int main(int ac, char *av[])
+static void run(void)
 {
 	int ifile;
 	char pfilname[40];
 	int serrno;
 
-	int lc;
-
 	ifile = -1;
 
-	tst_parse_opts(ac, av, NULL, NULL);
-
 	local_flag = PASSED;
 
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		sprintf(pfilname, "./dup205.%d\n", getpid());
-		unlink(pfilname);
-		serrno = 0;
-		if ((fildes[0] = creat(pfilname, 0666)) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "creat failed");
-		else {
-			fildes[fildes[0]] = fildes[0];
-			for (ifile = fildes[0] + 1; ifile < min + 10; ifile++) {
-				if ((fildes[ifile] = dup2(fildes[ifile - 1],
-							  ifile)) == -1) {
-					serrno = errno;
-					break;
-				} else {
-					if (fildes[ifile] != ifile) {
-						tst_brkm(TFAIL, cleanup,
-							 "got wrong descriptor "
-							 "number back (%d != %d)",
-							 fildes[ifile], ifile);
-					}
-				}
-			}	/* end for */
-			if (ifile < min) {
-				tst_resm(TFAIL, "Not enough files duped");
-				local_flag = FAILED;
-			} else if (ifile > min) {
-				tst_resm(TFAIL, "Too many files duped");
-				local_flag = FAILED;
-			}
-			if (serrno != EBADF && serrno != EMFILE &&
-			    serrno != EINVAL) {
-				tst_resm(TFAIL, "bad errno on dup2 failure");
-				local_flag = FAILED;
+	sprintf(pfilname, "./dup205.%d\n", getpid());
+	unlink(pfilname);
+	serrno = 0;
+
+	fildes[0] = creat(pfilname, 0666);
+	if (fildes[0] == -1)
+		tst_brk(TBROK | TERRNO, "creat failed");
+	else {
+		fildes[fildes[0]] = fildes[0];
+		for (ifile = fildes[0] + 1; ifile < min + 10; ifile++) {
+			fildes[ifile] = dup2(fildes[ifile - 1], ifile);
+			if (fildes[ifile] == -1) {
+				serrno = errno;
+				break;
 			}
+			if (fildes[ifile] != ifile)
+				tst_brk(TFAIL, "got wrong descriptor "
+					"number back (%d != %d)",
+					fildes[ifile], ifile);
+		}	/* end for */
+		if (ifile < min) {
+			tst_res(TFAIL, "Not enough files duped");
+			local_flag = FAILED;
+		} else if (ifile > min) {
+			tst_res(TFAIL, "Too many files duped");
+			local_flag = FAILED;
 		}
-		unlink(pfilname);
-		for (ifile = fildes[0]; ifile < min + 10; ifile++)
-			close(fildes[ifile]);
-		if (local_flag == PASSED) {
-			tst_resm(TPASS, "Test passed.");
-		} else {
-			tst_resm(TFAIL, "Test failed.");
+		if (serrno != EBADF && serrno != EMFILE &&
+			    serrno != EINVAL) {
+			tst_res(TFAIL, "bad errno on dup2 failure");
+			local_flag = FAILED;
 		}
-
 	}
-	cleanup();
-	tst_exit();
-}
-
-static void setup(void)
-{
-	tst_tmpdir();
-
-	min = getdtablesize();	/* get number of files allowed open */
-	fildes = malloc((min + 10) * sizeof(int));
-	if (fildes == NULL)
-		tst_brkm(TBROK | TERRNO, cleanup, "malloc error");
+	unlink(pfilname);
+	for (ifile = fildes[0]; ifile < min + 10; ifile++)
+		close(fildes[ifile]);
+	if (local_flag == PASSED) {
+		tst_res(TPASS, "Test passed.");
+	} else {
+			tst_res(TFAIL, "Test failed.");
+	}
 }
 
-static void cleanup(void)
-{
-	if (fildes != NULL)
-		free(fildes);
-	tst_rmdir();
-}
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.test_all = run,
+	.setup = setup,
+	.cleanup = cleanup,
+};
Signature · 1 line
2.31.1
#13 lore raw

PATCH 5/5 syscalls/dup2/dup205: Convert dup205 to the new API

Hi Qi

please use safe_macros and static prefix.

Quoted 25 lines

From: QI Fuli<[email protected]>

---
  testcases/kernel/syscalls/dup2/dup205.c | 165 +++++++++---------------
  1 file changed, 62 insertions(+), 103 deletions(-)
testcases/kernel/syscalls/dup2/dup205.c +3 -8
index 0b324531f..30526fda6 100644
@@ -1,45 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
  /*
+ * Copyright (c) International Business Machines  Corp., 2002
   *
- *   Copyright (c) International Business Machines  Corp., 2002
- *
- *   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,

testcases/kernel/syscalls/dup2/dup205.c line 8

Quoted 10 lines
testcases/kernel/syscalls/dup2/dup205.c +0 -7
- *   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
   */
 
  /* Ported from SPIE, section2/iosuite/dup6.c, by Airong Zhang */

I think we need a comment for this test's aim.
It looks like dup205 checks whether hit the error when we just consume max open file descriptors. And it is similar as shmget03/msgget03.

Quoted 28 lines
testcases/kernel/syscalls/dup2/dup205.c +2 -19
-/*======================================================================
-	=================== TESTPLAN SEGMENT ===================
->KEYS:<  dup2()
->WHAT:<  Does dup return -1 on the 21st file?
->HOW:<  Create up to _NFILE files and check for -1 return on the
-	<  next attempt
-	<  Should check NOFILE as well as _NFILE.  19-Jun-84 Dale.
->BUGS:<
-======================================================================*/
-
-#include<sys/param.h>
-#include<sys/types.h>
-#include<sys/stat.h>
-#include<errno.h>
-#include<fcntl.h>
+#include<stdlib.h>
  #include<stdio.h>
-#include<unistd.h>
-#include "test.h"
+#include "tst_test.h"
 
-char *TCID = "dup205";
-int TST_TOTAL = 1;
  int *fildes;
  int min;
  int local_flag;

testcases/kernel/syscalls/dup2/dup205.c int local_flag; · line 20

Quoted 3 lines
testcases/kernel/syscalls/dup2/dup205.c +0 -0
@@ -47,88 +18,76 @@ int local_flag;
  #define PASSED 1
  #define FAILED 0
 

local_flag, PASSED,FAILED macro are useless. We can remove them directly.

Quoted 55 lines
testcases/kernel/syscalls/dup2/dup205.c +14 -31
-static void setup(void);
-static void cleanup(void);
+static void setup(void)
+{
+	min = getdtablesize();	/* get number of files allowed open */
+	fildes = malloc((min + 10) * sizeof(int));
+	if (fildes == NULL)
+		tst_brk(TBROK | TERRNO, "malloc error");
+}
+
+static void cleanup(void)
+{
+	if (fildes != NULL)
+		free(fildes);
+}
 
-int main(int ac, char *av[])
+static void run(void)
  {
  	int ifile;
  	char pfilname[40];
  	int serrno;
 
-	int lc;
-
  	ifile = -1;
 
-	tst_parse_opts(ac, av, NULL, NULL);
-
  	local_flag = PASSED;
 
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		sprintf(pfilname, "./dup205.%d\n", getpid());
-		unlink(pfilname);
-		serrno = 0;
-		if ((fildes[0] = creat(pfilname, 0666)) == -1)
-			tst_brkm(TBROK | TERRNO, cleanup, "creat failed");
-		else {
-			fildes[fildes[0]] = fildes[0];
-			for (ifile = fildes[0] + 1; ifile<  min + 10; ifile++) {
-				if ((fildes[ifile] = dup2(fildes[ifile - 1],
-							  ifile)) == -1) {
-					serrno = errno;
-					break;
-				} else {
-					if (fildes[ifile] != ifile) {
-						tst_brkm(TFAIL, cleanup,
-							 "got wrong descriptor "
-							 "number back (%d != %d)",
-							 fildes[ifile], ifile);
-					}
-				}

testcases/kernel/syscalls/dup2/dup205.c int local_flag;

Quoted 10 lines
testcases/kernel/syscalls/dup2/dup205.c +0 -10
-			}	/* end for */
-			if (ifile<  min) {
-				tst_resm(TFAIL, "Not enough files duped");
-				local_flag = FAILED;
-			} else if (ifile>  min) {
-				tst_resm(TFAIL, "Too many files duped");
-				local_flag = FAILED;
-			}
-			if (serrno != EBADF&&  serrno != EMFILE&&
-			    serrno != EINVAL)

{

Quoted 7 lines
testcases/kernel/syscalls/dup2/dup205.c +5 -2
-				tst_resm(TFAIL, "bad errno on dup2 failure");
-				local_flag = FAILED;
+	sprintf(pfilname, "./dup205.%d\n", getpid());
+	unlink(pfilname);
+	serrno = 0;
+
+	fildes[0] = creat(pfilname, 0666);

testcases/kernel/syscalls/dup2/dup205.c int local_flag; · line 59

Quoted 10 lines
testcases/kernel/syscalls/dup2/dup205.c +9 -0
+	if (fildes[0] == -1)
+		tst_brk(TBROK | TERRNO, "creat failed");
+	else {
+		fildes[fildes[0]] = fildes[0];
+		for (ifile = fildes[0] + 1; ifile<  min + 10; ifile++) {
+			fildes[ifile] = dup2(fildes[ifile - 1], ifile);
+			if (fildes[ifile] == -1) {
+				serrno = errno;
+				break;
  			}

We can use test macro, so we don't need serrno variable.

Quoted 11 lines
testcases/kernel/syscalls/dup2/dup205.c +11 -0
+			if (fildes[ifile] != ifile)
+				tst_brk(TFAIL, "got wrong descriptor "
+					"number back (%d != %d)",
+					fildes[ifile], ifile);
+		}	/* end for */
+		if (ifile<  min) {
+			tst_res(TFAIL, "Not enough files duped");
+			local_flag = FAILED;
+		} else if (ifile>  min) {
+			tst_res(TFAIL, "Too many files duped");
+			local_flag = FAILED;

testcases/kernel/syscalls/dup2/dup205.c int local_flag; · line 73

Quoted 10 lines
testcases/kernel/syscalls/dup2/dup205.c +2 -7
  		}
-		unlink(pfilname);
-		for (ifile = fildes[0]; ifile<  min + 10; ifile++)
-			close(fildes[ifile]);
-		if (local_flag == PASSED) {
-			tst_resm(TPASS, "Test passed.");
-		} else {
-			tst_resm(TFAIL, "Test failed.");
+		if (serrno != EBADF&&  serrno != EMFILE&&
+			    serrno != EINVAL) {

I don't understand why check three errnos and need a comment here On my machine, this case fails with EBADF.

Best Regards
Yang Xu

Quoted 39 lines
testcases/kernel/syscalls/dup2/dup205.c +16 -19
+			tst_res(TFAIL, "bad errno on dup2 failure");
+			local_flag = FAILED;
  		}
-
  	}
-	cleanup();
-	tst_exit();
-}
-
-static void setup(void)
-{
-	tst_tmpdir();
-
-	min = getdtablesize();	/* get number of files allowed open */
-	fildes = malloc((min + 10) * sizeof(int));
-	if (fildes == NULL)
-		tst_brkm(TBROK | TERRNO, cleanup, "malloc error");
+	unlink(pfilname);
+	for (ifile = fildes[0]; ifile<  min + 10; ifile++)
+		close(fildes[ifile]);
+	if (local_flag == PASSED) {
+		tst_res(TPASS, "Test passed.");
+	} else {
+			tst_res(TFAIL, "Test failed.");
+	}
  }
 
-static void cleanup(void)
-{
-	if (fildes != NULL)
-		free(fildes);
-	tst_rmdir();
-}
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.test_all = run,
+	.setup = setup,
+	.cleanup = cleanup,
+};