.\" 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-07-17 "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
.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
.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
.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
.\" 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
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.
.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)
.\" 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
.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)"
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).
synchronized I/O
.I data
integrity completion.
-
+.IP
By the time
.BR write (2)
(and similar)
.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
.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.
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.
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.
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
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
.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),
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)
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
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
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)
.\" 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
.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
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 :
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
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
the calling process, as is done by
.BR open ()
for a relative pathname).
-
+.PP
If
.I pathname
is relative and
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).
(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
.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
.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
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
.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
.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
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
.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 ,
One must define
.B _GNU_SOURCE
to obtain their definitions.
-
+.PP
The
.BR O_CLOEXEC ,
.BR O_DIRECTORY ,
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
.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.
.\" Tru64 5.1B: truncate
.\" HP-UX 11.22: truncate
.\" FreeBSD 4.7: truncate
-
+.PP
Note that
.BR open ()
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 ,
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
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),
.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
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
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
(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
.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.
.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
.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:
.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
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
.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
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
.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.
.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)
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
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,
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.
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
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
when trying to determine whether the kernel supports
.B O_TMPFILE
functionality.
-
+.PP
When both
.B O_CREAT
and
.BR mmap (2),
.BR mount (2),
.BR open_by_handle_at (2),
+.BR openat2 (2),
.BR read (2),
.BR socket (2),
.BR stat (2),
.BR fopen (3),
.BR acl (5),
.BR fifo (7),
+.BR inode (7),
.BR path_resolution (7),
.BR symlink (7)