]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/open.2
open.2: Document fs.protected_fifos and fs.protected_regular
[thirdparty/man-pages.git] / man2 / open.2
CommitLineData
fea681da 1.\" This manpage is Copyright (C) 1992 Drew Eckhardt;
fd185f58
MK
2.\" and Copyright (C) 1993 Michael Haardt, Ian Jackson.
3.\" and Copyright (C) 2008 Greg Banks
7b8ba76c 4.\" and Copyright (C) 2006, 2008, 2013, 2014 Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 5.\"
93015253 6.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
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.
10.\"
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.
c13182ef 15.\"
fea681da
MK
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
22.\" professionally.
c13182ef 23.\"
fea681da
MK
24.\" Formatted or processed versions of this manual, if unaccompanied by
25.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 26.\" %%%LICENSE_END
fea681da
MK
27.\"
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
c11b1abf
MK
36.\" Modified 2002-05-07 by Michael Kerrisk <mtk.manpages@gmail.com>
37.\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
1c1e15ed
MK
38.\" 2004-12-08, mtk, reordered flags list alphabetically
39.\" 2004-12-08, Martin Pool <mbp@sourcefrog.net> (& mtk), added O_NOATIME
fe75ec04 40.\" 2007-09-18, mtk, Added description of O_CLOEXEC + other minor edits
447bb15e 41.\" 2008-01-03, mtk, with input from Trond Myklebust
f4b9d6a5
MK
42.\" <trond.myklebust@fys.uio.no> and Timo Sirainen <tss@iki.fi>
43.\" Rewrite description of O_EXCL.
ddc4d339
MK
44.\" 2008-01-11, Greg Banks <gnb@melbourne.sgi.com>: add more detail
45.\" on O_DIRECT.
d77eb764 46.\" 2008-02-26, Michael Haardt: Reorganized text for O_CREAT and mode
fea681da 47.\"
61b7c1e1 48.\" FIXME . Apr 08: The next POSIX revision has O_EXEC, O_SEARCH, and
9f91e36c
MK
49.\" O_TTYINIT. Eventually these may need to be documented. --mtk
50.\"
6b621d05 51.TH OPEN 2 2020-02-09 "Linux" "Linux Programmer's Manual"
fea681da 52.SH NAME
7b8ba76c 53open, openat, creat \- open and possibly create a file
fea681da
MK
54.SH SYNOPSIS
55.nf
56.B #include <sys/types.h>
57.B #include <sys/stat.h>
58.B #include <fcntl.h>
5355ff82 59.PP
fea681da
MK
60.BI "int open(const char *" pathname ", int " flags );
61.BI "int open(const char *" pathname ", int " flags ", mode_t " mode );
5355ff82 62.PP
fea681da 63.BI "int creat(const char *" pathname ", mode_t " mode );
5355ff82 64.PP
7b8ba76c
MK
65.BI "int openat(int " dirfd ", const char *" pathname ", int " flags );
66.BI "int openat(int " dirfd ", const char *" pathname ", int " flags \
67", mode_t " mode );
a2dbb2e3 68.PP
4b322a2f
MK
69/* Documented separately, in \fBopenat2\fP(2): */
70.BI "int openat2(int " dirfd ", const char *" pathname ,
71.BI " const struct open_how *" how ", size_t " size ");
fea681da 72.fi
5355ff82 73.PP
7b8ba76c
MK
74.in -4n
75Feature Test Macro Requirements for glibc (see
76.BR feature_test_macros (7)):
77.in
5355ff82 78.PP
7b8ba76c
MK
79.BR openat ():
80.PD 0
81.ad l
82.RS 4
83.TP 4
84Since glibc 2.10:
b0da7b8b 85_POSIX_C_SOURCE\ >=\ 200809L
7b8ba76c
MK
86.TP
87Before glibc 2.10:
88_ATFILE_SOURCE
89.RE
90.ad
91.PD
fea681da 92.SH DESCRIPTION
ef81e101 93The
1f6ceb40 94.BR open ()
ef81e101
MK
95system call opens the file specified by
96.IR pathname .
97If the specified file does not exist,
98it may optionally (if
99.B O_CREAT
100is specified in
101.IR flags )
102be created by
103.BR open ().
104.PP
105The return value of
106.BR open ()
107is a file descriptor, a small, nonnegative integer that is used
108in subsequent system calls
109.RB ( read "(2), " write "(2), " lseek "(2), " fcntl (2),
110etc.) to refer to the open file.
e366dbc4 111The file descriptor returned by a successful call will be
2c4bff36 112the lowest-numbered file descriptor not currently open for the process.
e366dbc4 113.PP
fe75ec04 114By default, the new file descriptor is set to remain open across an
e366dbc4 115.BR execve (2)
1f6ceb40
MK
116(i.e., the
117.B FD_CLOEXEC
118file descriptor flag described in
31d79098
SP
119.BR fcntl (2)
120is initially disabled); the
fe75ec04 121.B O_CLOEXEC
d6a74b95 122flag, described below, can be used to change this default.
1f6ceb40 123The file offset is set to the beginning of the file (see
c13182ef 124.BR lseek (2)).
e366dbc4
MK
125.PP
126A call to
127.BR open ()
128creates a new
129.IR "open file description" ,
130an entry in the system-wide table of open files.
61b12e2b 131The open file description records the file offset and the file status flags
20ee63c1 132(see below).
61b12e2b 133A file descriptor is a reference to an open file description;
2c4bff36
MK
134this reference is unaffected if
135.I pathname
136is subsequently removed or modified to refer to a different file.
d20d9d33 137For further details on open file descriptions, see NOTES.
e366dbc4 138.PP
c4bb193f 139The argument
fea681da 140.I flags
e366dbc4
MK
141must include one of the following
142.IR "access modes" :
c7992edc 143.BR O_RDONLY ", " O_WRONLY ", or " O_RDWR .
e366dbc4
MK
144These request opening the file read-only, write-only, or read/write,
145respectively.
5355ff82 146.PP
bfe9ba67 147In addition, zero or more file creation flags and file status flags
c13182ef 148can be
fea681da 149.RI bitwise- or 'd
e366dbc4 150in
bfe9ba67 151.IR flags .
c13182ef
MK
152The
153.I file creation flags
154are
0e40804c 155.BR O_CLOEXEC ,
b072a788 156.BR O_CREAT ,
0e40804c
MK
157.BR O_DIRECTORY ,
158.BR O_EXCL ,
159.BR O_NOCTTY ,
160.BR O_NOFOLLOW ,
f2698a42 161.BR O_TMPFILE ,
0e40804c 162and
15fb5d03 163.BR O_TRUNC .
c13182ef
MK
164The
165.I file status flags
bfe9ba67 166are all of the remaining flags listed below.
0e40804c 167.\" SUSv4 divides the flags into:
93ee8f96
MK
168.\" * Access mode
169.\" * File creation
170.\" * File status
171.\" * Other (O_CLOEXEC, O_DIRECTORY, O_NOFOLLOW)
172.\" though it's not clear what the difference between "other" and
0e40804c
MK
173.\" "File creation" flags is. I raised an Aardvark to see if this
174.\" can be clarified in SUSv4; 10 Oct 2008.
175.\" http://thread.gmane.org/gmane.comp.standards.posix.austin.general/64/focus=67
176.\" TC1 (balloted in 2013), resolved this, so that those three constants
177.\" are also categorized" as file status flags.
178.\"
bfe9ba67 179The distinction between these two groups of flags is that
68210340
MK
180the file creation flags affect the semantics of the open operation itself,
181while the file status flags affect the semantics of subsequent I/O operations.
182The file status flags can be retrieved and (in some cases)
566b427d
MK
183modified; see
184.BR fcntl (2)
185for details.
5355ff82 186.PP
bfe9ba67 187The full list of file creation flags and file status flags is as follows:
fea681da 188.TP
1c1e15ed 189.B O_APPEND
c13182ef
MK
190The file is opened in append mode.
191Before each
0bfa087b 192.BR write (2),
1e568304 193the file offset is positioned at the end of the file,
1c1e15ed 194as if with
0bfa087b 195.BR lseek (2).
17efe87f 196The modification of the file offset and the write operation
20b8f0e2 197are performed as a single atomic step.
5355ff82 198.IP
1c1e15ed 199.B O_APPEND
9ee4a2b6 200may lead to corrupted files on NFS filesystems if more than one process
c13182ef 201appends data to a file at once.
a4391429
MK
202.\" For more background, see
203.\" http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=453946
204.\" http://nfs.sourceforge.net/
c13182ef 205This is because NFS does not support
1c1e15ed
MK
206appending to a file, so the client kernel has to simulate it, which
207can't be done without a race condition.
208.TP
209.B O_ASYNC
b50582eb 210Enable signal-driven I/O:
8bd58774
MK
211generate a signal
212.RB ( SIGIO
213by default, but this can be changed via
1c1e15ed
MK
214.BR fcntl (2))
215when input or output becomes possible on this file descriptor.
33a0ccb2 216This feature is available only for terminals, pseudoterminals,
1f6ceb40
MK
217sockets, and (since Linux 2.6) pipes and FIFOs.
218See
1c1e15ed
MK
219.BR fcntl (2)
220for further details.
9bde4908 221See also BUGS, below.
fe75ec04 222.TP
31c1f2b0 223.BR O_CLOEXEC " (since Linux 2.6.23)"
7fdec065 224.\" NOTE! several other man pages refer to this text
fe75ec04 225Enable the close-on-exec flag for the new file descriptor.
00d82ce8
MK
226.\" FIXME . for later review when Issue 8 is one day released...
227.\" POSIX proposes to fix many APIs that provide hidden FDs
228.\" http://austingroupbugs.net/tag_view_page.php?tag_id=8
229.\" http://austingroupbugs.net/view.php?id=368
24ec631f 230Specifying this flag permits a program to avoid additional
fe75ec04
MK
231.BR fcntl (2)
232.B F_SETFD
24ec631f 233operations to set the
0daa9e92 234.B FD_CLOEXEC
fe75ec04 235flag.
5355ff82 236.IP
7756d157
MK
237Note that the use of this flag is essential in some multithreaded programs,
238because using a separate
fe75ec04
MK
239.BR fcntl (2)
240.B F_SETFD
241operation to set the
0daa9e92 242.B FD_CLOEXEC
fe75ec04 243flag does not suffice to avoid race conditions
7756d157
MK
244where one thread opens a file descriptor and
245attempts to set its close-on-exec flag using
246.BR fcntl (2)
247at the same time as another thread does a
fe75ec04
MK
248.BR fork (2)
249plus
250.BR execve (2).
7756d157 251Depending on the order of execution,
30821db8 252the race may lead to the file descriptor returned by
7756d157
MK
253.BR open ()
254being unintentionally leaked to the program executed by the child process
255created by
256.BR fork (2).
257(This kind of race is in principle possible for any system call
258that creates a file descriptor whose close-on-exec flag should be set,
259and various other Linux system calls provide an equivalent of the
260.BR O_CLOEXEC
261flag to deal with this problem.)
fe75ec04 262.\" This flag fixes only one form of the race condition;
d9cb0d7d 263.\" The race can also occur with, for example, file descriptors
fe75ec04 264.\" returned by accept(), pipe(), etc.
1c1e15ed 265.TP
fea681da 266.B O_CREAT
6f72cae5
MK
267If
268.I pathname
269does not exist, create it as a regular file.
5355ff82 270.IP
40169a93 271The owner (user ID) of the new file is set to the effective user ID
c13182ef 272of the process.
5355ff82 273.IP
ddf5e4ab
MK
274The group ownership (group ID) of the new file is set either to
275the effective group ID of the process (System V semantics)
276or to the group ID of the parent directory (BSD semantics).
277On Linux, the behavior depends on whether the
278set-group-ID mode bit is set on the parent directory:
279if that bit is set, then BSD semantics apply;
280otherwise, System V semantics apply.
281For some filesystems, the behavior also depends on the
fea681da
MK
282.I bsdgroups
283and
284.I sysvgroups
ddf5e4ab 285mount options described in
fea681da 286.BR mount (8)).
8b39ad66
MK
287.\" As at 2.6.25, bsdgroups is supported by ext2, ext3, ext4, and
288.\" XFS (since 2.6.14).
4e698277
MK
289.RS
290.PP
1bab84a8 291The
4e698277 292.I mode
1bab84a8 293argument specifies the file mode bits be applied when a new file is created.
4e698277
MK
294This argument must be supplied when
295.B O_CREAT
f2698a42
AL
296or
297.B O_TMPFILE
4e698277
MK
298is specified in
299.IR flags ;
f2698a42 300if neither
4e698277 301.B O_CREAT
f2698a42
AL
302nor
303.B O_TMPFILE
304is specified, then
4e698277
MK
305.I mode
306is ignored.
58222012 307The effective mode is modified by the process's
4e698277 308.I umask
58222012
MK
309in the usual way: in the absence of a default ACL, the mode of the
310created file is
84a275c4 311.IR "(mode\ &\ ~umask)" .
33a0ccb2 312Note that this mode applies only to future accesses of the
4e698277
MK
313newly created file; the
314.BR open ()
315call that creates a read-only file may well return a read/write
316file descriptor.
317.PP
318The following symbolic constants are provided for
319.IR mode :
320.TP 9
321.B S_IRWXU
97d5b762 32200700 user (file owner) has read, write, and execute permission
4e698277
MK
323.TP
324.B S_IRUSR
32500400 user has read permission
326.TP
327.B S_IWUSR
32800200 user has write permission
329.TP
330.B S_IXUSR
33100100 user has execute permission
332.TP
333.B S_IRWXG
97d5b762 33400070 group has read, write, and execute permission
4e698277
MK
335.TP
336.B S_IRGRP
33700040 group has read permission
338.TP
339.B S_IWGRP
34000020 group has write permission
341.TP
342.B S_IXGRP
34300010 group has execute permission
344.TP
345.B S_IRWXO
97d5b762 34600007 others have read, write, and execute permission
4e698277
MK
347.TP
348.B S_IROTH
34900004 others have read permission
350.TP
351.B S_IWOTH
35200002 others have write permission
353.TP
354.B S_IXOTH
35500001 others have execute permission
356.RE
9e1d8950
MK
357.IP
358According to POSIX, the effect when other bits are set in
359.I mode
360is unspecified.
361On Linux, the following bits are also honored in
362.IR mode :
363.RS
364.TP 9
365.B S_ISUID
3660004000 set-user-ID bit
367.TP
368.B S_ISGID
3690002000 set-group-ID bit (see
e6fc1596 370.BR inode (7)).
9e1d8950
MK
371.TP
372.B S_ISVTX
3730001000 sticky bit (see
e6fc1596 374.BR inode (7)).
9e1d8950 375.RE
fea681da 376.TP
31c1f2b0 377.BR O_DIRECT " (since Linux 2.4.10)"
1c1e15ed
MK
378Try to minimize cache effects of the I/O to and from this file.
379In general this will degrade performance, but it is useful in
380special situations, such as when applications do their own caching.
bce0482f 381File I/O is done directly to/from user-space buffers.
015221ef
CH
382The
383.B O_DIRECT
0deb3ce9 384flag on its own makes an effort to transfer data synchronously,
015221ef
CH
385but does not give the guarantees of the
386.B O_SYNC
0deb3ce9
JM
387flag that data and necessary metadata are transferred.
388To guarantee synchronous I/O,
015221ef
CH
389.B O_SYNC
390must be used in addition to
391.BR O_DIRECT .
be02e49f 392See NOTES below for further discussion.
5355ff82 393.IP
c13182ef 394A semantically similar (but deprecated) interface for block devices
9b54d4fa 395is described in
1c1e15ed
MK
396.BR raw (8).
397.TP
398.B O_DIRECTORY
a8d55537 399If \fIpathname\fP is not a directory, cause the open to fail.
9f8d688a
MK
400.\" But see the following and its replies:
401.\" http://marc.theaimsgroup.com/?t=112748702800001&r=1&w=2
402.\" [PATCH] open: O_DIRECTORY and O_CREAT together should fail
403.\" O_DIRECTORY | O_CREAT causes O_DIRECTORY to be ignored.
65496644 404This flag was added in kernel version 2.1.126, to
60a90ecd
MK
405avoid denial-of-service problems if
406.BR opendir (3)
407is called on a
a3041a58 408FIFO or tape device.
1c1e15ed 409.TP
6cf19e62
MK
410.B O_DSYNC
411Write operations on the file will complete according to the requirements of
412synchronized I/O
413.I data
414integrity completion.
5355ff82 415.IP
6cf19e62
MK
416By the time
417.BR write (2)
418(and similar)
419return, the output data
420has been transferred to the underlying hardware,
421along with any file metadata that would be required to retrieve that data
422(i.e., as though each
423.BR write (2)
424was followed by a call to
425.BR fdatasync (2)).
426.IR "See NOTES below" .
427.TP
fea681da 428.B O_EXCL
f4b9d6a5
MK
429Ensure that this call creates the file:
430if this flag is specified in conjunction with
fea681da 431.BR O_CREAT ,
f4b9d6a5
MK
432and
433.I pathname
434already exists, then
1c1e15ed 435.BR open ()
26cd31fd
MK
436fails with the error
437.BR EEXIST .
5355ff82 438.IP
f4b9d6a5
MK
439When these two flags are specified, symbolic links are not followed:
440.\" POSIX.1-2001 explicitly requires this behavior.
441if
442.I pathname
443is a symbolic link, then
444.BR open ()
43116169 445fails regardless of where the symbolic link points.
5355ff82 446.IP
10b7a945
IHV
447In general, the behavior of
448.B O_EXCL
449is undefined if it is used without
450.BR O_CREAT .
451There is one exception: on Linux 2.6 and later,
452.B O_EXCL
453can be used without
454.B O_CREAT
455if
456.I pathname
457refers to a block device.
6303d401
DB
458If the block device is in use by the system (e.g., mounted),
459.BR open ()
10b7a945
IHV
460fails with the error
461.BR EBUSY .
5355ff82 462.IP
efe08656 463On NFS,
f4b9d6a5 464.B O_EXCL
33a0ccb2 465is supported only when using NFSv3 or later on kernel 2.6 or later.
efe08656 466In NFS environments where
fea681da 467.B O_EXCL
f4b9d6a5
MK
468support is not provided, programs that rely on it
469for performing locking tasks will contain a race condition.
470Portable programs that want to perform atomic file locking using a lockfile,
471and need to avoid reliance on NFS support for
472.BR O_EXCL ,
473can create a unique file on
9ee4a2b6 474the same filesystem (e.g., incorporating hostname and PID), and use
fea681da 475.BR link (2)
c13182ef 476to make a link to the lockfile.
60a90ecd
MK
477If
478.BR link (2)
f4b9d6a5 479returns 0, the lock is successful.
c13182ef 480Otherwise, use
fea681da
MK
481.BR stat (2)
482on the unique file to check if its link count has increased to 2,
483in which case the lock is also successful.
484.TP
1c1e15ed
MK
485.B O_LARGEFILE
486(LFS)
487Allow files whose sizes cannot be represented in an
8478ee02 488.I off_t
1c1e15ed 489(but can be represented in an
8478ee02 490.IR off64_t )
1c1e15ed 491to be opened.
c13182ef 492The
bcdd964e 493.B _LARGEFILE64_SOURCE
e417acb0
MK
494macro must be defined
495(before including
496.I any
497header files)
498in order to obtain this definition.
c13182ef 499Setting the
bcdd964e 500.B _FILE_OFFSET_BITS
9f3d8b28
MK
501feature test macro to 64 (rather than using
502.BR O_LARGEFILE )
12e263f1 503is the preferred
9f3d8b28 504method of accessing large files on 32-bit systems (see
2dcbf4f7 505.BR feature_test_macros (7)).
1c1e15ed 506.TP
31c1f2b0 507.BR O_NOATIME " (since Linux 2.6.8)"
1bb72c96
MK
508Do not update the file last access time
509.RI ( st_atime
510in the inode)
310b7919 511when the file is
1c1e15ed 512.BR read (2).
5355ff82 513.IP
47c906e5
MK
514This flag can be employed only if one of the following conditions is true:
515.RS
516.IP * 3
517The effective UID of the process
518.\" Strictly speaking: the filesystem UID
519matches the owner UID of the file.
520.IP *
521The calling process has the
522.BR CAP_FOWNER
523capability in its user namespace and
524the owner UID of the file has a mapping in the namespace.
525.RE
526.IP
1c1e15ed
MK
527This flag is intended for use by indexing or backup programs,
528where its use can significantly reduce the amount of disk activity.
9ee4a2b6 529This flag may not be effective on all filesystems.
1c1e15ed 530One example is NFS, where the server maintains the access time.
0e1ad98c 531.\" The O_NOATIME flag also affects the treatment of st_atime
92057f4d 532.\" by mmap() and readdir(2), MTK, Dec 04.
1c1e15ed 533.TP
fea681da
MK
534.B O_NOCTTY
535If
536.I pathname
5503c85e 537refers to a terminal device\(emsee
1bb72c96
MK
538.BR tty (4)\(emit
539will not become the process's controlling terminal even if the
fea681da
MK
540process does not have one.
541.TP
1c1e15ed 542.B O_NOFOLLOW
7a11fc63
MK
543If the trailing component (i.e., basename) of
544.I pathname
545is a symbolic link, then the open fails, with the error
6ccb7137 546.BR ELOOP .
7fba0065
MK
547Symbolic links in earlier components of the pathname will still be
548followed.
549(Note that the
550.B ELOOP
551error that can occur in this case is indistinguishable from the case where
6ccb7137
MK
552an open fails because there are too many symbolic links found
553while resolving components in the prefix part of the pathname.)
5355ff82 554.IP
8db11e23
MK
555This flag is a FreeBSD extension, which was added to Linux in version 2.1.126,
556and has subsequently been standardized in POSIX.1-2008.
5355ff82 557.IP
1135dbe1 558See also
843068bd 559.BR O_PATH
1135dbe1 560below.
e366dbc4
MK
561.\" The headers from glibc 2.0.100 and later include a
562.\" definition of this flag; \fIkernels before 2.1.126 will ignore it if
a8d55537 563.\" used\fP.
fea681da
MK
564.TP
565.BR O_NONBLOCK " or " O_NDELAY
ff40dbb3 566When possible, the file is opened in nonblocking mode.
c13182ef 567Neither the
1c1e15ed 568.BR open ()
b0972b3b 569nor any subsequent I/O operations on the file descriptor which is
fea681da 570returned will cause the calling process to wait.
5355ff82 571.IP
f3fdbe28 572Note that the setting of this flag has no effect on the operation of
f2a11072
MK
573.BR poll (2),
574.BR select (2),
575.BR epoll (7),
576and similar,
577since those interfaces merely inform the caller about whether
578a file descriptor is "ready",
579meaning that an I/O operation performed on
580the file descriptor with the
581.B O_NONBLOCK
582flag
583.I clear
584would not block.
585.IP
9f629381
MK
586Note that this flag has no effect for regular files and block devices;
587that is, I/O operations will (briefly) block when device activity
588is required, regardless of whether
589.B O_NONBLOCK
590is set.
591Since
592.B O_NONBLOCK
593semantics might eventually be implemented,
594applications should not depend upon blocking behavior
595when specifying this flag for regular files and block devices.
5355ff82 596.IP
fea681da 597For the handling of FIFOs (named pipes), see also
af5b2ef2 598.BR fifo (7).
db28bfac 599For a discussion of the effect of
0daa9e92 600.B O_NONBLOCK
db28bfac
MK
601in conjunction with mandatory file locks and with file leases, see
602.BR fcntl (2).
fea681da 603.TP
1135dbe1
MK
604.BR O_PATH " (since Linux 2.6.39)"
605.\" commit 1abf0c718f15a56a0a435588d1b104c7a37dc9bd
606.\" commit 326be7b484843988afe57566b627fb7a70beac56
607.\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
608.\"
609.\" http://thread.gmane.org/gmane.linux.man/2790/focus=3496
610.\" Subject: Re: [PATCH] open(2): document O_PATH
611.\" Newsgroups: gmane.linux.man, gmane.linux.kernel
612.\"
1135dbe1 613Obtain a file descriptor that can be used for two purposes:
9ee4a2b6 614to indicate a location in the filesystem tree and
1135dbe1
MK
615to perform operations that act purely at the file descriptor level.
616The file itself is not opened, and other file operations (e.g.,
617.BR read (2),
618.BR write (2),
619.BR fchmod (2),
620.BR fchown (2),
2510e4e5 621.BR fgetxattr (2),
97a45d02 622.BR ioctl (2),
2510e4e5 623.BR mmap (2))
1135dbe1
MK
624fail with the error
625.BR EBADF .
5355ff82 626.IP
1135dbe1
MK
627The following operations
628.I can
629be performed on the resulting file descriptor:
630.RS
631.IP * 3
b9307a4a
MK
632.BR close (2).
633.IP *
f3cd742c
MK
634.BR fchdir (2),
635if the file descriptor refers to a directory
b9307a4a 636(since Linux 3.5).
1135dbe1 637.\" commit 332a2e1244bd08b9e3ecd378028513396a004a24
b9307a4a 638.IP *
1135dbe1 639.BR fstat (2)
b9307a4a
MK
640(since Linux 3.6).
641.IP *
1135dbe1 642.\" fstat(): commit 55815f70147dcfa3ead5738fd56d3574e2e3c1c2
97a45d02
N
643.BR fstatfs (2)
644(since Linux 3.12).
645.\" fstatfs(): commit 9d05746e7b16d8565dddbe3200faa1e669d23bbf
1135dbe1
MK
646.IP *
647Duplicating the file descriptor
648.RB ( dup (2),
649.BR fcntl (2)
650.BR F_DUPFD ,
651etc.).
652.IP *
653Getting and setting file descriptor flags
654.RB ( fcntl (2)
655.BR F_GETFD
656and
657.BR F_SETFD ).
09f677a3
MK
658.IP *
659Retrieving open file status flags using the
660.BR fcntl (2)
13a082cb 661.BR F_GETFL
09f677a3
MK
662operation: the returned flags will include the bit
663.BR O_PATH .
1135dbe1
MK
664.IP *
665Passing the file descriptor as the
666.IR dirfd
667argument of
490f876a 668.BR openat ()
1135dbe1 669and the other "*at()" system calls.
7dee406b
AL
670This includes
671.BR linkat (2)
672with
0da5e58a 673.BR AT_EMPTY_PATH
7dee406b
AL
674(or via procfs using
675.BR AT_SYMLINK_FOLLOW )
676even if the file is not a directory.
1135dbe1
MK
677.IP *
678Passing the file descriptor to another process via a UNIX domain socket
679(see
680.BR SCM_RIGHTS
681in
682.BR unix (7)).
683.RE
684.IP
685When
686.B O_PATH
687is specified in
688.IR flags ,
689flag bits other than
6807fc6f
MK
690.BR O_CLOEXEC ,
691.BR O_DIRECTORY ,
1135dbe1
MK
692and
693.BR O_NOFOLLOW
694are ignored.
5355ff82 695.IP
4a3b9ffc
MK
696Opening a file or directory with the
697.B O_PATH
698flag requires no permissions on the object itself
699(but does require execute permission on the directories in the path prefix).
700Depending on the subsequent operation,
701a check for suitable file permissions may be performed (e.g.,
702.BR fchdir (2)
703requires execute permission on the directory referred to
704by its file descriptor argument).
705By contrast,
706obtaining a reference to a filesystem object by opening it with the
707.B O_RDONLY
708flag requires that the caller have read permission on the object,
709even when the subsequent operation (e.g.,
710.BR fchdir (2),
711.BR fstat (2))
712does not require read permission on the object.
713.IP
d30344ab
MK
714If
715.I pathname
716is a symbolic link and the
1135dbe1
MK
717.BR O_NOFOLLOW
718flag is also specified,
719then the call returns a file descriptor referring to the symbolic link.
720This file descriptor can be used as the
721.I dirfd
722argument in calls to
723.BR fchownat (2),
724.BR fstatat (2),
725.BR linkat (2),
726and
727.BR readlinkat (2)
728with an empty pathname to have the calls operate on the symbolic link.
5355ff82 729.IP
97a45d02
N
730If
731.I pathname
732refers to an automount point that has not yet been triggered, so no
733other filesystem is mounted on it, then the call returns a file
734descriptor referring to the automount directory without triggering a mount.
735.BR fstatfs (2)
736can then be used to determine if it is, in fact, an untriggered
737automount point
738.RB ( ".f_type == AUTOFS_SUPER_MAGIC" ).
d1304ede
MK
739.IP
740One use of
741.B O_PATH
742for regular files is to provide the equivalent of POSIX.1's
743.B O_EXEC
744functionality.
745This permits us to open a file for which we have execute
ebab32e1 746permission but not read permission, and then execute that file,
d1304ede
MK
747with steps something like the following:
748.IP
749.in +4n
750.EX
751char buf[PATH_MAX];
752fd = open("some_prog", O_PATH);
8e13d566 753snprintf(buf, PATH_MAX, "/proc/self/fd/%d", fd);
d1304ede
MK
754execl(buf, "some_prog", (char *) NULL);
755.EE
756.in
e982cebf
MK
757.IP
758An
759.B O_PATH
760file descriptor can also be passed as the argument of
761.BR fexecve (3).
1135dbe1 762.TP
fea681da 763.B O_SYNC
6cf19e62
MK
764Write operations on the file will complete according to the requirements of
765synchronized I/O
766.I file
767integrity completion
f36a1468 768(by contrast with the
6cf19e62
MK
769synchronized I/O
770.I data
771integrity completion
772provided by
773.BR O_DSYNC .)
5355ff82 774.IP
6cf19e62
MK
775By the time
776.BR write (2)
ca20a8a5
MK
777(or similar)
778returns, the output data and associated file metadata
6cf19e62
MK
779have been transferred to the underlying hardware
780(i.e., as though each
781.BR write (2)
782was followed by a call to
783.BR fsync (2)).
784.IR "See NOTES below" .
fea681da 785.TP
40398c1a
MK
786.BR O_TMPFILE " (since Linux 3.11)"
787.\" commit 60545d0d4610b02e55f65d141c95b18ccf855b6e
788.\" commit f4e0c30c191f87851c4a53454abb55ee276f4a7e
789.\" commit bb458c644a59dbba3a1fe59b27106c5e68e1c4bd
6a11a5d4 790Create an unnamed temporary regular file.
40398c1a
MK
791The
792.I pathname
793argument specifies a directory;
794an unnamed inode will be created in that directory's filesystem.
795Anything written to the resulting file will be lost when
796the last file descriptor is closed, unless the file is given a name.
5355ff82 797.IP
40398c1a
MK
798.B O_TMPFILE
799must be specified with one of
800.B O_RDWR
801or
802.B O_WRONLY
803and, optionally,
804.BR O_EXCL .
805If
806.B O_EXCL
807is not specified, then
808.BR linkat (2)
809can be used to link the temporary file into the filesystem, making it
810permanent, using code like the following:
5355ff82 811.IP
40398c1a 812.in +4n
5355ff82 813.EX
40398c1a
MK
814char path[PATH_MAX];
815fd = open("/path/to/dir", O_TMPFILE | O_RDWR,
0fb83d00
MK
816 S_IRUSR | S_IWUSR);
817
40398c1a 818/* File I/O on 'fd'... */
0fb83d00 819
fc29a199 820linkat(fd, NULL, AT_FDCWD, "/path/for/file", AT_EMPTY_PATH);
a2587fbb
MK
821
822/* If the caller doesn't have the CAP_DAC_READ_SEARCH
823 capability (needed to use AT_EMPTY_PATH with linkat(2)),
824 and there is a proc(5) filesystem mounted, then the
825 linkat(2) call above can be replaced with:
826
827snprintf(path, PATH_MAX, "/proc/self/fd/%d", fd);
828linkat(AT_FDCWD, path, AT_FDCWD, "/path/for/file",
829 AT_SYMLINK_FOLLOW);
830*/
5355ff82 831.EE
40398c1a 832.in
5355ff82 833.IP
40398c1a
MK
834In this case,
835the
836.BR open ()
837.I mode
838argument determines the file permission mode, as with
839.BR O_CREAT .
5355ff82 840.IP
0115aaed
MK
841Specifying
842.B O_EXCL
843in conjunction with
844.B O_TMPFILE
845prevents a temporary file from being linked into the filesystem
846in the above manner.
847(Note that the meaning of
848.B O_EXCL
849in this case is different from the meaning of
850.B O_EXCL
851otherwise.)
5355ff82 852.IP
40398c1a
MK
853There are two main use cases for
854.\" Inspired by http://lwn.net/Articles/559147/
855.BR O_TMPFILE :
856.RS
857.IP * 3
858Improved
859.BR tmpfile (3)
860functionality: race-free creation of temporary files that
861(1) are automatically deleted when closed;
862(2) can never be reached via any pathname;
863(3) are not subject to symlink attacks; and
864(4) do not require the caller to devise unique names.
865.IP *
866Creating a file that is initially invisible, which is then populated
8b04592d 867with data and adjusted to have appropriate filesystem attributes
c89a9937
EB
868.RB ( fchown (2),
869.BR fchmod (2),
40398c1a
MK
870.BR fsetxattr (2),
871etc.)
872before being atomically linked into the filesystem
873in a fully formed state (using
874.BR linkat (2)
875as described above).
876.RE
877.IP
878.B O_TMPFILE
879requires support by the underlying filesystem;
40398c1a 880only a subset of Linux filesystems provide that support.
cde2074a 881In the initial implementation, support was provided in
9af6b115 882the ext2, ext3, ext4, UDF, Minix, and shmem filesystems.
bd79a35a 883.\" To check for support, grep for "tmpfile" in kernel sources
6065b906
MK
884Support for other filesystems has subsequently been added as follows:
885XFS (Linux 3.15);
cde2074a
MK
886.\" commit 99b6436bc29e4f10e4388c27a3e4810191cc4788
887.\" commit ab29743117f9f4c22ac44c13c1647fb24fb2bafe
1b9d5819 888Btrfs (Linux 3.16);
e746db2e 889.\" commit ef3b9af50bfa6a1f02cd7b3f5124b712b1ba3e3c
6065b906 890F2FS (Linux 3.16);
bd79a35a 891.\" commit 50732df02eefb39ab414ef655979c2c9b64ad21c
6065b906 892and ubifs (Linux 4.9)
40398c1a 893.TP
1c1e15ed 894.B O_TRUNC
4d61d36a 895If the file already exists and is a regular file and the access mode allows
682edefb
MK
896writing (i.e., is
897.B O_RDWR
898or
899.BR O_WRONLY )
900it will be truncated to length 0.
901If the file is a FIFO or terminal device file, the
902.B O_TRUNC
c13182ef 903flag is ignored.
2b9b829d 904Otherwise, the effect of
682edefb
MK
905.B O_TRUNC
906is unspecified.
7b8ba76c 907.SS creat()
1f7191bb 908A call to
1c1e15ed 909.BR creat ()
1f7191bb 910is equivalent to calling
1c1e15ed 911.BR open ()
fea681da
MK
912with
913.I flags
914equal to
915.BR O_CREAT|O_WRONLY|O_TRUNC .
7b8ba76c
MK
916.SS openat()
917The
918.BR openat ()
919system call operates in exactly the same way as
cadd38ba 920.BR open (),
7b8ba76c 921except for the differences described here.
3130d10b 922.PP
7b8ba76c
MK
923If the pathname given in
924.I pathname
925is relative, then it is interpreted relative to the directory
3ad65ff0 926referred to by the file descriptor
7b8ba76c
MK
927.I dirfd
928(rather than relative to the current working directory of
929the calling process, as is done by
cadd38ba 930.BR open ()
7b8ba76c 931for a relative pathname).
3130d10b 932.PP
7b8ba76c
MK
933If
934.I pathname
935is relative and
936.I dirfd
937is the special value
938.BR AT_FDCWD ,
939then
940.I pathname
941is interpreted relative to the current working
942directory of the calling process (like
cadd38ba 943.BR open ()).
3130d10b 944.PP
7b8ba76c
MK
945If
946.I pathname
947is absolute, then
948.I dirfd
949is ignored.
4b322a2f 950.\"
a2dbb2e3
AS
951.SS openat2(2)
952The
953.BR openat2 (2)
954system call is an extension of
955.BR openat (),
4b322a2f
MK
956and provides a superset of the features of
957.BR openat ().
958It is separate documented, in
959.BR openat2 (2).
47297adb 960.SH RETURN VALUE
7b8ba76c
MK
961.BR open (),
962.BR openat (),
c13182ef 963and
e1d6264d 964.BR creat ()
ed09120a
MK
965return the new file descriptor (a nonnegative integer),
966or \-1 if an error occurred
1c1e15ed 967(in which case,
fea681da
MK
968.I errno
969is set appropriately).
fea681da 970.SH ERRORS
7b8ba76c
MK
971.BR open (),
972.BR openat (),
973and
974.BR creat ()
975can fail with the following errors:
fea681da
MK
976.TP
977.B EACCES
978The requested access to the file is not allowed, or search permission
979is denied for one of the directories in the path prefix of
980.IR pathname ,
981or the file did not exist yet and write access to the parent directory
982is not allowed.
983(See also
ad7cc990 984.BR path_resolution (7).)
fea681da 985.TP
2ddf885a
JS
986.B EACCES
987.\" commit 30aba6656f61ed44cba445a3c0d38b296fa9e8f5
988Where
989.B O_CREAT
990is specified, the fs.protected_fifos or fs.protected_regular sysctl is
991enabled, the file already exists and is a FIFO or regular file, the
992owner of the file is neither the current user nor the owner of the
993containing directory, and the containing directory is both world- or
994group-writable and sticky.
995.TP
a1f01685
MH
996.B EDQUOT
997Where
998.B O_CREAT
999is specified, the file does not exist, and the user's quota of disk
9ee4a2b6 1000blocks or inodes on the filesystem has been exhausted.
a1f01685 1001.TP
fea681da
MK
1002.B EEXIST
1003.I pathname
1004already exists and
1005.BR O_CREAT " and " O_EXCL
1006were used.
1007.TP
1008.B EFAULT
0daa9e92 1009.I pathname
e1d6264d 1010points outside your accessible address space.
fea681da 1011.TP
9f5773f7 1012.B EFBIG
7c7fb552
MK
1013See
1014.BR EOVERFLOW .
9f5773f7 1015.TP
e51412ea
MK
1016.B EINTR
1017While blocked waiting to complete an open of a slow device
1018(e.g., a FIFO; see
1019.BR fifo (7)),
1020the call was interrupted by a signal handler; see
1021.BR signal (7).
1022.TP
ef490193
DG
1023.B EINVAL
1024The filesystem does not support the
1025.BR O_DIRECT
e6f89ed2
MK
1026flag.
1027See
ef490193
DG
1028.BR NOTES
1029for more information.
1030.TP
8e335391
MK
1031.B EINVAL
1032Invalid value in
1033.\" In particular, __O_TMPFILE instead of O_TMPFILE
1034.IR flags .
1035.TP
1036.B EINVAL
1037.B O_TMPFILE
1038was specified in
1039.IR flags ,
1040but neither
1041.B O_WRONLY
1042nor
1043.B O_RDWR
1044was specified.
1045.TP
5c6f8de0
MK
1046.B EINVAL
1047.B O_CREAT
1048was specified in
1049.I flags
1050and the final component ("basename") of the new file's
1051.I pathname
1052is invalid
1053(e.g., it contains characters not permitted by the underlying filesystem).
ed6fe005
MK
1054.TP
1055 .TP
1056.B EINVAL
1057The final component ("basename") of
1058.I pathname
1059is invalid
1060(e.g., it contains characters not permitted by the underlying filesystem).
5c6f8de0 1061.TP
fea681da
MK
1062.B EISDIR
1063.I pathname
1064refers to a directory and the access requested involved writing
1065(that is,
1066.B O_WRONLY
1067or
1068.B O_RDWR
1069is set).
1070.TP
8e335391 1071.B EISDIR
843068bd
MK
1072.I pathname
1073refers to an existing directory,
8e335391
MK
1074.B O_TMPFILE
1075and one of
1076.B O_WRONLY
1077or
1078.B O_RDWR
1079were specified in
1080.IR flags ,
1081but this kernel version does not provide the
1082.B O_TMPFILE
1083functionality.
1084.TP
fea681da
MK
1085.B ELOOP
1086Too many symbolic links were encountered in resolving
289f7907
MK
1087.IR pathname .
1088.TP
1089.B ELOOP
fea681da 1090.I pathname
289f7907
MK
1091was a symbolic link, and
1092.I flags
1093specified
1094.BR O_NOFOLLOW
1095but not
1096.BR O_PATH .
fea681da
MK
1097.TP
1098.B EMFILE
26c32fab 1099The per-process limit on the number of open file descriptors has been reached
12c21590
MK
1100(see the description of
1101.BR RLIMIT_NOFILE
1102in
1103.BR getrlimit (2)).
fea681da
MK
1104.TP
1105.B ENAMETOOLONG
0daa9e92 1106.I pathname
e1d6264d 1107was too long.
fea681da
MK
1108.TP
1109.B ENFILE
e258766b 1110The system-wide limit on the total number of open files has been reached.
fea681da
MK
1111.TP
1112.B ENODEV
1113.I pathname
1114refers to a device special file and no corresponding device exists.
682edefb
MK
1115(This is a Linux kernel bug; in this situation
1116.B ENXIO
1117must be returned.)
fea681da
MK
1118.TP
1119.B ENOENT
682edefb
MK
1120.B O_CREAT
1121is not set and the named file does not exist.
115bbafa
MK
1122.TP
1123.B ENOENT
1124A directory component in
fea681da
MK
1125.I pathname
1126does not exist or is a dangling symbolic link.
1127.TP
ba03011f
MK
1128.B ENOENT
1129.I pathname
1130refers to a nonexistent directory,
1131.B O_TMPFILE
1132and one of
1133.B O_WRONLY
1134or
1135.B O_RDWR
1136were specified in
1137.IR flags ,
1138but this kernel version does not provide the
1139.B O_TMPFILE
1140functionality.
1141.TP
fea681da 1142.B ENOMEM
8ef529f9
MK
1143The named file is a FIFO,
1144but memory for the FIFO buffer can't be allocated because
1145the per-user hard limit on memory allocation for pipes has been reached
1146and the caller is not privileged; see
1147.BR pipe (7).
1148.TP
1149.B ENOMEM
fea681da
MK
1150Insufficient kernel memory was available.
1151.TP
1152.B ENOSPC
1153.I pathname
1154was to be created but the device containing
1155.I pathname
1156has no room for the new file.
1157.TP
1158.B ENOTDIR
1159A component used as a directory in
1160.I pathname
a8d55537 1161is not, in fact, a directory, or \fBO_DIRECTORY\fP was specified and
fea681da
MK
1162.I pathname
1163was not a directory.
1164.TP
1165.B ENXIO
682edefb 1166.BR O_NONBLOCK " | " O_WRONLY
103ea4f6
MK
1167is set, the named file is a FIFO, and
1168no process has the FIFO open for reading.
7b032b23
MK
1169.TP
1170.B ENXIO
1171The file is a device special file and no corresponding device exists.
fea681da 1172.TP
71b12d0a 1173.B ENXIO
8b5bbcfa 1174The file is a UNIX domain socket.
71b12d0a 1175.TP
bbe02b45
MK
1176.BR EOPNOTSUPP
1177The filesystem containing
1178.I pathname
1179does not support
1180.BR O_TMPFILE .
1181.TP
7c7fb552
MK
1182.B EOVERFLOW
1183.I pathname
1184refers to a regular file that is too large to be opened.
1185The usual scenario here is that an application compiled
1186on a 32-bit platform without
5e4dc269 1187.I -D_FILE_OFFSET_BITS=64
7c7fb552 1188tried to open a file whose size exceeds
4e1a4d72
MK
1189.I (1<<31)-1
1190bytes;
7c7fb552
MK
1191see also
1192.B O_LARGEFILE
1193above.
c84d3aa3 1194This is the error specified by POSIX.1;
7c7fb552
MK
1195in kernels before 2.6.24, Linux gave the error
1196.B EFBIG
1197for this case.
1198.\" See http://bugzilla.kernel.org/show_bug.cgi?id=7253
1199.\" "Open of a large file on 32-bit fails with EFBIG, should be EOVERFLOW"
1200.\" Reported 2006-10-03
1201.TP
1c1e15ed
MK
1202.B EPERM
1203The
1204.B O_NOATIME
1205flag was specified, but the effective user ID of the caller
9ee4a2b6 1206.\" Strictly speaking, it's the filesystem UID... (MTK)
47c906e5 1207did not match the owner of the file and the caller was not privileged.
1c1e15ed 1208.TP
fbab10e5
MK
1209.B EPERM
1210The operation was prevented by a file seal; see
1211.BR fcntl (2).
1212.TP
fea681da
MK
1213.B EROFS
1214.I pathname
9ee4a2b6 1215refers to a file on a read-only filesystem and write access was
fea681da
MK
1216requested.
1217.TP
1218.B ETXTBSY
1219.I pathname
1220refers to an executable image which is currently being executed and
1221write access was requested.
d3952311 1222.TP
19d37126
JH
1223.B ETXTBSY
1224.I pathname
1225refers to a file that is currently in use as a swap file, and the
1226.B O_TRUNC
1227flag was specified.
1228.TP
1229.B ETXTBSY
1230.I pathname
1231refers to a file that is currently being read by the kernel (e.g. for
1232module/firmware loading), and write access was requested.
1233.TP
d3952311
MK
1234.B EWOULDBLOCK
1235The
1236.B O_NONBLOCK
1237flag was specified, and an incompatible lease was held on the file
1238(see
1239.BR fcntl (2)).
7b8ba76c
MK
1240.PP
1241The following additional errors can occur for
1242.BR openat ():
1243.TP
1244.B EBADF
1245.I dirfd
1246is not a valid file descriptor.
1247.TP
1248.B ENOTDIR
1249.I pathname
2feae602 1250is a relative pathname and
7b8ba76c
MK
1251.I dirfd
1252is a file descriptor referring to a file other than a directory.
1253.SH VERSIONS
1254.BR openat ()
1255was added to Linux in kernel 2.6.16;
1256library support was added to glibc in version 2.4.
47297adb 1257.SH CONFORMING TO
7b8ba76c
MK
1258.BR open (),
1259.BR creat ()
72ac7268 1260SVr4, 4.3BSD, POSIX.1-2001, POSIX.1-2008.
5355ff82 1261.PP
7b8ba76c
MK
1262.BR openat ():
1263POSIX.1-2008.
5355ff82 1264.PP
a2dbb2e3
AS
1265.BR openat2 (2)
1266is Linux-specific.
1267.PP
fea681da 1268The
72ac7268 1269.BR O_DIRECT ,
1c1e15ed 1270.BR O_NOATIME ,
72ac7268 1271.BR O_PATH ,
fea681da 1272and
72ac7268
MK
1273.BR O_TMPFILE
1274flags are Linux-specific.
1275One must define
61b7c1e1
MK
1276.B _GNU_SOURCE
1277to obtain their definitions.
5355ff82 1278.PP
9f91e36c 1279The
72ac7268
MK
1280.BR O_CLOEXEC ,
1281.BR O_DIRECTORY ,
1282and
1283.BR O_NOFOLLOW
1284flags are not specified in POSIX.1-2001,
1285but are specified in POSIX.1-2008.
1286Since glibc 2.12, one can obtain their definitions by defining either
1287.B _POSIX_C_SOURCE
1288with a value greater than or equal to 200809L or
1289.BR _XOPEN_SOURCE
1290with a value greater than or equal to 700.
1291In glibc 2.11 and earlier, one obtains the definitions by defining
1292.BR _GNU_SOURCE .
5355ff82 1293.PP
72ac7268
MK
1294As noted in
1295.BR feature_test_macros (7),
84fc2a6e 1296feature test macros such as
72ac7268
MK
1297.BR _POSIX_C_SOURCE ,
1298.BR _XOPEN_SOURCE ,
1299and
fe75ec04 1300.B _GNU_SOURCE
72ac7268 1301must be defined before including
e417acb0 1302.I any
72ac7268 1303header files.
a1d5f77c 1304.SH NOTES
988db661 1305Under Linux, the
a1d5f77c 1306.B O_NONBLOCK
3897a3f8 1307flag is sometimes used in cases where one wants to open
a1d5f77c 1308but does not necessarily have the intention to read or write.
3897a3f8
MK
1309For example,
1310this may be used to open a device in order to get a file descriptor
a1d5f77c
MK
1311for use with
1312.BR ioctl (2).
dd3568a1 1313.PP
fea681da
MK
1314The (undefined) effect of
1315.B O_RDONLY | O_TRUNC
c13182ef 1316varies among implementations.
bcdd964e 1317On many systems the file is actually truncated.
fea681da
MK
1318.\" Linux 2.0, 2.5: truncate
1319.\" Solaris 5.7, 5.8: truncate
1320.\" Irix 6.5: truncate
1321.\" Tru64 5.1B: truncate
1322.\" HP-UX 11.22: truncate
1323.\" FreeBSD 4.7: truncate
5355ff82 1324.PP
5dc8986d
MK
1325Note that
1326.BR open ()
1327can open device special files, but
1328.BR creat ()
1329cannot create them; use
1330.BR mknod (2)
1331instead.
5355ff82 1332.PP
5dc8986d
MK
1333If the file is newly created, its
1334.IR st_atime ,
1335.IR st_ctime ,
1336.I st_mtime
1337fields
1338(respectively, time of last access, time of last status change, and
1339time of last modification; see
1340.BR stat (2))
1341are set
1342to the current time, and so are the
1343.I st_ctime
1344and
1345.I st_mtime
1346fields of the
1347parent directory.
1348Otherwise, if the file is modified because of the
1349.B O_TRUNC
3a9c5a29
MK
1350flag, its
1351.I st_ctime
1352and
1353.I st_mtime
1354fields are set to the current time.
5355ff82 1355.PP
aaf7a574
MK
1356The files in the
1357.I /proc/[pid]/fd
1358directory show the open file descriptors of the process with the PID
1359.IR pid .
1360The files in the
1361.I /proc/[pid]/fdinfo
d40e0bfc 1362directory show even more information about these file descriptors.
aaf7a574
MK
1363See
1364.BR proc (5)
1365for further details of both of these directories.
8132c115 1366.PP
319e9b31 1367The Linux header file
8132c115
ES
1368.B <asm/fcntl.h>
1369doesn't define
1370.BR O_ASYNC ;
319e9b31 1371the (BSD-derived)
8132c115 1372.B FASYNC
319e9b31 1373synonym is defined instead.
5dc8986d
MK
1374.\"
1375.\"
d20d9d33
MK
1376.SS Open file descriptions
1377The term open file description is the one used by POSIX to refer to the
1378entries in the system-wide table of open files.
91085d85 1379In other contexts, this object is
d20d9d33
MK
1380variously also called an "open file object",
1381a "file handle", an "open file table entry",
1382or\(emin kernel-developer parlance\(ema
1383.IR "struct file" .
5355ff82 1384.PP
d20d9d33
MK
1385When a file descriptor is duplicated (using
1386.BR dup (2)
1387or similar),
1388the duplicate refers to the same open file description
1389as the original file descriptor,
1390and the two file descriptors consequently share
1391the file offset and file status flags.
1392Such sharing can also occur between processes:
1393a child process created via
91085d85 1394.BR fork (2)
d20d9d33
MK
1395inherits duplicates of its parent's file descriptors,
1396and those duplicates refer to the same open file descriptions.
5355ff82 1397.PP
d20d9d33 1398Each
bf7bc8b8 1399.BR open ()
d20d9d33
MK
1400of a file creates a new open file description;
1401thus, there may be multiple open file descriptions
1402corresponding to a file inode.
5355ff82 1403.PP
9539ebc9
MK
1404On Linux, one can use the
1405.BR kcmp (2)
1406.B KCMP_FILE
1407operation to test whether two file descriptors
1408(in the same process or in two different processes)
1409refer to the same open file description.
d20d9d33
MK
1410.\"
1411.\"
5dc8986d 1412.SS Synchronized I/O
6cf19e62
MK
1413The POSIX.1-2008 "synchronized I/O" option
1414specifies different variants of synchronized I/O,
1415and specifies the
1416.BR open ()
1417flags
015221ef
CH
1418.BR O_SYNC ,
1419.BR O_DSYNC ,
1420and
6cf19e62
MK
1421.BR O_RSYNC
1422for controlling the behavior.
1423Regardless of whether an implementation supports this option,
1424it must at least support the use of
1425.BR O_SYNC
1426for regular files.
5355ff82 1427.PP
89851a00 1428Linux implements
6cf19e62
MK
1429.BR O_SYNC
1430and
1431.BR O_DSYNC ,
1432but not
015221ef 1433.BR O_RSYNC .
352c4c5c 1434Somewhat incorrectly, glibc defines
6cf19e62
MK
1435.BR O_RSYNC
1436to have the same value as
352c4c5c
MK
1437.BR O_SYNC .
1438.RB ( O_RSYNC
1439is defined in the Linux header file
1440.I <asm/fcntl.h>
1441on HP PA-RISC, but it is not used.)
5355ff82 1442.PP
6cf19e62
MK
1443.BR O_SYNC
1444provides synchronized I/O
1445.I file
1446integrity completion,
1447meaning write operations will flush data and all associated metadata
1448to the underlying hardware.
1449.BR O_DSYNC
1450provides synchronized I/O
1451.I data
1452integrity completion,
1453meaning write operations will flush data
1454to the underlying hardware,
1455but will only flush metadata updates that are required
1456to allow a subsequent read operation to complete successfully.
1457Data integrity completion can reduce the number of disk operations
1458that are required for applications that don't need the guarantees
1459of file integrity completion.
5355ff82 1460.PP
a83923ca 1461To understand the difference between the two types of completion,
6cf19e62
MK
1462consider two pieces of file metadata:
1463the file last modification timestamp
1464.RI ( st_mtime )
1465and the file length.
1466All write operations will update the last file modification timestamp,
1467but only writes that add data to the end of the
1468file will change the file length.
1469The last modification timestamp is not needed to ensure that
1470a read completes successfully, but the file length is.
1471Thus,
1472.BR O_DSYNC
1473would only guarantee to flush updates to the file length metadata
1474(whereas
1475.BR O_SYNC
1476would also always flush the last modification timestamp metadata).
5355ff82 1477.PP
6cf19e62
MK
1478Before Linux 2.6.33, Linux implemented only the
1479.BR O_SYNC
89851a00 1480flag for
6cf19e62
MK
1481.BR open ().
1482However, when that flag was specified,
1483most filesystems actually provided the equivalent of synchronized I/O
1484.I data
1485integrity completion (i.e.,
1486.BR O_SYNC
1487was actually implemented as the equivalent of
1488.BR O_DSYNC ).
5355ff82 1489.PP
6cf19e62
MK
1490Since Linux 2.6.33, proper
1491.BR O_SYNC
1492support is provided.
1493However, to ensure backward binary compatibility,
1494.BR O_DSYNC
1495was defined with the same value as the historical
015221ef 1496.BR O_SYNC ,
015221ef 1497and
6cf19e62 1498.BR O_SYNC
89851a00 1499was defined as a new (two-bit) flag value that includes the
6cf19e62
MK
1500.BR O_DSYNC
1501flag value.
1502This ensures that applications compiled against
1503new headers get at least
1504.BR O_DSYNC
1505semantics on pre-2.6.33 kernels.
5dc8986d 1506.\"
76f054b1
MK
1507.SS C library/kernel differences
1508Since version 2.26,
1509the glibc wrapper function for
1510.BR open ()
1511employs the
1512.BR openat ()
1513system call, rather than the kernel's
1514.BR open ()
1515system call.
1516For certain architectures, this is also true in glibc versions before 2.26.
5dc8986d
MK
1517.\"
1518.SS NFS
1519There are many infelicities in the protocol underlying NFS, affecting
1520amongst others
1521.BR O_SYNC " and " O_NDELAY .
5355ff82 1522.PP
9ee4a2b6 1523On NFS filesystems with UID mapping enabled,
a1d5f77c
MK
1524.BR open ()
1525may
75b94dc3 1526return a file descriptor but, for example,
a1d5f77c
MK
1527.BR read (2)
1528requests are denied
1529with \fBEACCES\fP.
1530This is because the client performs
1531.BR open ()
1532by checking the
1533permissions, but UID mapping is performed by the server upon
1534read and write requests.
5dc8986d
MK
1535.\"
1536.\"
1bdc161d
MK
1537.SS FIFOs
1538Opening the read or write end of a FIFO blocks until the other
1539end is also opened (by another process or thread).
1540See
1541.BR fifo (7)
1542for further details.
1543.\"
1544.\"
5dc8986d
MK
1545.SS File access mode
1546Unlike the other values that can be specified in
1547.IR flags ,
1548the
1549.I "access mode"
1550values
1551.BR O_RDONLY ", " O_WRONLY ", and " O_RDWR
1552do not specify individual bits.
1553Rather, they define the low order two bits of
1554.IR flags ,
1555and are defined respectively as 0, 1, and 2.
1556In other words, the combination
1557.B "O_RDONLY | O_WRONLY"
1558is a logical error, and certainly does not have the same meaning as
1559.BR O_RDWR .
5355ff82 1560.PP
5dc8986d
MK
1561Linux reserves the special, nonstandard access mode 3 (binary 11) in
1562.I flags
1563to mean:
d9cb0d7d 1564check for read and write permission on the file and return a file descriptor
5dc8986d
MK
1565that can't be used for reading or writing.
1566This nonstandard access mode is used by some Linux drivers to return a
d9cb0d7d 1567file descriptor that is to be used only for device-specific
5dc8986d
MK
1568.BR ioctl (2)
1569operations.
1570.\" See for example util-linux's disk-utils/setfdprm.c
1571.\" For some background on access mode 3, see
1572.\" http://thread.gmane.org/gmane.linux.kernel/653123
1573.\" "[RFC] correct flags to f_mode conversion in __dentry_open"
1574.\" LKML, 12 Mar 2008
7b8ba76c
MK
1575.\"
1576.\"
80d250b4 1577.SS Rationale for openat() and other "directory file descriptor" APIs
7b8ba76c 1578.BR openat ()
80d250b4
MK
1579and the other system calls and library functions that take
1580a directory file descriptor argument
7b8ba76c 1581(i.e.,
c6a16783 1582.BR execveat (2),
7b8ba76c 1583.BR faccessat (2),
80d250b4 1584.BR fanotify_mark (2),
7b8ba76c
MK
1585.BR fchmodat (2),
1586.BR fchownat (2),
1587.BR fstatat (2),
1588.BR futimesat (2),
1589.BR linkat (2),
1590.BR mkdirat (2),
1591.BR mknodat (2),
80d250b4 1592.BR name_to_handle_at (2),
7b8ba76c
MK
1593.BR readlinkat (2),
1594.BR renameat (2),
3f092cef 1595.BR statx (2),
7b8ba76c
MK
1596.BR symlinkat (2),
1597.BR unlinkat (2),
f37759b1 1598.BR utimensat (2),
80d250b4 1599.BR mkfifoat (3),
7b8ba76c 1600and
80d250b4 1601.BR scandirat (3))
a98e0304 1602address two problems with the older interfaces that preceded them.
92692952 1603Here, the explanation is in terms of the
7b8ba76c 1604.BR openat ()
d26f8a31 1605call, but the rationale is analogous for the other interfaces.
5355ff82 1606.PP
7b8ba76c
MK
1607First,
1608.BR openat ()
1609allows an application to avoid race conditions that could
1610occur when using
cadd38ba 1611.BR open ()
7b8ba76c
MK
1612to open files in directories other than the current working directory.
1613These race conditions result from the fact that some component
1614of the directory prefix given to
cadd38ba 1615.BR open ()
7b8ba76c 1616could be changed in parallel with the call to
cadd38ba 1617.BR open ().
54305f5b 1618Suppose, for example, that we wish to create the file
a710e359 1619.I dir1/dir2/xxx.dep
54305f5b 1620if the file
a710e359 1621.I dir1/dir2/xxx
54305f5b 1622exists.
069d2f9a 1623The problem is that between the existence check and the file-creation step,
a710e359 1624.I dir1
54305f5b 1625or
a710e359 1626.I dir2
54305f5b
MK
1627(which might be symbolic links)
1628could be modified to point to a different location.
7b8ba76c
MK
1629Such races can be avoided by
1630opening a file descriptor for the target directory,
1631and then specifying that file descriptor as the
1632.I dirfd
54305f5b
MK
1633argument of (say)
1634.BR fstatat (2)
1635and
7b8ba76c 1636.BR openat ().
941d2892
MK
1637The use of the
1638.I dirfd
1639file descriptor also has other benefits:
1640.IP * 3
1641the file descriptor is a stable reference to the directory,
1642even if the directory is renamed; and
1643.IP *
1644the open file descriptor prevents the underlying filesystem from
1645being dismounted,
1646just as when a process has a current working directory on a filesystem.
1647.PP
7b8ba76c
MK
1648Second,
1649.BR openat ()
1650allows the implementation of a per-thread "current working
1651directory", via file descriptor(s) maintained by the application.
1652(This functionality can also be obtained by tricks based
1653on the use of
1654.IR /proc/self/fd/ dirfd,
1655but less efficiently.)
1656.\"
1657.\"
ddc4d339 1658.SS O_DIRECT
dd3568a1 1659.PP
ddc4d339
MK
1660The
1661.B O_DIRECT
1662flag may impose alignment restrictions on the length and address
7fac88a9 1663of user-space buffers and the file offset of I/Os.
ddc4d339 1664In Linux alignment
9ee4a2b6 1665restrictions vary by filesystem and kernel version and might be
ddc4d339 1666absent entirely.
9ee4a2b6 1667However there is currently no filesystem\-independent
ddc4d339 1668interface for an application to discover these restrictions for a given
9ee4a2b6
MK
1669file or filesystem.
1670Some filesystems provide their own interfaces
ddc4d339
MK
1671for doing so, for example the
1672.B XFS_IOC_DIOINFO
1673operation in
1674.BR xfsctl (3).
dd3568a1 1675.PP
85c2bdba
MK
1676Under Linux 2.4, transfer sizes, and the alignment of the user buffer
1677and the file offset must all be multiples of the logical block size
9ee4a2b6 1678of the filesystem.
21557928 1679Since Linux 2.6.0, alignment to the logical block size of the
e6042e4a 1680underlying storage (typically 512 bytes) suffices.
21557928 1681The logical block size can be determined using the
e6042e4a
PS
1682.BR ioctl (2)
1683.B BLKSSZGET
21557928 1684operation or from the shell using the command:
5355ff82
MK
1685.PP
1686.EX
21557928 1687 blockdev \-\-getss
5355ff82
MK
1688.EE
1689.PP
1847167b
NP
1690.B O_DIRECT
1691I/Os should never be run concurrently with the
04cd7f64 1692.BR fork (2)
1847167b
NP
1693system call,
1694if the memory buffer is a private mapping
1695(i.e., any mapping created with the
02ace852 1696.BR mmap (2)
1847167b 1697.BR MAP_PRIVATE
0ab8aeec 1698flag;
1847167b
NP
1699this includes memory allocated on the heap and statically allocated buffers).
1700Any such I/Os, whether submitted via an asynchronous I/O interface or from
1701another thread in the process,
1702should be completed before
1703.BR fork (2)
1704is called.
1705Failure to do so can result in data corruption and undefined behavior in
1706parent and child processes.
1707This restriction does not apply when the memory buffer for the
1708.B O_DIRECT
1709I/Os was created using
1710.BR shmat (2)
1711or
1712.BR mmap (2)
1713with the
1714.B MAP_SHARED
1715flag.
1716Nor does this restriction apply when the memory buffer has been advised as
1717.B MADV_DONTFORK
0ab8aeec 1718with
02ace852 1719.BR madvise (2),
1847167b
NP
1720ensuring that it will not be available
1721to the child after
1722.BR fork (2).
dd3568a1 1723.PP
ddc4d339
MK
1724The
1725.B O_DIRECT
1726flag was introduced in SGI IRIX, where it has alignment
1727restrictions similar to those of Linux 2.4.
1728IRIX has also a
1729.BR fcntl (2)
1730call to query appropriate alignments, and sizes.
1731FreeBSD 4.x introduced
1732a flag of the same name, but without alignment restrictions.
dd3568a1 1733.PP
ddc4d339
MK
1734.B O_DIRECT
1735support was added under Linux in kernel version 2.4.10.
1736Older Linux kernels simply ignore this flag.
fedb2ff5 1737Some filesystems may not implement the flag, in which case
ddc4d339 1738.BR open ()
9e4be7e9 1739fails with the error
ddc4d339
MK
1740.B EINVAL
1741if it is used.
dd3568a1 1742.PP
ddc4d339
MK
1743Applications should avoid mixing
1744.B O_DIRECT
1745and normal I/O to the same file,
1746and especially to overlapping byte regions in the same file.
9ee4a2b6 1747Even when the filesystem correctly handles the coherency issues in
ddc4d339
MK
1748this situation, overall I/O throughput is likely to be slower than
1749using either mode alone.
1750Likewise, applications should avoid mixing
1751.BR mmap (2)
1752of files with direct I/O to the same files.
dd3568a1 1753.PP
a1fa36af 1754The behavior of
ddc4d339 1755.B O_DIRECT
9ee4a2b6 1756with NFS will differ from local filesystems.
ddc4d339
MK
1757Older kernels, or
1758kernels configured in certain ways, may not support this combination.
1759The NFS protocol does not support passing the flag to the server, so
1760.B O_DIRECT
33a0ccb2 1761I/O will bypass the page cache only on the client; the server may
ddc4d339
MK
1762still cache the I/O.
1763The client asks the server to make the I/O
1764synchronous to preserve the synchronous semantics of
1765.BR O_DIRECT .
1766Some servers will perform poorly under these circumstances, especially
1767if the I/O size is small.
1768Some servers may also be configured to
1769lie to clients about the I/O having reached stable storage; this
1770will avoid the performance penalty at some risk to data integrity
1771in the event of server power failure.
1772The Linux NFS client places no alignment restrictions on
1773.B O_DIRECT
1774I/O.
1775.PP
1776In summary,
1777.B O_DIRECT
1778is a potentially powerful tool that should be used with caution.
1779It is recommended that applications treat use of
1780.B O_DIRECT
1781as a performance option which is disabled by default.
ddc4d339 1782.SH BUGS
b50582eb
MK
1783Currently, it is not possible to enable signal-driven
1784I/O by specifying
1785.B O_ASYNC
c13182ef 1786when calling
b50582eb
MK
1787.BR open ();
1788use
1789.BR fcntl (2)
1790to enable this flag.
0e1ad98c 1791.\" FIXME . Check bugzilla report on open(O_ASYNC)
92057f4d 1792.\" See http://bugzilla.kernel.org/show_bug.cgi?id=5993
5355ff82 1793.PP
0d730fcc
MK
1794One must check for two different error codes,
1795.B EISDIR
1796and
1797.BR ENOENT ,
1798when trying to determine whether the kernel supports
0d55b37f 1799.B O_TMPFILE
0d730fcc 1800functionality.
5355ff82 1801.PP
320f8a8e
MK
1802When both
1803.B O_CREAT
1804and
1805.B O_DIRECTORY
1806are specified in
1807.IR flags
1808and the file specified by
1809.I pathname
1810does not exist,
1811.BR open ()
1812will create a regular file (i.e.,
1813.B O_DIRECTORY
1814is ignored).
47297adb 1815.SH SEE ALSO
a3bf8022
MK
1816.BR chmod (2),
1817.BR chown (2),
fea681da 1818.BR close (2),
e366dbc4 1819.BR dup (2),
fea681da
MK
1820.BR fcntl (2),
1821.BR link (2),
1f6ceb40 1822.BR lseek (2),
fea681da 1823.BR mknod (2),
e366dbc4 1824.BR mmap (2),
f0c34053 1825.BR mount (2),
a2dbb2e3 1826.BR openat2 (2),
fa5d243f 1827.BR open_by_handle_at (2),
fea681da
MK
1828.BR read (2),
1829.BR socket (2),
1830.BR stat (2),
1831.BR umask (2),
1832.BR unlink (2),
1833.BR write (2),
1834.BR fopen (3),
b31056e3 1835.BR acl (5),
f0c34053 1836.BR fifo (7),
3b363b62 1837.BR inode (7),
a9cfde1d
MK
1838.BR path_resolution (7),
1839.BR symlink (7)