Skip to content

Commit 6e40d36

Browse files
committed
Add a syscall fallback for os.statx
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.
1 parent 45e5b1b commit 6e40d36

6 files changed

Lines changed: 169 additions & 77 deletions

File tree

‎Doc/library/os.rst‎

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3501,10 +3501,14 @@ features:
35013501

35023502
.. seealso:: The :manpage:`statx(2)` man page.
35033503

3504-
.. availability:: Linux >= 4.11 with glibc >= 2.28.
3504+
.. availability:: Linux >= 4.11.
35053505

35063506
.. versionadded:: 3.15
35073507

3508+
.. versionchanged:: 3.16
3509+
The function is also available when Python is built against a libc
3510+
that lacks ``statx()``, such as glibc older than 2.28.
3511+
35083512

35093513
.. class:: statx_result
35103514

@@ -3533,7 +3537,7 @@ features:
35333537
Equal to ``None`` if :data:`STATX_WRITE_ATOMIC` is missing from
35343538
:attr:`~statx_result.stx_mask`.
35353539

3536-
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3540+
.. availability:: Linux >= 4.11 and build-time kernel
35373541
userspace API headers >= 6.11.
35383542

35393543
.. attribute:: stx_atomic_write_unit_max
@@ -3543,7 +3547,7 @@ features:
35433547
Equal to ``None`` if :data:`STATX_WRITE_ATOMIC` is missing from
35443548
:attr:`~statx_result.stx_mask`.
35453549

3546-
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3550+
.. availability:: Linux >= 4.11 and build-time kernel
35473551
userspace API headers >= 6.11.
35483552

35493553
.. attribute:: stx_atomic_write_unit_max_opt
@@ -3553,7 +3557,7 @@ features:
35533557
Equal to ``None`` if :data:`STATX_WRITE_ATOMIC` is missing from
35543558
:attr:`~statx_result.stx_mask`.
35553559

3556-
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3560+
.. availability:: Linux >= 4.11 and build-time kernel
35573561
userspace API headers >= 6.16.
35583562

35593563
.. attribute:: stx_atomic_write_unit_min
@@ -3563,7 +3567,7 @@ features:
35633567
Equal to ``None`` if :data:`STATX_WRITE_ATOMIC` is missing from
35643568
:attr:`~statx_result.stx_mask`.
35653569

3566-
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3570+
.. availability:: Linux >= 4.11 and build-time kernel
35673571
userspace API headers >= 6.11.
35683572

35693573
.. attribute:: stx_attributes
@@ -3637,7 +3641,7 @@ features:
36373641
Equal to ``None`` if :data:`STATX_DIOALIGN` is missing from
36383642
:attr:`~statx_result.stx_mask`.
36393643

3640-
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3644+
.. availability:: Linux >= 4.11 and build-time kernel
36413645
userspace API headers >= 6.1.
36423646

36433647
.. attribute:: stx_dio_offset_align
@@ -3647,7 +3651,7 @@ features:
36473651
Equal to ``None`` if :data:`STATX_DIOALIGN` is missing from
36483652
:attr:`~statx_result.stx_mask`.
36493653

3650-
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3654+
.. availability:: Linux >= 4.11 and build-time kernel
36513655
userspace API headers >= 6.1.
36523656

36533657
.. attribute:: stx_dio_read_offset_align
@@ -3657,7 +3661,7 @@ features:
36573661
Equal to ``None`` if :data:`STATX_DIO_READ_ALIGN` is missing from
36583662
:attr:`~statx_result.stx_mask`.
36593663

3660-
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3664+
.. availability:: Linux >= 4.11 and build-time kernel
36613665
userspace API headers >= 6.14.
36623666

36633667
.. attribute:: stx_gid
@@ -3686,7 +3690,7 @@ features:
36863690
Equal to ``None`` if :data:`STATX_MNT_ID` is missing from
36873691
:attr:`~statx_result.stx_mask`.
36883692

3689-
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3693+
.. availability:: Linux >= 4.11 and build-time kernel
36903694
userspace API headers >= 5.8.
36913695

36923696
.. attribute:: stx_mode
@@ -3746,7 +3750,7 @@ features:
37463750
Equal to ``None`` if :data:`STATX_SUBVOL` is missing from
37473751
:attr:`~statx_result.stx_mask`.
37483752

3749-
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3753+
.. availability:: Linux >= 4.11 and build-time kernel
37503754
userspace API headers >= 6.10.
37513755

37523756
.. attribute:: stx_uid
@@ -3758,7 +3762,7 @@ features:
37583762

37593763
.. seealso:: The :manpage:`statx(2)` man page.
37603764

3761-
.. availability:: Linux >= 4.11 with glibc >= 2.28.
3765+
.. availability:: Linux >= 4.11.
37623766

37633767
.. versionadded:: 3.15
37643768

