All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH] dup06: Convert to new API
@ 2024-01-09  9:50 Shiyang Ruan
  2024-01-09 10:27 ` [LTP] [PATCH] dup07: " Shiyang Ruan
  2024-01-10 18:29 ` [LTP] [PATCH] dup06: " Petr Vorel
  0 siblings, 2 replies; 6+ messages in thread
From: Shiyang Ruan @ 2024-01-09  9:50 UTC (permalink / raw)
  To: ltp

Signed-off-by: Shiyang Ruan <ruansy.fnst@fujitsu.com>
---
 testcases/kernel/syscalls/dup/dup06.c | 141 ++++++++++----------------
 1 file changed, 52 insertions(+), 89 deletions(-)

diff --git a/testcases/kernel/syscalls/dup/dup06.c b/testcases/kernel/syscalls/dup/dup06.c
index e3f8070bf..84fc260a1 100644
--- a/testcases/kernel/syscalls/dup/dup06.c
+++ b/testcases/kernel/syscalls/dup/dup06.c
@@ -1,42 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
- *   Copyright (c) International Business Machines  Corp., 2002
- *    ported from SPIE, section2/iosuite/dup1.c, by Airong Zhang
- *   Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
- *
- *   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) International Business Machines  Corp., 2002
+ *  ported from SPIE, section2/iosuite/dup1.c, by Airong Zhang
+ * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
  */
 
-/*
-  WHAT:  Does dup return -1 on the 21st file?
-  HOW:   Create up to _NFILE (20) files and check for -1 return on the
-         next attempt
-         Should check NOFILE as well as _NFILE.  19-Jun-84 Dale.
-*/
-
-#include <stdio.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/param.h>
-#include "test.h"
+/*\
+ * [Description]
+ *
+ * Test for dup(2) syscall with max open file descriptors.
+ */
 
-char *TCID = "dup06";
-int TST_TOTAL = 1;
+#include <stdlib.h>
+#include "tst_test.h"
 
+static int *pfildes;
+static int minfd, maxfd, freefds;
+static char pfilname[40];
 static int cnt_free_fds(int maxfd)
 {
 	int freefds = 0;
@@ -48,67 +28,50 @@ static int cnt_free_fds(int maxfd)
 	return (freefds);
 }
 
-static void setup(void);
-static void cleanup(void);
-
-int main(int ac, char **av)
+static void setup(void)
 {
-	int *fildes, i;
-	int min;
-	int freefds;
-	int lc;
-	const char *pfilname = "dup06";
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	min = getdtablesize();
-	freefds = cnt_free_fds(min);
-	fildes = malloc((min + 5) * sizeof(int));
-
-	for (i = 0; i < min + 5; i++)
-		fildes[i] = 0;
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		unlink(pfilname);
-
-		if ((fildes[0] = creat(pfilname, 0666)) == -1) {
-			tst_resm(TFAIL, "Cannot open first file");
-		} else {
-			for (i = 1; i < min + 5; i++) {
-				if ((fildes[i] = dup(fildes[i - 1])) == -1)
-					break;
-			}
-			if (i < freefds) {
-				tst_resm(TFAIL, "Not enough files duped");
-			} else if (i > freefds) {
-				tst_resm(TFAIL, "Too many files duped");
-			} else {
-				tst_resm(TPASS, "Test passed.");
-			}
-		}
+	minfd = getdtablesize();	/* get number of files allowed open */
+	maxfd = minfd + 5;
+	freefds = cnt_free_fds(minfd);
+	pfildes = SAFE_MALLOC(maxfd * sizeof(int));
+	memset(pfildes, -1, maxfd * sizeof(int));
+	sprintf(pfilname, "./dup06.%d\n", getpid());
+}
 
-		unlink(pfilname);
+static void cleanup(void)
+{
+	if (pfildes != NULL)
+		free(pfildes);
+}
 
