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