]> git.ipfire.org Git - thirdparty/man-pages.git/blobdiff - man2/open.2
gettid.2, open.2, seccomp.2, pthread_mutex_consistent.3, signal.7, xattr.7: Put SEE...
[thirdparty/man-pages.git] / man2 / open.2
index 64037761f35b5e4400a44a7bee814368fc7d13eb..c25584455d99d414a21a94c530567e35dfb7670d 100644 (file)
@@ -48,7 +48,7 @@
 .\" FIXME . Apr 08: The next POSIX revision has O_EXEC, O_SEARCH, and
 .\" O_TTYINIT.  Eventually these may need to be documented.  --mtk
 .\"
-.TH OPEN 2 2016-03-15 "Linux" "Linux Programmer's Manual"
+.TH OPEN 2 2020-02-09 "Linux" "Linux Programmer's Manual"
 .SH NAME
 open, openat, creat \- open and possibly create a file
 .SH SYNOPSIS
@@ -56,22 +56,26 @@ open, openat, creat \- open and possibly create a file
 .B #include <sys/types.h>
 .B #include <sys/stat.h>
 .B #include <fcntl.h>
-.sp
+.PP
 .BI "int open(const char *" pathname ", int " flags );
 .BI "int open(const char *" pathname ", int " flags ", mode_t " mode );
-
+.PP
 .BI "int creat(const char *" pathname ", mode_t " mode );
-.sp
+.PP
 .BI "int openat(int " dirfd ", const char *" pathname ", int " flags );
 .BI "int openat(int " dirfd ", const char *" pathname ", int " flags \
 ", mode_t " mode );
+.PP
+/* Documented separately, in \fBopenat2\fP(2): */
+.BI "int openat2(int " dirfd ", const char *" pathname ,
+.BI "            const struct open_how *" how ", size_t " size ");
 .fi
-.sp
+.PP
 .in -4n
 Feature Test Macro Requirements for glibc (see
 .BR feature_test_macros (7)):
 .in
-.sp
+.PP
 .BR openat ():
 .PD 0
 .ad l
@@ -86,13 +90,24 @@ _ATFILE_SOURCE
 .ad
 .PD
 .SH DESCRIPTION
-Given a
-.I pathname
-for a file,
+The
 .BR open ()
-returns a file descriptor, a small, nonnegative integer
-for use in subsequent system calls
-.RB ( read "(2), " write "(2), " lseek "(2), " fcntl "(2), etc.)."
+system call opens the file specified by
+.IR pathname .
+If the specified file does not exist,
+it may optionally (if
+.B O_CREAT
+is specified in
+.IR flags )
+be created by
+.BR open ().
+.PP
+The return value of
+.BR open ()
+is a file descriptor, a small, nonnegative integer that is used
+in subsequent system calls
+.RB ( read "(2), " write "(2), " lseek "(2), " fcntl (2),
+etc.) to refer to the open file.
 The file descriptor returned by a successful call will be
 the lowest-numbered file descriptor not currently open for the process.
 .PP
@@ -128,7 +143,7 @@ must include one of the following
 .BR O_RDONLY ", " O_WRONLY ", or " O_RDWR .
 These request opening the file read-only, write-only, or read/write,
 respectively.
-
+.PP
 In addition, zero or more file creation flags and file status flags
 can be
 .RI bitwise- or 'd
@@ -162,11 +177,13 @@ are all of the remaining flags listed below.
 .\" are also categorized" as file status flags.
 .\"
 The distinction between these two groups of flags is that
-the file status flags can be retrieved and (in some cases)
+the file creation flags affect the semantics of the open operation itself,
+while the file status flags affect the semantics of subsequent I/O operations.
+The file status flags can be retrieved and (in some cases)
 modified; see
 .BR fcntl (2)
 for details.
-
+.PP
 The full list of file creation flags and file status flags is as follows:
 .TP
 .B O_APPEND
@@ -176,6 +193,9 @@ Before each
 the file offset is positioned at the end of the file,
 as if with
 .BR lseek (2).
+The modification of the file offset and the write operation
+are performed as a single atomic step.
+.IP
 .B O_APPEND
 may lead to corrupted files on NFS filesystems if more than one process
 appends data to a file at once.
@@ -203,13 +223,17 @@ See also BUGS, below.
 .BR O_CLOEXEC " (since Linux 2.6.23)"
 .\" NOTE! several other man pages refer to this text
 Enable the close-on-exec flag for the new file descriptor.
+.\" FIXME . for later review when Issue 8 is one day released...
+.\" POSIX proposes to fix many APIs that provide hidden FDs
+.\" http://austingroupbugs.net/tag_view_page.php?tag_id=8
+.\" http://austingroupbugs.net/view.php?id=368
 Specifying this flag permits a program to avoid additional
 .BR fcntl (2)
 .B F_SETFD
 operations to set the
 .B FD_CLOEXEC
 flag.
-
+.IP
 Note that the use of this flag is essential in some multithreaded programs,
 because using a separate
 .BR fcntl (2)
@@ -240,18 +264,26 @@ flag to deal with this problem.)
 .\" returned by accept(), pipe(), etc.
 .TP
 .B O_CREAT
