workaround to support pidfd_open (backported) while waitpid is missing

This commit is contained in:
2026-10-29 22:33:28 +02:00
parent 93b7d20dcc
commit afa212d153
2 changed files with 29 additions and 1 deletions
+2 -1
View File
@@ -8,7 +8,8 @@
/* Defined since glibc-2.36.
* Supported since kernel v5.3 (7615d9e1780e26e0178c93c55b73309a5dc093d7). */
int pidfd_open_shim(pid_t pid, unsigned flags);
#define pidfd_open pidfd_open_shim
int pidfd_open_checked(pid_t pid, unsigned flags);
#define pidfd_open pidfd_open_checked
/* Defined since glibc-2.36.
* Supported since kernel v5.1 (3eb39f47934f9d5a3027fe00d906a45fe3a15fad). */
+27
View File
@@ -1,6 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <sys/pidfd.h>
#include <sys/wait.h>
#include "libc-shim.h"
@@ -8,6 +9,32 @@ DEFINE_SYSCALL_SHIM(pidfd_open, int,
pid_t, pid,
unsigned, flags)
/* ANCIENT-MOD: vendor kernels < 5.4 (e.g. Android 4.19) may backport pidfd_open() without
* waitid(P_PIDFD); report pidfds as unsupported there so the plain-PID fallbacks are used. */
int pidfd_open_checked(pid_t pid, unsigned flags) {
static int usable = -1;
if (usable < 0) {
int fd = pidfd_open_shim(getpid(), 0);
if (fd < 0) {
if (errno == ENOSYS)
usable = 0;
return fd;
}
siginfo_t si = {};
usable = waitid(P_PIDFD, fd, &si, WEXITED|WNOHANG) >= 0 || errno != EINVAL;
close(fd);
}
if (!usable) {
errno = ENOSYS;
return -1;
}
return pidfd_open_shim(pid, flags);
}
DEFINE_SYSCALL_SHIM(pidfd_send_signal, int,
int, fd,
int, sig,