Skip to contentSkip to conversation

dup07.c: Rewrite using new LTP API

PATCH Superseded Avinesh Kumar 3 participants 3 messages Jul 6, 2022 – Jul 11, 2022

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dup07.c: Rewrite using new LTP API

---
 testcases/kernel/syscalls/dup/dup07.c | 180 +++++++++-----------------
 1 file changed, 59 insertions(+), 121 deletions(-)
testcases/kernel/syscalls/dup/dup07.c +59 -121
index a100f5d58..b696d54e0 100644
@@ -1,142 +1,80 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
- *
  *   Copyright (c) International Business Machines  Corp., 2002
  *    ported from SPIE, section2/iosuite/dup3.c, by Airong Zhang
  *   Copyright (c) 2013 Cyril Hrubis <[email protected]>
- *
- *   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
+ *   Copyright (c) 2022 SUSE LLC Avinesh Kumar <[email protected]>
  */
 
-/*
-  WHAT:  Is the access mode the same for both file descriptors?
-          0: read only?
-          1: write only?
-          2: read/write?
-  HOW:   Creat a file with each access mode; dup each file descriptor;
-         stat each file descriptor and compare mode of each pair
-*/
-
-#include <stdio.h>
-#include <fcntl.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include "test.h"
+/*\
+ * [Description]
+ *
+ * Verify that new file descriptor allocated by dup() has the same
+ * permissions mode as oldfd.
+ */
 
-char *TCID = "dup07";
-int TST_TOTAL = 3;
+#include "tst_test.h"
 
-static const char *testfile = "dup07";
+static const char *temp_file1 = "tmpfile1";
+static const char *temp_file2 = "tmpfile2";
+static const char *temp_file3 = "tmpfile3";
 
-static void setup(void);
-static void cleanup(void);
+static int rdo_fd, wro_fd, rdwr_fd;
+static struct stat rdo_st_buf, wro_st_buf, rdwr_st_buf;
 
-int main(int ac, char **av)
+static void setup(void)
 {
-	struct stat retbuf;
-	struct stat dupbuf;
-	int rdoret, wroret, rdwret;
-	int duprdo, dupwro, duprdwr;
-
-	int lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
+	rdo_fd = SAFE_CREAT(temp_file1, 0444);
+	wro_fd = SAFE_CREAT(temp_file2, 0222);
+	rdwr_fd = SAFE_CREAT(temp_file3, 0666);
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		if ((rdoret = creat(testfile, 0444)) == -1) {
-			tst_resm(TFAIL, "Unable to creat file '%s'", testfile);
-		} else {
-			if ((duprdo = dup(rdoret)) == -1) {
-				tst_resm(TFAIL, "Unable to dup '%s'", testfile);
-			} else {
-				fstat(rdoret, &retbuf);
-				fstat(duprdo, &dupbuf);
-				if (retbuf.st_mode != dupbuf.st_mode) {
-					tst_resm(TFAIL,
-						 "rdonly and dup do not match");
-				} else {
-					tst_resm(TPASS,
-					         "Passed in read mode.");
-				}
-				close(duprdo);
-			}
-			close(rdoret);
-		}
-
-		unlink(testfile);
-		
-		if ((wroret = creat(testfile, 0222)) == -1) {
-			tst_resm(TFAIL, "Unable to creat file '%s'", testfile);
-		} else {
-			if ((dupwro = dup(wroret)) == -1) {
-				tst_resm(TFAIL, "Unable to dup '%s'", testfile);
-			} else {
-				fstat(wroret, &retbuf);
-				fstat(dupwro, &dupbuf);
-				if (retbuf.st_mode != dupbuf.st_mode) {
-					tst_resm(TFAIL,
-						 "wronly and dup do not match");
-				} else {
-					tst_resm(TPASS,
-					         "Passed in write mode.");
-				}
-				close(dupwro);
-			}
-			close(wroret);
-
-		}
-
-		unlink(testfile);
+	SAFE_FSTAT(rdo_fd, &rdo_st_buf);
+	SAFE_FSTAT(wro_fd, &wro_st_buf);
+	SAFE_FSTAT(rdwr_fd, &rdwr_st_buf);
+}
 
