]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/open.2
open.2: Note ambiguity of ELOOP error when using O_NOFOLLOW
[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
6ccb7137
MK
520If \fIpathname\fP is a symbolic link, then the open fails, with the error
521.BR ELOOP .
522(Note that this is indistinguishable from the case where
523an open fails because there are too many symbolic links found
524while resolving components in the prefix part of the pathname.)
8db11e23
MK
525This flag is a FreeBSD extension, which was added to Linux in version 2.1.126,
526and has subsequently been standardized in POSIX.1-2008.
1c1e15ed 527Symbolic links in earlier components of the pathname will still be
e366dbc4 528followed.
1135dbe1 529See also
843068bd 530.BR O_PATH
1135dbe1 531below.
e366dbc4
MK
532.\" The headers from glibc 2.0.100 and later include a
533.\" definition of this flag; \fIkernels before 2.1.126 will ignore it if
a8d55537 534.\" used\fP.
fea681da
MK
535.TP
536.BR O_NONBLOCK " or " O_NDELAY
ff40dbb3 537When possible, the file is opened in nonblocking mode.
c13182ef 538Neither the
1c1e15ed 539.BR open ()
fea681da
MK
540nor any subsequent operations on the file descriptor which is
541returned will cause the calling process to wait.
403b78f8 542
9f629381
MK
543Note that this flag has no effect for regular files and block devices;
544that is, I/O operations will (briefly) block when device activity
545is required, regardless of whether
546.B O_NONBLOCK
547is set.
548Since
549.B O_NONBLOCK
550semantics might eventually be implemented,
551applications should not depend upon blocking behavior
552when specifying this flag for regular files and block devices.
553
fea681da 554For the handling of FIFOs (named pipes), see also
af5b2ef2 555.BR fifo (7).
db28bfac 556For a discussion of the effect of
0daa9e92 557.B O_NONBLOCK
db28bfac
MK
558in conjunction with mandatory file locks and with file leases, see
559.BR fcntl (2).
fea681da 560.TP
1135dbe1
MK
561.BR O_PATH " (since Linux 2.6.39)"
562.\" commit 1abf0c718f15a56a0a435588d1b104c7a37dc9bd
563.\" commit 326be7b484843988afe57566b627fb7a70beac56
564.\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
565.\"
566.\" http://thread.gmane.org/gmane.linux.man/2790/focus=3496
567.\" Subject: Re: [PATCH] open(2): document O_PATH
568.\" Newsgroups: gmane.linux.man, gmane.linux.kernel
569.\"
1135dbe1 570Obtain a file descriptor that can be used for two purposes:
9ee4a2b6 571to indicate a location in the filesystem tree and
1135dbe1
MK
572to perform operations that act purely at the file descriptor level.
573The file itself is not opened, and other file operations (e.g.,
574.BR read (2),
575.BR write (2),
576.BR fchmod (2),
577.BR fchown (2),
2510e4e5
RH
578.BR fgetxattr (2),
579.BR mmap (2))
1135dbe1
MK
580fail with the error
581.BR EBADF .
582
583The following operations
584.I can
585be performed on the resulting file descriptor:
586.RS
587.IP * 3
588.BR close (2);
589.BR fchdir (2)
590(since Linux 3.5);
591.\" commit 332a2e1244bd08b9e3ecd378028513396a004a24
592.BR fstat (2)
593(since Linux 3.6).
594.\" fstat(): commit 55815f70147dcfa3ead5738fd56d3574e2e3c1c2
595.IP *
596Duplicating the file descriptor
597.RB ( dup (2),
598.BR fcntl (2)
599.BR F_DUPFD ,
600etc.).
601.IP *
602Getting and setting file descriptor flags
603.RB ( fcntl (2)
604.BR F_GETFD
605and
606.BR F_SETFD ).
09f677a3
MK
607.IP *
608Retrieving open file status flags using the
609.BR fcntl (2)
13a082cb 610.BR F_GETFL
09f677a3
MK
611operation: the returned flags will include the bit
612.BR O_PATH .
1135dbe1
MK
613.IP *
614Passing the file descriptor as the
615.IR dirfd
616argument of
490f876a 617.BR openat ()
1135dbe1 618and the other "*at()" system calls.
7dee406b
AL
619This includes
620.BR linkat (2)
621with
0da5e58a 622.BR AT_EMPTY_PATH
7dee406b
AL
623(or via procfs using
624.BR AT_SYMLINK_FOLLOW )
625even if the file is not a directory.
1135dbe1
MK
626.IP *
627Passing the file descriptor to another process via a UNIX domain socket
628(see
629.BR SCM_RIGHTS
630in
631.BR unix (7)).
632.RE
633.IP
634When
635.B O_PATH
636is specified in
637.IR flags ,
638flag bits other than
6807fc6f
MK
639.BR O_CLOEXEC ,
640.BR O_DIRECTORY ,
1135dbe1
MK
641and
642.BR O_NOFOLLOW
643are ignored.
644
d30344ab
MK
645If
646.I pathname
647is a symbolic link and the
1135dbe1
MK
648.BR O_NOFOLLOW
649flag is also specified,
650then the call returns a file descriptor referring to the symbolic link.
651This file descriptor can be used as the
652.I dirfd
653argument in calls to
654.BR fchownat (2),
655.BR fstatat (2),
656.BR linkat (2),
657and
658.BR readlinkat (2)
659with an empty pathname to have the calls operate on the symbolic link.
660.TP
fea681da 661.B O_SYNC
6cf19e62
MK
662Write operations on the file will complete according to the requirements of
663synchronized I/O
664.I file
665integrity completion
f36a1468 666(by contrast with the
6cf19e62
MK
667synchronized I/O
668.I data
669integrity completion
670provided by
671.BR O_DSYNC .)
672
673By the time
674.BR write (2)
675(and similar)
676return, the output data and associated file metadata
677have been transferred to the underlying hardware
678(i.e., as though each
679.BR write (2)
680was followed by a call to
681.BR fsync (2)).
682.IR "See NOTES below" .
fea681da 683.TP
40398c1a
MK
684.BR O_TMPFILE " (since Linux 3.11)"
685.\" commit 60545d0d4610b02e55f65d141c95b18ccf855b6e
686.\" commit f4e0c30c191f87851c4a53454abb55ee276f4a7e
687.\" commit bb458c644a59dbba3a1fe59b27106c5e68e1c4bd
688Create an unnamed temporary file.
689The
690.I pathname
691argument specifies a directory;
692an unnamed inode will be created in that directory's filesystem.
693Anything written to the resulting file will be lost when
694the last file descriptor is closed, unless the file is given a name.
695
696.B O_TMPFILE
697must be specified with one of
698.B O_RDWR
699or
700.B O_WRONLY
701and, optionally,
702.BR O_EXCL .
703If
704.B O_EXCL
705is not specified, then
706.BR linkat (2)
707can be used to link the temporary file into the filesystem, making it
708permanent, using code like the following:
709
710.in +4n
711.nf
712char path[PATH_MAX];
713fd = open("/path/to/dir", O_TMPFILE | O_RDWR,
0fb83d00
MK
714 S_IRUSR | S_IWUSR);
715
40398c1a 716/* File I/O on 'fd'... */
0fb83d00 717
40398c1a 718snprintf(path, PATH_MAX, "/proc/self/fd/%d", fd);
e1252130 719linkat(AT_FDCWD, path, AT_FDCWD, "/path/for/file",
0fb83d00 720 AT_SYMLINK_FOLLOW);
40398c1a
MK
721.fi
722.in
723
724In this case,
725the
726.BR open ()
727.I mode
728argument determines the file permission mode, as with
729.BR O_CREAT .
730
0115aaed
MK
731Specifying
732.B O_EXCL
733in conjunction with
734.B O_TMPFILE
735prevents a temporary file from being linked into the filesystem
736in the above manner.
737(Note that the meaning of
738.B O_EXCL
739in this case is different from the meaning of
740.B O_EXCL
741otherwise.)
742
40398c1a
MK
743There are two main use cases for
744.\" Inspired by http://lwn.net/Articles/559147/
745.BR O_TMPFILE :
746.RS
747.IP * 3
748Improved
749.BR tmpfile (3)
750functionality: race-free creation of temporary files that
751(1) are automatically deleted when closed;
752(2) can never be reached via any pathname;
753(3) are not subject to symlink attacks; and
754(4) do not require the caller to devise unique names.
755.IP *
756Creating a file that is initially invisible, which is then populated
8b04592d 757with data and adjusted to have appropriate filesystem attributes
c89a9937
EB
758.RB ( fchown (2),
759.BR fchmod (2),
40398c1a
MK
760.BR fsetxattr (2),
761etc.)
762before being atomically linked into the filesystem
763in a fully formed state (using
764.BR linkat (2)
765as described above).
766.RE
767.IP
768.B O_TMPFILE
769requires support by the underlying filesystem;
40398c1a 770only a subset of Linux filesystems provide that support.
cde2074a 771In the initial implementation, support was provided in
9af6b115 772the ext2, ext3, ext4, UDF, Minix, and shmem filesystems.
bd79a35a 773.\" To check for support, grep for "tmpfile" in kernel sources
6065b906
MK
774Support for other filesystems has subsequently been added as follows:
775XFS (Linux 3.15);
cde2074a
MK
776.\" commit 99b6436bc29e4f10e4388c27a3e4810191cc4788
777.\" commit ab29743117f9f4c22ac44c13c1647fb24fb2bafe
1b9d5819 778Btrfs (Linux 3.16);
e746db2e 779.\" commit ef3b9af50bfa6a1f02cd7b3f5124b712b1ba3e3c
6065b906 780F2FS (Linux 3.16);
bd79a35a 781.\" commit 50732df02eefb39ab414ef655979c2c9b64ad21c
6065b906 782and ubifs (Linux 4.9)
40398c1a 783.TP
1c1e15ed 784.B O_TRUNC
4d61d36a 785If the file already exists and is a regular file and the access mode allows
682edefb
MK
786writing (i.e., is
787.B O_RDWR
788or
789.BR O_WRONLY )
790it will be truncated to length 0.
791If the file is a FIFO or terminal device file, the
792.B O_TRUNC
c13182ef 793flag is ignored.
2b9b829d 794Otherwise, the effect of
682edefb
MK
795.B O_TRUNC
796is unspecified.
7b8ba76c 797.SS creat()
1f7191bb 798A call to
1c1e15ed 799.BR creat ()
1f7191bb 800is equivalent to calling
1c1e15ed 801.BR open ()
fea681da
MK
802with
803.I flags
804equal to
805.BR O_CREAT|O_WRONLY|O_TRUNC .
7b8ba76c
MK
806.SS openat()
807The
808.BR openat ()
809system call operates in exactly the same way as
cadd38ba 810.BR open (),
7b8ba76c
MK
811except for the differences described here.
812
813If the pathname given in
814.I pathname
815is relative, then it is interpreted relative to the directory
3ad65ff0 816referred to by the file descriptor
7b8ba76c
MK
817.I dirfd
818(rather than relative to the current working directory of
819the calling process, as is done by
cadd38ba 820.BR open ()
7b8ba76c
MK
821for a relative pathname).
822
823If
824.I pathname
825is relative and
826.I dirfd
827is the special value
828.BR AT_FDCWD ,
829then
830.I pathname
831is interpreted relative to the current working
832directory of the calling process (like
cadd38ba 833.BR open ()).
7b8ba76c
MK
834
835If
836.I pathname
837is absolute, then
838.I dirfd
839is ignored.
47297adb 840.SH RETURN VALUE
7b8ba76c
MK
841.BR open (),
842.BR openat (),
c13182ef 843and
e1d6264d 844.BR creat ()
1c1e15ed
MK
845return the new file descriptor, or \-1 if an error occurred
846(in which case,
fea681da
MK
847.I errno
848is set appropriately).
fea681da 849.SH ERRORS
7b8ba76c
MK
850.BR open (),
851.BR openat (),
852and
853.BR creat ()
854can fail with the following errors:
fea681da
MK
855.TP
856.B EACCES
857The requested access to the file is not allowed, or search permission
858is denied for one of the directories in the path prefix of
859.IR pathname ,
860or the file did not exist yet and write access to the parent directory
861is not allowed.
862(See also
ad7cc990 863.BR path_resolution (7).)
fea681da 864.TP
a1f01685
MH
865.B EDQUOT
866Where
867.B O_CREAT
868is specified, the file does not exist, and the user's quota of disk
9ee4a2b6 869blocks or inodes on the filesystem has been exhausted.
a1f01685 870.TP
fea681da
MK
871.B EEXIST
872.I pathname
873already exists and
874.BR O_CREAT " and " O_EXCL
875were used.
876.TP
877.B EFAULT
0daa9e92 878.I pathname
e1d6264d 879points outside your accessible address space.
fea681da 880.TP
9f5773f7 881.B EFBIG
7c7fb552
MK
882See
883.BR EOVERFLOW .
9f5773f7 884.TP
e51412ea
MK
885.B EINTR
886While blocked waiting to complete an open of a slow device
887(e.g., a FIFO; see
888.BR fifo (7)),
889the call was interrupted by a signal handler; see
890.BR signal (7).
891.TP
ef490193
DG
892.B EINVAL
893The filesystem does not support the
894.BR O_DIRECT
e6f89ed2
MK
895flag.
896See
ef490193
DG
897.BR NOTES
898for more information.
899.TP
8e335391
MK
900.B EINVAL
901Invalid value in
902.\" In particular, __O_TMPFILE instead of O_TMPFILE
903.IR flags .
904.TP
905.B EINVAL
906.B O_TMPFILE
907was specified in
908.IR flags ,
909but neither
910.B O_WRONLY
911nor
912.B O_RDWR
913was specified.
914.TP
fea681da
MK
915.B EISDIR
916.I pathname
917refers to a directory and the access requested involved writing
918(that is,
919.B O_WRONLY
920or
921.B O_RDWR
922is set).
923.TP
8e335391 924.B EISDIR
843068bd
MK
925.I pathname
926refers to an existing directory,
8e335391
MK
927.B O_TMPFILE
928and one of
929.B O_WRONLY
930or
931.B O_RDWR
932were specified in
933.IR flags ,
934but this kernel version does not provide the
935.B O_TMPFILE
936functionality.
937.TP
fea681da
MK
938.B ELOOP
939Too many symbolic links were encountered in resolving
289f7907
MK
940.IR pathname .
941.TP
942.B ELOOP
fea681da 943.I pathname
289f7907
MK
944was a symbolic link, and
945.I flags
946specified
947.BR O_NOFOLLOW
948but not
949.BR O_PATH .
fea681da
MK
950.TP
951.B EMFILE
26c32fab 952The per-process limit on the number of open file descriptors has been reached
12c21590
MK
953(see the description of
954.BR RLIMIT_NOFILE
955in
956.BR getrlimit (2)).
fea681da
MK
957.TP
958.B ENAMETOOLONG
0daa9e92 959.I pathname
e1d6264d 960was too long.
fea681da
MK
961.TP
962.B ENFILE
e258766b 963The system-wide limit on the total number of open files has been reached.
fea681da
MK
964.TP
965.B ENODEV
966.I pathname
967refers to a device special file and no corresponding device exists.
682edefb
MK
968(This is a Linux kernel bug; in this situation
969.B ENXIO
970must be returned.)
fea681da
MK
971.TP
972.B ENOENT
682edefb
MK
973.B O_CREAT
974is not set and the named file does not exist.
fea681da
MK
975Or, a directory component in
976.I pathname
977does not exist or is a dangling symbolic link.
978.TP
ba03011f
MK
979.B ENOENT
980.I pathname
981refers to a nonexistent directory,
982.B O_TMPFILE
983and one of
984.B O_WRONLY
985or
986.B O_RDWR
987were specified in
988.IR flags ,
989but this kernel version does not provide the
990.B O_TMPFILE
991functionality.
992.TP
fea681da 993.B ENOMEM
8ef529f9
MK
994The named file is a FIFO,
995but memory for the FIFO buffer can't be allocated because
996the per-user hard limit on memory allocation for pipes has been reached
997and the caller is not privileged; see
998.BR pipe (7).
999.TP
1000.B ENOMEM
fea681da
MK
1001Insufficient kernel memory was available.
1002.TP
1003.B ENOSPC
1004.I pathname
1005was to be created but the device containing
1006.I pathname
1007has no room for the new file.
1008.TP
1009.B ENOTDIR
1010A component used as a directory in
1011.I pathname
a8d55537 1012is not, in fact, a directory, or \fBO_DIRECTORY\fP was specified and
fea681da
MK
1013.I pathname
1014was not a directory.
1015.TP
1016.B ENXIO
682edefb 1017.BR O_NONBLOCK " | " O_WRONLY
103ea4f6
MK
1018is set, the named file is a FIFO, and
1019no process has the FIFO open for reading.
7b032b23
MK
1020.TP
1021.B ENXIO
1022The file is a device special file and no corresponding device exists.
fea681da 1023.TP
bbe02b45
MK
1024.BR EOPNOTSUPP
1025The filesystem containing
1026.I pathname
1027does not support
1028.BR O_TMPFILE .
1029.TP
7c7fb552
MK
1030.B EOVERFLOW
1031.I pathname
1032refers to a regular file that is too large to be opened.
1033The usual scenario here is that an application compiled
1034on a 32-bit platform without
5e4dc269 1035.I -D_FILE_OFFSET_BITS=64
7c7fb552 1036tried to open a file whose size exceeds
4e1a4d72
MK
1037.I (1<<31)-1
1038bytes;
7c7fb552
MK
1039see also
1040.B O_LARGEFILE
1041above.
c84d3aa3 1042This is the error specified by POSIX.1;
7c7fb552
MK
1043in kernels before 2.6.24, Linux gave the error
1044.B EFBIG
1045for this case.
1046.\" See http://bugzilla.kernel.org/show_bug.cgi?id=7253
1047.\" "Open of a large file on 32-bit fails with EFBIG, should be EOVERFLOW"
1048.\" Reported 2006-10-03
1049.TP
1c1e15ed
MK
1050.B EPERM
1051The
1052.B O_NOATIME
1053flag was specified, but the effective user ID of the caller
9ee4a2b6 1054.\" Strictly speaking, it's the filesystem UID... (MTK)
47c906e5 1055did not match the owner of the file and the caller was not privileged.
1c1e15ed 1056.TP
fbab10e5
MK
1057.B EPERM
1058The operation was prevented by a file seal; see
1059.BR fcntl (2).
1060.TP
fea681da
MK
1061.B EROFS
1062.I pathname
9ee4a2b6 1063refers to a file on a read-only filesystem and write access was
fea681da
MK
1064requested.
1065.TP
1066.B ETXTBSY
1067.I pathname
1068refers to an executable image which is currently being executed and
1069write access was requested.
d3952311
MK
1070.TP
1071.B EWOULDBLOCK
1072The
1073.B O_NONBLOCK
1074flag was specified, and an incompatible lease was held on the file
1075(see
1076.BR fcntl (2)).
7b8ba76c
MK
1077.PP
1078The following additional errors can occur for
1079.BR openat ():
1080.TP
1081.B EBADF
1082.I dirfd
1083is not a valid file descriptor.
1084.TP
1085.B ENOTDIR
1086.I pathname
2feae602 1087is a relative pathname and
7b8ba76c
MK
1088.I dirfd
1089is a file descriptor referring to a file other than a directory.
1090.SH VERSIONS
1091.BR openat ()
1092was added to Linux in kernel 2.6.16;
1093library support was added to glibc in version 2.4.
47297adb 1094.SH CONFORMING TO
7b8ba76c
MK
1095.BR open (),
1096.BR creat ()
72ac7268
MK
1097SVr4, 4.3BSD, POSIX.1-2001, POSIX.1-2008.
1098
7b8ba76c
MK
1099.BR openat ():
1100POSIX.1-2008.
7b8ba76c 1101
fea681da 1102The
72ac7268 1103.BR O_DIRECT ,
1c1e15ed 1104.BR O_NOATIME ,
72ac7268 1105.BR O_PATH ,
fea681da 1106and
72ac7268
MK
1107.BR O_TMPFILE
1108flags are Linux-specific.
1109One must define
61b7c1e1
MK
1110.B _GNU_SOURCE
1111to obtain their definitions.
9f91e36c
MK
1112
1113The
72ac7268
MK
1114.BR O_CLOEXEC ,
1115.BR O_DIRECTORY ,
1116and
1117.BR O_NOFOLLOW
1118flags are not specified in POSIX.1-2001,
1119but are specified in POSIX.1-2008.
1120Since glibc 2.12, one can obtain their definitions by defining either
1121.B _POSIX_C_SOURCE
1122with a value greater than or equal to 200809L or
1123.BR _XOPEN_SOURCE
1124with a value greater than or equal to 700.
1125In glibc 2.11 and earlier, one obtains the definitions by defining
1126.BR _GNU_SOURCE .
9f91e36c 1127
72ac7268
MK
1128As noted in
1129.BR feature_test_macros (7),
84fc2a6e 1130feature test macros such as
72ac7268
MK
1131.BR _POSIX_C_SOURCE ,
1132.BR _XOPEN_SOURCE ,
1133and
fe75ec04 1134.B _GNU_SOURCE
72ac7268 1135must be defined before including
e417acb0 1136.I any
72ac7268 1137header files.
a1d5f77c 1138.SH NOTES
988db661 1139Under Linux, the
a1d5f77c
MK
1140.B O_NONBLOCK
1141flag indicates that one wants to open
1142but does not necessarily have the intention to read or write.
1143This is typically used to open devices in order to get a file descriptor
1144for use with
1145.BR ioctl (2).
fea681da
MK
1146.LP
1147The (undefined) effect of
1148.B O_RDONLY | O_TRUNC
c13182ef 1149varies among implementations.
bcdd964e 1150On many systems the file is actually truncated.
fea681da
MK
1151.\" Linux 2.0, 2.5: truncate
1152.\" Solaris 5.7, 5.8: truncate
1153.\" Irix 6.5: truncate
1154.\" Tru64 5.1B: truncate
1155.\" HP-UX 11.22: truncate
1156.\" FreeBSD 4.7: truncate
a1d5f77c 1157
5dc8986d
MK
1158Note that
1159.BR open ()
1160can open device special files, but
1161.BR creat ()
1162cannot create them; use
1163.BR mknod (2)
1164instead.
1165
1166If the file is newly created, its
1167.IR st_atime ,
1168.IR st_ctime ,
1169.I st_mtime
1170fields
1171(respectively, time of last access, time of last status change, and
1172time of last modification; see
1173.BR stat (2))
1174are set
1175to the current time, and so are the
1176.I st_ctime
1177and
1178.I st_mtime
1179fields of the
1180parent directory.
1181Otherwise, if the file is modified because of the
1182.B O_TRUNC
3a9c5a29
MK
1183flag, its
1184.I st_ctime
1185and
1186.I st_mtime
1187fields are set to the current time.
aaf7a574
MK
1188
1189The files in the
1190.I /proc/[pid]/fd
1191directory show the open file descriptors of the process with the PID
1192.IR pid .
1193The files in the
1194.I /proc/[pid]/fdinfo
1195directory show even more information about these files descriptors.
1196See
1197.BR proc (5)
1198for further details of both of these directories.
5dc8986d
MK
1199.\"
1200.\"
d20d9d33
MK
1201.SS Open file descriptions
1202The term open file description is the one used by POSIX to refer to the
1203entries in the system-wide table of open files.
91085d85 1204In other contexts, this object is
d20d9d33
MK
1205variously also called an "open file object",
1206a "file handle", an "open file table entry",
1207or\(emin kernel-developer parlance\(ema
1208.IR "struct file" .
1209
1210When a file descriptor is duplicated (using
1211.BR dup (2)
1212or similar),
1213the duplicate refers to the same open file description
1214as the original file descriptor,
1215and the two file descriptors consequently share
1216the file offset and file status flags.
1217Such sharing can also occur between processes:
1218a child process created via
91085d85 1219.BR fork (2)
d20d9d33
MK
1220inherits duplicates of its parent's file descriptors,
1221and those duplicates refer to the same open file descriptions.
1222
1223Each
bf7bc8b8 1224.BR open ()
d20d9d33
MK
1225of a file creates a new open file description;
1226thus, there may be multiple open file descriptions
1227corresponding to a file inode.
9539ebc9
MK
1228
1229On Linux, one can use the
1230.BR kcmp (2)
1231.B KCMP_FILE
1232operation to test whether two file descriptors
1233(in the same process or in two different processes)
1234refer to the same open file description.
d20d9d33
MK
1235.\"
1236.\"
5dc8986d 1237.SS Synchronized I/O
6cf19e62
MK
1238The POSIX.1-2008 "synchronized I/O" option
1239specifies different variants of synchronized I/O,
1240and specifies the
1241.BR open ()
1242flags
015221ef
CH
1243.BR O_SYNC ,
1244.BR O_DSYNC ,
1245and
6cf19e62
MK
1246.BR O_RSYNC
1247for controlling the behavior.
1248Regardless of whether an implementation supports this option,
1249it must at least support the use of
1250.BR O_SYNC
1251for regular files.
1252
89851a00 1253Linux implements
6cf19e62
MK
1254.BR O_SYNC
1255and
1256.BR O_DSYNC ,
1257but not
015221ef 1258.BR O_RSYNC .
6cf19e62
MK
1259(Somewhat incorrectly, glibc defines
1260.BR O_RSYNC
1261to have the same value as
1262.BR O_SYNC .)
1263
1264.BR O_SYNC
1265provides synchronized I/O
1266.I file
1267integrity completion,
1268meaning write operations will flush data and all associated metadata
1269to the underlying hardware.
1270.BR O_DSYNC
1271provides synchronized I/O
1272.I data
1273integrity completion,
1274meaning write operations will flush data
1275to the underlying hardware,
1276but will only flush metadata updates that are required
1277to allow a subsequent read operation to complete successfully.
1278Data integrity completion can reduce the number of disk operations
1279that are required for applications that don't need the guarantees
1280of file integrity completion.
1281
a83923ca 1282To understand the difference between the two types of completion,
6cf19e62
MK
1283consider two pieces of file metadata:
1284the file last modification timestamp
1285.RI ( st_mtime )
1286and the file length.
1287All write operations will update the last file modification timestamp,
1288but only writes that add data to the end of the
1289file will change the file length.
1290The last modification timestamp is not needed to ensure that
1291a read completes successfully, but the file length is.
1292Thus,
1293.BR O_DSYNC
1294would only guarantee to flush updates to the file length metadata
1295(whereas
1296.BR O_SYNC
1297would also always flush the last modification timestamp metadata).
1298
1299Before Linux 2.6.33, Linux implemented only the
1300.BR O_SYNC
89851a00 1301flag for
6cf19e62
MK
1302.BR open ().
1303However, when that flag was specified,
1304most filesystems actually provided the equivalent of synchronized I/O
1305.I data
1306integrity completion (i.e.,
1307.BR O_SYNC
1308was actually implemented as the equivalent of
1309.BR O_DSYNC ).
1310
1311Since Linux 2.6.33, proper
1312.BR O_SYNC
1313support is provided.
1314However, to ensure backward binary compatibility,
1315.BR O_DSYNC
1316was defined with the same value as the historical
015221ef 1317.BR O_SYNC ,
015221ef 1318and
6cf19e62 1319.BR O_SYNC
89851a00 1320was defined as a new (two-bit) flag value that includes the
6cf19e62
MK
1321.BR O_DSYNC
1322flag value.
1323This ensures that applications compiled against
1324new headers get at least
1325.BR O_DSYNC
1326semantics on pre-2.6.33 kernels.
5dc8986d
MK
1327.\"
1328.\"
1329.SS NFS
1330There are many infelicities in the protocol underlying NFS, affecting
1331amongst others
1332.BR O_SYNC " and " O_NDELAY .
a1d5f77c 1333
9ee4a2b6 1334On NFS filesystems with UID mapping enabled,
a1d5f77c
MK
1335.BR open ()
1336may
75b94dc3 1337return a file descriptor but, for example,
a1d5f77c
MK
1338.BR read (2)
1339requests are denied
1340with \fBEACCES\fP.
1341This is because the client performs
1342.BR open ()
1343by checking the
1344permissions, but UID mapping is performed by the server upon
1345read and write requests.
5dc8986d
MK
1346.\"
1347.\"
1bdc161d
MK
1348.SS FIFOs
1349Opening the read or write end of a FIFO blocks until the other
1350end is also opened (by another process or thread).
1351See
1352.BR fifo (7)
1353for further details.
1354.\"
1355.\"
5dc8986d
MK
1356.SS File access mode
1357Unlike the other values that can be specified in
1358.IR flags ,
1359the
1360.I "access mode"
1361values
1362.BR O_RDONLY ", " O_WRONLY ", and " O_RDWR
1363do not specify individual bits.
1364Rather, they define the low order two bits of
1365.IR flags ,
1366and are defined respectively as 0, 1, and 2.
1367In other words, the combination
1368.B "O_RDONLY | O_WRONLY"
1369is a logical error, and certainly does not have the same meaning as
1370.BR O_RDWR .
a1d5f77c 1371
5dc8986d
MK
1372Linux reserves the special, nonstandard access mode 3 (binary 11) in
1373.I flags
1374to mean:
d9cb0d7d 1375check for read and write permission on the file and return a file descriptor
5dc8986d
MK
1376that can't be used for reading or writing.
1377This nonstandard access mode is used by some Linux drivers to return a
d9cb0d7d 1378file descriptor that is to be used only for device-specific
5dc8986d
MK
1379.BR ioctl (2)
1380operations.
1381.\" See for example util-linux's disk-utils/setfdprm.c
1382.\" For some background on access mode 3, see
1383.\" http://thread.gmane.org/gmane.linux.kernel/653123
1384.\" "[RFC] correct flags to f_mode conversion in __dentry_open"
1385.\" LKML, 12 Mar 2008
7b8ba76c
MK
1386.\"
1387.\"
80d250b4 1388.SS Rationale for openat() and other "directory file descriptor" APIs
7b8ba76c 1389.BR openat ()
80d250b4
MK
1390and the other system calls and library functions that take
1391a directory file descriptor argument
7b8ba76c 1392(i.e.,
c6a16783 1393.BR execveat (2),
7b8ba76c 1394.BR faccessat (2),
80d250b4 1395.BR fanotify_mark (2),
7b8ba76c
MK
1396.BR fchmodat (2),
1397.BR fchownat (2),
1398.BR fstatat (2),
1399.BR futimesat (2),
1400.BR linkat (2),
1401.BR mkdirat (2),
1402.BR mknodat (2),
80d250b4 1403.BR name_to_handle_at (2),
7b8ba76c
MK
1404.BR readlinkat (2),
1405.BR renameat (2),
1406.BR symlinkat (2),
1407.BR unlinkat (2),
f37759b1 1408.BR utimensat (2),
80d250b4 1409.BR mkfifoat (3),
7b8ba76c 1410and
80d250b4 1411.BR scandirat (3))
7b8ba76c
MK
1412are supported
1413for two reasons.
92692952 1414Here, the explanation is in terms of the
7b8ba76c 1415.BR openat ()
d26f8a31 1416call, but the rationale is analogous for the other interfaces.
7b8ba76c
MK
1417
1418First,
1419.BR openat ()
1420allows an application to avoid race conditions that could
1421occur when using
cadd38ba 1422.BR open ()
7b8ba76c
MK
1423to open files in directories other than the current working directory.
1424These race conditions result from the fact that some component
1425of the directory prefix given to
cadd38ba 1426.BR open ()
7b8ba76c 1427could be changed in parallel with the call to
cadd38ba 1428.BR open ().
54305f5b
MK
1429Suppose, for example, that we wish to create the file
1430.I path/to/xxx.dep
1431if the file
1432.I path/to/xxx
1433exists.
1434The problem is that between the existence check and the file creation step,
1435.I path
1436or
1437.I to
1438(which might be symbolic links)
1439could be modified to point to a different location.
7b8ba76c
MK
1440Such races can be avoided by
1441opening a file descriptor for the target directory,
1442and then specifying that file descriptor as the
1443.I dirfd
54305f5b
MK
1444argument of (say)
1445.BR fstatat (2)
1446and
7b8ba76c
MK
1447.BR openat ().
1448
1449Second,
1450.BR openat ()
1451allows the implementation of a per-thread "current working
1452directory", via file descriptor(s) maintained by the application.
1453(This functionality can also be obtained by tricks based
1454on the use of
1455.IR /proc/self/fd/ dirfd,
1456but less efficiently.)
1457.\"
1458.\"
ddc4d339
MK
1459.SS O_DIRECT
1460.LP
1461The
1462.B O_DIRECT
1463flag may impose alignment restrictions on the length and address
7fac88a9 1464of user-space buffers and the file offset of I/Os.
ddc4d339 1465In Linux alignment
9ee4a2b6 1466restrictions vary by filesystem and kernel version and might be
ddc4d339 1467absent entirely.
9ee4a2b6 1468However there is currently no filesystem\-independent
ddc4d339 1469interface for an application to discover these restrictions for a given
9ee4a2b6
MK
1470file or filesystem.
1471Some filesystems provide their own interfaces
ddc4d339
MK
1472for doing so, for example the
1473.B XFS_IOC_DIOINFO
1474operation in
1475.BR xfsctl (3).
1476.LP
85c2bdba
MK
1477Under Linux 2.4, transfer sizes, and the alignment of the user buffer
1478and the file offset must all be multiples of the logical block size
9ee4a2b6 1479of the filesystem.
21557928 1480Since Linux 2.6.0, alignment to the logical block size of the
e6042e4a 1481underlying storage (typically 512 bytes) suffices.
21557928 1482The logical block size can be determined using the
e6042e4a
PS
1483.BR ioctl (2)
1484.B BLKSSZGET
21557928
MK
1485operation or from the shell using the command:
1486
1487 blockdev \-\-getss
1847167b
NP
1488.LP
1489.B O_DIRECT
1490I/Os should never be run concurrently with the
04cd7f64 1491.BR fork (2)
1847167b
NP
1492system call,
1493if the memory buffer is a private mapping
1494(i.e., any mapping created with the
02ace852 1495.BR mmap (2)
1847167b 1496.BR MAP_PRIVATE
0ab8aeec 1497flag;
1847167b
NP
1498this includes memory allocated on the heap and statically allocated buffers).
1499Any such I/Os, whether submitted via an asynchronous I/O interface or from
1500another thread in the process,
1501should be completed before
1502.BR fork (2)
1503is called.
1504Failure to do so can result in data corruption and undefined behavior in
1505parent and child processes.
1506This restriction does not apply when the memory buffer for the
1507.B O_DIRECT
1508I/Os was created using
1509.BR shmat (2)
1510or
1511.BR mmap (2)
1512with the
1513.B MAP_SHARED
1514flag.
1515Nor does this restriction apply when the memory buffer has been advised as
1516.B MADV_DONTFORK
0ab8aeec 1517with
02ace852 1518.BR madvise (2),
1847167b
NP
1519ensuring that it will not be available
1520to the child after
1521.BR fork (2).
ddc4d339
MK
1522.LP
1523The
1524.B O_DIRECT
1525flag was introduced in SGI IRIX, where it has alignment
1526restrictions similar to those of Linux 2.4.
1527IRIX has also a
1528.BR fcntl (2)
1529call to query appropriate alignments, and sizes.
1530FreeBSD 4.x introduced
1531a flag of the same name, but without alignment restrictions.
1532.LP
1533.B O_DIRECT
1534support was added under Linux in kernel version 2.4.10.
1535Older Linux kernels simply ignore this flag.
9ee4a2b6 1536Some filesystems may not implement the flag and
ddc4d339
MK
1537.BR open ()
1538will fail with
1539.B EINVAL
1540if it is used.
1541.LP
1542Applications should avoid mixing
1543.B O_DIRECT
1544and normal I/O to the same file,
1545and especially to overlapping byte regions in the same file.
9ee4a2b6 1546Even when the filesystem correctly handles the coherency issues in
ddc4d339
MK
1547this situation, overall I/O throughput is likely to be slower than
1548using either mode alone.
1549Likewise, applications should avoid mixing
1550.BR mmap (2)
1551of files with direct I/O to the same files.
1552.LP
a1fa36af 1553The behavior of
ddc4d339 1554.B O_DIRECT
9ee4a2b6 1555with NFS will differ from local filesystems.
ddc4d339
MK
1556Older kernels, or
1557kernels configured in certain ways, may not support this combination.
1558The NFS protocol does not support passing the flag to the server, so
1559.B O_DIRECT
33a0ccb2 1560I/O will bypass the page cache only on the client; the server may
ddc4d339
MK
1561still cache the I/O.
1562The client asks the server to make the I/O
1563synchronous to preserve the synchronous semantics of
1564.BR O_DIRECT .
1565Some servers will perform poorly under these circumstances, especially
1566if the I/O size is small.
1567Some servers may also be configured to
1568lie to clients about the I/O having reached stable storage; this
1569will avoid the performance penalty at some risk to data integrity
1570in the event of server power failure.
1571The Linux NFS client places no alignment restrictions on
1572.B O_DIRECT
1573I/O.
1574.PP
1575In summary,
1576.B O_DIRECT
1577is a potentially powerful tool that should be used with caution.
1578It is recommended that applications treat use of
1579.B O_DIRECT
1580as a performance option which is disabled by default.
1581.PP
1582.RS
fea681da
MK
1583"The thing that has always disturbed me about O_DIRECT is that the whole
1584interface is just stupid, and was probably designed by a deranged monkey
5503c85e 1585on some serious mind-controlling substances."\(emLinus
ddc4d339
MK
1586.RE
1587.SH BUGS
b50582eb
MK
1588Currently, it is not possible to enable signal-driven
1589I/O by specifying
1590.B O_ASYNC
c13182ef 1591when calling
b50582eb
MK
1592.BR open ();
1593use
1594.BR fcntl (2)
1595to enable this flag.
0e1ad98c 1596.\" FIXME . Check bugzilla report on open(O_ASYNC)
92057f4d 1597.\" See http://bugzilla.kernel.org/show_bug.cgi?id=5993
0d730fcc
MK
1598
1599One must check for two different error codes,
1600.B EISDIR
1601and
1602.BR ENOENT ,
1603when trying to determine whether the kernel supports
0d55b37f 1604.B O_TMPFILE
0d730fcc 1605functionality.
320f8a8e
MK
1606
1607When both
1608.B O_CREAT
1609and
1610.B O_DIRECTORY
1611are specified in
1612.IR flags
1613and the file specified by
1614.I pathname
1615does not exist,
1616.BR open ()
1617will create a regular file (i.e.,
1618.B O_DIRECTORY
1619is ignored).
47297adb 1620.SH SEE ALSO
a3bf8022
MK
1621.BR chmod (2),
1622.BR chown (2),
fea681da 1623.BR close (2),
e366dbc4 1624.BR dup (2),
fea681da
MK
1625.BR fcntl (2),
1626.BR link (2),
1f6ceb40 1627.BR lseek (2),
fea681da 1628.BR mknod (2),
e366dbc4 1629.BR mmap (2),
f0c34053 1630.BR mount (2),
fa5d243f 1631.BR open_by_handle_at (2),
fea681da
MK
1632.BR read (2),
1633.BR socket (2),
1634.BR stat (2),
1635.BR umask (2),
1636.BR unlink (2),
1637.BR write (2),
1638.BR fopen (3),
b31056e3 1639.BR acl (5),
f0c34053 1640.BR fifo (7),
a9cfde1d
MK
1641.BR path_resolution (7),
1642.BR symlink (7)