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