-If the file does not exist, it will be created.
-The owner (user ID) of the file is set to the effective user ID
+If
+.I pathname
+does not exist, create it as a regular file.
+.IP
+The owner (user ID) of the new file is set to the effective user ID
 of the process.
-The group ownership (group ID) is set either to
-the effective group ID of the process or to the group ID of the
-parent directory (depending on filesystem type and mount options,
-and the mode of the parent directory; see the mount options
+.IP
+The group ownership (group ID) of the new file is set either to
+the effective group ID of the process (System V semantics)
+or to the group ID of the parent directory (BSD semantics).
+On Linux, the behavior depends on whether the
+set-group-ID mode bit is set on the parent directory:
+if that bit is set, then BSD semantics apply;
+otherwise, System V semantics apply.
+For some filesystems, the behavior also depends on the
 .I bsdgroups
 and
 .I sysvgroups
-described in
-.BR mount (8)).
+mount options described in
+.BR mount (8).
 .\" As at 2.6.25, bsdgroups is supported by ext2, ext3, ext4, and
 .\" XFS (since 2.6.14).
 .RS
@@ -335,11 +367,11 @@ On Linux, the following bits are also honored in
 .TP
 .B S_ISGID
 0002000 set-group-ID bit (see
-.BR stat (2))
+.BR inode (7)).
 .TP
 .B S_ISVTX
 0001000 sticky bit (see
-.BR stat (2))
+.BR inode (7)).
 .RE
 .TP
 .BR O_DIRECT " (since Linux 2.4.10)"
@@ -358,7 +390,7 @@ To guarantee synchronous I/O,
 must be used in addition to
 .BR O_DIRECT .
 See NOTES below for further discussion.
-.sp
+.IP
 A semantically similar (but deprecated) interface for block devices
 is described in
 .BR raw (8).
@@ -380,7 +412,7 @@ Write operations on the file will complete according to the requirements of
 synchronized I/O
 .I data
 integrity completion.
-
+.IP
 By the time
 .BR write (2)
 (and similar)
@@ -401,16 +433,17 @@ and
 .I pathname
 already exists, then
 .BR open ()
-will fail.
-
+fails with the error
+.BR EEXIST .
+.IP
 When these two flags are specified, symbolic links are not followed:
 .\" POSIX.1-2001 explicitly requires this behavior.
 if
 .I pathname
 is a symbolic link, then
 .BR open ()
-fails regardless of where the symbolic link points to.
-
+fails regardless of where the symbolic link points.
+.IP
 In general, the behavior of
 .B O_EXCL
 is undefined if it is used without
@@ -426,7 +459,7 @@ If the block device is in use by the system (e.g., mounted),
 .BR open ()
 fails with the error
 .BR EBUSY .
-
+.IP
 On NFS,
 .B O_EXCL
 is supported only when using NFSv3 or later on kernel 2.6 or later.
@@ -477,6 +510,20 @@ Do not update the file last access time
 in the inode)
 when the file is
 .BR read (2).
+.IP
+This flag can be employed only if one of the following conditions is true:
+.RS
+.IP * 3
+The effective UID of the process
+.\" Strictly speaking: the filesystem UID
+matches the owner UID of the file.
+.IP *
+The calling process has the
+.BR CAP_FOWNER
+capability in its user namespace and
+the owner UID of the file has a mapping in the namespace.
+.RE
+.IP
 This flag is intended for use by indexing or backup programs,
 where its use can significantly reduce the amount of disk activity.
 This flag may not be effective on all filesystems.
@@ -493,10 +540,21 @@ will not become the process's controlling terminal even if the
 process does not have one.
 .TP
 .B O_NOFOLLOW
