Skip to content

Commit a87dc23

Browse files
jjhelmusambv
authored andcommitted
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 39bc958 commit a87dc23

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
@@ -3544,10 +3544,14 @@ features:
35443544

35453545
.. seealso:: The :manpage:`statx(2)` man page.
35463546

3547-
.. availability:: Linux >= 4.11 with glibc >= 2.28.
3547+
.. availability:: Linux >= 4.11.
35483548

35493549
.. versionadded:: 3.15
35503550

3551+
.. versionchanged:: 3.16
3552+
The function is also available when Python is built against a libc
3553+
that lacks ``statx()``, such as glibc older than 2.28.
3554+
35513555

35523556
.. class:: statx_result
35533557

@@ -3576,7 +3580,7 @@ features:
35763580
Equal to ``None`` if :data:`STATX_WRITE_ATOMIC` is missing from
35773581
:attr:`~statx_result.stx_mask`.
35783582

3579-
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3583+
.. availability:: Linux >= 4.11 and build-time kernel
35803584
userspace API headers >= 6.11.
35813585

35823586
.. attribute:: stx_atomic_write_unit_max
@@ -3586,7 +3590,7 @@ features:
35863590
Equal to ``None`` if :data:`STATX_WRITE_ATOMIC` is missing from
35873591
:attr:`~statx_result.stx_mask`.
35883592

3589-
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3593+
.. availability:: Linux >= 4.11 and build-time kernel
35903594
userspace API headers >= 6.11.
35913595

35923596
.. attribute:: stx_atomic_write_unit_max_opt
@@ -3596,7 +3600,7 @@ features:
35963600
Equal to ``None`` if :data:`STATX_WRITE_ATOMIC` is missing from
35973601
:attr:`~statx_result.stx_mask`.
35983602

3599-
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3603+
.. availability:: Linux >= 4.11 and build-time kernel
36003604
userspace API headers >= 6.16.
36013605

36023606
.. attribute:: stx_atomic_write_unit_min
@@ -3606,7 +3610,7 @@ features:
36063610
Equal to ``None`` if :data:`STATX_WRITE_ATOMIC` is missing from
36073611
:attr:`~statx_result.stx_mask`.
36083612

3609-
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3613+
.. availability:: Linux >= 4.11 and build-time kernel
36103614
userspace API headers >= 6.11.
36113615

36123616
.. attribute:: stx_attributes
@@ -3680,7 +3684,7 @@ features:
36803684
Equal to ``None`` if :data:`STATX_DIOALIGN` is missing from
36813685
:attr:`~statx_result.stx_mask`.
36823686

3683-
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3687+
.. availability:: Linux >= 4.11 and build-time kernel
36843688
userspace API headers >= 6.1.
36853689

36863690
.. attribute:: stx_dio_offset_align
@@ -3690,7 +3694,7 @@ features:
36903694
Equal to ``None`` if :data:`STATX_DIOALIGN` is missing from
36913695
:attr:`~statx_result.stx_mask`.
36923696

3693-
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3697+
.. availability:: Linux >= 4.11 and build-time kernel
36943698
userspace API headers >= 6.1.
36953699

36963700
.. attribute:: stx_dio_read_offset_align
@@ -3700,7 +3704,7 @@ features:
37003704
Equal to ``None`` if :data:`STATX_DIO_READ_ALIGN` is missing from
37013705
:attr:`~statx_result.stx_mask`.
37023706

3703-
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3707+
.. availability:: Linux >= 4.11 and build-time kernel
37043708
userspace API headers >= 6.14.
37053709

37063710
.. attribute:: stx_gid
@@ -3729,7 +3733,7 @@ features:
37293733
Equal to ``None`` if :data:`STATX_MNT_ID` is missing from
37303734
:attr:`~statx_result.stx_mask`.
37313735

3732-
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3736+
.. availability:: Linux >= 4.11 and build-time kernel
37333737
userspace API headers >= 5.8.
37343738

37353739
.. attribute:: stx_mode
@@ -3789,7 +3793,7 @@ features:
37893793
Equal to ``None`` if :data:`STATX_SUBVOL` is missing from
37903794
:attr:`~statx_result.stx_mask`.
37913795

3792-
.. availability:: Linux >= 4.11 with glibc >= 2.28 and build-time kernel
3796+
.. availability:: Linux >= 4.11 and build-time kernel
37933797
userspace API headers >= 6.10.
37943798

37953799
.. attribute:: stx_uid
@@ -3801,7 +3805,7 @@ features:
38013805

38023806
.. seealso:: The :manpage:`statx(2)` man page.
38033807

3804-
.. availability:: Linux >= 4.11 with glibc >= 2.28.
3808+
.. availability:: Linux >= 4.11.
38053809

38063810
.. versionadded:: 3.15
38073811

