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