chmod.2, chown.2, open.2, mkdir.2, mknod.2, readlink.2, stat.2, symlink.2, mkfifo...
[man-pages.git] / man2 / open.2
blob5eca172eccdfa209d2b7ca5ca37924274542388d
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.
930 .I dirfd
931 argument is used in conjunction with the
932 .I pathname
933 argument as follows:
934 .IP * 3
935 If the pathname given in
936 .I pathname
937 is absolute, then
938 .I dirfd
939 is ignored.
940 .IP *
941 If the pathname given in
942 .I pathname
943 is relative and
944 .I dirfd
945 is the special value
946 .BR AT_FDCWD ,
947 then
948 .I pathname
949 is interpreted relative to the current working
950 directory of the calling process (like
951 .BR open ()).
952 .IP *
953 If the pathname given in
954 .I pathname
955 is relative, then it is interpreted relative to the directory
956 referred to by the file descriptor
957 .I dirfd
958 (rather than relative to the current working directory of
959 the calling process, as is done by
960 .BR open ()
961 for a relative pathname).
962 In this case,
963 .I dirfd
964 must be a directory that was opened for reading
965 .RB ( O_RDONLY )
966 or using the
967 .B O_PATH
968 flag.
970 If the pathname given in
971 .I pathname
972 is relative, and
973 .I dirfd
974 is not a valid file descriptor, an error
975 .RB ( EBADF )
976 results.
977 (Specifying an invalid file descriptor number in
978 .I dirfd
979 can be used as a means to ensure that
980 .I pathname
981 is absolute.)
983 .SS openat2(2)
985 .BR openat2 (2)
986 system call is an extension of
987 .BR openat (),
988 and provides a superset of the features of
989 .BR openat ().
990 It is documented separately, in
991 .BR openat2 (2).
992 .SH RETURN VALUE
993 On success,
994 .BR open (),
995 .BR openat (),
997 .BR creat ()
998 return the new file descriptor (a nonnegative integer).
999 On error, \-1 is returned and
1000 .I errno
1001 is set to indicate the error.
1002 .SH ERRORS
1003 .BR open (),
1004 .BR openat (),
1006 .BR creat ()
1007 can fail with the following errors:
1009 .B EACCES
1010 The requested access to the file is not allowed, or search permission
1011 is denied for one of the directories in the path prefix of
1012 .IR pathname ,
1013 or the file did not exist yet and write access to the parent directory
1014 is not allowed.
1015 (See also
1016 .BR path_resolution (7).)
1018 .B EACCES
1019 .\" commit 30aba6656f61ed44cba445a3c0d38b296fa9e8f5
1020 Where
1021 .B O_CREAT
1022 is specified, the
1023 .I protected_fifos
1025 .I protected_regular
1026 sysctl is enabled, the file already exists and is a FIFO or regular file, the
1027 owner of the file is neither the current user nor the owner of the
1028 containing directory, and the containing directory is both world- or
1029 group-writable and sticky.
1030 For details, see the descriptions of
1031 .IR /proc/sys/fs/protected_fifos
1033 .IR /proc/sys/fs/protected_regular
1035 .BR proc (5).
1037 .B EBADF
1038 .RB ( openat ())
1039 .I pathname
1040 is relative but
1041 .I dirfd
1042 is neither
1043 .B AT_FDCWD
1044 nor a valid file descriptor.
1046 .B EBUSY
1047 .B O_EXCL
1048 was specified in
1049 .I flags
1051 .I pathname
1052 refers to a block device that is in use by the system (e.g., it is mounted).
1054 .B EDQUOT
1055 Where
1056 .B O_CREAT
1057 is specified, the file does not exist, and the user's quota of disk
1058 blocks or inodes on the filesystem has been exhausted.
1060 .B EEXIST
1061 .I pathname
1062 already exists and
1063 .BR O_CREAT " and " O_EXCL
1064 were used.
1066 .B EFAULT
1067 .I pathname
1068 points outside your accessible address space.
1070 .B EFBIG
1072 .BR EOVERFLOW .
1074 .B EINTR
1075 While blocked waiting to complete an open of a slow device
1076 (e.g., a FIFO; see
1077 .BR fifo (7)),
1078 the call was interrupted by a signal handler; see
1079 .BR signal (7).
1081 .B EINVAL
1082 The filesystem does not support the
1083 .BR O_DIRECT
1084 flag.
1086 .BR NOTES
1087 for more information.
1089 .B EINVAL
1090 Invalid value in
1091 .\" In particular, __O_TMPFILE instead of O_TMPFILE
1092 .IR flags .
1094 .B EINVAL
1095 .B O_TMPFILE
1096 was specified in
1097 .IR flags ,
1098 but neither
1099 .B O_WRONLY
1101 .B O_RDWR
1102 was specified.
1104 .B EINVAL
1105 .B O_CREAT
1106 was specified in
1107 .I flags
1108 and the final component ("basename") of the new file's
1109 .I pathname
1110 is invalid
1111 (e.g., it contains characters not permitted by the underlying filesystem).
1113 .B EINVAL
1114 The final component ("basename") of
1115 .I pathname
1116 is invalid
1117 (e.g., it contains characters not permitted by the underlying filesystem).
1119 .B EISDIR
1120 .I pathname
1121 refers to a directory and the access requested involved writing
1122 (that is,
1123 .B O_WRONLY
1125 .B O_RDWR
1126 is set).
1128 .B EISDIR
1129 .I pathname
1130 refers to an existing directory,
1131 .B O_TMPFILE
1132 and one of
1133 .B O_WRONLY
1135 .B O_RDWR
1136 were specified in
1137 .IR flags ,
1138 but this kernel version does not provide the
1139 .B O_TMPFILE
1140 functionality.
1142 .B ELOOP
1143 Too many symbolic links were encountered in resolving
1144 .IR pathname .
1146 .B ELOOP
1147 .I pathname
1148 was a symbolic link, and
1149 .I flags
1150 specified
1151 .BR O_NOFOLLOW
1152 but not
1153 .BR O_PATH .
1155 .B EMFILE
1156 The per-process limit on the number of open file descriptors has been reached
1157 (see the description of
1158 .BR RLIMIT_NOFILE
1160 .BR getrlimit (2)).
1162 .B ENAMETOOLONG
1163 .I pathname
1164 was too long.
1166 .B ENFILE
1167 The system-wide limit on the total number of open files has been reached.
1169 .B ENODEV
1170 .I pathname
1171 refers to a device special file and no corresponding device exists.
1172 (This is a Linux kernel bug; in this situation
1173 .B ENXIO
1174 must be returned.)
1176 .B ENOENT
1177 .B O_CREAT
1178 is not set and the named file does not exist.
1180 .B ENOENT
1181 A directory component in
1182 .I pathname
1183 does not exist or is a dangling symbolic link.
1185 .B ENOENT
1186 .I pathname
1187 refers to a nonexistent directory,
1188 .B O_TMPFILE
1189 and one of
1190 .B O_WRONLY
1192 .B O_RDWR
1193 were specified in
1194 .IR flags ,
1195 but this kernel version does not provide the
1196 .B O_TMPFILE
1197 functionality.
1199 .B ENOMEM
1200 The named file is a FIFO,
1201 but memory for the FIFO buffer can't be allocated because
1202 the per-user hard limit on memory allocation for pipes has been reached
1203 and the caller is not privileged; see
1204 .BR pipe (7).
1206 .B ENOMEM
1207 Insufficient kernel memory was available.
1209 .B ENOSPC
1210 .I pathname
1211 was to be created but the device containing
1212 .I pathname
1213 has no room for the new file.
1215 .B ENOTDIR
1216 A component used as a directory in
1217 .I pathname
1218 is not, in fact, a directory, or \fBO_DIRECTORY\fP was specified and
1219 .I pathname
1220 was not a directory.
1222 .B ENOTDIR
1223 .RB ( openat ())
1224 .I pathname
1225 is a relative pathname and
1226 .I dirfd
1227 is a file descriptor referring to a file other than a directory.
1229 .B ENXIO
1230 .BR O_NONBLOCK " | " O_WRONLY
1231 is set, the named file is a FIFO, and
1232 no process has the FIFO open for reading.
1234 .B ENXIO
1235 The file is a device special file and no corresponding device exists.
1237 .B ENXIO
1238 The file is a UNIX domain socket.
1240 .BR EOPNOTSUPP
1241 The filesystem containing
1242 .I pathname
1243 does not support
1244 .BR O_TMPFILE .
1246 .B EOVERFLOW
1247 .I pathname
1248 refers to a regular file that is too large to be opened.
1249 The usual scenario here is that an application compiled
1250 on a 32-bit platform without
1251 .I \-D_FILE_OFFSET_BITS=64
1252 tried to open a file whose size exceeds
1253 .I (1<<31)\-1
1254 bytes;
1255 see also
1256 .B O_LARGEFILE
1257 above.
1258 This is the error specified by POSIX.1;
1259 in kernels before 2.6.24, Linux gave the error
1260 .B EFBIG
1261 for this case.
1262 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=7253
1263 .\" "Open of a large file on 32-bit fails with EFBIG, should be EOVERFLOW"
1264 .\" Reported 2006-10-03
1266 .B EPERM
1268 .B O_NOATIME
1269 flag was specified, but the effective user ID of the caller
1270 .\" Strictly speaking, it's the filesystem UID... (MTK)
1271 did not match the owner of the file and the caller was not privileged.
1273 .B EPERM
1274 The operation was prevented by a file seal; see
1275 .BR fcntl (2).
1277 .B EROFS
1278 .I pathname
1279 refers to a file on a read-only filesystem and write access was
1280 requested.
1282 .B ETXTBSY
1283 .I pathname
1284 refers to an executable image which is currently being executed and
1285 write access was requested.
1287 .B ETXTBSY
1288 .I pathname
1289 refers to a file that is currently in use as a swap file, and the
1290 .B O_TRUNC
1291 flag was specified.
1293 .B ETXTBSY
1294 .I pathname
1295 refers to a file that is currently being read by the kernel (e.g., for
1296 module/firmware loading), and write access was requested.
1298 .B EWOULDBLOCK
1300 .B O_NONBLOCK
1301 flag was specified, and an incompatible lease was held on the file
1302 (see
1303 .BR fcntl (2)).
1304 .SH VERSIONS
1305 .BR openat ()
1306 was added to Linux in kernel 2.6.16;
1307 library support was added to glibc in version 2.4.
1308 .SH CONFORMING TO
1309 .BR open (),
1310 .BR creat ()
1311 SVr4, 4.3BSD, POSIX.1-2001, POSIX.1-2008.
1313 .BR openat ():
1314 POSIX.1-2008.
1316 .BR openat2 (2)
1317 is Linux-specific.
1320 .BR O_DIRECT ,
1321 .BR O_NOATIME ,
1322 .BR O_PATH ,
1324 .BR O_TMPFILE
1325 flags are Linux-specific.
1326 One must define
1327 .B _GNU_SOURCE
1328 to obtain their definitions.
1331 .BR O_CLOEXEC ,
1332 .BR O_DIRECTORY ,
1334 .BR O_NOFOLLOW
1335 flags are not specified in POSIX.1-2001,
1336 but are specified in POSIX.1-2008.
1337 Since glibc 2.12, one can obtain their definitions by defining either
1338 .B _POSIX_C_SOURCE
1339 with a value greater than or equal to 200809L or
1340 .BR _XOPEN_SOURCE
1341 with a value greater than or equal to 700.
1342 In glibc 2.11 and earlier, one obtains the definitions by defining
1343 .BR _GNU_SOURCE .
1345 As noted in
1346 .BR feature_test_macros (7),
1347 feature test macros such as
1348 .BR _POSIX_C_SOURCE ,
1349 .BR _XOPEN_SOURCE ,
1351 .B _GNU_SOURCE
1352 must be defined before including
1353 .I any
1354 header files.
1355 .SH NOTES
1356 Under Linux, the
1357 .B O_NONBLOCK
1358 flag is sometimes used in cases where one wants to open
1359 but does not necessarily have the intention to read or write.
1360 For example,
1361 this may be used to open a device in order to get a file descriptor
1362 for use with
1363 .BR ioctl (2).
1365 The (undefined) effect of
1366 .B O_RDONLY | O_TRUNC
1367 varies among implementations.
1368 On many systems the file is actually truncated.
1369 .\" Linux 2.0, 2.5: truncate
1370 .\" Solaris 5.7, 5.8: truncate
1371 .\" Irix 6.5: truncate
1372 .\" Tru64 5.1B: truncate
1373 .\" HP-UX 11.22: truncate
1374 .\" FreeBSD 4.7: truncate
1376 Note that
1377 .BR open ()
1378 can open device special files, but
1379 .BR creat ()
1380 cannot create them; use
1381 .BR mknod (2)
1382 instead.
1384 If the file is newly created, its
1385 .IR st_atime ,
1386 .IR st_ctime ,
1387 .I st_mtime
1388 fields
1389 (respectively, time of last access, time of last status change, and
1390 time of last modification; see
1391 .BR stat (2))
1392 are set
1393 to the current time, and so are the
1394 .I st_ctime
1396 .I st_mtime
1397 fields of the
1398 parent directory.
1399 Otherwise, if the file is modified because of the
1400 .B O_TRUNC
1401 flag, its
1402 .I st_ctime
1404 .I st_mtime
1405 fields are set to the current time.
1407 The files in the
1408 .I /proc/[pid]/fd
1409 directory show the open file descriptors of the process with the PID
1410 .IR pid .
1411 The files in the
1412 .I /proc/[pid]/fdinfo
1413 directory show even more information about these file descriptors.
1415 .BR proc (5)
1416 for further details of both of these directories.
1418 The Linux header file
1419 .B <asm/fcntl.h>
1420 doesn't define
1421 .BR O_ASYNC ;
1422 the (BSD-derived)
1423 .B FASYNC
1424 synonym is defined instead.
1427 .SS Open file descriptions
1428 The term open file description is the one used by POSIX to refer to the
1429 entries in the system-wide table of open files.
1430 In other contexts, this object is
1431 variously also called an "open file object",
1432 a "file handle", an "open file table entry",
1433 or\(emin kernel-developer parlance\(ema
1434 .IR "struct file" .
1436 When a file descriptor is duplicated (using
1437 .BR dup (2)
1438 or similar),
1439 the duplicate refers to the same open file description
1440 as the original file descriptor,
1441 and the two file descriptors consequently share
1442 the file offset and file status flags.
1443 Such sharing can also occur between processes:
1444 a child process created via
1445 .BR fork (2)
1446 inherits duplicates of its parent's file descriptors,
1447 and those duplicates refer to the same open file descriptions.
1449 Each
1450 .BR open ()
1451 of a file creates a new open file description;
1452 thus, there may be multiple open file descriptions
1453 corresponding to a file inode.
1455 On Linux, one can use the
1456 .BR kcmp (2)
1457 .B KCMP_FILE
1458 operation to test whether two file descriptors
1459 (in the same process or in two different processes)
1460 refer to the same open file description.
1463 .SS Synchronized I/O
1464 The POSIX.1-2008 "synchronized I/O" option
1465 specifies different variants of synchronized I/O,
1466 and specifies the
1467 .BR open ()
1468 flags
1469 .BR O_SYNC ,
1470 .BR O_DSYNC ,
1472 .BR O_RSYNC
1473 for controlling the behavior.
1474 Regardless of whether an implementation supports this option,
1475 it must at least support the use of
1476 .BR O_SYNC
1477 for regular files.
1479 Linux implements
1480 .BR O_SYNC
1482 .BR O_DSYNC ,
1483 but not
1484 .BR O_RSYNC .
1485 Somewhat incorrectly, glibc defines
1486 .BR O_RSYNC
1487 to have the same value as
1488 .BR O_SYNC .
1489 .RB ( O_RSYNC
1490 is defined in the Linux header file
1491 .I <asm/fcntl.h>
1492 on HP PA-RISC, but it is not used.)
1494 .BR O_SYNC
1495 provides synchronized I/O
1496 .I file
1497 integrity completion,
1498 meaning write operations will flush data and all associated metadata
1499 to the underlying hardware.
1500 .BR O_DSYNC
1501 provides synchronized I/O
1502 .I data
1503 integrity completion,
1504 meaning write operations will flush data
1505 to the underlying hardware,
1506 but will only flush metadata updates that are required
1507 to allow a subsequent read operation to complete successfully.
1508 Data integrity completion can reduce the number of disk operations
1509 that are required for applications that don't need the guarantees
1510 of file integrity completion.
1512 To understand the difference between the two types of completion,
1513 consider two pieces of file metadata:
1514 the file last modification timestamp
1515 .RI ( st_mtime )
1516 and the file length.
1517 All write operations will update the last file modification timestamp,
1518 but only writes that add data to the end of the
1519 file will change the file length.
1520 The last modification timestamp is not needed to ensure that
1521 a read completes successfully, but the file length is.
1522 Thus,
1523 .BR O_DSYNC
1524 would only guarantee to flush updates to the file length metadata
1525 (whereas
1526 .BR O_SYNC
1527 would also always flush the last modification timestamp metadata).
1529 Before Linux 2.6.33, Linux implemented only the
1530 .BR O_SYNC
1531 flag for
1532 .BR open ().
1533 However, when that flag was specified,
1534 most filesystems actually provided the equivalent of synchronized I/O
1535 .I data
1536 integrity completion (i.e.,
1537 .BR O_SYNC
1538 was actually implemented as the equivalent of
1539 .BR O_DSYNC ).
1541 Since Linux 2.6.33, proper
1542 .BR O_SYNC
1543 support is provided.
1544 However, to ensure backward binary compatibility,
1545 .BR O_DSYNC
1546 was defined with the same value as the historical
1547 .BR O_SYNC ,
1549 .BR O_SYNC
1550 was defined as a new (two-bit) flag value that includes the
1551 .BR O_DSYNC
1552 flag value.
1553 This ensures that applications compiled against
1554 new headers get at least
1555 .BR O_DSYNC
1556 semantics on pre-2.6.33 kernels.
1558 .SS C library/kernel differences
1559 Since version 2.26,
1560 the glibc wrapper function for
1561 .BR open ()
1562 employs the
1563 .BR openat ()
1564 system call, rather than the kernel's
1565 .BR open ()
1566 system call.
1567 For certain architectures, this is also true in glibc versions before 2.26.
1569 .SS NFS
1570 There are many infelicities in the protocol underlying NFS, affecting
1571 amongst others
1572 .BR O_SYNC " and " O_NDELAY .
1574 On NFS filesystems with UID mapping enabled,
1575 .BR open ()
1577 return a file descriptor but, for example,
1578 .BR read (2)
1579 requests are denied
1580 with \fBEACCES\fP.
1581 This is because the client performs
1582 .BR open ()
1583 by checking the
1584 permissions, but UID mapping is performed by the server upon
1585 read and write requests.
1588 .SS FIFOs
1589 Opening the read or write end of a FIFO blocks until the other
1590 end is also opened (by another process or thread).
1592 .BR fifo (7)
1593 for further details.
1596 .SS File access mode
1597 Unlike the other values that can be specified in
1598 .IR flags ,
1600 .I "access mode"
1601 values
1602 .BR O_RDONLY ", " O_WRONLY ", and " O_RDWR
1603 do not specify individual bits.
1604 Rather, they define the low order two bits of
1605 .IR flags ,
1606 and are defined respectively as 0, 1, and 2.
1607 In other words, the combination
1608 .B "O_RDONLY | O_WRONLY"
1609 is a logical error, and certainly does not have the same meaning as
1610 .BR O_RDWR .
1612 Linux reserves the special, nonstandard access mode 3 (binary 11) in
1613 .I flags
1614 to mean:
1615 check for read and write permission on the file and return a file descriptor
1616 that can't be used for reading or writing.
1617 This nonstandard access mode is used by some Linux drivers to return a
1618 file descriptor that is to be used only for device-specific
1619 .BR ioctl (2)
1620 operations.
1621 .\" See for example util-linux's disk-utils/setfdprm.c
1622 .\" For some background on access mode 3, see
1623 .\" http://thread.gmane.org/gmane.linux.kernel/653123
1624 .\" "[RFC] correct flags to f_mode conversion in __dentry_open"
1625 .\" LKML, 12 Mar 2008
1628 .SS Rationale for openat() and other "directory file descriptor" APIs
1629 .BR openat ()
1630 and the other system calls and library functions that take
1631 a directory file descriptor argument
1632 (i.e.,
1633 .BR execveat (2),
1634 .BR faccessat (2),
1635 .BR fanotify_mark (2),
1636 .BR fchmodat (2),
1637 .BR fchownat (2),
1638 .BR fspick (2),
1639 .BR fstatat (2),
1640 .BR futimesat (2),
1641 .BR linkat (2),
1642 .BR mkdirat (2),
1643 .BR mknodat (2),
1644 .BR mount_setattr (2),
1645 .BR move_mount (2),
1646 .BR name_to_handle_at (2),
1647 .BR open_tree (2),
1648 .BR openat2 (2),
1649 .BR readlinkat (2),
1650 .BR renameat (2),
1651 .BR renameat2 (2),
1652 .BR statx (2),
1653 .BR symlinkat (2),
1654 .BR unlinkat (2),
1655 .BR utimensat (2),
1656 .BR mkfifoat (3),
1658 .BR scandirat (3))
1659 address two problems with the older interfaces that preceded them.
1660 Here, the explanation is in terms of the
1661 .BR openat ()
1662 call, but the rationale is analogous for the other interfaces.
1664 First,
1665 .BR openat ()
1666 allows an application to avoid race conditions that could
1667 occur when using
1668 .BR open ()
1669 to open files in directories other than the current working directory.
1670 These race conditions result from the fact that some component
1671 of the directory prefix given to
1672 .BR open ()
1673 could be changed in parallel with the call to
1674 .BR open ().
1675 Suppose, for example, that we wish to create the file
1676 .I dir1/dir2/xxx.dep
1677 if the file
1678 .I dir1/dir2/xxx
1679 exists.
1680 The problem is that between the existence check and the file-creation step,
1681 .I dir1
1683 .I dir2
1684 (which might be symbolic links)
1685 could be modified to point to a different location.
1686 Such races can be avoided by
1687 opening a file descriptor for the target directory,
1688 and then specifying that file descriptor as the
1689 .I dirfd
1690 argument of (say)
1691 .BR fstatat (2)
1693 .BR openat ().
1694 The use of the
1695 .I dirfd
1696 file descriptor also has other benefits:
1697 .IP * 3
1698 the file descriptor is a stable reference to the directory,
1699 even if the directory is renamed; and
1700 .IP *
1701 the open file descriptor prevents the underlying filesystem from
1702 being dismounted,
1703 just as when a process has a current working directory on a filesystem.
1705 Second,
1706 .BR openat ()
1707 allows the implementation of a per-thread "current working
1708 directory", via file descriptor(s) maintained by the application.
1709 (This functionality can also be obtained by tricks based
1710 on the use of
1711 .IR /proc/self/fd/ dirfd,
1712 but less efficiently.)
1715 .I dirfd
1716 argument for these APIs can be obtained by using
1717 .BR open ()
1719 .BR openat ()
1720 to open a directory (with either the
1721 .BR O_RDONLY
1722 or the
1723 .BR O_PATH
1724 flag).
1725 Alternatively, such a file descriptor can be obtained by applying
1726 .BR dirfd (3)
1727 to a directory stream created using
1728 .BR opendir (3).
1730 When these APIs are given a
1731 .I dirfd
1732 argument of
1733 .BR AT_FDCWD
1734 or the specified pathname is absolute,
1735 then they handle their pathname argument in the same way as
1736 the corresponding conventional APIs.
1737 However, in this case, several of the APIs have a
1738 .I flags
1739 argument that provides access to functionality that is not available with
1740 the corresponding conventional APIs.
1743 .SS O_DIRECT
1745 .B O_DIRECT
1746 flag may impose alignment restrictions on the length and address
1747 of user-space buffers and the file offset of I/Os.
1748 In Linux alignment
1749 restrictions vary by filesystem and kernel version and might be
1750 absent entirely.
1751 However there is currently no filesystem\-independent
1752 interface for an application to discover these restrictions for a given
1753 file or filesystem.
1754 Some filesystems provide their own interfaces
1755 for doing so, for example the
1756 .B XFS_IOC_DIOINFO
1757 operation in
1758 .BR xfsctl (3).
1760 Under Linux 2.4, transfer sizes, the alignment of the user buffer,
1761 and the file offset must all be multiples of the logical block size
1762 of the filesystem.
1763 Since Linux 2.6.0, alignment to the logical block size of the
1764 underlying storage (typically 512 bytes) suffices.
1765 The logical block size can be determined using the
1766 .BR ioctl (2)
1767 .B BLKSSZGET
1768 operation or from the shell using the command:
1770 .in +4n
1772 blockdev \-\-getss
1776 .B O_DIRECT
1777 I/Os should never be run concurrently with the
1778 .BR fork (2)
1779 system call,
1780 if the memory buffer is a private mapping
1781 (i.e., any mapping created with the
1782 .BR mmap (2)
1783 .BR MAP_PRIVATE
1784 flag;
1785 this includes memory allocated on the heap and statically allocated buffers).
1786 Any such I/Os, whether submitted via an asynchronous I/O interface or from
1787 another thread in the process,
1788 should be completed before
1789 .BR fork (2)
1790 is called.
1791 Failure to do so can result in data corruption and undefined behavior in
1792 parent and child processes.
1793 This restriction does not apply when the memory buffer for the
1794 .B O_DIRECT
1795 I/Os was created using
1796 .BR shmat (2)
1798 .BR mmap (2)
1799 with the
1800 .B MAP_SHARED
1801 flag.
1802 Nor does this restriction apply when the memory buffer has been advised as
1803 .B MADV_DONTFORK
1804 with
1805 .BR madvise (2),
1806 ensuring that it will not be available
1807 to the child after
1808 .BR fork (2).
1811 .B O_DIRECT
1812 flag was introduced in SGI IRIX, where it has alignment
1813 restrictions similar to those of Linux 2.4.
1814 IRIX has also a
1815 .BR fcntl (2)
1816 call to query appropriate alignments, and sizes.
1817 FreeBSD 4.x introduced
1818 a flag of the same name, but without alignment restrictions.
1820 .B O_DIRECT
1821 support was added under Linux in kernel version 2.4.10.
1822 Older Linux kernels simply ignore this flag.
1823 Some filesystems may not implement the flag, in which case
1824 .BR open ()
1825 fails with the error
1826 .B EINVAL
1827 if it is used.
1829 Applications should avoid mixing
1830 .B O_DIRECT
1831 and normal I/O to the same file,
1832 and especially to overlapping byte regions in the same file.
1833 Even when the filesystem correctly handles the coherency issues in
1834 this situation, overall I/O throughput is likely to be slower than
1835 using either mode alone.
1836 Likewise, applications should avoid mixing
1837 .BR mmap (2)
1838 of files with direct I/O to the same files.
1840 The behavior of
1841 .B O_DIRECT
1842 with NFS will differ from local filesystems.
1843 Older kernels, or
1844 kernels configured in certain ways, may not support this combination.
1845 The NFS protocol does not support passing the flag to the server, so
1846 .B O_DIRECT
1847 I/O will bypass the page cache only on the client; the server may
1848 still cache the I/O.
1849 The client asks the server to make the I/O
1850 synchronous to preserve the synchronous semantics of
1851 .BR O_DIRECT .
1852 Some servers will perform poorly under these circumstances, especially
1853 if the I/O size is small.
1854 Some servers may also be configured to
1855 lie to clients about the I/O having reached stable storage; this
1856 will avoid the performance penalty at some risk to data integrity
1857 in the event of server power failure.
1858 The Linux NFS client places no alignment restrictions on
1859 .B O_DIRECT
1860 I/O.
1862 In summary,
1863 .B O_DIRECT
1864 is a potentially powerful tool that should be used with caution.
1865 It is recommended that applications treat use of
1866 .B O_DIRECT
1867 as a performance option which is disabled by default.
1868 .SH BUGS
1869 Currently, it is not possible to enable signal-driven
1870 I/O by specifying
1871 .B O_ASYNC
1872 when calling
1873 .BR open ();
1875 .BR fcntl (2)
1876 to enable this flag.
1877 .\" FIXME . Check bugzilla report on open(O_ASYNC)
1878 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=5993
1880 One must check for two different error codes,
1881 .B EISDIR
1883 .BR ENOENT ,
1884 when trying to determine whether the kernel supports
1885 .B O_TMPFILE
1886 functionality.
1888 When both
1889 .B O_CREAT
1891 .B O_DIRECTORY
1892 are specified in
1893 .IR flags
1894 and the file specified by
1895 .I pathname
1896 does not exist,
1897 .BR open ()
1898 will create a regular file (i.e.,
1899 .B O_DIRECTORY
1900 is ignored).
1901 .SH SEE ALSO
1902 .BR chmod (2),
1903 .BR chown (2),
1904 .BR close (2),
1905 .BR dup (2),
1906 .BR fcntl (2),
1907 .BR link (2),
1908 .BR lseek (2),
1909 .BR mknod (2),
1910 .BR mmap (2),
1911 .BR mount (2),
1912 .BR open_by_handle_at (2),
1913 .BR openat2 (2),
1914 .BR read (2),
1915 .BR socket (2),
1916 .BR stat (2),
1917 .BR umask (2),
1918 .BR unlink (2),
1919 .BR write (2),
1920 .BR fopen (3),
1921 .BR acl (5),
1922 .BR fifo (7),
1923 .BR inode (7),
1924 .BR path_resolution (7),
1925 .BR symlink (7)