-		for (i = 0; i < min + 5; i++) {
-			if (fildes[i] != 0 && fildes[i] != -1)
-				close(fildes[i]);
+static void run(void)
+{
+	int i;
 
-			fildes[i] = 0;
-		}
+	pfildes[0] = SAFE_CREAT(pfilname, 0666);
+	for (i = 1; i < maxfd; i++) {
+		if ((pfildes[i] = dup(pfildes[i - 1])) == -1)
+			break;
+	}
+	if (i < freefds) {
+		tst_res(TFAIL, "Not enough files duped");
+	} else if (i > freefds) {
+		tst_res(TFAIL, "Too many files duped");
+	} else {
+		tst_res(TPASS, "Test passed.");
 	}
 
-	cleanup();
-	tst_exit();
-}
+	SAFE_UNLINK(pfilname);
 
-static void setup(void)
-{
-	tst_tmpdir();
+	for (i = 0; i < maxfd; i++) {
+		if (pfildes[i] != 0 && pfildes[i] != -1)
+			SAFE_CLOSE(pfildes[i]);
+	}
 }
 
-static void cleanup(void)
-{
-	tst_rmdir();
-}
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.test_all = run,
+	.setup = setup,
+	.cleanup = cleanup,
+};
-- 
2.34.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [LTP] [PATCH] dup07: Convert to new API
  2024-01-09  9:50 [LTP] [PATCH] dup06: Convert to new API Shiyang Ruan
@ 2024-01-09 10:27 ` Shiyang Ruan
  2024-01-10 18:29 ` [LTP] [PATCH] dup06: " Petr Vorel
  1 sibling, 0 replies; 6+ messages in thread
From: Shiyang Ruan @ 2024-01-09 10:27 UTC (permalink / raw)
  To: ltp

Signed-off-by: Shiyang Ruan <ruansy.fnst@fujitsu.com>
---
 testcases/kernel/syscalls/dup/dup07.c | 160 +++++++-------------------
 1 file changed, 41 insertions(+), 119 deletions(-)

diff --git a/testcases/kernel/syscalls/dup/dup07.c b/testcases/kernel/syscalls/dup/dup07.c
index a100f5d58..bccee0776 100644
--- a/testcases/kernel/syscalls/dup/dup07.c
+++ b/testcases/kernel/syscalls/dup/dup07.c
@@ -1,22 +1,8 @@
+// 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 <chrubis@suse.cz>
- *
- *   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) International Business Machines  Corp., 2002
+ *  ported from SPIE, section2/iosuite/dup3.c, by Airong Zhang
+ * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
  */
 
 /*
@@ -27,116 +13,52 @@
   HOW:   Creat a file with each access mode; dup each file descriptor;
          stat each file descriptor and compare mode of each pair
 */
+/*\
+ * [Description]
+ *
+ * Verify that the file descriptor created by dup(2) syscall has the same
+ * access mode as the old one.
+ */
 
-#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"
-
-char *TCID = "dup07";
-int TST_TOTAL = 3;
+#include "tst_test.h"
 
 static const char *testfile = "dup07";
 
-static void setup(void);
-static void cleanup(void);
+static struct tcase {
+	char *mode_desc;
+	int mode;
+} tcases[] = {
+	{"read only", 0444},
+	{"write only", 0222},
+	{"read/write", 0666},
+};
 
