OSDN Git Service

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