[PATCH 03/23] Add OPENAT2_REGULAR from Linux 7.2 to bits/openat2.h

Adhemerval Zanella adhemerval.zanella@linaro.org
Tue Nov 3 14:32:08 GMT 2026


Linux 7.2 adds the OPENAT2_REGULAR open_how flag (commit
8b82cacad92ebae9619872a5a69c570eba30140b), which makes openat2 fail
with EFTYPE if the file is not a regular file.

The tst-openat2-consts.py test now also checks the OPENAT2_* constants
and is updated to Linux 7.3 (there are no further changes to
<linux/openat2.h> in Linux 7.3).  The tst-openat2 test checks the new
flag if the kernel supports it, and the manual documents it.

Checked on x86_64-linux-gnu and i686-linux-gnu.
---
 manual/errno.texi                             |  4 ++
 manual/llio.texi                              | 14 ++++
 sysdeps/unix/sysv/linux/bits/openat2.h        |  6 ++
 sysdeps/unix/sysv/linux/tst-openat2-consts.py |  6 +-
 sysdeps/unix/sysv/linux/tst-openat2.c         | 68 +++++++++++++++++++
 5 files changed, 95 insertions(+), 3 deletions(-)

diff --git a/manual/errno.texi b/manual/errno.texi
index a27223c14f8..51aa9cf3cf8 100644
--- a/manual/errno.texi
+++ b/manual/errno.texi
@@ -731,6 +731,10 @@ operation, or a data file had the wrong format.
 
 On some systems @code{chmod} returns this error if you try to set the
 sticky bit on a non-directory file; @pxref{Setting Permissions}.
+
+On Linux, @code{openat2} returns this error if @code{OPENAT2_REGULAR}
+is used and the file is not a regular file; @pxref{Opening and Closing
+Files}.
 @end deftypevr
 
 @deftypevr Macro int EAUTH
diff --git a/manual/llio.texi b/manual/llio.texi
index aa8f3b82b56..8934b9a3173 100644
--- a/manual/llio.texi
+++ b/manual/llio.texi
@@ -240,6 +240,16 @@ conflicting values. For example, @code{openat2} rejects a mode that
 exceeds 07777, whereas @code{openat} silently ignores
 excess high-order bits.
 
+In addition, the following flags can only be used with @code{openat2}.
+
+@table @code
+@item OPENAT2_REGULAR
+Only open the file if it is a regular file, and fail with @code{EFTYPE}
+otherwise.  If combined with @code{O_CREAT}, a regular file is created
+if the file does not exist.  This flag cannot be combined with
+@code{O_DIRECTORY}.
+@end table
+
 @item mode
 This field specifies the mode for the new file, similar to @code{mode}
 argument of @code{openat}. It should be in the range 0..07777.
@@ -300,6 +310,10 @@ fail for any of the reasons @code{openat} fails, plus the following reasons:
 @var{size} is too large for any future extension, @code{*@var{how}} contains
 non-zero members that are future extensions not supported by this kernel
 
+@item EFTYPE
+@code{@var{how}->flags} contains @code{OPENAT2_REGULAR} and the file is
+not a regular file.
+
 @item EINVAL
 An unknown flag or invalid value was used on @code{*@var{how}}; or
 @code{@var{how}->mode} is non-zero, but @code{@var{how}->flags} does not contain
diff --git a/sysdeps/unix/sysv/linux/bits/openat2.h b/sysdeps/unix/sysv/linux/bits/openat2.h
index 454d7699e72..cc0d0113d24 100644
--- a/sysdeps/unix/sysv/linux/bits/openat2.h
+++ b/sysdeps/unix/sysv/linux/bits/openat2.h
@@ -30,6 +30,12 @@ struct open_how
 };
 #endif
 
+/* how->flags bits exclusive to openat2.  They live in the upper 32 bits,
+   so they cannot be used with open or openat.  */
+#ifndef OPENAT2_REGULAR
+# define OPENAT2_REGULAR	((__uint64_t) 1 << 32) /* Only open regular files.  */
+#endif
+
 /* how->resolve flags for openat2. */
 #ifndef RESOLVE_NO_XDEV
 # define RESOLVE_NO_XDEV	0x01 /* Block mount-point crossings