-If \fIpathname\fP is a symbolic link, then the open fails.
-This is a FreeBSD extension, which was added to Linux in version 2.1.126.
+If the trailing component (i.e., basename) of
+.I pathname
+is a symbolic link, then the open fails, with the error
+.BR ELOOP .
 Symbolic links in earlier components of the pathname will still be
 followed.
+(Note that the
+.B ELOOP
+error that can occur in this case is indistinguishable from the case where
+an open fails because there are too many symbolic links found
+while resolving components in the prefix part of the pathname.)
+.IP
+This flag is a FreeBSD extension, which was added to Linux in version 2.1.126,
+and has subsequently been standardized in POSIX.1-2008.
+.IP
 See also
 .BR O_PATH
 below.
@@ -508,9 +566,23 @@ below.
 When possible, the file is opened in nonblocking mode.
 Neither the
 .BR open ()
-nor any subsequent operations on the file descriptor which is
+nor any subsequent I/O operations on the file descriptor which is
 returned will cause the calling process to wait.
-
+.IP
+Note that the setting of this flag has no effect on the operation of
+.BR poll (2),
+.BR select (2),
+.BR epoll (7),
+and similar,
+since those interfaces merely inform the caller about whether
+a file descriptor is "ready",
+meaning that an I/O operation performed on
+the file descriptor with the
+.B O_NONBLOCK
+flag
+.I clear
+would not block.
+.IP
 Note that this flag has no effect for regular files and block devices;
 that is, I/O operations will (briefly) block when device activity
 is required, regardless of whether
@@ -521,7 +593,7 @@ Since
 semantics might eventually be implemented,
 applications should not depend upon blocking behavior
 when specifying this flag for regular files and block devices.
-
+.IP
 For the handling of FIFOs (named pipes), see also
 .BR fifo (7).
 For a discussion of the effect of
@@ -547,22 +619,30 @@ The file itself is not opened, and other file operations (e.g.,
 .BR fchmod (2),
 .BR fchown (2),
 .BR fgetxattr (2),
+.BR ioctl (2),
 .BR mmap (2))
 fail with the error
 .BR EBADF .
-
+.IP
 The following operations
 .I can
 be performed on the resulting file descriptor:
 .RS
 .IP * 3
-.BR close (2);
-.BR fchdir (2)
-(since Linux 3.5);
+.BR close (2).
+.IP *
+.BR fchdir (2),
+if the file descriptor refers to a directory
+(since Linux 3.5).
 .\" commit 332a2e1244bd08b9e3ecd378028513396a004a24
+.IP *
 .BR fstat (2)
 (since Linux 3.6).
+.IP *
 .\" fstat(): commit 55815f70147dcfa3ead5738fd56d3574e2e3c1c2
+.BR fstatfs (2)
+(since Linux 3.12).
+.\" fstatfs(): commit 9d05746e7b16d8565dddbe3200faa1e669d23bbf
 .IP *
 Duplicating the file descriptor
 .RB ( dup (2),
@@ -585,7 +665,7 @@ operation: the returned flags will include the bit
 Passing the file descriptor as the
 .IR dirfd
 argument of
-.BR openat (2)
+.BR openat ()
 and the other "*at()" system calls.
 This includes
 .BR linkat (2)
@@ -612,7 +692,25 @@ flag bits other than
 and
 .BR O_NOFOLLOW
 are ignored.
-
+.IP
+Opening a file or directory with the
+.B O_PATH
+flag requires no permissions on the object itself
+(but does require execute permission on the directories in the path prefix).
+Depending on the subsequent operation,
+a check for suitable file permissions may be performed (e.g.,
+.BR fchdir (2)
+requires execute permission on the directory referred to
+by its file descriptor argument).
+By contrast,
+obtaining a reference to a filesystem object by opening it with the
+.B O_RDONLY
+flag requires that the caller have read permission on the object,
+even when the subsequent operation (e.g.,
+.BR fchdir (2),
+.BR fstat (2))
+does not require read permission on the object.
+.IP
 If
 .I pathname
 is a symbolic link and the
@@ -628,6 +726,39 @@ argument in calls to
 and
 .BR readlinkat (2)
 with an empty pathname to have the calls operate on the symbolic link.
+.IP
+If
+.I pathname
+refers to an automount point that has not yet been triggered, so no
+other filesystem is mounted on it, then the call returns a file
+descriptor referring to the automount directory without triggering a mount.
+.BR fstatfs (2)
+can then be used to determine if it is, in fact, an untriggered
+automount point
+.RB ( ".f_type == AUTOFS_SUPER_MAGIC" ).
+.IP
+One use of
+.B O_PATH
+for regular files is to provide the equivalent of POSIX.1's
+.B O_EXEC
+functionality.
+This permits us to open a file for which we have execute
+permission but not read permission, and then execute that file,
+with steps something like the following:
+.IP
+.in +4n
+.EX
+char buf[PATH_MAX];
+fd = open("some_prog", O_PATH);
+snprintf(buf, PATH_MAX, "/proc/self/fd/%d", fd);
+execl(buf, "some_prog", (char *) NULL);
+.EE
+.in
+.IP
+An
+.B O_PATH
+file descriptor can also be passed as the argument of
+.BR fexecve (3).
 .TP
 .B O_SYNC
 Write operations on the file will complete according to the requirements of
@@ -640,11 +771,11 @@ synchronized I/O
 integrity completion
 provided by
 .BR O_DSYNC .)
