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