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
This commit is contained in:
Viktor Szakats 2025-04-01 23:32:16 +02:00
parent 22352e627d
commit 87168807b2
No known key found for this signature in database
GPG Key ID: B5ABD165E2AEF201
5 changed files with 17 additions and 11 deletions

View File

@ -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 };

View File

@ -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 <stdio.h>
#include <assert.h>

View File

@ -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

View File

@ -27,10 +27,9 @@
#include "urldata.h"
#include "rand.h"
#ifdef HAVE_EVENTFD
#ifdef HAVE_SYS_EVENTFD_H
#ifdef USE_EVENTFD
#include <sys/eventfd.h>
#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 <fcntl.h>
#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

View File

@ -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 <curl/curl.h>
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 <curl/curl.h>