diff --git a/sysdeps/unix/sysv/linux/tst-openat2-consts.py b/sysdeps/unix/sysv/linux/tst-openat2-consts.py
index 0b917d17255..2bc152f4a0c 100755
--- a/sysdeps/unix/sysv/linux/tst-openat2-consts.py
+++ b/sysdeps/unix/sysv/linux/tst-openat2-consts.py
@@ -39,10 +39,10 @@ def main():
         sys.exit (77)
 
     linux_version_headers = glibcsyscalls.linux_kernel_version(args.cc)
-    # Constants in glibc were updated to match Linux v7.1.  When glibc
+    # Constants in glibc were updated to match Linux v7.3.  When glibc
     # constants are updated this value should be updated to match the
     # released kernel version from which the constants were taken.
-    linux_version_glibc = (7, 1)
+    linux_version_glibc = (7, 3)
     def check(cte, exclude=None):
         return glibcextract.compare_macro_consts(
                 '#define _FCNTL_H\n'
@@ -56,7 +56,7 @@ def main():
                 linux_version_glibc > linux_version_headers,
                 linux_version_headers > linux_version_glibc)
 
-    status = check('RESOLVE.*')
+    status = max(check('RESOLVE.*'), check('OPENAT2_.*'))
     sys.exit(status)
 
 if __name__ == '__main__':
diff --git a/sysdeps/unix/sysv/linux/tst-openat2.c b/sysdeps/unix/sysv/linux/tst-openat2.c
index aba1c3e8b4f..bad1680c919 100644
--- a/sysdeps/unix/sysv/linux/tst-openat2.c
+++ b/sysdeps/unix/sysv/linux/tst-openat2.c
@@ -21,6 +21,7 @@
 #include <fcntl.h>
 #include <libgen.h>
 #include <stdint.h>
+#include <stdio.h>
 
 #include <support/check.h>
 #include <support/support.h>
@@ -409,6 +410,72 @@ do_test_resolve (void)
   }
 }
 
+static void
+do_test_regular (void)
+{
+  /* VALID_LINK resolves to a regular file.  Kernels without OPENAT2_REGULAR
+     support reject the unknown flag with EINVAL.  */
+  int fd = openat2 (dir_fd,
+		    VALID_LINK,
+		    &(struct open_how)
+		    {
+		      .flags = O_RDONLY | OPENAT2_REGULAR,
+		    },
+		    sizeof (struct open_how));
+  if (fd == -1 && errno == EINVAL)
+    {
+      puts ("info: OPENAT2_REGULAR not supported by the kernel");
+      return;
+    }
+  TEST_VERIFY (fd != -1);
+  xclose (fd);
+
+  /* Neither a directory nor a character device are regular files.  */
+  fd = openat2 (dir_fd,
+		".",
+		&(struct open_how)
+		{
+		  .flags = O_RDONLY | OPENAT2_REGULAR,
+		},
+		sizeof (struct open_how));
+  TEST_COMPARE (fd, -1);
+  TEST_COMPARE (errno, EFTYPE);
+
+  fd = openat2 (AT_FDCWD,
+		"/dev/null",
+		&(struct open_how)
+		{
+		  .flags = O_RDONLY | OPENAT2_REGULAR,
+		},
+		sizeof (struct open_how));
+  TEST_COMPARE (fd, -1);
+  TEST_COMPARE (errno, EFTYPE);
+
+  /* OPENAT2_REGULAR cannot be combined with O_DIRECTORY.  */
+  fd = openat2 (dir_fd,
+		VALID_LINK,
+		&(struct open_how)
+		{
+		  .flags = O_RDONLY | O_DIRECTORY | OPENAT2_REGULAR,
+		},
+		sizeof (struct open_how));
+  TEST_COMPARE (fd, -1);
+  TEST_COMPARE (errno, EINVAL);
+
+  /* With O_CREAT a new regular file is created.  */
+  fd = openat2 (dir_fd,
+		"regular-file",
+		&(struct open_how)
+		{
+		  .flags = O_CREAT | O_EXCL | O_RDWR | OPENAT2_REGULAR,
+		  .mode = 0600,
+		},
+		sizeof (struct open_how));
+  TEST_VERIFY (fd != -1);
+  add_temp_file (xasprintf ("%s/%s", temp_dir, "regular-file"));
+  xclose (fd);
+}
+
 static int
 do_test_basic (void)
 {
@@ -474,6 +541,7 @@ do_test (void)
   do_test_struct ();
   do_test_flags ();
   do_test_resolve ();
+  do_test_regular ();
   do_test_basic ();
 
   return 0;
-- 
2.53.0



More information about the Libc-alpha mailing list