| 1 | /* Check if kernel supports PID file descriptors. |
| 2 | Copyright (C) 2023-2024 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <atomic.h> |
| 20 | #include <sys/wait.h> |
| 21 | #include <sysdep.h> |
| 22 | |
| 23 | /* The PID file descriptors was added during multiple releases: |
| 24 | - Linux 5.2 added CLONE_PIDFD support for clone and __clone_pidfd_supported |
| 25 | syscall. |
| 26 | - Linux 5.3 added support for poll and CLONE_PIDFD for clone3. |
| 27 | - Linux 5.4 added P_PIDFD support on waitid. |
| 28 | |
| 29 | For internal usage on spawn and fork, it only make sense to return a file |
| 30 | descriptor if caller can actually waitid on it. */ |
| 31 | |
| 32 | static int __waitid_pidfd_supported = 0; |
| 33 | |
| 34 | bool |
| 35 | __clone_pidfd_supported (void) |
| 36 | { |
| 37 | int state = atomic_load_relaxed (&__waitid_pidfd_supported); |
| 38 | if (state == 0) |
| 39 | { |
| 40 | /* Linux define the maximum allocated file descriptor value as |
| 41 | 0x7fffffc0 (from fs/file.c): |
| 42 | |
| 43 | #define __const_min(x, y) ((x) < (y) ? (x) : (y)) |
| 44 | unsigned int sysctl_nr_open_max = |
| 45 | __const_min(INT_MAX, ~(size_t)0/sizeof(void *)) & -BITS_PER_LONG; |
| 46 | |
| 47 | So we can detect whether kernel supports all pidfd interfaces by |
| 48 | using a valid but never allocated file descriptor: if is not |
| 49 | supported waitid will return EINVAL, otherwise EBADF. |
| 50 | |
| 51 | Also the waitid is a cancellation entrypoint, so issue the syscall |
| 52 | directly. */ |
| 53 | int r = INTERNAL_SYSCALL_CALL (waitid, P_PIDFD, INT_MAX, NULL, |
| 54 | WEXITED | WNOHANG); |
| 55 | state = r == -EBADF ? 1 : -1; |
| 56 | atomic_store_relaxed (&__waitid_pidfd_supported, state); |
| 57 | } |
| 58 | |
| 59 | return state > 0; |
| 60 | } |
| 61 | |