I noticed the existing FSTAT_O_PATH_BUG define internal to openat2.c
which would simplify some code I'm working on in coreutils chmod.c,
so the attached provides this through a HAVE_WORKING_FSTAT_O_PATH define.
cheers,
Padraig
From 46066961020bc23d1bd5e9f54acb7a576b604875 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <[email protected]>
Date: Sun, 22 Nov 2026 19:11:05 +0000
Subject: [PATCH] fcntl-h: Provide a HAVE_WORKING_FSTAT_O_PATH config define
* lib/openat2.c: Change from internal FSTAT_O_PATH_BUG to ...
* m4/fcntl-o.m4: ... provide HAVE_WORKING_FSTAT_O_PATH.
* doc/posix-functions/fstat.texi: Provide more detail on
the affected kernel range, and on the provided define.
* tests/test-fcntl-h.c: Ensure defined.
---
ChangeLog | 9 +++++++
doc/posix-functions/fstat.texi | 5 +++-
lib/openat2.c | 21 +++--------------
m4/fcntl-o.m4 | 43 +++++++++++++++++++++++++++++++++-
tests/test-fcntl-h.c | 5 ++++
5 files changed, 63 insertions(+), 20 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 50b324be40..3b8d0731dc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2026-11-22 Pádraig Brady <[email protected]>
+
+ fcntl-h: Provide a HAVE_WORKING_FSTAT_O_PATH config define
+ * lib/openat2.c: Change from internal FSTAT_O_PATH_BUG to ...
+ * m4/fcntl-o.m4: ... provide HAVE_WORKING_FSTAT_O_PATH.
+ * doc/posix-functions/fstat.texi: Provide more detail on
+ the affected kernel range, and on the provided define.
+ * tests/test-fcntl-h.c: Ensure defined.
+
2026-11-22 Bruno Haible <[email protected]>
getdate: Document portability problems.
diff --git a/doc/posix-functions/fstat.texi b/doc/posix-functions/fstat.texi
index ce6a9d5079..8ad2148c39 100644
--- a/doc/posix-functions/fstat.texi
+++ b/doc/posix-functions/fstat.texi
@@ -40,7 +40,10 @@ Portability problems not fixed by Gnulib:
@xref{sys/stat.h}, for general portability problems with @code{struct stat}.
@item
This function fails if the file descriptor was opened with @code{O_PATH}:
-Linux kernel 3.5.
+Linux kernels 2.6.39 through 3.5.
+The @code{fcntl-h} module defines @code{HAVE_WORKING_FSTAT_O_PATH} in
+@file{config.h} to 1 if @code{fstat} works on an @code{O_PATH} descriptor,
+and to 0 otherwise. When cross-compiling, it guesses from the target headers.
@item
On Cygwin, @code{fstat} applied to the file descriptors 0 and 1, returns
different @code{st_ino} values, even if standard input and standard output
diff --git a/lib/openat2.c b/lib/openat2.c
index 12c102c481..aef999270a 100644
--- a/lib/openat2.c
+++ b/lib/openat2.c
@@ -35,7 +35,6 @@
#if defined __linux__ || defined __ANDROID__
# include <sys/syscall.h>
-# include <linux/version.h>
# if HAVE_SYS_VFS_H && HAVE_FSTATFS && HAVE_STRUCT_STATFS_F_TYPE
# include <sys/vfs.h>
/* Linux-specific constant from coreutils src/fs.h. */
@@ -43,20 +42,6 @@
# endif
#endif
-/* FSTAT_O_PATH_BUG is true if fstat fails on O_PATH file descriptors.
- On these platforms fstatat with AT_EMPTY_PATH and "" similarly fails.
- Although it can be dicey to use static checks for Linux kernel versions,
- due to the dubious practice of building on newer kernels for older ones,
- do it here anyway as the buggy kernels are rare (all EOLed by 2016)
- and builders for them are unlikely to use the dubious practice.
- Circa 2030 we should remove the old-kernel workarounds entirely. */
-#ifdef LINUX_VERSION_CODE
-# define FSTAT_O_PATH_BUG (KERNEL_VERSION (2, 6, 39) <= LINUX_VERSION_CODE \
- && LINUX_VERSION_CODE < KERNEL_VERSION (3, 6, 0))
-#else
-# define FSTAT_O_PATH_BUG false
-#endif
-
#ifndef GNULIB_FSTATAT
# define GNULIB_FSTATAT 0
#endif
@@ -155,7 +140,7 @@ maybe_grow (char **buf, idx_t bufsize, char *stackbuf,
static int
dirstat (int dirfd, struct stat *st)
{
-#if FSTAT_O_PATH_BUG
+#if O_PATH && !HAVE_WORKING_FSTAT_O_PATH
/* Use fstatat with "." to work around a bug in fstat (and in
fstatat with "" and AT_EMPTY_PATH) in Linux kernels 2.6.39 (2011)
through 3.5.7 (2012). This workaround does not depend on
@@ -172,8 +157,8 @@ dirstat (int dirfd, struct stat *st)
/* Prefer fstatat with AT_EMPTY_PATH if available, as this avoids
admittedly-unlikely problems when the working directory is not
searchable. This workaround does not depend on Gnulib's fstatat
- module, as all systems with AT_EMPTY_PATH and without
- FSTAT_O_PATH_BUG should work well enough here. */
+ module, as all systems with AT_EMPTY_PATH reaching this point
+ should work well enough here. */
return fstatat (dirfd, GNULIB_FSTATAT ? NULL : "", st, AT_EMPTY_PATH);
# else
return stat (".", st);
diff --git a/m4/fcntl-o.m4 b/m4/fcntl-o.m4
index a94fc557e2..8a261b0c06 100644
--- a/m4/fcntl-o.m4
+++ b/m4/fcntl-o.m4
@@ -1,5 +1,5 @@
# fcntl-o.m4
-# serial 12
+# serial 13
dnl Copyright (C) 2006, 2009-2026 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
@@ -14,6 +14,7 @@ AC_PREREQ([2.60])
# Define HAVE_WORKING_O_DIRECTORY to 1 if O_DIRECTORY works, or to 0 otherwise.
# Define HAVE_WORKING_O_NOATIME to 1 if O_NOATIME works, or to 0 otherwise.
# Define HAVE_WORKING_O_NOFOLLOW to 1 if O_NOFOLLOW works, or to 0 otherwise.
+# Define HAVE_WORKING_FSTAT_O_PATH to 1 if fstat works on O_PATH descriptors.
AC_DEFUN([gl_FCNTL_O_FLAGS],
[
dnl Persuade glibc <fcntl.h> to define O_NOATIME and O_NOFOLLOW.
@@ -169,4 +170,44 @@ AC_DEFUN([gl_FCNTL_O_FLAGS],
[*O_NOFOLLOW* | *no], [gl_val=0], [gl_val=1])
AC_DEFINE_UNQUOTED([HAVE_WORKING_O_NOFOLLOW], [$gl_val],
[Define to 1 if O_NOFOLLOW works, 0 otherwise.])
+
+ dnl On Linux 2.6.39 through 3.5, fstat fails on O_PATH descriptors.
+ dnl On these platforms fstatat with AT_EMPTY_PATH and "" similarly
+ dnl fails. Although it can be dicey to use static checks due to
+ dnl the dubious practice of building on newer kernels for older ones,
+ dnl do it here anyway as the buggy kernels are rare (all EOLed by 2016)
+ dnl and builders for them are unlikely to use the dubious practice.
+ dnl Circa 2030 we should remove the old-kernel workarounds entirely.
+ AC_CACHE_CHECK([whether fstat works on O_PATH file descriptors],
+ [gl_cv_working_fstat_o_path],
+ [AC_RUN_IFELSE(
+ [AC_LANG_PROGRAM(
+ [[#include <fcntl.h>
+ #include <sys/stat.h>
+ #include <unistd.h>
+ #include <errno.h>]],
+ [[int fd = open ("confdefs.h", O_PATH);
+ struct stat st;
+ char c;
+ /* A kernel that ignores O_PATH opens this file for reading. */
+ return (fd < 0 || fstat (fd, &st) < 0
+ || read (fd, &c, 1) != -1 || errno != EBADF);]])],
+ [gl_cv_working_fstat_o_path=yes],
+ [gl_cv_working_fstat_o_path=no],
+ [AC_EGREP_CPP([working_fstat_o_path], [
+ #include <fcntl.h>
+ #if defined __linux__ || defined __ANDROID__
+ # include <linux/version.h>
+ # define FSTAT_OK (KERNEL_VERSION (3, 6, 0) <= LINUX_VERSION_CODE)
+ #else
+ # define FSTAT_OK 1
+ #endif
+ #if defined O_PATH && FSTAT_OK
+ working_fstat_o_path
+ #endif
+ ], [gl_cv_working_fstat_o_path="guessing yes"],
+ [gl_cv_working_fstat_o_path="guessing no"])])])
+ AS_CASE([$gl_cv_working_fstat_o_path], [*yes], [gl_val=1], [gl_val=0])
+ AC_DEFINE_UNQUOTED([HAVE_WORKING_FSTAT_O_PATH], [$gl_val],
+ [Define to 1 if fstat works on O_PATH file descriptors, 0 otherwise.])
])
diff --git a/tests/test-fcntl-h.c b/tests/test-fcntl-h.c
index f9664d5e3f..61d3302647 100644
--- a/tests/test-fcntl-h.c
+++ b/tests/test-fcntl-h.c
@@ -20,6 +20,11 @@
#include <fcntl.h>
+#if !defined HAVE_WORKING_FSTAT_O_PATH \
+ || (HAVE_WORKING_FSTAT_O_PATH != 0 && HAVE_WORKING_FSTAT_O_PATH != 1)
+# error "HAVE_WORKING_FSTAT_O_PATH must be defined to 0 or 1"
+#endif
+
/* Check that the various O_* macros are defined. */
int o = (O_DIRECT | O_DIRECTORY | O_DSYNC | O_IGNORE_CTTY | O_NDELAY | O_NOATIME
| O_NONBLOCK | O_NOCTTY | O_NOFOLLOW | O_NOLINK | O_NOLINKS | O_NOTRANS
--
2.56.0