From patchwork Mon Jan 26 19:34:15 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luca Boccassi X-Patchwork-Id: 128992 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 A40264BA9025 for ; Mon, 26 Jan 2026 19:35:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A40264BA9025 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=QKTBTfp0 X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by sourceware.org (Postfix) with ESMTPS id CCF0F4BA2E07 for ; Mon, 26 Jan 2026 19:35:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CCF0F4BA2E07 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 CCF0F4BA2E07 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::32c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1769456116; cv=none; b=pZHiUWnU+002JI1vS7CVlSEM3mwkSEQqn6wb8OPOBekHsVP1N9vIUQmuksttAj74bhyAQQoxwFoiIhDhxPOVcS+w/mHZMBLuQgTKMe8G3lkZxY7giLUn5HXCY0143kGpL3F6pQ6HkyE6Fud7Y5Ys7F0J/D6lM29AzYKGRXG1S6k= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1769456116; c=relaxed/simple; bh=ZIjQBDl8KiO9O3w4oHRlTEAyyJlfgiUls8bXtM0o0YY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ur+QXsbkWhpSP4vMKw2R1eGFieSq93LeFyvRnwmk2UEej3esT794RMVW09lum889iWRegwg2RlCxYuIhSWil3q7HyjzEdB/hhh3OJD6UYWQA2Z+wMaCdURRlJguheHRM/bJ2fJ96wEm3BIj7vDtWasH0Kq9qZ0mkUtEeJg87pwg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CCF0F4BA2E07 Received: by mail-wm1-x32c.google.com with SMTP id 5b1f17b1804b1-47ee4539adfso53044835e9.3 for ; Mon, 26 Jan 2026 11:35:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769456114; x=1770060914; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=rtPfzd5rNBTnA5uFKpqrthXwA7qvX3vHwqw2xB7bwo0=; b=QKTBTfp0k+e3Ovrj+7WOY6Y+66XY/E3WjTrJL/BqczI9iH+idiyu3H8t3nC99Mc5RX HoqVbZHre4KbFEtviYqMqdvLgVeBimjo82pbSnLNLZfTo6QHhoD3EjOJ8M7XEjp1AdBb HT++oLZrVB22mIWeDSmaf6DIBnDzC/vZ5c8vcf4W3s6qDJFwG439r1hJXXUaCvG3X0QJ mCs/+/iBJpCzTzfL+a7tM+7/9ez1YthenqwrL6kXU1yLFBBQISjx0wXJAJe6Svt0uwtB TGsMG1n57QWeiJ4xfK5YrH6hwVqLbFFEA0002J1uvZrXXUzxffbgGEbKu2mP90KaGlKG fx9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769456114; x=1770060914; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=rtPfzd5rNBTnA5uFKpqrthXwA7qvX3vHwqw2xB7bwo0=; b=J5Fo0vGOulNaTK+QHbGEk/YT+aaswMUSnrTqytNsLQVEM2ko8PPYBzf06PdyWqCLH+ dJaVki7koMvHp4treLYYSOBhIJo+nCWWaTxxd3Ke5m9pKVKKwNPhZkUhPCLs91GbEPCU LnonLiQfjnI+BlHou1hZ/9HvIt4OcraTt3bKxwbt6AiRBnXC35olZTtL7Zs0jacbAmFz +VfP8k6W4/7XoJYo9mRBUcG+2olhSzvkf8ZWhbXf4jloKXLsKqJP8v/pLvV51tLLIe0h KnZrcfGC5AZDzvGH358uNIczJHJO+TIYOMOmOr5bI7hGua1+z+F7i/uRSwmQP1RmsQz6 4KMw== X-Gm-Message-State: AOJu0YyQOxRRLDp3aatISAwtmnBIceIJTg1Rt8ls8LVlhhre7k5SPqu6 Mm2O5tn8VU2kUhoqL1XdYzczMZH14mRSmZerXKNK+R0V7jOcPiWEMt5HIZlNLQ== X-Gm-Gg: AZuq6aJeDvixDyWR/AaV9epRNKm42YXPVFHbK6hL6012m+8NDRtj6/gUJApWkwGjTNW 77jenTU+WbEhaphoyxyKzqfMAui5zXT0Y6Og5659AR7zygkyb1tZqFA5KRVMBaGoVMfYWQ41pCW Ht3PDQrOdLOFbCcHWkp+jJqDWxy8DA9zL7IsfBwfQNMwM70xsrIBkWX4hf7zFhagkUXN7EfAa1i LWnsewWIYKNkNNwWqbzXBVyE2+qt9SsK2ISYKgYrsJbZK0zV/7GBdkZcz6EajoCg7mk97cSJH2K IL9VY7Qfn3BDS4kS4prrB4E8Q8V2+EsE4pXgp2g7NjWXcDBebyCqsvbKf/c5LCm0CHczaQ5RyXE 3Qsc2fQO0qcCPAhNQo8UBEynqO/XH1LBc8DzSojDkR8N9M1fjAfy3u7X8doFi5YdggQ/w5K36Mm dxSnZr45GF0r6Ljoo5 X-Received: by 2002:a05:600c:c16e:b0:480:19ed:7efa with SMTP id 5b1f17b1804b1-480631ece15mr43333485e9.36.1769456114297; Mon, 26 Jan 2026 11:35:14 -0800 (PST) Received: from localhost ([2a01:4b00:d036:ae00:53d2:28bc:8a2d:b680]) by smtp.gmail.com with UTF8SMTPSA id 5b1f17b1804b1-4804d4faecbsm111421185e9.1.2026.01.26.11.35.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Jan 2026 11:35:13 -0800 (PST) From: luca.boccassi@gmail.com To: libc-alpha@sourceware.org Cc: adhemerval.zanella@linaro.org, fweimer@redhat.com Subject: [PATCH v6] linux: use PIDFD_GET_INFO ioctl for pidfd_getpid() if available Date: Mon, 26 Jan 2026 19:34:15 +0000 Message-ID: <20260126193510.396307-1-luca.boccassi@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260124220504.709905-1-luca.boccassi@gmail.com> References: <20260124220504.709905-1-luca.boccassi@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE, SPF_PASS, TXREP 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. Signed-off-by: Luca Boccassi --- v2: use sys/pidfd.h instead of installed kernel headers v3: add __ASSUME_PIDFD_GET_INFO use atomics for support cache var v4: drop support cache var v5: drop static helper given it's just the ioctl call now, simplifies the flow v6: map ENOTTY to EBADF to avoid breaking documented behaviour sysdeps/unix/sysv/linux/kernel-features.h | 8 +++++ sysdeps/unix/sysv/linux/pidfd_getpid.c | 44 ++++++++++++++++++----- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/sysdeps/unix/sysv/linux/kernel-features.h b/sysdeps/unix/sysv/linux/kernel-features.h index 01c865e2d5..94bdbbb730 100644 --- a/sysdeps/unix/sysv/linux/kernel-features.h +++ b/sysdeps/unix/sysv/linux/kernel-features.h @@ -269,4 +269,12 @@ # define __ASSUME_MSEAL 0 #endif +/* The PIDFD_GET_INFO ioctl was introduced across all architectures in Linux + 6.13. */ +#if __LINUX_KERNEL_VERSION >= 0x060D00 +# define __ASSUME_PIDFD_GET_INFO 1 +#else +# define __ASSUME_PIDFD_GET_INFO 0 +#endif + #endif /* kernel-features.h */ diff --git a/sysdeps/unix/sysv/linux/pidfd_getpid.c b/sysdeps/unix/sysv/linux/pidfd_getpid.c index 860829cf07..e0f1b7bf2b 100644 --- a/sysdeps/unix/sysv/linux/pidfd_getpid.c +++ b/sysdeps/unix/sysv/linux/pidfd_getpid.c @@ -23,8 +23,11 @@ #include #include #include +#include +#include #include +#if !__ASSUME_PIDFD_GET_INFO #define FDINFO_TO_FILENAME_PREFIX "/proc/self/fdinfo/" #define FDINFO_FILENAME_LEN \ @@ -91,15 +94,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 +123,34 @@ pidfd_getpid (int fd) return fdinfo.pid; } +#endif + +pid_t +pidfd_getpid (int fd) +{ + struct pidfd_info info = { .mask = PIDFD_INFO_PID }; + + if (__glibc_unlikely (fd < 0)) + { + __set_errno (EBADF); + return -1; + } + + /* New in kernel 6.13 */ + if (ioctl (fd, PIDFD_GET_INFO, &info) < 0) + { +#if __ASSUME_PIDFD_GET_INFO + /* The procfs implementation returns EBADF when called with a non-pidfd + fd, change the errno to keep it consistent across implementations. */ + if (errno == ENOTTY) + return INLINE_SYSCALL_ERROR_RETURN_VALUE (EBADF); +#else + if (errno == ENOTTY || errno == EINVAL) + return getpid_fdinfo (fd); +#endif + + return INLINE_SYSCALL_ERROR_RETURN_VALUE (errno); + } + + return info.pid; +}