-
+.IP
 By the time
 .BR write (2)
-(and similar)
-return, the output data and associated file metadata
+(or similar)
+returns, the output data and associated file metadata
 have been transferred to the underlying hardware
 (i.e., as though each
 .BR write (2)
@@ -656,14 +787,14 @@ was followed by a call to
 .\" commit 60545d0d4610b02e55f65d141c95b18ccf855b6e
 .\" commit f4e0c30c191f87851c4a53454abb55ee276f4a7e
 .\" commit bb458c644a59dbba3a1fe59b27106c5e68e1c4bd
-Create an unnamed temporary file.
+Create an unnamed temporary regular file.
 The
 .I pathname
 argument specifies a directory;
 an unnamed inode will be created in that directory's filesystem.
 Anything written to the resulting file will be lost when
 the last file descriptor is closed, unless the file is given a name.
-
+.IP
 .B O_TMPFILE
 must be specified with one of
 .B O_RDWR
@@ -677,28 +808,36 @@ is not specified, then
 .BR linkat (2)
 can be used to link the temporary file into the filesystem, making it
 permanent, using code like the following:
-
+.IP
 .in +4n
-.nf
+.EX
 char path[PATH_MAX];
 fd = open("/path/to/dir", O_TMPFILE | O_RDWR,
                         S_IRUSR | S_IWUSR);
 
 /* File I/O on 'fd'... */
 
+linkat(fd, NULL, AT_FDCWD, "/path/for/file", AT_EMPTY_PATH);
+
+/* If the caller doesn't have the CAP_DAC_READ_SEARCH
+   capability (needed to use AT_EMPTY_PATH with linkat(2)),
+   and there is a proc(5) filesystem mounted, then the
+   linkat(2) call above can be replaced with:
+
 snprintf(path, PATH_MAX,  "/proc/self/fd/%d", fd);
 linkat(AT_FDCWD, path, AT_FDCWD, "/path/for/file",
                         AT_SYMLINK_FOLLOW);
-.fi
+*/
+.EE
 .in
-
+.IP
 In this case,
 the
 .BR open ()
 .I mode
 argument determines the file permission mode, as with
 .BR O_CREAT .
-
+.IP
 Specifying
 .B O_EXCL
 in conjunction with
@@ -710,7 +849,7 @@ in the above manner.
 in this case is different from the meaning of
 .B O_EXCL
 otherwise.)
-
+.IP
 There are two main use cases for
 .\" Inspired by http://lwn.net/Articles/559147/
 .BR O_TMPFILE :
@@ -741,12 +880,16 @@ requires support by the underlying filesystem;
 only a subset of Linux filesystems provide that support.
 In the initial implementation, support was provided in
 the ext2, ext3, ext4, UDF, Minix, and shmem filesystems.
-XFS support was added
+.\" To check for support, grep for "tmpfile" in kernel sources
+Support for other filesystems has subsequently been added as follows:
+XFS (Linux 3.15);
 .\" commit 99b6436bc29e4f10e4388c27a3e4810191cc4788
 .\" commit ab29743117f9f4c22ac44c13c1647fb24fb2bafe
-in Linux 3.15, and Btrfs support was added
+Btrfs (Linux 3.16);
 .\" commit ef3b9af50bfa6a1f02cd7b3f5124b712b1ba3e3c
-in Linux 3.16.
+F2FS (Linux 3.16);
+.\" commit 50732df02eefb39ab414ef655979c2c9b64ad21c
+and ubifs (Linux 4.9)
 .TP
 .B O_TRUNC
 If the file already exists and is a regular file and the access mode allows
