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