[dpdk-dev] [PATCH v3 3/3] test: fix FreeBSD file closing function

kkanas at marvell.com kkanas at marvell.com
Fri Nov 8 11:21:35 CET 2019


From: Krzysztof Kanas <kkanas at marvell.com>

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

Signed-off-by: Krzysztof Kanas <kkanas at marvell.com>
---
 app/test/process.h | 51 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)

diff --git a/app/test/process.h b/app/test/process.h
index 34afac5a1f39..77bf11316355 100644
--- a/app/test/process.h
+++ b/app/test/process.h
@@ -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)
 {
-- 
2.21.0



More information about the dev mailing list