[PATCH v3 03/25] Add OPENAT2_REGULAR from Linux 7.2 to bits/openat2.h
Adhemerval Zanella
adhemerval.zanella@linaro.org
Mon Nov 16 19:42:16 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/llio.texi | 13 +++
sysdeps/unix/sysv/linux/bits/openat2.h | 6 ++
sysdeps/unix/sysv/linux/tst-openat2-consts.py | 6 +-
sysdeps/unix/sysv/linux/tst-openat2.c | 86 +++++++++++++++++++
4 files changed, 108 insertions(+), 3 deletions(-)
diff --git a/manual/llio.texi b/manual/llio.texi
index aa8f3b82b56..c055ea61b6a 100644
--- a/manual/llio.texi
+++ b/manual/llio.texi
@@ -240,6 +240,15 @@ 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. This flag cannot be combined with @code{O_DIRECTORY} or
+@code{O_PATH}.
+@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 +309,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..14a00b9846a 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,90 @@ do_test_resolve (void)
}
}
+static void
+do_test_regular (void)
+{
+ /* A directory is not a regular file. Kernels without OPENAT2_REGULAR
+ support reject the unknown flag with EINVAL, and emulators which
+ truncate the flags to 32 bits (such as qemu-user) ignore it. */
+ int fd = openat2 (dir_fd,
+ ".",
+ &(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;
+ }
+ if (fd != -1)
+ {
+ puts ("info: OPENAT2_REGULAR is ignored, skipping its tests");
+ xclose (fd);
+ return;
+ }
+ TEST_COMPARE (errno, EFTYPE);
+
+ /* Neither is a character device. */
+ 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);
+
+ /* VALID_LINK resolves to a regular file. */
+ fd = openat2 (dir_fd,
+ VALID_LINK,
+ &(struct open_how)
+ {
+ .flags = O_RDONLY | OPENAT2_REGULAR,
+ },
+ sizeof (struct open_how));
+ TEST_VERIFY (fd != -1);
+ if (fd != -1)
+ xclose (fd);
+
+ /* OPENAT2_REGULAR cannot be combined with O_DIRECTORY or O_PATH. */
+ 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);
+
+ fd = openat2 (dir_fd,
+ VALID_LINK,
+ &(struct open_how)
+ {
+ .flags = O_PATH | 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 +559,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