From 87168807b215b83141220b34ebb30a3e51e61770 Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Tue, 1 Apr 2025 23:32:16 +0200 Subject: [PATCH] eventfd: fix feature guards Enable eventfd code consistently when both `HAVE_EVENTFD` and `HAVE_SYS_EVENTFD_H` macros are defined. Before this patch `HAVE_EVENTFD` guarded it alone, though the code also required the header, which was guarded by `HAVE_SYS_EVENTFD_H`. These should normally be detected in pairs. When they aren't, omit using `eventfd()` to avoid calling it without a known matching header. If this disables valid cases (e.g. some system declares this function via a different header), feature detection and the code may be extended for those cases. If these are known to come in pairs, always, another option is detect them both at build stage, and forward a single macro to C. Reported-by: Abhinav Singhal Bug: https://curl.se/mail/lib-2025-04/0000.html Closes #16909 --- lib/asyn-thread.c | 4 ++-- lib/curl_setup.h | 5 +++++ lib/multi.c | 4 ++-- lib/socketpair.c | 9 +++++---- lib/socketpair.h | 6 +++--- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/asyn-thread.c b/lib/asyn-thread.c index c3edf72033..177cfc0c7d 100644 --- a/lib/asyn-thread.c +++ b/lib/asyn-thread.c @@ -170,7 +170,7 @@ void destroy_thread_sync_data(struct thread_sync_data *tsd) * close one end of the socket pair (may be done in resolver thread); * the other end (for reading) is always closed in the parent thread. */ -#ifndef HAVE_EVENTFD +#ifndef USE_EVENTFD if(tsd->sock_pair[1] != CURL_SOCKET_BAD) { wakeup_close(tsd->sock_pair[1]); } @@ -293,7 +293,7 @@ CURL_STDCALL getaddrinfo_thread(void *arg) else { #ifndef CURL_DISABLE_SOCKETPAIR if(tsd->sock_pair[1] != CURL_SOCKET_BAD) { -#ifdef HAVE_EVENTFD +#ifdef USE_EVENTFD const uint64_t buf[1] = { 1 }; #else const char buf[1] = { 1 }; diff --git a/lib/curl_setup.h b/lib/curl_setup.h index ff63d3d72f..c5433c02a4 100644 --- a/lib/curl_setup.h +++ b/lib/curl_setup.h @@ -455,6 +455,11 @@ # define __NO_NET_API #endif +/* Whether to use eventfd() */ +#if defined(HAVE_EVENTFD) && defined(HAVE_SYS_EVENTFD_H) +#define USE_EVENTFD +#endif + #include #include diff --git a/lib/multi.c b/lib/multi.c index 09afe804c0..858704beba 100644 --- a/lib/multi.c +++ b/lib/multi.c @@ -1408,7 +1408,7 @@ CURLMcode curl_multi_wakeup(CURLM *m) and before cleanup */ if(multi->wakeup_pair[1] != CURL_SOCKET_BAD) { while(1) { -#ifdef HAVE_EVENTFD +#ifdef USE_EVENTFD /* eventfd has a stringent rule of requiring the 8-byte buffer when calling write(2) on it */ const uint64_t buf[1] = { 1 }; @@ -2748,7 +2748,7 @@ CURLMcode curl_multi_cleanup(CURLM *m) #else #ifdef ENABLE_WAKEUP wakeup_close(multi->wakeup_pair[0]); -#ifndef HAVE_EVENTFD +#ifndef USE_EVENTFD wakeup_close(multi->wakeup_pair[1]); #endif #endif diff --git a/lib/socketpair.c b/lib/socketpair.c index 85fb7a826c..e4ca2cffa0 100644 --- a/lib/socketpair.c +++ b/lib/socketpair.c @@ -27,10 +27,9 @@ #include "urldata.h" #include "rand.h" -#ifdef HAVE_EVENTFD -#ifdef HAVE_SYS_EVENTFD_H +#ifdef USE_EVENTFD + #include -#endif int Curl_eventfd(curl_socket_t socks[2], bool nonblocking) { @@ -42,7 +41,9 @@ int Curl_eventfd(curl_socket_t socks[2], bool nonblocking) socks[0] = socks[1] = efd; return 0; } + #elif defined(HAVE_PIPE) + #ifdef HAVE_FCNTL #include #endif @@ -72,8 +73,8 @@ int Curl_pipe(curl_socket_t socks[2], bool nonblocking) return 0; } -#endif +#endif /* USE_EVENTFD */ #ifndef CURL_DISABLE_SOCKETPAIR #ifdef HAVE_SOCKETPAIR diff --git a/lib/socketpair.h b/lib/socketpair.h index da0d08b767..5541899445 100644 --- a/lib/socketpair.h +++ b/lib/socketpair.h @@ -26,7 +26,7 @@ #include "curl_setup.h" -#ifdef HAVE_EVENTFD +#ifdef USE_EVENTFD #define wakeup_write write #define wakeup_read read @@ -46,7 +46,7 @@ int Curl_eventfd(curl_socket_t socks[2], bool nonblocking); #include int Curl_pipe(curl_socket_t socks[2], bool nonblocking); -#else /* !HAVE_EVENTFD && !HAVE_PIPE */ +#else /* !USE_EVENTFD && !HAVE_PIPE */ #define wakeup_write swrite #define wakeup_read sread @@ -69,7 +69,7 @@ int Curl_pipe(curl_socket_t socks[2], bool nonblocking); #define wakeup_create(p,nb)\ Curl_socketpair(SOCKETPAIR_FAMILY, SOCKETPAIR_TYPE, 0, p, nb) -#endif /* HAVE_EVENTFD */ +#endif /* USE_EVENTFD */ #ifndef CURL_DISABLE_SOCKETPAIR #include -- 2.47.3