From 1498dee3aadf66dbae10e699d71c62b428a511a3 Mon Sep 17 00:00:00 2001 From: Pavel Radzivilovsky Date: Thu, 19 Nov 2026 21:46:54 +0200 Subject: inotify: use poll() instead of pselect() in usbmuxd_listen_inotify() 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 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 --- src/libusbmuxd.c | 52 ++++++++++++++++------------------------------------ 1 file changed, 16 insertions(+), 36 deletions(-) (limited to 'src') 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 -#include +#include #include #include #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; } -- cgit v1.1-32-gdbae