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