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