@@ -762,8 +905,9 @@ Otherwise, the effect of
 .B O_TRUNC
 is unspecified.
 .SS creat()
+A call to
 .BR creat ()
-is equivalent to
+is equivalent to calling
 .BR open ()
 with
 .I flags
@@ -775,7 +919,7 @@ The
 system call operates in exactly the same way as
 .BR open (),
 except for the differences described here.
-
+.PP
 If the pathname given in
 .I pathname
 is relative, then it is interpreted relative to the directory
@@ -785,7 +929,7 @@ referred to by the file descriptor
 the calling process, as is done by
 .BR open ()
 for a relative pathname).
-
+.PP
 If
 .I pathname
 is relative and
@@ -797,18 +941,29 @@ then
 is interpreted relative to the current working
 directory of the calling process (like
 .BR open ()).
-
+.PP
 If
 .I pathname
 is absolute, then
 .I dirfd
 is ignored.
+.\"
+.SS openat2(2)
+The
+.BR openat2 (2)
+system call is an extension of
+.BR openat (),
+and provides a superset of the features of
+.BR openat ().
+It is documented separately, in
+.BR openat2 (2).
 .SH RETURN VALUE
 .BR open (),
 .BR openat (),
 and
 .BR creat ()
-return the new file descriptor, or \-1 if an error occurred
+return the new file descriptor (a nonnegative integer),
+or \-1 if an error occurred
 (in which case,
 .I errno
 is set appropriately).
@@ -828,6 +983,25 @@ is not allowed.
 (See also
 .BR path_resolution (7).)
 .TP
+.B EACCES
+.\" commit 30aba6656f61ed44cba445a3c0d38b296fa9e8f5
+Where
+.B O_CREAT
+is specified, the
+.I protected_fifos
+or
+.I protected_regular
+sysctl is enabled, the file already exists and is a FIFO or regular file, the
+owner of the file is neither the current user nor the owner of the
+containing directory, and the containing directory is both world- or
+group-writable and sticky.
+For details, see the descriptions of
+.IR /proc/sys/fs/protected_fifos
+and
+.IR /proc/sys/fs/protected_regular
+in
+.BR proc (5).
+.TP
 .B EDQUOT
 Where
 .B O_CREAT
@@ -878,6 +1052,21 @@ nor
 .B O_RDWR
 was specified.
 .TP
+.B EINVAL
+.B O_CREAT
+was specified in
+.I flags
+and the final component ("basename") of the new file's
+.I pathname
+is invalid
+(e.g., it contains characters not permitted by the underlying filesystem).
+.TP
+.B EINVAL
+The final component ("basename") of
+.I pathname
+is invalid
+(e.g., it contains characters not permitted by the underlying filesystem).
+.TP
 .B EISDIR
 .I pathname
 refers to a directory and the access requested involved writing
@@ -938,7 +1127,9 @@ must be returned.)
 .B ENOENT
 .B O_CREAT
 is not set and the named file does not exist.
-Or, a directory component in
+.TP
+.B ENOENT
+A directory component in
 .I pathname
 does not exist or is a dangling symbolic link.
 .TP
@@ -957,6 +1148,13 @@ but this kernel version does not provide the
 functionality.
 .TP
 .B ENOMEM
+The named file is a FIFO,
+but memory for the FIFO buffer can't be allocated because
+the per-user hard limit on memory allocation for pipes has been reached
+and the caller is not privileged; see
+.BR pipe (7).
+.TP
+.B ENOMEM
 Insufficient kernel memory was available.
 .TP
 .B ENOSPC
@@ -976,7 +1174,12 @@ was not a directory.
 .BR O_NONBLOCK " | " O_WRONLY
 is set, the named file is a FIFO, and
 no process has the FIFO open for reading.
-Or, the file is a device special file and no corresponding device exists.
+.TP
+.B ENXIO
+The file is a device special file and no corresponding device exists.
+.TP
+.B ENXIO
+The file is a UNIX domain socket.
 .TP
 .BR EOPNOTSUPP
 The filesystem containing
@@ -1009,8 +1212,7 @@ The
 .B O_NOATIME
 flag was specified, but the effective user ID of the caller
 .\" Strictly speaking, it's the filesystem UID... (MTK)
-did not match the owner of the file and the caller was not privileged
-.RB ( CAP_FOWNER ).
+did not match the owner of the file and the caller was not privileged.
 .TP
 .B EPERM
 The operation was prevented by a file seal; see
