Skip to contentSkip to conversation

open04: add EMFILE check

PATCH In review Li Wang 3 participants 9 messages Sep 15, 2022 – Sep 20, 2022

lore
Share & tools
lore

Li Wang

author
#0 lore raw

PATCH 1/1 open04: add EMFILE check

[pre-release testing fix]

Test in automation easily get EMFILE error before reaching the fds_limit, but hard to reproduce it again manually. The possible reason is that some shared fd being opened in the parent shell and occupying the fd numbers which inherited by test then results in open failed with EMFILE early.

This patch adds back of the EMFILE check in the open() loops, to flexible test fd limitation.

open04.c:36: TBROK: open(open04.1020,66,0777) failed: EMFILE (24) open04.c:53: TWARN: close(0) failed: EBADF (9)

---
 testcases/kernel/syscalls/open/open04.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
testcases/kernel/syscalls/open/open04.c +6 -1
index d452405d4..01a8b12d6 100644
@@ -33,7 +33,12 @@ static void setup(void)
 
 	for (i = first + 1; i < fds_limit; i++) {
 		sprintf(fname, FNAME ".%d", i);
-		fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0777);
+		fd = open(fname, O_RDWR | O_CREAT, 0777);
+		if (fd == -1) {
+			if (errno != EMFILE)
+				tst_brk(TBROK, "Expected EMFILE but got %d", errno);
+			break;
+		}
 		fds[i - first] = fd;
 	}
 }
Signature · 5 lines
2.35.3


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

Hi LI,

Quoted 26 lines

[pre-release testing fix]

Test in automation easily get EMFILE error before reaching the fds_limit, but hard to reproduce it again manually. The possible reason is that some shared fd being opened in the parent shell and occupying the fd numbers which inherited by test then results in open failed with EMFILE early.

This patch adds back of the EMFILE check in the open() loops, to flexible test fd limitation.

open04.c:36: TBROK: open(open04.1020,66,0777) failed: EMFILE (24) open04.c:53: TWARN: close(0) failed: EBADF (9)

---
 testcases/kernel/syscalls/open/open04.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
testcases/kernel/syscalls/open/open04.c +0 -0
index d452405d4..01a8b12d6 100644
@@ -33,7 +33,12 @@ static void setup(void)
 
 	for (i = first + 1; i < fds_limit; i++) {
 		sprintf(fname, FNAME ".%d", i);

testcases/kernel/syscalls/open/open04.c static void setup(void) · line 44

Quoted 10 lines
testcases/kernel/syscalls/open/open04.c +6 -1
-		fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0777);
+		fd = open(fname, O_RDWR | O_CREAT, 0777);
+		if (fd == -1) {
+			if (errno != EMFILE)
+				tst_brk(TBROK, "Expected EMFILE but got %d", errno);
+			break;
+		}
 		fds[i - first] = fd;
 	}
 }

LGTM.

Kind regards,
Petr

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

Hi!

Quoted 20 lines
Quoted 8 lines
testcases/kernel/syscalls/open/open04.c +0 -0
index d452405d4..01a8b12d6 100644
@@ -33,7 +33,12 @@ static void setup(void)
 
 	for (i = first + 1; i < fds_limit; i++) {
 		sprintf(fname, FNAME ".%d", i);

testcases/kernel/syscalls/open/open04.c static void setup(void) · line 44

Quoted 10 lines
testcases/kernel/syscalls/open/open04.c +6 -1
-		fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0777);
+		fd = open(fname, O_RDWR | O_CREAT, 0777);
+		if (fd == -1) {
+			if (errno != EMFILE)
+				tst_brk(TBROK, "Expected EMFILE but got %d", errno);
+			break;
+		}
 		fds[i - first] = fd;
 	}
 }

LGTM.

Quoted 1 line

I faintly remmeber a similar patch where we decided not to work around for a test harness leaking filedescriptors into testcases.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#3 lore raw
Quoted 22 lines · Cyril Hrubis

Hi!

Quoted 1 line

Review

Quoted 1 line
(quoted diff) +0 -0

b/testcases/kernel/syscalls/open/open04.c

Quoted 5 lines
Quoted 4 lines

index d452405d4..01a8b12d6 100644

testcases/kernel/syscalls/open/open04.c +0 -0
@@ -33,7 +33,12 @@ static void setup(void)
Quoted 7 lines

testcases/kernel/syscalls/open/open04.c static void setup(void)

Quoted 7 lines
testcases/kernel/syscalls/open/open04.c +4 -1
    for (i = first + 1; i < fds_limit; i++) {
            sprintf(fname, FNAME ".%d", i);
-           fd = SAFE_OPEN(fname, O_RDWR | O_CREAT, 0777);
+           fd = open(fname, O_RDWR | O_CREAT, 0777);
+           if (fd == -1) {
+                   if (errno != EMFILE)
+                           tst_brk(TBROK, "Expected EMFILE but got

%d", errno);

Quoted 6 lines
Quoted 5 lines
testcases/kernel/syscalls/open/open04.c +2 -0
+                   break;
+           }
            fds[i - first] = fd;
    }
 }
Quoted 6 lines
Quoted 2 lines

LGTM.

I faintly remmeber a similar patch where we decided not to work around for a test harness leaking filedescriptors into testcases.

This also should be a solution, I searched the mailing list and got a patch[1].
Do you mean adding that close-on-exec flag when opening fd in harness?

[1] https://lists.linux.it/pipermail/ltp/2020-November/019650.html

Signature · 2 lines
Regards,
Li Wang
Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

Hi!

Quoted 7 lines
Quoted 3 lines

I faintly remmeber a similar patch where we decided not to work around for a test harness leaking filedescriptors into testcases.

