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 2.6.12" "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. Before each
121 the file offset is positioned at the end of the file,
125 may lead to corrupted files on NFS file systems if more than one process
126 appends data to a file at once. This is because NFS does not support
127 appending to a file, so the client kernel has to simulate it, which
128 can't be done without a race condition.
131 Enable signal-driven I/O:
132 generate a signal (SIGIO by default, but this can be changed via
134 when input or output becomes possible on this file descriptor.
135 This feature is only available for terminals, pseudo-terminals,
136 sockets, and (since Linux 2.6) pipes and FIFOs.
142 If the file does not exist it will be created.
143 The owner (user ID) of the file is set to the effective user ID
144 of the process. The group ownership (group ID) is set either to
145 the effective group ID of the process or to the group ID of the
146 parent directory (depending on filesystem type and mount options,
147 and the mode of the parent directory, see, e.g., the mount options
151 of the ext2 filesystem, as described in
155 Try to minimize cache effects of the I/O to and from this file.
156 In general this will degrade performance, but it is useful in
157 special situations, such as when applications do their own caching.
158 File I/O is done directly to/from user space buffers.
159 The I/O is synchronous, i.e., at the completion of a
163 data is guaranteed to have been transferred.
164 Under Linux 2.4 transfer sizes, and the alignment of user buffer
165 and file offset must all be multiples of the logical block size
166 of the file system. Under Linux 2.6 alignment to 512-byte boundaries
168 .\" Alignment should satisfy requirements for the underlying device
169 .\" There may be coherency problems.
171 A semantically similar (but deprecated) interface for block devices
176 If \fIpathname\fR is not a directory, cause the open to fail.
177 .\" But see the following and its replies:
178 .\" http://marc.theaimsgroup.com/?t=112748702800001&r=1&w=2
179 .\" [PATCH] open: O_DIRECTORY and O_CREAT together should fail
180 .\" O_DIRECTORY | O_CREAT causes O_DIRECTORY to be ignored.
181 This flag is Linux-specific, and was added in kernel version 2.1.126, to
182 avoid denial-of-service problems if \fBopendir\fR(3) is called on a
183 FIFO or tape device, but should not be used outside of the
184 implementation of \fBopendir\fR.
189 if the file already exists it is an error and the
191 will fail. In this context, a symbolic link exists, regardless
192 of where it points to.
194 is broken on NFS file systems; programs which rely on it for performing
195 locking tasks will contain a race condition. The solution for performing
196 atomic file locking using a lockfile is to create a unique file on
197 the same file system (e.g., incorporating hostname and pid), use
199 to make a link to the lockfile. If \fBlink\fP() returns 0, the lock is
200 successful. Otherwise, use
202 on the unique file to check if its link count has increased to 2,
203 in which case the lock is also successful.
207 Allow files whose sizes cannot be represented in an
209 (but can be represented in an
215 Do not update the file last access time (st_atime in the inode)
218 This flag is intended for use by indexing or backup programs,
219 where its use can significantly reduce the amount of disk activity.
220 This flag may not be effective on all filesystems.
221 One example is NFS, where the server maintains the access time.
222 .\" FIXME? The O_NOATIME flag also affects the treatment of st_atime
223 .\" by mmap() and readdir(2), MTK, Dec 04.
228 refers to a terminal device \(em see
230 \(em it will not become the process's controlling terminal even if the
231 process does not have one.
234 If \fIpathname\fR is a symbolic link, then the open fails. This is a
235 FreeBSD extension, which was added to Linux in version 2.1.126.
236 Symbolic links in earlier components of the pathname will still be
238 .\" The headers from glibc 2.0.100 and later include a
239 .\" definition of this flag; \fIkernels before 2.1.126 will ignore it if
242 .BR O_NONBLOCK " or " O_NDELAY
243 When possible, the file is opened in non-blocking mode. Neither the
245 nor any subsequent operations on the file descriptor which is
246 returned will cause the calling process to wait.
247 For the handling of FIFOs (named pipes), see also
249 For a discussion of the effect of
251 in conjunction with mandatory file locks and with file leases, see
256 The file is opened for synchronous I/O. Any
258 on the resulting file descriptor will block the calling process until
259 the data has been physically written to the underlying hardware.
260 .IR "But see RESTRICTIONS below" .
263 If the file already exists and is a regular file and the open mode allows
264 writing (i.e., is O_RDWR or O_WRONLY) it will be truncated to length 0.
265 If the file is a FIFO or terminal device file, the O_TRUNC
266 flag is ignored. Otherwise the effect of O_TRUNC is unspecified.
268 Some of these optional flags can be altered using
270 after the file has been opened.
274 specifies the permissions to use in case a new file is created. It is
275 modified by the process's
277 in the usual way: the permissions of the created file are
278 .BR "(mode & ~umask)" .
279 Note that this mode only applies to future accesses of the
280 newly created file; the
282 call that creates a read-only file may well return a read/write
285 The following symbolic constants are provided for
289 00700 user (file owner) has read, write and execute permission
292 00400 user has read permission
295 00200 user has write permission
298 00100 user has execute permission
301 00070 group has read, write and execute permission
304 00040 group has read permission
307 00020 group has write permission
310 00010 group has execute permission
313 00007 others have read, write and execute permission
316 00004 others have read permission
319 00002 others have write permission
322 00001 others have execute permission
325 must be specified when
329 and is ignored otherwise.
337 .BR O_CREAT|O_WRONLY|O_TRUNC .
342 return the new file descriptor, or \-1 if an error occurred
345 is set appropriately).
349 can open device special files, but
351 cannot create them; use
355 On NFS file systems with UID mapping enabled, \fBopen\fP() may
356 return a file descriptor but e.g. \fBread\fP(2) requests are denied
358 This is because the client performs \fBopen\fP() by checking the
359 permissions, but UID mapping is performed by the server upon
360 read and write requests.
362 If the file is newly created, its st_atime, st_ctime, st_mtime fields
363 (respectively, time of last access, time of last status change, and
364 time of last modification; see
367 to the current time, and so are the st_ctime and st_mtime fields of the
369 Otherwise, if the file is modified because of the O_TRUNC flag,
370 its st_ctime and st_mtime fields are set to the current time.
374 The requested access to the file is not allowed, or search permission
375 is denied for one of the directories in the path prefix of
377 or the file did not exist yet and write access to the parent directory
380 .BR path_resolution (2).)
385 .BR O_CREAT " and " O_EXCL
390 points outside your accessible address space.
394 refers to a directory and the access requested involved writing
402 Too many symbolic links were encountered in resolving
404 or \fBO_NOFOLLOW\fR was specified but
409 The process already has the maximum number of files open.
416 The system limit on the total number of open files has been reached.
420 refers to a device special file and no corresponding device exists.
421 (This is a Linux kernel bug; in this situation ENXIO must be returned.)
424 O_CREAT is not set and the named file does not exist.
425 Or, a directory component in
427 does not exist or is a dangling symbolic link.
430 Insufficient kernel memory was available.
434 was to be created but the device containing
436 has no room for the new file.
439 A component used as a directory in
441 is not, in fact, a directory, or \fBO_DIRECTORY\fR was specified and
446 O_NONBLOCK | O_WRONLY is set, the named file is a FIFO and
447 no process has the file open for reading.
448 Or, the file is a device special file and no corresponding device exists.
452 refers to a regular file, too large to be opened; see O_LARGEFILE above.
457 flag was specified, but the effective user ID of the caller
458 .\" Strictly speaking, it's the file system UID... (MTK)
459 did not match the owner of the file and the caller was not privileged
464 refers to a file on a read-only filesystem and write access was
469 refers to an executable image which is currently being executed and
470 write access was requested.
475 flag was specified, and an incompatible lease was held on the file
479 Under Linux, the O_NONBLOCK flag indicates that one wants to open
480 but does not necessarily have the intention to read or write.
481 This is typically used to open devices in order to get a file descriptor
485 SVr4, SVID, POSIX, X/OPEN, 4.3BSD.
491 flags are Linux-specific.
492 One may have to define the
494 macro to get their definitions.
496 The (undefined) effect of
497 .B O_RDONLY | O_TRUNC
498 varies among implementations. On many systems the file is actually
500 .\" Linux 2.0, 2.5: truncate
501 .\" Solaris 5.7, 5.8: truncate
502 .\" Irix 6.5: truncate
503 .\" Tru64 5.1B: truncate
504 .\" HP-UX 11.22: truncate
505 .\" FreeBSD 4.7: truncate
509 flag was introduced in SGI IRIX, where it has alignment restrictions
510 similar to those of Linux 2.4. IRIX has also a fcntl(2) call to
511 query appropriate alignments, and sizes. FreeBSD 4.x introduced
512 a flag of same name, but without alignment restrictions.
513 Support was added under Linux in kernel version 2.4.10.
514 Older Linux kernels simply ignore this flag.
515 One may have to define the
517 macro to get its definition.
519 "The thing that has always disturbed me about O_DIRECT is that the whole
520 interface is just stupid, and was probably designed by a deranged monkey
521 on some serious mind-controlling substances." \(em Linus
523 Currently, it is not possible to enable signal-driven
531 .\" FIXME Check bugzilla report on open(O_ASYNC)
532 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=5993
534 There are many infelicities in the protocol underlying NFS, affecting
536 .BR O_SYNC " and " O_NDELAY .
538 POSIX provides for three different variants of synchronised I/O,
539 corresponding to the flags \fBO_SYNC\fR, \fBO_DSYNC\fR and
540 \fBO_RSYNC\fR. Currently (2.1.130) these are all synonymous under Linux.
551 .BR path_resolution (2),
560 .BR feature_test_macros (7)