.\" Hey Emacs! This file is -*- nroff -*- source. .\" .\" This manpage is Copyright (C) 1992 Drew Eckhardt; .\" 1993 Michael Haardt, Ian Jackson. .\" .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" .\" Modified 1993-07-21 by Rik Faith .\" Modified 1994-08-21 by Michael Haardt .\" Modified 1996-04-13 by Andries Brouwer .\" Modified 1996-05-13 by Thomas Koenig .\" Modified 1996-12-20 by Michael Haardt .\" Modified 1999-02-19 by Andries Brouwer .\" Modified 1998-11-28 by Joseph S. Myers .\" Modified 1999-06-03 by Michael Haardt .\" Modified 2002-05-07 by Michael Kerrisk .\" Modified 2004-06-23 by Michael Kerrisk .\" 2004-12-08, mtk, reordered flags list alphabetically .\" 2004-12-08, Martin Pool (& mtk), added O_NOATIME .\" .TH OPEN 2 2005-06-22 "Linux 2.6.12" "Linux Programmer's Manual" .SH NAME open, creat \- open and possibly create a file or device .SH SYNOPSIS .nf .B #include .B #include .B #include .sp .BI "int open(const char *" pathname ", int " flags ); .BI "int open(const char *" pathname ", int " flags ", mode_t " mode ); .BI "int creat(const char *" pathname ", mode_t " mode ); .fi .SH DESCRIPTION Given a .IR pathname for a file, .BR open () returns a file descriptor, a small, non-negative integer for use in subsequent system calls .RB ( read "(2), " write "(2), " lseek "(2), " fcntl "(2), etc.)." The file descriptor returned by a successful call will be the lowest-numbered file descriptor not currently open for the process. .PP The new file descriptor is set to remain open across an .BR execve (2) (i.e., the .B FD_CLOEXEC file descriptor flag described in .BR fcntl (2) is initially disabled). The file offset is set to the beginning of the file (see .BR lseek (2)). .PP A call to .BR open () creates a new .IR "open file description" , an entry in the system-wide table of open files. This entry records the file offset and the file status flags (modifiable via the .BR fcntl () .B F_SETFL operation). A file descriptor is a reference to one of these entries; this reference is unaffected if .I pathname is subsequently removed or modified to refer to a different file. The new open file description is initially not shared with any other process, but sharing may arise via .BR fork (2). .PP The parameter .I flags must include one of the following .IR "access modes" : .BR O_RDONLY ", " O_WRONLY ", or " O_RDWR. These request opening the file read-only, write-only, or read/write, respectively. In addition, zero or more file creation flags and file status flags can be .RI bitwise- or 'd in .IR flags . The .I file creation flags are .BR O_CREAT ", " O_EXCL ", " O_NOCTTY ", and " O_TRUNC . The .I file status flags are all of the remaining flags listed below. The distinction between these two groups of flags is that the file status flags can be retrieved and (in some cases) modified using .BR fcntl (2). The full list of file creation flags and file status flags is as follows: .TP .B O_APPEND The file is opened in append mode. Before each .BR write (), the file offset is positioned at the end of the file, as if with .BR lseek (). .B O_APPEND may lead to corrupted files on NFS file systems if more than one process appends data to a file at once. This is because NFS does not support appending to a file, so the client kernel has to simulate it, which can't be done without a race condition. .TP .B O_ASYNC Enable signal-driven I/O: generate a signal (SIGIO by default, but this can be changed via .BR fcntl (2)) when input or output becomes possible on this file descriptor. This feature is only available for terminals, pseudo-terminals, sockets, and (since Linux 2.6) pipes and FIFOs. See .BR fcntl (2) for further details. .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 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, e.g., the mount options .I bsdgroups and .I sysvgroups of the ext2 filesystem, as described in .BR mount (8)). .TP .B 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 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 a .BR read (2) or .BR write (2), data is guaranteed to have been transferred. Under Linux 2.4 transfer sizes, and the alignment of user buffer and file offset must all be multiples of the logical block size of the file system. Under Linux 2.6 alignment to 512-byte boundaries suffices. .\" Alignment should satisfy requirements for the underlying device .\" There may be coherency problems. .sp A semantically similar (but deprecated) interface for block devices is described in .BR raw (8). .TP .B O_DIRECTORY If \fIpathname\fR is not a directory, cause the open to fail. .\" But see the following and its replies: .\" http://marc.theaimsgroup.com/?t=112748702800001&r=1&w=2 .\" [PATCH] open: O_DIRECTORY and O_CREAT together should fail .\" O_DIRECTORY | O_CREAT causes O_DIRECTORY to be ignored. This flag is Linux-specific, and was added in kernel version 2.1.126, to avoid denial-of-service problems if \fBopendir\fR(3) is called on a FIFO or tape device, but should not be used outside of the implementation of \fBopendir\fR. .TP .B O_EXCL When used with .BR O_CREAT , if the file already exists it is an error and the .BR open () will fail. In this context, a symbolic link exists, regardless of where it points to. .B O_EXCL is broken on NFS file systems; programs which rely on it for performing locking tasks will contain a race condition. The solution for performing atomic file locking using a lockfile is to create a unique file on the same file system (e.g., incorporating hostname and pid), use .BR link (2) to make a link to the lockfile. If \fBlink\fP() returns 0, the lock is successful. Otherwise, use .BR stat (2) on the unique file to check if its link count has increased to 2, in which case the lock is also successful. .TP .B O_LARGEFILE (LFS) Allow files whose sizes cannot be represented in an .I off_t (but can be represented in an .IR off64_t ) to be opened. The .B _LARGEFILE64_SOURCE macro must be defined in order to obtain this definition. Setting the .B _FILE_OFFSET_BITS feature test macro to 64 (rather than using .BR O_LARGEFILE ) is the preferred method of obtaining method of accessing large files on 32-bit systems (see .BR feature_test_macros (7)). .TP .B O_NOATIME (Since Linux 2.6.8) Do not update the file last access time (st_atime in the inode) when the file is .BR read (2). 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. One example is NFS, where the server maintains the access time. .\" FIXME? The O_NOATIME flag also affects the treatment of st_atime .\" by mmap() and readdir(2), MTK, Dec 04. .TP .B O_NOCTTY If .I pathname refers to a terminal device \(em see .BR tty (4) \(em it will not become the process's controlling terminal even if the process does not have one. .TP .B O_NOFOLLOW If \fIpathname\fR is a symbolic link, then the open fails. This is a FreeBSD extension, which was added to Linux in version 2.1.126. Symbolic links in earlier components of the pathname will still be followed. .\" The headers from glibc 2.0.100 and later include a .\" definition of this flag; \fIkernels before 2.1.126 will ignore it if .\" used\fR. .TP .BR O_NONBLOCK " or " O_NDELAY When possible, the file is opened in non-blocking mode. Neither the .BR open () nor any subsequent operations on the file descriptor which is returned will cause the calling process to wait. For the handling of FIFOs (named pipes), see also .BR fifo (7). For a discussion of the effect of .BR O_NONBLOCK in conjunction with mandatory file locks and with file leases, see .BR fcntl (2). .TP .B O_SYNC The file is opened for synchronous I/O. Any .BR write ()s on the resulting file descriptor will block the calling process until the data has been physically written to the underlying hardware. .IR "But see RESTRICTIONS below" . .TP .B O_TRUNC If the file already exists and is a regular file and the open mode allows writing (i.e., is O_RDWR or O_WRONLY) it will be truncated to length 0. If the file is a FIFO or terminal device file, the O_TRUNC flag is ignored. Otherwise the effect of O_TRUNC is unspecified. .PP Some of these optional flags can be altered using .BR fcntl () after the file has been opened. The argument .I mode specifies the permissions to use in case a new file is created. It is modified by the process's .BR umask in the usual way: the permissions of the created file are .BR "(mode & ~umask)" . Note that this mode only applies to future accesses of the newly created file; the .BR open () call that creates a read-only file may well return a read/write file descriptor. .PP The following symbolic constants are provided for .IR mode : .TP .B S_IRWXU 00700 user (file owner) has read, write and execute permission .TP .B S_IRUSR 00400 user has read permission .TP .B S_IWUSR 00200 user has write permission .TP .B S_IXUSR 00100 user has execute permission .TP .B S_IRWXG 00070 group has read, write and execute permission .TP .B S_IRGRP 00040 group has read permission .TP .B S_IWGRP 00020 group has write permission .TP .B S_IXGRP 00010 group has execute permission .TP .B S_IRWXO 00007 others have read, write and execute permission .TP .B S_IROTH 00004 others have read permission .TP .B S_IWOTH 00002 others have write permission .TP .B S_IXOTH 00001 others have execute permission .PP .I mode must be specified when .B O_CREAT is in the .IR flags , and is ignored otherwise. .BR creat () is equivalent to .BR open () with .I flags equal to .BR O_CREAT|O_WRONLY|O_TRUNC . .SH "RETURN VALUE" .BR open () and .BR creat () return the new file descriptor, or \-1 if an error occurred (in which case, .I errno is set appropriately). .SH NOTES Note that .BR open () can open device special files, but .BR creat () cannot create them; use .BR mknod (2) instead. .LP On NFS file systems with UID mapping enabled, \fBopen\fP() may return a file descriptor but e.g. \fBread\fP(2) requests are denied with \fBEACCES\fP. This is because the client performs \fBopen\fP() by checking the permissions, but UID mapping is performed by the server upon read and write requests. If the file is newly created, its st_atime, st_ctime, st_mtime fields (respectively, time of last access, time of last status change, and time of last modification; see .BR stat (2)) are set to the current time, and so are the st_ctime and st_mtime fields of the parent directory. Otherwise, if the file is modified because of the O_TRUNC flag, its st_ctime and st_mtime fields are set to the current time. .SH ERRORS .TP .B EACCES The requested access to the file is not allowed, or search permission is denied for one of the directories in the path prefix of .IR pathname , or the file did not exist yet and write access to the parent directory is not allowed. (See also .BR path_resolution (2).) .TP .B EEXIST .I pathname already exists and .BR O_CREAT " and " O_EXCL were used. .TP .B EFAULT .IR pathname points outside your accessible address space. .TP .B EFBIG .I pathname refers to a regular file, too large to be opened; see O_LARGEFILE above. (POSIX.1-2001 specifies the error .B EOVERFLOW for this case.) .\" FIXME Maybe this deviation from the standard will get repaired. .\" See http://bugzilla.kernel.org/show_bug.cgi?id=7253 .\" "Open of a large file on 32-bit fails with EFBIG, should be EOVERFLOW" .\" Reported 2006-10-03 .TP .B EISDIR .I pathname refers to a directory and the access requested involved writing (that is, .B O_WRONLY or .B O_RDWR is set). .TP .B ELOOP Too many symbolic links were encountered in resolving .IR pathname , or \fBO_NOFOLLOW\fR was specified but .I pathname was a symbolic link. .TP .B EMFILE The process already has the maximum number of files open. .TP .B ENAMETOOLONG .IR pathname was too long. .TP .B ENFILE The system limit on the total number of open files has been reached. .TP .B ENODEV .I pathname refers to a device special file and no corresponding device exists. (This is a Linux kernel bug; in this situation ENXIO must be returned.) .TP .B ENOENT O_CREAT is not set and the named file does not exist. Or, a directory component in .I pathname does not exist or is a dangling symbolic link. .TP .B ENOMEM Insufficient kernel memory was available. .TP .B ENOSPC .I pathname was to be created but the device containing .I pathname has no room for the new file. .TP .B ENOTDIR A component used as a directory in .I pathname is not, in fact, a directory, or \fBO_DIRECTORY\fR was specified and .I pathname was not a directory. .TP .B ENXIO O_NONBLOCK | O_WRONLY is set, the named file is a FIFO and no process has the file open for reading. Or, the file is a device special file and no corresponding device exists. .TP .B EPERM The .B O_NOATIME flag was specified, but the effective user ID of the caller .\" Strictly speaking, it's the file system UID... (MTK) did not match the owner of the file and the caller was not privileged .RB ( CAP_FOWNER ). .TP .B EROFS .I pathname refers to a file on a read-only filesystem and write access was requested. .TP .B ETXTBSY .I pathname refers to an executable image which is currently being executed and write access was requested. .TP .B EWOULDBLOCK The .B O_NONBLOCK flag was specified, and an incompatible lease was held on the file (see .BR fcntl (2)). .SH NOTE Under Linux, the O_NONBLOCK flag indicates that 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 use with .BR ioctl (2). .SH "CONFORMING TO" SVr4, 4.3BSD, POSIX.1-2001. The .BR O_NOATIME , .BR O_NOFOLLOW , and .B O_DIRECTORY flags are Linux specific. One may have to define the .B _GNU_SOURCE macro to get their definitions. .LP The (undefined) effect of .B O_RDONLY | O_TRUNC varies among implementations. On many systems the file is actually truncated. .\" Linux 2.0, 2.5: truncate .\" Solaris 5.7, 5.8: truncate .\" Irix 6.5: truncate .\" Tru64 5.1B: truncate .\" HP-UX 11.22: truncate .\" FreeBSD 4.7: truncate .LP The .B O_DIRECT flag was introduced in 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 introduced a flag of same name, but without alignment restrictions. Support was added under Linux in kernel version 2.4.10. Older Linux kernels simply ignore this flag. One may have to define the .B _GNU_SOURCE macro to get its definition. .SH 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." \(em Linus Currently, it is not possible to enable signal-driven I/O by specifying .B O_ASYNC when calling .BR open (); use .BR fcntl (2) to enable this flag. .\" FIXME Check bugzilla report on open(O_ASYNC) .\" See http://bugzilla.kernel.org/show_bug.cgi?id=5993 .SH RESTRICTIONS There are many infelicities in the protocol underlying NFS, affecting amongst others .BR O_SYNC " and " O_NDELAY . POSIX provides for three different variants of synchronised I/O, corresponding to the flags \fBO_SYNC\fR, \fBO_DSYNC\fR and \fBO_RSYNC\fR. Currently (2.1.130) these are all synonymous under Linux. .SH "SEE ALSO" .BR close (2), .BR dup (2), .BR fcntl (2), .BR link (2), .BR lseek (2), .BR mknod (2), .BR mount (2), .BR mmap (2), .BR openat (2), .BR path_resolution (2), .BR read (2), .BR socket (2), .BR stat (2), .BR umask (2), .BR unlink (2), .BR write (2), .BR fopen (3), .BR fifo (7), .BR feature_test_macros (7)