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