@@ -1026,6 +1228,17 @@ requested.
 refers to an executable image which is currently being executed and
 write access was requested.
 .TP
+.B ETXTBSY
+.I pathname
+refers to a file that is currently in use as a swap file, and the
+.B O_TRUNC
+flag was specified.
+.TP
+.B ETXTBSY
+.I pathname
+refers to a file that is currently being read by the kernel (e.g. for
+module/firmware loading), and write access was requested.
+.TP
 .B EWOULDBLOCK
 The
 .B O_NONBLOCK
@@ -1053,10 +1266,13 @@ library support was added to glibc in version 2.4.
 .BR open (),
 .BR creat ()
 SVr4, 4.3BSD, POSIX.1-2001, POSIX.1-2008.
-
+.PP
 .BR openat ():
 POSIX.1-2008.
-
+.PP
+.BR openat2 (2)
+is Linux-specific.
+.PP
 The
 .BR O_DIRECT ,
 .BR O_NOATIME ,
@@ -1067,7 +1283,7 @@ flags are Linux-specific.
 One must define
 .B _GNU_SOURCE
 to obtain their definitions.
-
+.PP
 The
 .BR O_CLOEXEC ,
 .BR O_DIRECTORY ,
@@ -1082,7 +1298,7 @@ with a value greater than or equal to 200809L or
 with a value greater than or equal to 700.
 In glibc 2.11 and earlier, one obtains the definitions by defining
 .BR _GNU_SOURCE .
-
+.PP
 As noted in
 .BR feature_test_macros (7),
 feature test macros such as
@@ -1096,12 +1312,13 @@ header files.
 .SH NOTES
 Under Linux, the
 .B O_NONBLOCK
-flag indicates that one wants to open
+flag is sometimes used in cases where one wants to open
 but does not necessarily have the intention to read or write.
-This is typically used to open devices in order to get a file descriptor
+For example,
+this may be used to open a device in order to get a file descriptor
 for use with
 .BR ioctl (2).
-.LP
+.PP
 The (undefined) effect of
 .B O_RDONLY | O_TRUNC
 varies among implementations.
@@ -1112,7 +1329,7 @@ On many systems the file is actually truncated.
 .\" Tru64 5.1B: truncate
 .\" HP-UX 11.22: truncate
 .\" FreeBSD 4.7: truncate
-
+.PP
 Note that
 .BR open ()
 can open device special files, but
@@ -1120,7 +1337,7 @@ can open device special files, but
 cannot create them; use
 .BR mknod (2)
 instead.
-
+.PP
 If the file is newly created, its
 .IR st_atime ,
 .IR st_ctime ,
@@ -1143,17 +1360,25 @@ flag, its
 and
 .I st_mtime
 fields are set to the current time.
-
+.PP
 The files in the
 .I /proc/[pid]/fd
 directory show the open file descriptors of the process with the PID
 .IR pid .
 The files in the
 .I /proc/[pid]/fdinfo
-directory show even more information about these files descriptors.
+directory show even more information about these file descriptors.
 See
 .BR proc (5)
 for further details of both of these directories.
+.PP
+The Linux header file
+.B <asm/fcntl.h>
+doesn't define
+.BR O_ASYNC ;
+the (BSD-derived)
+.B FASYNC
+synonym is defined instead.
 .\"
 .\"
 .SS Open file descriptions
