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