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