@@ -3787,7 +3791,7 @@ features:
37873791
flags may be available even when their corresponding members in
37883792
:class:`statx_result` are not available.
37893793

3790-
.. availability:: Linux >= 4.11 with glibc >= 2.28.
3794+
.. availability:: Linux >= 4.11.
37913795

37923796
.. versionadded:: 3.15
37933797

@@ -3797,7 +3801,7 @@ features:
37973801
up-to-date information even when doing so is expensive (for example,
37983802
requiring a round trip to the server for a file on a network filesystem).
37993803

3800-
.. availability:: Linux >= 4.11 with glibc >= 2.28.
3804+
.. availability:: Linux >= 4.11.
38013805

38023806
.. versionadded:: 3.15
38033807

@@ -3806,7 +3810,7 @@ features:
38063810
A flag for the :func:`os.statx` function. Requests that the kernel return
38073811
cached information if possible.
38083812

3809-
.. availability:: Linux >= 4.11 with glibc >= 2.28.
3813+
.. availability:: Linux >= 4.11.
38103814

38113815
.. versionadded:: 3.15
38123816

@@ -3818,7 +3822,7 @@ features:
38183822
In the absence of the other two flags, the kernel will generally return
38193823
information as fresh as :func:`os.stat` would return.
38203824

3821-
.. availability:: Linux >= 4.11 with glibc >= 2.28.
3825+
.. availability:: Linux >= 4.11.
38223826

38233827
.. versionadded:: 3.15
38243828

‎Modules/clinic/posixmodule.c.h‎

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

‎Modules/posixmodule.c‎

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@
8989
#ifdef HAVE_SYS_STAT_H
9090
# include <sys/stat.h>
9191
#endif
92+
#if defined(__linux__) && !defined(HAVE_STRUCT_STATX) && defined(HAVE_LINUX_STAT_H)
93+
# include <linux/stat.h> // struct statx on older libc versions
94+
#endif
9295

9396
#ifdef HAVE_SYS_WAIT_H
9497
# include <sys/wait.h> // WNOHANG
@@ -416,9 +419,31 @@ extern char *ctermid_r(char *);
416419
# define STRUCT_STAT struct stat
417420
#endif
418421