-int main(int ac, char **av)
+static void run(unsigned int n)
 {
-	struct stat retbuf;
-	struct stat dupbuf;
-	int rdoret, wroret, rdwret;
-	int duprdo, dupwro, duprdwr;
-
-	int lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		if ((rdoret = creat(testfile, 0444)) == -1) {
-			tst_resm(TFAIL, "Unable to creat file '%s'", testfile);
+	int oldfd, dupfd;
+	struct stat oldbuf, dupbuf;
+	struct tcase *tc = &tcases[n];
+
+	oldfd = SAFE_CREAT(testfile, tc->mode);
+	dupfd = TST_EXP_FD_SILENT(dup(oldfd), "dup() %s file", tc->mode_desc);
+	if (TST_PASS) {
+		SAFE_FSTAT(oldfd, &oldbuf);
+		SAFE_FSTAT(dupfd, &dupbuf);
+
+		if (oldbuf.st_mode != dupbuf.st_mode) {
+			tst_res(TFAIL, "%s and dup do not match", tc->mode_desc);
 		} 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);
+			tst_res(TPASS, "Passed in %s mode.", tc->mode_desc);
 		}
-
-		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);
-
-		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);
+		SAFE_CLOSE(dupfd);
 	}
+	SAFE_CLOSE(oldfd);
 
-	cleanup();
-	tst_exit();
-}
-
-static void setup(void)
-{
-	tst_tmpdir();
+	SAFE_UNLINK(testfile);
 }
 
-static void cleanup(void)
-{
-	tst_rmdir();
-}
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.test = run,
+	.tcnt = ARRAY_SIZE(tcases),
+};
-- 
2.34.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [LTP] [PATCH] dup06: Convert to new API
  2024-01-09  9:50 [LTP] [PATCH] dup06: Convert to new API Shiyang Ruan
  2024-01-09 10:27 ` [LTP] [PATCH] dup07: " Shiyang Ruan
@ 2024-01-10 18:29 ` Petr Vorel
  2024-01-11  2:10   ` Shiyang Ruan
  2024-01-11  2:19   ` [LTP] [PATCH v2 1/2] " Shiyang Ruan
  1 sibling, 2 replies; 6+ messages in thread
From: Petr Vorel @ 2024-01-10 18:29 UTC (permalink / raw)
  To: Shiyang Ruan; +Cc: ltp

Hi Shiyang,

> Signed-off-by: Shiyang Ruan <ruansy.fnst@fujitsu.com>
> ---
>  testcases/kernel/syscalls/dup/dup06.c | 141 ++++++++++----------------
>  1 file changed, 52 insertions(+), 89 deletions(-)