@@ -1164,7 +1389,7 @@ variously also called an "open file object",
 a "file handle", an "open file table entry",
 or\(emin kernel-developer parlance\(ema
 .IR "struct file" .
-
+.PP
 When a file descriptor is duplicated (using
 .BR dup (2)
 or similar),
@@ -1177,12 +1402,19 @@ a child process created via
 .BR fork (2)
 inherits duplicates of its parent's file descriptors,
 and those duplicates refer to the same open file descriptions.
-
+.PP
 Each
-.BR open (2)
+.BR open ()
 of a file creates a new open file description;
 thus, there may be multiple open file descriptions
 corresponding to a file inode.
+.PP
+On Linux, one can use the
+.BR kcmp (2)
+.B KCMP_FILE
+operation to test whether two file descriptors
+(in the same process or in two different processes)
+refer to the same open file description.
 .\"
 .\"
 .SS Synchronized I/O
@@ -1200,18 +1432,22 @@ Regardless of whether an implementation supports this option,
 it must at least support the use of
 .BR O_SYNC
 for regular files.
-
+.PP
 Linux implements
 .BR O_SYNC
 and
 .BR O_DSYNC ,
 but not
 .BR O_RSYNC .
-(Somewhat incorrectly, glibc defines
+Somewhat incorrectly, glibc defines
 .BR O_RSYNC
 to have the same value as
-.BR O_SYNC .)
-
+.BR O_SYNC .
+.RB ( O_RSYNC
+is defined in the Linux header file
+.I <asm/fcntl.h>
+on HP PA-RISC, but it is not used.)
+.PP
 .BR O_SYNC
 provides synchronized I/O
 .I file
@@ -1229,7 +1465,7 @@ to allow a subsequent read operation to complete successfully.
 Data integrity completion can reduce the number of disk operations
 that are required for applications that don't need the guarantees
 of file integrity completion.
-
+.PP
 To understand the difference between the two types of completion,
 consider two pieces of file metadata:
 the file last modification timestamp
@@ -1246,7 +1482,7 @@ would only guarantee to flush updates to the file length metadata
 (whereas
 .BR O_SYNC
 would also always flush the last modification timestamp metadata).
-
+.PP
 Before Linux 2.6.33, Linux implemented only the
 .BR O_SYNC
 flag for
@@ -1258,7 +1494,7 @@ integrity completion (i.e.,
 .BR O_SYNC
 was actually implemented as the equivalent of
 .BR O_DSYNC ).
-
+.PP
 Since Linux 2.6.33, proper
 .BR O_SYNC
 support is provided.
@@ -1276,12 +1512,22 @@ new headers get at least
 .BR O_DSYNC
 semantics on pre-2.6.33 kernels.
 .\"
+.SS C library/kernel differences
+Since version 2.26,
+the glibc wrapper function for
+.BR open ()
+employs the
+.BR openat ()
+system call, rather than the kernel's
+.BR open ()
+system call.
+For certain architectures, this is also true in glibc versions before 2.26.
 .\"
 .SS NFS
 There are many infelicities in the protocol underlying NFS, affecting
 amongst others
 .BR O_SYNC " and " O_NDELAY .
-
+.PP
 On NFS filesystems with UID mapping enabled,
 .BR open ()
 may
@@ -1319,7 +1565,7 @@ In other words, the combination
 .B "O_RDONLY | O_WRONLY"
 is a logical error, and certainly does not have the same meaning as
 .BR O_RDWR .
-
+.PP
 Linux reserves the special, nonstandard access mode 3 (binary 11) in
 .I flags
 to mean:
@@ -1346,26 +1592,30 @@ a directory file descriptor argument
 .BR fanotify_mark (2),
 .BR fchmodat (2),
 .BR fchownat (2),
+.BR fspick (2),
 .BR fstatat (2),
 .BR futimesat (2),
 .BR linkat (2),
 .BR mkdirat (2),
+.BR move_mount (2),
 .BR mknodat (2),
 .BR name_to_handle_at (2),
+.BR open_tree (2),
+.BR openat2 (2),
 .BR readlinkat (2),
 .BR renameat (2),
+.BR statx (2),
 .BR symlinkat (2),
 .BR unlinkat (2),
 .BR utimensat (2),
 .BR mkfifoat (3),
 and
 .BR scandirat (3))
-are supported
-for two reasons.
+address two problems with the older interfaces that preceded them.
 Here, the explanation is in terms of the
 .BR openat ()
 call, but the rationale is analogous for the other interfaces.
-
+.PP
 First,
 .BR openat ()
 allows an application to avoid race conditions that could
@@ -1378,14 +1628,14 @@ of the directory prefix given to
 could be changed in parallel with the call to
 .BR open ().
 Suppose, for example, that we wish to create the file
-.I path/to/xxx.dep
+.I dir1/dir2/xxx.dep
 if the file
-.I path/to/xxx
+.I dir1/dir2/xxx
 exists.
-The problem is that between the existence check and the file creation step,
-.I path
+The problem is that between the existence check and the file-creation step,
+.I dir1
 or
-.I to
+.I dir2
 (which might be symbolic links)
 could be modified to point to a different location.
 Such races can be avoided by
@@ -1396,7 +1646,17 @@ argument of (say)
 .BR fstatat (2)
 and
 .BR openat ().
-
+The use of the
+.I dirfd
+file descriptor also has other benefits:
+.IP * 3
+the file descriptor is a stable reference to the directory,
+even if the directory is renamed; and
+.IP *
+the open file descriptor prevents the underlying filesystem from
+being dismounted,
+just as when a process has a current working directory on a filesystem.
+.PP
 Second,
 .BR openat ()
 allows the implementation of a per-thread "current working
@@ -1405,10 +1665,38 @@ directory", via file descriptor(s) maintained by the application.
 on the use of
 .IR /proc/self/fd/ dirfd,
 but less efficiently.)
