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