-		if ((rdwret = creat(testfile, 0666)) == -1) {
-			tst_resm(TFAIL, "Unable to creat file '%s'", testfile);
-		} else {
-			if ((duprdwr = dup(rdwret)) == -1) {
-				tst_resm(TFAIL, "Unable to dup '%s'", testfile);
-			} else {
-				fstat(rdwret, &retbuf);
-				fstat(duprdwr, &dupbuf);
-				if (retbuf.st_mode != dupbuf.st_mode) {
-					tst_resm(TFAIL,
-						 "rdwr and dup do not match");
-				} else {
-					tst_resm(TPASS,
-					         "Passed in read/write mode.");
-				}
-				close(duprdwr);
-			}
-			close(rdwret);
-		}
-		
-		unlink(testfile);
+static void run(void)
+{
+	struct stat dup_st_buf;
+
+	TEST(dup(rdo_fd));
+	if (TST_RET == -1)
+		tst_res(TFAIL | TERRNO, "Unable to dup '%s'", temp_file1);
+	else {
+		SAFE_FSTAT(TST_RET, &dup_st_buf);
+		TST_EXP_EQ_LI(rdo_st_buf.st_mode, dup_st_buf.st_mode);
+		SAFE_CLOSE(TST_RET);
 	}
 
-	cleanup();
-	tst_exit();
-}
+	TEST(dup(wro_fd));
+	if (TST_RET == -1)
+		tst_res(TFAIL | TERRNO, "Unable to dup '%s'", temp_file2);
+	else {
+		SAFE_FSTAT(TST_RET, &dup_st_buf);
+		TST_EXP_EQ_LI(wro_st_buf.st_mode, dup_st_buf.st_mode);
+		SAFE_CLOSE(TST_RET);
+	}
 
-static void setup(void)
-{
-	tst_tmpdir();
+	TEST(dup(rdwr_fd));
+	if (TST_RET == -1)
+		tst_res(TFAIL | TERRNO, "Unable to dup '%s'", temp_file3);
+	else {
+		SAFE_FSTAT(TST_RET, &dup_st_buf);
+		TST_EXP_EQ_LI(rdwr_st_buf.st_mode, dup_st_buf.st_mode);
+		SAFE_CLOSE(TST_RET);
+	}
 }
 
 static void cleanup(void)
 {
-	tst_rmdir();
+	SAFE_CLOSE(rdo_fd);
+	SAFE_CLOSE(wro_fd);
+	SAFE_CLOSE(rdwr_fd);
 }
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_tmpdir = 1
+};
Signature · 5 lines
2.36.1


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

Hi Avinesh

I did't look this cleanup patch, but I see the old code is same as dup202.c(This case covers more situation, ie using chmod). Maybe we can just add a test_variant to use dup syscall in dup202.c?

Then remove this case directly.

Best Regards
Yang Xu

Quoted 211 lines
---
  testcases/kernel/syscalls/dup/dup07.c | 180 +++++++++-----------------
  1 file changed, 59 insertions(+), 121 deletions(-)
testcases/kernel/syscalls/dup/dup07.c +59 -121
index a100f5d58..b696d54e0 100644
@@ -1,142 +1,80 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
  /*
- *
   *   Copyright (c) International Business Machines  Corp., 2002
   *    ported from SPIE, section2/iosuite/dup3.c, by Airong Zhang
   *   Copyright (c) 2013 Cyril Hrubis<[email protected]>
- *
- *   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
+ *   Copyright (c) 2022SUSE LLC Avinesh Kumar<[email protected]>
   */
 
-/*
-  WHAT:  Is the access mode the same for both file descriptors?
-          0: read only?
-          1: write only?
-          2: read/write?
-  HOW:   Creat a file with each access mode; dup each file descriptor;
-         stat each file descriptor and compare mode of each pair
-*/
-
-#include<stdio.h>
-#include<fcntl.h>
-#include<sys/types.h>
-#include<sys/stat.h>
-#include<sys/types.h>
-#include<sys/stat.h>
-#include "test.h"
+/*\
+ * [Description]
+ *
+ * Verify that new file descriptor allocated by dup() has the same
+ * permissions mode as oldfd.
+ */
 
-char *TCID = "dup07";
-int TST_TOTAL = 3;
+#include "tst_test.h"
 
-static const char *testfile = "dup07";
+static const char *temp_file1 = "tmpfile1";
+static const char *temp_file2 = "tmpfile2";
+static const char *temp_file3 = "tmpfile3";
 
-static void setup(void);
-static void cleanup(void);
+static int rdo_fd, wro_fd, rdwr_fd;
+static struct stat rdo_st_buf, wro_st_buf, rdwr_st_buf;
 
