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;