> diff --git a/testcases/kernel/syscalls/dup/dup06.c b/testcases/kernel/syscalls/dup/dup06.c
> index e3f8070bf..84fc260a1 100644
> --- a/testcases/kernel/syscalls/dup/dup06.c
> +++ b/testcases/kernel/syscalls/dup/dup06.c
> @@ -1,42 +1,22 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
>  /*
> - *   Copyright (c) International Business Machines  Corp., 2002
> - *    ported from SPIE, section2/iosuite/dup1.c, by Airong Zhang
> - *   Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
...
> + * Copyright (c) International Business Machines  Corp., 2002
> + *  ported from SPIE, section2/iosuite/dup1.c, by Airong Zhang
> + * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
Could you please also add your or LTP copyright for this rewrite?
E.g.
* Copyright (c) Linux Test Project, 2003-2015
+ your copyright, or just:
* Copyright (c) Linux Test Project, 2003-2024

$ make check-dup06
CHECK testcases/kernel/syscalls/dup/dup06.c
dup06.c:28: ERROR: return is not a function, parentheses are not required
dup06.c:53: ERROR: do not use assignment in if condition
dup06.c:56: WARNING: braces {} are not necessary for any arm of this statement

Therefore I'm going to merge with following change (+ please let me know if you
want to add your copyright).

Reviewed-by: Petr Vorel <pvorel@suse.cz>

Kind regards,
Petr

diff --git testcases/kernel/syscalls/dup/dup06.c testcases/kernel/syscalls/dup/dup06.c
index 84fc260a1..e7e27b8f9 100644
--- testcases/kernel/syscalls/dup/dup06.c
+++ testcases/kernel/syscalls/dup/dup06.c
@@ -1,8 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) International Business Machines  Corp., 2002
- *  ported from SPIE, section2/iosuite/dup1.c, by Airong Zhang
+ * ported from SPIE, section2/iosuite/dup1.c, by Airong Zhang
  * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
+ * Copyright (c) Linux Test Project, 2003-2024
  */
 
 /*\
@@ -17,6 +18,7 @@
 static int *pfildes;
 static int minfd, maxfd, freefds;
 static char pfilname[40];
+
 static int cnt_free_fds(int maxfd)
 {
 	int freefds = 0;
@@ -25,7 +27,7 @@ static int cnt_free_fds(int maxfd)
 		if (fcntl(maxfd, F_GETFD) == -1 && errno == EBADF)
 			freefds++;
 
-	return (freefds);
+	return freefds;
 }
 
 static void setup(void)
@@ -50,16 +52,17 @@ static void run(void)
 
 	pfildes[0] = SAFE_CREAT(pfilname, 0666);
 	for (i = 1; i < maxfd; i++) {
-		if ((pfildes[i] = dup(pfildes[i - 1])) == -1)
+		pfildes[i] = dup(pfildes[i - 1]);
+		if (pfildes[i] == -1)
 			break;
 	}
-	if (i < freefds) {
+
+	if (i < freefds)
 		tst_res(TFAIL, "Not enough files duped");
-	} else if (i > freefds) {
+	else if (i > freefds)
 		tst_res(TFAIL, "Too many files duped");
-	} else {
-		tst_res(TPASS, "Test passed.");
-	}
+	else
+		tst_res(TPASS, "Test passed");
 
 	SAFE_UNLINK(pfilname);
 

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [LTP] [PATCH] dup06: Convert to new API
  2024-01-10 18:29 ` [LTP] [PATCH] dup06: " Petr Vorel
@ 2024-01-11  2:10   ` Shiyang Ruan
  2024-01-11  2:19   ` [LTP] [PATCH v2 1/2] " Shiyang Ruan
  1 sibling, 0 replies; 6+ messages in thread
From: Shiyang Ruan @ 2024-01-11  2:10 UTC (permalink / raw)
  To: Petr Vorel; +Cc: ltp



在 2024/1/11 2:29, Petr Vorel 写道:
> Hi Shiyang,
> 
>> Signed-off-by: Shiyang Ruan <ruansy.fnst@fujitsu.com>
>> ---
>>   testcases/kernel/syscalls/dup/dup06.c | 141 ++++++++++----------------
>>   1 file changed, 52 insertions(+), 89 deletions(-)
> 
>> diff --git a/testcases/kernel/syscalls/dup/dup06.c b/testcases/kernel/syscalls/dup/dup06.c
>> index e3f8070bf..84fc260a1 100644
>> --- a/testcases/kernel/syscalls/dup/dup06.c
>> +++ b/testcases/kernel/syscalls/dup/dup06.c
>> @@ -1,42 +1,22 @@
>> +// SPDX-License-Identifier: GPL-2.0-or-later
>>   /*
>> - *   Copyright (c) International Business Machines  Corp., 2002
>> - *    ported from SPIE, section2/iosuite/dup1.c, by Airong Zhang
>> - *   Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
> ...
>> + * Copyright (c) International Business Machines  Corp., 2002
>> + *  ported from SPIE, section2/iosuite/dup1.c, by Airong Zhang
>> + * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
> Could you please also add your or LTP copyright for this rewrite?
> E.g.
> * Copyright (c) Linux Test Project, 2003-2015
> + your copyright, or just:
> * Copyright (c) Linux Test Project, 2003-2024
> 
> $ make check-dup06
> CHECK testcases/kernel/syscalls/dup/dup06.c
> dup06.c:28: ERROR: return is not a function, parentheses are not required
> dup06.c:53: ERROR: do not use assignment in if condition
> dup06.c:56: WARNING: braces {} are not necessary for any arm of this statement
> 
> Therefore I'm going to merge with following change (+ please let me know if you
> want to add your copyright).
> 
> Reviewed-by: Petr Vorel <pvorel@suse.cz>

Thanks, adding LTP copyright is fine.

Let me send a new version of these two patches because the next one has 
the same issue.  I've fixed it.


--
Ruan.

> 
> Kind regards,
> Petr
> 
> diff --git testcases/kernel/syscalls/dup/dup06.c testcases/kernel/syscalls/dup/dup06.c
> index 84fc260a1..e7e27b8f9 100644
> --- testcases/kernel/syscalls/dup/dup06.c
> +++ testcases/kernel/syscalls/dup/dup06.c
> @@ -1,8 +1,9 @@
>   // SPDX-License-Identifier: GPL-2.0-or-later
>   /*
>    * Copyright (c) International Business Machines  Corp., 2002
> - *  ported from SPIE, section2/iosuite/dup1.c, by Airong Zhang
> + * ported from SPIE, section2/iosuite/dup1.c, by Airong Zhang
>    * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
> + * Copyright (c) Linux Test Project, 2003-2024
>    */
>   
>   /*\
> @@ -17,6 +18,7 @@
>   static int *pfildes;
>   static int minfd, maxfd, freefds;
>   static char pfilname[40];
> +
>   static int cnt_free_fds(int maxfd)
>   {
>   	int freefds = 0;
> @@ -25,7 +27,7 @@ static int cnt_free_fds(int maxfd)
>   		if (fcntl(maxfd, F_GETFD) == -1 && errno == EBADF)
>   			freefds++;
>   
> -	return (freefds);
> +	return freefds;
>   }
>   
>   static void setup(void)
> @@ -50,16 +52,17 @@ static void run(void)
>   
>   	pfildes[0] = SAFE_CREAT(pfilname, 0666);
>   	for (i = 1; i < maxfd; i++) {
> -		if ((pfildes[i] = dup(pfildes[i - 1])) == -1)
> +		pfildes[i] = dup(pfildes[i - 1]);
> +		if (pfildes[i] == -1)
>   			break;
>   	}
> -	if (i < freefds) {
> +
> +	if (i < freefds)
>   		tst_res(TFAIL, "Not enough files duped");
> -	} else if (i > freefds) {
> +	else if (i > freefds)
>   		tst_res(TFAIL, "Too many files duped");
> -	} else {
> -		tst_res(TPASS, "Test passed.");
> -	}
> +	else
> +		tst_res(TPASS, "Test passed");
>   
>   	SAFE_UNLINK(pfilname);
>   

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [LTP] [PATCH v2 1/2] dup06: Convert to new API
  2024-01-10 18:29 ` [LTP] [PATCH] dup06: " Petr Vorel
  2024-01-11  2:10   ` Shiyang Ruan
@ 2024-01-11  2:19   ` Shiyang Ruan
  2024-01-11  2:19     ` [LTP] [PATCH v2 2/2] dup07: " Shiyang Ruan
  1 sibling, 1 reply; 6+ messages in thread
From: Shiyang Ruan @ 2024-01-11  2:19 UTC (permalink / raw)
  To: ltp

Signed-off-by: Shiyang Ruan <ruansy.fnst@fujitsu.com>
---
 testcases/kernel/syscalls/dup/dup06.c | 145 ++++++++++----------------
 1 file changed, 55 insertions(+), 90 deletions(-)

diff --git a/testcases/kernel/syscalls/dup/dup06.c b/testcases/kernel/syscalls/dup/dup06.c
index e3f8070bf..e7dfbbac8 100644
--- a/testcases/kernel/syscalls/dup/dup06.c
+++ b/testcases/kernel/syscalls/dup/dup06.c
@@ -1,41 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
- *   Copyright (c) International Business Machines  Corp., 2002
- *    ported from SPIE, section2/iosuite/dup1.c, by Airong Zhang
- *   Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
- *
- *   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) International Business Machines  Corp., 2002
+ * Ported from SPIE, section2/iosuite/dup1.c, by Airong Zhang
+ * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
+ * Copyright (c) Linux Test Project, 2003-2024
  */
 
