diff options
| author | 2026-11-19 21:46:54 +0200 | |
|---|---|---|
| committer | 2026-11-19 21:46:54 +0200 | |
| commit | 1498dee3aadf66dbae10e699d71c62b428a511a3 (patch) | |
| tree | f666f66fadaceaaf1378ffada69c5f4d9b8999c9 /src/libusbmuxd.c | |
| parent | cf4b806dff1003e56b8219367bbb29cb01c3f619 (diff) | |
| download | libusbmuxd-1498dee3aadf66dbae10e699d71c62b428a511a3.tar.gz libusbmuxd-1498dee3aadf66dbae10e699d71c62b428a511a3.tar.bz2 | |
When usbmuxd isn't running, the device_monitor thread waits for the
socket to appear by select()ing on an inotify fd. In a long-lived
process with many open descriptors (e.g. KDE's kwin_wayland, which
loads libusbmuxd via Solid's iMobileDevice backend), that fd can be
>= FD_SETSIZE (1024). FD_SET() on it is undefined behaviour, and with
_FORTIFY_SOURCE glibc aborts the whole process:
*** bit out of range 0 - FD_SETSIZE on fd_set ***: terminated
#8 __fortify_fail (libc)
#9 usbmuxd_listen_inotify src/libusbmuxd.c:944
#10 device_monitor src/libusbmuxd.c:1130
Here this took down the Wayland compositor (and with it the whole
desktop session) after ~4 weeks of uptime, when typing in the Overview
search triggered a KRunner plugin that initialised Solid.
poll() has no descriptor limit. This replaces the pselect() call with
poll() on the inotify fd and the wake pipe; the pselect() fallback shim
and the <sys/select.h> include become unused and are removed.
libimobiledevice-glue's socket code already uses poll() on non-Windows
platforms for the same reason.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'src/libusbmuxd.c')
| -rw-r--r-- | src/libusbmuxd.c | 52 |
1 files changed, 16 insertions, 36 deletions
diff --git a/src/libusbmuxd.c b/src/libusbmuxd.c index b391723..645fdcf 100644 --- a/src/libusbmuxd.c +++ b/src/libusbmuxd.c @@ -71,7 +71,7 @@ extern int _NSGetExecutablePath(char* buf, uint32_t* bufsize); #ifdef HAVE_INOTIFY #include <sys/inotify.h> -#include <sys/select.h> +#include <poll.h> #include <fcntl.h> #include <pthread.h> #define EVENT_SIZE (sizeof (struct inotify_event)) @@ -946,30 +946,6 @@ static int usbmuxd_listen_poll() } #ifdef HAVE_INOTIFY -#ifndef HAVE_PSELECT -static int pselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timespec *timeout, const sigset_t *sigmask) -{ - int ready; - struct timeval tv; - struct timeval *p_timeout; - sigset_t origmask; - - if (timeout) { - tv.tv_sec = timeout->tv_sec; - tv.tv_usec = timeout->tv_nsec / 1000; - p_timeout = &tv; - } else { - p_timeout = NULL; - } - - pthread_sigmask(SIG_SETMASK, sigmask, &origmask); - ready = select(nfds, readfds, writefds, exceptfds, p_timeout); - pthread_sigmask(SIG_SETMASK, &origmask, NULL); - - return ready; -} -#endif - static int usbmuxd_listen_inotify() { int inot_fd; @@ -1000,18 +976,22 @@ static int usbmuxd_listen_inotify() } while (1) { - fd_set rfds; - int maxfd = inot_fd; - - FD_ZERO(&rfds); - FD_SET(inot_fd, &rfds); + struct pollfd pfds[2]; + nfds_t nfds = 1; + + /* use poll() rather than select(): in a process with many open + * descriptors inot_fd can be >= FD_SETSIZE, and FD_SET() on it + * is undefined behaviour (glibc's fortify check aborts) */ + pfds[0].fd = inot_fd; + pfds[0].events = POLLIN; + pfds[0].revents = 0; if (inotify_wake_fd[0] >= 0) { - FD_SET(inotify_wake_fd[0], &rfds); - if (inotify_wake_fd[0] > maxfd) { - maxfd = inotify_wake_fd[0]; - } + pfds[1].fd = inotify_wake_fd[0]; + pfds[1].events = POLLIN; + pfds[1].revents = 0; + nfds = 2; } - int r = pselect(maxfd+1, &rfds, NULL, NULL, NULL, NULL); + int r = poll(pfds, nfds, -1); if (r < 0) { if (errno == EINTR && !cancelling) { continue; @@ -1019,7 +999,7 @@ static int usbmuxd_listen_inotify() break; } - if (inotify_wake_fd[0] >= 0 && FD_ISSET(inotify_wake_fd[0], &rfds)) { + if (nfds == 2 && (pfds[1].revents & (POLLIN | POLLHUP | POLLERR))) { break; } |
