1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
4 .\" 1993 Michael Haardt, Ian Jackson.
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" Modified 1993-07-21 by Rik Faith <faith@cs.unc.edu>
27 .\" Modified 1994-08-21 by Michael Haardt
28 .\" Modified 1996-04-13 by Andries Brouwer <aeb@cwi.nl>
29 .\" Modified 1996-05-13 by Thomas Koenig
30 .\" Modified 1996-12-20 by Michael Haardt
31 .\" Modified 1999-02-19 by Andries Brouwer <aeb@cwi.nl>
32 .\" Modified 1998-11-28 by Joseph S. Myers <jsm28@hermes.cam.ac.uk>
33 .\" Modified 1999-06-03 by Michael Haardt
34 .\" Modified 2002-05-07 by Michael Kerrisk <mtk-manpages@gmx.net>
35 .\" Modified 2004-06-23 by Michael Kerrisk <mtk-manpages@gmx.net>
36 .\" 2004-12-08, mtk, reordered flags list alphabetically
37 .\" 2004-12-08, Martin Pool <mbp@sourcefrog.net> (& mtk), added O_NOATIME
39 .TH OPEN 2 2005-06-22 "Linux" "Linux Programmer's Manual"
41 open, creat \- open and possibly create a file or device
44 .B #include <sys/types.h>
45 .B #include <sys/stat.h>
48 .BI "int open(const char *" pathname ", int " flags );
49 .BI "int open(const char *" pathname ", int " flags ", mode_t " mode );
50 .BI "int creat(const char *" pathname ", mode_t " mode );
57 returns a file descriptor, a small, non-negative integer
58 for use in subsequent system calls
59 .RB ( read "(2), " write "(2), " lseek "(2), " fcntl "(2), etc.)."
60 The file descriptor returned by a successful call will be
61 the lowest-numbered file descriptor not currently open for the process.
63 The new file descriptor is set to remain open across an
67 file descriptor flag described in
69 is initially disabled).
70 The file offset is set to the beginning of the file (see
76 .IR "open file description" ,
77 an entry in the system-wide table of open files.
78 This entry records the file offset and the file status flags
83 A file descriptor is a reference to one of these entries;
84 this reference is unaffected if
86 is subsequently removed or modified to refer to a different file.
87 The new open file description is initially not shared
88 with any other process,
89 but sharing may arise via
94 must include one of the following
96 .BR O_RDONLY ", " O_WRONLY ", or " O_RDWR.
97 These request opening the file read-only, write-only, or read/write,
100 In addition, zero or more file creation flags and file status flags
106 .I file creation flags
108 .BR O_CREAT ", " O_EXCL ", " O_NOCTTY ", and " O_TRUNC .
111 are all of the remaining flags listed below.
112 The distinction between these two groups of flags is that
113 the file status flags can be retrieved and (in some cases)
116 The full list of file creation flags and file status flags is as follows:
119 The file is opened in append mode.
122 the file offset is positioned at the end of the file,
126 may lead to corrupted files on NFS file systems if more than one process
127 appends data to a file at once.
128 This is because NFS does not support
129 appending to a file, so the client kernel has to simulate it, which
130 can't be done without a race condition.
133 Enable signal-driven I/O:
136 by default, but this can be changed via
138 when input or output becomes possible on this file descriptor.
139 This feature is only available for terminals, pseudo-terminals,
140 sockets, and (since Linux 2.6) pipes and FIFOs.
146 If the file does not exist it will be created.
147 The owner (user ID) of the file is set to the effective user ID
149 The group ownership (group ID) is set either to
150 the effective group ID of the process or to the group ID of the
151 parent directory (depending on filesystem type and mount options,
152 and the mode of the parent directory, see, for example, the mount options
156 of the ext2 filesystem, as described in
160 Try to minimize cache effects of the I/O to and from this file.
161 In general this will degrade performance, but it is useful in
162 special situations, such as when applications do their own caching.
163 File I/O is done directly to/from user space buffers.
164 The I/O is synchronous, that is, at the completion of a
168 data is guaranteed to have been transferred.
169 Under Linux 2.4 transfer sizes, and the alignment of user buffer
170 and file offset must all be multiples of the logical block size
172 Under Linux 2.6 alignment to 512-byte boundaries
174 .\" Alignment should satisfy requirements for the underlying device
175 .\" There may be coherency problems.
177 A semantically similar (but deprecated) interface for block devices
182 If \fIpathname\fR is not a directory, cause the open to fail.
183 .\" But see the following and its replies:
184 .\" http://marc.theaimsgroup.com/?t=112748702800001&r=1&w=2
185 .\" [PATCH] open: O_DIRECTORY and O_CREAT together should fail
186 .\" O_DIRECTORY | O_CREAT causes O_DIRECTORY to be ignored.
187 This flag is Linux-specific, and was added in kernel version 2.1.126, to
188 avoid denial-of-service problems if
191 FIFO or tape device, but should not be used outside of the
198 if the file already exists it is an error and the
201 In this context, a symbolic link exists, regardless
202 of where it points to.
204 is broken on NFS file systems; programs which rely on it for performing
205 locking tasks will contain a race condition.
206 The solution for performing
207 atomic file locking using a lockfile is to create a unique file on
208 the same file system (e.g., incorporating hostname and pid), use
210 to make a link to the lockfile.
213 returns 0, the lock is
217 on the unique file to check if its link count has increased to 2,
218 in which case the lock is also successful.
222 Allow files whose sizes cannot be represented in an
224 (but can be represented in an
228 .B _LARGEFILE64_SOURCE
229 macro must be defined in order to obtain this definition.
232 feature test macro to 64 (rather than using
234 is the preferred method of obtaining
235 method of accessing large files on 32-bit systems (see
236 .BR feature_test_macros (7)).
240 Do not update the file last access time (st_atime in the inode)
243 This flag is intended for use by indexing or backup programs,
244 where its use can significantly reduce the amount of disk activity.
245 This flag may not be effective on all filesystems.
246 One example is NFS, where the server maintains the access time.
247 .\" The O_NOATIME flag also affects the treatment of st_atime
248 .\" by mmap() and readdir(2), MTK, Dec 04.
253 refers to a terminal device \(em see
255 \(em it will not become the process's controlling terminal even if the
256 process does not have one.
259 If \fIpathname\fR is a symbolic link, then the open fails.
260 This is a FreeBSD extension, which was added to Linux in version 2.1.126.
261 Symbolic links in earlier components of the pathname will still be
263 .\" The headers from glibc 2.0.100 and later include a
264 .\" definition of this flag; \fIkernels before 2.1.126 will ignore it if
267 .BR O_NONBLOCK " or " O_NDELAY
268 When possible, the file is opened in non-blocking mode.
271 nor any subsequent operations on the file descriptor which is
272 returned will cause the calling process to wait.
273 For the handling of FIFOs (named pipes), see also
275 For a discussion of the effect of
277 in conjunction with mandatory file locks and with file leases, see
281 The file is opened for synchronous I/O.
284 on the resulting file descriptor will block the calling process until
285 the data has been physically written to the underlying hardware.
286 .IR "But see NOTES below" .
289 If the file already exists and is a regular file and the open mode allows
294 it will be truncated to length 0.
295 If the file is a FIFO or terminal device file, the
298 Otherwise the effect of
302 Some of these optional flags can be altered using
304 after the file has been opened.
308 specifies the permissions to use in case a new file is created.
310 modified by the process's
312 in the usual way: the permissions of the created file are
313 .IR "(mode & ~umask)" .
314 Note that this mode only applies to future accesses of the
315 newly created file; the
317 call that creates a read-only file may well return a read/write
320 The following symbolic constants are provided for
324 00700 user (file owner) has read, write and execute permission
327 00400 user has read permission
330 00200 user has write permission
333 00100 user has execute permission
336 00070 group has read, write and execute permission
339 00040 group has read permission
342 00020 group has write permission
345 00010 group has execute permission
348 00007 others have read, write and execute permission
351 00004 others have read permission
354 00002 others have write permission
357 00001 others have execute permission
360 must be specified when
364 and is ignored otherwise.
372 .BR O_CREAT|O_WRONLY|O_TRUNC .
377 return the new file descriptor, or \-1 if an error occurred
380 is set appropriately).
384 The requested access to the file is not allowed, or search permission
385 is denied for one of the directories in the path prefix of
387 or the file did not exist yet and write access to the parent directory
390 .BR path_resolution (7).)
395 .BR O_CREAT " and " O_EXCL
400 points outside your accessible address space.
404 refers to a regular file, too large to be opened; see
407 (POSIX.1-2001 specifies the error
410 .\" FIXME . Maybe this deviation from the standard will get repaired.
411 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=7253
412 .\" "Open of a large file on 32-bit fails with EFBIG, should be EOVERFLOW"
413 .\" Reported 2006-10-03
417 refers to a directory and the access requested involved writing
425 Too many symbolic links were encountered in resolving
427 or \fBO_NOFOLLOW\fR was specified but
432 The process already has the maximum number of files open.
439 The system limit on the total number of open files has been reached.
443 refers to a device special file and no corresponding device exists.
444 (This is a Linux kernel bug; in this situation
450 is not set and the named file does not exist.
451 Or, a directory component in
453 does not exist or is a dangling symbolic link.
456 Insufficient kernel memory was available.
460 was to be created but the device containing
462 has no room for the new file.
465 A component used as a directory in
467 is not, in fact, a directory, or \fBO_DIRECTORY\fR was specified and
472 .BR O_NONBLOCK " | " O_WRONLY
473 is set, the named file is a FIFO and
474 no process has the file open for reading.
475 Or, the file is a device special file and no corresponding device exists.
480 flag was specified, but the effective user ID of the caller
481 .\" Strictly speaking, it's the file system UID... (MTK)
482 did not match the owner of the file and the caller was not privileged
487 refers to a file on a read-only filesystem and write access was
492 refers to an executable image which is currently being executed and
493 write access was requested.
498 flag was specified, and an incompatible lease was held on the file
502 SVr4, 4.3BSD, POSIX.1-2001.
508 flags are Linux specific.
509 One may have to define the
511 macro to get their definitions.
515 flag indicates that one wants to open
516 but does not necessarily have the intention to read or write.
517 This is typically used to open devices in order to get a file descriptor
521 The (undefined) effect of
522 .B O_RDONLY | O_TRUNC
523 varies among implementations.
524 On many systems the file is actually truncated.
525 .\" Linux 2.0, 2.5: truncate
526 .\" Solaris 5.7, 5.8: truncate
527 .\" Irix 6.5: truncate
528 .\" Tru64 5.1B: truncate
529 .\" HP-UX 11.22: truncate
530 .\" FreeBSD 4.7: truncate
534 flag was introduced in SGI IRIX, where it has alignment restrictions
535 similar to those of Linux 2.4.
536 IRIX has also a fcntl(2) call to
537 query appropriate alignments, and sizes.
538 FreeBSD 4.x introduced
539 a flag of same name, but without alignment restrictions.
540 Support was added under Linux in kernel version 2.4.10.
541 Older Linux kernels simply ignore this flag.
542 One may have to define the
544 macro to get its definition.
546 There are many infelicities in the protocol underlying NFS, affecting
548 .BR O_SYNC " and " O_NDELAY .
550 POSIX provides for three different variants of synchronized I/O,
551 corresponding to the flags \fBO_SYNC\fR, \fBO_DSYNC\fR and
553 Currently (2.1.130) these are all synonymous under Linux.
557 can open device special files, but
559 cannot create them; use
563 On NFS file systems with UID mapping enabled,
566 return a file descriptor but, for example,
570 This is because the client performs
573 permissions, but UID mapping is performed by the server upon
574 read and write requests.
576 If the file is newly created, its
581 (respectively, time of last access, time of last status change, and
582 time of last modification; see
585 to the current time, and so are the
591 Otherwise, if the file is modified because of the
593 flag, its st_ctime and st_mtime fields are set to the current time.
595 "The thing that has always disturbed me about O_DIRECT is that the whole
596 interface is just stupid, and was probably designed by a deranged monkey
597 on some serious mind-controlling substances." \(em Linus
599 Currently, it is not possible to enable signal-driven
607 .\" FIXME . Check bugzilla report on open(O_ASYNC)
608 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=5993
627 .BR feature_test_macros (7),
628 .BR path_resolution (7)