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