From patchwork Sat Jan 24 22:04:25 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luca Boccassi X-Patchwork-Id: 128917 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id A74904BA9005 for ; Sat, 24 Jan 2026 22:05:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A74904BA9005 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=TRCvj9ip X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by sourceware.org (Postfix) with ESMTPS id C1C074BA2E3C for ; Sat, 24 Jan 2026 22:05:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C1C074BA2E3C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C1C074BA2E3C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::331 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1769292310; cv=none; b=w4OwDANgP+tCpv/ZAC2CS5Cp5PmKamKd7GsnuiaM3Va5+PYLp1HRBptJ+dHfoCNHQcrtqF4FluORINP8San508nqLgRs16SiUxy9Qt2HNOynyWklnfmjZDCPzYSfNfdSgeoQn2gMBKgCT/WLvpbltC2tJFBehhThWkaQM9RAUIo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1769292310; c=relaxed/simple; bh=e6CJWegp/feAwY+6Rp/8lUYxlzeYU2bA6/c+V6jLcTQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Grk4HH9SexCVs7lW3yoq9OHjqkbzf9GVpCJOHbmrr0ixWtVGUNMTzca8Nk1xXmRGERvTUECylMlaYDdETOeTB3IzMvb6XcAfAYDf9900lanKZJ6jTEIoydiRyaq1a+1ivjglZSnWM2cCojH+f6B+QKiPyiX0dqhZME7oVJJ9wz0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C1C074BA2E3C Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-47f5c2283b6so25549545e9.1 for ; Sat, 24 Jan 2026 14:05:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769292308; x=1769897108; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=lesr/QekzWjWApODxVsASWuVGtzLggskFyDRfEXEmQM=; b=TRCvj9ipsSBoKiTyr2nsIl+0gdjQIX+mkNnCi+dPy5J09g8I6RRkeYWkMEroQbYE8L KipFZ/dRmvfV70vgvJkmGSwLnvXXb2AI9baQW5vpUOcQULvKxTgBbXDxGbxffplJNUJz d8Jl9TzPvt92Tcjd1VhkgXBgiStI47NjLw8nUa/Aff+2pcApgyGP2f8LsHMb5ifga4MM WSq/69Ljw3hflmns/K2epg938s7tUdWds765et/+A++ZHVXoM2NLSRjzQxEXna7esGCL LC9HnGedICqu+xxIwhrht/r6ZCwYgCuebn9KiOAXI2HwO7dTEYqJy09gpKOyG0cr5Gdz K5bA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769292308; x=1769897108; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=lesr/QekzWjWApODxVsASWuVGtzLggskFyDRfEXEmQM=; b=N2LBxyaZ2DOdOvhM7Nsb0XQ6u7VXbuJPSi+dIEQ271sxkq/uD762wcEVxFT5wFkIgK +c9yaN8Vln/gbx7VsPIKbsZ2pIFx/MvFopA8xZJPaCti1ydSzmqOuKEOhtdGro6LUtYj ZhZBr4SuFVcqxN6apSjPxY7xHeeSDXF2yjnsUN9HkDdq5qIB/BhZCVuoeX5DH3yMP+nw 4rMoVIbL/Ox63V866v4ccA4x2qj/I+J13i4lOuRUOSs3Zs6fWUYsSEMtTbCXKnAviy5g XmpYf6VErd1EnMsYWD2QmhmlFPmi9xIjsNgwI0IAnjt+s3iQJLTgRruEijyaB1fTYOPn Ehxg== X-Gm-Message-State: AOJu0YzVFaKxUM2vbmtZUyG2ei3uN+rKFyznsxmA3buDibHNyK0k0zWW HuL8lyHZ8pRE3wthK5iMpnwlrp0r4ajV0drAoGUzO633KKxI3GiS2Uk0AqMKUA== X-Gm-Gg: AZuq6aJD18CEdC5niCqTaIEc7SQsibNYwK/fWPIBZ+tziMlUn/MjGdL7Zt4W/6x2W5o G02GOGKeSC64OsR6pfK4Ac2UQA1wUEVZPay6HmPtA3zP8qSFKlniCmoFb356Fa/pabwArhDdvE5 YHsRCRn9h7/jv+ODbeyUT73jyVlz0H6KQRDBETYlZF2bLcpfj+ju0pWqCPfA7Ere5oP/N3vRdbE 3r9njrO50FOyRdhkr0DchdXrXsqwVwRWFZ2/a51tYhHQ3Q7Y/fa2ANNiSR/JwK47cRe3vcQTNa7 oh+30sZD5mHhlv1uZojYCYvhOFfdggRaxdVJybMP/SSKK0XXNMnQwQY83HLBeaQub0YRX4jqZpb JvtPHfBvDB8kVKt1ewhhLNKN30EHncawl1YNx9s9jN7qVyXBxsIY6Fuoc9M1m7ogaZuZg0g3tA/ g8k20foBXWQLd2P4Gf X-Received: by 2002:a05:600c:b86:b0:475:e067:f23d with SMTP id 5b1f17b1804b1-4805d0643d0mr586755e9.25.1769292308033; Sat, 24 Jan 2026 14:05:08 -0800 (PST) Received: from localhost ([2a01:4b00:d036:ae00:4db5:44b3:3bb9:453b]) by smtp.gmail.com with UTF8SMTPSA id 5b1f17b1804b1-4804dbab1b8sm57147605e9.8.2026.01.24.14.05.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 24 Jan 2026 14:05:07 -0800 (PST) From: luca.boccassi@gmail.com To: libc-alpha@sourceware.org Subject: [PATCH] linux: use PIDFD_GET_INFO ioctl for pidfd_getpid() if available Date: Sat, 24 Jan 2026 22:04:25 +0000 Message-ID: <20260124220504.709905-1-luca.boccassi@gmail.com> X-Mailer: git-send-email 2.47.3 MIME-Version: 1.0 X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_LINEPADDING, RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org From: Luca Boccassi Linux v6.13 introduced a new ioctl to query info from a pidfd. The advantage of this vs. parsing /proc/ is that it works even when procfs is not mounted. It's also a single syscall, and doesn't need manual string parsing. Use it when available. sysdeps/unix/sysv/linux/configure regenerated with: $ autoconf sysdeps/unix/sysv/linux/configure.ac > sysdeps/unix/sysv/linux/configure Signed-off-by: Luca Boccassi --- config.h.in | 3 + sysdeps/unix/sysv/linux/configure | 133 +++++++++++++++++++++++++ sysdeps/unix/sysv/linux/configure.ac | 3 + sysdeps/unix/sysv/linux/pidfd_getpid.c | 57 +++++++++-- 4 files changed, 188 insertions(+), 8 deletions(-) diff --git a/config.h.in b/config.h.in index 8b4077f578..6729404578 100644 --- a/config.h.in +++ b/config.h.in @@ -216,6 +216,9 @@ /* An integer used to scale the timeout of test programs. */ #define TIMEOUTFACTOR 1 +/* Define if linux/pidfd.h is available. */ +#undef HAVE_LINUX_PIDFD_H + /* */ diff --git a/sysdeps/unix/sysv/linux/configure b/sysdeps/unix/sysv/linux/configure index 4d642e953b..797c04d8c3 100644 --- a/sysdeps/unix/sysv/linux/configure +++ b/sysdeps/unix/sysv/linux/configure @@ -1,3 +1,99 @@ +# as_fn_append VAR VALUE +# ---------------------- +# Append the text in VALUE to the end of the definition contained in VAR. Take +# advantage of any shell optimizations that allow amortized linear growth over +# repeated appends, instead of the typical quadratic growth present in naive +# implementations. +if (eval "as_var=1; as_var+=2; test x\$as_var = x12") 2>/dev/null +then : + eval 'as_fn_append () + { + eval $1+=\$2 + }' +else case e in #( + e) as_fn_append () + { + eval $1=\$$1\$2 + } ;; +esac +fi # as_fn_append + +# Factoring default headers for most tests. +ac_includes_default="\ +#include +#ifdef HAVE_STDIO_H +# include +#endif +#ifdef HAVE_STDLIB_H +# include +#endif +#ifdef HAVE_STRING_H +# include +#endif +#ifdef HAVE_INTTYPES_H +# include +#endif +#ifdef HAVE_STDINT_H +# include +#endif +#ifdef HAVE_STRINGS_H +# include +#endif +#ifdef HAVE_SYS_TYPES_H +# include +#endif +#ifdef HAVE_SYS_STAT_H +# include +#endif +#ifdef HAVE_UNISTD_H +# include +#endif" + +ac_header_c_list= + +# ac_fn_c_check_header_compile LINENO HEADER VAR INCLUDES +# ------------------------------------------------------- +# Tests whether HEADER exists and can be compiled using the include files in +# INCLUDES, setting the cache variable VAR accordingly. +ac_fn_c_check_header_compile () +{ + as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for $2" >&5 +printf %s "checking for $2... " >&6; } +if eval test \${$3+y} +then : + printf %s "(cached) " >&6 +else case e in #( + e) cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +$4 +#include <$2> +_ACEOF +if ac_fn_c_try_compile "$LINENO" +then : + eval "$3=yes" +else case e in #( + e) eval "$3=no" ;; +esac +fi +rm -f core conftest.err conftest.$ac_objext conftest.beam conftest.$ac_ext ;; +esac +fi +eval ac_res=\$$3 + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5 +printf "%s\n" "$ac_res" >&6; } + eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno + +} # ac_fn_c_check_header_compile +as_fn_append ac_header_c_list " stdio.h stdio_h HAVE_STDIO_H" +as_fn_append ac_header_c_list " stdlib.h stdlib_h HAVE_STDLIB_H" +as_fn_append ac_header_c_list " string.h string_h HAVE_STRING_H" +as_fn_append ac_header_c_list " inttypes.h inttypes_h HAVE_INTTYPES_H" +as_fn_append ac_header_c_list " stdint.h stdint_h HAVE_STDINT_H" +as_fn_append ac_header_c_list " strings.h strings_h HAVE_STRINGS_H" +as_fn_append ac_header_c_list " sys/stat.h sys_stat_h HAVE_SYS_STAT_H" +as_fn_append ac_header_c_list " sys/types.h sys_types_h HAVE_SYS_TYPES_H" +as_fn_append ac_header_c_list " unistd.h unistd_h HAVE_UNISTD_H" # This file is generated from configure.ac by Autoconf. DO NOT EDIT! # Local configure fragment for sysdeps/unix/sysv/linux. @@ -132,6 +228,43 @@ if test "$prefix" = "/usr/local" -o "$prefix" = "/usr/local/" -o "$prefix" = "NO fi fi +ac_header= ac_cache= +for ac_item in $ac_header_c_list +do + if test $ac_cache; then + ac_fn_c_check_header_compile "$LINENO" $ac_header ac_cv_header_$ac_cache "$ac_includes_default" + if eval test \"x\$ac_cv_header_$ac_cache\" = xyes; then + printf "%s\n" "#define $ac_item 1" >> confdefs.h + fi + ac_header= ac_cache= + elif test $ac_header; then + ac_cache=$ac_item + else + ac_header=$ac_item + fi +done + + + + + + + + +if test $ac_cv_header_stdlib_h = yes && test $ac_cv_header_string_h = yes +then : + +printf "%s\n" "#define STDC_HEADERS 1" >>confdefs.h + +fi +ac_fn_c_check_header_compile "$LINENO" "linux/pidfd.h" "ac_cv_header_linux_pidfd_h" "$ac_includes_default" +if test "x$ac_cv_header_linux_pidfd_h" = xyes +then : + printf "%s\n" "#define HAVE_LINUX_PIDFD_H 1" >>confdefs.h + +fi + + # One Linux we use ldconfig. use_ldconfig=yes diff --git a/sysdeps/unix/sysv/linux/configure.ac b/sysdeps/unix/sysv/linux/configure.ac index c177e082a9..8beb2670f5 100644 --- a/sysdeps/unix/sysv/linux/configure.ac +++ b/sysdeps/unix/sysv/linux/configure.ac @@ -87,6 +87,9 @@ if test "$prefix" = "/usr/local" -o "$prefix" = "/usr/local/" -o "$prefix" = "NO fi fi +# New in kernel 5.10 +AC_CHECK_HEADERS([linux/pidfd.h]) + # One Linux we use ldconfig. use_ldconfig=yes diff --git a/sysdeps/unix/sysv/linux/pidfd_getpid.c b/sysdeps/unix/sysv/linux/pidfd_getpid.c index 4f16865c7e..80d4d48058 100644 --- a/sysdeps/unix/sysv/linux/pidfd_getpid.c +++ b/sysdeps/unix/sysv/linux/pidfd_getpid.c @@ -19,6 +19,10 @@ #include <_itoa.h> #include #include +#ifdef HAVE_LINUX_PIDFD_H +#include +#include +#endif #include #include #include @@ -91,15 +95,9 @@ parse_fdinfo (const char *l, void *arg) return 1; } -pid_t -pidfd_getpid (int fd) +static pid_t +getpid_fdinfo (int fd) { - if (__glibc_unlikely (fd < 0)) - { - __set_errno (EBADF); - return -1; - } - char fdinfoname[FDINFO_FILENAME_LEN]; char *p = mempcpy (fdinfoname, FDINFO_TO_FILENAME_PREFIX, @@ -126,3 +124,46 @@ pidfd_getpid (int fd) return fdinfo.pid; } + +static pid_t +getpid_ioctl (int fd) +{ +/* New in kernel 6.13 */ +#ifdef PIDFD_GET_INFO + static bool cached_supported = true; + struct pidfd_info info = { .mask = PIDFD_INFO_PID }; + + if (!cached_supported) + return -EOPNOTSUPP; + + if (ioctl (fd, PIDFD_GET_INFO, &info) < 0) + { + if (errno == ENOSYS || errno == EINVAL || errno == ENOTTY || errno == EOPNOTSUPP) + { + cached_supported = false; + return -EOPNOTSUPP; + } + + return INLINE_SYSCALL_ERROR_RETURN_VALUE (errno); + } + return info.pid; +#else + return -EOPNOTSUPP; +#endif +} + +pid_t +pidfd_getpid (int fd) +{ + if (__glibc_unlikely (fd < 0)) + { + __set_errno (EBADF); + return -1; + } + + int pid = getpid_ioctl (fd); + if (pid != -EOPNOTSUPP) + return pid; + + return getpid_fdinfo (fd); +}