Skip to content

gh-153400: Add a syscall fallback for os.statx - #159043

Open
jjhelmus wants to merge 7 commits into
python:mainfrom
jjhelmus:jjh/statx_syscall_fallback
Open

jjhelmus wants to merge 7 commits into
python:mainfrom
jjhelmus:jjh/statx_syscall_fallback

Conversation

@jjhelmus

@jjhelmus jjhelmus commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Following the pattern in #155520, this adds a syscall fallback for statx.

Expose os.statx when built with Linux headers that provide the syscall number and statx struct, even if the build time libc lacks a statx wrapper.

Call the libc wrapper when available, to keep the symbol versioning and fortification checks. Fall back to the syscall if the wrapper is absent, either at build or run time. If the run time kernel lacks the syscall an OSError with errno.ENOSYS will be raised.

Detect the optional statx struct fields from the available libc or kernel headers.

Comment thread Doc/library/os.rst Resolved
@KaiErikNiermann

Copy link
Copy Markdown

I believe this would now need the same try-catch on OSError as the other syscall fallbacks which rely on hasattr (otherwise I believe logging can crash on pre-statx-syscall kernels or the alrdy mentioned seccomp case before it has the chance to fallback to os.stat)

if hasattr(os, 'statx'):
statx_result = os.statx(filename,
os.STATX_BTIME|os.STATX_CTIME|os.STATX_MTIME)

e.g.

if not hasattr(os, 'pidfd_open'):
return False
try:
pid = os.getpid()
os.close(os.pidfd_open(pid, 0))
except OSError:
# blocked by security policy like SECCOMP
return False
return True

jjhelmus and others added 2 commits October 20, 2026 23:27
Expose os.statx when build with Linux headers that provide the syscall
number and struct statx, even if the build-time libc lacks a statx
wrapper.

Call the libc wrapper when available, to keep the symbol versioing and
fortification checks. Fall back to the syscall if the wrapper is absent,
either at build or run time.

Detect optional struct statx fields from the available libc or kernel headers.

os.statx is not longer exposed when the syscall is unavailable at build
time, even is libc provides a wrapper. This is a rare edge case where
glibc would emulate the behavior at runtime.
@ambv
ambv force-pushed the jjh/statx_syscall_fallback branch from 411cbb0 to 372ca48 Compare October 20, 2026 21:29
@read-the-docs-community

read-the-docs-community Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
📖 239 files changed · 2 added · 237 modified

Preview · build overview · cpython-previews · built Dec. 4, 2026, 6:19 p.m. UTC

@python-cla-bot

python-cla-bot Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@ambv
ambv force-pushed the jjh/statx_syscall_fallback branch from db0e4e4 to 943aa85 Compare October 21, 2026 01:53
Comment thread Doc/library/os.rst Resolved
Comment thread Doc/library/os.rst Resolved
@emmatyping

Copy link
Copy Markdown
Member

os.statx is no longer exposed when the syscall is unavailable at build time, even if libc provides a wrapper. This is a rare edge case where glibc would emulate the behavior of statx at runtime.

Is there a reason not to do this? AIUI it really is just changing the header check to HAVE_X || (__linux__ && __NR_x). Then if someone is relying on a glibc fallback they can keep using that, and the code isn't really that much more complicated I don't think?

@jjhelmus
jjhelmus requested a review from vsajip as a code owner December 4, 2026 18:16
@jjhelmus

jjhelmus commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Finally got back to this.
I updated the logging handler to use a try-catch to cover seccomp cases.
The selection logic makes os.statx available if either libc provides a wrapper or the kernel header provide the syscall and statx structure. Docs are updated to mention this.

jjhelmus added a commit to astral-sh/python-build-standalone that referenced this pull request Dec 4, 2026
Backport python/cpython#159043 to 3.15 and 3.16 to provide os.statx on
Linux for targets where the glibc version does not provide the wrapper.

closes #1316
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants