diff --git a/src/util/os_file.c b/src/util/os_file.c index bb7135c62b0..71cd38b019c 100644 --- a/src/util/os_file.c +++ b/src/util/os_file.c @@ -207,6 +207,8 @@ os_read_file(const char *filename, size_t *size) #define KCMP_FILE 0 #endif +#elif DETECT_OS_LINUX +#include #endif #if DETECT_OS_DRAGONFLY || DETECT_OS_FREEBSD @@ -266,6 +268,45 @@ os_same_file_description(int fd1, int fd2) return -1; 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; + + struct epoll_event evt = {}; + /* Get a new file descriptor number for fd1. */ + int tmp = os_dupfd_cloexec(fd1); + /* Add it to evt. */ + r = epoll_ctl(efd, EPOLL_CTL_ADD, tmp, &evt); + if (r) + goto error; + + /* Now use dup2 to get tmp to point to fd2's file description. */ + r = dup2(fd2, tmp); + if (r < 0) + goto error; + + /* Last step: add tmp again to evt. Given that we're adding the + * same file description as fd2 (thanks to dup2), it will fail with + * EEXIST if fd1 and fd2 both point to the same file description. + */ + r = epoll_ctl(efd, EPOLL_CTL_ADD, tmp, &evt); + if (r) { + if (errno == EEXIST) + r = 0; + else + r = -1; + } else { + r = 1; + } + +error: + close(tmp); + close(efd); + + return r; #else /* Otherwise we can't tell */ return -1;