On 03/11/26 17:18, Paul Eggert wrote:
> On 2026-11-03 06:32, Adhemerval Zanella wrote:
>> +... If combined with @code{O_CREAT}, a regular file is created
>> +if the file does not exist.
>
> Is this sentence needed in the documentation? If so, the reason should be explained; if not, I suggest removing the sentence because it's normally the case that a regular file is created if the file does not exist.
Ack, I will remove it.
@@ -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
@@ -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
@@ -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
@@ -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__':
@@ -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;