This also should be a solution, I searched the mailing list and got a patch[1].
Do you mean adding that close-on-exec flag when opening fd in harness?

Yes, that way you can be sure that no file descriptors are leaked to the tests.

Signature · 5 lines
Cyril Hrubis
[email protected]

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

Cyril Hrubis <[email protected]> wrote:

Hi!

Quoted 11 lines
Quoted 7 lines
Quoted 3 lines

I faintly remmeber a similar patch where we decided not to work around for a test harness leaking filedescriptors into testcases.

This also should be a solution, I searched the mailing list and got a patch[1].
Do you mean adding that close-on-exec flag when opening fd in harness?

Yes, that way you can be sure that no file descriptors are leaked to the tests.

Ok, should I send patch v2 like this below?

Note: the automation test open04 got passed but I'm not sure if this has a side effect on logs. But from my observation, some tests (with old-API) log can't be collected anymore.

pan/ltp-pan.c +1 -1
@@ -443,7 +443,7 @@ int main(int argc, char **argv)
        }
 
        if (outputfilename) {
-               if (!freopen(outputfilename, "a+", stdout)) {
+               if (!freopen(outputfilename, "a+e", stdout)) {
                        fprintf(stderr,
                                "pan(%s): Error %s (%d) opening output file

'%s'\n",
panname, strerror(errno), errno,
@@ -565,7 +565,7 @@ int main(int argc, char **argv)

                } else if (starts == -1)        //wjh
                {
                        FILE *f = (FILE *) - 1;
-                       if ((f = fopen(PAN_STOP_FILE, "r")) != 0) {
+                       if ((f = fopen(PAN_STOP_FILE, "r+")) != 0) {
                                printf("Got %s Stopping!\n", PAN_STOP_FILE);
                                fclose(f);
                                unlink(PAN_STOP_FILE);
@@ -1277,7 +1277,7 @@ static char *slurp(char *file)
        int fd;
        struct stat sbuf;

- if ((fd = open(file, O_RDONLY)) < 0) {

+       if ((fd = open(file, O_RDONLY | O_CLOEXEC)) < 0) {
                fprintf(stderr,
                        "pan(%s): open(%s,O_RDONLY) failed.  errno:%d
 %s\n",
                        panname, file, errno, strerror(errno));
@@ -1372,7 +1372,7 @@ static void write_kmsg(const char *fmt, ...)
        FILE *kmsg;
        va_list ap;

- if ((kmsg = fopen("/dev/kmsg", "r+")) == NULL) {

+       if ((kmsg = fopen("/dev/kmsg", "r+e")) == NULL) {
                fprintf(stderr, "Error %s: (%d) opening /dev/kmsg\n",
                                strerror(errno), errno);
                exit(1);
Signature · 2 lines
Regards,
Li Wang
Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#6 lore raw
Quoted 15 lines · Li Wang

Cyril Hrubis <[email protected]> wrote:

Hi!

Quoted 11 lines
Quoted 7 lines
Quoted 3 lines

I faintly remmeber a similar patch where we decided not to work around for a test harness leaking filedescriptors into testcases.

This also should be a solution, I searched the mailing list and got a patch[1].
Do you mean adding that close-on-exec flag when opening fd in harness?

Yes, that way you can be sure that no file descriptors are leaked to the tests.

Quoted 6 lines

Ok, should I send patch v2 like this below?

Note: the automation test open04 got passed but I'm not sure if this has a side effect on logs. But from my observation, some tests (with old-API) log can't be collected anymore.

Seems we shouldn't fix by adding 'close-on-exec' flag simply, it brings more issues to some old-API tests, I'm still looking into the problems which look like caused by ltp-pan designed.

So can we just merge the patch as the original?

Signature · 2 lines
Regards,
Li Wang
Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by
#7 lore raw

Hi!

Quoted 14 lines
Quoted 14 lines
Quoted 11 lines
Quoted 7 lines
Quoted 3 lines

I faintly remmeber a similar patch where we decided not to work around for a test harness leaking filedescriptors into testcases.

This also should be a solution, I searched the mailing list and got a patch[1].
Do you mean adding that close-on-exec flag when opening fd in harness?

Yes, that way you can be sure that no file descriptors are leaked to the tests.

Ok, should I send patch v2 like this below?

Quoted 8 lines
Quoted 4 lines

Note: the automation test open04 got passed but I'm not sure if this has a side effect on logs. But from my observation, some tests (with old-API) log can't be collected anymore.

Seems we shouldn't fix by adding 'close-on-exec' flag simply, it brings more issues to some old-API tests, I'm still looking into the problems which look like caused by ltp-pan designed.

Sound strange, what is the problem here.

Quoted 1 line

So can we just merge the patch as the original?

Sounds sensible for the release, feel free to add:

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Acked-by
#8 lore raw
In reply to Li Wang
Quoted 24 lines · Li Wang
Quoted 16 lines

Cyril Hrubis <[email protected]> wrote:

Hi!

Quoted 10 lines
Quoted 7 lines
Quoted 2 lines

I faintly remmeber a similar patch where we decided not to work around for a test harness leaking filedescriptors into testcases.

This also should be a solution, I searched the mailing list and got a patch[1].
Do you mean adding that close-on-exec flag when opening fd in harness?

Yes, that way you can be sure that no file descriptors are leaked to the tests.

Quoted 5 lines

Ok, should I send patch v2 like this below?

Note: the automation test open04 got passed but I'm not sure if this has a side effect on logs. But from my observation, some tests (with old-API) log can't be collected anymore.

Quoted 5 lines

Seems we shouldn't fix by adding 'close-on-exec' flag simply, it brings more issues to some old-API tests, I'm still looking into the problems which look like caused by ltp-pan designed.

So can we just merge the patch as the original?

Kind regards,
Petr

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