+.PP
+The
+.I dirfd
+argument for these APIs can be obtained by using
+.BR open ()
+or
+.BR openat ()
+to open a directory (with either the
+.BR O_RDONLY
+or the
+.BR O_PATH
+flag).
+Alternatively, such a file descriptor can be obtained by applying
+.BR dirfd (3)
+to a directory stream created using
+.BR opendir (3).
+.PP
+When these APIs are given a
+.I dirfd
+argument of
+.BR AT_FDCWD
+or the specified pathname is absolute,
+then they handle their pathname argument in the same was as
+the corresponding conventional APIs.
+However, in this case, several of the APIs have a
+.I flags
+argument that provides access to functionality that is not available with
+the corresponding conventional APIs.
 .\"
 .\"
 .SS O_DIRECT
-.LP
+.PP
 The
 .B O_DIRECT
 flag may impose alignment restrictions on the length and address
@@ -1424,7 +1712,7 @@ for doing so, for example the
 .B XFS_IOC_DIOINFO
 operation in
 .BR xfsctl (3).
-.LP
+.PP
 Under Linux 2.4, transfer sizes, and the alignment of the user buffer
 and the file offset must all be multiples of the logical block size
 of the filesystem.
@@ -1434,9 +1722,11 @@ The logical block size can be determined using the
 .BR ioctl (2)
 .B BLKSSZGET
 operation or from the shell using the command:
-
+.PP
+.EX
     blockdev \-\-getss
-.LP
+.EE
+.PP
 .B O_DIRECT
 I/Os should never be run concurrently with the
 .BR fork (2)
@@ -1470,7 +1760,7 @@ with
 ensuring that it will not be available
 to the child after
 .BR fork (2).
-.LP
+.PP
 The
 .B O_DIRECT
 flag was introduced in SGI IRIX, where it has alignment
@@ -1480,16 +1770,16 @@ IRIX has also a
 call to query appropriate alignments, and sizes.
 FreeBSD 4.x introduced
 a flag of the same name, but without alignment restrictions.
-.LP
+.PP
 .B O_DIRECT
 support was added under Linux in kernel version 2.4.10.
 Older Linux kernels simply ignore this flag.
-Some filesystems may not implement the flag and
+Some filesystems may not implement the flag, in which case
 .BR open ()
-will fail with
+fails with the error
 .B EINVAL
 if it is used.
-.LP
+.PP
 Applications should avoid mixing
 .B O_DIRECT
 and normal I/O to the same file,
@@ -1500,7 +1790,7 @@ using either mode alone.
 Likewise, applications should avoid mixing
 .BR mmap (2)
 of files with direct I/O to the same files.
-.LP
+.PP
 The behavior of
 .B O_DIRECT
 with NFS will differ from local filesystems.
@@ -1529,12 +1819,6 @@ is a potentially powerful tool that should be used with caution.
 It is recommended that applications treat use of
 .B O_DIRECT
 as a performance option which is disabled by default.
-.PP
-.RS
-"The thing that has always disturbed me about O_DIRECT is that the whole
-interface is just stupid, and was probably designed by a deranged monkey
-on some serious mind-controlling substances."\(emLinus
-.RE
 .SH BUGS
 Currently, it is not possible to enable signal-driven
 I/O by specifying
@@ -1546,7 +1830,7 @@ use
 to enable this flag.
 .\" FIXME . Check bugzilla report on open(O_ASYNC)
 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=5993
-
+.PP
 One must check for two different error codes,
 .B EISDIR
 and
@@ -1554,7 +1838,7 @@ and
 when trying to determine whether the kernel supports
 .B O_TMPFILE
 functionality.
-
+.PP
 When both
 .B O_CREAT
 and
@@ -1580,6 +1864,7 @@ is ignored).
 .BR mmap (2),
 .BR mount (2),
 .BR open_by_handle_at (2),
+.BR openat2 (2),
 .BR read (2),
 .BR socket (2),
 .BR stat (2),
@@ -1589,5 +1874,6 @@ is ignored).
 .BR fopen (3),
 .BR acl (5),
 .BR fifo (7),
+.BR inode (7),
 .BR path_resolution (7),
 .BR symlink (7)