-/*
-  WHAT:  Does dup return -1 on the 21st file?
-  HOW:   Create up to _NFILE (20) files and check for -1 return on the
-         next attempt
-         Should check NOFILE as well as _NFILE.  19-Jun-84 Dale.
-*/
+/*\
+ * [Description]
+ *
+ * Test for dup(2) syscall with max open file descriptors.
+ */
 
-#include <stdio.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/param.h>
-#include "test.h"
+#include <stdlib.h>
+#include "tst_test.h"
 
-char *TCID = "dup06";
-int TST_TOTAL = 1;
+static int *pfildes;
+static int minfd, maxfd, freefds;
+static char pfilname[40];
 
 static int cnt_free_fds(int maxfd)
 {
@@ -45,70 +27,53 @@ static int cnt_free_fds(int maxfd)
 		if (fcntl(maxfd, F_GETFD) == -1 && errno == EBADF)
 			freefds++;
 
-	return (freefds);
+	return freefds;
 }
 
-static void setup(void);
-static void cleanup(void);
-
-int main(int ac, char **av)
+static void setup(void)
 {
-	int *fildes, i;
-	int min;
-	int freefds;
-	int lc;
-	const char *pfilname = "dup06";
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	min = getdtablesize();
-	freefds = cnt_free_fds(min);
-	fildes = malloc((min + 5) * sizeof(int));
-
-	for (i = 0; i < min + 5; i++)
-		fildes[i] = 0;
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		unlink(pfilname);
-
-		if ((fildes[0] = creat(pfilname, 0666)) == -1) {
-			tst_resm(TFAIL, "Cannot open first file");
-		} else {
-			for (i = 1; i < min + 5; i++) {
-				if ((fildes[i] = dup(fildes[i - 1])) == -1)
-					break;
-			}
-			if (i < freefds) {
-				tst_resm(TFAIL, "Not enough files duped");
-			} else if (i > freefds) {
-				tst_resm(TFAIL, "Too many files duped");
-			} else {
-				tst_resm(TPASS, "Test passed.");
-			}
-		}
-
-		unlink(pfilname);
-
-		for (i = 0; i < min + 5; i++) {
-			if (fildes[i] != 0 && fildes[i] != -1)
-				close(fildes[i]);
-
-			fildes[i] = 0;
-		}
-	}
-
-	cleanup();
-	tst_exit();
+	minfd = getdtablesize();	/* get number of files allowed open */
+	maxfd = minfd + 5;
+	freefds = cnt_free_fds(minfd);
+	pfildes = SAFE_MALLOC(maxfd * sizeof(int));
+	memset(pfildes, -1, maxfd * sizeof(int));
+	sprintf(pfilname, "./dup06.%d\n", getpid());
 }
 
