1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
4 .\" 1993 Michael Haardt, Ian Jackson.
7 .\" Permission is granted to make and distribute verbatim copies of this
8 .\" manual provided the copyright notice and this permission notice are
9 .\" preserved on all copies.
11 .\" Permission is granted to copy and distribute modified versions of this
12 .\" manual under the conditions for verbatim copying, provided that the
13 .\" entire resulting derived work is distributed under the terms of a
14 .\" permission notice identical to this one.
16 .\" Since the Linux kernel and libraries are constantly changing, this
17 .\" manual page may be incorrect or out-of-date. The author(s) assume no
18 .\" responsibility for errors or omissions, or for damages resulting from
19 .\" the use of the information contained herein. The author(s) may not
20 .\" have taken the same level of care in the production of this manual,
21 .\" which is licensed free of charge, as they might when working
24 .\" Formatted or processed versions of this manual, if unaccompanied by
25 .\" the source, must acknowledge the copyright and authors of this work.
27 .\" Modified 1993-07-21 by Rik Faith <faith@cs.unc.edu>
28 .\" Modified 1994-08-21 by Michael Haardt
29 .\" Modified 1996-04-13 by Andries Brouwer <aeb@cwi.nl>
30 .\" Modified 1996-05-13 by Thomas Koenig
31 .\" Modified 1996-12-20 by Michael Haardt
32 .\" Modified 1999-02-19 by Andries Brouwer <aeb@cwi.nl>
33 .\" Modified 1998-11-28 by Joseph S. Myers <jsm28@hermes.cam.ac.uk>
34 .\" Modified 1999-06-03 by Michael Haardt
35 .\" Modified 2002-05-07 by Michael Kerrisk <mtk.manpages@gmail.com>
36 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
37 .\" 2004-12-08, mtk, reordered flags list alphabetically
38 .\" 2004-12-08, Martin Pool <mbp@sourcefrog.net> (& mtk), added O_NOATIME
39 .\" 2007-09-18, mtk, Added description of O_CLOEXEC + other minor edits
40 .\" 2008-01-03, mtk, with input from Trond Myklebust
41 .\" <trond.myklebust@fys.uio.no> and Timo Sirainen <tss@iki.fi>
42 .\" Rewrite description of O_EXCL.
43 .\" 2008-01-11, Greg Banks <gnb@melbourne.sgi.com>: add more detail
45 .\" 2008-02-26, Michael Haardt: Reorganized text for O_CREAT and mode
47 .\" FIXME . Apr 08: The next POSIX revision has O_EXEC, O_SEARCH, and
48 .\" O_TTYINIT. Eventually these may need to be documented. --mtk
49 .\" FIXME Linux 2.6.33 has O_DSYNC, and a hidden __O_SYNC.
50 .\" FIXME: Linux 2.6.39 added O_PATH
52 .TH OPEN 2 2012-02-27 "Linux" "Linux Programmer's Manual"
54 open, creat \- open and possibly create a file or device
57 .B #include <sys/types.h>
58 .B #include <sys/stat.h>
61 .BI "int open(const char *" pathname ", int " flags );
62 .BI "int open(const char *" pathname ", int " flags ", mode_t " mode );
64 .BI "int creat(const char *" pathname ", mode_t " mode );
71 returns a file descriptor, a small, nonnegative integer
72 for use in subsequent system calls
73 .RB ( read "(2), " write "(2), " lseek "(2), " fcntl "(2), etc.)."
74 The file descriptor returned by a successful call will be
75 the lowest-numbered file descriptor not currently open for the process.
77 By default, the new file descriptor is set to remain open across an
81 file descriptor flag described in
83 is initially disabled; the
85 flag, described below, can be used to change this default).
86 The file offset is set to the beginning of the file (see
92 .IR "open file description" ,
93 an entry in the system-wide table of open files.
94 This entry records the file offset and the file status flags
99 A file descriptor is a reference to one of these entries;
100 this reference is unaffected if
102 is subsequently removed or modified to refer to a different file.
103 The new open file description is initially not shared
104 with any other process,
105 but sharing may arise via
110 must include one of the following
112 .BR O_RDONLY ", " O_WRONLY ", or " O_RDWR .
113 These request opening the file read-only, write-only, or read/write,
116 In addition, zero or more file creation flags and file status flags
122 .I file creation flags
124 .BR O_CREAT ", " O_EXCL ", " O_NOCTTY ", and " O_TRUNC .
127 are all of the remaining flags listed below.
128 .\" FIXME . Actually is it true that the "file status flags" are all of the
129 .\" remaining flags listed below? SUSv4 divides the flags into:
133 .\" * Other (O_CLOEXEC, O_DIRECTORY, O_NOFOLLOW)
134 .\" though it's not clear what the difference between "other" and
135 .\" "File creation" flags is. (I've raised an Aardvark to see if this
136 .\" can be clarified in SUSv4; 10 Oct 2008.)
137 The distinction between these two groups of flags is that
138 the file status flags can be retrieved and (in some cases)
141 The full list of file creation flags and file status flags is as follows:
144 The file is opened in append mode.
147 the file offset is positioned at the end of the file,
151 may lead to corrupted files on NFS file systems if more than one process
152 appends data to a file at once.
153 .\" For more background, see
154 .\" http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=453946
155 .\" http://nfs.sourceforge.net/
156 This is because NFS does not support
157 appending to a file, so the client kernel has to simulate it, which
158 can't be done without a race condition.
161 Enable signal-driven I/O:
164 by default, but this can be changed via
166 when input or output becomes possible on this file descriptor.
167 This feature is only available for terminals, pseudoterminals,
168 sockets, and (since Linux 2.6) pipes and FIFOs.
173 .BR O_CLOEXEC " (Since Linux 2.6.23)"
174 Enable the close-on-exec flag for the new file descriptor.
175 Specifying this flag permits a program to avoid additional
178 operations to set the
182 use of this flag is essential in some multithreaded programs
183 since using a separate
188 flag does not suffice to avoid race conditions
189 where one thread opens a file descriptor at the same
190 time as another thread does a
194 .\" This flag fixes only one form of the race condition;
195 .\" The race can also occur with, for example, descriptors
196 .\" returned by accept(), pipe(), etc.
199 If the file does not exist it will be created.
200 The owner (user ID) of the file is set to the effective user ID
202 The group ownership (group ID) is set either to
203 the effective group ID of the process or to the group ID of the
204 parent directory (depending on file system type and mount options,
205 and the mode of the parent directory, see the mount options
211 .\" As at 2.6.25, bsdgroups is supported by ext2, ext3, ext4, and
212 .\" XFS (since 2.6.14).
216 specifies the permissions to use in case a new file is created.
217 This argument must be supplied when
223 is not specified, then
226 The effective permissions are modified by
229 in the usual way: The permissions of the created file are
230 .IR "(mode\ &\ ~umask)" .
231 Note that this mode only applies to future accesses of the
232 newly created file; the
234 call that creates a read-only file may well return a read/write
237 The following symbolic constants are provided for
241 00700 user (file owner) has read, write and execute permission
244 00400 user has read permission
247 00200 user has write permission
250 00100 user has execute permission
253 00070 group has read, write and execute permission
256 00040 group has read permission
259 00020 group has write permission
262 00010 group has execute permission
265 00007 others have read, write and execute permission
268 00004 others have read permission
271 00002 others have write permission
274 00001 others have execute permission
277 .BR O_DIRECT " (Since Linux 2.4.10)"
278 Try to minimize cache effects of the I/O to and from this file.
279 In general this will degrade performance, but it is useful in
280 special situations, such as when applications do their own caching.
281 File I/O is done directly to/from user space buffers.
284 flag on its own makes an effort to transfer data synchronously,
285 but does not give the guarantees of the
287 flag that data and necessary metadata are transferred.
288 To guarantee synchronous I/O,
290 must be used in addition to
294 below for further discussion.
296 A semantically similar (but deprecated) interface for block devices
301 If \fIpathname\fP is not a directory, cause the open to fail.
302 .\" But see the following and its replies:
303 .\" http://marc.theaimsgroup.com/?t=112748702800001&r=1&w=2
304 .\" [PATCH] open: O_DIRECTORY and O_CREAT together should fail
305 .\" O_DIRECTORY | O_CREAT causes O_DIRECTORY to be ignored.
306 This flag is Linux-specific, and was added in kernel version 2.1.126, to
307 avoid denial-of-service problems if
310 FIFO or tape device, but should not be used outside of the
315 Ensure that this call creates the file:
316 if this flag is specified in conjunction with
324 When these two flags are specified, symbolic links are not followed:
325 .\" POSIX.1-2001 explicitly requires this behavior.
328 is a symbolic link, then
330 fails regardless of where the symbolic link points to.
332 In general, the behavior of
334 is undefined if it is used without
336 There is one exception: on Linux 2.6 and later,
342 refers to a block device.
343 If the block device is in use by the system (e.g., mounted),
350 is only supported when using NFSv3 or later on kernel 2.6 or later.
351 In NFS environments where
353 support is not provided, programs that rely on it
354 for performing locking tasks will contain a race condition.
355 Portable programs that want to perform atomic file locking using a lockfile,
356 and need to avoid reliance on NFS support for
358 can create a unique file on
359 the same file system (e.g., incorporating hostname and PID), and use
361 to make a link to the lockfile.
364 returns 0, the lock is successful.
367 on the unique file to check if its link count has increased to 2,
368 in which case the lock is also successful.
372 Allow files whose sizes cannot be represented in an
374 (but can be represented in an
378 .B _LARGEFILE64_SOURCE
379 macro must be defined
383 in order to obtain this definition.
386 feature test macro to 64 (rather than using
389 method of accessing large files on 32-bit systems (see
390 .BR feature_test_macros (7)).
392 .BR O_NOATIME " (Since Linux 2.6.8)"
393 Do not update the file last access time (st_atime in the inode)
396 This flag is intended for use by indexing or backup programs,
397 where its use can significantly reduce the amount of disk activity.
398 This flag may not be effective on all file systems.
399 One example is NFS, where the server maintains the access time.
400 .\" The O_NOATIME flag also affects the treatment of st_atime
401 .\" by mmap() and readdir(2), MTK, Dec 04.
406 refers to a terminal device\(emsee
408 it will not become the process's controlling terminal even if the
409 process does not have one.
412 If \fIpathname\fP is a symbolic link, then the open fails.
413 This is a FreeBSD extension, which was added to Linux in version 2.1.126.
414 Symbolic links in earlier components of the pathname will still be
416 .\" The headers from glibc 2.0.100 and later include a
417 .\" definition of this flag; \fIkernels before 2.1.126 will ignore it if
420 .BR O_NONBLOCK " or " O_NDELAY
421 When possible, the file is opened in nonblocking mode.
424 nor any subsequent operations on the file descriptor which is
425 returned will cause the calling process to wait.
426 For the handling of FIFOs (named pipes), see also
428 For a discussion of the effect of
430 in conjunction with mandatory file locks and with file leases, see
434 The file is opened for synchronous I/O.
437 on the resulting file descriptor will block the calling process until
438 the data has been physically written to the underlying hardware.
439 .IR "But see NOTES below" .
442 If the file already exists and is a regular file and the open mode allows
447 it will be truncated to length 0.
448 If the file is a FIFO or terminal device file, the
451 Otherwise the effect of
455 Some of these optional flags can be altered using
457 after the file has been opened.
465 .BR O_CREAT|O_WRONLY|O_TRUNC .
470 return the new file descriptor, or \-1 if an error occurred
473 is set appropriately).
477 The requested access to the file is not allowed, or search permission
478 is denied for one of the directories in the path prefix of
480 or the file did not exist yet and write access to the parent directory
483 .BR path_resolution (7).)
488 .BR O_CREAT " and " O_EXCL
493 points outside your accessible address space.
500 While blocked waiting to complete an open of a slow device
503 the call was interrupted by a signal handler; see
508 refers to a directory and the access requested involved writing
516 Too many symbolic links were encountered in resolving
518 or \fBO_NOFOLLOW\fP was specified but
523 The process already has the maximum number of files open.
530 The system limit on the total number of open files has been reached.
534 refers to a device special file and no corresponding device exists.
535 (This is a Linux kernel bug; in this situation
541 is not set and the named file does not exist.
542 Or, a directory component in
544 does not exist or is a dangling symbolic link.
547 Insufficient kernel memory was available.
551 was to be created but the device containing
553 has no room for the new file.
556 A component used as a directory in
558 is not, in fact, a directory, or \fBO_DIRECTORY\fP was specified and
563 .BR O_NONBLOCK " | " O_WRONLY
564 is set, the named file is a FIFO and
565 no process has the file open for reading.
566 Or, the file is a device special file and no corresponding device exists.
570 refers to a regular file that is too large to be opened.
571 The usual scenario here is that an application compiled
572 on a 32-bit platform without
573 .I -D_FILE_OFFSET_BITS=64
574 tried to open a file whose size exceeds
580 This is the error specified by POSIX.1-2001;
581 in kernels before 2.6.24, Linux gave the error
584 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=7253
585 .\" "Open of a large file on 32-bit fails with EFBIG, should be EOVERFLOW"
586 .\" Reported 2006-10-03
591 flag was specified, but the effective user ID of the caller
592 .\" Strictly speaking, it's the file system UID... (MTK)
593 did not match the owner of the file and the caller was not privileged
598 refers to a file on a read-only file system and write access was
603 refers to an executable image which is currently being executed and
604 write access was requested.
609 flag was specified, and an incompatible lease was held on the file
613 SVr4, 4.3BSD, POSIX.1-2001.
619 flags are Linux-specific, and one may need to define
624 to obtain their definitions.
628 flag is not specified in POSIX.1-2001,
629 but is specified in POSIX.1-2008.
632 is not specified in POSIX; one has to define
637 to get its definition.
641 flag indicates that one wants to open
642 but does not necessarily have the intention to read or write.
643 This is typically used to open devices in order to get a file descriptor
647 Unlike the other values that can be specified in
652 .BR O_RDONLY ", " O_WRONLY ", and " O_RDWR ,
653 do not specify individual bits.
654 Rather, they define the low order two bits of
656 and are defined respectively as 0, 1, and 2.
657 In other words, the combination
658 .B "O_RDONLY | O_WRONLY"
659 is a logical error, and certainly does not have the same meaning as
661 Linux reserves the special, nonstandard access mode 3 (binary 11) in
664 check for read and write permission on the file and return a descriptor
665 that can't be used for reading or writing.
666 This nonstandard access mode is used by some Linux drivers to return a
667 descriptor that is only to be used for device-specific
670 .\" See for example util-linux's disk-utils/setfdprm.c
671 .\" For some background on access mode 3, see
672 .\" http://thread.gmane.org/gmane.linux.kernel/653123
673 .\" "[RFC] correct flags to f_mode conversion in __dentry_open"
674 .\" LKML, 12 Mar 2008
676 The (undefined) effect of
677 .B O_RDONLY | O_TRUNC
678 varies among implementations.
679 On many systems the file is actually truncated.
680 .\" Linux 2.0, 2.5: truncate
681 .\" Solaris 5.7, 5.8: truncate
682 .\" Irix 6.5: truncate
683 .\" Tru64 5.1B: truncate
684 .\" HP-UX 11.22: truncate
685 .\" FreeBSD 4.7: truncate
687 There are many infelicities in the protocol underlying NFS, affecting
689 .BR O_SYNC " and " O_NDELAY .
691 POSIX provides for three different variants of synchronized I/O,
692 corresponding to the flags
697 Currently (2.6.31), Linux only implements
703 to the same numerical value as
705 Most Linux file systems don't actually implement the POSIX
707 semantics, which require all metadata updates of a write
708 to be on disk on returning to userspace, but only the
710 semantics, which require only actual file data and metadata necessary
711 to retrieve it to be on disk by the time the system call returns.
715 can open device special files, but
717 cannot create them; use
721 On NFS file systems with UID mapping enabled,
724 return a file descriptor but, for example,
728 This is because the client performs
731 permissions, but UID mapping is performed by the server upon
732 read and write requests.
734 If the file is newly created, its
739 (respectively, time of last access, time of last status change, and
740 time of last modification; see
743 to the current time, and so are the
749 Otherwise, if the file is modified because of the
751 flag, its st_ctime and st_mtime fields are set to the current time.
756 flag may impose alignment restrictions on the length and address
757 of userspace buffers and the file offset of I/Os.
759 restrictions vary by file system and kernel version and might be
761 However there is currently no file system\-independent
762 interface for an application to discover these restrictions for a given
764 Some file systems provide their own interfaces
765 for doing so, for example the
770 Under Linux 2.4, transfer sizes, and the alignment of the user buffer
771 and the file offset must all be multiples of the logical block size
773 Under Linux 2.6, alignment to 512-byte boundaries
778 flag was introduced in SGI IRIX, where it has alignment
779 restrictions similar to those of Linux 2.4.
782 call to query appropriate alignments, and sizes.
783 FreeBSD 4.x introduced
784 a flag of the same name, but without alignment restrictions.
787 support was added under Linux in kernel version 2.4.10.
788 Older Linux kernels simply ignore this flag.
789 Some file systems may not implement the flag and
795 Applications should avoid mixing
797 and normal I/O to the same file,
798 and especially to overlapping byte regions in the same file.
799 Even when the file system correctly handles the coherency issues in
800 this situation, overall I/O throughput is likely to be slower than
801 using either mode alone.
802 Likewise, applications should avoid mixing
804 of files with direct I/O to the same files.
808 with NFS will differ from local file systems.
810 kernels configured in certain ways, may not support this combination.
811 The NFS protocol does not support passing the flag to the server, so
813 I/O will only bypass the page cache on the client; the server may
815 The client asks the server to make the I/O
816 synchronous to preserve the synchronous semantics of
818 Some servers will perform poorly under these circumstances, especially
819 if the I/O size is small.
820 Some servers may also be configured to
821 lie to clients about the I/O having reached stable storage; this
822 will avoid the performance penalty at some risk to data integrity
823 in the event of server power failure.
824 The Linux NFS client places no alignment restrictions on
830 is a potentially powerful tool that should be used with caution.
831 It is recommended that applications treat use of
833 as a performance option which is disabled by default.
836 "The thing that has always disturbed me about O_DIRECT is that the whole
837 interface is just stupid, and was probably designed by a deranged monkey
838 on some serious mind-controlling substances."\(emLinus
841 Currently, it is not possible to enable signal-driven
849 .\" FIXME . Check bugzilla report on open(O_ASYNC)
850 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=5993
871 .BR path_resolution (7),