All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH] open04.c: convert to new LTP API
@ 2022-07-14  6:07 Avinesh Kumar
  2022-09-01  6:32 ` Petr Vorel
  0 siblings, 1 reply; 5+ messages in thread
From: Avinesh Kumar @ 2022-07-14  6:07 UTC (permalink / raw)
  To: ltp

Signed-off-by: Avinesh Kumar <akumar@suse.de>
---
 testcases/kernel/syscalls/open/open04.c | 144 ++++++------------------
 1 file changed, 34 insertions(+), 110 deletions(-)

diff --git a/testcases/kernel/syscalls/open/open04.c b/testcases/kernel/syscalls/open/open04.c
index 7b3b5eb6f..e7cb533fe 100644
--- a/testcases/kernel/syscalls/open/open04.c
+++ b/testcases/kernel/syscalls/open/open04.c
@@ -1,133 +1,57 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
- *
  *   Copyright (c) International Business Machines  Corp., 2001
- *
- *   This program is free software;  you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License as published by
- *   the Free Software Foundation; either version 2 of the License, or
- *   (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
- *   the GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program;  if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *   Copyright (c) 2022 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
  */
 
-/*
- * DESCRIPTION
- *	Testcase to check that open(2) sets EMFILE if a process opens files
- *	more than its descriptor size
+/*\
+ * [Description]
  *
- * ALGORITHM
- *	First get the file descriptor table size which is set for a process.
- *	Use open(2) for creating files till the descriptor table becomes full.
- *	These open(2)s should succeed. Finally use open(2) to open another
- *	file. This attempt should fail with EMFILE.
+ * Verify that open(2) fails with EMFILE when
+ * per-process limit on the number of open file descriptors has been reached.
  */
 
 #include <stdio.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include "test.h"
-
-char *TCID = "open04";
-int TST_TOTAL = 1;
-
-static int fd, ifile, mypid, first;
-static int nfile;
-static int *buf;
-static char fname[40];
+#include "tst_test.h"
 
-static void setup(void);
-static void cleanup(void);
+static int fds_limit, first, i;
+static int *fds;
+static char fname[20];
 
-int main(int ac, char **av)
+static void setup(void)
 {
-	int lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
+	int fd;
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
+	fds_limit = getdtablesize();
+	first = SAFE_OPEN("open04", O_RDWR | O_CREAT, 0777);
 
-		TEST(open(fname, O_RDWR | O_CREAT, 0777));
+	fds = SAFE_MALLOC(sizeof(int) * (fds_limit - first));
+	fds[0] = first;
 
-		if (TEST_RETURN != -1) {
-			tst_resm(TFAIL, "call succeeded unexpectedly");
-			continue;
-		}
-
-		if (TEST_ERRNO != EMFILE)
-			tst_resm(TFAIL, "Expected EMFILE, got %d", TEST_ERRNO);
-		else
-			tst_resm(TPASS, "call returned expected EMFILE error");
+	for (i = first + 1; i < fds_limit; i++) {
+		sprintf(fname, "open04.%d", i);
+		fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0777);
+		fds[i - first] = fd;
 	}
-
-	close(first);
-	close(fd);
-	cleanup();
-	tst_exit();
 }
 
-static void setup(void)
+static void run(void)
 {
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	/* make a temporary directory and cd to it */
-	tst_tmpdir();
-
-	mypid = getpid();
-	nfile = getdtablesize();
-	sprintf(fname, "open04.%d", mypid);
-
-	first = fd = open(fname, O_RDWR | O_CREAT, 0777);
-	if (first == -1)
-		tst_brkm(TBROK, cleanup, "Cannot open first file");
-
-	close(fd);
-	close(first);
-	unlink(fname);
-
-	/* Allocate memory for stat and ustat structure variables */
-	buf = malloc(sizeof(int) * nfile - first);
-	if (buf == NULL)
-		tst_brkm(TBROK, NULL, "Failed to allocate Memory");
-
-	for (ifile = first; ifile <= nfile; ifile++) {
-		sprintf(fname, "open04.%d.%d", ifile, mypid);
-		fd = open(fname, O_RDWR | O_CREAT, 0777);
-		if (fd == -1) {
-			if (errno != EMFILE) {
-				tst_brkm(TBROK, cleanup, "Expected EMFILE got "
-					 "%d", errno);
-			}
-			break;
-		}
-		buf[ifile - first] = fd;
-	}
+	sprintf(fname, "open04.%d", fds_limit);
+	TST_EXP_FAIL2(open(fname, O_RDWR | O_CREAT, 0777),
+				EMFILE,
+				"open(%s, O_RDWR | O_CREAT, 0777)", fname);
 }
 
 static void cleanup(void)
 {
-	close(first);
-
-	for (ifile = first; ifile < nfile; ifile++) {
-		sprintf(fname, "open04.%d.%d", ifile, mypid);
-		close(buf[ifile - first]);
-		unlink(fname);
-	}
-
-	free(buf);
-
-	/* delete the test directory created in setup() */
-	tst_rmdir();
+	for (i = first; i < fds_limit; i++)
+		SAFE_CLOSE(fds[i - first]);
 }
+
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_tmpdir = 1
+};
-- 
2.36.1


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

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

* Re: [LTP] [PATCH] open04.c: convert to new LTP API
  2022-07-14  6:07 [LTP] [PATCH] open04.c: convert to new LTP API Avinesh Kumar
@ 2022-09-01  6:32 ` Petr Vorel
  2022-09-01 11:39   ` Petr Vorel
  2022-09-01 16:07   ` Avinesh Kumar
  0 siblings, 2 replies; 5+ messages in thread
From: Petr Vorel @ 2022-09-01  6:32 UTC (permalink / raw)
  To: Avinesh Kumar; +Cc: ltp

Hi Avinesh,

I suggest to merge with these fixes:

* added free (if needed - that was in the original source)
* fixed cleanup (don't run SAFE_CLOSE() if previous SAFE_OPEN() or SAFE_MALLOC()
  failed)
* use TST_EXP_FAIL() - the same result as TST_EXP_FAIL2() with shorter code
* allocate memory needed (it's actually -2)
* #define FNAME "open04"

If it's ok, I'll merge it.

Kind regards,
Petr

diff --git testcases/kernel/syscalls/open/open04.c testcases/kernel/syscalls/open/open04.c
index e7cb533fe..16477e459 100644
--- testcases/kernel/syscalls/open/open04.c
+++ testcases/kernel/syscalls/open/open04.c
@@ -1,19 +1,22 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
 /*
- *   Copyright (c) International Business Machines  Corp., 2001
- *   Copyright (c) 2022 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
+ * Copyright (c) International Business Machines  Corp., 2001
+ * Copyright (c) 2022 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
  */
 
 /*\
  * [Description]
  *
- * Verify that open(2) fails with EMFILE when
- * per-process limit on the number of open file descriptors has been reached.
+ * Verify that open(2) fails with EMFILE when per-process limit on the number
+ * of open file descriptors has been reached.
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include "tst_test.h"
 
+#define FNAME "open04"
+
 static int fds_limit, first, i;
 static int *fds;
 static char fname[20];
@@ -23,13 +26,13 @@ static void setup(void)
 	int fd;
 
 	fds_limit = getdtablesize();
-	first = SAFE_OPEN("open04", O_RDWR | O_CREAT, 0777);
+	first = SAFE_OPEN(FNAME, O_RDWR | O_CREAT, 0777);
 
-	fds = SAFE_MALLOC(sizeof(int) * (fds_limit - first));
+	fds = SAFE_MALLOC(sizeof(int) * (fds_limit - first - 2));
 	fds[0] = first;
 
 	for (i = first + 1; i < fds_limit; i++) {
-		sprintf(fname, "open04.%d", i);
+		sprintf(fname, FNAME ".%d", i);
 		fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0777);
 		fds[i - first] = fd;
 	}
@@ -37,16 +40,20 @@ static void setup(void)
 
 static void run(void)
 {
-	sprintf(fname, "open04.%d", fds_limit);
-	TST_EXP_FAIL2(open(fname, O_RDWR | O_CREAT, 0777),
-				EMFILE,
-				"open(%s, O_RDWR | O_CREAT, 0777)", fname);
+	sprintf(fname, FNAME ".%d", fds_limit);
+	TST_EXP_FAIL(open(fname, O_RDWR | O_CREAT, 0777), EMFILE);
 }
 
 static void cleanup(void)
 {
+	if (!first || !fds)
+		return;
+
 	for (i = first; i < fds_limit; i++)
 		SAFE_CLOSE(fds[i - first]);
+
+	if (fds)
+		free(fds);
 }
 
 static struct tst_test test = {

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

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

* Re: [LTP] [PATCH] open04.c: convert to new LTP API
  2022-09-01  6:32 ` Petr Vorel
@ 2022-09-01 11:39   ` Petr Vorel
  2022-09-01 16:07   ` Avinesh Kumar
  1 sibling, 0 replies; 5+ messages in thread
From: Petr Vorel @ 2022-09-01 11:39 UTC (permalink / raw)
  To: Avinesh Kumar, ltp

Hi Avinesh,

> I suggest to merge with these fixes:
...
> * use TST_EXP_FAIL() - the same result as TST_EXP_FAIL2() with shorter code
I'm sorry I was completely wrong, TST_EXP_FAIL2() is correct here (I forget it
is about >0 return value, not about parameters).

Kind regards,
Petr

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

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

* Re: [LTP] [PATCH] open04.c: convert to new LTP API
  2022-09-01  6:32 ` Petr Vorel
  2022-09-01 11:39   ` Petr Vorel
@ 2022-09-01 16:07   ` Avinesh Kumar
  2022-09-01 20:49     ` Petr Vorel
  1 sibling, 1 reply; 5+ messages in thread
From: Avinesh Kumar @ 2022-09-01 16:07 UTC (permalink / raw)
  To: Petr Vorel; +Cc: ltp

Hi Petr,

On Thursday, September 1, 2022 12:02:55 PM IST Petr Vorel wrote:
> Hi Avinesh,
> 
> I suggest to merge with these fixes:
> 
> * added free (if needed - that was in the original source)
> * fixed cleanup (don't run SAFE_CLOSE() if previous SAFE_OPEN() or SAFE_MALLOC()
>   failed)
> * use TST_EXP_FAIL() - the same result as TST_EXP_FAIL2() with shorter code
TST_EXP_FAIL2() is correct here as you mentioned in previous reply.
> * allocate memory needed (it's actually -2)
fds_limit(1024) - first(3) is correct as we need to allocate memory for1021 int fds.
Test executes fine even with (fds_limit - first - 2) probably because malloc allocating
more than requested memory.

> * #define FNAME "open04"
> 
> If it's ok, I'll merge it.
please go ahead and merge with rest of the suggestions, or let me know if I should
sent v2.


Thanks,
Avinesh



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

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

* Re: [LTP] [PATCH] open04.c: convert to new LTP API
  2022-09-01 16:07   ` Avinesh Kumar
@ 2022-09-01 20:49     ` Petr Vorel
  0 siblings, 0 replies; 5+ messages in thread
From: Petr Vorel @ 2022-09-01 20:49 UTC (permalink / raw)
  To: Avinesh Kumar; +Cc: ltp

Hi Avinesh,

> > I suggest to merge with these fixes:

> > * added free (if needed - that was in the original source)
> > * fixed cleanup (don't run SAFE_CLOSE() if previous SAFE_OPEN() or SAFE_MALLOC()
> >   failed)
> > * use TST_EXP_FAIL() - the same result as TST_EXP_FAIL2() with shorter code
> TST_EXP_FAIL2() is correct here as you mentioned in previous reply.

> > * allocate memory needed (it's actually -2)
> fds_limit(1024) - first(3) is correct as we need to allocate memory for1021 int fds.
> Test executes fine even with (fds_limit - first - 2) probably because malloc allocating
> more than requested memory.

Yes, thank you for correcting me, I forget the basic (the alignment of
dynamically allocating memory).

> > * #define FNAME "open04"

> > If it's ok, I'll merge it.
> please go ahead and merge with rest of the suggestions, or let me know if I should
> sent v2.

Merged, thanks!

Kind regards,
Petr

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

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

end of thread, other threads:[~2022-09-01 20:49 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-07-14  6:07 [LTP] [PATCH] open04.c: convert to new LTP API Avinesh Kumar
2022-09-01  6:32 ` Petr Vorel
2022-09-01 11:39   ` Petr Vorel
2022-09-01 16:07   ` Avinesh Kumar
2022-09-01 20:49     ` Petr Vorel

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.