Blame man2/open.2

Packit 7cfc04
.\" This manpage is Copyright (C) 1992 Drew Eckhardt;
Packit 7cfc04
.\" and Copyright (C) 1993 Michael Haardt, Ian Jackson.
Packit 7cfc04
.\" and Copyright (C) 2008 Greg Banks
Packit 7cfc04
.\" and Copyright (C) 2006, 2008, 2013, 2014 Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" Modified 1993-07-21 by Rik Faith <faith@cs.unc.edu>
Packit 7cfc04
.\" Modified 1994-08-21 by Michael Haardt
Packit 7cfc04
.\" Modified 1996-04-13 by Andries Brouwer <aeb@cwi.nl>
Packit 7cfc04
.\" Modified 1996-05-13 by Thomas Koenig
Packit 7cfc04
.\" Modified 1996-12-20 by Michael Haardt
Packit 7cfc04
.\" Modified 1999-02-19 by Andries Brouwer <aeb@cwi.nl>
Packit 7cfc04
.\" Modified 1998-11-28 by Joseph S. Myers <jsm28@hermes.cam.ac.uk>
Packit 7cfc04
.\" Modified 1999-06-03 by Michael Haardt
Packit 7cfc04
.\" Modified 2002-05-07 by Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\" 2004-12-08, mtk, reordered flags list alphabetically
Packit 7cfc04
.\" 2004-12-08, Martin Pool <mbp@sourcefrog.net> (& mtk), added O_NOATIME
Packit 7cfc04
.\" 2007-09-18, mtk, Added description of O_CLOEXEC + other minor edits
Packit 7cfc04
.\" 2008-01-03, mtk, with input from Trond Myklebust
Packit 7cfc04
.\"     <trond.myklebust@fys.uio.no> and Timo Sirainen <tss@iki.fi>
Packit 7cfc04
.\"     Rewrite description of O_EXCL.
Packit 7cfc04
.\" 2008-01-11, Greg Banks <gnb@melbourne.sgi.com>: add more detail
Packit 7cfc04
.\"     on O_DIRECT.
Packit 7cfc04
.\" 2008-02-26, Michael Haardt: Reorganized text for O_CREAT and mode
Packit 7cfc04
.\"
Packit 7cfc04
.\" FIXME . Apr 08: The next POSIX revision has O_EXEC, O_SEARCH, and
Packit 7cfc04
.\" O_TTYINIT.  Eventually these may need to be documented.  --mtk
Packit 7cfc04
.\"
Packit 7cfc04
.TH OPEN 2 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
open, openat, creat \- open and possibly create a file
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.B #include <sys/types.h>
Packit 7cfc04
.B #include <sys/stat.h>
Packit 7cfc04
.B #include <fcntl.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int open(const char *" pathname ", int " flags );
Packit 7cfc04
.BI "int open(const char *" pathname ", int " flags ", mode_t " mode );
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int creat(const char *" pathname ", mode_t " mode );
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int openat(int " dirfd ", const char *" pathname ", int " flags );
Packit 7cfc04
.BI "int openat(int " dirfd ", const char *" pathname ", int " flags \
Packit 7cfc04
", mode_t " mode );
Packit 7cfc04
.fi
Packit 7cfc04
.PP
Packit 7cfc04
.in -4n
Packit 7cfc04
Feature Test Macro Requirements for glibc (see
Packit 7cfc04
.BR feature_test_macros (7)):
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
.BR openat ():
Packit 7cfc04
.PD 0
Packit 7cfc04
.ad l
Packit 7cfc04
.RS 4
Packit 7cfc04
.TP 4
Packit 7cfc04
Since glibc 2.10:
Packit 7cfc04
_POSIX_C_SOURCE\ >=\ 200809L
Packit 7cfc04
.TP
Packit 7cfc04
Before glibc 2.10:
Packit 7cfc04
_ATFILE_SOURCE
Packit 7cfc04
.RE
Packit 7cfc04
.ad
Packit 7cfc04
.PD
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The
Packit 7cfc04
.BR open ()
Packit 7cfc04
system call opens the file specified by
Packit 7cfc04
.IR pathname .
Packit 7cfc04
If the specified file does not exist,
Packit 7cfc04
it may optionally (if
Packit 7cfc04
.B O_CREAT
Packit 7cfc04
is specified in
Packit 7cfc04
.IR flags )
Packit 7cfc04
be created by
Packit 7cfc04
.BR open ().
Packit 7cfc04
.PP
Packit 7cfc04
The return value of
Packit 7cfc04
.BR open ()
Packit 7cfc04
is a file descriptor, a small, nonnegative integer that is used
Packit 7cfc04
in subsequent system calls
Packit 7cfc04
.RB ( read "(2), " write "(2), " lseek "(2), " fcntl (2),
Packit 7cfc04
etc.) to refer to the open file.
Packit 7cfc04
The file descriptor returned by a successful call will be
Packit 7cfc04
the lowest-numbered file descriptor not currently open for the process.
Packit 7cfc04
.PP
Packit 7cfc04
By default, the new file descriptor is set to remain open across an
Packit 7cfc04
.BR execve (2)
Packit 7cfc04
(i.e., the
Packit 7cfc04
.B FD_CLOEXEC
Packit 7cfc04
file descriptor flag described in
Packit 7cfc04
.BR fcntl (2)
Packit 7cfc04
is initially disabled); the
Packit 7cfc04
.B O_CLOEXEC
Packit 7cfc04
flag, described below, can be used to change this default.
Packit 7cfc04
The file offset is set to the beginning of the file (see
Packit 7cfc04
.BR lseek (2)).
Packit 7cfc04
.PP
Packit 7cfc04
A call to
Packit 7cfc04
.BR open ()
Packit 7cfc04
creates a new
Packit 7cfc04
.IR "open file description" ,
Packit 7cfc04
an entry in the system-wide table of open files.
Packit 7cfc04
The open file description records the file offset and the file status flags
Packit 7cfc04
(see below).
Packit 7cfc04
A file descriptor is a reference to an open file description;
Packit 7cfc04
this reference is unaffected if
Packit 7cfc04
.I pathname
Packit 7cfc04
is subsequently removed or modified to refer to a different file.
Packit 7cfc04
For further details on open file descriptions, see NOTES.
Packit 7cfc04
.PP
Packit 7cfc04
The argument
Packit 7cfc04
.I flags
Packit 7cfc04
must include one of the following
Packit 7cfc04
.IR "access modes" :
Packit 7cfc04
.BR O_RDONLY ", " O_WRONLY ", or " O_RDWR .
Packit 7cfc04
These request opening the file read-only, write-only, or read/write,
Packit 7cfc04
respectively.
Packit 7cfc04
.PP
Packit 7cfc04
In addition, zero or more file creation flags and file status flags
Packit 7cfc04
can be
Packit 7cfc04
.RI bitwise- or 'd
Packit 7cfc04
in
Packit 7cfc04
.IR flags .
Packit 7cfc04
The
Packit 7cfc04
.I file creation flags
Packit 7cfc04
are
Packit 7cfc04
.BR O_CLOEXEC ,
Packit 7cfc04
.BR O_CREAT ,
Packit 7cfc04
.BR O_DIRECTORY ,
Packit 7cfc04
.BR O_EXCL ,
Packit 7cfc04
.BR O_NOCTTY ,
Packit 7cfc04
.BR O_NOFOLLOW ,
Packit 7cfc04
.BR O_TMPFILE ,
Packit 7cfc04
and
Packit 7cfc04
.BR O_TRUNC .
Packit 7cfc04
The
Packit 7cfc04
.I file status flags
Packit 7cfc04
are all of the remaining flags listed below.
Packit 7cfc04
.\" SUSv4 divides the flags into:
Packit 7cfc04
.\" * Access mode
Packit 7cfc04
.\" * File creation
Packit 7cfc04
.\" * File status
Packit 7cfc04
.\" * Other (O_CLOEXEC, O_DIRECTORY, O_NOFOLLOW)
Packit 7cfc04
.\" though it's not clear what the difference between "other" and
Packit 7cfc04
.\" "File creation" flags is.  I raised an Aardvark to see if this
Packit 7cfc04
.\" can be clarified in SUSv4; 10 Oct 2008.
Packit 7cfc04
.\" http://thread.gmane.org/gmane.comp.standards.posix.austin.general/64/focus=67
Packit 7cfc04
.\" TC1 (balloted in 2013), resolved this, so that those three constants
Packit 7cfc04
.\" are also categorized" as file status flags.
Packit 7cfc04
.\"
Packit 7cfc04
The distinction between these two groups of flags is that
Packit 7cfc04
the file creation flags affect the semantics of the open operation itself,
Packit 7cfc04
while the file status flags affect the semantics of subsequent I/O operations.
Packit 7cfc04
The file status flags can be retrieved and (in some cases)
Packit 7cfc04
modified; see
Packit 7cfc04
.BR fcntl (2)
Packit 7cfc04
for details.
Packit 7cfc04
.PP
Packit 7cfc04
The full list of file creation flags and file status flags is as follows:
Packit 7cfc04
.TP
Packit 7cfc04
.B O_APPEND
Packit 7cfc04
The file is opened in append mode.
Packit 7cfc04
Before each
Packit 7cfc04
.BR write (2),
Packit 7cfc04
the file offset is positioned at the end of the file,
Packit 7cfc04
as if with
Packit 7cfc04
.BR lseek (2).
Packit 7cfc04
The modification of the file offset and the write operation
Packit 7cfc04
are performed as a single atomic step.
Packit 7cfc04
.IP
Packit 7cfc04
.B O_APPEND
Packit 7cfc04
may lead to corrupted files on NFS filesystems if more than one process
Packit 7cfc04
appends data to a file at once.
Packit 7cfc04
.\" For more background, see
Packit 7cfc04
.\" http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=453946
Packit 7cfc04
.\" http://nfs.sourceforge.net/
Packit 7cfc04
This is because NFS does not support
Packit 7cfc04
appending to a file, so the client kernel has to simulate it, which
Packit 7cfc04
can't be done without a race condition.
Packit 7cfc04
.TP
Packit 7cfc04
.B O_ASYNC
Packit 7cfc04
Enable signal-driven I/O:
Packit 7cfc04
generate a signal
Packit 7cfc04
.RB ( SIGIO
Packit 7cfc04
by default, but this can be changed via
Packit 7cfc04
.BR fcntl (2))
Packit 7cfc04
when input or output becomes possible on this file descriptor.
Packit 7cfc04
This feature is available only for terminals, pseudoterminals,
Packit 7cfc04
sockets, and (since Linux 2.6) pipes and FIFOs.
Packit 7cfc04
See
Packit 7cfc04
.BR fcntl (2)
Packit 7cfc04
for further details.
Packit 7cfc04
See also BUGS, below.
Packit 7cfc04
.TP
Packit 7cfc04
.BR O_CLOEXEC " (since Linux 2.6.23)"
Packit 7cfc04
.\" NOTE! several other man pages refer to this text
Packit 7cfc04
Enable the close-on-exec flag for the new file descriptor.
Packit 7cfc04
.\" FIXME . for later review when Issue 8 is one day released...
Packit 7cfc04
.\" POSIX proposes to fix many APIs that provide hidden FDs
Packit 7cfc04
.\" http://austingroupbugs.net/tag_view_page.php?tag_id=8
Packit 7cfc04
.\" http://austingroupbugs.net/view.php?id=368
Packit 7cfc04
Specifying this flag permits a program to avoid additional
Packit 7cfc04
.BR fcntl (2)
Packit 7cfc04
.B F_SETFD
Packit 7cfc04
operations to set the
Packit 7cfc04
.B FD_CLOEXEC
Packit 7cfc04
flag.
Packit 7cfc04
.IP
Packit 7cfc04
Note that the use of this flag is essential in some multithreaded programs,
Packit 7cfc04
because using a separate
Packit 7cfc04
.BR fcntl (2)
Packit 7cfc04
.B F_SETFD
Packit 7cfc04
operation to set the
Packit 7cfc04
.B FD_CLOEXEC
Packit 7cfc04
flag does not suffice to avoid race conditions
Packit 7cfc04
where one thread opens a file descriptor and
Packit 7cfc04
attempts to set its close-on-exec flag using
Packit 7cfc04
.BR fcntl (2)
Packit 7cfc04
at the same time as another thread does a
Packit 7cfc04
.BR fork (2)
Packit 7cfc04
plus
Packit 7cfc04
.BR execve (2).
Packit 7cfc04
Depending on the order of execution,
Packit 7cfc04
the race may lead to the file descriptor returned by
Packit 7cfc04
.BR open ()
Packit 7cfc04
being unintentionally leaked to the program executed by the child process
Packit 7cfc04
created by
Packit 7cfc04
.BR fork (2).
Packit 7cfc04
(This kind of race is in principle possible for any system call
Packit 7cfc04
that creates a file descriptor whose close-on-exec flag should be set,
Packit 7cfc04
and various other Linux system calls provide an equivalent of the
Packit 7cfc04
.BR O_CLOEXEC
Packit 7cfc04
flag to deal with this problem.)
Packit 7cfc04
.\" This flag fixes only one form of the race condition;
Packit 7cfc04
.\" The race can also occur with, for example, file descriptors
Packit 7cfc04
.\" returned by accept(), pipe(), etc.
Packit 7cfc04
.TP
Packit 7cfc04
.B O_CREAT
Packit 7cfc04
If
Packit 7cfc04
.I pathname
Packit 7cfc04
does not exist, create it as a regular file.
Packit 7cfc04
.IP
Packit 7cfc04
The owner (user ID) of the new file is set to the effective user ID
Packit 7cfc04
of the process.
Packit 7cfc04
.IP
Packit 7cfc04
The group ownership (group ID) of the new file is set either to
Packit 7cfc04
the effective group ID of the process (System V semantics)
Packit 7cfc04
or to the group ID of the parent directory (BSD semantics).
Packit 7cfc04
On Linux, the behavior depends on whether the
Packit 7cfc04
set-group-ID mode bit is set on the parent directory:
Packit 7cfc04
if that bit is set, then BSD semantics apply;
Packit 7cfc04
otherwise, System V semantics apply.
Packit 7cfc04
For some filesystems, the behavior also depends on the
Packit 7cfc04
.I bsdgroups
Packit 7cfc04
and
Packit 7cfc04
.I sysvgroups
Packit 7cfc04
mount options described in
Packit 7cfc04
.BR mount (8)).
Packit 7cfc04
.\" As at 2.6.25, bsdgroups is supported by ext2, ext3, ext4, and
Packit 7cfc04
.\" XFS (since 2.6.14).
Packit 7cfc04
.RS
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.I mode
Packit 7cfc04
argument specifies the file mode bits be applied when a new file is created.
Packit 7cfc04
This argument must be supplied when
Packit 7cfc04
.B O_CREAT
Packit 7cfc04
or
Packit 7cfc04
.B O_TMPFILE
Packit 7cfc04
is specified in
Packit 7cfc04
.IR flags ;
Packit 7cfc04
if neither
Packit 7cfc04
.B O_CREAT
Packit 7cfc04
nor
Packit 7cfc04
.B O_TMPFILE
Packit 7cfc04
is specified, then
Packit 7cfc04
.I mode
Packit 7cfc04
is ignored.
Packit 7cfc04
The effective mode is modified by the process's
Packit 7cfc04
.I umask
Packit 7cfc04
in the usual way: in the absence of a default ACL, the mode of the
Packit 7cfc04
created file is
Packit 7cfc04
.IR "(mode\ &\ ~umask)" .
Packit 7cfc04
Note that this mode applies only to future accesses of the
Packit 7cfc04
newly created file; the
Packit 7cfc04
.BR open ()
Packit 7cfc04
call that creates a read-only file may well return a read/write
Packit 7cfc04
file descriptor.
Packit 7cfc04
.PP
Packit 7cfc04
The following symbolic constants are provided for
Packit 7cfc04
.IR mode :
Packit 7cfc04
.TP 9
Packit 7cfc04
.B S_IRWXU
Packit 7cfc04
00700 user (file owner) has read, write, and execute permission
Packit 7cfc04
.TP
Packit 7cfc04
.B S_IRUSR
Packit 7cfc04
00400 user has read permission
Packit 7cfc04
.TP
Packit 7cfc04
.B S_IWUSR
Packit 7cfc04
00200 user has write permission
Packit 7cfc04
.TP
Packit 7cfc04
.B S_IXUSR
Packit 7cfc04
00100 user has execute permission
Packit 7cfc04
.TP
Packit 7cfc04
.B S_IRWXG
Packit 7cfc04
00070 group has read, write, and execute permission
Packit 7cfc04
.TP
Packit 7cfc04
.B S_IRGRP
Packit 7cfc04
00040 group has read permission
Packit 7cfc04
.TP
Packit 7cfc04
.B S_IWGRP
Packit 7cfc04
00020 group has write permission
Packit 7cfc04
.TP
Packit 7cfc04
.B S_IXGRP
Packit 7cfc04
00010 group has execute permission
Packit 7cfc04
.TP
Packit 7cfc04
.B S_IRWXO
Packit 7cfc04
00007 others have read, write, and execute permission
Packit 7cfc04
.TP
Packit 7cfc04
.B S_IROTH
Packit 7cfc04
00004 others have read permission
Packit 7cfc04
.TP
Packit 7cfc04
.B S_IWOTH
Packit 7cfc04
00002 others have write permission
Packit 7cfc04
.TP
Packit 7cfc04
.B S_IXOTH
Packit 7cfc04
00001 others have execute permission
Packit 7cfc04
.RE
Packit 7cfc04
.IP
Packit 7cfc04
According to POSIX, the effect when other bits are set in
Packit 7cfc04
.I mode
Packit 7cfc04
is unspecified.
Packit 7cfc04
On Linux, the following bits are also honored in
Packit 7cfc04
.IR mode :
Packit 7cfc04
.RS
Packit 7cfc04
.TP 9
Packit 7cfc04
.B S_ISUID
Packit 7cfc04
0004000 set-user-ID bit
Packit 7cfc04
.TP
Packit 7cfc04
.B S_ISGID
Packit 7cfc04
0002000 set-group-ID bit (see
Packit 7cfc04
.BR inode (7)).
Packit 7cfc04
.TP
Packit 7cfc04
.B S_ISVTX
Packit 7cfc04
0001000 sticky bit (see
Packit 7cfc04
.BR inode (7)).
Packit 7cfc04
.RE
Packit 7cfc04
.TP
Packit 7cfc04
.BR O_DIRECT " (since Linux 2.4.10)"
Packit 7cfc04
Try to minimize cache effects of the I/O to and from this file.
Packit 7cfc04
In general this will degrade performance, but it is useful in
Packit 7cfc04
special situations, such as when applications do their own caching.
Packit 7cfc04
File I/O is done directly to/from user-space buffers.
Packit 7cfc04
The
Packit 7cfc04
.B O_DIRECT
Packit 7cfc04
flag on its own makes an effort to transfer data synchronously,
Packit 7cfc04
but does not give the guarantees of the
Packit 7cfc04
.B O_SYNC
Packit 7cfc04
flag that data and necessary metadata are transferred.
Packit 7cfc04
To guarantee synchronous I/O,
Packit 7cfc04
.B O_SYNC
Packit 7cfc04
must be used in addition to
Packit 7cfc04
.BR O_DIRECT .
Packit 7cfc04
See NOTES below for further discussion.
Packit 7cfc04
.IP
Packit 7cfc04
A semantically similar (but deprecated) interface for block devices
Packit 7cfc04
is described in
Packit 7cfc04
.BR raw (8).
Packit 7cfc04
.TP
Packit 7cfc04
.B O_DIRECTORY
Packit 7cfc04
If \fIpathname\fP is not a directory, cause the open to fail.
Packit 7cfc04
.\" But see the following and its replies:
Packit 7cfc04
.\" http://marc.theaimsgroup.com/?t=112748702800001&r=1&w=2
Packit 7cfc04
.\" [PATCH] open: O_DIRECTORY and O_CREAT together should fail
Packit 7cfc04
.\" O_DIRECTORY | O_CREAT causes O_DIRECTORY to be ignored.
Packit 7cfc04
This flag was added in kernel version 2.1.126, to
Packit 7cfc04
avoid denial-of-service problems if
Packit 7cfc04
.BR opendir (3)
Packit 7cfc04
is called on a
Packit 7cfc04
FIFO or tape device.
Packit 7cfc04
.TP
Packit 7cfc04
.B O_DSYNC
Packit 7cfc04
Write operations on the file will complete according to the requirements of
Packit 7cfc04
synchronized I/O
Packit 7cfc04
.I data
Packit 7cfc04
integrity completion.
Packit 7cfc04
.IP
Packit 7cfc04
By the time
Packit 7cfc04
.BR write (2)
Packit 7cfc04
(and similar)
Packit 7cfc04
return, the output data
Packit 7cfc04
has been transferred to the underlying hardware,
Packit 7cfc04
along with any file metadata that would be required to retrieve that data
Packit 7cfc04
(i.e., as though each
Packit 7cfc04
.BR write (2)
Packit 7cfc04
was followed by a call to
Packit 7cfc04
.BR fdatasync (2)).
Packit 7cfc04
.IR "See NOTES below" .
Packit 7cfc04
.TP
Packit 7cfc04
.B O_EXCL
Packit 7cfc04
Ensure that this call creates the file:
Packit 7cfc04
if this flag is specified in conjunction with
Packit 7cfc04
.BR O_CREAT ,
Packit 7cfc04
and
Packit 7cfc04
.I pathname
Packit 7cfc04
already exists, then
Packit 7cfc04
.BR open ()
Packit 7cfc04
fails with the error
Packit 7cfc04
.BR EEXIST .
Packit 7cfc04
.IP
Packit 7cfc04
When these two flags are specified, symbolic links are not followed:
Packit 7cfc04
.\" POSIX.1-2001 explicitly requires this behavior.
Packit 7cfc04
if
Packit 7cfc04
.I pathname
Packit 7cfc04
is a symbolic link, then
Packit 7cfc04
.BR open ()
Packit 7cfc04
fails regardless of where the symbolic link points.
Packit 7cfc04
.IP
Packit 7cfc04
In general, the behavior of
Packit 7cfc04
.B O_EXCL
Packit 7cfc04
is undefined if it is used without
Packit 7cfc04
.BR O_CREAT .
Packit 7cfc04
There is one exception: on Linux 2.6 and later,
Packit 7cfc04
.B O_EXCL
Packit 7cfc04
can be used without
Packit 7cfc04
.B O_CREAT
Packit 7cfc04
if
Packit 7cfc04
.I pathname
Packit 7cfc04
refers to a block device.
Packit 7cfc04
If the block device is in use by the system (e.g., mounted),
Packit 7cfc04
.BR open ()
Packit 7cfc04
fails with the error
Packit 7cfc04
.BR EBUSY .
Packit 7cfc04
.IP
Packit 7cfc04
On NFS,
Packit 7cfc04
.B O_EXCL
Packit 7cfc04
is supported only when using NFSv3 or later on kernel 2.6 or later.
Packit 7cfc04
In NFS environments where
Packit 7cfc04
.B O_EXCL
Packit 7cfc04
support is not provided, programs that rely on it
Packit 7cfc04
for performing locking tasks will contain a race condition.
Packit 7cfc04
Portable programs that want to perform atomic file locking using a lockfile,
Packit 7cfc04
and need to avoid reliance on NFS support for
Packit 7cfc04
.BR O_EXCL ,
Packit 7cfc04
can create a unique file on
Packit 7cfc04
the same filesystem (e.g., incorporating hostname and PID), and use
Packit 7cfc04
.BR link (2)
Packit 7cfc04
to make a link to the lockfile.
Packit 7cfc04
If
Packit 7cfc04
.BR link (2)
Packit 7cfc04
returns 0, the lock is successful.
Packit 7cfc04
Otherwise, use
Packit 7cfc04
.BR stat (2)
Packit 7cfc04
on the unique file to check if its link count has increased to 2,
Packit 7cfc04
in which case the lock is also successful.
Packit 7cfc04
.TP
Packit 7cfc04
.B O_LARGEFILE
Packit 7cfc04
(LFS)
Packit 7cfc04
Allow files whose sizes cannot be represented in an
Packit 7cfc04
.I off_t
Packit 7cfc04
(but can be represented in an
Packit 7cfc04
.IR off64_t )
Packit 7cfc04
to be opened.
Packit 7cfc04
The
Packit 7cfc04
.B _LARGEFILE64_SOURCE
Packit 7cfc04
macro must be defined
Packit 7cfc04
(before including
Packit 7cfc04
.I any
Packit 7cfc04
header files)
Packit 7cfc04
in order to obtain this definition.
Packit 7cfc04
Setting the
Packit 7cfc04
.B _FILE_OFFSET_BITS
Packit 7cfc04
feature test macro to 64 (rather than using
Packit 7cfc04
.BR O_LARGEFILE )
Packit 7cfc04
is the preferred
Packit 7cfc04
method of accessing large files on 32-bit systems (see
Packit 7cfc04
.BR feature_test_macros (7)).
Packit 7cfc04
.TP
Packit 7cfc04
.BR O_NOATIME " (since Linux 2.6.8)"
Packit 7cfc04
Do not update the file last access time
Packit 7cfc04
.RI ( st_atime
Packit 7cfc04
in the inode)
Packit 7cfc04
when the file is
Packit 7cfc04
.BR read (2).
Packit 7cfc04
.IP
Packit 7cfc04
This flag can be employed only if one of the following conditions is true:
Packit 7cfc04
.RS
Packit 7cfc04
.IP * 3
Packit 7cfc04
The effective UID of the process
Packit 7cfc04
.\" Strictly speaking: the filesystem UID
Packit 7cfc04
matches the owner UID of the file.
Packit 7cfc04
.IP *
Packit 7cfc04
The calling process has the
Packit 7cfc04
.BR CAP_FOWNER
Packit 7cfc04
capability in its user namespace and
Packit 7cfc04
the owner UID of the file has a mapping in the namespace.
Packit 7cfc04
.RE
Packit 7cfc04
.IP
Packit 7cfc04
This flag is intended for use by indexing or backup programs,
Packit 7cfc04
where its use can significantly reduce the amount of disk activity.
Packit 7cfc04
This flag may not be effective on all filesystems.
Packit 7cfc04
One example is NFS, where the server maintains the access time.
Packit 7cfc04
.\" The O_NOATIME flag also affects the treatment of st_atime
Packit 7cfc04
.\" by mmap() and readdir(2), MTK, Dec 04.
Packit 7cfc04
.TP
Packit 7cfc04
.B O_NOCTTY
Packit 7cfc04
If
Packit 7cfc04
.I pathname
Packit 7cfc04
refers to a terminal device\(emsee
Packit 7cfc04
.BR tty (4)\(emit
Packit 7cfc04
will not become the process's controlling terminal even if the
Packit 7cfc04
process does not have one.
Packit 7cfc04
.TP
Packit 7cfc04
.B O_NOFOLLOW
Packit 7cfc04
If \fIpathname\fP is a symbolic link, then the open fails, with the error
Packit 7cfc04
.BR ELOOP .
Packit 7cfc04
Symbolic links in earlier components of the pathname will still be
Packit 7cfc04
followed.
Packit 7cfc04
(Note that the
Packit 7cfc04
.B ELOOP
Packit 7cfc04
error that can occur in this case is indistinguishable from the case where
Packit 7cfc04
an open fails because there are too many symbolic links found
Packit 7cfc04
while resolving components in the prefix part of the pathname.)
Packit 7cfc04
.IP
Packit 7cfc04
This flag is a FreeBSD extension, which was added to Linux in version 2.1.126,
Packit 7cfc04
and has subsequently been standardized in POSIX.1-2008.
Packit 7cfc04
.IP
Packit 7cfc04
See also
Packit 7cfc04
.BR O_PATH
Packit 7cfc04
below.
Packit 7cfc04
.\" The headers from glibc 2.0.100 and later include a
Packit 7cfc04
.\" definition of this flag; \fIkernels before 2.1.126 will ignore it if
Packit 7cfc04
.\" used\fP.
Packit 7cfc04
.TP
Packit 7cfc04
.BR O_NONBLOCK " or " O_NDELAY
Packit 7cfc04
When possible, the file is opened in nonblocking mode.
Packit 7cfc04
Neither the
Packit 7cfc04
.BR open ()
Packit 7cfc04
nor any subsequent operations on the file descriptor which is
Packit 7cfc04
returned will cause the calling process to wait.
Packit 7cfc04
.IP
Packit 7cfc04
Note that this flag has no effect for regular files and block devices;
Packit 7cfc04
that is, I/O operations will (briefly) block when device activity
Packit 7cfc04
is required, regardless of whether
Packit 7cfc04
.B O_NONBLOCK
Packit 7cfc04
is set.
Packit 7cfc04
Since
Packit 7cfc04
.B O_NONBLOCK
Packit 7cfc04
semantics might eventually be implemented,
Packit 7cfc04
applications should not depend upon blocking behavior
Packit 7cfc04
when specifying this flag for regular files and block devices.
Packit 7cfc04
.IP
Packit 7cfc04
For the handling of FIFOs (named pipes), see also
Packit 7cfc04
.BR fifo (7).
Packit 7cfc04
For a discussion of the effect of
Packit 7cfc04
.B O_NONBLOCK
Packit 7cfc04
in conjunction with mandatory file locks and with file leases, see
Packit 7cfc04
.BR fcntl (2).
Packit 7cfc04
.TP
Packit 7cfc04
.BR O_PATH " (since Linux 2.6.39)"
Packit 7cfc04
.\" commit 1abf0c718f15a56a0a435588d1b104c7a37dc9bd
Packit 7cfc04
.\" commit 326be7b484843988afe57566b627fb7a70beac56
Packit 7cfc04
.\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
Packit 7cfc04
.\"
Packit 7cfc04
.\" http://thread.gmane.org/gmane.linux.man/2790/focus=3496
Packit 7cfc04
.\"	Subject: Re: [PATCH] open(2): document O_PATH
Packit 7cfc04
.\"	Newsgroups: gmane.linux.man, gmane.linux.kernel
Packit 7cfc04
.\"
Packit 7cfc04
Obtain a file descriptor that can be used for two purposes:
Packit 7cfc04
to indicate a location in the filesystem tree and
Packit 7cfc04
to perform operations that act purely at the file descriptor level.
Packit 7cfc04
The file itself is not opened, and other file operations (e.g.,
Packit 7cfc04
.BR read (2),
Packit 7cfc04
.BR write (2),
Packit 7cfc04
.BR fchmod (2),
Packit 7cfc04
.BR fchown (2),
Packit 7cfc04
.BR fgetxattr (2),
Packit 7cfc04
.BR ioctl (2),
Packit 7cfc04
.BR mmap (2))
Packit 7cfc04
fail with the error
Packit 7cfc04
.BR EBADF .
Packit 7cfc04
.IP
Packit 7cfc04
The following operations
Packit 7cfc04
.I can
Packit 7cfc04
be performed on the resulting file descriptor:
Packit 7cfc04
.RS
Packit 7cfc04
.IP * 3
Packit 7cfc04
.BR close (2).
Packit 7cfc04
.IP *
Packit 7cfc04
.BR fchdir (2),
Packit 7cfc04
if the file descriptor refers to a directory
Packit 7cfc04
(since Linux 3.5).
Packit 7cfc04
.\" commit 332a2e1244bd08b9e3ecd378028513396a004a24
Packit 7cfc04
.IP *
Packit 7cfc04
.BR fstat (2)
Packit 7cfc04
(since Linux 3.6).
Packit 7cfc04
.IP *
Packit 7cfc04
.\" fstat(): commit 55815f70147dcfa3ead5738fd56d3574e2e3c1c2
Packit 7cfc04
.BR fstatfs (2)
Packit 7cfc04
(since Linux 3.12).
Packit 7cfc04
.\" fstatfs(): commit 9d05746e7b16d8565dddbe3200faa1e669d23bbf
Packit 7cfc04
.IP *
Packit 7cfc04
Duplicating the file descriptor
Packit 7cfc04
.RB ( dup (2),
Packit 7cfc04
.BR fcntl (2)
Packit 7cfc04
.BR F_DUPFD ,
Packit 7cfc04
etc.).
Packit 7cfc04
.IP *
Packit 7cfc04
Getting and setting file descriptor flags
Packit 7cfc04
.RB ( fcntl (2)
Packit 7cfc04
.BR F_GETFD
Packit 7cfc04
and
Packit 7cfc04
.BR F_SETFD ).
Packit 7cfc04
.IP *
Packit 7cfc04
Retrieving open file status flags using the
Packit 7cfc04
.BR fcntl (2)
Packit 7cfc04
.BR F_GETFL
Packit 7cfc04
operation: the returned flags will include the bit
Packit 7cfc04
.BR O_PATH .
Packit 7cfc04
.IP *
Packit 7cfc04
Passing the file descriptor as the
Packit 7cfc04
.IR dirfd
Packit 7cfc04
argument of
Packit 7cfc04
.BR openat ()
Packit 7cfc04
and the other "*at()" system calls.
Packit 7cfc04
This includes
Packit 7cfc04
.BR linkat (2)
Packit 7cfc04
with
Packit 7cfc04
.BR AT_EMPTY_PATH
Packit 7cfc04
(or via procfs using
Packit 7cfc04
.BR AT_SYMLINK_FOLLOW )
Packit 7cfc04
even if the file is not a directory.
Packit 7cfc04
.IP *
Packit 7cfc04
Passing the file descriptor to another process via a UNIX domain socket
Packit 7cfc04
(see
Packit 7cfc04
.BR SCM_RIGHTS
Packit 7cfc04
in
Packit 7cfc04
.BR unix (7)).
Packit 7cfc04
.RE
Packit 7cfc04
.IP
Packit 7cfc04
When
Packit 7cfc04
.B O_PATH
Packit 7cfc04
is specified in
Packit 7cfc04
.IR flags ,
Packit 7cfc04
flag bits other than
Packit 7cfc04
.BR O_CLOEXEC ,
Packit 7cfc04
.BR O_DIRECTORY ,
Packit 7cfc04
and
Packit 7cfc04
.BR O_NOFOLLOW
Packit 7cfc04
are ignored.
Packit 7cfc04
.IP
Packit 7cfc04
Opening a file or directory with the
Packit 7cfc04
.B O_PATH
Packit 7cfc04
flag requires no permissions on the object itself
Packit 7cfc04
(but does require execute permission on the directories in the path prefix).
Packit 7cfc04
Depending on the subsequent operation,
Packit 7cfc04
a check for suitable file permissions may be performed (e.g.,
Packit 7cfc04
.BR fchdir (2)
Packit 7cfc04
requires execute permission on the directory referred to
Packit 7cfc04
by its file descriptor argument).
Packit 7cfc04
By contrast,
Packit 7cfc04
obtaining a reference to a filesystem object by opening it with the
Packit 7cfc04
.B O_RDONLY
Packit 7cfc04
flag requires that the caller have read permission on the object,
Packit 7cfc04
even when the subsequent operation (e.g.,
Packit 7cfc04
.BR fchdir (2),
Packit 7cfc04
.BR fstat (2))
Packit 7cfc04
does not require read permission on the object.
Packit 7cfc04
.IP
Packit 7cfc04
If
Packit 7cfc04
.I pathname
Packit 7cfc04
is a symbolic link and the
Packit 7cfc04
.BR O_NOFOLLOW
Packit 7cfc04
flag is also specified,
Packit 7cfc04
then the call returns a file descriptor referring to the symbolic link.
Packit 7cfc04
This file descriptor can be used as the
Packit 7cfc04
.I dirfd
Packit 7cfc04
argument in calls to
Packit 7cfc04
.BR fchownat (2),
Packit 7cfc04
.BR fstatat (2),
Packit 7cfc04
.BR linkat (2),
Packit 7cfc04
and
Packit 7cfc04
.BR readlinkat (2)
Packit 7cfc04
with an empty pathname to have the calls operate on the symbolic link.
Packit 7cfc04
.IP
Packit 7cfc04
If
Packit 7cfc04
.I pathname
Packit 7cfc04
refers to an automount point that has not yet been triggered, so no
Packit 7cfc04
other filesystem is mounted on it, then the call returns a file
Packit 7cfc04
descriptor referring to the automount directory without triggering a mount.
Packit 7cfc04
.BR fstatfs (2)
Packit 7cfc04
can then be used to determine if it is, in fact, an untriggered
Packit 7cfc04
automount point
Packit 7cfc04
.RB ( ".f_type == AUTOFS_SUPER_MAGIC" ).
Packit 7cfc04
.IP
Packit 7cfc04
One use of
Packit 7cfc04
.B O_PATH
Packit 7cfc04
for regular files is to provide the equivalent of POSIX.1's
Packit 7cfc04
.B O_EXEC
Packit 7cfc04
functionality.
Packit 7cfc04
This permits us to open a file for which we have execute
Packit 7cfc04
permission but not read permission, and then execute that file,
Packit 7cfc04
with steps something like the following:
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
char buf[PATH_MAX];
Packit 7cfc04
fd = open("some_prog", O_PATH);
Packit 7cfc04
snprintf(buf, "/proc/self/fd/%d", fd);
Packit 7cfc04
execl(buf, "some_prog", (char *) NULL);
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
An
Packit 7cfc04
.B O_PATH
Packit 7cfc04
file descriptor can also be passed as the argument of
Packit 7cfc04
.BR fexecve (3).
Packit 7cfc04
.TP
Packit 7cfc04
.B O_SYNC
Packit 7cfc04
Write operations on the file will complete according to the requirements of
Packit 7cfc04
synchronized I/O
Packit 7cfc04
.I file
Packit 7cfc04
integrity completion
Packit 7cfc04
(by contrast with the
Packit 7cfc04
synchronized I/O
Packit 7cfc04
.I data
Packit 7cfc04
integrity completion
Packit 7cfc04
provided by
Packit 7cfc04
.BR O_DSYNC .)
Packit 7cfc04
.IP
Packit 7cfc04
By the time
Packit 7cfc04
.BR write (2)
Packit 7cfc04
(or similar)
Packit 7cfc04
returns, the output data and associated file metadata
Packit 7cfc04
have been transferred to the underlying hardware
Packit 7cfc04
(i.e., as though each
Packit 7cfc04
.BR write (2)
Packit 7cfc04
was followed by a call to
Packit 7cfc04
.BR fsync (2)).
Packit 7cfc04
.IR "See NOTES below" .
Packit 7cfc04
.TP
Packit 7cfc04
.BR O_TMPFILE " (since Linux 3.11)"
Packit 7cfc04
.\" commit 60545d0d4610b02e55f65d141c95b18ccf855b6e
Packit 7cfc04
.\" commit f4e0c30c191f87851c4a53454abb55ee276f4a7e
Packit 7cfc04
.\" commit bb458c644a59dbba3a1fe59b27106c5e68e1c4bd
Packit 7cfc04
Create an unnamed temporary regular file.
Packit 7cfc04
The
Packit 7cfc04
.I pathname
Packit 7cfc04
argument specifies a directory;
Packit 7cfc04
an unnamed inode will be created in that directory's filesystem.
Packit 7cfc04
Anything written to the resulting file will be lost when
Packit 7cfc04
the last file descriptor is closed, unless the file is given a name.
Packit 7cfc04
.IP
Packit 7cfc04
.B O_TMPFILE
Packit 7cfc04
must be specified with one of
Packit 7cfc04
.B O_RDWR
Packit 7cfc04
or
Packit 7cfc04
.B O_WRONLY
Packit 7cfc04
and, optionally,
Packit 7cfc04
.BR O_EXCL .
Packit 7cfc04
If
Packit 7cfc04
.B O_EXCL
Packit 7cfc04
is not specified, then
Packit 7cfc04
.BR linkat (2)
Packit 7cfc04
can be used to link the temporary file into the filesystem, making it
Packit 7cfc04
permanent, using code like the following:
Packit 7cfc04
.IP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
char path[PATH_MAX];
Packit 7cfc04
fd = open("/path/to/dir", O_TMPFILE | O_RDWR,
Packit 7cfc04
                        S_IRUSR | S_IWUSR);
Packit 7cfc04
Packit 7cfc04
/* File I/O on 'fd'... */
Packit 7cfc04
Packit 7cfc04
snprintf(path, PATH_MAX,  "/proc/self/fd/%d", fd);
Packit 7cfc04
linkat(AT_FDCWD, path, AT_FDCWD, "/path/for/file",
Packit 7cfc04
                        AT_SYMLINK_FOLLOW);
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.IP
Packit 7cfc04
In this case,
Packit 7cfc04
the
Packit 7cfc04
.BR open ()
Packit 7cfc04
.I mode
Packit 7cfc04
argument determines the file permission mode, as with
Packit 7cfc04
.BR O_CREAT .
Packit 7cfc04
.IP
Packit 7cfc04
Specifying
Packit 7cfc04
.B O_EXCL
Packit 7cfc04
in conjunction with
Packit 7cfc04
.B O_TMPFILE
Packit 7cfc04
prevents a temporary file from being linked into the filesystem
Packit 7cfc04
in the above manner.
Packit 7cfc04
(Note that the meaning of
Packit 7cfc04
.B O_EXCL
Packit 7cfc04
in this case is different from the meaning of
Packit 7cfc04
.B O_EXCL
Packit 7cfc04
otherwise.)
Packit 7cfc04
.IP
Packit 7cfc04
There are two main use cases for
Packit 7cfc04
.\" Inspired by http://lwn.net/Articles/559147/
Packit 7cfc04
.BR O_TMPFILE :
Packit 7cfc04
.RS
Packit 7cfc04
.IP * 3
Packit 7cfc04
Improved
Packit 7cfc04
.BR tmpfile (3)
Packit 7cfc04
functionality: race-free creation of temporary files that
Packit 7cfc04
(1) are automatically deleted when closed;
Packit 7cfc04
(2) can never be reached via any pathname;
Packit 7cfc04
(3) are not subject to symlink attacks; and
Packit 7cfc04
(4) do not require the caller to devise unique names.
Packit 7cfc04
.IP *
Packit 7cfc04
Creating a file that is initially invisible, which is then populated
Packit 7cfc04
with data and adjusted to have appropriate filesystem attributes
Packit 7cfc04
.RB ( fchown (2),
Packit 7cfc04
.BR fchmod (2),
Packit 7cfc04
.BR fsetxattr (2),
Packit 7cfc04
etc.)
Packit 7cfc04
before being atomically linked into the filesystem
Packit 7cfc04
in a fully formed state (using
Packit 7cfc04
.BR linkat (2)
Packit 7cfc04
as described above).
Packit 7cfc04
.RE
Packit 7cfc04
.IP
Packit 7cfc04
.B O_TMPFILE
Packit 7cfc04
requires support by the underlying filesystem;
Packit 7cfc04
only a subset of Linux filesystems provide that support.
Packit 7cfc04
In the initial implementation, support was provided in
Packit 7cfc04
the ext2, ext3, ext4, UDF, Minix, and shmem filesystems.
Packit 7cfc04
.\" To check for support, grep for "tmpfile" in kernel sources
Packit 7cfc04
Support for other filesystems has subsequently been added as follows:
Packit 7cfc04
XFS (Linux 3.15);
Packit 7cfc04
.\" commit 99b6436bc29e4f10e4388c27a3e4810191cc4788
Packit 7cfc04
.\" commit ab29743117f9f4c22ac44c13c1647fb24fb2bafe
Packit 7cfc04
Btrfs (Linux 3.16);
Packit 7cfc04
.\" commit ef3b9af50bfa6a1f02cd7b3f5124b712b1ba3e3c
Packit 7cfc04
F2FS (Linux 3.16);
Packit 7cfc04
.\" commit 50732df02eefb39ab414ef655979c2c9b64ad21c
Packit 7cfc04
and ubifs (Linux 4.9)
Packit 7cfc04
.TP
Packit 7cfc04
.B O_TRUNC
Packit 7cfc04
If the file already exists and is a regular file and the access mode allows
Packit 7cfc04
writing (i.e., is
Packit 7cfc04
.B O_RDWR
Packit 7cfc04
or
Packit 7cfc04
.BR O_WRONLY )
Packit 7cfc04
it will be truncated to length 0.
Packit 7cfc04
If the file is a FIFO or terminal device file, the
Packit 7cfc04
.B O_TRUNC
Packit 7cfc04
flag is ignored.
Packit 7cfc04
Otherwise, the effect of
Packit 7cfc04
.B O_TRUNC
Packit 7cfc04
is unspecified.
Packit 7cfc04
.SS creat()
Packit 7cfc04
A call to
Packit 7cfc04
.BR creat ()
Packit 7cfc04
is equivalent to calling
Packit 7cfc04
.BR open ()
Packit 7cfc04
with
Packit 7cfc04
.I flags
Packit 7cfc04
equal to
Packit 7cfc04
.BR O_CREAT|O_WRONLY|O_TRUNC .
Packit 7cfc04
.SS openat()
Packit 7cfc04
The
Packit 7cfc04
.BR openat ()
Packit 7cfc04
system call operates in exactly the same way as
Packit 7cfc04
.BR open (),
Packit 7cfc04
except for the differences described here.
Packit 7cfc04
.PP
Packit 7cfc04
If the pathname given in
Packit 7cfc04
.I pathname
Packit 7cfc04
is relative, then it is interpreted relative to the directory
Packit 7cfc04
referred to by the file descriptor
Packit 7cfc04
.I dirfd
Packit 7cfc04
(rather than relative to the current working directory of
Packit 7cfc04
the calling process, as is done by
Packit 7cfc04
.BR open ()
Packit 7cfc04
for a relative pathname).
Packit 7cfc04
.PP
Packit 7cfc04
If
Packit 7cfc04
.I pathname
Packit 7cfc04
is relative and
Packit 7cfc04
.I dirfd
Packit 7cfc04
is the special value
Packit 7cfc04
.BR AT_FDCWD ,
Packit 7cfc04
then
Packit 7cfc04
.I pathname
Packit 7cfc04
is interpreted relative to the current working
Packit 7cfc04
directory of the calling process (like
Packit 7cfc04
.BR open ()).
Packit 7cfc04
.PP
Packit 7cfc04
If
Packit 7cfc04
.I pathname
Packit 7cfc04
is absolute, then
Packit 7cfc04
.I dirfd
Packit 7cfc04
is ignored.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
.BR open (),
Packit 7cfc04
.BR openat (),
Packit 7cfc04
and
Packit 7cfc04
.BR creat ()
Packit 7cfc04
return the new file descriptor, or \-1 if an error occurred
Packit 7cfc04
(in which case,
Packit 7cfc04
.I errno
Packit 7cfc04
is set appropriately).
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
.BR open (),
Packit 7cfc04
.BR openat (),
Packit 7cfc04
and
Packit 7cfc04
.BR creat ()
Packit 7cfc04
can fail with the following errors:
Packit 7cfc04
.TP
Packit 7cfc04
.B EACCES
Packit 7cfc04
The requested access to the file is not allowed, or search permission
Packit 7cfc04
is denied for one of the directories in the path prefix of
Packit 7cfc04
.IR pathname ,
Packit 7cfc04
or the file did not exist yet and write access to the parent directory
Packit 7cfc04
is not allowed.
Packit 7cfc04
(See also
Packit 7cfc04
.BR path_resolution (7).)
Packit 7cfc04
.TP
Packit 7cfc04
.B EDQUOT
Packit 7cfc04
Where
Packit 7cfc04
.B O_CREAT
Packit 7cfc04
is specified, the file does not exist, and the user's quota of disk
Packit 7cfc04
blocks or inodes on the filesystem has been exhausted.
Packit 7cfc04
.TP
Packit 7cfc04
.B EEXIST
Packit 7cfc04
.I pathname
Packit 7cfc04
already exists and
Packit 7cfc04
.BR O_CREAT " and " O_EXCL
Packit 7cfc04
were used.
Packit 7cfc04
.TP
Packit 7cfc04
.B EFAULT
Packit 7cfc04
.I pathname
Packit 7cfc04
points outside your accessible address space.
Packit 7cfc04
.TP
Packit 7cfc04
.B EFBIG
Packit 7cfc04
See
Packit 7cfc04
.BR EOVERFLOW .
Packit 7cfc04
.TP
Packit 7cfc04
.B EINTR
Packit 7cfc04
While blocked waiting to complete an open of a slow device
Packit 7cfc04
(e.g., a FIFO; see
Packit 7cfc04
.BR fifo (7)),
Packit 7cfc04
the call was interrupted by a signal handler; see
Packit 7cfc04
.BR signal (7).
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
The filesystem does not support the
Packit 7cfc04
.BR O_DIRECT
Packit 7cfc04
flag.
Packit 7cfc04
See
Packit 7cfc04
.BR NOTES
Packit 7cfc04
for more information.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
Invalid value in
Packit 7cfc04
.\" In particular, __O_TMPFILE instead of O_TMPFILE
Packit 7cfc04
.IR flags .
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
.B O_TMPFILE
Packit 7cfc04
was specified in
Packit 7cfc04
.IR flags ,
Packit 7cfc04
but neither
Packit 7cfc04
.B O_WRONLY
Packit 7cfc04
nor
Packit 7cfc04
.B O_RDWR
Packit 7cfc04
was specified.
Packit 7cfc04
.TP
Packit 7cfc04
.B EINVAL
Packit 7cfc04
.B O_CREAT
Packit 7cfc04
was specified in
Packit 7cfc04
.I flags
Packit 7cfc04
and the final component ("basename") of the new file's
Packit 7cfc04
.I pathname
Packit 7cfc04
is invalid
Packit 7cfc04
(e.g., it contains characters not permitted by the underlying filesystem).
Packit 7cfc04
.TP
Packit 7cfc04
.B EISDIR
Packit 7cfc04
.I pathname
Packit 7cfc04
refers to a directory and the access requested involved writing
Packit 7cfc04
(that is,
Packit 7cfc04
.B O_WRONLY
Packit 7cfc04
or
Packit 7cfc04
.B O_RDWR
Packit 7cfc04
is set).
Packit 7cfc04
.TP
Packit 7cfc04
.B EISDIR
Packit 7cfc04
.I pathname
Packit 7cfc04
refers to an existing directory,
Packit 7cfc04
.B O_TMPFILE
Packit 7cfc04
and one of
Packit 7cfc04
.B O_WRONLY
Packit 7cfc04
or
Packit 7cfc04
.B O_RDWR
Packit 7cfc04
were specified in
Packit 7cfc04
.IR flags ,
Packit 7cfc04
but this kernel version does not provide the
Packit 7cfc04
.B O_TMPFILE
Packit 7cfc04
functionality.
Packit 7cfc04
.TP
Packit 7cfc04
.B ELOOP
Packit 7cfc04
Too many symbolic links were encountered in resolving
Packit 7cfc04
.IR pathname .
Packit 7cfc04
.TP
Packit 7cfc04
.B ELOOP
Packit 7cfc04
.I pathname
Packit 7cfc04
was a symbolic link, and
Packit 7cfc04
.I flags
Packit 7cfc04
specified
Packit 7cfc04
.BR O_NOFOLLOW
Packit 7cfc04
but not
Packit 7cfc04
.BR O_PATH .
Packit 7cfc04
.TP
Packit 7cfc04
.B EMFILE
Packit 7cfc04
The per-process limit on the number of open file descriptors has been reached
Packit 7cfc04
(see the description of
Packit 7cfc04
.BR RLIMIT_NOFILE
Packit 7cfc04
in
Packit 7cfc04
.BR getrlimit (2)).
Packit 7cfc04
.TP
Packit 7cfc04
.B ENAMETOOLONG
Packit 7cfc04
.I pathname
Packit 7cfc04
was too long.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENFILE
Packit 7cfc04
The system-wide limit on the total number of open files has been reached.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENODEV
Packit 7cfc04
.I pathname
Packit 7cfc04
refers to a device special file and no corresponding device exists.
Packit 7cfc04
(This is a Linux kernel bug; in this situation
Packit 7cfc04
.B ENXIO
Packit 7cfc04
must be returned.)
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOENT
Packit 7cfc04
.B O_CREAT
Packit 7cfc04
is not set and the named file does not exist.
Packit 7cfc04
Or, a directory component in
Packit 7cfc04
.I pathname
Packit 7cfc04
does not exist or is a dangling symbolic link.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOENT
Packit 7cfc04
.I pathname
Packit 7cfc04
refers to a nonexistent directory,
Packit 7cfc04
.B O_TMPFILE
Packit 7cfc04
and one of
Packit 7cfc04
.B O_WRONLY
Packit 7cfc04
or
Packit 7cfc04
.B O_RDWR
Packit 7cfc04
were specified in
Packit 7cfc04
.IR flags ,
Packit 7cfc04
but this kernel version does not provide the
Packit 7cfc04
.B O_TMPFILE
Packit 7cfc04
functionality.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOMEM
Packit 7cfc04
The named file is a FIFO,
Packit 7cfc04
but memory for the FIFO buffer can't be allocated because
Packit 7cfc04
the per-user hard limit on memory allocation for pipes has been reached
Packit 7cfc04
and the caller is not privileged; see
Packit 7cfc04
.BR pipe (7).
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOMEM
Packit 7cfc04
Insufficient kernel memory was available.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOSPC
Packit 7cfc04
.I pathname
Packit 7cfc04
was to be created but the device containing
Packit 7cfc04
.I pathname
Packit 7cfc04
has no room for the new file.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOTDIR
Packit 7cfc04
A component used as a directory in
Packit 7cfc04
.I pathname
Packit 7cfc04
is not, in fact, a directory, or \fBO_DIRECTORY\fP was specified and
Packit 7cfc04
.I pathname
Packit 7cfc04
was not a directory.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENXIO
Packit 7cfc04
.BR O_NONBLOCK " | " O_WRONLY
Packit 7cfc04
is set, the named file is a FIFO, and
Packit 7cfc04
no process has the FIFO open for reading.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENXIO
Packit 7cfc04
The file is a device special file and no corresponding device exists.
Packit 7cfc04
.TP
Packit 7cfc04
.BR EOPNOTSUPP
Packit 7cfc04
The filesystem containing
Packit 7cfc04
.I pathname
Packit 7cfc04
does not support
Packit 7cfc04
.BR O_TMPFILE .
Packit 7cfc04
.TP
Packit 7cfc04
.B EOVERFLOW
Packit 7cfc04
.I pathname
Packit 7cfc04
refers to a regular file that is too large to be opened.
Packit 7cfc04
The usual scenario here is that an application compiled
Packit 7cfc04
on a 32-bit platform without
Packit 7cfc04
.I -D_FILE_OFFSET_BITS=64
Packit 7cfc04
tried to open a file whose size exceeds
Packit 7cfc04
.I (1<<31)-1
Packit 7cfc04
bytes;
Packit 7cfc04
see also
Packit 7cfc04
.B O_LARGEFILE
Packit 7cfc04
above.
Packit 7cfc04
This is the error specified by POSIX.1;
Packit 7cfc04
in kernels before 2.6.24, Linux gave the error
Packit 7cfc04
.B EFBIG
Packit 7cfc04
for this case.
Packit 7cfc04
.\" See http://bugzilla.kernel.org/show_bug.cgi?id=7253
Packit 7cfc04
.\" "Open of a large file on 32-bit fails with EFBIG, should be EOVERFLOW"
Packit 7cfc04
.\" Reported 2006-10-03
Packit 7cfc04
.TP
Packit 7cfc04
.B EPERM
Packit 7cfc04
The
Packit 7cfc04
.B O_NOATIME
Packit 7cfc04
flag was specified, but the effective user ID of the caller
Packit 7cfc04
.\" Strictly speaking, it's the filesystem UID... (MTK)
Packit 7cfc04
did not match the owner of the file and the caller was not privileged.
Packit 7cfc04
.TP
Packit 7cfc04
.B EPERM
Packit 7cfc04
The operation was prevented by a file seal; see
Packit 7cfc04
.BR fcntl (2).
Packit 7cfc04
.TP
Packit 7cfc04
.B EROFS
Packit 7cfc04
.I pathname
Packit 7cfc04
refers to a file on a read-only filesystem and write access was
Packit 7cfc04
requested.
Packit 7cfc04
.TP
Packit 7cfc04
.B ETXTBSY
Packit 7cfc04
.I pathname
Packit 7cfc04
refers to an executable image which is currently being executed and
Packit 7cfc04
write access was requested.
Packit 7cfc04
.TP
Packit 7cfc04
.B EWOULDBLOCK
Packit 7cfc04
The
Packit 7cfc04
.B O_NONBLOCK
Packit 7cfc04
flag was specified, and an incompatible lease was held on the file
Packit 7cfc04
(see
Packit 7cfc04
.BR fcntl (2)).
Packit 7cfc04
.PP
Packit 7cfc04
The following additional errors can occur for
Packit 7cfc04
.BR openat ():
Packit 7cfc04
.TP
Packit 7cfc04
.B EBADF
Packit 7cfc04
.I dirfd
Packit 7cfc04
is not a valid file descriptor.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOTDIR
Packit 7cfc04
.I pathname
Packit 7cfc04
is a relative pathname and
Packit 7cfc04
.I dirfd
Packit 7cfc04
is a file descriptor referring to a file other than a directory.
Packit 7cfc04
.SH VERSIONS
Packit 7cfc04
.BR openat ()
Packit 7cfc04
was added to Linux in kernel 2.6.16;
Packit 7cfc04
library support was added to glibc in version 2.4.
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
.BR open (),
Packit 7cfc04
.BR creat ()
Packit 7cfc04
SVr4, 4.3BSD, POSIX.1-2001, POSIX.1-2008.
Packit 7cfc04
.PP
Packit 7cfc04
.BR openat ():
Packit 7cfc04
POSIX.1-2008.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.BR O_DIRECT ,
Packit 7cfc04
.BR O_NOATIME ,
Packit 7cfc04
.BR O_PATH ,
Packit 7cfc04
and
Packit 7cfc04
.BR O_TMPFILE
Packit 7cfc04
flags are Linux-specific.
Packit 7cfc04
One must define
Packit 7cfc04
.B _GNU_SOURCE
Packit 7cfc04
to obtain their definitions.
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.BR O_CLOEXEC ,
Packit 7cfc04
.BR O_DIRECTORY ,
Packit 7cfc04
and
Packit 7cfc04
.BR O_NOFOLLOW
Packit 7cfc04
flags are not specified in POSIX.1-2001,
Packit 7cfc04
but are specified in POSIX.1-2008.
Packit 7cfc04
Since glibc 2.12, one can obtain their definitions by defining either
Packit 7cfc04
.B _POSIX_C_SOURCE
Packit 7cfc04
with a value greater than or equal to 200809L or
Packit 7cfc04
.BR _XOPEN_SOURCE
Packit 7cfc04
with a value greater than or equal to 700.
Packit 7cfc04
In glibc 2.11 and earlier, one obtains the definitions by defining
Packit 7cfc04
.BR _GNU_SOURCE .
Packit 7cfc04
.PP
Packit 7cfc04
As noted in
Packit 7cfc04
.BR feature_test_macros (7),
Packit 7cfc04
feature test macros such as
Packit 7cfc04
.BR _POSIX_C_SOURCE ,
Packit 7cfc04
.BR _XOPEN_SOURCE ,
Packit 7cfc04
and
Packit 7cfc04
.B _GNU_SOURCE
Packit 7cfc04
must be defined before including
Packit 7cfc04
.I any
Packit 7cfc04
header files.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
Under Linux, the
Packit 7cfc04
.B O_NONBLOCK
Packit 7cfc04
flag indicates that one wants to open
Packit 7cfc04
but does not necessarily have the intention to read or write.
Packit 7cfc04
This is typically used to open devices in order to get a file descriptor
Packit 7cfc04
for use with
Packit 7cfc04
.BR ioctl (2).
Packit 7cfc04
.PP
Packit 7cfc04
The (undefined) effect of
Packit 7cfc04
.B O_RDONLY | O_TRUNC
Packit 7cfc04
varies among implementations.
Packit 7cfc04
On many systems the file is actually truncated.
Packit 7cfc04
.\" Linux 2.0, 2.5: truncate
Packit 7cfc04
.\" Solaris 5.7, 5.8: truncate
Packit 7cfc04
.\" Irix 6.5: truncate
Packit 7cfc04
.\" Tru64 5.1B: truncate
Packit 7cfc04
.\" HP-UX 11.22: truncate
Packit 7cfc04
.\" FreeBSD 4.7: truncate
Packit 7cfc04
.PP
Packit 7cfc04
Note that
Packit 7cfc04
.BR open ()
Packit 7cfc04
can open device special files, but
Packit 7cfc04
.BR creat ()
Packit 7cfc04
cannot create them; use
Packit 7cfc04
.BR mknod (2)
Packit 7cfc04
instead.
Packit 7cfc04
.PP
Packit 7cfc04
If the file is newly created, its
Packit 7cfc04
.IR st_atime ,
Packit 7cfc04
.IR st_ctime ,
Packit 7cfc04
.I st_mtime
Packit 7cfc04
fields
Packit 7cfc04
(respectively, time of last access, time of last status change, and
Packit 7cfc04
time of last modification; see
Packit 7cfc04
.BR stat (2))
Packit 7cfc04
are set
Packit 7cfc04
to the current time, and so are the
Packit 7cfc04
.I st_ctime
Packit 7cfc04
and
Packit 7cfc04
.I st_mtime
Packit 7cfc04
fields of the
Packit 7cfc04
parent directory.
Packit 7cfc04
Otherwise, if the file is modified because of the
Packit 7cfc04
.B O_TRUNC
Packit 7cfc04
flag, its
Packit 7cfc04
.I st_ctime
Packit 7cfc04
and
Packit 7cfc04
.I st_mtime
Packit 7cfc04
fields are set to the current time.
Packit 7cfc04
.PP
Packit 7cfc04
The files in the
Packit 7cfc04
.I /proc/[pid]/fd
Packit 7cfc04
directory show the open file descriptors of the process with the PID
Packit 7cfc04
.IR pid .
Packit 7cfc04
The files in the
Packit 7cfc04
.I /proc/[pid]/fdinfo
Packit 7cfc04
directory show even more information about these files descriptors.
Packit 7cfc04
See
Packit 7cfc04
.BR proc (5)
Packit 7cfc04
for further details of both of these directories.
Packit 7cfc04
.\"
Packit 7cfc04
.\"
Packit 7cfc04
.SS Open file descriptions
Packit 7cfc04
The term open file description is the one used by POSIX to refer to the
Packit 7cfc04
entries in the system-wide table of open files.
Packit 7cfc04
In other contexts, this object is
Packit 7cfc04
variously also called an "open file object",
Packit 7cfc04
a "file handle", an "open file table entry",
Packit 7cfc04
or\(emin kernel-developer parlance\(ema
Packit 7cfc04
.IR "struct file" .
Packit 7cfc04
.PP
Packit 7cfc04
When a file descriptor is duplicated (using
Packit 7cfc04
.BR dup (2)
Packit 7cfc04
or similar),
Packit 7cfc04
the duplicate refers to the same open file description
Packit 7cfc04
as the original file descriptor,
Packit 7cfc04
and the two file descriptors consequently share
Packit 7cfc04
the file offset and file status flags.
Packit 7cfc04
Such sharing can also occur between processes:
Packit 7cfc04
a child process created via
Packit 7cfc04
.BR fork (2)
Packit 7cfc04
inherits duplicates of its parent's file descriptors,
Packit 7cfc04
and those duplicates refer to the same open file descriptions.
Packit 7cfc04
.PP
Packit 7cfc04
Each
Packit 7cfc04
.BR open ()
Packit 7cfc04
of a file creates a new open file description;
Packit 7cfc04
thus, there may be multiple open file descriptions
Packit 7cfc04
corresponding to a file inode.
Packit 7cfc04
.PP
Packit 7cfc04
On Linux, one can use the
Packit 7cfc04
.BR kcmp (2)
Packit 7cfc04
.B KCMP_FILE
Packit 7cfc04
operation to test whether two file descriptors
Packit 7cfc04
(in the same process or in two different processes)
Packit 7cfc04
refer to the same open file description.
Packit 7cfc04
.\"
Packit 7cfc04
.\"
Packit 7cfc04
.SS Synchronized I/O
Packit 7cfc04
The POSIX.1-2008 "synchronized I/O" option
Packit 7cfc04
specifies different variants of synchronized I/O,
Packit 7cfc04
and specifies the
Packit 7cfc04
.BR open ()
Packit 7cfc04
flags
Packit 7cfc04
.BR O_SYNC ,
Packit 7cfc04
.BR O_DSYNC ,
Packit 7cfc04
and
Packit 7cfc04
.BR O_RSYNC
Packit 7cfc04
for controlling the behavior.
Packit 7cfc04
Regardless of whether an implementation supports this option,
Packit 7cfc04
it must at least support the use of
Packit 7cfc04
.BR O_SYNC
Packit 7cfc04
for regular files.
Packit 7cfc04
.PP
Packit 7cfc04
Linux implements
Packit 7cfc04
.BR O_SYNC
Packit 7cfc04
and
Packit 7cfc04
.BR O_DSYNC ,
Packit 7cfc04
but not
Packit 7cfc04
.BR O_RSYNC .
Packit 7cfc04
(Somewhat incorrectly, glibc defines
Packit 7cfc04
.BR O_RSYNC
Packit 7cfc04
to have the same value as
Packit 7cfc04
.BR O_SYNC .)
Packit 7cfc04
.PP
Packit 7cfc04
.BR O_SYNC
Packit 7cfc04
provides synchronized I/O
Packit 7cfc04
.I file
Packit 7cfc04
integrity completion,
Packit 7cfc04
meaning write operations will flush data and all associated metadata
Packit 7cfc04
to the underlying hardware.
Packit 7cfc04
.BR O_DSYNC
Packit 7cfc04
provides synchronized I/O
Packit 7cfc04
.I data
Packit 7cfc04
integrity completion,
Packit 7cfc04
meaning write operations will flush data
Packit 7cfc04
to the underlying hardware,
Packit 7cfc04
but will only flush metadata updates that are required
Packit 7cfc04
to allow a subsequent read operation to complete successfully.
Packit 7cfc04
Data integrity completion can reduce the number of disk operations
Packit 7cfc04
that are required for applications that don't need the guarantees
Packit 7cfc04
of file integrity completion.
Packit 7cfc04
.PP
Packit 7cfc04
To understand the difference between the two types of completion,
Packit 7cfc04
consider two pieces of file metadata:
Packit 7cfc04
the file last modification timestamp
Packit 7cfc04
.RI ( st_mtime )
Packit 7cfc04
and the file length.
Packit 7cfc04
All write operations will update the last file modification timestamp,
Packit 7cfc04
but only writes that add data to the end of the
Packit 7cfc04
file will change the file length.
Packit 7cfc04
The last modification timestamp is not needed to ensure that
Packit 7cfc04
a read completes successfully, but the file length is.
Packit 7cfc04
Thus,
Packit 7cfc04
.BR O_DSYNC
Packit 7cfc04
would only guarantee to flush updates to the file length metadata
Packit 7cfc04
(whereas
Packit 7cfc04
.BR O_SYNC
Packit 7cfc04
would also always flush the last modification timestamp metadata).
Packit 7cfc04
.PP
Packit 7cfc04
Before Linux 2.6.33, Linux implemented only the
Packit 7cfc04
.BR O_SYNC
Packit 7cfc04
flag for
Packit 7cfc04
.BR open ().
Packit 7cfc04
However, when that flag was specified,
Packit 7cfc04
most filesystems actually provided the equivalent of synchronized I/O
Packit 7cfc04
.I data
Packit 7cfc04
integrity completion (i.e.,
Packit 7cfc04
.BR O_SYNC
Packit 7cfc04
was actually implemented as the equivalent of
Packit 7cfc04
.BR O_DSYNC ).
Packit 7cfc04
.PP
Packit 7cfc04
Since Linux 2.6.33, proper
Packit 7cfc04
.BR O_SYNC
Packit 7cfc04
support is provided.
Packit 7cfc04
However, to ensure backward binary compatibility,
Packit 7cfc04
.BR O_DSYNC
Packit 7cfc04
was defined with the same value as the historical
Packit 7cfc04
.BR O_SYNC ,
Packit 7cfc04
and
Packit 7cfc04
.BR O_SYNC
Packit 7cfc04
was defined as a new (two-bit) flag value that includes the
Packit 7cfc04
.BR O_DSYNC
Packit 7cfc04
flag value.
Packit 7cfc04
This ensures that applications compiled against
Packit 7cfc04
new headers get at least
Packit 7cfc04
.BR O_DSYNC
Packit 7cfc04
semantics on pre-2.6.33 kernels.
Packit 7cfc04
.\"
Packit 7cfc04
.SS C library/kernel differences
Packit 7cfc04
Since version 2.26,
Packit 7cfc04
the glibc wrapper function for
Packit 7cfc04
.BR open ()
Packit 7cfc04
employs the
Packit 7cfc04
.BR openat ()
Packit 7cfc04
system call, rather than the kernel's
Packit 7cfc04
.BR open ()
Packit 7cfc04
system call.
Packit 7cfc04
For certain architectures, this is also true in glibc versions before 2.26.
Packit 7cfc04
.\"
Packit 7cfc04
.SS NFS
Packit 7cfc04
There are many infelicities in the protocol underlying NFS, affecting
Packit 7cfc04
amongst others
Packit 7cfc04
.BR O_SYNC " and " O_NDELAY .
Packit 7cfc04
.PP
Packit 7cfc04
On NFS filesystems with UID mapping enabled,
Packit 7cfc04
.BR open ()
Packit 7cfc04
may
Packit 7cfc04
return a file descriptor but, for example,
Packit 7cfc04
.BR read (2)
Packit 7cfc04
requests are denied
Packit 7cfc04
with \fBEACCES\fP.
Packit 7cfc04
This is because the client performs
Packit 7cfc04
.BR open ()
Packit 7cfc04
by checking the
Packit 7cfc04
permissions, but UID mapping is performed by the server upon
Packit 7cfc04
read and write requests.
Packit 7cfc04
.\"
Packit 7cfc04
.\"
Packit 7cfc04
.SS FIFOs
Packit 7cfc04
Opening the read or write end of a FIFO blocks until the other
Packit 7cfc04
end is also opened (by another process or thread).
Packit 7cfc04
See
Packit 7cfc04
.BR fifo (7)
Packit 7cfc04
for further details.
Packit 7cfc04
.\"
Packit 7cfc04
.\"
Packit 7cfc04
.SS File access mode
Packit 7cfc04
Unlike the other values that can be specified in
Packit 7cfc04
.IR flags ,
Packit 7cfc04
the
Packit 7cfc04
.I "access mode"
Packit 7cfc04
values
Packit 7cfc04
.BR O_RDONLY ", " O_WRONLY ", and " O_RDWR
Packit 7cfc04
do not specify individual bits.
Packit 7cfc04
Rather, they define the low order two bits of
Packit 7cfc04
.IR flags ,
Packit 7cfc04
and are defined respectively as 0, 1, and 2.
Packit 7cfc04
In other words, the combination
Packit 7cfc04
.B "O_RDONLY | O_WRONLY"
Packit 7cfc04
is a logical error, and certainly does not have the same meaning as
Packit 7cfc04
.BR O_RDWR .
Packit 7cfc04
.PP
Packit 7cfc04
Linux reserves the special, nonstandard access mode 3 (binary 11) in
Packit 7cfc04
.I flags
Packit 7cfc04
to mean:
Packit 7cfc04
check for read and write permission on the file and return a file descriptor
Packit 7cfc04
that can't be used for reading or writing.
Packit 7cfc04
This nonstandard access mode is used by some Linux drivers to return a
Packit 7cfc04
file descriptor that is to be used only for device-specific
Packit 7cfc04
.BR ioctl (2)
Packit 7cfc04
operations.
Packit 7cfc04
.\" See for example util-linux's disk-utils/setfdprm.c
Packit 7cfc04
.\" For some background on access mode 3, see
Packit 7cfc04
.\" http://thread.gmane.org/gmane.linux.kernel/653123
Packit 7cfc04
.\" "[RFC] correct flags to f_mode conversion in __dentry_open"
Packit 7cfc04
.\" LKML, 12 Mar 2008
Packit 7cfc04
.\"
Packit 7cfc04
.\"
Packit 7cfc04
.SS Rationale for openat() and other "directory file descriptor" APIs
Packit 7cfc04
.BR openat ()
Packit 7cfc04
and the other system calls and library functions that take
Packit 7cfc04
a directory file descriptor argument
Packit 7cfc04
(i.e.,
Packit 7cfc04
.BR execveat (2),
Packit 7cfc04
.BR faccessat (2),
Packit 7cfc04
.BR fanotify_mark (2),
Packit 7cfc04
.BR fchmodat (2),
Packit 7cfc04
.BR fchownat (2),
Packit 7cfc04
.BR fstatat (2),
Packit 7cfc04
.BR futimesat (2),
Packit 7cfc04
.BR linkat (2),
Packit 7cfc04
.BR mkdirat (2),
Packit 7cfc04
.BR mknodat (2),
Packit 7cfc04
.BR name_to_handle_at (2),
Packit 7cfc04
.BR readlinkat (2),
Packit 7cfc04
.BR renameat (2),
Packit 7cfc04
.BR statx (2),
Packit 7cfc04
.BR symlinkat (2),
Packit 7cfc04
.BR unlinkat (2),
Packit 7cfc04
.BR utimensat (2),
Packit 7cfc04
.BR mkfifoat (3),
Packit 7cfc04
and
Packit 7cfc04
.BR scandirat (3))
Packit 7cfc04
address two problems with the older interfaces that preceded them.
Packit 7cfc04
Here, the explanation is in terms of the
Packit 7cfc04
.BR openat ()
Packit 7cfc04
call, but the rationale is analogous for the other interfaces.
Packit 7cfc04
.PP
Packit 7cfc04
First,
Packit 7cfc04
.BR openat ()
Packit 7cfc04
allows an application to avoid race conditions that could
Packit 7cfc04
occur when using
Packit 7cfc04
.BR open ()
Packit 7cfc04
to open files in directories other than the current working directory.
Packit 7cfc04
These race conditions result from the fact that some component
Packit 7cfc04
of the directory prefix given to
Packit 7cfc04
.BR open ()
Packit 7cfc04
could be changed in parallel with the call to
Packit 7cfc04
.BR open ().
Packit 7cfc04
Suppose, for example, that we wish to create the file
Packit 7cfc04
.I dir1/dir2/xxx.dep
Packit 7cfc04
if the file
Packit 7cfc04
.I dir1/dir2/xxx
Packit 7cfc04
exists.
Packit 7cfc04
The problem is that between the existence check and the file-creation step,
Packit 7cfc04
.I dir1
Packit 7cfc04
or
Packit 7cfc04
.I dir2
Packit 7cfc04
(which might be symbolic links)
Packit 7cfc04
could be modified to point to a different location.
Packit 7cfc04
Such races can be avoided by
Packit 7cfc04
opening a file descriptor for the target directory,
Packit 7cfc04
and then specifying that file descriptor as the
Packit 7cfc04
.I dirfd
Packit 7cfc04
argument of (say)
Packit 7cfc04
.BR fstatat (2)
Packit 7cfc04
and
Packit 7cfc04
.BR openat ().
Packit 7cfc04
The use of the
Packit 7cfc04
.I dirfd
Packit 7cfc04
file descriptor also has other benefits:
Packit 7cfc04
.IP * 3
Packit 7cfc04
the file descriptor is a stable reference to the directory,
Packit 7cfc04
even if the directory is renamed; and
Packit 7cfc04
.IP *
Packit 7cfc04
the open file descriptor prevents the underlying filesystem from
Packit 7cfc04
being dismounted,
Packit 7cfc04
just as when a process has a current working directory on a filesystem.
Packit 7cfc04
.PP
Packit 7cfc04
Second,
Packit 7cfc04
.BR openat ()
Packit 7cfc04
allows the implementation of a per-thread "current working
Packit 7cfc04
directory", via file descriptor(s) maintained by the application.
Packit 7cfc04
(This functionality can also be obtained by tricks based
Packit 7cfc04
on the use of
Packit 7cfc04
.IR /proc/self/fd/ dirfd,
Packit 7cfc04
but less efficiently.)
Packit 7cfc04
.\"
Packit 7cfc04
.\"
Packit 7cfc04
.SS O_DIRECT
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.B O_DIRECT
Packit 7cfc04
flag may impose alignment restrictions on the length and address
Packit 7cfc04
of user-space buffers and the file offset of I/Os.
Packit 7cfc04
In Linux alignment
Packit 7cfc04
restrictions vary by filesystem and kernel version and might be
Packit 7cfc04
absent entirely.
Packit 7cfc04
However there is currently no filesystem\-independent
Packit 7cfc04
interface for an application to discover these restrictions for a given
Packit 7cfc04
file or filesystem.
Packit 7cfc04
Some filesystems provide their own interfaces
Packit 7cfc04
for doing so, for example the
Packit 7cfc04
.B XFS_IOC_DIOINFO
Packit 7cfc04
operation in
Packit 7cfc04
.BR xfsctl (3).
Packit 7cfc04
.PP
Packit 7cfc04
Under Linux 2.4, transfer sizes, and the alignment of the user buffer
Packit 7cfc04
and the file offset must all be multiples of the logical block size
Packit 7cfc04
of the filesystem.
Packit 7cfc04
Since Linux 2.6.0, alignment to the logical block size of the
Packit 7cfc04
underlying storage (typically 512 bytes) suffices.
Packit 7cfc04
The logical block size can be determined using the
Packit 7cfc04
.BR ioctl (2)
Packit 7cfc04
.B BLKSSZGET
Packit 7cfc04
operation or from the shell using the command:
Packit 7cfc04
.PP
Packit 7cfc04
.EX
Packit 7cfc04
    blockdev \-\-getss
Packit 7cfc04
.EE
Packit 7cfc04
.PP
Packit 7cfc04
.B O_DIRECT
Packit 7cfc04
I/Os should never be run concurrently with the
Packit 7cfc04
.BR fork (2)
Packit 7cfc04
system call,
Packit 7cfc04
if the memory buffer is a private mapping
Packit 7cfc04
(i.e., any mapping created with the
Packit 7cfc04
.BR mmap (2)
Packit 7cfc04
.BR MAP_PRIVATE
Packit 7cfc04
flag;
Packit 7cfc04
this includes memory allocated on the heap and statically allocated buffers).
Packit 7cfc04
Any such I/Os, whether submitted via an asynchronous I/O interface or from
Packit 7cfc04
another thread in the process,
Packit 7cfc04
should be completed before
Packit 7cfc04
.BR fork (2)
Packit 7cfc04
is called.
Packit 7cfc04
Failure to do so can result in data corruption and undefined behavior in
Packit 7cfc04
parent and child processes.
Packit 7cfc04
This restriction does not apply when the memory buffer for the
Packit 7cfc04
.B O_DIRECT
Packit 7cfc04
I/Os was created using
Packit 7cfc04
.BR shmat (2)
Packit 7cfc04
or
Packit 7cfc04
.BR mmap (2)
Packit 7cfc04
with the
Packit 7cfc04
.B MAP_SHARED
Packit 7cfc04
flag.
Packit 7cfc04
Nor does this restriction apply when the memory buffer has been advised as
Packit 7cfc04
.B MADV_DONTFORK
Packit 7cfc04
with
Packit 7cfc04
.BR madvise (2),
Packit 7cfc04
ensuring that it will not be available
Packit 7cfc04
to the child after
Packit 7cfc04
.BR fork (2).
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.B O_DIRECT
Packit 7cfc04
flag was introduced in SGI IRIX, where it has alignment
Packit 7cfc04
restrictions similar to those of Linux 2.4.
Packit 7cfc04
IRIX has also a
Packit 7cfc04
.BR fcntl (2)
Packit 7cfc04
call to query appropriate alignments, and sizes.
Packit 7cfc04
FreeBSD 4.x introduced
Packit 7cfc04
a flag of the same name, but without alignment restrictions.
Packit 7cfc04
.PP
Packit 7cfc04
.B O_DIRECT
Packit 7cfc04
support was added under Linux in kernel version 2.4.10.
Packit 7cfc04
Older Linux kernels simply ignore this flag.
Packit 7cfc04
Some filesystems may not implement the flag, in which case
Packit 7cfc04
.BR open ()
Packit 7cfc04
fails with the error
Packit 7cfc04
.B EINVAL
Packit 7cfc04
if it is used.
Packit 7cfc04
.PP
Packit 7cfc04
Applications should avoid mixing
Packit 7cfc04
.B O_DIRECT
Packit 7cfc04
and normal I/O to the same file,
Packit 7cfc04
and especially to overlapping byte regions in the same file.
Packit 7cfc04
Even when the filesystem correctly handles the coherency issues in
Packit 7cfc04
this situation, overall I/O throughput is likely to be slower than
Packit 7cfc04
using either mode alone.
Packit 7cfc04
Likewise, applications should avoid mixing
Packit 7cfc04
.BR mmap (2)
Packit 7cfc04
of files with direct I/O to the same files.
Packit 7cfc04
.PP
Packit 7cfc04
The behavior of
Packit 7cfc04
.B O_DIRECT
Packit 7cfc04
with NFS will differ from local filesystems.
Packit 7cfc04
Older kernels, or
Packit 7cfc04
kernels configured in certain ways, may not support this combination.
Packit 7cfc04
The NFS protocol does not support passing the flag to the server, so
Packit 7cfc04
.B O_DIRECT
Packit 7cfc04
I/O will bypass the page cache only on the client; the server may
Packit 7cfc04
still cache the I/O.
Packit 7cfc04
The client asks the server to make the I/O
Packit 7cfc04
synchronous to preserve the synchronous semantics of
Packit 7cfc04
.BR O_DIRECT .
Packit 7cfc04
Some servers will perform poorly under these circumstances, especially
Packit 7cfc04
if the I/O size is small.
Packit 7cfc04
Some servers may also be configured to
Packit 7cfc04
lie to clients about the I/O having reached stable storage; this
Packit 7cfc04
will avoid the performance penalty at some risk to data integrity
Packit 7cfc04
in the event of server power failure.
Packit 7cfc04
The Linux NFS client places no alignment restrictions on
Packit 7cfc04
.B O_DIRECT
Packit 7cfc04
I/O.
Packit 7cfc04
.PP
Packit 7cfc04
In summary,
Packit 7cfc04
.B O_DIRECT
Packit 7cfc04
is a potentially powerful tool that should be used with caution.
Packit 7cfc04
It is recommended that applications treat use of
Packit 7cfc04
.B O_DIRECT
Packit 7cfc04
as a performance option which is disabled by default.
Packit 7cfc04
.PP
Packit 7cfc04
.RS
Packit 7cfc04
"The thing that has always disturbed me about O_DIRECT is that the whole
Packit 7cfc04
interface is just stupid, and was probably designed by a deranged monkey
Packit 7cfc04
on some serious mind-controlling substances."\(emLinus
Packit 7cfc04
.RE
Packit 7cfc04
.SH BUGS
Packit 7cfc04
Currently, it is not possible to enable signal-driven
Packit 7cfc04
I/O by specifying
Packit 7cfc04
.B O_ASYNC
Packit 7cfc04
when calling
Packit 7cfc04
.BR open ();
Packit 7cfc04
use
Packit 7cfc04
.BR fcntl (2)
Packit 7cfc04
to enable this flag.
Packit 7cfc04
.\" FIXME . Check bugzilla report on open(O_ASYNC)
Packit 7cfc04
.\" See http://bugzilla.kernel.org/show_bug.cgi?id=5993
Packit 7cfc04
.PP
Packit 7cfc04
One must check for two different error codes,
Packit 7cfc04
.B EISDIR
Packit 7cfc04
and
Packit 7cfc04
.BR ENOENT ,
Packit 7cfc04
when trying to determine whether the kernel supports
Packit 7cfc04
.B O_TMPFILE
Packit 7cfc04
functionality.
Packit 7cfc04
.PP
Packit 7cfc04
When both
Packit 7cfc04
.B O_CREAT
Packit 7cfc04
and
Packit 7cfc04
.B O_DIRECTORY
Packit 7cfc04
are specified in
Packit 7cfc04
.IR flags
Packit 7cfc04
and the file specified by
Packit 7cfc04
.I pathname
Packit 7cfc04
does not exist,
Packit 7cfc04
.BR open ()
Packit 7cfc04
will create a regular file (i.e.,
Packit 7cfc04
.B O_DIRECTORY
Packit 7cfc04
is ignored).
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR chmod (2),
Packit 7cfc04
.BR chown (2),
Packit 7cfc04
.BR close (2),
Packit 7cfc04
.BR dup (2),
Packit 7cfc04
.BR fcntl (2),
Packit 7cfc04
.BR link (2),
Packit 7cfc04
.BR lseek (2),
Packit 7cfc04
.BR mknod (2),
Packit 7cfc04
.BR mmap (2),
Packit 7cfc04
.BR mount (2),
Packit 7cfc04
.BR open_by_handle_at (2),
Packit 7cfc04
.BR read (2),
Packit 7cfc04
.BR socket (2),
Packit 7cfc04
.BR stat (2),
Packit 7cfc04
.BR umask (2),
Packit 7cfc04
.BR unlink (2),
Packit 7cfc04
.BR write (2),
Packit 7cfc04
.BR fopen (3),
Packit 7cfc04
.BR acl (5),
Packit 7cfc04
.BR fifo (7),
Packit 7cfc04
.BR inode (7),
Packit 7cfc04
.BR path_resolution (7),
Packit 7cfc04
.BR symlink (7)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.