Skip to contentSkip to conversation

dup01.c: use TST_EXP_FD macro and add inode check

PATCH In review Avinesh Kumar 2 participants 9 messages Apr 27, 2023 – May 29, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 dup01.c: use TST_EXP_FD macro and add inode check

- simplify using TST_EXP_FD() macro
- add inode comparison check for the newly allocated file descriptor
- add test description

---
 testcases/kernel/syscalls/dup/dup01.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
testcases/kernel/syscalls/dup/dup01.c +15 -10
index 74e24cc02..ce6f39ed3 100644
@@ -7,27 +7,32 @@
  *
  */
 
+/*\
+ * [Description]
+ *
+ * Verify that dup(2) syscall executes successfully and allocates
+ * a new file descriptor which refers to the same open file as oldfd.
+ */
+
 #include "tst_test.h"
 
 static int fd;
+static struct stat buf1, buf2;
 
 static void verify_dup(void)
 {
-	TEST(dup(fd));
-
-	if (TST_RET < -1) {
-		tst_res(TFAIL, "Invalid dup() return value %ld", TST_RET);
-	} else if (TST_RET == -1) {
-		tst_res(TFAIL | TTERRNO, "dup(%d) Failed", fd);
-	} else {
-		tst_res(TPASS, "dup(%d) returned %ld", fd, TST_RET);
-		SAFE_CLOSE(TST_RET);
-	}
+	TST_EXP_FD(dup(fd), "dup(%d)", fd);
+
+	SAFE_FSTAT(TST_RET, &buf2);
+	TST_EXP_EQ_LU(buf1.st_ino, buf2.st_ino);
+
+	SAFE_CLOSE(TST_RET);
 }
 
 static void setup(void)
 {
 	fd = SAFE_OPEN("dupfile", O_RDWR | O_CREAT, 0700);
+	SAFE_FSTAT(fd, &buf1);
 }
 
 static void cleanup(void)
Signature · 5 lines
2.40.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH 2/5 dup02.c: Simplify using TST_EXP macro and doc rewording

---
 testcases/kernel/syscalls/dup/dup02.c | 29 +++++----------------------
 1 file changed, 5 insertions(+), 24 deletions(-)
testcases/kernel/syscalls/dup/dup02.c +5 -24
index 528bcdbc1..1bb1d5be3 100644
@@ -8,11 +8,9 @@
  */
 /*\
  * [Description]
- * Negative test for dup(2) with bad fds.
  *
- * [Algorithm]
- * Call dup(2) with invalid argument and make sure it returns -1 with errno set
- * to EBADF.
+ * Verify that dup(2) syscall fails with errno EBADF when called with
+ * invalid value for oldfd argument.
  */
 
 #include "tst_test.h"
