From c0c95dc845a99f0233968d15b41f550e93f86530 Mon Sep 17 00:00:00 2001 From: Pierre-Eric Pelloux-Prayer Date: Thu, 19 Jun 2025 16:27:47 +0200 Subject: [PATCH] util: use F_DUPFD_QUERY on Linux MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit F_DUPFD_QUERY has been introduced in 6.10 exactly for the purpose of telling if 2 fd points at the same file description so use it first. If it's not supported, we'll get r=-1 and errno=EINVAL, and we can fallback on KCMP or epoll. Acked-by: Simona Vetter Acked-by: Marek Olšák Part-of: --- src/util/os_file.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/util/os_file.c b/src/util/os_file.c index 71cd38b019c..6336941c7f4 100644 --- a/src/util/os_file.c +++ b/src/util/os_file.c @@ -29,6 +29,9 @@ #ifndef F_DUPFD_CLOEXEC #define F_DUPFD_CLOEXEC 1030 #endif +#ifndef F_DUPFD_QUERY +#define F_DUPFD_QUERY 1027 +#endif #endif @@ -240,6 +243,18 @@ os_same_file_description(int fd1, int fd2) if (fd1 == fd2) return 0; +#if DETECT_OS_LINUX + /* Use F_DUPFD_QUERY if available. */ + int r = fcntl(fd1, F_DUPFD_QUERY, fd2); + + if (r < 0) { + if (errno == EBADF) + return 1; + } else { + return r == 1 ? 0 : 1; + } +#endif + #ifdef SYS_kcmp return syscall(SYS_kcmp, pid, pid, KCMP_FILE, fd1, fd2); #elif DETECT_OS_DRAGONFLY || DETECT_OS_FREEBSD @@ -269,8 +284,6 @@ os_same_file_description(int fd1, int fd2) return (fd1_kfile < fd2_kfile) | ((fd1_kfile > fd2_kfile) << 1); #elif DETECT_OS_LINUX - int r; - int efd = epoll_create1(EPOLL_CLOEXEC); if (efd < 0) return -1;