-static void setup(void)
+static void cleanup(void)
 {
-	tst_tmpdir();
+	if (pfildes != NULL)
+		free(pfildes);
 }
 
-static void cleanup(void)
+static void run(void)
 {
-	tst_rmdir();
+	int i;
+
+	pfildes[0] = SAFE_CREAT(pfilname, 0666);
+	for (i = 1; i < maxfd; i++) {
+		pfildes[i] = dup(pfildes[i - 1]);
+		if (pfildes[i] == -1)
+			break;
+	}
+	if (i < freefds)
+		tst_res(TFAIL, "Not enough files duped");
+	else if (i > freefds)
+		tst_res(TFAIL, "Too many files duped");
+	else
+		tst_res(TPASS, "Test passed.");
+
+	SAFE_UNLINK(pfilname);
+
+	for (i = 0; i < maxfd; i++) {
+		if (pfildes[i] != 0 && pfildes[i] != -1)
+			SAFE_CLOSE(pfildes[i]);
+	}
 }
+
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.test_all = run,
+	.setup = setup,
+	.cleanup = cleanup,
+};
-- 
2.34.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [LTP] [PATCH v2 2/2] dup07: Convert to new API
  2024-01-11  2:19   ` [LTP] [PATCH v2 1/2] " Shiyang Ruan
@ 2024-01-11  2:19     ` Shiyang Ruan
  0 siblings, 0 replies; 6+ messages in thread
