Skip to content

Commit 8c0011d

Browse files
committed
gh-153400: Use pidfd_open() and pidfd_getfd() functions
Use pidfd_open() and pidfd_getfd() functions if available (glibc 2.36 and newer), instead of using syscall().
1 parent 219768f commit 8c0011d

5 files changed

Lines changed: 37 additions & 5 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:mod:`os`: Use ``pidfd_open()`` and ``pidfd_getfd()`` functions if available
2+
(glibc 2.36 and newer), instead of using ``syscall()``. Patch by Victor
3+
Stinner.

Modules/posixmodule.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10810,8 +10810,9 @@ os_wait_impl(PyObject *module)
1081010810

1081110811

1081210812
// This system call always crashes on older Android versions.
10813-
#if defined(__linux__) && defined(__NR_pidfd_open) && \
10814-
!(defined(__ANDROID__) && __ANDROID_API__ < 31)
10813+
#if defined(HAVE_PIDFD_OPEN) \
10814+
|| (defined(__linux__) && defined(__NR_pidfd_open) \
10815+
&& !(defined(__ANDROID__) && __ANDROID_API__ < 31))
1081510816
/*[clinic input]
1081610817
os.pidfd_open
1081710818
pid: pid_t
@@ -10827,7 +10828,11 @@ static PyObject *
1082710828
os_pidfd_open_impl(PyObject *module, pid_t pid, unsigned int flags)
1082810829
/*[clinic end generated code: output=5c7252698947dc41 input=03058b32c389f874]*/
1082910830
{
10831+
#ifdef HAVE_PIDFD_OPEN
10832+
int fd = pidfd_open(pid, flags);
10833+
#else
1083010834
int fd = syscall(__NR_pidfd_open, pid, flags);
10835+
#endif
1083110836
if (fd < 0) {
1083210837
return posix_error();
1083310838
}
@@ -10836,8 +10841,9 @@ os_pidfd_open_impl(PyObject *module, pid_t pid, unsigned int flags)
1083610841
#endif
1083710842

1083810843

10839-
#if defined(__linux__) && defined(__NR_pidfd_getfd) && \
10840-
!(defined(__ANDROID__) && __ANDROID_API__ < 31)
10844+
#if defined(HAVE_PIDFD_GETFD) \
10845+
|| (defined(__linux__) && defined(__NR_pidfd_getfd) \
10846+
&& !(defined(__ANDROID__) && __ANDROID_API__ < 31))
1084110847
/*[clinic input]
1084210848
os.pidfd_getfd
1084310849
pidfd: int
@@ -10856,7 +10862,11 @@ os_pidfd_getfd_impl(PyObject *module, int pidfd, int targetfd,
1085610862
unsigned int flags)
1085710863
/*[clinic end generated code: output=e1a1415a13c7137f input=ef6417fb10deb1cc]*/
1085810864
{
10865+
#ifdef HAVE_PIDFD_GETFD
10866+
int fd = pidfd_getfd(pidfd, targetfd, flags);
10867+
#else
1085910868
int fd = syscall(__NR_pidfd_getfd, pidfd, targetfd, flags);
10869+
#endif
1086010870
if (fd < 0) {
1086110871
return posix_error();
1086210872
}

configure

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5503,7 +5503,8 @@ AC_CHECK_FUNCS([ \
55035503
getpwent getpwnam_r getpwuid getpwuid_r getresgid getresuid getrusage getsid getspent \
55045504
getspnam getuid getwd grantpt if_nameindex initgroups kill killpg lchown linkat \
55055505
lockf lstat lutimes madvise mbrtowc memrchr mkdirat mkfifo mkfifoat \
5506-
mknod mknodat mktime mmap mremap nice openat opendir pathconf pause pipe \
5506+
mknod mknodat mktime mmap mremap nice openat opendir pathconf pause \
5507+
pidfd_open pidfd_getfd pipe \
55075508
plock poll ppoll posix_fadvise posix_fallocate posix_openpt posix_spawn posix_spawnp \
55085509
posix_spawn_file_actions_addclosefrom_np \
55095510
pread preadv preadv2 process_vm_readv \

pyconfig.h.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,12 @@
10461046
/* Define to 1 if you have the 'pause' function. */
10471047
#undef HAVE_PAUSE
10481048

1049+
/* Define to 1 if you have the 'pidfd_getfd' function. */
1050+
#undef HAVE_PIDFD_GETFD
1051+
1052+
/* Define to 1 if you have the 'pidfd_open' function. */
1053+
#undef HAVE_PIDFD_OPEN
1054+
10491055
/* Define to 1 if you have the 'pipe' function. */
10501056
#undef HAVE_PIPE
10511057

0 commit comments

Comments
 (0)