1OPEN(2) Linux Programmer's Manual OPEN(2)
2
3
4
6 open, creat - open and possibly create a file or device
7
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12
13 int open(const char *pathname, int flags);
14 int open(const char *pathname, int flags, mode_t mode);
15
16 int creat(const char *pathname, mode_t mode);
17
19 Given a pathname for a file, open() returns a file descriptor, a small,
20 non-negative integer for use in subsequent system calls (read(2),
21 write(2), lseek(2), fcntl(2), etc.). The file descriptor returned by a
22 successful call will be the lowest-numbered file descriptor not cur‐
23 rently open for the process.
24
25 By default, the new file descriptor is set to remain open across an
26 execve(2) (i.e., the FD_CLOEXEC file descriptor flag described in
27 fcntl(2) is initially disabled; the Linux-specific O_CLOEXEC flag,
28 described below, can be used to change this default). The file offset
29 is set to the beginning of the file (see lseek(2)).
30
31 A call to open() creates a new open file description, an entry in the
32 system-wide table of open files. This entry records the file offset
33 and the file status flags (modifiable via the fcntl(2) F_SETFL opera‐
34 tion). A file descriptor is a reference to one of these entries; this
35 reference is unaffected if pathname is subsequently removed or modified
36 to refer to a different file. The new open file description is ini‐
37 tially not shared with any other process, but sharing may arise via
38 fork(2).
39
40 The argument flags must include one of the following access modes:
41 O_RDONLY, O_WRONLY, or O_RDWR. These request opening the file read-
42 only, write-only, or read/write, respectively.
43
44 In addition, zero or more file creation flags and file status flags can
45 be bitwise-or'd in flags. The file creation flags are O_CREAT, O_EXCL,
46 O_NOCTTY, and O_TRUNC. The file status flags are all of the remaining
47 flags listed below. The distinction between these two groups of flags
48 is that the file status flags can be retrieved and (in some cases) mod‐
49 ified using fcntl(2). The full list of file creation flags and file
50 status flags is as follows:
51
52 O_APPEND
53 The file is opened in append mode. Before each write(2), the
54 file offset is positioned at the end of the file, as if with
55 lseek(2). O_APPEND may lead to corrupted files on NFS file sys‐
56 tems if more than one process appends data to a file at once.
57 This is because NFS does not support appending to a file, so the
58 client kernel has to simulate it, which can't be done without a
59 race condition.
60
61 O_ASYNC
62 Enable signal-driven I/O: generate a signal (SIGIO by default,
63 but this can be changed via fcntl(2)) when input or output
64 becomes possible on this file descriptor. This feature is only
65 available for terminals, pseudo-terminals, sockets, and (since
66 Linux 2.6) pipes and FIFOs. See fcntl(2) for further details.
67
68 O_CLOEXEC (Since Linux 2.6.23)
69 Enable the close-on-exec flag for the new file descriptor.
70 Specifying this flag permits a program to avoid additional
71 fcntl(2) F_SETFD operations to set the FD_CLOEXEC flag. Addi‐
72 tionally, use of this flag is essential in some multithreaded
73 programs since using a separate fcntl(2) F_SETFD operation to
74 set the FD_CLOEXEC flag does not suffice to avoid race condi‐
75 tions where one thread opens a file descriptor at the same time
76 as another thread does a fork(2) plus execve(2).
77
78 O_CREAT
79 If the file does not exist it will be created. The owner (user
80 ID) of the file is set to the effective user ID of the process.
81 The group ownership (group ID) is set either to the effective
82 group ID of the process or to the group ID of the parent direc‐
83 tory (depending on file system type and mount options, and the
84 mode of the parent directory, see the mount options bsdgroups
85 and sysvgroups described in mount(8)).
86
87 mode specifies the permissions to use in case a new file is cre‐
88 ated. This argument must be supplied when O_CREAT is specified
89 in flags; if O_CREAT is not specified, then mode is ignored.
90 The effective permissions are modified by the process's umask in
91 the usual way: The permissions of the created file are
92 (mode & ~umask). Note that this mode only applies to future
93 accesses of the newly created file; the open() call that creates
94 a read-only file may well return a read/write file descriptor.
95
96 The following symbolic constants are provided for mode:
97
98 S_IRWXU 00700 user (file owner) has read, write and execute
99 permission
100
101 S_IRUSR 00400 user has read permission
102
103 S_IWUSR 00200 user has write permission
104
105 S_IXUSR 00100 user has execute permission
106
107 S_IRWXG 00070 group has read, write and execute permission
108
109 S_IRGRP 00040 group has read permission
110
111 S_IWGRP 00020 group has write permission
112
113 S_IXGRP 00010 group has execute permission
114
115 S_IRWXO 00007 others have read, write and execute permission
116
117 S_IROTH 00004 others have read permission
118
119 S_IWOTH 00002 others have write permission
120
121 S_IXOTH 00001 others have execute permission
122
123 O_DIRECT (Since Linux 2.4.10)
124 Try to minimize cache effects of the I/O to and from this file.
125 In general this will degrade performance, but it is useful in
126 special situations, such as when applications do their own
127 caching. File I/O is done directly to/from user space buffers.
128 The I/O is synchronous, that is, at the completion of a read(2)
129 or write(2), data is guaranteed to have been transferred. See
130 NOTES below for further discussion.
131
132 A semantically similar (but deprecated) interface for block
133 devices is described in raw(8).
134
135 O_DIRECTORY
136 If pathname is not a directory, cause the open to fail. This
137 flag is Linux-specific, and was added in kernel version 2.1.126,
138 to avoid denial-of-service problems if opendir(3) is called on a
139 FIFO or tape device, but should not be used outside of the
140 implementation of opendir(3).
141
142 O_EXCL Ensure that this call creates the file: if this flag is speci‐
143 fied in conjunction with O_CREAT, and pathname already exists,
144 then open() will fail. The behavior of O_EXCL is undefined if
145 O_CREAT is not specified.
146
147 When these two flags are specified, symbolic links are not fol‐
148 lowed: if pathname is a symbolic link, then open() fails regard‐
149 less of where the symbolic link points to.
150
151 O_EXCL is only supported on NFS when using NFSv3 or later on
152 kernel 2.6 or later. In environments where NFS O_EXCL support
153 is not provided, programs that rely on it for performing locking
154 tasks will contain a race condition. Portable programs that
155 want to perform atomic file locking using a lockfile, and need
156 to avoid reliance on NFS support for O_EXCL, can create a unique
157 file on the same file system (e.g., incorporating hostname and
158 PID), and use link(2) to make a link to the lockfile. If
159 link(2) returns 0, the lock is successful. Otherwise, use
160 stat(2) on the unique file to check if its link count has
161 increased to 2, in which case the lock is also successful.
162
163 O_LARGEFILE
164 (LFS) Allow files whose sizes cannot be represented in an off_t
165 (but can be represented in an off64_t) to be opened. The
166 _LARGEFILE64_SOURCE macro must be defined in order to obtain
167 this definition. Setting the _FILE_OFFSET_BITS feature test
168 macro to 64 (rather than using O_LARGEFILE) is the preferred
169 method of obtaining method of accessing large files on 32-bit
170 systems (see feature_test_macros(7)).
171
172 O_NOATIME (Since Linux 2.6.8)
173 Do not update the file last access time (st_atime in the inode)
174 when the file is read(2). This flag is intended for use by
175 indexing or backup programs, where its use can significantly
176 reduce the amount of disk activity. This flag may not be effec‐
177 tive on all file systems. One example is NFS, where the server
178 maintains the access time.
179
180 O_NOCTTY
181 If pathname refers to a terminal device — see tty(4) — it will
182 not become the process's controlling terminal even if the
183 process does not have one.
184
185 O_NOFOLLOW
186 If pathname is a symbolic link, then the open fails. This is a
187 FreeBSD extension, which was added to Linux in version 2.1.126.
188 Symbolic links in earlier components of the pathname will still
189 be followed.
190
191 O_NONBLOCK or O_NDELAY
192 When possible, the file is opened in non-blocking mode. Neither
193 the open() nor any subsequent operations on the file descriptor
194 which is returned will cause the calling process to wait. For
195 the handling of FIFOs (named pipes), see also fifo(7). For a
196 discussion of the effect of O_NONBLOCK in conjunction with
197 mandatory file locks and with file leases, see fcntl(2).
198
199 O_SYNC The file is opened for synchronous I/O. Any write(2)s on the
200 resulting file descriptor will block the calling process until
201 the data has been physically written to the underlying hardware.
202 But see NOTES below.
203
204 O_TRUNC
205 If the file already exists and is a regular file and the open
206 mode allows writing (i.e., is O_RDWR or O_WRONLY) it will be
207 truncated to length 0. If the file is a FIFO or terminal device
208 file, the O_TRUNC flag is ignored. Otherwise the effect of
209 O_TRUNC is unspecified.
210
211 Some of these optional flags can be altered using fcntl(2) after the
212 file has been opened.
213
214 creat() is equivalent to open() with flags equal to
215 O_CREAT|O_WRONLY|O_TRUNC.
216
218 open() and creat() return the new file descriptor, or -1 if an error
219 occurred (in which case, errno is set appropriately).
220
222 EACCES The requested access to the file is not allowed, or search per‐
223 mission is denied for one of the directories in the path prefix
224 of pathname, or the file did not exist yet and write access to
225 the parent directory is not allowed. (See also path_resolu‐
226 tion(7).)
227
228 EEXIST pathname already exists and O_CREAT and O_EXCL were used.
229
230 EFAULT pathname points outside your accessible address space.
231
232 EFBIG See EOVERFLOW.
233
234 EINTR While blocked waiting to complete an open of a slow device
235 (e.g., a FIFO; see fifo(7)), the call was interrupted by a sig‐
236 nal handler; see signal(7).
237
238 EISDIR pathname refers to a directory and the access requested involved
239 writing (that is, O_WRONLY or O_RDWR is set).
240
241 ELOOP Too many symbolic links were encountered in resolving pathname,
242 or O_NOFOLLOW was specified but pathname was a symbolic link.
243
244 EMFILE The process already has the maximum number of files open.
245
246 ENAMETOOLONG
247 pathname was too long.
248
249 ENFILE The system limit on the total number of open files has been
250 reached.
251
252 ENODEV pathname refers to a device special file and no corresponding
253 device exists. (This is a Linux kernel bug; in this situation
254 ENXIO must be returned.)
255
256 ENOENT O_CREAT is not set and the named file does not exist. Or, a
257 directory component in pathname does not exist or is a dangling
258 symbolic link.
259
260 ENOMEM Insufficient kernel memory was available.
261
262 ENOSPC pathname was to be created but the device containing pathname
263 has no room for the new file.
264
265 ENOTDIR
266 A component used as a directory in pathname is not, in fact, a
267 directory, or O_DIRECTORY was specified and pathname was not a
268 directory.
269
270 ENXIO O_NONBLOCK | O_WRONLY is set, the named file is a FIFO and no
271 process has the file open for reading. Or, the file is a device
272 special file and no corresponding device exists.
273
274 EOVERFLOW
275 pathname refers to a regular file that is too large to be
276 opened. The usual scenario here is that an application compiled
277 on a 32-bit platform without -D_FILE_OFFSET_BITS=64 tried to
278 open a file whose size exceeds (2<<31)-1 bits; see also O_LARGE‐
279 FILE above. This is the error specified by POSIX.1-2001; in
280 kernels before 2.6.24, Linux gave the error EFBIG for this case.
281
282 EPERM The O_NOATIME flag was specified, but the effective user ID of
283 the caller did not match the owner of the file and the caller
284 was not privileged (CAP_FOWNER).
285
286 EROFS pathname refers to a file on a read-only file system and write
287 access was requested.
288
289 ETXTBSY
290 pathname refers to an executable image which is currently being
291 executed and write access was requested.
292
293 EWOULDBLOCK
294 The O_NONBLOCK flag was specified, and an incompatible lease was
295 held on the file (see fcntl(2)).
296
298 SVr4, 4.3BSD, POSIX.1-2001. The O_DIRECTORY, O_NOATIME, and O_NOFOLLOW
299 flags are Linux-specific, and one may need to define _GNU_SOURCE to
300 obtain their definitions.
301
302 The O_CLOEXEC flag is not specified in POSIX.1-2001, but is specified
303 in POSIX.1-2008.
304
305 O_DIRECT is not specified in POSIX; one has to define _GNU_SOURCE to
306 get its definition.
307
309 Under Linux, the O_NONBLOCK flag indicates that one wants to open but
310 does not necessarily have the intention to read or write. This is typ‐
311 ically used to open devices in order to get a file descriptor for use
312 with ioctl(2).
313
314 Unlike the other values that can be specified in flags, the access mode
315 values O_RDONLY, O_WRONLY, and O_RDWR, do not specify individual bits.
316 Rather, they define the low order two bits of flags, and are defined
317 respectively as 0, 1, and 2. In other words, the combination O_RDONLY
318 | O_WRONLY is a logical error, and certainly does not have the same
319 meaning as O_RDWR. Linux reserves the special, non-standard access
320 mode 3 (binary 11) in flags to mean: check for read and write permis‐
321 sion on the file and return a descriptor that can't be used for reading
322 or writing. This non-standard access mode is used by some Linux driv‐
323 ers to return a descriptor that is only to be used for device-specific
324 ioctl(2) operations.
325
326 The (undefined) effect of O_RDONLY | O_TRUNC varies among implementa‐
327 tions. On many systems the file is actually truncated.
328
329 There are many infelicities in the protocol underlying NFS, affecting
330 amongst others O_SYNC and O_NDELAY.
331
332 POSIX provides for three different variants of synchronized I/O, corre‐
333 sponding to the flags O_SYNC, O_DSYNC and O_RSYNC. Currently (2.1.130)
334 these are all synonymous under Linux.
335
336 Note that open() can open device special files, but creat() cannot cre‐
337 ate them; use mknod(2) instead.
338
339 On NFS file systems with UID mapping enabled, open() may return a file
340 descriptor but, for example, read(2) requests are denied with EACCES.
341 This is because the client performs open() by checking the permissions,
342 but UID mapping is performed by the server upon read and write
343 requests.
344
345 If the file is newly created, its st_atime, st_ctime, st_mtime fields
346 (respectively, time of last access, time of last status change, and
347 time of last modification; see stat(2)) are set to the current time,
348 and so are the st_ctime and st_mtime fields of the parent directory.
349 Otherwise, if the file is modified because of the O_TRUNC flag, its
350 st_ctime and st_mtime fields are set to the current time.
351
352 O_DIRECT
353 The O_DIRECT flag may impose alignment restrictions on the length and
354 address of userspace buffers and the file offset of I/Os. In Linux
355 alignment restrictions vary by file system and kernel version and might
356 be absent entirely. However there is currently no file system-indepen‐
357 dent interface for an application to discover these restrictions for a
358 given file or file system. Some file systems provide their own inter‐
359 faces for doing so, for example the XFS_IOC_DIOINFO operation in
360 xfsctl(3).
361
362 Under Linux 2.4, transfer sizes, and the alignment of the user buffer
363 and the file offset must all be multiples of the logical block size of
364 the file system. Under Linux 2.6, alignment to 512-byte boundaries
365 suffices.
366
367 The O_DIRECT flag was introduced in SGI IRIX, where it has alignment
368 restrictions similar to those of Linux 2.4. IRIX has also a fcntl(2)
369 call to query appropriate alignments, and sizes. FreeBSD 4.x intro‐
370 duced a flag of the same name, but without alignment restrictions.
371
372 O_DIRECT support was added under Linux in kernel version 2.4.10. Older
373 Linux kernels simply ignore this flag. Some file systems may not
374 implement the flag and open() will fail with EINVAL if it is used.
375
376 Applications should avoid mixing O_DIRECT and normal I/O to the same
377 file, and especially to overlapping byte regions in the same file.
378 Even when the file system correctly handles the coherency issues in
379 this situation, overall I/O throughput is likely to be slower than
380 using either mode alone. Likewise, applications should avoid mixing
381 mmap(2) of files with direct I/O to the same files.
382
383 The behaviour of O_DIRECT with NFS will differ from local file systems.
384 Older kernels, or kernels configured in certain ways, may not support
385 this combination. The NFS protocol does not support passing the flag
386 to the server, so O_DIRECT I/O will only bypass the page cache on the
387 client; the server may still cache the I/O. The client asks the server
388 to make the I/O synchronous to preserve the synchronous semantics of
389 O_DIRECT. Some servers will perform poorly under these circumstances,
390 especially if the I/O size is small. Some servers may also be config‐
391 ured to lie to clients about the I/O having reached stable storage;
392 this will avoid the performance penalty at some risk to data integrity
393 in the event of server power failure. The Linux NFS client places no
394 alignment restrictions on O_DIRECT I/O.
395
396 In summary, O_DIRECT is a potentially powerful tool that should be used
397 with caution. It is recommended that applications treat use of
398 O_DIRECT as a performance option which is disabled by default.
399
400 "The thing that has always disturbed me about O_DIRECT is that
401 the whole interface is just stupid, and was probably designed by
402 a deranged monkey on some serious mind-controlling substances."
403 — Linus
404
406 Currently, it is not possible to enable signal-driven I/O by specifying
407 O_ASYNC when calling open(); use fcntl(2) to enable this flag.
408
410 chmod(2), chown(2), close(2), dup(2), fcntl(2), link(2), lseek(2),
411 mknod(2), mmap(2), mount(2), openat(2), read(2), socket(2), stat(2),
412 umask(2), unlink(2), write(2), fopen(3), feature_test_macros(7),
413 fifo(7), path_resolution(7), symlink(7)
414
416 This page is part of release 3.22 of the Linux man-pages project. A
417 description of the project, and information about reporting bugs, can
418 be found at http://www.kernel.org/doc/man-pages/.
419
420
421
422Linux 2008-12-03 OPEN(2)