[glibc/release/2.36/master] include: isolate __O_CLOEXEC flag for sys/mount.h and fcntl.h

Florian Weimer via Glibc-cvs <[email protected]>
Newsgroups gmane.comp.lib.glibc.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=c13121204aea247799184590aafc28dbda0ccb39

commit c13121204aea247799184590aafc28dbda0ccb39
Author: DJ Delorie <[email protected]>
Date:   Mon Jan 26 22:24:42 2026 -0500

    include: isolate __O_CLOEXEC flag for sys/mount.h and fcntl.h
    
    Including sys/mount.h should not implicitly include fcntl.h
    as that causes namespace pollution and conflicts with kernel
    headers.  It only needs O_CLOEXEC for OPEN_TREE_CLOEXEC
    (although it shouldn't need that, but it's defined that way)
    so we provide that define (via a private version) separately.
    
    Reviewed-by: Adhemerval Zanella  <[email protected]>
    Tested-by: Florian Weimer <[email protected]>
    (cherry picked from commit 419245719ccbc7dad6a97f24465e7f09c090327a)
    
    (Adjusted sysdeps/unix/sysv/linux/Makefile for lack of reformatting.)

Diff:
---
 io/fcntl.c                                   | 4 ++++
 sysdeps/unix/sysv/linux/Makefile             | 2 +-
 sysdeps/unix/sysv/linux/alpha/bits/cloexec.h | 1 +
 sysdeps/unix/sysv/linux/bits/cloexec.h       | 1 +
 sysdeps/unix/sysv/linux/bits/fcntl-linux.h   | 4 +---
 sysdeps/unix/sysv/linux/hppa/bits/cloexec.h  | 1 +
 sysdeps/unix/sysv/linux/sparc/bits/cloexec.h | 1 +
 sysdeps/unix/sysv/linux/sys/mount.h          | 6 +++++-
 sysdeps/unix/sysv/linux/tst-mount.c          | 1 +
 9 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/io/fcntl.c b/io/fcntl.c
index 491678bccc..d6e56629b8 100644
--- a/io/fcntl.c
+++ b/io/fcntl.c
@@ -18,6 +18,10 @@
 #include <errno.h>
 #include <fcntl.h>
 
+#ifndef __O_CLOEXEC
+# error __O_CLOEXEC not defined by fcntl.h/cloexec.h
+#endif
+
 /* Perform file control operations on FD.  */
 int
 __fcntl (int fd, int cmd, ...)
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index d92ea7a1f3..80ad22259c 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -114,7 +114,7 @@ sysdep_headers += sys/mount.h sys/acct.h \
 		  bits/types/struct_shmid64_ds_helper.h \
 		  bits/pthread_stack_min.h bits/pthread_stack_min-dynamic.h \
 		  sys/rseq.h bits/rseq.h \
-		  sys/pidfd.h
+		  sys/pidfd.h bits/cloexec.h
 
 tests += tst-clone tst-clone2 tst-clone3 tst-fanotify tst-personality \
 	 tst-quota tst-sync_file_range tst-sysconf-iov_max tst-ttyname \
diff --git a/sysdeps/unix/sysv/linux/alpha/bits/cloexec.h b/sysdeps/unix/sysv/linux/alpha/bits/cloexec.h
new file mode 100644
index 0000000000..f381f28a53
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/alpha/bits/cloexec.h
@@ -0,0 +1 @@
+#define __O_CLOEXEC 010000000
diff --git a/sysdeps/unix/sysv/linux/bits/cloexec.h b/sysdeps/unix/sysv/linux/bits/cloexec.h
new file mode 100644
index 0000000000..3059fb6473
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/bits/cloexec.h
@@ -0,0 +1 @@
+#define __O_CLOEXEC 02000000
diff --git a/sysdeps/unix/sysv/linux/bits/fcntl-linux.h b/sysdeps/unix/sysv/linux/bits/fcntl-linux.h
index bfc674235d..b7d1ad99e2 100644
--- a/sysdeps/unix/sysv/linux/bits/fcntl-linux.h
+++ b/sysdeps/unix/sysv/linux/bits/fcntl-linux.h
@@ -81,9 +81,7 @@
 #ifndef __O_NOFOLLOW
 # define __O_NOFOLLOW	0400000
 #endif
-#ifndef __O_CLOEXEC
-# define __O_CLOEXEC   02000000
-#endif
+#include <bits/cloexec.h>
 #ifndef __O_DIRECT
 # define __O_DIRECT	 040000
 #endif
diff --git a/sysdeps/unix/sysv/linux/hppa/bits/cloexec.h b/sysdeps/unix/sysv/linux/hppa/bits/cloexec.h
new file mode 100644
index 0000000000..f381f28a53
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/hppa/bits/cloexec.h
@@ -0,0 +1 @@
+#define __O_CLOEXEC 010000000
diff --git a/sysdeps/unix/sysv/linux/sparc/bits/cloexec.h b/sysdeps/unix/sysv/linux/sparc/bits/cloexec.h
new file mode 100644
index 0000000000..6706eaa7d5
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/sparc/bits/cloexec.h
@@ -0,0 +1 @@
+#define __O_CLOEXEC 0x400000
diff --git a/sysdeps/unix/sysv/linux/sys/mount.h b/sysdeps/unix/sysv/linux/sys/mount.h
index 19841d0738..4a65508a26 100644
--- a/sysdeps/unix/sysv/linux/sys/mount.h
+++ b/sysdeps/unix/sysv/linux/sys/mount.h
@@ -21,7 +21,6 @@
 #ifndef _SYS_MOUNT_H
 #define _SYS_MOUNT_H	1
 
-#include <fcntl.h>
 #include <features.h>
 #include <stdint.h>
 #include <stddef.h>
@@ -263,6 +262,11 @@ enum fsconfig_command
 
 /* open_tree flags.  */
 #define OPEN_TREE_CLONE    1         /* Clone the target tree and attach the clone */
+#ifndef O_CLOEXEC
+# include <bits/cloexec.h>
+# define O_CLOEXEC __O_CLOEXEC
+#endif
+#undef  OPEN_TREE_CLOEXEC
 #define OPEN_TREE_CLOEXEC  O_CLOEXEC /* Close the file on execve() */
 
 
diff --git a/sysdeps/unix/sysv/linux/tst-mount.c b/sysdeps/unix/sysv/linux/tst-mount.c
index b6333a60e6..d5c844455c 100644
--- a/sysdeps/unix/sysv/linux/tst-mount.c
+++ b/sysdeps/unix/sysv/linux/tst-mount.c
@@ -20,6 +20,7 @@
 #include <support/check.h>
 #include <support/xunistd.h>
 #include <support/namespace.h>
+#include <fcntl.h> /* For AT_ constants.  */
 #include <sys/mount.h>
 
 _Static_assert (sizeof (struct mount_attr) == MOUNT_ATTR_SIZE_VER0,
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.