@@ -3830,7 +3834,7 @@ features:
38303834
flags may be available even when their corresponding members in
38313835
:class:`statx_result` are not available.
38323836

3833-
.. availability:: Linux >= 4.11 with glibc >= 2.28.
3837+
.. availability:: Linux >= 4.11.
38343838

38353839
.. versionadded:: 3.15
38363840

@@ -3840,7 +3844,7 @@ features:
38403844
up-to-date information even when doing so is expensive (for example,
38413845
requiring a round trip to the server for a file on a network filesystem).
38423846

3843-
.. availability:: Linux >= 4.11 with glibc >= 2.28.
3847+
.. availability:: Linux >= 4.11.
38443848

38453849
.. versionadded:: 3.15
38463850

@@ -3849,7 +3853,7 @@ features:
38493853
A flag for the :func:`os.statx` function. Requests that the kernel return
38503854
cached information if possible.
38513855

3852-
.. availability:: Linux >= 4.11 with glibc >= 2.28.
3856+
.. availability:: Linux >= 4.11.
38533857

38543858
.. versionadded:: 3.15
38553859

@@ -3861,7 +3865,7 @@ features:
38613865
In the absence of the other two flags, the kernel will generally return
38623866
information as fresh as :func:`os.stat` would return.
38633867

3864-
.. availability:: Linux >= 4.11 with glibc >= 2.28.
3868+
.. availability:: Linux >= 4.11.
38653869

38663870
.. versionadded:: 3.15
38673871

‎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
@@ -90,6 +90,9 @@
9090
#ifdef HAVE_SYS_STAT_H
9191
# include <sys/stat.h>
9292
#endif
93+
#if defined(__linux__) && !defined(HAVE_STRUCT_STATX) && defined(HAVE_LINUX_STAT_H)
94+
# include <linux/stat.h> // struct statx on older libc versions
95+
#endif
9396

9497
#ifdef HAVE_SYS_WAIT_H
9598
# include <sys/wait.h> // WNOHANG
@@ -420,9 +423,31 @@ extern char *ctermid_r(char *);
420423
# define STRUCT_STAT struct stat
421424
#endif
422425

