]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/open.2
getifaddrs.3, iswctype.3, sem_post.3, sem_unlink.3, sem_wait.3, packet.7: tstamp
[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 49.\" O_TTYINIT. Eventually these may need to be documented. --mtk
803e1d2f 50.\" FIXME Linux 2.6.33 has O_DSYNC, and a hidden __O_SYNC.
9f91e36c 51.\"
7b8ba76c 52.TH OPEN 2 2014-02-21 "Linux" "Linux Programmer's Manual"
fea681da 53.SH NAME
7b8ba76c 54open, openat, creat \- open and possibly create a file
fea681da
MK
55.SH SYNOPSIS
56.nf
57.B #include <sys/types.h>
58.B #include <sys/stat.h>
59.B #include <fcntl.h>
60.sp
61.BI "int open(const char *" pathname ", int " flags );
62.BI "int open(const char *" pathname ", int " flags ", mode_t " mode );
5895e7eb 63
fea681da 64.BI "int creat(const char *" pathname ", mode_t " mode );
7b8ba76c
MK
65.sp
66.BI "int openat(int " dirfd ", const char *" pathname ", int " flags );
67.BI "int openat(int " dirfd ", const char *" pathname ", int " flags \
68", mode_t " mode );
fea681da 69.fi
7b8ba76c
MK
70.sp
71.in -4n
72Feature Test Macro Requirements for glibc (see
73.BR feature_test_macros (7)):
74.in
75.sp
76.BR openat ():
77.PD 0
78.ad l
79.RS 4
80.TP 4
81Since glibc 2.10:
82_XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
83.TP
84Before glibc 2.10:
85_ATFILE_SOURCE
86.RE
87.ad
88.PD
fea681da 89.SH DESCRIPTION
e366dbc4 90Given a
0daa9e92 91.I pathname
e366dbc4 92for a file,
1f6ceb40 93.BR open ()
2fda57bd 94returns a file descriptor, a small, nonnegative integer
e366dbc4
MK
95for use in subsequent system calls
96.RB ( read "(2), " write "(2), " lseek "(2), " fcntl "(2), etc.)."
97The file descriptor returned by a successful call will be
2c4bff36 98the lowest-numbered file descriptor not currently open for the process.
e366dbc4 99.PP
fe75ec04 100By default, the new file descriptor is set to remain open across an
e366dbc4 101.BR execve (2)
1f6ceb40
MK
102(i.e., the
103.B FD_CLOEXEC
104file descriptor flag described in
105.BR fcntl (2)
fd3ac440 106is initially disabled; the
fe75ec04
MK
107.B O_CLOEXEC
108flag, described below, can be used to change this default).
1f6ceb40 109The file offset is set to the beginning of the file (see
c13182ef 110.BR lseek (2)).
e366dbc4
MK
111.PP
112A call to
113.BR open ()
114creates a new
115.IR "open file description" ,
116an entry in the system-wide table of open files.
e366dbc4
MK
117This entry records the file offset and the file status flags
118(modifiable via the
0bfa087b 119.BR fcntl (2)
e366dbc4
MK
120.B F_SETFL
121operation).
2c4bff36
MK
122A file descriptor is a reference to one of these entries;
123this reference is unaffected if
124.I pathname
125is subsequently removed or modified to refer to a different file.
e366dbc4 126The new open file description is initially not shared
2c4bff36
MK
127with any other process,
128but sharing may arise via
129.BR fork (2).
e366dbc4 130.PP
c4bb193f 131The argument
fea681da 132.I flags
e366dbc4
MK
133must include one of the following
134.IR "access modes" :
c7992edc 135.BR O_RDONLY ", " O_WRONLY ", or " O_RDWR .
e366dbc4
MK
136These request opening the file read-only, write-only, or read/write,
137respectively.
bfe9ba67
MK
138
139In addition, zero or more file creation flags and file status flags
c13182ef 140can be
fea681da 141.RI bitwise- or 'd
e366dbc4 142in
bfe9ba67 143.IR flags .
c13182ef
MK
144The
145.I file creation flags
146are
0e40804c 147.BR O_CLOEXEC ,
b072a788 148.BR O_CREAT ,
0e40804c
MK
149.BR O_DIRECTORY ,
150.BR O_EXCL ,
151.BR O_NOCTTY ,
152.BR O_NOFOLLOW ,
f2698a42 153.BR O_TMPFILE ,
0e40804c
MK
154.BR O_TRUNC ,
155and
156.BR O_TTY_INIT .
c13182ef
MK
157The
158.I file status flags
bfe9ba67 159are all of the remaining flags listed below.
0e40804c 160.\" SUSv4 divides the flags into:
93ee8f96
MK
161.\" * Access mode
162.\" * File creation
163.\" * File status
164.\" * Other (O_CLOEXEC, O_DIRECTORY, O_NOFOLLOW)
165.\" though it's not clear what the difference between "other" and
0e40804c
MK
166.\" "File creation" flags is. I raised an Aardvark to see if this
167.\" can be clarified in SUSv4; 10 Oct 2008.
168.\" http://thread.gmane.org/gmane.comp.standards.posix.austin.general/64/focus=67
169.\" TC1 (balloted in 2013), resolved this, so that those three constants
170.\" are also categorized" as file status flags.
171.\"
bfe9ba67
MK
172The distinction between these two groups of flags is that
173the file status flags can be retrieved and (in some cases)
566b427d
MK
174modified; see
175.BR fcntl (2)
176for details.
177
bfe9ba67 178The full list of file creation flags and file status flags is as follows:
fea681da 179.TP
1c1e15ed 180.B O_APPEND
c13182ef
MK
181The file is opened in append mode.
182Before each
0bfa087b 183.BR write (2),
1e568304 184the file offset is positioned at the end of the file,
1c1e15ed 185as if with
0bfa087b 186.BR lseek (2).
1c1e15ed 187.B O_APPEND
9ee4a2b6 188may lead to corrupted files on NFS filesystems if more than one process
c13182ef 189appends data to a file at once.
a4391429
MK
190.\" For more background, see
191.\" http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=453946
192.\" http://nfs.sourceforge.net/
c13182ef 193This is because NFS does not support
1c1e15ed
MK
194appending to a file, so the client kernel has to simulate it, which
195can't be done without a race condition.
196.TP
197.B O_ASYNC
b50582eb 198Enable signal-driven I/O:
8bd58774
MK
199generate a signal
200.RB ( SIGIO
201by default, but this can be changed via
1c1e15ed
MK
202.BR fcntl (2))
203when input or output becomes possible on this file descriptor.
33a0ccb2 204This feature is available only for terminals, pseudoterminals,
1f6ceb40
MK
205sockets, and (since Linux 2.6) pipes and FIFOs.
206See
1c1e15ed
MK
207.BR fcntl (2)
208for further details.
9bde4908 209See also BUGS, below.
fe75ec04 210.TP
31c1f2b0 211.BR O_CLOEXEC " (since Linux 2.6.23)"
fe75ec04 212Enable the close-on-exec flag for the new file descriptor.
24ec631f 213Specifying this flag permits a program to avoid additional
fe75ec04
MK
214.BR fcntl (2)
215.B F_SETFD
24ec631f 216operations to set the
0daa9e92 217.B FD_CLOEXEC
fe75ec04
MK
218flag.
219Additionally,
220use of this flag is essential in some multithreaded programs
221since using a separate
222.BR fcntl (2)
223.B F_SETFD
224operation to set the
0daa9e92 225.B FD_CLOEXEC
fe75ec04
MK
226flag does not suffice to avoid race conditions
227where one thread opens a file descriptor at the same
228time as another thread does a
229.BR fork (2)
230plus
231.BR execve (2).
232.\" This flag fixes only one form of the race condition;
233.\" The race can also occur with, for example, descriptors
234.\" returned by accept(), pipe(), etc.
1c1e15ed 235.TP
fea681da
MK
236.B O_CREAT
237If the file does not exist it will be created.
238The owner (user ID) of the file is set to the effective user ID
c13182ef
MK
239of the process.
240The group ownership (group ID) is set either to
fea681da 241the effective group ID of the process or to the group ID of the
9ee4a2b6 242parent directory (depending on filesystem type and mount options,
0fb83d00 243and the mode of the parent directory; see the mount options
fea681da
MK
244.I bsdgroups
245and
246.I sysvgroups
8b39ad66 247described in
fea681da 248.BR mount (8)).
8b39ad66
MK
249.\" As at 2.6.25, bsdgroups is supported by ext2, ext3, ext4, and
250.\" XFS (since 2.6.14).
4e698277
MK
251.RS
252.PP
253.I mode
254specifies the permissions to use in case a new file is created.
255This argument must be supplied when
256.B O_CREAT
f2698a42
AL
257or
258.B O_TMPFILE
4e698277
MK
259is specified in
260.IR flags ;
f2698a42 261if neither
4e698277 262.B O_CREAT
f2698a42
AL
263nor
264.B O_TMPFILE
265is specified, then
4e698277
MK
266.I mode
267is ignored.
268The effective permissions are modified by
269the process's
270.I umask
271in the usual way: The permissions of the created file are
84a275c4 272.IR "(mode\ &\ ~umask)" .
33a0ccb2 273Note that this mode applies only to future accesses of the
4e698277
MK
274newly created file; the
275.BR open ()
276call that creates a read-only file may well return a read/write
277file descriptor.
278.PP
279The following symbolic constants are provided for
280.IR mode :
281.TP 9
282.B S_IRWXU
28300700 user (file owner) has read, write and execute permission
284.TP
285.B S_IRUSR
28600400 user has read permission
287.TP
288.B S_IWUSR
28900200 user has write permission
290.TP
291.B S_IXUSR
29200100 user has execute permission
293.TP
294.B S_IRWXG
29500070 group has read, write and execute permission
296.TP
297.B S_IRGRP
29800040 group has read permission
299.TP
300.B S_IWGRP
30100020 group has write permission
302.TP
303.B S_IXGRP
30400010 group has execute permission
305.TP
306.B S_IRWXO
30700007 others have read, write and execute permission
308.TP
309.B S_IROTH
31000004 others have read permission
311.TP
312.B S_IWOTH
31300002 others have write permission
314.TP
315.B S_IXOTH
31600001 others have execute permission
317.RE
fea681da 318.TP
31c1f2b0 319.BR O_DIRECT " (since Linux 2.4.10)"
1c1e15ed
MK
320Try to minimize cache effects of the I/O to and from this file.
321In general this will degrade performance, but it is useful in
322special situations, such as when applications do their own caching.
bce0482f 323File I/O is done directly to/from user-space buffers.
015221ef
CH
324The
325.B O_DIRECT
0deb3ce9 326flag on its own makes an effort to transfer data synchronously,
015221ef
CH
327but does not give the guarantees of the
328.B O_SYNC
0deb3ce9
JM
329flag that data and necessary metadata are transferred.
330To guarantee synchronous I/O,
015221ef
CH
331.B O_SYNC
332must be used in addition to
333.BR O_DIRECT .
be02e49f 334See NOTES below for further discussion.
9b54d4fa 335.sp
c13182ef 336A semantically similar (but deprecated) interface for block devices
9b54d4fa 337is described in
1c1e15ed
MK
338.BR raw (8).
339.TP
340.B O_DIRECTORY
a8d55537 341If \fIpathname\fP is not a directory, cause the open to fail.
9f8d688a
MK
342.\" But see the following and its replies:
343.\" http://marc.theaimsgroup.com/?t=112748702800001&r=1&w=2
344.\" [PATCH] open: O_DIRECTORY and O_CREAT together should fail
345.\" O_DIRECTORY | O_CREAT causes O_DIRECTORY to be ignored.
8382f16d 346This flag is Linux-specific, and was added in kernel version 2.1.126, to
60a90ecd
MK
347avoid denial-of-service problems if
348.BR opendir (3)
349is called on a
a3041a58 350FIFO or tape device.
1c1e15ed 351.TP
fea681da 352.B O_EXCL
f4b9d6a5
MK
353Ensure that this call creates the file:
354if this flag is specified in conjunction with
fea681da 355.BR O_CREAT ,
f4b9d6a5
MK
356and
357.I pathname
358already exists, then
1c1e15ed 359.BR open ()
c13182ef 360will fail.
f4b9d6a5
MK
361
362When these two flags are specified, symbolic links are not followed:
363.\" POSIX.1-2001 explicitly requires this behavior.
364if
365.I pathname
366is a symbolic link, then
367.BR open ()
368fails regardless of where the symbolic link points to.
369
10b7a945
IHV
370In general, the behavior of
371.B O_EXCL
372is undefined if it is used without
373.BR O_CREAT .
374There is one exception: on Linux 2.6 and later,
375.B O_EXCL
376can be used without
377.B O_CREAT
378if
379.I pathname
380refers to a block device.
6303d401
DB
381If the block device is in use by the system (e.g., mounted),
382.BR open ()
10b7a945
IHV
383fails with the error
384.BR EBUSY .
385
efe08656 386On NFS,
f4b9d6a5 387.B O_EXCL
33a0ccb2 388is supported only when using NFSv3 or later on kernel 2.6 or later.
efe08656 389In NFS environments where
fea681da 390.B O_EXCL
f4b9d6a5
MK
391support is not provided, programs that rely on it
392for performing locking tasks will contain a race condition.
393Portable programs that want to perform atomic file locking using a lockfile,
394and need to avoid reliance on NFS support for
395.BR O_EXCL ,
396can create a unique file on
9ee4a2b6 397the same filesystem (e.g., incorporating hostname and PID), and use
fea681da 398.BR link (2)
c13182ef 399to make a link to the lockfile.
60a90ecd
MK
400If
401.BR link (2)
f4b9d6a5 402returns 0, the lock is successful.
c13182ef 403Otherwise, use
fea681da
MK
404.BR stat (2)
405on the unique file to check if its link count has increased to 2,
406in which case the lock is also successful.
407.TP
1c1e15ed
MK
408.B O_LARGEFILE
409(LFS)
410Allow files whose sizes cannot be represented in an
8478ee02 411.I off_t
1c1e15ed 412(but can be represented in an
8478ee02 413.IR off64_t )
1c1e15ed 414to be opened.
c13182ef 415The
bcdd964e 416.B _LARGEFILE64_SOURCE
e417acb0
MK
417macro must be defined
418(before including
419.I any
420header files)
421in order to obtain this definition.
c13182ef 422Setting the
bcdd964e 423.B _FILE_OFFSET_BITS
9f3d8b28
MK
424feature test macro to 64 (rather than using
425.BR O_LARGEFILE )
12e263f1 426is the preferred
9f3d8b28 427method of accessing large files on 32-bit systems (see
2dcbf4f7 428.BR feature_test_macros (7)).
1c1e15ed 429.TP
31c1f2b0 430.BR O_NOATIME " (since Linux 2.6.8)"
1bb72c96
MK
431Do not update the file last access time
432.RI ( st_atime
433in the inode)
310b7919 434when the file is
1c1e15ed
MK
435.BR read (2).
436This flag is intended for use by indexing or backup programs,
437where its use can significantly reduce the amount of disk activity.
9ee4a2b6 438This flag may not be effective on all filesystems.
1c1e15ed 439One example is NFS, where the server maintains the access time.
0e1ad98c 440.\" The O_NOATIME flag also affects the treatment of st_atime
92057f4d 441.\" by mmap() and readdir(2), MTK, Dec 04.
1c1e15ed 442.TP
fea681da
MK
443.B O_NOCTTY
444If
445.I pathname
5503c85e 446refers to a terminal device\(emsee
1bb72c96
MK
447.BR tty (4)\(emit
448will not become the process's controlling terminal even if the
fea681da
MK
449process does not have one.
450.TP
1c1e15ed 451.B O_NOFOLLOW
a8d55537 452If \fIpathname\fP is a symbolic link, then the open fails.
c13182ef 453This is a FreeBSD extension, which was added to Linux in version 2.1.126.
1c1e15ed 454Symbolic links in earlier components of the pathname will still be
e366dbc4 455followed.
1135dbe1 456See also
843068bd 457.BR O_PATH
1135dbe1 458below.
e366dbc4
MK
459.\" The headers from glibc 2.0.100 and later include a
460.\" definition of this flag; \fIkernels before 2.1.126 will ignore it if
a8d55537 461.\" used\fP.
fea681da
MK
462.TP
463.BR O_NONBLOCK " or " O_NDELAY
ff40dbb3 464When possible, the file is opened in nonblocking mode.
c13182ef 465Neither the
1c1e15ed 466.BR open ()
fea681da
MK
467nor any subsequent operations on the file descriptor which is
468returned will cause the calling process to wait.
469For the handling of FIFOs (named pipes), see also
af5b2ef2 470.BR fifo (7).
db28bfac 471For a discussion of the effect of
0daa9e92 472.B O_NONBLOCK
db28bfac
MK
473in conjunction with mandatory file locks and with file leases, see
474.BR fcntl (2).
fea681da 475.TP
1135dbe1
MK
476.BR O_PATH " (since Linux 2.6.39)"
477.\" commit 1abf0c718f15a56a0a435588d1b104c7a37dc9bd
478.\" commit 326be7b484843988afe57566b627fb7a70beac56
479.\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
480.\"
481.\" http://thread.gmane.org/gmane.linux.man/2790/focus=3496
482.\" Subject: Re: [PATCH] open(2): document O_PATH
483.\" Newsgroups: gmane.linux.man, gmane.linux.kernel
484.\"
1135dbe1 485Obtain a file descriptor that can be used for two purposes:
9ee4a2b6 486to indicate a location in the filesystem tree and
1135dbe1
MK
487to perform operations that act purely at the file descriptor level.
488The file itself is not opened, and other file operations (e.g.,
489.BR read (2),
490.BR write (2),
491.BR fchmod (2),
492.BR fchown (2),
2510e4e5
RH
493.BR fgetxattr (2),
494.BR mmap (2))
1135dbe1
MK
495fail with the error
496.BR EBADF .
497
498The following operations
499.I can
500be performed on the resulting file descriptor:
501.RS
502.IP * 3
503.BR close (2);
504.BR fchdir (2)
505(since Linux 3.5);
506.\" commit 332a2e1244bd08b9e3ecd378028513396a004a24
507.BR fstat (2)
508(since Linux 3.6).
509.\" fstat(): commit 55815f70147dcfa3ead5738fd56d3574e2e3c1c2
510.IP *
511Duplicating the file descriptor
512.RB ( dup (2),
513.BR fcntl (2)
514.BR F_DUPFD ,
515etc.).
516.IP *
517Getting and setting file descriptor flags
518.RB ( fcntl (2)
519.BR F_GETFD
520and
521.BR F_SETFD ).
09f677a3
MK
522.IP *
523Retrieving open file status flags using the
524.BR fcntl (2)
13a082cb 525.BR F_GETFL
09f677a3
MK
526operation: the returned flags will include the bit
527.BR O_PATH .
528
1135dbe1
MK
529.IP *
530Passing the file descriptor as the
531.IR dirfd
532argument of
533.BR openat (2)
534and the other "*at()" system calls.
535.IP *
536Passing the file descriptor to another process via a UNIX domain socket
537(see
538.BR SCM_RIGHTS
539in
540.BR unix (7)).
541.RE
542.IP
543When
544.B O_PATH
545is specified in
546.IR flags ,
547flag bits other than
548.BR O_DIRECTORY
549and
550.BR O_NOFOLLOW
551are ignored.
552
553If the
554.BR O_NOFOLLOW
555flag is also specified,
556then the call returns a file descriptor referring to the symbolic link.
557This file descriptor can be used as the
558.I dirfd
559argument in calls to
560.BR fchownat (2),
561.BR fstatat (2),
562.BR linkat (2),
563and
564.BR readlinkat (2)
565with an empty pathname to have the calls operate on the symbolic link.
566.TP
fea681da 567.B O_SYNC
c13182ef
MK
568The file is opened for synchronous I/O.
569Any
0bfa087b 570.BR write (2)s
fea681da
MK
571on the resulting file descriptor will block the calling process until
572the data has been physically written to the underlying hardware.
b07cd0a9 573.IR "But see NOTES below" .
fea681da 574.TP
40398c1a
MK
575.BR O_TMPFILE " (since Linux 3.11)"
576.\" commit 60545d0d4610b02e55f65d141c95b18ccf855b6e
577.\" commit f4e0c30c191f87851c4a53454abb55ee276f4a7e
578.\" commit bb458c644a59dbba3a1fe59b27106c5e68e1c4bd
579Create an unnamed temporary file.
580The
581.I pathname
582argument specifies a directory;
583an unnamed inode will be created in that directory's filesystem.
584Anything written to the resulting file will be lost when
585the last file descriptor is closed, unless the file is given a name.
586
587.B O_TMPFILE
588must be specified with one of
589.B O_RDWR
590or
591.B O_WRONLY
592and, optionally,
593.BR O_EXCL .
594If
595.B O_EXCL
596is not specified, then
597.BR linkat (2)
598can be used to link the temporary file into the filesystem, making it
599permanent, using code like the following:
600
601.in +4n
602.nf
603char path[PATH_MAX];
604fd = open("/path/to/dir", O_TMPFILE | O_RDWR,
0fb83d00
MK
605 S_IRUSR | S_IWUSR);
606
40398c1a 607/* File I/O on 'fd'... */
0fb83d00 608
40398c1a 609snprintf(path, PATH_MAX, "/proc/self/fd/%d", fd);
e1252130 610linkat(AT_FDCWD, path, AT_FDCWD, "/path/for/file",
0fb83d00 611 AT_SYMLINK_FOLLOW);
40398c1a
MK
612.fi
613.in
614
615In this case,
616the
617.BR open ()
618.I mode
619argument determines the file permission mode, as with
620.BR O_CREAT .
621
0115aaed
MK
622Specifying
623.B O_EXCL
624in conjunction with
625.B O_TMPFILE
626prevents a temporary file from being linked into the filesystem
627in the above manner.
628(Note that the meaning of
629.B O_EXCL
630in this case is different from the meaning of
631.B O_EXCL
632otherwise.)
633
634
40398c1a
MK
635There are two main use cases for
636.\" Inspired by http://lwn.net/Articles/559147/
637.BR O_TMPFILE :
638.RS
639.IP * 3
640Improved
641.BR tmpfile (3)
642functionality: race-free creation of temporary files that
643(1) are automatically deleted when closed;
644(2) can never be reached via any pathname;
645(3) are not subject to symlink attacks; and
646(4) do not require the caller to devise unique names.
647.IP *
648Creating a file that is initially invisible, which is then populated
8b04592d 649with data and adjusted to have appropriate filesystem attributes
40398c1a
MK
650.RB ( chown (2),
651.BR chmod (2),
652.BR fsetxattr (2),
653etc.)
654before being atomically linked into the filesystem
655in a fully formed state (using
656.BR linkat (2)
657as described above).
658.RE
659.IP
660.B O_TMPFILE
661requires support by the underlying filesystem;
662.\" As at 3.13, there's support for at least ext2, ext3, ext4
663only a subset of Linux filesystems provide that support.
664.TP
1c1e15ed 665.B O_TRUNC
4d61d36a 666If the file already exists and is a regular file and the access mode allows
682edefb
MK
667writing (i.e., is
668.B O_RDWR
669or
670.BR O_WRONLY )
671it will be truncated to length 0.
672If the file is a FIFO or terminal device file, the
673.B O_TRUNC
c13182ef 674flag is ignored.
682edefb
MK
675Otherwise the effect of
676.B O_TRUNC
677is unspecified.
7b8ba76c 678.SS creat()
1c1e15ed 679.BR creat ()
fea681da 680is equivalent to
1c1e15ed 681.BR open ()
fea681da
MK
682with
683.I flags
684equal to
685.BR O_CREAT|O_WRONLY|O_TRUNC .
7b8ba76c
MK
686.SS openat()
687The
688.BR openat ()
689system call operates in exactly the same way as
cadd38ba 690.BR open (),
7b8ba76c
MK
691except for the differences described here.
692
693If the pathname given in
694.I pathname
695is relative, then it is interpreted relative to the directory
696referred to by the file descriptor
697.I dirfd
698(rather than relative to the current working directory of
699the calling process, as is done by
cadd38ba 700.BR open ()
7b8ba76c
MK
701for a relative pathname).
702
703If
704.I pathname
705is relative and
706.I dirfd
707is the special value
708.BR AT_FDCWD ,
709then
710.I pathname
711is interpreted relative to the current working
712directory of the calling process (like
cadd38ba 713.BR open ()).
7b8ba76c
MK
714
715If
716.I pathname
717is absolute, then
718.I dirfd
719is ignored.
47297adb 720.SH RETURN VALUE
7b8ba76c
MK
721.BR open (),
722.BR openat (),
c13182ef 723and
e1d6264d 724.BR creat ()
1c1e15ed
MK
725return the new file descriptor, or \-1 if an error occurred
726(in which case,
fea681da
MK
727.I errno
728is set appropriately).
fea681da 729.SH ERRORS
7b8ba76c
MK
730.BR open (),
731.BR openat (),
732and
733.BR creat ()
734can fail with the following errors:
fea681da
MK
735.TP
736.B EACCES
737The requested access to the file is not allowed, or search permission
738is denied for one of the directories in the path prefix of
739.IR pathname ,
740or the file did not exist yet and write access to the parent directory
741is not allowed.
742(See also
ad7cc990 743.BR path_resolution (7).)
fea681da 744.TP
a1f01685
MH
745.B EDQUOT
746Where
747.B O_CREAT
748is specified, the file does not exist, and the user's quota of disk
9ee4a2b6 749blocks or inodes on the filesystem has been exhausted.
a1f01685 750.TP
fea681da
MK
751.B EEXIST
752.I pathname
753already exists and
754.BR O_CREAT " and " O_EXCL
755were used.
756.TP
757.B EFAULT
0daa9e92 758.I pathname
e1d6264d 759points outside your accessible address space.
fea681da 760.TP
9f5773f7 761.B EFBIG
7c7fb552
MK
762See
763.BR EOVERFLOW .
9f5773f7 764.TP
e51412ea
MK
765.B EINTR
766While blocked waiting to complete an open of a slow device
767(e.g., a FIFO; see
768.BR fifo (7)),
769the call was interrupted by a signal handler; see
770.BR signal (7).
771.TP
ef490193
DG
772.B EINVAL
773The filesystem does not support the
774.BR O_DIRECT
e6f89ed2
MK
775flag.
776See
ef490193
DG
777.BR NOTES
778for more information.
779.TP
8e335391
MK
780.B EINVAL
781Invalid value in
782.\" In particular, __O_TMPFILE instead of O_TMPFILE
783.IR flags .
784.TP
785.B EINVAL
786.B O_TMPFILE
787was specified in
788.IR flags ,
789but neither
790.B O_WRONLY
791nor
792.B O_RDWR
793was specified.
794.TP
fea681da
MK
795.B EISDIR
796.I pathname
797refers to a directory and the access requested involved writing
798(that is,
799.B O_WRONLY
800or
801.B O_RDWR
802is set).
803.TP
8e335391 804.B EISDIR
843068bd
MK
805.I pathname
806refers to an existing directory,
8e335391
MK
807.B O_TMPFILE
808and one of
809.B O_WRONLY
810or
811.B O_RDWR
812were specified in
813.IR flags ,
814but this kernel version does not provide the
815.B O_TMPFILE
816functionality.
817.TP
fea681da
MK
818.B ELOOP
819Too many symbolic links were encountered in resolving
820.IR pathname ,
a8d55537 821or \fBO_NOFOLLOW\fP was specified but
fea681da
MK
822.I pathname
823was a symbolic link.
824.TP
825.B EMFILE
826The process already has the maximum number of files open.
827.TP
828.B ENAMETOOLONG
0daa9e92 829.I pathname
e1d6264d 830was too long.
fea681da
MK
831.TP
832.B ENFILE
833The system limit on the total number of open files has been reached.
834.TP
835.B ENODEV
836.I pathname
837refers to a device special file and no corresponding device exists.
682edefb
MK
838(This is a Linux kernel bug; in this situation
839.B ENXIO
840must be returned.)
fea681da
MK
841.TP
842.B ENOENT
682edefb
MK
843.B O_CREAT
844is not set and the named file does not exist.
fea681da
MK
845Or, a directory component in
846.I pathname
847does not exist or is a dangling symbolic link.
848.TP
ba03011f
MK
849.B ENOENT
850.I pathname
851refers to a nonexistent directory,
852.B O_TMPFILE
853and one of
854.B O_WRONLY
855or
856.B O_RDWR
857were specified in
858.IR flags ,
859but this kernel version does not provide the
860.B O_TMPFILE
861functionality.
862.TP
fea681da
MK
863.B ENOMEM
864Insufficient kernel memory was available.
865.TP
866.B ENOSPC
867.I pathname
868was to be created but the device containing
869.I pathname
870has no room for the new file.
871.TP
872.B ENOTDIR
873A component used as a directory in
874.I pathname
a8d55537 875is not, in fact, a directory, or \fBO_DIRECTORY\fP was specified and
fea681da
MK
876.I pathname
877was not a directory.
878.TP
879.B ENXIO
682edefb
MK
880.BR O_NONBLOCK " | " O_WRONLY
881is set, the named file is a FIFO and
fea681da
MK
882no process has the file open for reading.
883Or, the file is a device special file and no corresponding device exists.
884.TP
bbe02b45
MK
885.BR EOPNOTSUPP
886The filesystem containing
887.I pathname
888does not support
889.BR O_TMPFILE .
890.TP
7c7fb552
MK
891.B EOVERFLOW
892.I pathname
893refers to a regular file that is too large to be opened.
894The usual scenario here is that an application compiled
895on a 32-bit platform without
5e4dc269 896.I -D_FILE_OFFSET_BITS=64
7c7fb552
MK
897tried to open a file whose size exceeds
898.I (2<<31)-1
899bits;
900see also
901.B O_LARGEFILE
902above.
903This is the error specified by POSIX.1-2001;
904in kernels before 2.6.24, Linux gave the error
905.B EFBIG
906for this case.
907.\" See http://bugzilla.kernel.org/show_bug.cgi?id=7253
908.\" "Open of a large file on 32-bit fails with EFBIG, should be EOVERFLOW"
909.\" Reported 2006-10-03
910.TP
1c1e15ed
MK
911.B EPERM
912The
913.B O_NOATIME
914flag was specified, but the effective user ID of the caller
9ee4a2b6 915.\" Strictly speaking, it's the filesystem UID... (MTK)
1c1e15ed
MK
916did not match the owner of the file and the caller was not privileged
917.RB ( CAP_FOWNER ).
918.TP
fea681da
MK
919.B EROFS
920.I pathname
9ee4a2b6 921refers to a file on a read-only filesystem and write access was
fea681da
MK
922requested.
923.TP
924.B ETXTBSY
925.I pathname
926refers to an executable image which is currently being executed and
927write access was requested.
d3952311
MK
928.TP
929.B EWOULDBLOCK
930The
931.B O_NONBLOCK
932flag was specified, and an incompatible lease was held on the file
933(see
934.BR fcntl (2)).
7b8ba76c
MK
935.PP
936The following additional errors can occur for
937.BR openat ():
938.TP
939.B EBADF
940.I dirfd
941is not a valid file descriptor.
942.TP
943.B ENOTDIR
944.I pathname
945is relative and
946.I dirfd
947is a file descriptor referring to a file other than a directory.
948.SH VERSIONS
949.BR openat ()
950was added to Linux in kernel 2.6.16;
951library support was added to glibc in version 2.4.
47297adb 952.SH CONFORMING TO
7b8ba76c
MK
953.BR open (),
954.BR creat ()
72ac7268
MK
955SVr4, 4.3BSD, POSIX.1-2001, POSIX.1-2008.
956
7b8ba76c
MK
957.BR openat ():
958POSIX.1-2008.
7b8ba76c 959
fea681da 960The
72ac7268 961.BR O_DIRECT ,
1c1e15ed 962.BR O_NOATIME ,
72ac7268 963.BR O_PATH ,
fea681da 964and
72ac7268
MK
965.BR O_TMPFILE
966flags are Linux-specific.
967One must define
61b7c1e1
MK
968.B _GNU_SOURCE
969to obtain their definitions.
9f91e36c
MK
970
971The
72ac7268
MK
972.BR O_CLOEXEC ,
973.BR O_DIRECTORY ,
974and
975.BR O_NOFOLLOW
976flags are not specified in POSIX.1-2001,
977but are specified in POSIX.1-2008.
978Since glibc 2.12, one can obtain their definitions by defining either
979.B _POSIX_C_SOURCE
980with a value greater than or equal to 200809L or
981.BR _XOPEN_SOURCE
982with a value greater than or equal to 700.
983In glibc 2.11 and earlier, one obtains the definitions by defining
984.BR _GNU_SOURCE .
9f91e36c 985
72ac7268
MK
986As noted in
987.BR feature_test_macros (7),
84fc2a6e 988feature test macros such as
72ac7268
MK
989.BR _POSIX_C_SOURCE ,
990.BR _XOPEN_SOURCE ,
991and
fe75ec04 992.B _GNU_SOURCE
72ac7268 993must be defined before including
e417acb0 994.I any
72ac7268 995header files.
a1d5f77c 996.SH NOTES
988db661 997Under Linux, the
a1d5f77c
MK
998.B O_NONBLOCK
999flag indicates that one wants to open
1000but does not necessarily have the intention to read or write.
1001This is typically used to open devices in order to get a file descriptor
1002for use with
1003.BR ioctl (2).
c734b9f2
MK
1004
1005Unlike the other values that can be specified in
1006.IR flags ,
1007the
1008.I "access mode"
1009values
f869dc04 1010.BR O_RDONLY ", " O_WRONLY ", and " O_RDWR
c734b9f2
MK
1011do not specify individual bits.
1012Rather, they define the low order two bits of
1013.IR flags ,
1014and are defined respectively as 0, 1, and 2.
1015In other words, the combination
1016.B "O_RDONLY | O_WRONLY"
1017is a logical error, and certainly does not have the same meaning as
1018.BR O_RDWR .
c8f2dd47 1019Linux reserves the special, nonstandard access mode 3 (binary 11) in
c734b9f2
MK
1020.I flags
1021to mean:
1022check for read and write permission on the file and return a descriptor
1023that can't be used for reading or writing.
c8f2dd47 1024This nonstandard access mode is used by some Linux drivers to return a
33a0ccb2 1025descriptor that is to be used only for device-specific
c734b9f2
MK
1026.BR ioctl (2)
1027operations.
1028.\" See for example util-linux's disk-utils/setfdprm.c
1029.\" For some background on access mode 3, see
1030.\" http://thread.gmane.org/gmane.linux.kernel/653123
1031.\" "[RFC] correct flags to f_mode conversion in __dentry_open"
1032.\" LKML, 12 Mar 2008
fea681da
MK
1033.LP
1034The (undefined) effect of
1035.B O_RDONLY | O_TRUNC
c13182ef 1036varies among implementations.
bcdd964e 1037On many systems the file is actually truncated.
fea681da
MK
1038.\" Linux 2.0, 2.5: truncate
1039.\" Solaris 5.7, 5.8: truncate
1040.\" Irix 6.5: truncate
1041.\" Tru64 5.1B: truncate
1042.\" HP-UX 11.22: truncate
1043.\" FreeBSD 4.7: truncate
a1d5f77c
MK
1044.PP
1045There are many infelicities in the protocol underlying NFS, affecting
1046amongst others
1047.BR O_SYNC " and " O_NDELAY .
1048
d9bfdb9c 1049POSIX provides for three different variants of synchronized I/O,
015221ef
CH
1050corresponding to the flags
1051.BR O_SYNC ,
1052.BR O_DSYNC ,
1053and
1054.BR O_RSYNC .
33a0ccb2 1055Currently (2.6.31), Linux implements only
015221ef
CH
1056.BR O_SYNC ,
1057but glibc maps
1058.B O_DSYNC
1059and
1060.B O_RSYNC
1061to the same numerical value as
0a598d26 1062.BR O_SYNC .
9ee4a2b6 1063Most Linux filesystems don't actually implement the POSIX
015221ef
CH
1064.B O_SYNC
1065semantics, which require all metadata updates of a write
7fac88a9 1066to be on disk on returning to user space, but only the
015221ef
CH
1067.B O_DSYNC
1068semantics, which require only actual file data and metadata necessary
1069to retrieve it to be on disk by the time the system call returns.
a1d5f77c
MK
1070
1071Note that
1072.BR open ()
1073can open device special files, but
1074.BR creat ()
1075cannot create them; use
1076.BR mknod (2)
1077instead.
1078.LP
9ee4a2b6 1079On NFS filesystems with UID mapping enabled,
a1d5f77c
MK
1080.BR open ()
1081may
75b94dc3 1082return a file descriptor but, for example,
a1d5f77c
MK
1083.BR read (2)
1084requests are denied
1085with \fBEACCES\fP.
1086This is because the client performs
1087.BR open ()
1088by checking the
1089permissions, but UID mapping is performed by the server upon
1090read and write requests.
1091
1092If the file is newly created, its
988db661 1093.IR st_atime ,
a1d5f77c
MK
1094.IR st_ctime ,
1095.I st_mtime
1096fields
1097(respectively, time of last access, time of last status change, and
1098time of last modification; see
1099.BR stat (2))
1100are set
1101to the current time, and so are the
1102.I st_ctime
988db661 1103and
a1d5f77c
MK
1104.I st_mtime
1105fields of the
1106parent directory.
988db661 1107Otherwise, if the file is modified because of the
a1d5f77c
MK
1108.B O_TRUNC
1109flag, its st_ctime and st_mtime fields are set to the current time.
7b8ba76c
MK
1110.\"
1111.\"
1112.SS Rationale for openat() and the other *at() calls
1113.BR openat ()
1114and other similar system calls and library functions suffixed "at"
1115(i.e.,
1116.BR faccessat (2),
1117.BR fchmodat (2),
1118.BR fchownat (2),
1119.BR fstatat (2),
1120.BR futimesat (2),
1121.BR linkat (2),
1122.BR mkdirat (2),
1123.BR mknodat (2),
1124.BR readlinkat (2),
1125.BR renameat (2),
1126.BR symlinkat (2),
1127.BR unlinkat (2),
1128.BR utimensat (2)
1129and
1130.BR mkfifoat (3))
1131are supported
1132for two reasons.
1133Here, the explanation is in terms of the
1134.BR openat ()
1135call, but the rationale is the analogous for the other interfaces.
1136
1137First,
1138.BR openat ()
1139allows an application to avoid race conditions that could
1140occur when using
cadd38ba 1141.BR open ()
7b8ba76c
MK
1142to open files in directories other than the current working directory.
1143These race conditions result from the fact that some component
1144of the directory prefix given to
cadd38ba 1145.BR open ()
7b8ba76c 1146could be changed in parallel with the call to
cadd38ba 1147.BR open ().
7b8ba76c
MK
1148Such races can be avoided by
1149opening a file descriptor for the target directory,
1150and then specifying that file descriptor as the
1151.I dirfd
1152argument of
1153.BR openat ().
1154
1155Second,
1156.BR openat ()
1157allows the implementation of a per-thread "current working
1158directory", via file descriptor(s) maintained by the application.
1159(This functionality can also be obtained by tricks based
1160on the use of
1161.IR /proc/self/fd/ dirfd,
1162but less efficiently.)
1163.\"
1164.\"
ddc4d339
MK
1165.SS O_DIRECT
1166.LP
1167The
1168.B O_DIRECT
1169flag may impose alignment restrictions on the length and address
7fac88a9 1170of user-space buffers and the file offset of I/Os.
ddc4d339 1171In Linux alignment
9ee4a2b6 1172restrictions vary by filesystem and kernel version and might be
ddc4d339 1173absent entirely.
9ee4a2b6 1174However there is currently no filesystem\-independent
ddc4d339 1175interface for an application to discover these restrictions for a given
9ee4a2b6
MK
1176file or filesystem.
1177Some filesystems provide their own interfaces
ddc4d339
MK
1178for doing so, for example the
1179.B XFS_IOC_DIOINFO
1180operation in
1181.BR xfsctl (3).
1182.LP
85c2bdba
MK
1183Under Linux 2.4, transfer sizes, and the alignment of the user buffer
1184and the file offset must all be multiples of the logical block size
9ee4a2b6 1185of the filesystem.
04cd7f64 1186Under Linux 2.6, alignment to 512-byte boundaries suffices.
1847167b
NP
1187.LP
1188.B O_DIRECT
1189I/Os should never be run concurrently with the
04cd7f64 1190.BR fork (2)
1847167b
NP
1191system call,
1192if the memory buffer is a private mapping
1193(i.e., any mapping created with the
02ace852 1194.BR mmap (2)
1847167b 1195.BR MAP_PRIVATE
0ab8aeec 1196flag;
1847167b
NP
1197this includes memory allocated on the heap and statically allocated buffers).
1198Any such I/Os, whether submitted via an asynchronous I/O interface or from
1199another thread in the process,
1200should be completed before
1201.BR fork (2)
1202is called.
1203Failure to do so can result in data corruption and undefined behavior in
1204parent and child processes.
1205This restriction does not apply when the memory buffer for the
1206.B O_DIRECT
1207I/Os was created using
1208.BR shmat (2)
1209or
1210.BR mmap (2)
1211with the
1212.B MAP_SHARED
1213flag.
1214Nor does this restriction apply when the memory buffer has been advised as
1215.B MADV_DONTFORK
0ab8aeec 1216with
02ace852 1217.BR madvise (2),
1847167b
NP
1218ensuring that it will not be available
1219to the child after
1220.BR fork (2).
ddc4d339
MK
1221.LP
1222The
1223.B O_DIRECT
1224flag was introduced in SGI IRIX, where it has alignment
1225restrictions similar to those of Linux 2.4.
1226IRIX has also a
1227.BR fcntl (2)
1228call to query appropriate alignments, and sizes.
1229FreeBSD 4.x introduced
1230a flag of the same name, but without alignment restrictions.
1231.LP
1232.B O_DIRECT
1233support was added under Linux in kernel version 2.4.10.
1234Older Linux kernels simply ignore this flag.
9ee4a2b6 1235Some filesystems may not implement the flag and
ddc4d339
MK
1236.BR open ()
1237will fail with
1238.B EINVAL
1239if it is used.
1240.LP
1241Applications should avoid mixing
1242.B O_DIRECT
1243and normal I/O to the same file,
1244and especially to overlapping byte regions in the same file.
9ee4a2b6 1245Even when the filesystem correctly handles the coherency issues in
ddc4d339
MK
1246this situation, overall I/O throughput is likely to be slower than
1247using either mode alone.
1248Likewise, applications should avoid mixing
1249.BR mmap (2)
1250of files with direct I/O to the same files.
1251.LP
1252The behaviour of
1253.B O_DIRECT
9ee4a2b6 1254with NFS will differ from local filesystems.
ddc4d339
MK
1255Older kernels, or
1256kernels configured in certain ways, may not support this combination.
1257The NFS protocol does not support passing the flag to the server, so
1258.B O_DIRECT
33a0ccb2 1259I/O will bypass the page cache only on the client; the server may
ddc4d339
MK
1260still cache the I/O.
1261The client asks the server to make the I/O
1262synchronous to preserve the synchronous semantics of
1263.BR O_DIRECT .
1264Some servers will perform poorly under these circumstances, especially
1265if the I/O size is small.
1266Some servers may also be configured to
1267lie to clients about the I/O having reached stable storage; this
1268will avoid the performance penalty at some risk to data integrity
1269in the event of server power failure.
1270The Linux NFS client places no alignment restrictions on
1271.B O_DIRECT
1272I/O.
1273.PP
1274In summary,
1275.B O_DIRECT
1276is a potentially powerful tool that should be used with caution.
1277It is recommended that applications treat use of
1278.B O_DIRECT
1279as a performance option which is disabled by default.
1280.PP
1281.RS
fea681da
MK
1282"The thing that has always disturbed me about O_DIRECT is that the whole
1283interface is just stupid, and was probably designed by a deranged monkey
5503c85e 1284on some serious mind-controlling substances."\(emLinus
ddc4d339
MK
1285.RE
1286.SH BUGS
b50582eb
MK
1287Currently, it is not possible to enable signal-driven
1288I/O by specifying
1289.B O_ASYNC
c13182ef 1290when calling
b50582eb
MK
1291.BR open ();
1292use
1293.BR fcntl (2)
1294to enable this flag.
0e1ad98c 1295.\" FIXME . Check bugzilla report on open(O_ASYNC)
92057f4d 1296.\" See http://bugzilla.kernel.org/show_bug.cgi?id=5993
0d730fcc
MK
1297
1298One must check for two different error codes,
1299.B EISDIR
1300and
1301.BR ENOENT ,
1302when trying to determine whether the kernel supports
0d55b37f 1303.B O_TMPFILE
0d730fcc 1304functionality.
47297adb 1305.SH SEE ALSO
a3bf8022
MK
1306.BR chmod (2),
1307.BR chown (2),
fea681da 1308.BR close (2),
e366dbc4 1309.BR dup (2),
fea681da
MK
1310.BR fcntl (2),
1311.BR link (2),
1f6ceb40 1312.BR lseek (2),
fea681da 1313.BR mknod (2),
e366dbc4 1314.BR mmap (2),
f0c34053 1315.BR mount (2),
fea681da
MK
1316.BR read (2),
1317.BR socket (2),
1318.BR stat (2),
1319.BR umask (2),
1320.BR unlink (2),
1321.BR write (2),
1322.BR fopen (3),
f0c34053 1323.BR fifo (7),
a9cfde1d
MK
1324.BR path_resolution (7),
1325.BR symlink (7)