mirror of
https://gitlab.freedesktop.org/mesa/mesa.git
synced 2025-12-20 11:40:10 +01:00
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:
parent
88f53906d8
commit
17e286529b
1 changed files with 41 additions and 0 deletions
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue