util: mimic KCMP_FILE via epoll when KCMP is missing

On Linux platforms where KCMP isn't allowed epoll can be used
as a fallback mechanism to provide the same feature.

Acked-by: Marek Olšák <marek.olsak@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/34447>
This commit is contained in:
Pierre-Eric Pelloux-Prayer 2025-04-10 09:53:11 +02:00 committed by Marge Bot
parent 88f53906d8
commit 17e286529b

View file

@ -207,6 +207,8 @@ os_read_file(const char *filename, size_t *size)
#define KCMP_FILE 0
#endif
#elif DETECT_OS_LINUX
#include <sys/epoll.h>
#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;