OSDN Git Service

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