@@ -29,27 +27,10 @@ static void run(unsigned int n)
 {
 	struct tcase *tc = &tcases[n];
 
-	TEST(dup(tc->fd));
+	TST_EXP_FAIL2(dup(tc->fd), tc->expected_errno, "dup(%d)", tc->fd);
 
-	if (TST_RET < -1) {
-		tst_res(TFAIL | TTERRNO, "Invalid dup() return value %ld",
-			TST_RET);
-		return;
-	}
-
-	if (TST_RET == -1) {
-		if (tc->expected_errno == TST_ERR) {
-			tst_res(TPASS | TTERRNO, "dup(%d) failed as expected",
-				tc->fd);
-		} else {
-			tst_res(TFAIL | TTERRNO, "dup(%d) failed unexpectedly",
-				tc->fd);
-		}
-		return;
-	}
-
-	tst_res(TFAIL, "dup(%d) succeeded unexpectedly", tc->fd);
-	SAFE_CLOSE(TST_RET);
+	if (TST_RET != -1)
+		SAFE_CLOSE(TST_RET);
 }
 
 static struct tst_test test = {
Signature · 5 lines
2.40.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

PATCH 3/5 dup03.c: use TST_EXP macro and make check fixes

In reply to Avinesh Kumar

- use TST_EXP_FAIL2() macro for expected failure check
- make check fixes: change vars to static
- reword doc comment

---
 testcases/kernel/syscalls/dup/dup03.c | 34 +++++++--------------------
 1 file changed, 8 insertions(+), 26 deletions(-)
testcases/kernel/syscalls/dup/dup03.c +8 -26
index 0e99813f4..d59e61f2b 100644
@@ -6,48 +6,30 @@
  */
 /*\
  * [Description]
- * Negative test for dup(2) (too many fds).
  *
- * [Algorithm]
- * Open the maximum allowed number of file descriptors and then try to call
- * dup() once more and verify it fails with EMFILE.
+ * Verify that dup(2) syscall fails with errno EMFILE when the per-process
+ * limit on the number of open file descriptors has been reached.
  */
 
 #include <stdlib.h>
 #include "tst_test.h"
 
-int *fd;
-int nfds;
+static int *fd;
+static int nfds;
 
 static void run(void)
 {
-	TEST(dup(fd[0]));
+	TST_EXP_FAIL2(dup(fd[0]), EMFILE, "dup(%d)", fd[0]);
 
-	if (TST_RET < -1) {
-		tst_res(TFAIL, "Invalid dup() return value %ld", TST_RET);
-		return;
-	}
-
-	if (TST_RET == -1) {
-		if (TST_ERR == EMFILE)
-			tst_res(TPASS | TTERRNO, "dup() failed as expected");
-		else
-			tst_res(TFAIL | TTERRNO, "dup() failed unexpectedly");
-		return;
-	}
-
-	tst_res(TFAIL, "dup() succeeded unexpectedly");
-	SAFE_CLOSE(TST_RET);
+	if (TST_RET != -1)
+		SAFE_CLOSE(TST_RET);
 }
 
 static void setup(void)
 {
 	long maxfds;
 
-	maxfds = sysconf(_SC_OPEN_MAX);
-	if (maxfds == -1)
-		tst_brk(TBROK, "sysconf(_SC_OPEN_MAX) failed");
-
+	maxfds = SAFE_SYSCONF(_SC_OPEN_MAX);
 	fd = SAFE_MALLOC(maxfds * sizeof(int));
 
 	fd[0] = SAFE_OPEN("dupfile", O_RDWR | O_CREAT, 0700);
Signature · 5 lines
2.40.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

PATCH 4/5 dup04.c: use TST_EXP_FD macro and make check fixes

In reply to Avinesh Kumar

+ updade copyright

---
 testcases/kernel/syscalls/dup/dup04.c | 44 ++++++++-------------------
 1 file changed, 12 insertions(+), 32 deletions(-)
testcases/kernel/syscalls/dup/dup04.c +12 -32
index 8d45f7a9c..053fb40c2 100644
@@ -1,12 +1,12 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /*
  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
- *
  * 06/1994 AUTHOR: Richard Logan CO-PILOT: William Roske
+ * Copyright (c) 2023 SUSE LLC
  */
 
 /*\
- * [DESCRIPTION]
+ * [Description]
  *
  * Basic test for dup(2) of a system pipe descriptor.
  */
@@ -17,38 +17,18 @@
 
 #include "tst_test.h"
 
-int fd[2];
+static int fd[2];
 
 static void run(void)
 {
-	TEST(dup(fd[0]));
-
-	if (TST_RET == -1)
-		tst_res(TFAIL | TTERRNO,
-			 "dup of read side of pipe failed");
-	else {
-		tst_res(TPASS,
-			 "dup(%d) read side of syspipe returned %ld",
-			 fd[0], TST_RET);
-
-		SAFE_CLOSE(TST_RET);
-	}
-
-	TEST(dup(fd[1]));
-
-	if (TST_RET == -1) {
-		tst_res(TFAIL | TTERRNO,
-			 "dup of write side of pipe failed");
-	} else {
-		tst_res(TPASS,
-			 "dup(%d) write side of syspipe returned %ld",
-			 fd[1], TST_RET);
-
-		SAFE_CLOSE(TST_RET);
-	}
+	TST_EXP_FD(dup(fd[0]), "dup(%d) read end of the pipe", fd[0]);
+	SAFE_CLOSE(TST_RET);
+
+	TST_EXP_FD(dup(fd[1]), "dup(%d) write end of the pipe", fd[1]);
+	SAFE_CLOSE(TST_RET);
 }
 
-void setup(void)
+static void setup(void)
 {
 	fd[0] = -1;
 
@@ -56,7 +36,7 @@ void setup(void)
 }
 
 static struct tst_test test = {
-        .test_all = run,
-        .setup = setup,
-        .needs_tmpdir = 1,
+	.test_all = run,
+	.setup = setup,
+	.needs_tmpdir = 1,
 };
Signature · 5 lines
2.40.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

PATCH 5/5 dup05.c: use TST_EXP_FD() macro and make check fixes

In reply to Avinesh Kumar

+ update copyright.
+ use SAFE_OPEN() and SAFE_CLOSE()

---
 testcases/kernel/syscalls/dup/dup05.c | 40 ++++++++++-----------------
 1 file changed, 15 insertions(+), 25 deletions(-)
testcases/kernel/syscalls/dup/dup05.c +15 -25
index 362f3e170..375fa36a7 100644
@@ -1,55 +1,45 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /*
  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
- *
  * 06/1994 AUTHOR: Richard Logan CO-PILOT: William Roske
+ * Copyright (c) 2023 SUSE LLC
  */
 
 /*\
- * [DESCRIPTION]
+ * [Description]
  *
  * Basic test for dup(2) of a named pipe descriptor
  */
-#include <stdio.h>
+
 #include "tst_test.h"
 
-char Fname[255];
-int fd;
+#define Fname "dupfile"
+
+static int fd;
 
 static void run(void)
 {
-	TEST(dup(fd));
-
-	if (TST_RET == -1) {
-		tst_res(TFAIL | TTERRNO, "dup failed");
-	} else {
-		tst_res(TPASS, "dup returned %ld",
-			 TST_RET);
-
-		SAFE_CLOSE(TST_RET);
-	}
+	TST_EXP_FD(dup(fd), "dup(%d)", fd);
+	SAFE_CLOSE(TST_RET);
 }
 
-void setup(void)
+static void setup(void)
 {
 	fd = -1;
 
-	sprintf(Fname, "dupfile");
 	SAFE_MKFIFO(Fname, 0777);
-	if ((fd = open(Fname, O_RDWR, 0700)) == -1)
-		tst_brk(TBROK, "open failed");
+	fd = SAFE_OPEN(Fname, O_RDWR, 0700);
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
 	if (fd != -1)
-		if (close(fd) == -1)
-			tst_res(TWARN | TERRNO, "close failed");
+		SAFE_CLOSE(fd);
 }
 
 static struct tst_test test = {
-        .test_all = run,
-        .setup = setup,
-        .cleanup = cleanup,
+	.test_all = run,
+	.setup = setup,
+	.cleanup = cleanup,
 	.needs_tmpdir = 1,
 };
Signature · 5 lines
2.40.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

Re: [LTP] [PATCH 5/5] dup05.c: use TST_EXP_FD() macro and make check fixes

Hi Avinesh,

I slightly modified some of the commits and merged the patchset. Thanks!

Quoted 2 lines

+ update copyright.
+ use SAFE_OPEN() and SAFE_CLOSE()

...

Quoted 3 lines

/*\
- * [DESCRIPTION]
+ * [Description]

+1

testcases/kernel/syscalls/dup/dup05.c line 16

Quoted 10 lines

*
* Basic test for dup(2) of a named pipe descriptor */

testcases/kernel/syscalls/dup/dup05.c +2 -3
-#include <stdio.h>
+
 #include "tst_test.h"
 
-char Fname[255];
-int fd;
+#define Fname "dupfile"

FYI I renamed to FNAME (don't hesitate to rename constants which aren't lowercase or variables with very long name (expected_errno => exp_err - used widely in LTP).

Quoted 13 lines
testcases/kernel/syscalls/dup/dup05.c +2 -8
+
+static int fd;
 
 static void run(void)
 {
-	TEST(dup(fd));
-
-	if (TST_RET == -1) {
-		tst_res(TFAIL | TTERRNO, "dup failed");
-	} else {
-		tst_res(TPASS, "dup returned %ld",
-			 TST_RET);
-

testcases/kernel/syscalls/dup/dup05.c line 28

Quoted 10 lines
testcases/kernel/syscalls/dup/dup05.c +3 -3
-		SAFE_CLOSE(TST_RET);
-	}
+	TST_EXP_FD(dup(fd), "dup(%d)", fd);
+	SAFE_CLOSE(TST_RET);
 }
 
-void setup(void)
+static void setup(void)
 {
 	fd = -1;

FYI I moved -1 to declaration:
static int fd = -1;

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw
In reply to Avinesh Kumar

Hi Avinesh,

Quoted 2 lines
---
 testcases/kernel/syscalls/dup/dup01.c | 25 +++++++++++++++----------

...

testcases/kernel/syscalls/dup/dup01.c line 24

Quoted 3 lines

static void verify_dup(void)
{

testcases/kernel/syscalls/dup/dup01.c +1 -0
+	TST_EXP_FD(dup(fd), "dup(%d)", fd);

IMHO only this is enough:
TST_EXP_FD(dup(fd));

Kind regards,
Petr

Quoted 6 lines
testcases/kernel/syscalls/dup/dup01.c +5 -0
+
+	SAFE_FSTAT(TST_RET, &buf2);
+	TST_EXP_EQ_LU(buf1.st_ino, buf2.st_ino);
+
+	SAFE_CLOSE(TST_RET);
 }
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw

Hi Petr,

Quoted 5 lines

testcases/kernel/syscalls/dup/dup01.c line 24

Quoted 1 line
testcases/kernel/syscalls/dup/dup01.c +1 -0
+	TST_EXP_FD(dup(fd), "dup(%d)", fd);

IMHO only this is enough:
TST_EXP_FD(dup(fd));

okay, please update when you merge, if everything else is fine.

Thank you,
Avinesh

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

Hi Avinesh,

merged this one. Thanks!

Kind regards,
Petr

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