From: Shiyang Ruan @ 2024-01-11  2:19 UTC (permalink / raw)
  To: ltp

Signed-off-by: Shiyang Ruan <ruansy.fnst@fujitsu.com>
---
 testcases/kernel/syscalls/dup/dup07.c | 173 +++++++-------------------
 1 file changed, 44 insertions(+), 129 deletions(-)

diff --git a/testcases/kernel/syscalls/dup/dup07.c b/testcases/kernel/syscalls/dup/dup07.c
index a100f5d58..fd7792133 100644
--- a/testcases/kernel/syscalls/dup/dup07.c
+++ b/testcases/kernel/syscalls/dup/dup07.c
@@ -1,142 +1,57 @@
+// 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 <chrubis@suse.cz>
- *
- *   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) International Business Machines  Corp., 2002
+ * Ported from SPIE, section2/iosuite/dup3.c, by Airong Zhang
+ * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
+ * Copyright (c) Linux Test Project, 2003-2024
  */
 
-/*
-  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 the file descriptor created by dup(2) syscall has the same
+ * access mode as the old one.
+ */
 
-char *TCID = "dup07";
-int TST_TOTAL = 3;
+#include "tst_test.h"
 
 static const char *testfile = "dup07";
 
-static void setup(void);
-static void cleanup(void);
+static struct tcase {
+	char *mode_desc;
+	int mode;
+} tcases[] = {
+	{"read only", 0444},
+	{"write only", 0222},
+	{"read/write", 0666},
+};
 
-int main(int ac, char **av)
+static void run(unsigned int n)
 {
-	struct stat retbuf;
-	struct stat dupbuf;
-	int rdoret, wroret, rdwret;
-	int duprdo, dupwro, duprdwr;
-
-	int lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	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);
-
-		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);
+	int oldfd, dupfd;
+	struct stat oldbuf, dupbuf;
+	struct tcase *tc = &tcases[n];
+
+	oldfd = SAFE_CREAT(testfile, tc->mode);
+	dupfd = TST_EXP_FD_SILENT(dup(oldfd), "dup() %s file", tc->mode_desc);
+	if (TST_PASS) {
+		SAFE_FSTAT(oldfd, &oldbuf);
+		SAFE_FSTAT(dupfd, &dupbuf);
+
+		if (oldbuf.st_mode != dupbuf.st_mode)
+			tst_res(TFAIL, "%s and dup do not match", tc->mode_desc);
+		else
+			tst_res(TPASS, "Passed in %s mode.", tc->mode_desc);
+
+		SAFE_CLOSE(dupfd);
 	}
+	SAFE_CLOSE(oldfd);
 
-	cleanup();
-	tst_exit();
+	SAFE_UNLINK(testfile);
 }
 
-static void setup(void)
-{
-	tst_tmpdir();
-}
-
-static void cleanup(void)
-{
-	tst_rmdir();
-}
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.test = run,
+	.tcnt = ARRAY_SIZE(tcases),
+};
-- 
2.34.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-01-11  2:20 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-09  9:50 [LTP] [PATCH] dup06: Convert to new API Shiyang Ruan
2024-01-09 10:27 ` [LTP] [PATCH] dup07: " Shiyang Ruan
2024-01-10 18:29 ` [LTP] [PATCH] dup06: " Petr Vorel
2024-01-11  2:10   ` Shiyang Ruan
2024-01-11  2:19   ` [LTP] [PATCH v2 1/2] " Shiyang Ruan
2024-01-11  2:19     ` [LTP] [PATCH v2 2/2] dup07: " Shiyang Ruan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.