Skip to contentSkip to conversation

test: fix process dup fd close

PATCH Superseded [email protected] 3 participants 20 messages Sep 2, 2019 – Nov 15, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 test: fix process dup fd close

From: Krzysztof Kanas <[email protected]>

process_dup was intending to close it's own fd's but failed to do so

Fixes af75078fece3 ("first public release")

Reviewed-on: https://sj1git1.cavium.com/14906

---
 app/test/process.h | 40 +++++++++++++++++++++++++++++++++++-----
 1 file changed, 35 insertions(+), 5 deletions(-)
app/test/process.h +35 -5
index 128ce41219a1..2a6428f104e1 100644
@@ -11,6 +11,7 @@
 #include <stdlib.h> /* NULL */
 #include <string.h> /* strerror */
 #include <unistd.h> /* readlink */
+#include <dirent.h>
 #include <sys/wait.h>
 
 #include <rte_string_fns.h> /* strlcpy */
@@ -40,8 +41,12 @@ process_dup(const char *const argv[], int numargs, const char *env_value)
 {
 	int num;
 	char *argv_cpy[numargs + 1];
-	int i, fd, status;
+	int i, fd, fdir, status;
+	struct dirent *dirent = NULL;
+	const char *procdir = "/proc/self/fd/";
 	char path[32];
+	char *endptr;
+	DIR *dir = NULL;
 #ifdef RTE_LIBRTE_PDUMP
 	pthread_t thread;
 #endif
@@ -58,11 +63,36 @@ process_dup(const char *const argv[], int numargs, const char *env_value)
 
 		/* close all open file descriptors, check /proc/self/fd to only
 		 * call close on open fds. Exclude fds 0, 1 and 2*/
-		for (fd = getdtablesize(); fd > 2; fd-- ) {
-			snprintf(path, sizeof(path), "/proc/" exe "/fd/%d", fd);
-			if (access(path, F_OK) == 0)
-				close(fd);
+		dir = opendir(procdir);
+
+		if (dir == NULL) {
+			rte_panic("Error opening %s: %s\n", procdir,
+					strerror(errno));
+		}
+
+		fdir = dirfd(dir);
+		if (fdir < 0) {
+			status = errno;
+			closedir(dir);
+			rte_panic("Error %d obtaining fd for dir %s: %s\n",
+					fdir, procdir, strerror(status));
 		}
+
+		while ((dirent = readdir(dir)) != NULL) {
+			errno = 0;
+			fd = strtol(dirent->d_name, &endptr, 10);
+			if (errno != 0 || dirent->d_name == endptr) {
+				printf("Error converint name fd %d %s:\n",
+					fd, dirent->d_name);
+				continue;
+			}
+
+			if (fd == fdir || fd <= 2)
+				continue;
+
+			close(fd);
+		}
+
 		printf("Running binary with argv[]:");
 		for (i = 0; i < num; i++)
 			printf("'%s' ", argv_cpy[i]);
Signature · 1 line
2.21.0
#1 lore raw

PATCH v2 1/1 test: fix process dup fd close

From: Krzysztof Kanas <[email protected]>

process_dup was intending to close it's own fd's but failed to do so

Fixes af75078fece3 ("first public release")

---
v2:
* remove unnecessary commit msg information

 app/test/process.h | 40 +++++++++++++++++++++++++++++++++++-----
 1 file changed, 35 insertions(+), 5 deletions(-)
app/test/process.h +35 -5
index 128ce41219a1..2a6428f104e1 100644
@@ -11,6 +11,7 @@
 #include <stdlib.h> /* NULL */
 #include <string.h> /* strerror */
 #include <unistd.h> /* readlink */
+#include <dirent.h>
 #include <sys/wait.h>
 
 #include <rte_string_fns.h> /* strlcpy */
@@ -40,8 +41,12 @@ process_dup(const char *const argv[], int numargs, const char *env_value)
 {
 	int num;
 	char *argv_cpy[numargs + 1];
-	int i, fd, status;
+	int i, fd, fdir, status;
+	struct dirent *dirent = NULL;
+	const char *procdir = "/proc/self/fd/";
 	char path[32];
+	char *endptr;
+	DIR *dir = NULL;
 #ifdef RTE_LIBRTE_PDUMP
 	pthread_t thread;
 #endif
@@ -58,11 +63,36 @@ process_dup(const char *const argv[], int numargs, const char *env_value)
 
 		/* close all open file descriptors, check /proc/self/fd to only
 		 * call close on open fds. Exclude fds 0, 1 and 2*/
-		for (fd = getdtablesize(); fd > 2; fd-- ) {
-			snprintf(path, sizeof(path), "/proc/" exe "/fd/%d", fd);
-			if (access(path, F_OK) == 0)
-				close(fd);
+		dir = opendir(procdir);
+
+		if (dir == NULL) {
+			rte_panic("Error opening %s: %s\n", procdir,
+					strerror(errno));
+		}
+
+		fdir = dirfd(dir);
+		if (fdir < 0) {
+			status = errno;
+			closedir(dir);
+			rte_panic("Error %d obtaining fd for dir %s: %s\n",
+					fdir, procdir, strerror(status));
 		}
+
+		while ((dirent = readdir(dir)) != NULL) {
+			errno = 0;
+			fd = strtol(dirent->d_name, &endptr, 10);
+			if (errno != 0 || dirent->d_name == endptr) {
+				printf("Error converint name fd %d %s:\n",
+					fd, dirent->d_name);
+				continue;
+			}
+
+			if (fd == fdir || fd <= 2)
+				continue;
+
+			close(fd);
+		}
+
 		printf("Running binary with argv[]:");
 		for (i = 0; i < num; i++)
 			printf("'%s' ", argv_cpy[i]);
Signature · 1 line
2.21.0
#2 lore raw

Ping..

Quoted 84 lines · On 19-09-02 11:49, [email protected] wrote:

From: Krzysztof Kanas <[email protected]>

process_dup was intending to close it's own fd's but failed to do so

Fixes af75078fece3 ("first public release")

---
v2:
* remove unnecessary commit msg information

 app/test/process.h | 40 +++++++++++++++++++++++++++++++++++-----
 1 file changed, 35 insertions(+), 5 deletions(-)
app/test/process.h +35 -5
index 128ce41219a1..2a6428f104e1 100644
@@ -11,6 +11,7 @@
 #include <stdlib.h> /* NULL */
 #include <string.h> /* strerror */
 #include <unistd.h> /* readlink */
+#include <dirent.h>
 #include <sys/wait.h>
 
 #include <rte_string_fns.h> /* strlcpy */
@@ -40,8 +41,12 @@ process_dup(const char *const argv[], int numargs, const char *env_value)
 {
 	int num;
 	char *argv_cpy[numargs + 1];
-	int i, fd, status;
+	int i, fd, fdir, status;
+	struct dirent *dirent = NULL;
+	const char *procdir = "/proc/self/fd/";
 	char path[32];
+	char *endptr;
+	DIR *dir = NULL;
 #ifdef RTE_LIBRTE_PDUMP
 	pthread_t thread;
 #endif
@@ -58,11 +63,36 @@ process_dup(const char *const argv[], int numargs, const char *env_value)
 
 		/* close all open file descriptors, check /proc/self/fd to only
 		 * call close on open fds. Exclude fds 0, 1 and 2*/
-		for (fd = getdtablesize(); fd > 2; fd-- ) {
-			snprintf(path, sizeof(path), "/proc/" exe "/fd/%d", fd);
-			if (access(path, F_OK) == 0)
-				close(fd);
+		dir = opendir(procdir);
+
+		if (dir == NULL) {
+			rte_panic("Error opening %s: %s\n", procdir,
+					strerror(errno));
+		}
+
+		fdir = dirfd(dir);
+		if (fdir < 0) {
+			status = errno;
+			closedir(dir);
+			rte_panic("Error %d obtaining fd for dir %s: %s\n",
+					fdir, procdir, strerror(status));
 		}
+
+		while ((dirent = readdir(dir)) != NULL) {
+			errno = 0;
+			fd = strtol(dirent->d_name, &endptr, 10);
+			if (errno != 0 || dirent->d_name == endptr) {
+				printf("Error converint name fd %d %s:\n",
+					fd, dirent->d_name);
+				continue;
+			}
+
+			if (fd == fdir || fd <= 2)
+				continue;
+
+			close(fd);
+		}
+
 		printf("Running binary with argv[]:");
 		for (i = 0; i < num; i++)
 			printf("'%s' ", argv_cpy[i]);
Signature · 1 line
2.21.0
Signature · 3 lines
-
Regards,
Krzysztof(Chris) Kanas
#4 lore raw
Quoted 5 lines · David Marchand

External Email

----------------------------------------------------------------------

Quoted 1 line · On Mon, Sep 2, 2019 at 11:50 AM <[email protected]> wrote:
Quoted 7 lines
Quoted 3 lines

From: Krzysztof Kanas <[email protected]>

process_dup was intending to close it's own fd's but failed to do so

A bit hard to digest, what is the problem that you want to fix?

Thanks.

I don't recall the exact test name but I think it was test_eal_flags that is included in the meson test suite for fast tests. This test was timing out on ARM64.

Using strace (-f -c IIRC) showed that the process is spending most of the time in access syscall trying to close not existing fd's.

- for (fd = getdtablesize(); fd > 2; fd-- ) {
- snprintf(path, sizeof(path), "/proc/" exe "/fd/%d", fd);
- if (access(path, F_OK) == 0)
- close(fd);

So the simplest way to do so is to close only the opened fd's not blindly try every possible one.

Also I think the code was wrong in another way as it tried to iterate over /proc/ *exe* /fd/%d/ when it should iterate over /proc/ *self* /fd/, so either the comment bove the for loop was wrong or the code was incorrect.

Quoted 5 lines
Signature · 2 lines
-- 
David Marchand
Signature · 3 lines
-
Regards,
Krzysztof(Chris) Kanas
#5 lore raw
Quoted 27 lines · Krzysztof Kanas
Quoted 5 lines · David Marchand

External Email

----------------------------------------------------------------------

Quoted 1 line · On Mon, Sep 2, 2019 at 11:50 AM <[email protected]> wrote:
Quoted 7 lines
Quoted 3 lines

From: Krzysztof Kanas <[email protected]>

process_dup was intending to close it's own fd's but failed to do so

A bit hard to digest, what is the problem that you want to fix?

Thanks.

I don't recall the exact test name but I think it was test_eal_flags that is included in the meson test suite for fast tests. This test was timing out on ARM64.

Using strace (-f -c IIRC) showed that the process is spending most of the time in access syscall trying to close not existing fd's.

app/test/process.h +0 -4
-               for (fd = getdtablesize(); fd > 2; fd-- ) {
-                       snprintf(path, sizeof(path), "/proc/" exe "/fd/%d", fd);
-                       if (access(path, F_OK) == 0)
-                               close(fd);
Quoted 7 lines

So the simplest way to do so is to close only the opened fd's not blindly try every possible one.

Also I think the code was wrong in another way as it tried to iterate over /proc/ *exe* /fd/%d/ when it should iterate over /proc/ *self* /fd/, so either the comment bove the for loop was wrong or the code was incorrect.

Thanks.
I agree that this loop is terrible.
I have some comments on the patch.

Signature · 1 line
David Marchand
#6 lore raw
In reply to [email protected]
Quoted 4 lines · On Mon, Sep 2, 2019 at 11:50 AM <[email protected]> wrote:

From: Krzysztof Kanas <[email protected]>

process_dup was intending to close it's own fd's but failed to do so

Please, explain how you caught the issue and what the impact was on your system.

Quoted 2 lines
Fixes af75078fece3 ("first public release")
Quoted 22 lines

---
v2:
* remove unnecessary commit msg information

 app/test/process.h | 40 +++++++++++++++++++++++++++++++++++-----
 1 file changed, 35 insertions(+), 5 deletions(-)
app/test/process.h +1 -0
index 128ce41219a1..2a6428f104e1 100644
@@ -11,6 +11,7 @@
 #include <stdlib.h> /* NULL */
 #include <string.h> /* strerror */
 #include <unistd.h> /* readlink */
+#include <dirent.h>
 #include <sys/wait.h>
 
 #include <rte_string_fns.h> /* strlcpy */

app/test/process.h process_dup(const char *const argv[], int numargs, const char *env_value) · line 45

Quoted 6 lines
app/test/process.h +2 -1
@@ -40,8 +41,12 @@ process_dup(const char *const argv[], int numargs, const char *env_value)
 {
        int num;
        char *argv_cpy[numargs + 1];
-       int i, fd, status;
+       int i, fd, fdir, status;
+       struct dirent *dirent = NULL;

No need to initialise to NULL.

app/test/process.h process_dup(const char *const argv[], int numargs, const char *env_value) · line 46

Quoted 1 line
app/test/process.h +1 -0
+       const char *procdir = "/proc/self/fd/";

self is a Linux thing.
This won't work on FreeBSD.

app/test/process.h process_dup(const char *const argv[], int numargs, const char *env_value) · line 49

Quoted 3 lines
app/test/process.h +2 -0
        char path[32];
+       char *endptr;
+       DIR *dir = NULL;

Idem, no need to initialize.

app/test/process.h process_dup(const char *const argv[], int numargs, const char *env_value) · line 67

Quoted 13 lines
app/test/process.h +2 -4
 #ifdef RTE_LIBRTE_PDUMP
        pthread_t thread;
 #endif
@@ -58,11 +63,36 @@ process_dup(const char *const argv[], int numargs, const char *env_value)
 
                /* close all open file descriptors, check /proc/self/fd to only
                 * call close on open fds. Exclude fds 0, 1 and 2*/
-               for (fd = getdtablesize(); fd > 2; fd-- ) {
-                       snprintf(path, sizeof(path), "/proc/" exe "/fd/%d", fd);
-                       if (access(path, F_OK) == 0)
-                               close(fd);
+               dir = opendir(procdir);
+

Remove empty line.

app/test/process.h process_dup(const char *const argv[], int numargs, const char *env_value) · line 75

Quoted 8 lines
app/test/process.h +8 -0
+               if (dir == NULL) {
+                       rte_panic("Error opening %s: %s\n", procdir,
+                                       strerror(errno));
+               }
+
+               fdir = dirfd(dir);
+               if (fdir < 0) {
+                       status = errno;

No need to set status, this (forked) process will panic.

app/test/process.h process_dup(const char *const argv[], int numargs, const char *env_value) · line 84

Quoted 9 lines
app/test/process.h +8 -0
+                       closedir(dir);
+                       rte_panic("Error %d obtaining fd for dir %s: %s\n",
+                                       fdir, procdir, strerror(status));
                }
+
+               while ((dirent = readdir(dir)) != NULL) {
+                       errno = 0;
+                       fd = strtol(dirent->d_name, &endptr, 10);
+                       if (errno != 0 || dirent->d_name == endptr) {

If you want to validate that the entire string is valid:

if (errno != 0 || endptr[0] != '\0')

app/test/process.h process_dup(const char *const argv[], int numargs, const char *env_value) · line 86

Quoted 2 lines
app/test/process.h +2 -0
+                               printf("Error converint name fd %d %s:\n",
+                                       fd, dirent->d_name);

converting

app/test/process.h process_dup(const char *const argv[], int numargs, const char *env_value) · line 94

Quoted 8 lines
app/test/process.h +8 -0
+                               continue;
+                       }
+
+                       if (fd == fdir || fd <= 2)
+                               continue;
+
+                       close(fd);
+               }

Missing closedir().

Quoted 7 lines
app/test/process.h +1 -1
+
                printf("Running binary with argv[]:");
                for (i = 0; i < num; i++)
                        printf("'%s' ", argv_cpy[i]);
--

2.21.0

Signature · 1 line
David Marchand
#7 lore raw

Re: [dpdk-dev] [PATCH v3 0/3] test: fix timeout in flags autotest

Hi David,

Thanks for review, hopefully this patch will addresses most of the sutff. Rest I will address here.

Quoted 5 lines
Quoted 2 lines

+ const char *procdir = "/proc/self/fd/";

self is a Linux thing.
This won't work on FreeBSD.

IMHO original code didn't worked on FreeBSD as well. I have created function to adress in third patch

Quoted 1 line

No need to set status, this (forked) process will panic.

I am using status to save errno as it value can be overwritten on unlikely event of closedir failure.

---
v3 changes:
add missing closedir
fix strtol string validation test
removed empty line
remove unecssary initalization
add check for '.' and '..' to avoid errors in parsing entries fix commit message
add FreeBSD function for closing files
v2 changes:
fix commit message

Krzysztof Kanas (3):
test: fix timeout in flags autotest
test: move close files to separate function
test: fix FreeBSD file closing function

 process.h |  183 ++++++++++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 138 insertions(+), 45 deletions(-)
#8 lore raw

PATCH v3 1/3 test: fix timeout in flags autotest

From: Krzysztof Kanas <[email protected]>

Test eal_flags_autotest times out on 64 ARM due to excessive calls to 'access' system call. While at it fix process_dup logic so it tests correct file path for opened fd's.

Fixes af75078fece3 ("first public release")
Cc intel.com
---
 app/test/process.h | 43 ++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 38 insertions(+), 5 deletions(-)
app/test/process.h +38 -5
index 128ce41219a1..298ba288087a 100644
@@ -11,6 +11,7 @@
 #include <stdlib.h> /* NULL */
 #include <string.h> /* strerror */
 #include <unistd.h> /* readlink */
+#include <dirent.h>
 #include <sys/wait.h>
 
 #include <rte_string_fns.h> /* strlcpy */
@@ -40,8 +41,12 @@ process_dup(const char *const argv[], int numargs, const char *env_value)
 {
 	int num;
 	char *argv_cpy[numargs + 1];
-	int i, fd, status;
+	int i, fd, fdir, status;
+	struct dirent *dirent;
+	const char *procdir = "/proc/self/fd/";
 	char path[32];
+	char *endptr;
+	DIR *dir;
 #ifdef RTE_LIBRTE_PDUMP
 	pthread_t thread;
 #endif
@@ -58,11 +63,39 @@ process_dup(const char *const argv[], int numargs, const char *env_value)
 
 		/* close all open file descriptors, check /proc/self/fd to only
 		 * call close on open fds. Exclude fds 0, 1 and 2*/
-		for (fd = getdtablesize(); fd > 2; fd-- ) {
-			snprintf(path, sizeof(path), "/proc/" exe "/fd/%d", fd);
-			if (access(path, F_OK) == 0)
-				close(fd);
+		dir = opendir(procdir);
+		if (dir == NULL) {
+			rte_panic("Error opening %s: %s\n", procdir,
+					strerror(errno));
 		}
+
+		fdir = dirfd(dir);
+		if (fdir < 0) {
+			status = errno;
+			closedir(dir);
+			rte_panic("Error %d obtaining fd for dir %s: %s\n",
+					fdir, procdir, strerror(status));
+		}
+
+		while ((dirent = readdir(dir)) != NULL) {
+			if (!strcmp(".", dirent->d_name) ||
+			    !strcmp("..", dirent->d_name))
+				continue;
+
+			errno = 0;
+			fd = strtol(dirent->d_name, &endptr, 10);
+			if (errno != 0 || endptr[0] != '\0') {
+				printf("Error converint name fd %d %s:\n",
+					fd, dirent->d_name);
+				continue;
+			}
+			if (fd == fdir || fd <= 2)
+				continue;
+
+			close(fd);
+		}
+		closedir(dir);
+
 		printf("Running binary with argv[]:");
 		for (i = 0; i < num; i++)
 			printf("'%s' ", argv_cpy[i]);
Signature · 1 line
2.21.0
#9 lore raw

PATCH v3 2/3 test: move close files to separate function

In reply to [email protected]

From: Krzysztof Kanas <[email protected]>

As iterating over opened files is different on Linux and FreeBSD move to separate function.

---
 app/test/process.h | 89 ++++++++++++++++++++++++++--------------------
 1 file changed, 50 insertions(+), 39 deletions(-)
app/test/process.h +50 -39
index 298ba288087a..34afac5a1f39 100644
@@ -30,6 +30,54 @@ extern void *send_pkts(void *empty);
 extern uint16_t flag_for_send_pkts;
 #endif
 
+
+/* close all open file descriptors, check /proc/self/fd to only
+ * call close on open fds. Exclude fds 0, 1 and 2
+ */
+#ifdef RTE_EXEC_ENV_LINUX
+static inline void
+close_files(void)
+{
+	const char *procdir = "/proc/self/fd/";
+	struct dirent *dirent;
+	int fd, fdir, status;
+	char *endptr;
+	DIR *dir;
+
+	dir = opendir(procdir);
+	if (dir == NULL) {
+		rte_panic("Error opening %s: %s\n", procdir,
+				strerror(errno));
+	}
+
+	fdir = dirfd(dir);
+	if (fdir < 0) {
+		status = errno;
+		closedir(dir);
+		rte_panic("Error %d obtaining fd for dir %s: %s\n",
+				fdir, procdir, strerror(status));
+	}
+
+	while ((dirent = readdir(dir)) != NULL) {
+		if (!strcmp(".", dirent->d_name) ||
+			!strcmp("..", dirent->d_name))
+			continue;
+
+		errno = 0;
+		fd = strtol(dirent->d_name, &endptr, 10);
+		if (errno != 0 || endptr[0] != '\0') {
+			printf("Error converint name fd %d %s:\n",
+				fd, dirent->d_name);
+			continue;
+		}
+		if (fd == fdir || fd <= 2)
+			continue;
+
+		close(fd);
+	}
+	closedir(dir);
+}
+#endif
 /*
  * launches a second copy of the test process using the given argv parameters,
  * which should include argv[0] as the process name. To identify in the
@@ -41,12 +89,8 @@ process_dup(const char *const argv[], int numargs, const char *env_value)
 {
 	int num;
 	char *argv_cpy[numargs + 1];
-	int i, fd, fdir, status;
-	struct dirent *dirent;
-	const char *procdir = "/proc/self/fd/";
+	int i, status;
 	char path[32];
-	char *endptr;
-	DIR *dir;
 #ifdef RTE_LIBRTE_PDUMP
 	pthread_t thread;
 #endif
@@ -61,40 +105,7 @@ process_dup(const char *const argv[], int numargs, const char *env_value)
 		argv_cpy[i] = NULL;
 		num = numargs;
 
-		/* close all open file descriptors, check /proc/self/fd to only
-		 * call close on open fds. Exclude fds 0, 1 and 2*/
-		dir = opendir(procdir);
-		if (dir == NULL) {
-			rte_panic("Error opening %s: %s\n", procdir,
-					strerror(errno));
-		}
-
-		fdir = dirfd(dir);
-		if (fdir < 0) {
-			status = errno;
-			closedir(dir);
-			rte_panic("Error %d obtaining fd for dir %s: %s\n",
-					fdir, procdir, strerror(status));
-		}
-
-		while ((dirent = readdir(dir)) != NULL) {
-			if (!strcmp(".", dirent->d_name) ||
-			    !strcmp("..", dirent->d_name))
-				continue;
-
-			errno = 0;
-			fd = strtol(dirent->d_name, &endptr, 10);
-			if (errno != 0 || endptr[0] != '\0') {
-				printf("Error converint name fd %d %s:\n",
-					fd, dirent->d_name);
-				continue;
-			}
-			if (fd == fdir || fd <= 2)
-				continue;
-
-			close(fd);
-		}
-		closedir(dir);
+		close_files();
 
 		printf("Running binary with argv[]:");
 		for (i = 0; i < num; i++)
Signature · 1 line
2.21.0
#10 lore raw

PATCH v3 3/3 test: fix FreeBSD file closing function

In reply to [email protected]

From: Krzysztof Kanas <[email protected]>

The FreeBSD was iterating over non existing procfs entries, where sysctl could give same information.

---
 app/test/process.h | 51 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)
app/test/process.h +50 -1
index 34afac5a1f39..77bf11316355 100644
@@ -16,6 +16,13 @@
 
 #include <rte_string_fns.h> /* strlcpy */
 
+#ifdef RTE_EXEC_ENV_FREEBSD
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#include <sys/user.h>
+#endif
+
 #ifdef RTE_EXEC_ENV_FREEBSD
 #define self "curproc"
 #define exe "file"
@@ -34,7 +41,49 @@ extern uint16_t flag_for_send_pkts;
 /* close all open file descriptors, check /proc/self/fd to only
  * call close on open fds. Exclude fds 0, 1 and 2
  */
-#ifdef RTE_EXEC_ENV_LINUX
+#ifdef RTE_EXEC_ENV_FREEBSD
+static inline void
+close_files(void)
+{
+	int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_FILEDESC };
+	size_t sysctl_len;
+	void *oldp;
+	int ret;
+
+	mib[3] = getpid();
+	ret = sysctl(mib, 4, NULL, &sysctl_len, NULL, 0);
+	if (ret < 0) {
+		rte_panic("Error sysctl failed %d %d %s\n", ret, errno,
+			  strerror(errno));
+		return;
+	}
+	oldp = malloc(sysctl_len);
+	if (!oldp) {
+		rte_panic("Error malloc failed\n");
+		return;
+	}
+	ret = sysctl(mib, 4, oldp, &sysctl_len, NULL, 0);
+	if (ret < 0) {
+		ret = errno;
+		free(oldp);
+		rte_panic("Error sysctl failed %d %d %s\n", ret, errno,
+			  strerror(errno));
+	}
+	char *curr = oldp, *end = (char *)oldp + sysctl_len;
+	struct kinfo_file *kf_info;
+	while (curr < end) {
+		kf_info = (struct kinfo_file *)curr;
+		if (kf_info->kf_fd <= 2) {
+			curr += kf_info->kf_structsize;
+			continue;
+		}
+		close(kf_info->kf_fd);
+		curr += kf_info->kf_structsize;
+	}
+	free(oldp);
+}
+
+#else
 static inline void
 close_files(void)
 {
Signature · 1 line
2.21.0
#11 lore raw

Re: [dpdk-dev] [PATCH v3 0/3] test: fix timeout in flags autotest

In reply to [email protected]
Quoted 7 lines · On Fri, Nov 8, 2019 at 11:21 AM <[email protected]> wrote:

Hi David,

Thanks for review, hopefully this patch will addresses most of the sutff. Rest I will address here.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

+ const char *procdir = "/proc/self/fd/";

self is a Linux thing.
This won't work on FreeBSD.

IMHO original code didn't worked on FreeBSD as well. I have created function to adress in third patch

Indeed...

Well, wait.
Why do we need to close those file descriptors?
FreeBSD has been like this for quite some time.

Can't we just remove this code?
Maybe someone from Intel has an idea of why it was like this?

Adding Bruce.

Signature · 1 line
David Marchand
#12 lore raw

Re: [dpdk-dev] [PATCH v3 0/3] test: fix timeout in flags autotest

Quoted 17 lines · David Marchand
Quoted 7 lines · On Fri, Nov 8, 2019 at 11:21 AM <[email protected]> wrote:

Hi David,

Thanks for review, hopefully this patch will addresses most of the sutff. Rest I will address here.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

+ const char *procdir = "/proc/self/fd/";

self is a Linux thing.
This won't work on FreeBSD.

IMHO original code didn't worked on FreeBSD as well. I have created function to adress in third patch

Quoted 8 lines

Indeed...

Well, wait.
Why do we need to close those file descriptors?
FreeBSD has been like this for quite some time.

Can't we just remove this code?
Maybe someone from Intel has an idea of why it was like this?

Great...

        /* get file for config (fd is always 3) */
        snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);

And this only applies to linux (getting the current file prefix).

Signature · 1 line
David Marchand
#13 lore raw

Re: [dpdk-dev] [PATCH v3 0/3] test: fix timeout in flags autotest

In reply to David Marchand
Quoted 14 lines · David Marchand
Quoted 12 lines · On Fri, Nov 8, 2019 at 11:21 AM <[email protected]> wrote:

Hi David,

Thanks for review, hopefully this patch will addresses most of the sutff. Rest I will address here.

Quoted 5 lines
Quoted 2 lines

+ const char *procdir = "/proc/self/fd/";

self is a Linux thing.
This won't work on FreeBSD.

Quoted 8 lines
Quoted 2 lines

IMHO original code didn't worked on FreeBSD as well. I have created function to adress in third patch

Indeed...

Well, wait.
Why do we need to close those file descriptors?
FreeBSD has been like this for quite some time.

We don't know what this is used for.

We know it does not work on FreeBSD, but this does not seem to be a problem. Introducing something more on FreeBSD is a risk with no actual benefit at first sight.

Either we take only the first patch under a #ifdef EXEC_ENV_LINUX or we leave this as is.

Krzystof, is this a problem for you if we postpone and investigate further for 20.02?

Signature · 1 line
David Marchand
#14 lore raw

Re: [dpdk-dev] [EXT] Re: [PATCH v3 0/3] test: fix timeout in flags autotest

Quoted 28 lines · David Marchand

External Email

----------------------------------------------------------------------

Quoted 14 lines · David Marchand
Quoted 12 lines · On Fri, Nov 8, 2019 at 11:21 AM <[email protected]> wrote:

Hi David,

Thanks for review, hopefully this patch will addresses most of the sutff. Rest I will address here.

Quoted 5 lines
Quoted 2 lines

+ const char *procdir = "/proc/self/fd/";

self is a Linux thing.
This won't work on FreeBSD.

Quoted 8 lines
Quoted 2 lines

IMHO original code didn't worked on FreeBSD as well. I have created function to adress in third patch

Indeed...

Well, wait.
Why do we need to close those file descriptors?
FreeBSD has been like this for quite some time.

Quoted 5 lines

We don't know what this is used for.

We know it does not work on FreeBSD, but this does not seem to be a problem. Introducing something more on FreeBSD is a risk with no actual benefit at first sight.

Ok.

Quoted 3 lines

Either we take only the first patch under a #ifdef EXEC_ENV_LINUX or we leave this as is.

I would like have that because ARM64 is main platform for me and this makes the test pass, and fixes the difference between the comment and the code.

Quoted 3 lines

Krzystof, is this a problem for you if we postpone and investigate further for 20.02?

Not a problem. I would like to have fix for EXEC_ENV_LINUX for now. Rest, FreeBSD case and the decisions weather this whole code should be deleted can be made later.

Quoted 5 lines
Signature · 2 lines
-- 
David Marchand
Signature · 3 lines
-
Regards,
Krzysztof(Chris) Kanas
#15 lore raw

Re: [dpdk-dev] [EXT] Re: [PATCH v3 0/3] test: fix timeout in flags autotest

Quoted 44 lines · Krzysztof Kanas
Quoted 28 lines · David Marchand

External Email

----------------------------------------------------------------------

Quoted 14 lines · David Marchand
Quoted 12 lines · On Fri, Nov 8, 2019 at 11:21 AM <[email protected]> wrote:

Hi David,

Thanks for review, hopefully this patch will addresses most of the sutff. Rest I will address here.

Quoted 5 lines
Quoted 2 lines

+ const char *procdir = "/proc/self/fd/";

self is a Linux thing.
This won't work on FreeBSD.

Quoted 8 lines
Quoted 2 lines

IMHO original code didn't worked on FreeBSD as well. I have created function to adress in third patch

Indeed...

Well, wait.
Why do we need to close those file descriptors?
FreeBSD has been like this for quite some time.

Quoted 5 lines

We don't know what this is used for.

We know it does not work on FreeBSD, but this does not seem to be a problem. Introducing something more on FreeBSD is a risk with no actual benefit at first sight.

Ok.

Quoted 3 lines

Either we take only the first patch under a #ifdef EXEC_ENV_LINUX or we leave this as is.

I would like have that because ARM64 is main platform for me and this makes the test pass, and fixes the difference between the comment and the code.

Quoted 1 line
Quoted 5 lines
Quoted 2 lines

Krzystof, is this a problem for you if we postpone and investigate further for 20.02?

Not a problem. I would like to have fix for EXEC_ENV_LINUX for now. Rest, FreeBSD case and the decisions weather this whole code should be deleted can be made later.

In my tests of the original code, I never saw anything but access -1 ENOENT. Anyway, just sent a v4, your v3 patch, with just cosmetics and commitlog updated.

Signature · 1 line
David Marchand
#16 lore raw

Re: [dpdk-dev] [EXT] Re: [PATCH v3 0/3] test: fix timeout in flags autotest

Quoted 35 lines · David Marchand
Quoted 34 lines · Krzysztof Kanas
Quoted 18 lines · David Marchand

External Email

----------------------------------------------------------------------

Quoted 13 lines · David Marchand
Quoted 10 lines · On Fri, Nov 8, 2019 at 11:21 AM <[email protected]> wrote:

Hi David,

Thanks for review, hopefully this patch will addresses most of the sutff. Rest I will address here.

Quoted 3 lines
Quoted 1 line

+ const char *procdir = "/proc/self/fd/";

self is a Linux thing.
This won't work on FreeBSD.

IMHO original code didn't worked on FreeBSD as well. I have created function to adress in third patch

Indeed...

Quoted 8 lines
Quoted 3 lines

Well, wait.
Why do we need to close those file descriptors?
FreeBSD has been like this for quite some time.

We don't know what this is used for.

We know it does not work on FreeBSD, but this does not seem to be a problem. Introducing something more on FreeBSD is a risk with no actual benefit at first sight.

Ok.

Quoted 2 lines

Either we take only the first patch under a #ifdef EXEC_ENV_LINUX or we leave this as is.

I would like have that because ARM64 is main platform for me and this makes the test pass, and fixes the difference between the comment and the code.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Krzystof, is this a problem for you if we postpone and investigate further for 20.02?

Not a problem. I would like to have fix for EXEC_ENV_LINUX for now. Rest, FreeBSD case and the decisions weather this whole code should be deleted can be made later.

In my tests of the original code, I never saw anything but access -1 ENOENT. Anyway, just sent a v4, your v3 patch, with just cosmetics and commitlog updated.

ACK. Tested, the v4 patch works fine.

Quoted 2 lines
Signature · 3 lines
Best Regards
--
Krzysztof Kanas [email protected]
#17 lore raw

PATCH v4 1/1 test: optimise fd closing in forked test process

In reply to [email protected]

From: Krzysztof Kanas <[email protected]>

Caught while investigating timeouts on a ARM64 server.

Stracing a test process running the eal_flags_autotest, we can see that the fork helper is checking all possible file descriptors from getdtablesize() to 2, and close the existing ones.
We can do better by inspecting this forked process /proc/self/fd directory.

Besides, checking file descriptors via /proc/self/fd only makes sense for Linux. This code was a noop on FreeBSD.

Fixes af75078fece3 ("first public release")

---
Changelog since v3:
- rewrote commit log message,
- restricted the fix to Linux only,

---
 app/test/process.h | 50 ++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 44 insertions(+), 6 deletions(-)
app/test/process.h +44 -6
index 128ce41..191d279 100644
@@ -11,6 +11,7 @@
 #include <stdlib.h> /* NULL */
 #include <string.h> /* strerror */
 #include <unistd.h> /* readlink */
+#include <dirent.h>
 #include <sys/wait.h>
 
 #include <rte_string_fns.h> /* strlcpy */
@@ -40,7 +41,7 @@ process_dup(const char *const argv[], int numargs, const char *env_value)
 {
 	int num;
 	char *argv_cpy[numargs + 1];
-	int i, fd, status;
+	int i, status;
 	char path[32];
 #ifdef RTE_LIBRTE_PDUMP
 	pthread_t thread;
@@ -56,13 +57,50 @@ process_dup(const char *const argv[], int numargs, const char *env_value)
 		argv_cpy[i] = NULL;
 		num = numargs;
 
-		/* close all open file descriptors, check /proc/self/fd to only
-		 * call close on open fds. Exclude fds 0, 1 and 2*/
-		for (fd = getdtablesize(); fd > 2; fd-- ) {
-			snprintf(path, sizeof(path), "/proc/" exe "/fd/%d", fd);
-			if (access(path, F_OK) == 0)
+#ifdef RTE_EXEC_ENV_LINUX
+		{
+			const char *procdir = "/proc/" self "/fd/";
+			struct dirent *dirent;
+			char *endptr;
+			int fd, fdir;
+			DIR *dir;
+
+			/* close all open file descriptors, check /proc/self/fd
+			 * to only call close on open fds. Exclude fds 0, 1 and
+			 * 2
+			 */
+			dir = opendir(procdir);
+			if (dir == NULL) {
+				rte_panic("Error opening %s: %s\n", procdir,
+						strerror(errno));
+			}
+
+			fdir = dirfd(dir);
+			if (fdir < 0) {
+				status = errno;
+				closedir(dir);
+				rte_panic("Error %d obtaining fd for dir %s: %s\n",
+						fdir, procdir,
+						strerror(status));
+			}
+
+			while ((dirent = readdir(dir)) != NULL) {
+				errno = 0;
+				fd = strtol(dirent->d_name, &endptr, 10);
+				if (errno != 0 || endptr[0] != '\0') {
+					printf("Error converting name fd %d %s:\n",
+						fd, dirent->d_name);
+					continue;
+				}
+
+				if (fd == fdir || fd <= 2)
+					continue;
+
 				close(fd);
+			}
+			closedir(dir);
 		}
+#endif
 		printf("Running binary with argv[]:");
 		for (i = 0; i < num; i++)
 			printf("'%s' ", argv_cpy[i]);
Signature · 1 line
1.8.3.1
Acked-by on v4 1/1
#18 lore raw

Re: [dpdk-dev] [PATCH v4] test: optimise fd closing in forked test process

Quoted 10 lines · David Marchand

From: Krzysztof Kanas <[email protected]>

Caught while investigating timeouts on a ARM64 server.

Stracing a test process running the eal_flags_autotest, we can see that the fork helper is checking all possible file descriptors from getdtablesize() to 2, and close the existing ones.
We can do better by inspecting this forked process /proc/self/fd directory.

Quoted 8 lines

Besides, checking file descriptors via /proc/self/fd only makes sense for Linux. This code was a noop on FreeBSD.

Fixes af75078fece3 ("first public release")

Seems to be an improvement on what is existing.

#19 lore raw

PATCH v4 1/1 test: optimise fd closing in forked test process

Quoted 12 lines · Kevin Traynor
Quoted 10 lines · David Marchand

From: Krzysztof Kanas <[email protected]>

Caught while investigating timeouts on a ARM64 server.

Stracing a test process running the eal_flags_autotest, we can see that the fork helper is checking all possible file descriptors from getdtablesize() to 2, and close the existing ones.
We can do better by inspecting this forked process /proc/self/fd directory.

Quoted 8 lines
Quoted 8 lines

Besides, checking file descriptors via /proc/self/fd only makes sense for Linux. This code was a noop on FreeBSD.

Fixes af75078fece3 ("first public release")
Quoted 4 lines

Seems to be an improvement on what is existing.

Applied, thanks.

--
David Marchand