OSDN Git Service

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