1 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
2 .\" and Copyright (C) 1993 Michael Haardt, Ian Jackson.
3 .\" and Copyright (C) 2008 Greg Banks
4 .\" and Copyright (C) 2006, 2008, 2013, 2014 Michael Kerrisk <mtk.manpages@gmail.com>
6 .\" %%%LICENSE_START(VERBATIM)
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.
28 .\" Modified 1993-07-21 by Rik Faith <faith@cs.unc.edu>
29 .\" Modified 1994-08-21 by Michael Haardt
30 .\" Modified 1996-04-13 by Andries Brouwer <aeb@cwi.nl>
31 .\" Modified 1996-05-13 by Thomas Koenig
32 .\" Modified 1996-12-20 by Michael Haardt
33 .\" Modified 1999-02-19 by Andries Brouwer <aeb@cwi.nl>
34 .\" Modified 1998-11-28 by Joseph S. Myers <jsm28@hermes.cam.ac.uk>
35 .\" Modified 1999-06-03 by Michael Haardt
36 .\" Modified 2002-05-07 by Michael Kerrisk <mtk.manpages@gmail.com>
37 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
38 .\" 2004-12-08, mtk, reordered flags list alphabetically
39 .\" 2004-12-08, Martin Pool <mbp@sourcefrog.net> (& mtk), added O_NOATIME
40 .\" 2007-09-18, mtk, Added description of O_CLOEXEC + other minor edits
41 .\" 2008-01-03, mtk, with input from Trond Myklebust
42 .\" <trond.myklebust@fys.uio.no> and Timo Sirainen <tss@iki.fi>
43 .\" Rewrite description of O_EXCL.
44 .\" 2008-01-11, Greg Banks <gnb@melbourne.sgi.com>: add more detail
46 .\" 2008-02-26, Michael Haardt: Reorganized text for O_CREAT and mode
48 .\" FIXME . Apr 08: The next POSIX revision has O_EXEC, O_SEARCH, and
49 .\" O_TTYINIT. Eventually these may need to be documented. --mtk
51 .TH OPEN 2 2021-03-22 "Linux" "Linux Programmer's Manual"
53 open, openat, creat \- open and possibly create a file
58 .BI "int open(const char *" pathname ", int " flags );
59 .BI "int open(const char *" pathname ", int " flags ", mode_t " mode );
61 .BI "int creat(const char *" pathname ", mode_t " mode );
63 .BI "int openat(int " dirfd ", const char *" pathname ", int " flags );
64 .BI "int openat(int " dirfd ", const char *" pathname ", int " flags \
67 /* Documented separately, in \fBopenat2\fP(2): */
68 .BI "int openat2(int " dirfd ", const char *" pathname ,
69 .BI " const struct open_how *" how ", size_t " size ");"
73 Feature Test Macro Requirements for glibc (see
74 .BR feature_test_macros (7)):
80 _POSIX_C_SOURCE >= 200809L
87 system call opens the file specified by
89 If the specified file does not exist,
99 is a file descriptor, a small, nonnegative integer that is an index
100 to an entry in the process's table of open file descriptors.
101 The file descriptor is used
102 in subsequent system calls
103 .RB ( read "(2), " write "(2), " lseek "(2), " fcntl (2),
104 etc.) to refer to the open file.
105 The file descriptor returned by a successful call will be
106 the lowest-numbered file descriptor not currently open for the process.
108 By default, the new file descriptor is set to remain open across an
112 file descriptor flag described in
114 is initially disabled); the
116 flag, described below, can be used to change this default.
117 The file offset is set to the beginning of the file (see
123 .IR "open file description" ,
124 an entry in the system-wide table of open files.
125 The open file description records the file offset and the file status flags
127 A file descriptor is a reference to an open file description;
128 this reference is unaffected if
130 is subsequently removed or modified to refer to a different file.
131 For further details on open file descriptions, see NOTES.
135 must include one of the following
137 .BR O_RDONLY ", " O_WRONLY ", or " O_RDWR .
138 These request opening the file read-only, write-only, or read/write,
141 In addition, zero or more file creation flags and file status flags
147 .I file creation flags
160 are all of the remaining flags listed below.
161 .\" SUSv4 divides the flags into:
165 .\" * Other (O_CLOEXEC, O_DIRECTORY, O_NOFOLLOW)
166 .\" though it's not clear what the difference between "other" and
167 .\" "File creation" flags is. I raised an Aardvark to see if this
168 .\" can be clarified in SUSv4; 10 Oct 2008.
169 .\" http://thread.gmane.org/gmane.comp.standards.posix.austin.general/64/focus=67
170 .\" TC1 (balloted in 2013), resolved this, so that those three constants
171 .\" are also categorized" as file status flags.
173 The distinction between these two groups of flags is that
174 the file creation flags affect the semantics of the open operation itself,
175 while the file status flags affect the semantics of subsequent I/O operations.
176 The file status flags can be retrieved and (in some cases)
181 The full list of file creation flags and file status flags is as follows:
184 The file is opened in append mode.
187 the file offset is positioned at the end of the file,
190 The modification of the file offset and the write operation
191 are performed as a single atomic step.
194 may lead to corrupted files on NFS filesystems if more than one process
195 appends data to a file at once.
196 .\" For more background, see
197 .\" http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=453946
198 .\" http://nfs.sourceforge.net/
199 This is because NFS does not support
200 appending to a file, so the client kernel has to simulate it, which
201 can't be done without a race condition.
204 Enable signal-driven I/O:
207 by default, but this can be changed via
209 when input or output becomes possible on this file descriptor.
210 This feature is available only for terminals, pseudoterminals,
211 sockets, and (since Linux 2.6) pipes and FIFOs.
215 See also BUGS, below.
217 .BR O_CLOEXEC " (since Linux 2.6.23)"
218 .\" NOTE! several other man pages refer to this text
219 Enable the close-on-exec flag for the new file descriptor.
220 .\" FIXME . for later review when Issue 8 is one day released...
221 .\" POSIX proposes to fix many APIs that provide hidden FDs
222 .\" http://austingroupbugs.net/tag_view_page.php?tag_id=8
223 .\" http://austingroupbugs.net/view.php?id=368
224 Specifying this flag permits a program to avoid additional
227 operations to set the
231 Note that the use of this flag is essential in some multithreaded programs,
232 because using a separate
237 flag does not suffice to avoid race conditions
238 where one thread opens a file descriptor and
239 attempts to set its close-on-exec flag using
241 at the same time as another thread does a
245 Depending on the order of execution,
246 the race may lead to the file descriptor returned by
248 being unintentionally leaked to the program executed by the child process
251 (This kind of race is in principle possible for any system call
252 that creates a file descriptor whose close-on-exec flag should be set,
253 and various other Linux system calls provide an equivalent of the
255 flag to deal with this problem.)
256 .\" This flag fixes only one form of the race condition;
257 .\" The race can also occur with, for example, file descriptors
258 .\" returned by accept(), pipe(), etc.
263 does not exist, create it as a regular file.
265 The owner (user ID) of the new file is set to the effective user ID
268 The group ownership (group ID) of the new file is set either to
269 the effective group ID of the process (System V semantics)
270 or to the group ID of the parent directory (BSD semantics).
271 On Linux, the behavior depends on whether the
272 set-group-ID mode bit is set on the parent directory:
273 if that bit is set, then BSD semantics apply;
274 otherwise, System V semantics apply.
275 For some filesystems, the behavior also depends on the
279 mount options described in
281 .\" As at 2.6.25, bsdgroups is supported by ext2, ext3, ext4, and
282 .\" XFS (since 2.6.14).
286 argument specifies the file mode bits to be applied when a new file is created.
295 is ignored (and can thus be specified as 0, or simply omitted).
306 if it is not supplied,
307 some arbitrary bytes from the stack will be applied as the file mode.
309 The effective mode is modified by the process's
311 in the usual way: in the absence of a default ACL, the mode of the
313 .IR "(mode\ &\ \(tiumask)" .
317 applies only to future accesses of the
318 newly created file; the
320 call that creates a read-only file may well return a read/write
323 The following symbolic constants are provided for
328 00700 user (file owner) has read, write, and execute permission
331 00400 user has read permission
334 00200 user has write permission
337 00100 user has execute permission
340 00070 group has read, write, and execute permission
343 00040 group has read permission
346 00020 group has write permission
349 00010 group has execute permission
352 00007 others have read, write, and execute permission
355 00004 others have read permission
358 00002 others have write permission
361 00001 others have execute permission
364 According to POSIX, the effect when other bits are set in
367 On Linux, the following bits are also honored in
372 0004000 set-user-ID bit
375 0002000 set-group-ID bit (see
379 0001000 sticky bit (see
383 .BR O_DIRECT " (since Linux 2.4.10)"
384 Try to minimize cache effects of the I/O to and from this file.
385 In general this will degrade performance, but it is useful in
386 special situations, such as when applications do their own caching.
387 File I/O is done directly to/from user-space buffers.
390 flag on its own makes an effort to transfer data synchronously,
391 but does not give the guarantees of the
393 flag that data and necessary metadata are transferred.
394 To guarantee synchronous I/O,
396 must be used in addition to
398 See NOTES below for further discussion.
400 A semantically similar (but deprecated) interface for block devices
405 If \fIpathname\fP is not a directory, cause the open to fail.
406 .\" But see the following and its replies:
407 .\" http://marc.theaimsgroup.com/?t=112748702800001&r=1&w=2
408 .\" [PATCH] open: O_DIRECTORY and O_CREAT together should fail
409 .\" O_DIRECTORY | O_CREAT causes O_DIRECTORY to be ignored.
410 This flag was added in kernel version 2.1.126, to
411 avoid denial-of-service problems if
417 Write operations on the file will complete according to the requirements of
420 integrity completion.
425 return, the output data
426 has been transferred to the underlying hardware,
427 along with any file metadata that would be required to retrieve that data
428 (i.e., as though each
430 was followed by a call to
432 .IR "See NOTES below" .
435 Ensure that this call creates the file:
436 if this flag is specified in conjunction with
445 When these two flags are specified, symbolic links are not followed:
446 .\" POSIX.1-2001 explicitly requires this behavior.
449 is a symbolic link, then
451 fails regardless of where the symbolic link points.
453 In general, the behavior of
455 is undefined if it is used without
457 There is one exception: on Linux 2.6 and later,
463 refers to a block device.
464 If the block device is in use by the system (e.g., mounted),
471 is supported only when using NFSv3 or later on kernel 2.6 or later.
472 In NFS environments where
474 support is not provided, programs that rely on it
475 for performing locking tasks will contain a race condition.
476 Portable programs that want to perform atomic file locking using a lockfile,
477 and need to avoid reliance on NFS support for
479 can create a unique file on
480 the same filesystem (e.g., incorporating hostname and PID), and use
482 to make a link to the lockfile.
485 returns 0, the lock is successful.
488 on the unique file to check if its link count has increased to 2,
489 in which case the lock is also successful.
493 Allow files whose sizes cannot be represented in an
495 (but can be represented in an
499 .B _LARGEFILE64_SOURCE
500 macro must be defined
504 in order to obtain this definition.
507 feature test macro to 64 (rather than using
510 method of accessing large files on 32-bit systems (see
511 .BR feature_test_macros (7)).
513 .BR O_NOATIME " (since Linux 2.6.8)"
514 Do not update the file last access time
520 This flag can be employed only if one of the following conditions is true:
523 The effective UID of the process
524 .\" Strictly speaking: the filesystem UID
525 matches the owner UID of the file.
527 The calling process has the
529 capability in its user namespace and
530 the owner UID of the file has a mapping in the namespace.
533 This flag is intended for use by indexing or backup programs,
534 where its use can significantly reduce the amount of disk activity.
535 This flag may not be effective on all filesystems.
536 One example is NFS, where the server maintains the access time.
537 .\" The O_NOATIME flag also affects the treatment of st_atime
538 .\" by mmap() and readdir(2), MTK, Dec 04.
543 refers to a terminal device\(emsee
545 will not become the process's controlling terminal even if the
546 process does not have one.
549 If the trailing component (i.e., basename) of
551 is a symbolic link, then the open fails, with the error
553 Symbolic links in earlier components of the pathname will still be
557 error that can occur in this case is indistinguishable from the case where
558 an open fails because there are too many symbolic links found
559 while resolving components in the prefix part of the pathname.)
561 This flag is a FreeBSD extension, which was added to Linux in version 2.1.126,
562 and has subsequently been standardized in POSIX.1-2008.
567 .\" The headers from glibc 2.0.100 and later include a
568 .\" definition of this flag; \fIkernels before 2.1.126 will ignore it if
571 .BR O_NONBLOCK " or " O_NDELAY
572 When possible, the file is opened in nonblocking mode.
575 nor any subsequent I/O operations on the file descriptor which is
576 returned will cause the calling process to wait.
578 Note that the setting of this flag has no effect on the operation of
583 since those interfaces merely inform the caller about whether
584 a file descriptor is "ready",
585 meaning that an I/O operation performed on
586 the file descriptor with the
592 Note that this flag has no effect for regular files and block devices;
593 that is, I/O operations will (briefly) block when device activity
594 is required, regardless of whether
599 semantics might eventually be implemented,
600 applications should not depend upon blocking behavior
601 when specifying this flag for regular files and block devices.
603 For the handling of FIFOs (named pipes), see also
605 For a discussion of the effect of
607 in conjunction with mandatory file locks and with file leases, see
610 .BR O_PATH " (since Linux 2.6.39)"
611 .\" commit 1abf0c718f15a56a0a435588d1b104c7a37dc9bd
612 .\" commit 326be7b484843988afe57566b627fb7a70beac56
613 .\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
615 .\" http://thread.gmane.org/gmane.linux.man/2790/focus=3496
616 .\" Subject: Re: [PATCH] open(2): document O_PATH
617 .\" Newsgroups: gmane.linux.man, gmane.linux.kernel
619 Obtain a file descriptor that can be used for two purposes:
620 to indicate a location in the filesystem tree and
621 to perform operations that act purely at the file descriptor level.
622 The file itself is not opened, and other file operations (e.g.,
633 The following operations
635 be performed on the resulting file descriptor:
641 if the file descriptor refers to a directory
643 .\" commit 332a2e1244bd08b9e3ecd378028513396a004a24
648 .\" fstat(): commit 55815f70147dcfa3ead5738fd56d3574e2e3c1c2
651 .\" fstatfs(): commit 9d05746e7b16d8565dddbe3200faa1e669d23bbf
653 Duplicating the file descriptor
659 Getting and setting file descriptor flags
665 Retrieving open file status flags using the
668 operation: the returned flags will include the bit
671 Passing the file descriptor as the
675 and the other "*at()" system calls.
681 .BR AT_SYMLINK_FOLLOW )
682 even if the file is not a directory.
684 Passing the file descriptor to another process via a UNIX domain socket
702 Opening a file or directory with the
704 flag requires no permissions on the object itself
705 (but does require execute permission on the directories in the path prefix).
706 Depending on the subsequent operation,
707 a check for suitable file permissions may be performed (e.g.,
709 requires execute permission on the directory referred to
710 by its file descriptor argument).
712 obtaining a reference to a filesystem object by opening it with the
714 flag requires that the caller have read permission on the object,
715 even when the subsequent operation (e.g.,
718 does not require read permission on the object.
722 is a symbolic link and the
724 flag is also specified,
725 then the call returns a file descriptor referring to the symbolic link.
726 This file descriptor can be used as the
734 with an empty pathname to have the calls operate on the symbolic link.
738 refers to an automount point that has not yet been triggered, so no
739 other filesystem is mounted on it, then the call returns a file
740 descriptor referring to the automount directory without triggering a mount.
742 can then be used to determine if it is, in fact, an untriggered
744 .RB ( ".f_type == AUTOFS_SUPER_MAGIC" ).
748 for regular files is to provide the equivalent of POSIX.1's
751 This permits us to open a file for which we have execute
752 permission but not read permission, and then execute that file,
753 with steps something like the following:
758 fd = open("some_prog", O_PATH);
759 snprintf(buf, PATH_MAX, "/proc/self/fd/%d", fd);
760 execl(buf, "some_prog", (char *) NULL);
766 file descriptor can also be passed as the argument of
770 Write operations on the file will complete according to the requirements of
774 (by contrast with the
784 returns, the output data and associated file metadata
785 have been transferred to the underlying hardware
786 (i.e., as though each
788 was followed by a call to
790 .IR "See NOTES below" .
792 .BR O_TMPFILE " (since Linux 3.11)"
793 .\" commit 60545d0d4610b02e55f65d141c95b18ccf855b6e
794 .\" commit f4e0c30c191f87851c4a53454abb55ee276f4a7e
795 .\" commit bb458c644a59dbba3a1fe59b27106c5e68e1c4bd
796 Create an unnamed temporary regular file.
799 argument specifies a directory;
800 an unnamed inode will be created in that directory's filesystem.
801 Anything written to the resulting file will be lost when
802 the last file descriptor is closed, unless the file is given a name.
805 must be specified with one of
813 is not specified, then
815 can be used to link the temporary file into the filesystem, making it
816 permanent, using code like the following:
821 fd = open("/path/to/dir", O_TMPFILE | O_RDWR,
824 /* File I/O on \(aqfd\(aq... */
826 linkat(fd, "", AT_FDCWD, "/path/for/file", AT_EMPTY_PATH);
828 /* If the caller doesn\(aqt have the CAP_DAC_READ_SEARCH
829 capability (needed to use AT_EMPTY_PATH with linkat(2)),
830 and there is a proc(5) filesystem mounted, then the
831 linkat(2) call above can be replaced with:
833 snprintf(path, PATH_MAX, "/proc/self/fd/%d", fd);
834 linkat(AT_FDCWD, path, AT_FDCWD, "/path/for/file",
844 argument determines the file permission mode, as with
851 prevents a temporary file from being linked into the filesystem
853 (Note that the meaning of
855 in this case is different from the meaning of
859 There are two main use cases for
860 .\" Inspired by http://lwn.net/Articles/559147/
866 functionality: race-free creation of temporary files that
867 (1) are automatically deleted when closed;
868 (2) can never be reached via any pathname;
869 (3) are not subject to symlink attacks; and
870 (4) do not require the caller to devise unique names.
872 Creating a file that is initially invisible, which is then populated
873 with data and adjusted to have appropriate filesystem attributes
878 before being atomically linked into the filesystem
879 in a fully formed state (using
885 requires support by the underlying filesystem;
886 only a subset of Linux filesystems provide that support.
887 In the initial implementation, support was provided in
888 the ext2, ext3, ext4, UDF, Minix, and tmpfs filesystems.
889 .\" To check for support, grep for "tmpfile" in kernel sources
890 Support for other filesystems has subsequently been added as follows:
892 .\" commit 99b6436bc29e4f10e4388c27a3e4810191cc4788
893 .\" commit ab29743117f9f4c22ac44c13c1647fb24fb2bafe
895 .\" commit ef3b9af50bfa6a1f02cd7b3f5124b712b1ba3e3c
897 .\" commit 50732df02eefb39ab414ef655979c2c9b64ad21c
898 and ubifs (Linux 4.9)
901 If the file already exists and is a regular file and the access mode allows
906 it will be truncated to length 0.
907 If the file is a FIFO or terminal device file, the
910 Otherwise, the effect of
916 is equivalent to calling
921 .BR O_CREAT|O_WRONLY|O_TRUNC .
925 system call operates in exactly the same way as
927 except for the differences described here.
931 argument is used in conjunction with the
935 If the pathname given in
941 If the pathname given in
949 is interpreted relative to the current working
950 directory of the calling process (like
953 If the pathname given in
955 is relative, then it is interpreted relative to the directory
956 referred to by the file descriptor
958 (rather than relative to the current working directory of
959 the calling process, as is done by
961 for a relative pathname).
966 system call is an extension of
968 and provides a superset of the features of
970 It is documented separately, in
978 return the new file descriptor (a nonnegative integer).
979 On error, \-1 is returned and
981 is set to indicate the error.
987 can fail with the following errors:
990 The requested access to the file is not allowed, or search permission
991 is denied for one of the directories in the path prefix of
993 or the file did not exist yet and write access to the parent directory
996 .BR path_resolution (7).)
999 .\" commit 30aba6656f61ed44cba445a3c0d38b296fa9e8f5
1005 .I protected_regular
1006 sysctl is enabled, the file already exists and is a FIFO or regular file, the
1007 owner of the file is neither the current user nor the owner of the
1008 containing directory, and the containing directory is both world- or
1009 group-writable and sticky.
1010 For details, see the descriptions of
1011 .IR /proc/sys/fs/protected_fifos
1013 .IR /proc/sys/fs/protected_regular
1023 refers to a block device that is in use by the system (e.g., it is mounted).
1028 is specified, the file does not exist, and the user's quota of disk
1029 blocks or inodes on the filesystem has been exhausted.
1034 .BR O_CREAT " and " O_EXCL
1039 points outside your accessible address space.
1046 While blocked waiting to complete an open of a slow device
1049 the call was interrupted by a signal handler; see
1053 The filesystem does not support the
1058 for more information.
1062 .\" In particular, __O_TMPFILE instead of O_TMPFILE
1079 and the final component ("basename") of the new file's
1082 (e.g., it contains characters not permitted by the underlying filesystem).
1085 The final component ("basename") of
1088 (e.g., it contains characters not permitted by the underlying filesystem).
1092 refers to a directory and the access requested involved writing
1101 refers to an existing directory,
1109 but this kernel version does not provide the
1114 Too many symbolic links were encountered in resolving
1119 was a symbolic link, and
1127 The per-process limit on the number of open file descriptors has been reached
1128 (see the description of
1138 The system-wide limit on the total number of open files has been reached.
1142 refers to a device special file and no corresponding device exists.
1143 (This is a Linux kernel bug; in this situation
1149 is not set and the named file does not exist.
1152 A directory component in
1154 does not exist or is a dangling symbolic link.
1158 refers to a nonexistent directory,
1166 but this kernel version does not provide the
1171 The named file is a FIFO,
1172 but memory for the FIFO buffer can't be allocated because
1173 the per-user hard limit on memory allocation for pipes has been reached
1174 and the caller is not privileged; see
1178 Insufficient kernel memory was available.
1182 was to be created but the device containing
1184 has no room for the new file.
1187 A component used as a directory in
1189 is not, in fact, a directory, or \fBO_DIRECTORY\fP was specified and
1191 was not a directory.
1194 .BR O_NONBLOCK " | " O_WRONLY
1195 is set, the named file is a FIFO, and
1196 no process has the FIFO open for reading.
1199 The file is a device special file and no corresponding device exists.
1202 The file is a UNIX domain socket.
1205 The filesystem containing
1212 refers to a regular file that is too large to be opened.
1213 The usual scenario here is that an application compiled
1214 on a 32-bit platform without
1215 .I \-D_FILE_OFFSET_BITS=64
1216 tried to open a file whose size exceeds
1222 This is the error specified by POSIX.1;
1223 in kernels before 2.6.24, Linux gave the error
1226 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=7253
1227 .\" "Open of a large file on 32-bit fails with EFBIG, should be EOVERFLOW"
1228 .\" Reported 2006-10-03
1233 flag was specified, but the effective user ID of the caller
1234 .\" Strictly speaking, it's the filesystem UID... (MTK)
1235 did not match the owner of the file and the caller was not privileged.
1238 The operation was prevented by a file seal; see
1243 refers to a file on a read-only filesystem and write access was
1248 refers to an executable image which is currently being executed and
1249 write access was requested.
1253 refers to a file that is currently in use as a swap file, and the
1259 refers to a file that is currently being read by the kernel (e.g., for
1260 module/firmware loading), and write access was requested.
1265 flag was specified, and an incompatible lease was held on the file
1269 The following additional errors can occur for
1274 is not a valid file descriptor.
1278 is a relative pathname and
1280 is a file descriptor referring to a file other than a directory.
1283 was added to Linux in kernel 2.6.16;
1284 library support was added to glibc in version 2.4.
1288 SVr4, 4.3BSD, POSIX.1-2001, POSIX.1-2008.
1302 flags are Linux-specific.
1305 to obtain their definitions.
1312 flags are not specified in POSIX.1-2001,
1313 but are specified in POSIX.1-2008.
1314 Since glibc 2.12, one can obtain their definitions by defining either
1316 with a value greater than or equal to 200809L or
1318 with a value greater than or equal to 700.
1319 In glibc 2.11 and earlier, one obtains the definitions by defining
1323 .BR feature_test_macros (7),
1324 feature test macros such as
1325 .BR _POSIX_C_SOURCE ,
1329 must be defined before including
1335 flag is sometimes used in cases where one wants to open
1336 but does not necessarily have the intention to read or write.
1338 this may be used to open a device in order to get a file descriptor
1342 The (undefined) effect of
1343 .B O_RDONLY | O_TRUNC
1344 varies among implementations.
1345 On many systems the file is actually truncated.
1346 .\" Linux 2.0, 2.5: truncate
1347 .\" Solaris 5.7, 5.8: truncate
1348 .\" Irix 6.5: truncate
1349 .\" Tru64 5.1B: truncate
1350 .\" HP-UX 11.22: truncate
1351 .\" FreeBSD 4.7: truncate
1355 can open device special files, but
1357 cannot create them; use
1361 If the file is newly created, its
1366 (respectively, time of last access, time of last status change, and
1367 time of last modification; see
1370 to the current time, and so are the
1376 Otherwise, if the file is modified because of the
1382 fields are set to the current time.
1386 directory show the open file descriptors of the process with the PID
1389 .I /proc/[pid]/fdinfo
1390 directory show even more information about these file descriptors.
1393 for further details of both of these directories.
1395 The Linux header file
1401 synonym is defined instead.
1404 .SS Open file descriptions
1405 The term open file description is the one used by POSIX to refer to the
1406 entries in the system-wide table of open files.
1407 In other contexts, this object is
1408 variously also called an "open file object",
1409 a "file handle", an "open file table entry",
1410 or\(emin kernel-developer parlance\(ema
1413 When a file descriptor is duplicated (using
1416 the duplicate refers to the same open file description
1417 as the original file descriptor,
1418 and the two file descriptors consequently share
1419 the file offset and file status flags.
1420 Such sharing can also occur between processes:
1421 a child process created via
1423 inherits duplicates of its parent's file descriptors,
1424 and those duplicates refer to the same open file descriptions.
1428 of a file creates a new open file description;
1429 thus, there may be multiple open file descriptions
1430 corresponding to a file inode.
1432 On Linux, one can use the
1435 operation to test whether two file descriptors
1436 (in the same process or in two different processes)
1437 refer to the same open file description.
1440 .SS Synchronized I/O
1441 The POSIX.1-2008 "synchronized I/O" option
1442 specifies different variants of synchronized I/O,
1450 for controlling the behavior.
1451 Regardless of whether an implementation supports this option,
1452 it must at least support the use of
1462 Somewhat incorrectly, glibc defines
1464 to have the same value as
1467 is defined in the Linux header file
1469 on HP PA-RISC, but it is not used.)
1472 provides synchronized I/O
1474 integrity completion,
1475 meaning write operations will flush data and all associated metadata
1476 to the underlying hardware.
1478 provides synchronized I/O
1480 integrity completion,
1481 meaning write operations will flush data
1482 to the underlying hardware,
1483 but will only flush metadata updates that are required
1484 to allow a subsequent read operation to complete successfully.
1485 Data integrity completion can reduce the number of disk operations
1486 that are required for applications that don't need the guarantees
1487 of file integrity completion.
1489 To understand the difference between the two types of completion,
1490 consider two pieces of file metadata:
1491 the file last modification timestamp
1493 and the file length.
1494 All write operations will update the last file modification timestamp,
1495 but only writes that add data to the end of the
1496 file will change the file length.
1497 The last modification timestamp is not needed to ensure that
1498 a read completes successfully, but the file length is.
1501 would only guarantee to flush updates to the file length metadata
1504 would also always flush the last modification timestamp metadata).
1506 Before Linux 2.6.33, Linux implemented only the
1510 However, when that flag was specified,
1511 most filesystems actually provided the equivalent of synchronized I/O
1513 integrity completion (i.e.,
1515 was actually implemented as the equivalent of
1518 Since Linux 2.6.33, proper
1520 support is provided.
1521 However, to ensure backward binary compatibility,
1523 was defined with the same value as the historical
1527 was defined as a new (two-bit) flag value that includes the
1530 This ensures that applications compiled against
1531 new headers get at least
1533 semantics on pre-2.6.33 kernels.
1535 .SS C library/kernel differences
1537 the glibc wrapper function for
1541 system call, rather than the kernel's
1544 For certain architectures, this is also true in glibc versions before 2.26.
1547 There are many infelicities in the protocol underlying NFS, affecting
1549 .BR O_SYNC " and " O_NDELAY .
1551 On NFS filesystems with UID mapping enabled,
1554 return a file descriptor but, for example,
1558 This is because the client performs
1561 permissions, but UID mapping is performed by the server upon
1562 read and write requests.
1566 Opening the read or write end of a FIFO blocks until the other
1567 end is also opened (by another process or thread).
1570 for further details.
1573 .SS File access mode
1574 Unlike the other values that can be specified in
1579 .BR O_RDONLY ", " O_WRONLY ", and " O_RDWR
1580 do not specify individual bits.
1581 Rather, they define the low order two bits of
1583 and are defined respectively as 0, 1, and 2.
1584 In other words, the combination
1585 .B "O_RDONLY | O_WRONLY"
1586 is a logical error, and certainly does not have the same meaning as
1589 Linux reserves the special, nonstandard access mode 3 (binary 11) in
1592 check for read and write permission on the file and return a file descriptor
1593 that can't be used for reading or writing.
1594 This nonstandard access mode is used by some Linux drivers to return a
1595 file descriptor that is to be used only for device-specific
1598 .\" See for example util-linux's disk-utils/setfdprm.c
1599 .\" For some background on access mode 3, see
1600 .\" http://thread.gmane.org/gmane.linux.kernel/653123
1601 .\" "[RFC] correct flags to f_mode conversion in __dentry_open"
1602 .\" LKML, 12 Mar 2008
1605 .SS Rationale for openat() and other "directory file descriptor" APIs
1607 and the other system calls and library functions that take
1608 a directory file descriptor argument
1612 .BR fanotify_mark (2),
1622 .BR name_to_handle_at (2),
1634 address two problems with the older interfaces that preceded them.
1635 Here, the explanation is in terms of the
1637 call, but the rationale is analogous for the other interfaces.
1641 allows an application to avoid race conditions that could
1644 to open files in directories other than the current working directory.
1645 These race conditions result from the fact that some component
1646 of the directory prefix given to
1648 could be changed in parallel with the call to
1650 Suppose, for example, that we wish to create the file
1651 .I dir1/dir2/xxx.dep
1655 The problem is that between the existence check and the file-creation step,
1659 (which might be symbolic links)
1660 could be modified to point to a different location.
1661 Such races can be avoided by
1662 opening a file descriptor for the target directory,
1663 and then specifying that file descriptor as the
1671 file descriptor also has other benefits:
1673 the file descriptor is a stable reference to the directory,
1674 even if the directory is renamed; and
1676 the open file descriptor prevents the underlying filesystem from
1678 just as when a process has a current working directory on a filesystem.
1682 allows the implementation of a per-thread "current working
1683 directory", via file descriptor(s) maintained by the application.
1684 (This functionality can also be obtained by tricks based
1686 .IR /proc/self/fd/ dirfd,
1687 but less efficiently.)
1691 argument for these APIs can be obtained by using
1695 to open a directory (with either the
1700 Alternatively, such a file descriptor can be obtained by applying
1702 to a directory stream created using
1705 When these APIs are given a
1709 or the specified pathname is absolute,
1710 then they handle their pathname argument in the same way as
1711 the corresponding conventional APIs.
1712 However, in this case, several of the APIs have a
1714 argument that provides access to functionality that is not available with
1715 the corresponding conventional APIs.
1721 flag may impose alignment restrictions on the length and address
1722 of user-space buffers and the file offset of I/Os.
1724 restrictions vary by filesystem and kernel version and might be
1726 However there is currently no filesystem\-independent
1727 interface for an application to discover these restrictions for a given
1729 Some filesystems provide their own interfaces
1730 for doing so, for example the
1735 Under Linux 2.4, transfer sizes, the alignment of the user buffer,
1736 and the file offset must all be multiples of the logical block size
1738 Since Linux 2.6.0, alignment to the logical block size of the
1739 underlying storage (typically 512 bytes) suffices.
1740 The logical block size can be determined using the
1743 operation or from the shell using the command:
1752 I/Os should never be run concurrently with the
1755 if the memory buffer is a private mapping
1756 (i.e., any mapping created with the
1760 this includes memory allocated on the heap and statically allocated buffers).
1761 Any such I/Os, whether submitted via an asynchronous I/O interface or from
1762 another thread in the process,
1763 should be completed before
1766 Failure to do so can result in data corruption and undefined behavior in
1767 parent and child processes.
1768 This restriction does not apply when the memory buffer for the
1770 I/Os was created using
1777 Nor does this restriction apply when the memory buffer has been advised as
1781 ensuring that it will not be available
1787 flag was introduced in SGI IRIX, where it has alignment
1788 restrictions similar to those of Linux 2.4.
1791 call to query appropriate alignments, and sizes.
1792 FreeBSD 4.x introduced
1793 a flag of the same name, but without alignment restrictions.
1796 support was added under Linux in kernel version 2.4.10.
1797 Older Linux kernels simply ignore this flag.
1798 Some filesystems may not implement the flag, in which case
1800 fails with the error
1804 Applications should avoid mixing
1806 and normal I/O to the same file,
1807 and especially to overlapping byte regions in the same file.
1808 Even when the filesystem correctly handles the coherency issues in
1809 this situation, overall I/O throughput is likely to be slower than
1810 using either mode alone.
1811 Likewise, applications should avoid mixing
1813 of files with direct I/O to the same files.
1817 with NFS will differ from local filesystems.
1819 kernels configured in certain ways, may not support this combination.
1820 The NFS protocol does not support passing the flag to the server, so
1822 I/O will bypass the page cache only on the client; the server may
1823 still cache the I/O.
1824 The client asks the server to make the I/O
1825 synchronous to preserve the synchronous semantics of
1827 Some servers will perform poorly under these circumstances, especially
1828 if the I/O size is small.
1829 Some servers may also be configured to
1830 lie to clients about the I/O having reached stable storage; this
1831 will avoid the performance penalty at some risk to data integrity
1832 in the event of server power failure.
1833 The Linux NFS client places no alignment restrictions on
1839 is a potentially powerful tool that should be used with caution.
1840 It is recommended that applications treat use of
1842 as a performance option which is disabled by default.
1844 Currently, it is not possible to enable signal-driven
1851 to enable this flag.
1852 .\" FIXME . Check bugzilla report on open(O_ASYNC)
1853 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=5993
1855 One must check for two different error codes,
1859 when trying to determine whether the kernel supports
1869 and the file specified by
1873 will create a regular file (i.e.,
1887 .BR open_by_handle_at (2),
1899 .BR path_resolution (7),