423-
#ifdef HAVE_STATX
426+
#if defined(__linux__) && defined(__NR_statx) && \
427+
(defined(HAVE_STRUCT_STATX) || \
428+
(defined(HAVE_LINUX_STAT_H) && defined(STATX_BASIC_STATS)))
429+
# define _Py_HAVE_STATX
430+
#endif
431+
432+
#if defined(_Py_HAVE_STATX) && defined(HAVE_STATX)
424433
/* until we can assume glibc 2.28 at runtime, we must weakly link */
425434
# pragma weak statx
435+
#endif
436+
437+
#ifdef _Py_HAVE_STATX
438+
/* These Linux UAPI flags may be missing from older libc headers. Including
439+
linux/fcntl.h alongside fcntl.h would redefine libc structures. */
440+
# ifndef AT_NO_AUTOMOUNT
441+
# define AT_NO_AUTOMOUNT 0x800
442+
# endif
443+
# ifndef AT_EMPTY_PATH
444+
# define AT_EMPTY_PATH 0x1000
445+
# endif
446+
# ifndef AT_STATX_SYNC_AS_STAT
447+
# define AT_STATX_SYNC_AS_STAT 0x0000
448+
# define AT_STATX_FORCE_SYNC 0x2000
449+
# define AT_STATX_DONT_SYNC 0x4000
450+
# endif
426451
static const unsigned int _Py_STATX_KNOWN = (STATX_BASIC_STATS | STATX_BTIME
427452
#ifdef STATX_MNT_ID
428453
| STATX_MNT_ID
@@ -443,7 +468,7 @@ static const unsigned int _Py_STATX_KNOWN = (STATX_BASIC_STATS | STATX_BTIME
443468
| STATX_DIO_READ_ALIGN
444469
#endif
445470
);
446-
#endif /* HAVE_STATX */
471+
#endif /* _Py_HAVE_STATX */
447472

448473

449474
#if !defined(EX_OK) && defined(EXIT_SUCCESS)
@@ -1235,7 +1260,7 @@ typedef struct {
12351260
#endif
12361261
newfunc statresult_new_orig;
12371262
PyObject *StatResultType;
1238-
#ifdef HAVE_STATX
1263+
#ifdef _Py_HAVE_STATX
12391264
PyObject *StatxResultType;
12401265
#endif
12411266
PyObject *StatVFSResultType;
@@ -2620,7 +2645,7 @@ _posix_clear(PyObject *module)
26202645
Py_CLEAR(state->SchedParamType);
26212646
#endif
26222647
Py_CLEAR(state->StatResultType);
2623-
#ifdef HAVE_STATX
2648+
#ifdef _Py_HAVE_STATX
26242649
Py_CLEAR(state->StatxResultType);
26252650
#endif
26262651
Py_CLEAR(state->StatVFSResultType);
@@ -2648,7 +2673,7 @@ _posix_traverse(PyObject *module, visitproc visit, void *arg)
26482673
Py_VISIT(state->SchedParamType);
26492674
#endif
26502675
Py_VISIT(state->StatResultType);
2651-
#ifdef HAVE_STATX
2676+
#ifdef _Py_HAVE_STATX
26522677
Py_VISIT(state->StatxResultType);
26532678
#endif
26542679
Py_VISIT(state->StatVFSResultType);
@@ -3337,7 +3362,7 @@ os_lstat_impl(PyObject *module, path_t *path, int dir_fd)
33373362
}
33383363

33393364

3340-
#ifdef HAVE_STATX
3365+
#ifdef _Py_HAVE_STATX
33413366
typedef struct {
33423367
PyObject_HEAD
33433368
dev_t rdev, dev;
@@ -3693,12 +3718,21 @@ os_statx_impl(PyObject *module, path_t *path, unsigned int mask, int flags,
36933718
}
36943719

36953720
int result;
3696-
Py_BEGIN_ALLOW_THREADS
3721+
int statx_dir_fd = path->is_fd ? path->fd : dir_fd;
3722+
const char *statx_path = path->is_fd ? "" : path->narrow;
36973723
if (path->is_fd) {
3698-
result = statx(path->fd, "", flags | AT_EMPTY_PATH, mask, &v->stx);
3724+
flags |= AT_EMPTY_PATH;
36993725
}
3700-
else {
3701-
result = statx(dir_fd, path->narrow, flags, mask, &v->stx);
3726+
Py_BEGIN_ALLOW_THREADS
3727+
#ifdef HAVE_STATX
3728+
if (statx != NULL) {
3729+
result = statx(statx_dir_fd, statx_path, flags, mask, &v->stx);
3730+
}
3731+
else
3732+
#endif
3733+
{
3734+
result = syscall(__NR_statx, statx_dir_fd, statx_path, flags, mask,
3735+
&v->stx);
37023736
}
37033737
Py_END_ALLOW_THREADS
37043738

@@ -3713,7 +3747,7 @@ os_statx_impl(PyObject *module, path_t *path, unsigned int mask, int flags,
37133747
assert(!PyErr_Occurred());
37143748
return (PyObject *)v;
37153749
}
3716-
#endif /* HAVE_STATX */
3750+
#endif /* _Py_HAVE_STATX */
37173751

37183752

37193753
/*[clinic input]
@@ -18808,7 +18842,7 @@ all_ins(PyObject *m)
1880818842
if (PyModule_AddIntMacro(m, AT_NO_AUTOMOUNT)) return -1;
1880918843
#endif
1881018844

18811-
#ifdef HAVE_STATX
18845+
#ifdef _Py_HAVE_STATX
1881218846
if (PyModule_AddIntMacro(m, STATX_TYPE)) return -1;
1881318847
if (PyModule_AddIntMacro(m, STATX_MODE)) return -1;
1881418848
if (PyModule_AddIntMacro(m, STATX_NLINK)) return -1;
@@ -18845,7 +18879,7 @@ all_ins(PyObject *m)
1884518879
if (PyModule_AddIntMacro(m, AT_STATX_FORCE_SYNC)) return -1;
1884618880
if (PyModule_AddIntMacro(m, AT_STATX_DONT_SYNC)) return -1;
1884718881
/* STATX_ATTR_* constants are in the stat module */
18848-
#endif /* HAVE_STATX */
18882+
#endif /* _Py_HAVE_STATX */
1884918883

1885018884
#if defined(__APPLE__)
1885118885
if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1;
@@ -19126,21 +19160,10 @@ posixmodule_exec(PyObject *m)
1912619160
}
1912719161
#endif
1912819162

19129-
#ifdef HAVE_STATX
19130-
if (statx == NULL) {
19131-
PyObject* dct = PyModule_GetDict(m);
19132-
if (dct == NULL) {
19133-
return -1;
19134-
}
19135-
if (PyDict_PopString(dct, "statx", NULL) < 0) {
19136-
return -1;
19137-
}
19138-
}
19139-
else {
19140-
state->StatxResultType = PyType_FromModuleAndSpec(m, &pystatx_result_spec, NULL);
19141-
if (PyModule_AddObjectRef(m, "statx_result", state->StatxResultType) < 0) {
19142-
return -1;
19143-
}
19163+
#ifdef _Py_HAVE_STATX
19164+
state->StatxResultType = PyType_FromModuleAndSpec(m, &pystatx_result_spec, NULL);
19165+
if (PyModule_AddObjectRef(m, "statx_result", state->StatxResultType) < 0) {
19166+
return -1;
1914419167
}
1914519168
#endif
1914619169

0 commit comments

Comments
 (0)