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