419-
#ifdef HAVE_STATX
422+
#if defined(__linux__) && defined(__NR_statx) && \
423+
(defined(HAVE_STRUCT_STATX) || \
424+
(defined(HAVE_LINUX_STAT_H) && defined(STATX_BASIC_STATS)))
425+
# define _Py_HAVE_STATX
426+
#endif
427+
428+
#if defined(_Py_HAVE_STATX) && defined(HAVE_STATX)
420429
/* until we can assume glibc 2.28 at runtime, we must weakly link */
421430
# pragma weak statx
431+
#endif
432+
433+
#ifdef _Py_HAVE_STATX
434+
/* These Linux UAPI flags may be missing from older libc headers. Including
435+
linux/fcntl.h alongside fcntl.h would redefine libc structures. */
436+
# ifndef AT_NO_AUTOMOUNT
437+
# define AT_NO_AUTOMOUNT 0x800
438+
# endif
439+
# ifndef AT_EMPTY_PATH
440+
# define AT_EMPTY_PATH 0x1000
441+
# endif
442+
# ifndef AT_STATX_SYNC_AS_STAT
443+
# define AT_STATX_SYNC_AS_STAT 0x0000
444+
# define AT_STATX_FORCE_SYNC 0x2000
445+
# define AT_STATX_DONT_SYNC 0x4000
446+
# endif
422447
static const unsigned int _Py_STATX_KNOWN = (STATX_BASIC_STATS | STATX_BTIME
423448
#ifdef STATX_MNT_ID
424449
| STATX_MNT_ID
@@ -439,7 +464,7 @@ static const unsigned int _Py_STATX_KNOWN = (STATX_BASIC_STATS | STATX_BTIME
439464
| STATX_DIO_READ_ALIGN
440465
#endif
441466
);
442-
#endif /* HAVE_STATX */
467+
#endif /* _Py_HAVE_STATX */
443468

444469

445470
#if !defined(EX_OK) && defined(EXIT_SUCCESS)
@@ -1221,7 +1246,7 @@ typedef struct {
12211246
#endif
12221247
newfunc statresult_new_orig;
12231248
PyObject *StatResultType;
1224-
#ifdef HAVE_STATX
1249+
#ifdef _Py_HAVE_STATX
12251250
PyObject *StatxResultType;
12261251
#endif
12271252
PyObject *StatVFSResultType;
@@ -2590,7 +2615,7 @@ _posix_clear(PyObject *module)
25902615
Py_CLEAR(state->SchedParamType);
25912616
#endif
25922617
Py_CLEAR(state->StatResultType);
2593-
#ifdef HAVE_STATX
2618+
#ifdef _Py_HAVE_STATX
25942619
Py_CLEAR(state->StatxResultType);
25952620
#endif
25962621
Py_CLEAR(state->StatVFSResultType);
@@ -2618,7 +2643,7 @@ _posix_traverse(PyObject *module, visitproc visit, void *arg)
26182643
Py_VISIT(state->SchedParamType);
26192644
#endif
26202645
Py_VISIT(state->StatResultType);
2621-
#ifdef HAVE_STATX
2646+
#ifdef _Py_HAVE_STATX
26222647
Py_VISIT(state->StatxResultType);
26232648
#endif
26242649
Py_VISIT(state->StatVFSResultType);
@@ -3315,7 +3340,7 @@ os_lstat_impl(PyObject *module, path_t *path, int dir_fd)
33153340
}
33163341

33173342

3318-
#ifdef HAVE_STATX
3343+
#ifdef _Py_HAVE_STATX
33193344
typedef struct {
33203345
PyObject_HEAD
33213346
dev_t rdev, dev;
@@ -3671,12 +3696,21 @@ os_statx_impl(PyObject *module, path_t *path, unsigned int mask, int flags,
36713696
}
36723697

36733698
int result;
3674-
Py_BEGIN_ALLOW_THREADS
3699+
int statx_dir_fd = path->is_fd ? path->fd : dir_fd;
3700+
const char *statx_path = path->is_fd ? "" : path->narrow;
36753701
if (path->is_fd) {
3676-
result = statx(path->fd, "", flags | AT_EMPTY_PATH, mask, &v->stx);
3702+
flags |= AT_EMPTY_PATH;
36773703
}
3678-
else {
3679-
result = statx(dir_fd, path->narrow, flags, mask, &v->stx);
3704+
Py_BEGIN_ALLOW_THREADS
3705+
#ifdef HAVE_STATX
3706+
if (statx != NULL) {
3707+
result = statx(statx_dir_fd, statx_path, flags, mask, &v->stx);
3708+
}
3709+
else
3710+
#endif
3711+
{
3712+
result = syscall(__NR_statx, statx_dir_fd, statx_path, flags, mask,
3713+
&v->stx);
36803714
}
36813715
Py_END_ALLOW_THREADS
36823716

@@ -3691,7 +3725,7 @@ os_statx_impl(PyObject *module, path_t *path, unsigned int mask, int flags,
36913725
assert(!PyErr_Occurred());
36923726
return (PyObject *)v;
36933727
}
3694-
#endif /* HAVE_STATX */
3728+
#endif /* _Py_HAVE_STATX */
36953729

36963730

36973731
/*[clinic input]
@@ -18667,7 +18701,7 @@ all_ins(PyObject *m)
1866718701
if (PyModule_AddIntMacro(m, AT_NO_AUTOMOUNT)) return -1;
1866818702
#endif
1866918703

18670-
#ifdef HAVE_STATX
18704+
#ifdef _Py_HAVE_STATX
1867118705
if (PyModule_AddIntMacro(m, STATX_TYPE)) return -1;
1867218706
if (PyModule_AddIntMacro(m, STATX_MODE)) return -1;
1867318707
if (PyModule_AddIntMacro(m, STATX_NLINK)) return -1;
@@ -18704,7 +18738,7 @@ all_ins(PyObject *m)
1870418738
if (PyModule_AddIntMacro(m, AT_STATX_FORCE_SYNC)) return -1;
1870518739
if (PyModule_AddIntMacro(m, AT_STATX_DONT_SYNC)) return -1;
1870618740
/* STATX_ATTR_* constants are in the stat module */
18707-
#endif /* HAVE_STATX */
18741+
#endif /* _Py_HAVE_STATX */
1870818742

1870918743
#if defined(__APPLE__)
1871018744
if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1;
@@ -18977,21 +19011,10 @@ posixmodule_exec(PyObject *m)
1897719011
}
1897819012
#endif
1897919013

18980-
#ifdef HAVE_STATX
18981-
if (statx == NULL) {
18982-
PyObject* dct = PyModule_GetDict(m);
18983-
if (dct == NULL) {
18984-
return -1;
18985-
}
18986-
if (PyDict_PopString(dct, "statx", NULL) < 0) {
18987-
return -1;
18988-
}
18989-
}
18990-
else {
18991-
state->StatxResultType = PyType_FromModuleAndSpec(m, &pystatx_result_spec, NULL);
18992-
if (PyModule_AddObjectRef(m, "statx_result", state->StatxResultType) < 0) {
18993-
return -1;
18994-
}
19014+
#ifdef _Py_HAVE_STATX
19015+
state->StatxResultType = PyType_FromModuleAndSpec(m, &pystatx_result_spec, NULL);
19016+
if (PyModule_AddObjectRef(m, "statx_result", state->StatxResultType) < 0) {
19017+
return -1;
1899519018
}
1899619019
#endif
1899719020

0 commit comments

Comments
 (0)