OPEN(2) Linux Programmer's Manual OPEN(2)
NAME
open(2,3,n), creat - open(2,3,n) and possibly create a file(1,n) or device
SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(2,3,n)(const char *pathname, int flags);
int open(2,3,n)(const char *pathname, int flags, mode_t mode);
int creat(const char *pathname, mode_t mode);
DESCRIPTION
The open(2,3,n)() system call is used to convert a pathname into a file(1,n)
descriptor (a small, non-negative integer for use in(1,8) subsequent I/O as
with read(2,n,1 builtins), write(1,2), etc.). When the call is successful, the file(1,n)
descriptor returned will be the lowest file(1,n) descriptor not currently
open(2,3,n) for the process. This call creates a new open(2,3,n) file(1,n), not shared
with any other process. (But shared open(2,3,n) files may arise via the
fork(2) system call.) The new file(1,n) descriptor is set(7,n,1 builtins) to remain open(2,3,n)
across exec(3,n,1 builtins) functions (see fcntl(2)). The file(1,n) offset is set(7,n,1 builtins) to the
beginning of the file.
The parameter flags is one of O_RDONLY, O_WRONLY or O_RDWR which
request opening the file(1,n) read-only, write-only or read(2,n,1 builtins)/write(1,2), respec-
tively, bitwise-or'd with zero or more of the following:
O_CREAT
If the file(1,n) does not exist it will be created. The owner (user
ID) of the file(1,n) is set(7,n,1 builtins) to the effective user ID of the process.
The group ownership (group ID) is set(7,n,1 builtins) either to the effective
group ID of the process or to the group ID of the parent direc-
tory (depending on filesystem type and mount(2,8) options, and the
mode of the parent directory, see, e.g., the mount(2,8) options bsd-
groups and sysvgroups of the ext2 filesystem, as described in(1,8)
mount(2,8)(8)).
O_EXCL When used with O_CREAT, if(3,n) the file(1,n) already exists it is an
error(8,n) and the open(2,3,n) will fail. In this context, a symbolic link(1,2)
exists, regardless of where its points to. O_EXCL is broken on
NFS file(1,n) systems, programs which rely on it for performing lock-
ing tasks will contain a race condition. The solution for per-
forming atomic file(1,n) locking using a lockfile is to create a
unique file(1,n) on the same fs (e.g., incorporating hostname and
pid), use link(1,2)(2) to make a link(1,2) to the lockfile. If link(1,2)()
returns 0, the lock is successful. Otherwise, use stat(1,2)(2) on
the unique file(1,n) to check if(3,n) its link(1,2) count has increased to 2,
in(1,8) which case the lock is also successful.
O_NOCTTY
If pathname refers to a terminal device -- see tty(1,4)(4) -- it will
not become the process's controlling terminal even if(3,n) the
process does not have one.
O_TRUNC
If the file(1,n) already exists and is a regular file(1,n) and the open(2,3,n)
mode allows writing (i.e., is O_RDWR or O_WRONLY) it will be
truncated to length 0. If the file(1,n) is a FIFO or terminal device
file(1,n), the O_TRUNC flag is ignored. Otherwise the effect of
O_TRUNC is unspecified.
O_APPEND
The file(1,n) is opened in(1,8) append mode. Before each write(1,2), the file(1,n)
pointer is positioned at the end of the file(1,n), as if(3,n) with lseek.
O_APPEND may lead to corrupted files on NFS file(1,n) systems if(3,n) more
than one process appends data to a file(1,n) at once. This is
because NFS does not support appending to a file(1,n), so the client
kernel has to simulate it, which can't be done without a race
condition.
O_NONBLOCK or O_NDELAY
When possible, the file(1,n) is opened in(1,8) non-blocking mode. Neither
the open(2,3,n) nor any subsequent operations on the file(1,n) descriptor
which is returned will cause the calling process to wait. For
the handling of FIFOs (named(5,8) pipes), see also fifo(4). This
mode need not have any effect on files other than FIFOs.
O_SYNC The file(1,n) is opened for synchronous I/O. Any write(1,2)s on the
resulting file(1,n) descriptor will block the calling process until
the data has been physically written to the underlying hardware.
See RESTRICTIONS below, though.
O_NOFOLLOW
If pathname is a symbolic link(1,2), then the open(2,3,n) fails. This is a
FreeBSD extension, which was added to Linux in(1,8) version(1,3,5) 2.1.126.
Symbolic links in(1,8) earlier components of the pathname will still
be followed. The headers from glibc 2.0.100 and later include a
definition of this flag; kernels before 2.1.126 will ignore it
if(3,n) used.
O_DIRECTORY
If pathname is not a directory, cause the open(2,3,n) to fail. This
flag is Linux-specific, and was added in(1,8) kernel version(1,3,5) 2.1.126,
to avoid denial-of-service problems if(3,n) opendir(3) is called on a
FIFO or tape device, but should not be used outside of the
implementation of opendir.
O_DIRECT
Try to minimize cache effects of the I/O to and from this file.
In general this will degrade performance, but it is useful in(1,8)
special situations, such as when applications do their own
caching. File I/O is done directly to/from user space buffers.
The I/O is synchronous, i.e., at the completion of the read(2,n,1 builtins)(2)
or write(1,2)(2) system call, data is guaranteed to have been trans-
ferred. Under Linux 2.4 transfer sizes, and the alignment of
user buffer and file(1,n) offset must all be multiples of the logical
block size of the file(1,n) system. Under Linux 2.6 alignment to
512-byte boundaries suffices.
A semantically similar interface for block devices is described
in(1,8) raw(3x,7,8,3x cbreak)(8).
O_ASYNC
Generate a signal(2,7) (SIGIO by default, but this can be changed via
fcntl(2)) when input or output becomes possible on this file(1,n)
descriptor. This feature is only available for terminals,
pseudo-terminals, and sockets. See fcntl(2) for further details.
O_LARGEFILE
(LFS) Allow files whose sizes cannot be represented in(1,8) an off_t
(but can be represented in(1,8) an off64_t) to be opened.
Some of these optional flags can be altered using fcntl after the file(1,n)
has been opened.
The argument mode specifies the permissions to use in(1,8) case a new file(1,n)
is created. It is modified by the process's umask in(1,8) the usual way: the
permissions of the created file(1,n) are (mode & ~umask). Note that this
mode only applies to future accesses of the newly created file(1,n); the
open(2,3,n) call that creates a read-only file(1,n) may well return a read(2,n,1 builtins)/write(1,2)
file(1,n) descriptor.
The following symbolic constants are provided for mode:
S_IRWXU
00700 user (file(1,n) owner) has read(2,n,1 builtins), write(1,2) and execute permission
S_IRUSR (S_IREAD)
00400 user has read(2,n,1 builtins) permission
S_IWUSR (S_IWRITE)
00200 user has write(1,2) permission
S_IXUSR (S_IEXEC)
00100 user has execute permission
S_IRWXG
00070 group has read(2,n,1 builtins), write(1,2) and execute permission
S_IRGRP
00040 group has read(2,n,1 builtins) permission
S_IWGRP
00020 group has write(1,2) permission
S_IXGRP
00010 group has execute permission
S_IRWXO
00007 others have read(2,n,1 builtins), write(1,2) and execute permission
S_IROTH
00004 others have read(2,n,1 builtins) permission
S_IWOTH
00002 others have write(1,2) permisson
S_IXOTH
00001 others have execute permission
mode must be specified when O_CREAT is in(1,8) the flags, and is ignored
otherwise.
creat is equivalent to open(2,3,n) with flags equal to
O_CREAT|O_WRONLY|O_TRUNC.
RETURN VALUE
open(2,3,n) and creat return the new file(1,n) descriptor, or -1 if(3,n) an error(8,n)
occurred (in(1,8) which case, errno is set(7,n,1 builtins) appropriately). Note that open(2,3,n)
can open(2,3,n) device special files, but creat cannot create them - use
mknod(1,2)(2) instead.
On NFS file(1,n) systems with UID mapping enabled, open(2,3,n) may return a file(1,n)
descriptor but e.g. read(2,n,1 builtins)(2) requests are denied with EACCES. This is
because the client performs open(2,3,n) by checking the permissions, but UID
mapping is performed by the server upon read(2,n,1 builtins) and write(1,2) requests.
If the file(1,n) is newly created, its atime, ctime, mtime fields are set(7,n,1 builtins) to
the current time(1,2,n), and so are the ctime and mtime fields of the parent
directory. Otherwise, if(3,n) the file(1,n) is modified because of the O_TRUNC
flag, its ctime and mtime fields are set(7,n,1 builtins) to the current time.
ERRORS
EACCES The requested access(2,5) to the file(1,n) is not allowed, or search per-
mission is denied for one of the directories in(1,8) the path prefix
of pathname, or the file(1,n) did not exist yet and write(1,2) access(2,5) to
the parent directory is not allowed. (See also path_resolu-
tion(2).)
EEXIST pathname already exists and O_CREAT and O_EXCL were used.
EFAULT pathname points outside your accessible address space.
EISDIR pathname refers to a directory and the access(2,5) requested involved
writing (that is, O_WRONLY or O_RDWR is set(7,n,1 builtins)).
ELOOP Too many symbolic links were encountered in(1,8) resolving pathname,
or O_NOFOLLOW was specified but pathname was a symbolic link.
EMFILE The process already has the maximum number of files open.
ENAMETOOLONG
pathname was too long.
ENFILE The system limit on the total number of open(2,3,n) files has been
reached.
ENODEV pathname refers to a device special file(1,n) and no corresponding
device exists. (This is a Linux kernel bug - in(1,8) this situation
ENXIO must be returned.)
ENOENT O_CREAT is not set(7,n,1 builtins) and the named(5,8) file(1,n) does not exist. Or, a
directory component in(1,8) pathname does not exist or is a dangling
symbolic link.
ENOMEM Insufficient kernel memory was available.
ENOSPC pathname was to be created but the device containing pathname
has no room for the new file.
ENOTDIR
A component used as a directory in(1,8) pathname is not, in(1,8) fact, a
directory, or O_DIRECTORY was specified and pathname was not a
directory.
ENXIO O_NONBLOCK | O_WRONLY is set(7,n,1 builtins), the named(5,8) file(1,n) is a FIFO and no
process has the file(1,n) open(2,3,n) for reading. Or, the file(1,n) is a device
special file(1,n) and no corresponding device exists.
EOVERFLOW
pathname refers to a regular file(1,n), too large to be opened - see
O_LARGEFILE above.
EROFS pathname refers to a file(1,n) on a read-only filesystem and write(1,2)
access(2,5) was requested.
ETXTBSY
pathname refers to an executable image which is currently being
executed and write(1,2) access(2,5) was requested.
NOTE
Under Linux, the O_NONBLOCK flag indicates that one wants to open(2,3,n) but
does not necessarily have the intention to read(2,n,1 builtins) or write. This is typ-
ically used to open(2,3,n) devices in(1,8) order to get a file(1,n) descriptor for use
with ioctl(2).
CONFORMING TO
SVr4, SVID, POSIX, X/OPEN, BSD 4.3. The O_NOFOLLOW and O_DIRECTORY
flags are Linux-specific. One may have to define the _GNU_SOURCE macro
to get their definitions.
The (undefined) effect of O_RDONLY | O_TRUNC various among implementa-
tions. On many systems the file(1,n) is actually truncated.
The O_DIRECT flag was introduced in(1,8) SGI IRIX, where it has alignment
restrictions similar to those of Linux 2.4. IRIX has also a fcntl(2)
call to query appropriate alignments, and sizes. FreeBSD 4.x intro-
duced a flag of same name, but without alignment restrictions. Support
was added under Linux in(1,8) kernel version(1,3,5) 2.4.10. Older Linux kernels
simply ignore this flag.
BUGS
"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." -- Linus
RESTRICTIONS
There are many infelicities in(1,8) the protocol underlying NFS, affecting
amongst others O_SYNC and O_NDELAY.
POSIX provides for three different variants of synchronised I/O, corre-
sponding to the flags O_SYNC, O_DSYNC and O_RSYNC. Currently (2.1.130)
these are all synonymous under Linux.
SEE ALSO
close(2,7,n)(2), fcntl(2), link(1,2)(2), mknod(1,2)(2), mount(2,8)(2), path_resolution(2),
read(2,n,1 builtins)(2), socket(2,7,n)(2), stat(1,2)(2), umask(2), unlink(1,2)(2), write(1,2)(2), fopen(3),
fifo(4)
Linux 2.6.7 2004-06-23 OPEN(2)