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