-int main(int ac, char **av)
+static void setup(void)
  {
-	struct stat retbuf;
-	struct stat dupbuf;
-	int rdoret, wroret, rdwret;
-	int duprdo, dupwro, duprdwr;
-
-	int lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
+	rdo_fd = SAFE_CREAT(temp_file1, 0444);
+	wro_fd = SAFE_CREAT(temp_file2, 0222);
+	rdwr_fd = SAFE_CREAT(temp_file3, 0666);
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		if ((rdoret = creat(testfile, 0444)) == -1) {
-			tst_resm(TFAIL, "Unable to creat file '%s'", testfile);
-		} else {
-			if ((duprdo = dup(rdoret)) == -1) {
-				tst_resm(TFAIL, "Unable to dup '%s'", testfile);
-			} else {
-				fstat(rdoret,&retbuf);
-				fstat(duprdo,&dupbuf);
-				if (retbuf.st_mode != dupbuf.st_mode) {
-					tst_resm(TFAIL,
-						 "rdonly and dup do not match");
-				} else {
-					tst_resm(TPASS,
-					         "Passed in read mode.");
-				}
-				close(duprdo);
-			}
-			close(rdoret);
-		}
-
-		unlink(testfile);
-		
-		if ((wroret = creat(testfile, 0222)) == -1) {
-			tst_resm(TFAIL, "Unable to creat file '%s'", testfile);
-		} else {
-			if ((dupwro = dup(wroret)) == -1) {
-				tst_resm(TFAIL, "Unable to dup '%s'", testfile);
-			} else {
-				fstat(wroret,&retbuf);
-				fstat(dupwro,&dupbuf);
-				if (retbuf.st_mode != dupbuf.st_mode) {
-					tst_resm(TFAIL,
-						 "wronly and dup do not match");
-				} else {
-					tst_resm(TPASS,
-					         "Passed in write mode.");
-				}
-				close(dupwro);
-			}
-			close(wroret);
-
-		}
-
-		unlink(testfile);
+	SAFE_FSTAT(rdo_fd,&rdo_st_buf);
+	SAFE_FSTAT(wro_fd,&wro_st_buf);
+	SAFE_FSTAT(rdwr_fd,&rdwr_st_buf);
+}
 
-		if ((rdwret = creat(testfile, 0666)) == -1) {
-			tst_resm(TFAIL, "Unable to creat file '%s'", testfile);
-		} else {
-			if ((duprdwr = dup(rdwret)) == -1) {
-				tst_resm(TFAIL, "Unable to dup '%s'", testfile);
-			} else {
-				fstat(rdwret,&retbuf);
-				fstat(duprdwr,&dupbuf);
-				if (retbuf.st_mode != dupbuf.st_mode) {
-					tst_resm(TFAIL,
-						 "rdwr and dup do not match");
-				} else {
-					tst_resm(TPASS,
-					         "Passed in read/write mode.");
-				}
-				close(duprdwr);
-			}
-			close(rdwret);
-		}
-		
-		unlink(testfile);
+static void run(void)
+{
+	struct stat dup_st_buf;
+
+	TEST(dup(rdo_fd));
+	if (TST_RET == -1)
+		tst_res(TFAIL | TERRNO, "Unable to dup '%s'", temp_file1);
+	else {
+		SAFE_FSTAT(TST_RET,&dup_st_buf);
+		TST_EXP_EQ_LI(rdo_st_buf.st_mode, dup_st_buf.st_mode);
+		SAFE_CLOSE(TST_RET);
  	}
 
-	cleanup();
-	tst_exit();
-}
+	TEST(dup(wro_fd));
+	if (TST_RET == -1)
+		tst_res(TFAIL | TERRNO, "Unable to dup '%s'", temp_file2);
+	else {
+		SAFE_FSTAT(TST_RET,&dup_st_buf);
+		TST_EXP_EQ_LI(wro_st_buf.st_mode, dup_st_buf.st_mode);
+		SAFE_CLOSE(TST_RET);
+	}
 
-static void setup(void)
-{
-	tst_tmpdir();
+	TEST(dup(rdwr_fd));
+	if (TST_RET == -1)
+		tst_res(TFAIL | TERRNO, "Unable to dup '%s'", temp_file3);
+	else {
+		SAFE_FSTAT(TST_RET,&dup_st_buf);
+		TST_EXP_EQ_LI(rdwr_st_buf.st_mode, dup_st_buf.st_mode);
+		SAFE_CLOSE(TST_RET);
+	}
  }
 
  static void cleanup(void)
  {
-	tst_rmdir();
+	SAFE_CLOSE(rdo_fd);
+	SAFE_CLOSE(wro_fd);
+	SAFE_CLOSE(rdwr_fd);
  }
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_tmpdir = 1
+};
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Hi Avinesh, Xu,

Quoted 7 lines

Hi Avinesh

I did't look this cleanup patch, but I see the old code is same as dup202.c(This case covers more situation, ie using chmod). Maybe we can just add a test_variant to use dup syscall in dup202.c?

Then remove this case directly.

+1, yes, the code in both tests indeed does the same.

Kind regards,
Petr

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