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