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