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