public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH] creat05: count opened fds
@ 2016-05-17 13:16 Jan Stancek
  2016-05-17 14:19 ` Cyril Hrubis
  0 siblings, 1 reply; 3+ messages in thread
From: Jan Stancek @ 2016-05-17 13:16 UTC (permalink / raw)
  To: ltp

This testcase estimates number of opened fds by opening
a new one and assuming there are no gaps. This doesn't always
hold true. There can be gaps if test harness that runs it
opens couple fds with O_CLOEXEC prior to starting creat05.
As result, testcase unexpectedly fails in setup.

This patch counts opened fds by going through /proc/self/fd entries.

Signed-off-by: Jan Stancek <jstancek@redhat.com>
---
 testcases/kernel/syscalls/creat/creat05.c | 53 +++++++++++++++++++++----------
 1 file changed, 36 insertions(+), 17 deletions(-)

diff --git a/testcases/kernel/syscalls/creat/creat05.c b/testcases/kernel/syscalls/creat/creat05.c
index 9a51b872590d..048d24329651 100644
--- a/testcases/kernel/syscalls/creat/creat05.c
+++ b/testcases/kernel/syscalls/creat/creat05.c
@@ -21,18 +21,20 @@
  * Testcase to check that creat(2) system call returns EMFILE.
  */
 
+#include <dirent.h>
 #include <stdio.h>
 #include <errno.h>
 #include <sys/types.h>
 #include <sys/time.h>
 #include <sys/resource.h>
 #include <sys/stat.h>
+#include <sys/types.h>
 #include <fcntl.h>
 #include <linux/limits.h>
 #include <unistd.h>
 #include "tst_test.h"
 
-static int first_fd, last_fd;
+static int *opened_fds, num_opened_fds;
 
 static void verify_creat(void)
 {
@@ -52,37 +54,54 @@ static void verify_creat(void)
 
 static void setup(void)
 {
-	int max_open;
-	int fd;
+	int i, max_open, opened_at_start = 0;
 	char fname[PATH_MAX];
-
-	/* create a file to get the first file descriptor available */
-	first_fd = fd = SAFE_CREAT("fname", 0666);
-	SAFE_CLOSE(fd);
-	SAFE_UNLINK("fname");
-	tst_res(TINFO, "first fd is #%d", first_fd);
+	DIR *dir;
+	struct dirent *dir_ent;
 
 	/* get the maximum number of files that we can open */
 	max_open = getdtablesize();
 	tst_res(TINFO, "getdtablesize() = %d", max_open);
+	opened_fds = SAFE_MALLOC(max_open * sizeof(int));
+
+	/* count already opened fds */
+	dir = SAFE_OPENDIR("/proc/self/fd");
+	for (dir_ent = SAFE_READDIR(dir); dir_ent != NULL;
+		dir_ent = SAFE_READDIR(dir)) {
+		/* Don't count "." or ".." */
+		if (!strcmp(dir_ent->d_name, ".")
+		    || !strcmp(dir_ent->d_name, ".."))
+			continue;
+		opened_at_start++;
+	}
+	SAFE_CLOSEDIR(dir);
+
+	/* subtract one fd for opendir() */
+	opened_at_start--;
+
+	tst_res(TINFO, "Num fds opened at start #%d", opened_at_start);
+	tst_res(TINFO, "Opening additional #%d fds",
+		max_open - opened_at_start);
 
 	/* now open as many files as we can up to max_open */
-	for (fd = first_fd; fd < max_open; fd++) {
-		snprintf(fname, sizeof(fname), "creat05_%d", fd);
-		last_fd = SAFE_CREAT(fname, 0666);
+	for (i = 0; i < max_open - opened_at_start; i++) {
+		snprintf(fname, sizeof(fname), "creat05_%d", i);
+		opened_fds[num_opened_fds++] = SAFE_CREAT(fname, 0666);
 	}
 }
 
 static void cleanup(void)
 {
-	int fd;
+	int i;
 
-	if (last_fd <= 0)
+	if (num_opened_fds == 0)
 		return;
 
-	for (fd = first_fd + 1; fd <= last_fd; fd++) {
-		if (close(fd))
-			tst_res(TWARN | TERRNO, "close(%i) failed", fd);
+	for (i = 0; i < num_opened_fds; i++) {
+		if (close(opened_fds[i])) {
+			tst_res(TWARN | TERRNO, "close(%i) failed",
+				opened_fds[i]);
+		}
 	}
 }
 
-- 
1.8.3.1


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

* [LTP] [PATCH] creat05: count opened fds
  2016-05-17 13:16 [LTP] [PATCH] creat05: count opened fds Jan Stancek
@ 2016-05-17 14:19 ` Cyril Hrubis
  2016-05-17 14:34   ` Jan Stancek
  0 siblings, 1 reply; 3+ messages in thread
From: Cyril Hrubis @ 2016-05-17 14:19 UTC (permalink / raw)
  To: ltp

Hi!
> This testcase estimates number of opened fds by opening
> a new one and assuming there are no gaps. This doesn't always
> hold true. There can be gaps if test harness that runs it
> opens couple fds with O_CLOEXEC prior to starting creat05.
> As result, testcase unexpectedly fails in setup.

As far as I understand it the O_CLOEXEC is not the problem here. It may
be reproduced even without it when the test harness leaves an open fd
after some open one, right?

Something as:

fd0 = open("foo", ...);
fd1 = open("bar", ...);
close(fd0);
fork();
if (!pid)
	exec("creat05");


And in that case the testcase can do even without listing the
/proc/self/fd. Since POSIX specifies that we get the smallest unused fd
for each creat() we can just call it in a loop until the resulting
fd == max_fd - 1. We would have to allocate the array to store the fds
so that we can close them in cleanup though.

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH] creat05: count opened fds
  2016-05-17 14:19 ` Cyril Hrubis
