]>
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 | |
7b8ba76c | 4 | .\" and Copyright (C) 2006, 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 MK |
49 | .\" O_TTYINIT. Eventually these may need to be documented. --mtk |
50 | .\" | |
09b8afdc | 51 | .TH OPEN 2 2018-04-30 "Linux" "Linux Programmer's Manual" |
fea681da | 52 | .SH NAME |
7b8ba76c | 53 | open, openat, creat \- open and possibly create a file |
fea681da MK |
54 | .SH SYNOPSIS |
55 | .nf | |
56 | .B #include <sys/types.h> | |
57 | .B #include <sys/stat.h> | |
58 | .B #include <fcntl.h> | |
5355ff82 | 59 | .PP |
fea681da MK |
60 | .BI "int open(const char *" pathname ", int " flags ); |
61 | .BI "int open(const char *" pathname ", int " flags ", mode_t " mode ); | |
5355ff82 | 62 | .PP |
fea681da | 63 | .BI "int creat(const char *" pathname ", mode_t " mode ); |
5355ff82 | 64 | .PP |
7b8ba76c MK |
65 | .BI "int openat(int " dirfd ", const char *" pathname ", int " flags ); |
66 | .BI "int openat(int " dirfd ", const char *" pathname ", int " flags \ | |
67 | ", mode_t " mode ); | |
fea681da | 68 | .fi |
5355ff82 | 69 | .PP |
7b8ba76c MK |
70 | .in -4n |
71 | Feature Test Macro Requirements for glibc (see | |
72 | .BR feature_test_macros (7)): | |
73 | .in | |
5355ff82 | 74 | .PP |
7b8ba76c MK |
75 | .BR openat (): |
76 | .PD 0 | |
77 | .ad l | |
78 | .RS 4 | |
79 | .TP 4 | |
80 | Since glibc 2.10: | |
b0da7b8b | 81 | _POSIX_C_SOURCE\ >=\ 200809L |
7b8ba76c MK |
82 | .TP |
83 | Before glibc 2.10: | |
84 | _ATFILE_SOURCE | |
85 | .RE | |
86 | .ad | |
87 | .PD | |
fea681da | 88 | .SH DESCRIPTION |
ef81e101 | 89 | The |
1f6ceb40 | 90 | .BR open () |
ef81e101 MK |
91 | system call opens the file specified by |
92 | .IR pathname . | |
93 | If the specified file does not exist, | |
94 | it may optionally (if | |
95 | .B O_CREAT | |
96 | is specified in | |
97 | .IR flags ) | |
98 | be created by | |
99 | .BR open (). | |
100 | .PP | |
101 | The return value of | |
102 | .BR open () | |
103 | is a file descriptor, a small, nonnegative integer that is used | |
104 | in subsequent system calls | |
105 | .RB ( read "(2), " write "(2), " lseek "(2), " fcntl (2), | |
106 | etc.) to refer to the open file. | |
e366dbc4 | 107 | The file descriptor returned by a successful call will be |
2c4bff36 | 108 | the lowest-numbered file descriptor not currently open for the process. |
e366dbc4 | 109 | .PP |
fe75ec04 | 110 | By default, the new file descriptor is set to remain open across an |
e366dbc4 | 111 | .BR execve (2) |
1f6ceb40 MK |
112 | (i.e., the |
113 | .B FD_CLOEXEC | |
114 | file descriptor flag described in | |
31d79098 SP |
115 | .BR fcntl (2) |
116 | is initially disabled); the | |
fe75ec04 | 117 | .B O_CLOEXEC |
d6a74b95 | 118 | flag, described below, can be used to change this default. |
1f6ceb40 | 119 | The file offset is set to the beginning of the file (see |
c13182ef | 120 | .BR lseek (2)). |
e366dbc4 MK |
121 | .PP |
122 | A call to | |
123 | .BR open () | |
124 | creates a new | |
125 | .IR "open file description" , | |
126 | an entry in the system-wide table of open files. | |
61b12e2b | 127 | The open file description records the file offset and the file status flags |
20ee63c1 | 128 | (see below). |
61b12e2b | 129 | A file descriptor is a reference to an open file description; |
2c4bff36 MK |
130 | this reference is unaffected if |
131 | .I pathname | |
132 | is subsequently removed or modified to refer to a different file. | |
d20d9d33 | 133 | For further details on open file descriptions, see NOTES. |
e366dbc4 | 134 | .PP |
c4bb193f | 135 | The argument |
fea681da | 136 | .I flags |
e366dbc4 MK |
137 | must include one of the following |
138 | .IR "access modes" : | |
c7992edc | 139 | .BR O_RDONLY ", " O_WRONLY ", or " O_RDWR . |
e366dbc4 MK |
140 | These request opening the file read-only, write-only, or read/write, |
141 | respectively. | |
5355ff82 | 142 | .PP |
bfe9ba67 | 143 | In addition, zero or more file creation flags and file status flags |
c13182ef | 144 | can be |
fea681da | 145 | .RI bitwise- or 'd |
e366dbc4 | 146 | in |
bfe9ba67 | 147 | .IR flags . |
c13182ef MK |
148 | The |
149 | .I file creation flags | |
150 | are | |
0e40804c | 151 | .BR O_CLOEXEC , |
b072a788 | 152 | .BR O_CREAT , |
0e40804c MK |
153 | .BR O_DIRECTORY , |
154 | .BR O_EXCL , | |
155 | .BR O_NOCTTY , | |
156 | .BR O_NOFOLLOW , | |
f2698a42 | 157 | .BR O_TMPFILE , |
0e40804c | 158 | and |
15fb5d03 | 159 | .BR O_TRUNC . |
c13182ef MK |
160 | The |
161 | .I file status flags | |
bfe9ba67 | 162 | are all of the remaining flags listed below. |
0e40804c | 163 | .\" SUSv4 divides the flags into: |
93ee8f96 MK |
164 | .\" * Access mode |
165 | .\" * File creation | |
166 | .\" * File status | |
167 | .\" * Other (O_CLOEXEC, O_DIRECTORY, O_NOFOLLOW) | |
168 | .\" though it's not clear what the difference between "other" and | |
0e40804c MK |
169 | .\" "File creation" flags is. I raised an Aardvark to see if this |
170 | .\" can be clarified in SUSv4; 10 Oct 2008. | |
171 | .\" http://thread.gmane.org/gmane.comp.standards.posix.austin.general/64/focus=67 | |
172 | .\" TC1 (balloted in 2013), resolved this, so that those three constants | |
173 | .\" are also categorized" as file status flags. | |
174 | .\" | |
bfe9ba67 | 175 | The distinction between these two groups of flags is that |
68210340 MK |
176 | the file creation flags affect the semantics of the open operation itself, |
177 | while the file status flags affect the semantics of subsequent I/O operations. | |
178 | The file status flags can be retrieved and (in some cases) | |
566b427d MK |
179 | modified; see |
180 | .BR fcntl (2) | |
181 | for details. | |
5355ff82 | 182 | .PP |
bfe9ba67 | 183 | The full list of file creation flags and file status flags is as follows: |
fea681da | 184 | .TP |
1c1e15ed | 185 | .B O_APPEND |
c13182ef MK |
186 | The file is opened in append mode. |
187 | Before each | |
0bfa087b | 188 | .BR write (2), |
1e568304 | 189 | the file offset is positioned at the end of the file, |
1c1e15ed | 190 | as if with |
0bfa087b | 191 | .BR lseek (2). |
17efe87f | 192 | The modification of the file offset and the write operation |
20b8f0e2 | 193 | are performed as a single atomic step. |
5355ff82 | 194 | .IP |
1c1e15ed | 195 | .B O_APPEND |
9ee4a2b6 | 196 | may lead to corrupted files on NFS filesystems if more than one process |
c13182ef | 197 | appends data to a file at once. |
a4391429 MK |
198 | .\" For more background, see |
199 | .\" http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=453946 | |
200 | .\" http://nfs.sourceforge.net/ | |
c13182ef | 201 | This is because NFS does not support |
1c1e15ed MK |
202 | appending to a file, so the client kernel has to simulate it, which |
203 | can't be done without a race condition. | |
204 | .TP | |
205 | .B O_ASYNC | |
b50582eb | 206 | Enable signal-driven I/O: |
8bd58774 MK |
207 | generate a signal |
208 | .RB ( SIGIO | |
209 | by default, but this can be changed via | |
1c1e15ed MK |
210 | .BR fcntl (2)) |
211 | when input or output becomes possible on this file descriptor. | |
33a0ccb2 | 212 | This feature is available only for terminals, pseudoterminals, |
1f6ceb40 MK |
213 | sockets, and (since Linux 2.6) pipes and FIFOs. |
214 | See | |
1c1e15ed MK |
215 | .BR fcntl (2) |
216 | for further details. | |
9bde4908 | 217 | See also BUGS, below. |
fe75ec04 | 218 | .TP |
31c1f2b0 | 219 | .BR O_CLOEXEC " (since Linux 2.6.23)" |
7fdec065 | 220 | .\" NOTE! several other man pages refer to this text |
fe75ec04 | 221 | Enable the close-on-exec flag for the new file descriptor. |
00d82ce8 MK |
222 | .\" FIXME . for later review when Issue 8 is one day released... |
223 | .\" POSIX proposes to fix many APIs that provide hidden FDs | |
224 | .\" http://austingroupbugs.net/tag_view_page.php?tag_id=8 | |
225 | .\" http://austingroupbugs.net/view.php?id=368 | |
24ec631f | 226 | Specifying this flag permits a program to avoid additional |
fe75ec04 MK |
227 | .BR fcntl (2) |
228 | .B F_SETFD | |
24ec631f | 229 | operations to set the |
0daa9e92 | 230 | .B FD_CLOEXEC |
fe75ec04 | 231 | flag. |
5355ff82 | 232 | .IP |
7756d157 MK |
233 | Note that the use of this flag is essential in some multithreaded programs, |
234 | because using a separate | |
fe75ec04 MK |
235 | .BR fcntl (2) |
236 | .B F_SETFD | |
237 | operation to set the | |
0daa9e92 | 238 | .B FD_CLOEXEC |
fe75ec04 | 239 | flag does not suffice to avoid race conditions |
7756d157 MK |
240 | where one thread opens a file descriptor and |
241 | attempts to set its close-on-exec flag using | |
242 | .BR fcntl (2) | |
243 | at the same time as another thread does a | |
fe75ec04 MK |
244 | .BR fork (2) |
245 | plus | |
246 | .BR execve (2). | |
7756d157 | 247 | Depending on the order of execution, |
30821db8 | 248 | the race may lead to the file descriptor returned by |
7756d157 MK |
249 | .BR open () |
250 | being unintentionally leaked to the program executed by the child process | |
251 | created by | |
252 | .BR fork (2). | |
253 | (This kind of race is in principle possible for any system call | |
254 | that creates a file descriptor whose close-on-exec flag should be set, | |
255 | and various other Linux system calls provide an equivalent of the | |
256 | .BR O_CLOEXEC | |
257 | flag to deal with this problem.) | |
fe75ec04 | 258 | .\" This flag fixes only one form of the race condition; |
d9cb0d7d | 259 | .\" The race can also occur with, for example, file descriptors |
fe75ec04 | 260 | .\" returned by accept(), pipe(), etc. |
1c1e15ed | 261 | .TP |
fea681da | 262 | .B O_CREAT |
6f72cae5 MK |
263 | If |
264 | .I pathname | |
265 | does not exist, create it as a regular file. | |
5355ff82 | 266 | .IP |
40169a93 | 267 | The owner (user ID) of the new file is set to the effective user ID |
c13182ef | 268 | of the process. |
5355ff82 | 269 | .IP |
ddf5e4ab MK |
270 | The group ownership (group ID) of the new file is set either to |
271 | the effective group ID of the process (System V semantics) | |
272 | or to the group ID of the parent directory (BSD semantics). | |
273 | On Linux, the behavior depends on whether the | |
274 | set-group-ID mode bit is set on the parent directory: | |
275 | if that bit is set, then BSD semantics apply; | |
276 | otherwise, System V semantics apply. | |
277 | For some filesystems, the behavior also depends on the | |
fea681da MK |
278 | .I bsdgroups |
279 | and | |
280 | .I sysvgroups | |
ddf5e4ab | 281 | mount options described in |
fea681da | 282 | .BR mount (8)). |
8b39ad66 MK |
283 | .\" As at 2.6.25, bsdgroups is supported by ext2, ext3, ext4, and |
284 | .\" XFS (since 2.6.14). | |
4e698277 MK |
285 | .RS |
286 | .PP | |
1bab84a8 | 287 | The |
4e698277 | 288 | .I mode |
1bab84a8 | 289 | argument specifies the file mode bits be applied when a new file is created. |
4e698277 MK |
290 | This argument must be supplied when |
291 | .B O_CREAT | |
f2698a42 AL |
292 | or |
293 | .B O_TMPFILE | |
4e698277 MK |
294 | is specified in |
295 | .IR flags ; | |
f2698a42 | 296 | if neither |
4e698277 | 297 | .B O_CREAT |
f2698a42 AL |
298 | nor |
299 | .B O_TMPFILE | |
300 | is specified, then | |
4e698277 MK |
301 | .I mode |
302 | is ignored. | |
58222012 | 303 | The effective mode is modified by the process's |
4e698277 | 304 | .I umask |
58222012 MK |
305 | in the usual way: in the absence of a default ACL, the mode of the |
306 | created file is | |
84a275c4 | 307 | .IR "(mode\ &\ ~umask)" . |
33a0ccb2 | 308 | Note that this mode applies only to future accesses of the |
4e698277 MK |
309 | newly created file; the |
310 | .BR open () | |
311 | call that creates a read-only file may well return a read/write | |
312 | file descriptor. | |
313 | .PP | |
314 | The following symbolic constants are provided for | |
315 | .IR mode : | |
316 | .TP 9 | |
317 | .B S_IRWXU | |
97d5b762 | 318 | 00700 user (file owner) has read, write, and execute permission |
4e698277 MK |
319 | .TP |
320 | .B S_IRUSR | |
321 | 00400 user has read permission | |
322 | .TP | |
323 | .B S_IWUSR | |
324 | 00200 user has write permission | |
325 | .TP | |
326 | .B S_IXUSR | |
327 | 00100 user has execute permission | |
328 | .TP | |
329 | .B S_IRWXG | |
97d5b762 | 330 | 00070 group has read, write, and execute permission |
4e698277 MK |
331 | .TP |
332 | .B S_IRGRP | |
333 | 00040 group has read permission | |
334 | .TP | |
335 | .B S_IWGRP | |
336 | 00020 group has write permission | |
337 | .TP | |
338 | .B S_IXGRP | |
339 | 00010 group has execute permission | |
340 | .TP | |
341 | .B S_IRWXO | |
97d5b762 | 342 | 00007 others have read, write, and execute permission |
4e698277 MK |
343 | .TP |
344 | .B S_IROTH | |
345 | 00004 others have read permission | |
346 | .TP | |
347 | .B S_IWOTH | |
348 | 00002 others have write permission | |
349 | .TP | |
350 | .B S_IXOTH | |
351 | 00001 others have execute permission | |
352 | .RE | |
9e1d8950 MK |
353 | .IP |
354 | According to POSIX, the effect when other bits are set in | |
355 | .I mode | |
356 | is unspecified. | |
357 | On Linux, the following bits are also honored in | |
358 | .IR mode : | |
359 | .RS | |
360 | .TP 9 | |
361 | .B S_ISUID | |
362 | 0004000 set-user-ID bit | |
363 | .TP | |
364 | .B S_ISGID | |
365 | 0002000 set-group-ID bit (see | |
e6fc1596 | 366 | .BR inode (7)). |
9e1d8950 MK |
367 | .TP |
368 | .B S_ISVTX | |
369 | 0001000 sticky bit (see | |
e6fc1596 | 370 | .BR inode (7)). |
9e1d8950 | 371 | .RE |
fea681da | 372 | .TP |
31c1f2b0 | 373 | .BR O_DIRECT " (since Linux 2.4.10)" |
1c1e15ed MK |
374 | Try to minimize cache effects of the I/O to and from this file. |
375 | In general this will degrade performance, but it is useful in | |
376 | special situations, such as when applications do their own caching. | |
bce0482f | 377 | File I/O is done directly to/from user-space buffers. |
015221ef CH |
378 | The |
379 | .B O_DIRECT | |
0deb3ce9 | 380 | flag on its own makes an effort to transfer data synchronously, |
015221ef CH |
381 | but does not give the guarantees of the |
382 | .B O_SYNC | |
0deb3ce9 JM |
383 | flag that data and necessary metadata are transferred. |
384 | To guarantee synchronous I/O, | |
015221ef CH |
385 | .B O_SYNC |
386 | must be used in addition to | |
387 | .BR O_DIRECT . | |
be02e49f | 388 | See NOTES below for further discussion. |
5355ff82 | 389 | .IP |
c13182ef | 390 | A semantically similar (but deprecated) interface for block devices |
9b54d4fa | 391 | is described in |
1c1e15ed MK |
392 | .BR raw (8). |
393 | .TP | |
394 | .B O_DIRECTORY | |
a8d55537 | 395 | If \fIpathname\fP is not a directory, cause the open to fail. |
9f8d688a MK |
396 | .\" But see the following and its replies: |
397 | .\" http://marc.theaimsgroup.com/?t=112748702800001&r=1&w=2 | |
398 | .\" [PATCH] open: O_DIRECTORY and O_CREAT together should fail | |
399 | .\" O_DIRECTORY | O_CREAT causes O_DIRECTORY to be ignored. | |
65496644 | 400 | This flag was added in kernel version 2.1.126, to |
60a90ecd MK |
401 | avoid denial-of-service problems if |
402 | .BR opendir (3) | |
403 | is called on a | |
a3041a58 | 404 | FIFO or tape device. |
1c1e15ed | 405 | .TP |
6cf19e62 MK |
406 | .B O_DSYNC |
407 | Write operations on the file will complete according to the requirements of | |
408 | synchronized I/O | |
409 | .I data | |
410 | integrity completion. | |
5355ff82 | 411 | .IP |
6cf19e62 MK |
412 | By the time |
413 | .BR write (2) | |
414 | (and similar) | |
415 | return, the output data | |
416 | has been transferred to the underlying hardware, | |
417 | along with any file metadata that would be required to retrieve that data | |
418 | (i.e., as though each | |
419 | .BR write (2) | |
420 | was followed by a call to | |
421 | .BR fdatasync (2)). | |
422 | .IR "See NOTES below" . | |
423 | .TP | |
fea681da | 424 | .B O_EXCL |
f4b9d6a5 MK |
425 | Ensure that this call creates the file: |
426 | if this flag is specified in conjunction with | |
fea681da | 427 | .BR O_CREAT , |
f4b9d6a5 MK |
428 | and |
429 | .I pathname | |
430 | already exists, then | |
1c1e15ed | 431 | .BR open () |
26cd31fd MK |
432 | fails with the error |
433 | .BR EEXIST . | |
5355ff82 | 434 | .IP |
f4b9d6a5 MK |
435 | When these two flags are specified, symbolic links are not followed: |
436 | .\" POSIX.1-2001 explicitly requires this behavior. | |
437 | if | |
438 | .I pathname | |
439 | is a symbolic link, then | |
440 | .BR open () | |
43116169 | 441 | fails regardless of where the symbolic link points. |
5355ff82 | 442 | .IP |
10b7a945 IHV |
443 | In general, the behavior of |
444 | .B O_EXCL | |
445 | is undefined if it is used without | |
446 | .BR O_CREAT . | |
447 | There is one exception: on Linux 2.6 and later, | |
448 | .B O_EXCL | |
449 | can be used without | |
450 | .B O_CREAT | |
451 | if | |
452 | .I pathname | |
453 | refers to a block device. | |
6303d401 DB |
454 | If the block device is in use by the system (e.g., mounted), |
455 | .BR open () | |
10b7a945 IHV |
456 | fails with the error |
457 | .BR EBUSY . | |
5355ff82 | 458 | .IP |
efe08656 | 459 | On NFS, |
f4b9d6a5 | 460 | .B O_EXCL |
33a0ccb2 | 461 | is supported only when using NFSv3 or later on kernel 2.6 or later. |
efe08656 | 462 | In NFS environments where |
fea681da | 463 | .B O_EXCL |
f4b9d6a5 MK |
464 | support is not provided, programs that rely on it |
465 | for performing locking tasks will contain a race condition. | |
466 | Portable programs that want to perform atomic file locking using a lockfile, | |
467 | and need to avoid reliance on NFS support for | |
468 | .BR O_EXCL , | |
469 | can create a unique file on | |
9ee4a2b6 | 470 | the same filesystem (e.g., incorporating hostname and PID), and use |
fea681da | 471 | .BR link (2) |
c13182ef | 472 | to make a link to the lockfile. |
60a90ecd MK |
473 | If |
474 | .BR link (2) | |
f4b9d6a5 | 475 | returns 0, the lock is successful. |
c13182ef | 476 | Otherwise, use |
fea681da MK |
477 | .BR stat (2) |
478 | on the unique file to check if its link count has increased to 2, | |
479 | in which case the lock is also successful. | |
480 | .TP | |
1c1e15ed MK |
481 | .B O_LARGEFILE |
482 | (LFS) | |
483 | Allow files whose sizes cannot be represented in an | |
8478ee02 | 484 | .I off_t |
1c1e15ed | 485 | (but can be represented in an |
8478ee02 | 486 | .IR off64_t ) |
1c1e15ed | 487 | to be opened. |
c13182ef | 488 | The |
bcdd964e | 489 | .B _LARGEFILE64_SOURCE |
e417acb0 MK |
490 | macro must be defined |
491 | (before including | |
492 | .I any | |
493 | header files) | |
494 | in order to obtain this definition. | |
c13182ef | 495 | Setting the |
bcdd964e | 496 | .B _FILE_OFFSET_BITS |
9f3d8b28 MK |
497 | feature test macro to 64 (rather than using |
498 | .BR O_LARGEFILE ) | |
12e263f1 | 499 | is the preferred |
9f3d8b28 | 500 | method of accessing large files on 32-bit systems (see |
2dcbf4f7 | 501 | .BR feature_test_macros (7)). |
1c1e15ed | 502 | .TP |
31c1f2b0 | 503 | .BR O_NOATIME " (since Linux 2.6.8)" |
1bb72c96 MK |
504 | Do not update the file last access time |
505 | .RI ( st_atime | |
506 | in the inode) | |
310b7919 | 507 | when the file is |
1c1e15ed | 508 | .BR read (2). |
5355ff82 | 509 | .IP |
47c906e5 MK |
510 | This flag can be employed only if one of the following conditions is true: |
511 | .RS | |
512 | .IP * 3 | |
513 | The effective UID of the process | |
514 | .\" Strictly speaking: the filesystem UID | |
515 | matches the owner UID of the file. | |
516 | .IP * | |
517 | The calling process has the | |
518 | .BR CAP_FOWNER | |
519 | capability in its user namespace and | |
520 | the owner UID of the file has a mapping in the namespace. | |
521 | .RE | |
522 | .IP | |
1c1e15ed MK |
523 | This flag is intended for use by indexing or backup programs, |
524 | where its use can significantly reduce the amount of disk activity. | |
9ee4a2b6 | 525 | This flag may not be effective on all filesystems. |
1c1e15ed | 526 | One example is NFS, where the server maintains the access time. |
0e1ad98c | 527 | .\" The O_NOATIME flag also affects the treatment of st_atime |
92057f4d | 528 | .\" by mmap() and readdir(2), MTK, Dec 04. |
1c1e15ed | 529 | .TP |
fea681da MK |
530 | .B O_NOCTTY |
531 | If | |
532 | .I pathname | |
5503c85e | 533 | refers to a terminal device\(emsee |
1bb72c96 MK |
534 | .BR tty (4)\(emit |
535 | will not become the process's controlling terminal even if the | |
fea681da MK |
536 | process does not have one. |
537 | .TP | |
1c1e15ed | 538 | .B O_NOFOLLOW |
6ccb7137 MK |
539 | If \fIpathname\fP is a symbolic link, then the open fails, with the error |
540 | .BR ELOOP . | |
7fba0065 MK |
541 | Symbolic links in earlier components of the pathname will still be |
542 | followed. | |
543 | (Note that the | |
544 | .B ELOOP | |
545 | error that can occur in this case is indistinguishable from the case where | |
6ccb7137 MK |
546 | an open fails because there are too many symbolic links found |
547 | while resolving components in the prefix part of the pathname.) | |
5355ff82 | 548 | .IP |
8db11e23 MK |
549 | This flag is a FreeBSD extension, which was added to Linux in version 2.1.126, |
550 | and has subsequently been standardized in POSIX.1-2008. | |
5355ff82 | 551 | .IP |
1135dbe1 | 552 | See also |
843068bd | 553 | .BR O_PATH |
1135dbe1 | 554 | below. |
e366dbc4 MK |
555 | .\" The headers from glibc 2.0.100 and later include a |
556 | .\" definition of this flag; \fIkernels before 2.1.126 will ignore it if | |
a8d55537 | 557 | .\" used\fP. |
fea681da MK |
558 | .TP |
559 | .BR O_NONBLOCK " or " O_NDELAY | |
ff40dbb3 | 560 | When possible, the file is opened in nonblocking mode. |
c13182ef | 561 | Neither the |
1c1e15ed | 562 | .BR open () |
b0972b3b | 563 | nor any subsequent I/O operations on the file descriptor which is |
fea681da | 564 | returned will cause the calling process to wait. |
5355ff82 | 565 | .IP |
f2a11072 MK |
566 | Note that ithe setting of this flag has no effect on the operation of |
567 | .BR poll (2), | |
568 | .BR select (2), | |
569 | .BR epoll (7), | |
570 | and similar, | |
571 | since those interfaces merely inform the caller about whether | |
572 | a file descriptor is "ready", | |
573 | meaning that an I/O operation performed on | |
574 | the file descriptor with the | |
575 | .B O_NONBLOCK | |
576 | flag | |
577 | .I clear | |
578 | would not block. | |
579 | .IP | |
9f629381 MK |
580 | Note that this flag has no effect for regular files and block devices; |
581 | that is, I/O operations will (briefly) block when device activity | |
582 | is required, regardless of whether | |
583 | .B O_NONBLOCK | |
584 | is set. | |
585 | Since | |
586 | .B O_NONBLOCK | |
587 | semantics might eventually be implemented, | |
588 | applications should not depend upon blocking behavior | |
589 | when specifying this flag for regular files and block devices. | |
5355ff82 | 590 | .IP |
fea681da | 591 | For the handling of FIFOs (named pipes), see also |
af5b2ef2 | 592 | .BR fifo (7). |
db28bfac | 593 | For a discussion of the effect of |
0daa9e92 | 594 | .B O_NONBLOCK |
db28bfac MK |
595 | in conjunction with mandatory file locks and with file leases, see |
596 | .BR fcntl (2). | |
fea681da | 597 | .TP |
1135dbe1 MK |
598 | .BR O_PATH " (since Linux 2.6.39)" |
599 | .\" commit 1abf0c718f15a56a0a435588d1b104c7a37dc9bd | |
600 | .\" commit 326be7b484843988afe57566b627fb7a70beac56 | |
601 | .\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d | |
602 | .\" | |
603 | .\" http://thread.gmane.org/gmane.linux.man/2790/focus=3496 | |
604 | .\" Subject: Re: [PATCH] open(2): document O_PATH | |
605 | .\" Newsgroups: gmane.linux.man, gmane.linux.kernel | |
606 | .\" | |
1135dbe1 | 607 | Obtain a file descriptor that can be used for two purposes: |
9ee4a2b6 | 608 | to indicate a location in the filesystem tree and |
1135dbe1 MK |
609 | to perform operations that act purely at the file descriptor level. |
610 | The file itself is not opened, and other file operations (e.g., | |
611 | .BR read (2), | |
612 | .BR write (2), | |
613 | .BR fchmod (2), | |
614 | .BR fchown (2), | |
2510e4e5 | 615 | .BR fgetxattr (2), |
97a45d02 | 616 | .BR ioctl (2), |
2510e4e5 | 617 | .BR mmap (2)) |
1135dbe1 MK |
618 | fail with the error |
619 | .BR EBADF . | |
5355ff82 | 620 | .IP |
1135dbe1 MK |
621 | The following operations |
622 | .I can | |
623 | be performed on the resulting file descriptor: | |
624 | .RS | |
625 | .IP * 3 | |
b9307a4a MK |
626 | .BR close (2). |
627 | .IP * | |
f3cd742c MK |
628 | .BR fchdir (2), |
629 | if the file descriptor refers to a directory | |
b9307a4a | 630 | (since Linux 3.5). |
1135dbe1 | 631 | .\" commit 332a2e1244bd08b9e3ecd378028513396a004a24 |
b9307a4a | 632 | .IP * |
1135dbe1 | 633 | .BR fstat (2) |
b9307a4a MK |
634 | (since Linux 3.6). |
635 | .IP * | |
1135dbe1 | 636 | .\" fstat(): commit 55815f70147dcfa3ead5738fd56d3574e2e3c1c2 |
97a45d02 N |
637 | .BR fstatfs (2) |
638 | (since Linux 3.12). | |
639 | .\" fstatfs(): commit 9d05746e7b16d8565dddbe3200faa1e669d23bbf | |
1135dbe1 MK |
640 | .IP * |
641 | Duplicating the file descriptor | |
642 | .RB ( dup (2), | |
643 | .BR fcntl (2) | |
644 | .BR F_DUPFD , | |
645 | etc.). | |
646 | .IP * | |
647 | Getting and setting file descriptor flags | |
648 | .RB ( fcntl (2) | |
649 | .BR F_GETFD | |
650 | and | |
651 | .BR F_SETFD ). | |
09f677a3 MK |
652 | .IP * |
653 | Retrieving open file status flags using the | |
654 | .BR fcntl (2) | |
13a082cb | 655 | .BR F_GETFL |
09f677a3 MK |
656 | operation: the returned flags will include the bit |
657 | .BR O_PATH . | |
1135dbe1 MK |
658 | .IP * |
659 | Passing the file descriptor as the | |
660 | .IR dirfd | |
661 | argument of | |
490f876a | 662 | .BR openat () |
1135dbe1 | 663 | and the other "*at()" system calls. |
7dee406b AL |
664 | This includes |
665 | .BR linkat (2) | |
666 | with | |
0da5e58a | 667 | .BR AT_EMPTY_PATH |
7dee406b AL |
668 | (or via procfs using |
669 | .BR AT_SYMLINK_FOLLOW ) | |
670 | even if the file is not a directory. | |
1135dbe1 MK |
671 | .IP * |
672 | Passing the file descriptor to another process via a UNIX domain socket | |
673 | (see | |
674 | .BR SCM_RIGHTS | |
675 | in | |
676 | .BR unix (7)). | |
677 | .RE | |
678 | .IP | |
679 | When | |
680 | .B O_PATH | |
681 | is specified in | |
682 | .IR flags , | |
683 | flag bits other than | |
6807fc6f MK |
684 | .BR O_CLOEXEC , |
685 | .BR O_DIRECTORY , | |
1135dbe1 MK |
686 | and |
687 | .BR O_NOFOLLOW | |
688 | are ignored. | |
5355ff82 | 689 | .IP |
4a3b9ffc MK |
690 | Opening a file or directory with the |
691 | .B O_PATH | |
692 | flag requires no permissions on the object itself | |
693 | (but does require execute permission on the directories in the path prefix). | |
694 | Depending on the subsequent operation, | |
695 | a check for suitable file permissions may be performed (e.g., | |
696 | .BR fchdir (2) | |
697 | requires execute permission on the directory referred to | |
698 | by its file descriptor argument). | |
699 | By contrast, | |
700 | obtaining a reference to a filesystem object by opening it with the | |
701 | .B O_RDONLY | |
702 | flag requires that the caller have read permission on the object, | |
703 | even when the subsequent operation (e.g., | |
704 | .BR fchdir (2), | |
705 | .BR fstat (2)) | |
706 | does not require read permission on the object. | |
707 | .IP | |
d30344ab MK |
708 | If |
709 | .I pathname | |
710 | is a symbolic link and the | |
1135dbe1 MK |
711 | .BR O_NOFOLLOW |
712 | flag is also specified, | |
713 | then the call returns a file descriptor referring to the symbolic link. | |
714 | This file descriptor can be used as the | |
715 | .I dirfd | |
716 | argument in calls to | |
717 | .BR fchownat (2), | |
718 | .BR fstatat (2), | |
719 | .BR linkat (2), | |
720 | and | |
721 | .BR readlinkat (2) | |
722 | with an empty pathname to have the calls operate on the symbolic link. | |
5355ff82 | 723 | .IP |
97a45d02 N |
724 | If |
725 | .I pathname | |
726 | refers to an automount point that has not yet been triggered, so no | |
727 | other filesystem is mounted on it, then the call returns a file | |
728 | descriptor referring to the automount directory without triggering a mount. | |
729 | .BR fstatfs (2) | |
730 | can then be used to determine if it is, in fact, an untriggered | |
731 | automount point | |
732 | .RB ( ".f_type == AUTOFS_SUPER_MAGIC" ). | |
d1304ede MK |
733 | .IP |
734 | One use of | |
735 | .B O_PATH | |
736 | for regular files is to provide the equivalent of POSIX.1's | |
737 | .B O_EXEC | |
738 | functionality. | |
739 | This permits us to open a file for which we have execute | |
ebab32e1 | 740 | permission but not read permission, and then execute that file, |
d1304ede MK |
741 | with steps something like the following: |
742 | .IP | |
743 | .in +4n | |
744 | .EX | |
745 | char buf[PATH_MAX]; | |
746 | fd = open("some_prog", O_PATH); | |
8e13d566 | 747 | snprintf(buf, PATH_MAX, "/proc/self/fd/%d", fd); |
d1304ede MK |
748 | execl(buf, "some_prog", (char *) NULL); |
749 | .EE | |
750 | .in | |
e982cebf MK |
751 | .IP |
752 | An | |
753 | .B O_PATH | |
754 | file descriptor can also be passed as the argument of | |
755 | .BR fexecve (3). | |
1135dbe1 | 756 | .TP |
fea681da | 757 | .B O_SYNC |
6cf19e62 MK |
758 | Write operations on the file will complete according to the requirements of |
759 | synchronized I/O | |
760 | .I file | |
761 | integrity completion | |
f36a1468 | 762 | (by contrast with the |
6cf19e62 MK |
763 | synchronized I/O |
764 | .I data | |
765 | integrity completion | |
766 | provided by | |
767 | .BR O_DSYNC .) | |
5355ff82 | 768 | .IP |
6cf19e62 MK |
769 | By the time |
770 | .BR write (2) | |
ca20a8a5 MK |
771 | (or similar) |
772 | returns, the output data and associated file metadata | |
6cf19e62 MK |
773 | have been transferred to the underlying hardware |
774 | (i.e., as though each | |
775 | .BR write (2) | |
776 | was followed by a call to | |
777 | .BR fsync (2)). | |
778 | .IR "See NOTES below" . | |
fea681da | 779 | .TP |
40398c1a MK |
780 | .BR O_TMPFILE " (since Linux 3.11)" |
781 | .\" commit 60545d0d4610b02e55f65d141c95b18ccf855b6e | |
782 | .\" commit f4e0c30c191f87851c4a53454abb55ee276f4a7e | |
783 | .\" commit bb458c644a59dbba3a1fe59b27106c5e68e1c4bd | |
6a11a5d4 | 784 | Create an unnamed temporary regular file. |
40398c1a MK |
785 | The |
786 | .I pathname | |
787 | argument specifies a directory; | |
788 | an unnamed inode will be created in that directory's filesystem. | |
789 | Anything written to the resulting file will be lost when | |
790 | the last file descriptor is closed, unless the file is given a name. | |
5355ff82 | 791 | .IP |
40398c1a MK |
792 | .B O_TMPFILE |
793 | must be specified with one of | |
794 | .B O_RDWR | |
795 | or | |
796 | .B O_WRONLY | |
797 | and, optionally, | |
798 | .BR O_EXCL . | |
799 | If | |
800 | .B O_EXCL | |
801 | is not specified, then | |
802 | .BR linkat (2) | |
803 | can be used to link the temporary file into the filesystem, making it | |
804 | permanent, using code like the following: | |
5355ff82 | 805 | .IP |
40398c1a | 806 | .in +4n |
5355ff82 | 807 | .EX |
40398c1a MK |
808 | char path[PATH_MAX]; |
809 | fd = open("/path/to/dir", O_TMPFILE | O_RDWR, | |
0fb83d00 MK |
810 | S_IRUSR | S_IWUSR); |
811 | ||
40398c1a | 812 | /* File I/O on 'fd'... */ |
0fb83d00 | 813 | |
40398c1a | 814 | snprintf(path, PATH_MAX, "/proc/self/fd/%d", fd); |
e1252130 | 815 | linkat(AT_FDCWD, path, AT_FDCWD, "/path/for/file", |
0fb83d00 | 816 | AT_SYMLINK_FOLLOW); |
5355ff82 | 817 | .EE |
40398c1a | 818 | .in |
5355ff82 | 819 | .IP |
40398c1a MK |
820 | In this case, |
821 | the | |
822 | .BR open () | |
823 | .I mode | |
824 | argument determines the file permission mode, as with | |
825 | .BR O_CREAT . | |
5355ff82 | 826 | .IP |
0115aaed MK |
827 | Specifying |
828 | .B O_EXCL | |
829 | in conjunction with | |
830 | .B O_TMPFILE | |
831 | prevents a temporary file from being linked into the filesystem | |
832 | in the above manner. | |
833 | (Note that the meaning of | |
834 | .B O_EXCL | |
835 | in this case is different from the meaning of | |
836 | .B O_EXCL | |
837 | otherwise.) | |
5355ff82 | 838 | .IP |
40398c1a MK |
839 | There are two main use cases for |
840 | .\" Inspired by http://lwn.net/Articles/559147/ | |
841 | .BR O_TMPFILE : | |
842 | .RS | |
843 | .IP * 3 | |
844 | Improved | |
845 | .BR tmpfile (3) | |
846 | functionality: race-free creation of temporary files that | |
847 | (1) are automatically deleted when closed; | |
848 | (2) can never be reached via any pathname; | |
849 | (3) are not subject to symlink attacks; and | |
850 | (4) do not require the caller to devise unique names. | |
851 | .IP * | |
852 | Creating a file that is initially invisible, which is then populated | |
8b04592d | 853 | with data and adjusted to have appropriate filesystem attributes |
c89a9937 EB |
854 | .RB ( fchown (2), |
855 | .BR fchmod (2), | |
40398c1a MK |
856 | .BR fsetxattr (2), |
857 | etc.) | |
858 | before being atomically linked into the filesystem | |
859 | in a fully formed state (using | |
860 | .BR linkat (2) | |
861 | as described above). | |
862 | .RE | |
863 | .IP | |
864 | .B O_TMPFILE | |
865 | requires support by the underlying filesystem; | |
40398c1a | 866 | only a subset of Linux filesystems provide that support. |
cde2074a | 867 | In the initial implementation, support was provided in |
9af6b115 | 868 | the ext2, ext3, ext4, UDF, Minix, and shmem filesystems. |
bd79a35a | 869 | .\" To check for support, grep for "tmpfile" in kernel sources |
6065b906 MK |
870 | Support for other filesystems has subsequently been added as follows: |
871 | XFS (Linux 3.15); | |
cde2074a MK |
872 | .\" commit 99b6436bc29e4f10e4388c27a3e4810191cc4788 |
873 | .\" commit ab29743117f9f4c22ac44c13c1647fb24fb2bafe | |
1b9d5819 | 874 | Btrfs (Linux 3.16); |
e746db2e | 875 | .\" commit ef3b9af50bfa6a1f02cd7b3f5124b712b1ba3e3c |
6065b906 | 876 | F2FS (Linux 3.16); |
bd79a35a | 877 | .\" commit 50732df02eefb39ab414ef655979c2c9b64ad21c |
6065b906 | 878 | and ubifs (Linux 4.9) |
40398c1a | 879 | .TP |
1c1e15ed | 880 | .B O_TRUNC |
4d61d36a | 881 | If the file already exists and is a regular file and the access mode allows |
682edefb MK |
882 | writing (i.e., is |
883 | .B O_RDWR | |
884 | or | |
885 | .BR O_WRONLY ) | |
886 | it will be truncated to length 0. | |
887 | If the file is a FIFO or terminal device file, the | |
888 | .B O_TRUNC | |
c13182ef | 889 | flag is ignored. |
2b9b829d | 890 | Otherwise, the effect of |
682edefb MK |
891 | .B O_TRUNC |
892 | is unspecified. | |
7b8ba76c | 893 | .SS creat() |
1f7191bb | 894 | A call to |
1c1e15ed | 895 | .BR creat () |
1f7191bb | 896 | is equivalent to calling |
1c1e15ed | 897 | .BR open () |
fea681da MK |
898 | with |
899 | .I flags | |
900 | equal to | |
901 | .BR O_CREAT|O_WRONLY|O_TRUNC . | |
7b8ba76c MK |
902 | .SS openat() |
903 | The | |
904 | .BR openat () | |
905 | system call operates in exactly the same way as | |
cadd38ba | 906 | .BR open (), |
7b8ba76c | 907 | except for the differences described here. |
3130d10b | 908 | .PP |
7b8ba76c MK |
909 | If the pathname given in |
910 | .I pathname | |
911 | is relative, then it is interpreted relative to the directory | |
3ad65ff0 | 912 | referred to by the file descriptor |
7b8ba76c MK |
913 | .I dirfd |
914 | (rather than relative to the current working directory of | |
915 | the calling process, as is done by | |
cadd38ba | 916 | .BR open () |
7b8ba76c | 917 | for a relative pathname). |
3130d10b | 918 | .PP |
7b8ba76c MK |
919 | If |
920 | .I pathname | |
921 | is relative and | |
922 | .I dirfd | |
923 | is the special value | |
924 | .BR AT_FDCWD , | |
925 | then | |
926 | .I pathname | |
927 | is interpreted relative to the current working | |
928 | directory of the calling process (like | |
cadd38ba | 929 | .BR open ()). |
3130d10b | 930 | .PP |
7b8ba76c MK |
931 | If |
932 | .I pathname | |
933 | is absolute, then | |
934 | .I dirfd | |
935 | is ignored. | |
47297adb | 936 | .SH RETURN VALUE |
7b8ba76c MK |
937 | .BR open (), |
938 | .BR openat (), | |
c13182ef | 939 | and |
e1d6264d | 940 | .BR creat () |
1c1e15ed MK |
941 | return the new file descriptor, or \-1 if an error occurred |
942 | (in which case, | |
fea681da MK |
943 | .I errno |
944 | is set appropriately). | |
fea681da | 945 | .SH ERRORS |
7b8ba76c MK |
946 | .BR open (), |
947 | .BR openat (), | |
948 | and | |
949 | .BR creat () | |
950 | can fail with the following errors: | |
fea681da MK |
951 | .TP |
952 | .B EACCES | |
953 | The requested access to the file is not allowed, or search permission | |
954 | is denied for one of the directories in the path prefix of | |
955 | .IR pathname , | |
956 | or the file did not exist yet and write access to the parent directory | |
957 | is not allowed. | |
958 | (See also | |
ad7cc990 | 959 | .BR path_resolution (7).) |
fea681da | 960 | .TP |
a1f01685 MH |
961 | .B EDQUOT |
962 | Where | |
963 | .B O_CREAT | |
964 | is specified, the file does not exist, and the user's quota of disk | |
9ee4a2b6 | 965 | blocks or inodes on the filesystem has been exhausted. |
a1f01685 | 966 | .TP |
fea681da MK |
967 | .B EEXIST |
968 | .I pathname | |
969 | already exists and | |
970 | .BR O_CREAT " and " O_EXCL | |
971 | were used. | |
972 | .TP | |
973 | .B EFAULT | |
0daa9e92 | 974 | .I pathname |
e1d6264d | 975 | points outside your accessible address space. |
fea681da | 976 | .TP |
9f5773f7 | 977 | .B EFBIG |
7c7fb552 MK |
978 | See |
979 | .BR EOVERFLOW . | |
9f5773f7 | 980 | .TP |
e51412ea MK |
981 | .B EINTR |
982 | While blocked waiting to complete an open of a slow device | |
983 | (e.g., a FIFO; see | |
984 | .BR fifo (7)), | |
985 | the call was interrupted by a signal handler; see | |
986 | .BR signal (7). | |
987 | .TP | |
ef490193 DG |
988 | .B EINVAL |
989 | The filesystem does not support the | |
990 | .BR O_DIRECT | |
e6f89ed2 MK |
991 | flag. |
992 | See | |
ef490193 DG |
993 | .BR NOTES |
994 | for more information. | |
995 | .TP | |
8e335391 MK |
996 | .B EINVAL |
997 | Invalid value in | |
998 | .\" In particular, __O_TMPFILE instead of O_TMPFILE | |
999 | .IR flags . | |
1000 | .TP | |
1001 | .B EINVAL | |
1002 | .B O_TMPFILE | |
1003 | was specified in | |
1004 | .IR flags , | |
1005 | but neither | |
1006 | .B O_WRONLY | |
1007 | nor | |
1008 | .B O_RDWR | |
1009 | was specified. | |
1010 | .TP | |
5c6f8de0 MK |
1011 | .B EINVAL |
1012 | .B O_CREAT | |
1013 | was specified in | |
1014 | .I flags | |
1015 | and the final component ("basename") of the new file's | |
1016 | .I pathname | |
1017 | is invalid | |
1018 | (e.g., it contains characters not permitted by the underlying filesystem). | |
1019 | .TP | |
fea681da MK |
1020 | .B EISDIR |
1021 | .I pathname | |
1022 | refers to a directory and the access requested involved writing | |
1023 | (that is, | |
1024 | .B O_WRONLY | |
1025 | or | |
1026 | .B O_RDWR | |
1027 | is set). | |
1028 | .TP | |
8e335391 | 1029 | .B EISDIR |
843068bd MK |
1030 | .I pathname |
1031 | refers to an existing directory, | |
8e335391 MK |
1032 | .B O_TMPFILE |
1033 | and one of | |
1034 | .B O_WRONLY | |
1035 | or | |
1036 | .B O_RDWR | |
1037 | were specified in | |
1038 | .IR flags , | |
1039 | but this kernel version does not provide the | |
1040 | .B O_TMPFILE | |
1041 | functionality. | |
1042 | .TP | |
fea681da MK |
1043 | .B ELOOP |
1044 | Too many symbolic links were encountered in resolving | |
289f7907 MK |
1045 | .IR pathname . |
1046 | .TP | |
1047 | .B ELOOP | |
fea681da | 1048 | .I pathname |
289f7907 MK |
1049 | was a symbolic link, and |
1050 | .I flags | |
1051 | specified | |
1052 | .BR O_NOFOLLOW | |
1053 | but not | |
1054 | .BR O_PATH . | |
fea681da MK |
1055 | .TP |
1056 | .B EMFILE | |
26c32fab | 1057 | The per-process limit on the number of open file descriptors has been reached |
12c21590 MK |
1058 | (see the description of |
1059 | .BR RLIMIT_NOFILE | |
1060 | in | |
1061 | .BR getrlimit (2)). | |
fea681da MK |
1062 | .TP |
1063 | .B ENAMETOOLONG | |
0daa9e92 | 1064 | .I pathname |
e1d6264d | 1065 | was too long. |
fea681da MK |
1066 | .TP |
1067 | .B ENFILE | |
e258766b | 1068 | The system-wide limit on the total number of open files has been reached. |
fea681da MK |
1069 | .TP |
1070 | .B ENODEV | |
1071 | .I pathname | |
1072 | refers to a device special file and no corresponding device exists. | |
682edefb MK |
1073 | (This is a Linux kernel bug; in this situation |
1074 | .B ENXIO | |
1075 | must be returned.) | |
fea681da MK |
1076 | .TP |
1077 | .B ENOENT | |
682edefb MK |
1078 | .B O_CREAT |
1079 | is not set and the named file does not exist. | |
fea681da MK |
1080 | Or, a directory component in |
1081 | .I pathname | |
1082 | does not exist or is a dangling symbolic link. | |
1083 | .TP | |
ba03011f MK |
1084 | .B ENOENT |
1085 | .I pathname | |
1086 | refers to a nonexistent directory, | |
1087 | .B O_TMPFILE | |
1088 | and one of | |
1089 | .B O_WRONLY | |
1090 | or | |
1091 | .B O_RDWR | |
1092 | were specified in | |
1093 | .IR flags , | |
1094 | but this kernel version does not provide the | |
1095 | .B O_TMPFILE | |
1096 | functionality. | |
1097 | .TP | |
fea681da | 1098 | .B ENOMEM |
8ef529f9 MK |
1099 | The named file is a FIFO, |
1100 | but memory for the FIFO buffer can't be allocated because | |
1101 | the per-user hard limit on memory allocation for pipes has been reached | |
1102 | and the caller is not privileged; see | |
1103 | .BR pipe (7). | |
1104 | .TP | |
1105 | .B ENOMEM | |
fea681da MK |
1106 | Insufficient kernel memory was available. |
1107 | .TP | |
1108 | .B ENOSPC | |
1109 | .I pathname | |
1110 | was to be created but the device containing | |
1111 | .I pathname | |
1112 | has no room for the new file. | |
1113 | .TP | |
1114 | .B ENOTDIR | |
1115 | A component used as a directory in | |
1116 | .I pathname | |
a8d55537 | 1117 | is not, in fact, a directory, or \fBO_DIRECTORY\fP was specified and |
fea681da MK |
1118 | .I pathname |
1119 | was not a directory. | |
1120 | .TP | |
1121 | .B ENXIO | |
682edefb | 1122 | .BR O_NONBLOCK " | " O_WRONLY |
103ea4f6 MK |
1123 | is set, the named file is a FIFO, and |
1124 | no process has the FIFO open for reading. | |
7b032b23 MK |
1125 | .TP |
1126 | .B ENXIO | |
1127 | The file is a device special file and no corresponding device exists. | |
fea681da | 1128 | .TP |
bbe02b45 MK |
1129 | .BR EOPNOTSUPP |
1130 | The filesystem containing | |
1131 | .I pathname | |
1132 | does not support | |
1133 | .BR O_TMPFILE . | |
1134 | .TP | |
7c7fb552 MK |
1135 | .B EOVERFLOW |
1136 | .I pathname | |
1137 | refers to a regular file that is too large to be opened. | |
1138 | The usual scenario here is that an application compiled | |
1139 | on a 32-bit platform without | |
5e4dc269 | 1140 | .I -D_FILE_OFFSET_BITS=64 |
7c7fb552 | 1141 | tried to open a file whose size exceeds |
4e1a4d72 MK |
1142 | .I (1<<31)-1 |
1143 | bytes; | |
7c7fb552 MK |
1144 | see also |
1145 | .B O_LARGEFILE | |
1146 | above. | |
c84d3aa3 | 1147 | This is the error specified by POSIX.1; |
7c7fb552 MK |
1148 | in kernels before 2.6.24, Linux gave the error |
1149 | .B EFBIG | |
1150 | for this case. | |
1151 | .\" See http://bugzilla.kernel.org/show_bug.cgi?id=7253 | |
1152 | .\" "Open of a large file on 32-bit fails with EFBIG, should be EOVERFLOW" | |
1153 | .\" Reported 2006-10-03 | |
1154 | .TP | |
1c1e15ed MK |
1155 | .B EPERM |
1156 | The | |
1157 | .B O_NOATIME | |
1158 | flag was specified, but the effective user ID of the caller | |
9ee4a2b6 | 1159 | .\" Strictly speaking, it's the filesystem UID... (MTK) |
47c906e5 | 1160 | did not match the owner of the file and the caller was not privileged. |
1c1e15ed | 1161 | .TP |
fbab10e5 MK |
1162 | .B EPERM |
1163 | The operation was prevented by a file seal; see | |
1164 | .BR fcntl (2). | |
1165 | .TP | |
fea681da MK |
1166 | .B EROFS |
1167 | .I pathname | |
9ee4a2b6 | 1168 | refers to a file on a read-only filesystem and write access was |
fea681da MK |
1169 | requested. |
1170 | .TP | |
1171 | .B ETXTBSY | |
1172 | .I pathname | |
1173 | refers to an executable image which is currently being executed and | |
1174 | write access was requested. | |
d3952311 | 1175 | .TP |
19d37126 JH |
1176 | .B ETXTBSY |
1177 | .I pathname | |
1178 | refers to a file that is currently in use as a swap file, and the | |
1179 | .B O_TRUNC | |
1180 | flag was specified. | |
1181 | .TP | |
1182 | .B ETXTBSY | |
1183 | .I pathname | |
1184 | refers to a file that is currently being read by the kernel (e.g. for | |
1185 | module/firmware loading), and write access was requested. | |
1186 | .TP | |
d3952311 MK |
1187 | .B EWOULDBLOCK |
1188 | The | |
1189 | .B O_NONBLOCK | |
1190 | flag was specified, and an incompatible lease was held on the file | |
1191 | (see | |
1192 | .BR fcntl (2)). | |
7b8ba76c MK |
1193 | .PP |
1194 | The following additional errors can occur for | |
1195 | .BR openat (): | |
1196 | .TP | |
1197 | .B EBADF | |
1198 | .I dirfd | |
1199 | is not a valid file descriptor. | |
1200 | .TP | |
1201 | .B ENOTDIR | |
1202 | .I pathname | |
2feae602 | 1203 | is a relative pathname and |
7b8ba76c MK |
1204 | .I dirfd |
1205 | is a file descriptor referring to a file other than a directory. | |
1206 | .SH VERSIONS | |
1207 | .BR openat () | |
1208 | was added to Linux in kernel 2.6.16; | |
1209 | library support was added to glibc in version 2.4. | |
47297adb | 1210 | .SH CONFORMING TO |
7b8ba76c MK |
1211 | .BR open (), |
1212 | .BR creat () | |
72ac7268 | 1213 | SVr4, 4.3BSD, POSIX.1-2001, POSIX.1-2008. |
5355ff82 | 1214 | .PP |
7b8ba76c MK |
1215 | .BR openat (): |
1216 | POSIX.1-2008. | |
5355ff82 | 1217 | .PP |
fea681da | 1218 | The |
72ac7268 | 1219 | .BR O_DIRECT , |
1c1e15ed | 1220 | .BR O_NOATIME , |
72ac7268 | 1221 | .BR O_PATH , |
fea681da | 1222 | and |
72ac7268 MK |
1223 | .BR O_TMPFILE |
1224 | flags are Linux-specific. | |
1225 | One must define | |
61b7c1e1 MK |
1226 | .B _GNU_SOURCE |
1227 | to obtain their definitions. | |
5355ff82 | 1228 | .PP |
9f91e36c | 1229 | The |
72ac7268 MK |
1230 | .BR O_CLOEXEC , |
1231 | .BR O_DIRECTORY , | |
1232 | and | |
1233 | .BR O_NOFOLLOW | |
1234 | flags are not specified in POSIX.1-2001, | |
1235 | but are specified in POSIX.1-2008. | |
1236 | Since glibc 2.12, one can obtain their definitions by defining either | |
1237 | .B _POSIX_C_SOURCE | |
1238 | with a value greater than or equal to 200809L or | |
1239 | .BR _XOPEN_SOURCE | |
1240 | with a value greater than or equal to 700. | |
1241 | In glibc 2.11 and earlier, one obtains the definitions by defining | |
1242 | .BR _GNU_SOURCE . | |
5355ff82 | 1243 | .PP |
72ac7268 MK |
1244 | As noted in |
1245 | .BR feature_test_macros (7), | |
84fc2a6e | 1246 | feature test macros such as |
72ac7268 MK |
1247 | .BR _POSIX_C_SOURCE , |
1248 | .BR _XOPEN_SOURCE , | |
1249 | and | |
fe75ec04 | 1250 | .B _GNU_SOURCE |
72ac7268 | 1251 | must be defined before including |
e417acb0 | 1252 | .I any |
72ac7268 | 1253 | header files. |
a1d5f77c | 1254 | .SH NOTES |
988db661 | 1255 | Under Linux, the |
a1d5f77c | 1256 | .B O_NONBLOCK |
3897a3f8 | 1257 | flag is sometimes used in cases where one wants to open |
a1d5f77c | 1258 | but does not necessarily have the intention to read or write. |
3897a3f8 MK |
1259 | For example, |
1260 | this may be used to open a device in order to get a file descriptor | |
a1d5f77c MK |
1261 | for use with |
1262 | .BR ioctl (2). | |
dd3568a1 | 1263 | .PP |
fea681da MK |
1264 | The (undefined) effect of |
1265 | .B O_RDONLY | O_TRUNC | |
c13182ef | 1266 | varies among implementations. |
bcdd964e | 1267 | On many systems the file is actually truncated. |
fea681da MK |
1268 | .\" Linux 2.0, 2.5: truncate |
1269 | .\" Solaris 5.7, 5.8: truncate | |
1270 | .\" Irix 6.5: truncate | |
1271 | .\" Tru64 5.1B: truncate | |
1272 | .\" HP-UX 11.22: truncate | |
1273 | .\" FreeBSD 4.7: truncate | |
5355ff82 | 1274 | .PP |
5dc8986d MK |
1275 | Note that |
1276 | .BR open () | |
1277 | can open device special files, but | |
1278 | .BR creat () | |
1279 | cannot create them; use | |
1280 | .BR mknod (2) | |
1281 | instead. | |
5355ff82 | 1282 | .PP |
5dc8986d MK |
1283 | If the file is newly created, its |
1284 | .IR st_atime , | |
1285 | .IR st_ctime , | |
1286 | .I st_mtime | |
1287 | fields | |
1288 | (respectively, time of last access, time of last status change, and | |
1289 | time of last modification; see | |
1290 | .BR stat (2)) | |
1291 | are set | |
1292 | to the current time, and so are the | |
1293 | .I st_ctime | |
1294 | and | |
1295 | .I st_mtime | |
1296 | fields of the | |
1297 | parent directory. | |
1298 | Otherwise, if the file is modified because of the | |
1299 | .B O_TRUNC | |
3a9c5a29 MK |
1300 | flag, its |
1301 | .I st_ctime | |
1302 | and | |
1303 | .I st_mtime | |
1304 | fields are set to the current time. | |
5355ff82 | 1305 | .PP |
aaf7a574 MK |
1306 | The files in the |
1307 | .I /proc/[pid]/fd | |
1308 | directory show the open file descriptors of the process with the PID | |
1309 | .IR pid . | |
1310 | The files in the | |
1311 | .I /proc/[pid]/fdinfo | |
1312 | directory show even more information about these files descriptors. | |
1313 | See | |
1314 | .BR proc (5) | |
1315 | for further details of both of these directories. | |
5dc8986d MK |
1316 | .\" |
1317 | .\" | |
d20d9d33 MK |
1318 | .SS Open file descriptions |
1319 | The term open file description is the one used by POSIX to refer to the | |
1320 | entries in the system-wide table of open files. | |
91085d85 | 1321 | In other contexts, this object is |
d20d9d33 MK |
1322 | variously also called an "open file object", |
1323 | a "file handle", an "open file table entry", | |
1324 | or\(emin kernel-developer parlance\(ema | |
1325 | .IR "struct file" . | |
5355ff82 | 1326 | .PP |
d20d9d33 MK |
1327 | When a file descriptor is duplicated (using |
1328 | .BR dup (2) | |
1329 | or similar), | |
1330 | the duplicate refers to the same open file description | |
1331 | as the original file descriptor, | |
1332 | and the two file descriptors consequently share | |
1333 | the file offset and file status flags. | |
1334 | Such sharing can also occur between processes: | |
1335 | a child process created via | |
91085d85 | 1336 | .BR fork (2) |
d20d9d33 MK |
1337 | inherits duplicates of its parent's file descriptors, |
1338 | and those duplicates refer to the same open file descriptions. | |
5355ff82 | 1339 | .PP |
d20d9d33 | 1340 | Each |
bf7bc8b8 | 1341 | .BR open () |
d20d9d33 MK |
1342 | of a file creates a new open file description; |
1343 | thus, there may be multiple open file descriptions | |
1344 | corresponding to a file inode. | |
5355ff82 | 1345 | .PP |
9539ebc9 MK |
1346 | On Linux, one can use the |
1347 | .BR kcmp (2) | |
1348 | .B KCMP_FILE | |
1349 | operation to test whether two file descriptors | |
1350 | (in the same process or in two different processes) | |
1351 | refer to the same open file description. | |
d20d9d33 MK |
1352 | .\" |
1353 | .\" | |
5dc8986d | 1354 | .SS Synchronized I/O |
6cf19e62 MK |
1355 | The POSIX.1-2008 "synchronized I/O" option |
1356 | specifies different variants of synchronized I/O, | |
1357 | and specifies the | |
1358 | .BR open () | |
1359 | flags | |
015221ef CH |
1360 | .BR O_SYNC , |
1361 | .BR O_DSYNC , | |
1362 | and | |
6cf19e62 MK |
1363 | .BR O_RSYNC |
1364 | for controlling the behavior. | |
1365 | Regardless of whether an implementation supports this option, | |
1366 | it must at least support the use of | |
1367 | .BR O_SYNC | |
1368 | for regular files. | |
5355ff82 | 1369 | .PP |
89851a00 | 1370 | Linux implements |
6cf19e62 MK |
1371 | .BR O_SYNC |
1372 | and | |
1373 | .BR O_DSYNC , | |
1374 | but not | |
015221ef | 1375 | .BR O_RSYNC . |
6cf19e62 MK |
1376 | (Somewhat incorrectly, glibc defines |
1377 | .BR O_RSYNC | |
1378 | to have the same value as | |
1379 | .BR O_SYNC .) | |
5355ff82 | 1380 | .PP |
6cf19e62 MK |
1381 | .BR O_SYNC |
1382 | provides synchronized I/O | |
1383 | .I file | |
1384 | integrity completion, | |
1385 | meaning write operations will flush data and all associated metadata | |
1386 | to the underlying hardware. | |
1387 | .BR O_DSYNC | |
1388 | provides synchronized I/O | |
1389 | .I data | |
1390 | integrity completion, | |
1391 | meaning write operations will flush data | |
1392 | to the underlying hardware, | |
1393 | but will only flush metadata updates that are required | |
1394 | to allow a subsequent read operation to complete successfully. | |
1395 | Data integrity completion can reduce the number of disk operations | |
1396 | that are required for applications that don't need the guarantees | |
1397 | of file integrity completion. | |
5355ff82 | 1398 | .PP |
a83923ca | 1399 | To understand the difference between the two types of completion, |
6cf19e62 MK |
1400 | consider two pieces of file metadata: |
1401 | the file last modification timestamp | |
1402 | .RI ( st_mtime ) | |
1403 | and the file length. | |
1404 | All write operations will update the last file modification timestamp, | |
1405 | but only writes that add data to the end of the | |
1406 | file will change the file length. | |
1407 | The last modification timestamp is not needed to ensure that | |
1408 | a read completes successfully, but the file length is. | |
1409 | Thus, | |
1410 | .BR O_DSYNC | |
1411 | would only guarantee to flush updates to the file length metadata | |
1412 | (whereas | |
1413 | .BR O_SYNC | |
1414 | would also always flush the last modification timestamp metadata). | |
5355ff82 | 1415 | .PP |
6cf19e62 MK |
1416 | Before Linux 2.6.33, Linux implemented only the |
1417 | .BR O_SYNC | |
89851a00 | 1418 | flag for |
6cf19e62 MK |
1419 | .BR open (). |
1420 | However, when that flag was specified, | |
1421 | most filesystems actually provided the equivalent of synchronized I/O | |
1422 | .I data | |
1423 | integrity completion (i.e., | |
1424 | .BR O_SYNC | |
1425 | was actually implemented as the equivalent of | |
1426 | .BR O_DSYNC ). | |
5355ff82 | 1427 | .PP |
6cf19e62 MK |
1428 | Since Linux 2.6.33, proper |
1429 | .BR O_SYNC | |
1430 | support is provided. | |
1431 | However, to ensure backward binary compatibility, | |
1432 | .BR O_DSYNC | |
1433 | was defined with the same value as the historical | |
015221ef | 1434 | .BR O_SYNC , |
015221ef | 1435 | and |
6cf19e62 | 1436 | .BR O_SYNC |
89851a00 | 1437 | was defined as a new (two-bit) flag value that includes the |
6cf19e62 MK |
1438 | .BR O_DSYNC |
1439 | flag value. | |
1440 | This ensures that applications compiled against | |
1441 | new headers get at least | |
1442 | .BR O_DSYNC | |
1443 | semantics on pre-2.6.33 kernels. | |
5dc8986d | 1444 | .\" |
76f054b1 MK |
1445 | .SS C library/kernel differences |
1446 | Since version 2.26, | |
1447 | the glibc wrapper function for | |
1448 | .BR open () | |
1449 | employs the | |
1450 | .BR openat () | |
1451 | system call, rather than the kernel's | |
1452 | .BR open () | |
1453 | system call. | |
1454 | For certain architectures, this is also true in glibc versions before 2.26. | |
5dc8986d MK |
1455 | .\" |
1456 | .SS NFS | |
1457 | There are many infelicities in the protocol underlying NFS, affecting | |
1458 | amongst others | |
1459 | .BR O_SYNC " and " O_NDELAY . | |
5355ff82 | 1460 | .PP |
9ee4a2b6 | 1461 | On NFS filesystems with UID mapping enabled, |
a1d5f77c MK |
1462 | .BR open () |
1463 | may | |
75b94dc3 | 1464 | return a file descriptor but, for example, |
a1d5f77c MK |
1465 | .BR read (2) |
1466 | requests are denied | |
1467 | with \fBEACCES\fP. | |
1468 | This is because the client performs | |
1469 | .BR open () | |
1470 | by checking the | |
1471 | permissions, but UID mapping is performed by the server upon | |
1472 | read and write requests. | |
5dc8986d MK |
1473 | .\" |
1474 | .\" | |
1bdc161d MK |
1475 | .SS FIFOs |
1476 | Opening the read or write end of a FIFO blocks until the other | |
1477 | end is also opened (by another process or thread). | |
1478 | See | |
1479 | .BR fifo (7) | |
1480 | for further details. | |
1481 | .\" | |
1482 | .\" | |
5dc8986d MK |
1483 | .SS File access mode |
1484 | Unlike the other values that can be specified in | |
1485 | .IR flags , | |
1486 | the | |
1487 | .I "access mode" | |
1488 | values | |
1489 | .BR O_RDONLY ", " O_WRONLY ", and " O_RDWR | |
1490 | do not specify individual bits. | |
1491 | Rather, they define the low order two bits of | |
1492 | .IR flags , | |
1493 | and are defined respectively as 0, 1, and 2. | |
1494 | In other words, the combination | |
1495 | .B "O_RDONLY | O_WRONLY" | |
1496 | is a logical error, and certainly does not have the same meaning as | |
1497 | .BR O_RDWR . | |
5355ff82 | 1498 | .PP |
5dc8986d MK |
1499 | Linux reserves the special, nonstandard access mode 3 (binary 11) in |
1500 | .I flags | |
1501 | to mean: | |
d9cb0d7d | 1502 | check for read and write permission on the file and return a file descriptor |
5dc8986d MK |
1503 | that can't be used for reading or writing. |
1504 | This nonstandard access mode is used by some Linux drivers to return a | |
d9cb0d7d | 1505 | file descriptor that is to be used only for device-specific |
5dc8986d MK |
1506 | .BR ioctl (2) |
1507 | operations. | |
1508 | .\" See for example util-linux's disk-utils/setfdprm.c | |
1509 | .\" For some background on access mode 3, see | |
1510 | .\" http://thread.gmane.org/gmane.linux.kernel/653123 | |
1511 | .\" "[RFC] correct flags to f_mode conversion in __dentry_open" | |
1512 | .\" LKML, 12 Mar 2008 | |
7b8ba76c MK |
1513 | .\" |
1514 | .\" | |
80d250b4 | 1515 | .SS Rationale for openat() and other "directory file descriptor" APIs |
7b8ba76c | 1516 | .BR openat () |
80d250b4 MK |
1517 | and the other system calls and library functions that take |
1518 | a directory file descriptor argument | |
7b8ba76c | 1519 | (i.e., |
c6a16783 | 1520 | .BR execveat (2), |
7b8ba76c | 1521 | .BR faccessat (2), |
80d250b4 | 1522 | .BR fanotify_mark (2), |
7b8ba76c MK |
1523 | .BR fchmodat (2), |
1524 | .BR fchownat (2), | |
1525 | .BR fstatat (2), | |
1526 | .BR futimesat (2), | |
1527 | .BR linkat (2), | |
1528 | .BR mkdirat (2), | |
1529 | .BR mknodat (2), | |
80d250b4 | 1530 | .BR name_to_handle_at (2), |
7b8ba76c MK |
1531 | .BR readlinkat (2), |
1532 | .BR renameat (2), | |
3f092cef | 1533 | .BR statx (2), |
7b8ba76c MK |
1534 | .BR symlinkat (2), |
1535 | .BR unlinkat (2), | |
f37759b1 | 1536 | .BR utimensat (2), |
80d250b4 | 1537 | .BR mkfifoat (3), |
7b8ba76c | 1538 | and |
80d250b4 | 1539 | .BR scandirat (3)) |
a98e0304 | 1540 | address two problems with the older interfaces that preceded them. |
92692952 | 1541 | Here, the explanation is in terms of the |
7b8ba76c | 1542 | .BR openat () |
d26f8a31 | 1543 | call, but the rationale is analogous for the other interfaces. |
5355ff82 | 1544 | .PP |
7b8ba76c MK |
1545 | First, |
1546 | .BR openat () | |
1547 | allows an application to avoid race conditions that could | |
1548 | occur when using | |
cadd38ba | 1549 | .BR open () |
7b8ba76c MK |
1550 | to open files in directories other than the current working directory. |
1551 | These race conditions result from the fact that some component | |
1552 | of the directory prefix given to | |
cadd38ba | 1553 | .BR open () |
7b8ba76c | 1554 | could be changed in parallel with the call to |
cadd38ba | 1555 | .BR open (). |
54305f5b | 1556 | Suppose, for example, that we wish to create the file |
a710e359 | 1557 | .I dir1/dir2/xxx.dep |
54305f5b | 1558 | if the file |
a710e359 | 1559 | .I dir1/dir2/xxx |
54305f5b | 1560 | exists. |
069d2f9a | 1561 | The problem is that between the existence check and the file-creation step, |
a710e359 | 1562 | .I dir1 |
54305f5b | 1563 | or |
a710e359 | 1564 | .I dir2 |
54305f5b MK |
1565 | (which might be symbolic links) |
1566 | could be modified to point to a different location. | |
7b8ba76c MK |
1567 | Such races can be avoided by |
1568 | opening a file descriptor for the target directory, | |
1569 | and then specifying that file descriptor as the | |
1570 | .I dirfd | |
54305f5b MK |
1571 | argument of (say) |
1572 | .BR fstatat (2) | |
1573 | and | |
7b8ba76c | 1574 | .BR openat (). |
941d2892 MK |
1575 | The use of the |
1576 | .I dirfd | |
1577 | file descriptor also has other benefits: | |
1578 | .IP * 3 | |
1579 | the file descriptor is a stable reference to the directory, | |
1580 | even if the directory is renamed; and | |
1581 | .IP * | |
1582 | the open file descriptor prevents the underlying filesystem from | |
1583 | being dismounted, | |
1584 | just as when a process has a current working directory on a filesystem. | |
1585 | .PP | |
7b8ba76c MK |
1586 | Second, |
1587 | .BR openat () | |
1588 | allows the implementation of a per-thread "current working | |
1589 | directory", via file descriptor(s) maintained by the application. | |
1590 | (This functionality can also be obtained by tricks based | |
1591 | on the use of | |
1592 | .IR /proc/self/fd/ dirfd, | |
1593 | but less efficiently.) | |
1594 | .\" | |
1595 | .\" | |
ddc4d339 | 1596 | .SS O_DIRECT |
dd3568a1 | 1597 | .PP |
ddc4d339 MK |
1598 | The |
1599 | .B O_DIRECT | |
1600 | flag may impose alignment restrictions on the length and address | |
7fac88a9 | 1601 | of user-space buffers and the file offset of I/Os. |
ddc4d339 | 1602 | In Linux alignment |
9ee4a2b6 | 1603 | restrictions vary by filesystem and kernel version and might be |
ddc4d339 | 1604 | absent entirely. |
9ee4a2b6 | 1605 | However there is currently no filesystem\-independent |
ddc4d339 | 1606 | interface for an application to discover these restrictions for a given |
9ee4a2b6 MK |
1607 | file or filesystem. |
1608 | Some filesystems provide their own interfaces | |
ddc4d339 MK |
1609 | for doing so, for example the |
1610 | .B XFS_IOC_DIOINFO | |
1611 | operation in | |
1612 | .BR xfsctl (3). | |
dd3568a1 | 1613 | .PP |
85c2bdba MK |
1614 | Under Linux 2.4, transfer sizes, and the alignment of the user buffer |
1615 | and the file offset must all be multiples of the logical block size | |
9ee4a2b6 | 1616 | of the filesystem. |
21557928 | 1617 | Since Linux 2.6.0, alignment to the logical block size of the |
e6042e4a | 1618 | underlying storage (typically 512 bytes) suffices. |
21557928 | 1619 | The logical block size can be determined using the |
e6042e4a PS |
1620 | .BR ioctl (2) |
1621 | .B BLKSSZGET | |
21557928 | 1622 | operation or from the shell using the command: |
5355ff82 MK |
1623 | .PP |
1624 | .EX | |
21557928 | 1625 | blockdev \-\-getss |
5355ff82 MK |
1626 | .EE |
1627 | .PP | |
1847167b NP |
1628 | .B O_DIRECT |
1629 | I/Os should never be run concurrently with the | |
04cd7f64 | 1630 | .BR fork (2) |
1847167b NP |
1631 | system call, |
1632 | if the memory buffer is a private mapping | |
1633 | (i.e., any mapping created with the | |
02ace852 | 1634 | .BR mmap (2) |
1847167b | 1635 | .BR MAP_PRIVATE |
0ab8aeec | 1636 | flag; |
1847167b NP |
1637 | this includes memory allocated on the heap and statically allocated buffers). |
1638 | Any such I/Os, whether submitted via an asynchronous I/O interface or from | |
1639 | another thread in the process, | |
1640 | should be completed before | |
1641 | .BR fork (2) | |
1642 | is called. | |
1643 | Failure to do so can result in data corruption and undefined behavior in | |
1644 | parent and child processes. | |
1645 | This restriction does not apply when the memory buffer for the | |
1646 | .B O_DIRECT | |
1647 | I/Os was created using | |
1648 | .BR shmat (2) | |
1649 | or | |
1650 | .BR mmap (2) | |
1651 | with the | |
1652 | .B MAP_SHARED | |
1653 | flag. | |
1654 | Nor does this restriction apply when the memory buffer has been advised as | |
1655 | .B MADV_DONTFORK | |
0ab8aeec | 1656 | with |
02ace852 | 1657 | .BR madvise (2), |
1847167b NP |
1658 | ensuring that it will not be available |
1659 | to the child after | |
1660 | .BR fork (2). | |
dd3568a1 | 1661 | .PP |
ddc4d339 MK |
1662 | The |
1663 | .B O_DIRECT | |
1664 | flag was introduced in SGI IRIX, where it has alignment | |
1665 | restrictions similar to those of Linux 2.4. | |
1666 | IRIX has also a | |
1667 | .BR fcntl (2) | |
1668 | call to query appropriate alignments, and sizes. | |
1669 | FreeBSD 4.x introduced | |
1670 | a flag of the same name, but without alignment restrictions. | |
dd3568a1 | 1671 | .PP |
ddc4d339 MK |
1672 | .B O_DIRECT |
1673 | support was added under Linux in kernel version 2.4.10. | |
1674 | Older Linux kernels simply ignore this flag. | |
fedb2ff5 | 1675 | Some filesystems may not implement the flag, in which case |
ddc4d339 | 1676 | .BR open () |
9e4be7e9 | 1677 | fails with the error |
ddc4d339 MK |
1678 | .B EINVAL |
1679 | if it is used. | |
dd3568a1 | 1680 | .PP |
ddc4d339 MK |
1681 | Applications should avoid mixing |
1682 | .B O_DIRECT | |
1683 | and normal I/O to the same file, | |
1684 | and especially to overlapping byte regions in the same file. | |
9ee4a2b6 | 1685 | Even when the filesystem correctly handles the coherency issues in |
ddc4d339 MK |
1686 | this situation, overall I/O throughput is likely to be slower than |
1687 | using either mode alone. | |
1688 | Likewise, applications should avoid mixing | |
1689 | .BR mmap (2) | |
1690 | of files with direct I/O to the same files. | |
dd3568a1 | 1691 | .PP |
a1fa36af | 1692 | The behavior of |
ddc4d339 | 1693 | .B O_DIRECT |
9ee4a2b6 | 1694 | with NFS will differ from local filesystems. |
ddc4d339 MK |
1695 | Older kernels, or |
1696 | kernels configured in certain ways, may not support this combination. | |
1697 | The NFS protocol does not support passing the flag to the server, so | |
1698 | .B O_DIRECT | |
33a0ccb2 | 1699 | I/O will bypass the page cache only on the client; the server may |
ddc4d339 MK |
1700 | still cache the I/O. |
1701 | The client asks the server to make the I/O | |
1702 | synchronous to preserve the synchronous semantics of | |
1703 | .BR O_DIRECT . | |
1704 | Some servers will perform poorly under these circumstances, especially | |
1705 | if the I/O size is small. | |
1706 | Some servers may also be configured to | |
1707 | lie to clients about the I/O having reached stable storage; this | |
1708 | will avoid the performance penalty at some risk to data integrity | |
1709 | in the event of server power failure. | |
1710 | The Linux NFS client places no alignment restrictions on | |
1711 | .B O_DIRECT | |
1712 | I/O. | |
1713 | .PP | |
1714 | In summary, | |
1715 | .B O_DIRECT | |
1716 | is a potentially powerful tool that should be used with caution. | |
1717 | It is recommended that applications treat use of | |
1718 | .B O_DIRECT | |
1719 | as a performance option which is disabled by default. | |
ddc4d339 | 1720 | .SH BUGS |
b50582eb MK |
1721 | Currently, it is not possible to enable signal-driven |
1722 | I/O by specifying | |
1723 | .B O_ASYNC | |
c13182ef | 1724 | when calling |
b50582eb MK |
1725 | .BR open (); |
1726 | use | |
1727 | .BR fcntl (2) | |
1728 | to enable this flag. | |
0e1ad98c | 1729 | .\" FIXME . Check bugzilla report on open(O_ASYNC) |
92057f4d | 1730 | .\" See http://bugzilla.kernel.org/show_bug.cgi?id=5993 |
5355ff82 | 1731 | .PP |
0d730fcc MK |
1732 | One must check for two different error codes, |
1733 | .B EISDIR | |
1734 | and | |
1735 | .BR ENOENT , | |
1736 | when trying to determine whether the kernel supports | |
0d55b37f | 1737 | .B O_TMPFILE |
0d730fcc | 1738 | functionality. |
5355ff82 | 1739 | .PP |
320f8a8e MK |
1740 | When both |
1741 | .B O_CREAT | |
1742 | and | |
1743 | .B O_DIRECTORY | |
1744 | are specified in | |
1745 | .IR flags | |
1746 | and the file specified by | |
1747 | .I pathname | |
1748 | does not exist, | |
1749 | .BR open () | |
1750 | will create a regular file (i.e., | |
1751 | .B O_DIRECTORY | |
1752 | is ignored). | |
47297adb | 1753 | .SH SEE ALSO |
a3bf8022 MK |
1754 | .BR chmod (2), |
1755 | .BR chown (2), | |
fea681da | 1756 | .BR close (2), |
e366dbc4 | 1757 | .BR dup (2), |
fea681da MK |
1758 | .BR fcntl (2), |
1759 | .BR link (2), | |
1f6ceb40 | 1760 | .BR lseek (2), |
fea681da | 1761 | .BR mknod (2), |
e366dbc4 | 1762 | .BR mmap (2), |
f0c34053 | 1763 | .BR mount (2), |
fa5d243f | 1764 | .BR open_by_handle_at (2), |
fea681da MK |
1765 | .BR read (2), |
1766 | .BR socket (2), | |
1767 | .BR stat (2), | |
1768 | .BR umask (2), | |
1769 | .BR unlink (2), | |
1770 | .BR write (2), | |
1771 | .BR fopen (3), | |
b31056e3 | 1772 | .BR acl (5), |
f0c34053 | 1773 | .BR fifo (7), |
3b363b62 | 1774 | .BR inode (7), |
a9cfde1d MK |
1775 | .BR path_resolution (7), |
1776 | .BR symlink (7) |