elf: handle addition overflow in _dl_find_object_update_1 [BZ #32245]
[glibc.git] / sysdeps / unix / sysv / linux / clone-pidfd-support.c
blob0ca1958e779cd7edc56b578b33c49bdfedadcb87
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.
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.
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.
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/>. */
19 #include <atomic.h>
20 #include <sys/wait.h>
21 #include <sysdep.h>
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.
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. */
32 static int __waitid_pidfd_supported = 0;
34 bool
35 __clone_pidfd_supported (void)
37 int state = atomic_load_relaxed (&__waitid_pidfd_supported);
38 if (state == 0)
40 /* Linux define the maximum allocated file descriptor value as
41 0x7fffffc0 (from fs/file.c):
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;
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.
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);
59 return state > 0;