@ 2016-05-17 14:34   ` Jan Stancek
  0 siblings, 0 replies; 3+ messages in thread
From: Jan Stancek @ 2016-05-17 14:34 UTC (permalink / raw)
  To: ltp





----- Original Message -----
> From: "Cyril Hrubis" <chrubis@suse.cz>
> To: "Jan Stancek" <jstancek@redhat.com>
> Cc: ltp@lists.linux.it
> Sent: Tuesday, 17 May, 2016 4:19:06 PM
> Subject: Re: [PATCH] creat05: count opened fds
> 
> Hi!
> > This testcase estimates number of opened fds by opening
> > a new one and assuming there are no gaps. This doesn't always
> > hold true. There can be gaps if test harness that runs it
> > opens couple fds with O_CLOEXEC prior to starting creat05.
> > As result, testcase unexpectedly fails in setup.
> 
> As far as I understand it the O_CLOEXEC is not the problem here. It may
> be reproduced even without it when the test harness leaves an open fd
> after some open one, right?

Agreed, that is also one way to get "gap".

> 
> Something as:
> 
> fd0 = open("foo", ...);
> fd1 = open("bar", ...);
> close(fd0);
> fork();
> if (!pid)
> 	exec("creat05");
> 
> 
> And in that case the testcase can do even without listing the
> /proc/self/fd. Since POSIX specifies that we get the smallest unused fd
> for each creat() we can just call it in a loop until the resulting
> fd == max_fd - 1.

We could do that. It's quite unlikely that fd would be already in use.
I'll send v2 with this change.

Regards,
Jan

> We would have to allocate the array to store the fds
> so that we can close them in cleanup though.
> 
> --
> Cyril Hrubis
> chrubis@suse.cz
> 

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

end of thread, other threads:[~2016-05-17 14:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-05-17 13:16 [LTP] [PATCH] creat05: count opened fds Jan Stancek
2016-05-17 14:19 ` Cyril Hrubis
2016-05-17 14:34   ` Jan Stancek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox