]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/open.2
Rename ftm.7 to feature_test_macros.7
[thirdparty/man-pages.git] / man2 / open.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
4 .\" 1993 Michael Haardt, Ian Jackson.
5 .\"
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.
9 .\"
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.
14 .\"
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
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\"
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
38 .\"
39 .TH OPEN 2 2005-06-22 "Linux 2.6.12" "Linux Programmer's Manual"
40 .SH NAME
41 open, creat \- open and possibly create a file or device
42 .SH SYNOPSIS
43 .nf
44 .B #include <sys/types.h>
45 .B #include <sys/stat.h>
46 .B #include <fcntl.h>
47 .sp
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 );
51 .fi
52 .SH DESCRIPTION
53 Given a
54 .IR pathname
55 for a file,
56 .BR open ()
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.
62 .PP
63 The new file descriptor is set to remain open across an
64 .BR execve (2)
65 (i.e., the
66 .B FD_CLOEXEC
67 file descriptor flag described in
68 .BR fcntl (2)
69 is initially disabled).
70 The file offset is set to the beginning of the file (see
71 .BR lseek (2)).
72 .PP
73 A call to
74 .BR open ()
75 creates a new
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
79 (modifiable via the
80 .BR fcntl ()
81 .B F_SETFL
82 operation).
83 A file descriptor is a reference to one of these entries;
84 this reference is unaffected if
85 .I pathname
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
90 .BR fork (2).
91 .PP
92 The parameter
93 .I flags
94 must include one of the following
95 .IR "access modes" :
96 .BR O_RDONLY ", " O_WRONLY ", or " O_RDWR.
97 These request opening the file read-only, write-only, or read/write,
98 respectively.
99
100 In addition, zero or more file creation flags and file status flags
101 can be
102 .RI bitwise- or 'd
103 in
104 .IR flags .
105 The
106 .I file creation flags
107 are
108 .BR O_CREAT ", " O_EXCL ", " O_NOCTTY ", and " O_TRUNC .
109 The
110 .I file status flags
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)
114 modified using
115 .BR fcntl (2).
116 The full list of file creation flags and file status flags is as follows:
117 .TP
118 .B O_APPEND
119 The file is opened in append mode. Before each
120 .BR write (),
121 the file offset is positioned at the end of the file,
122 as if with
123 .BR lseek ().
124 .B O_APPEND
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.
129 .TP
130 .B O_ASYNC
131 Enable signal-driven I/O:
132 generate a signal (SIGIO by default, but this can be changed via
133 .BR fcntl (2))
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.
137 See
138 .BR fcntl (2)
139 for further details.
140 .TP
141 .B O_CREAT
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
148 .I bsdgroups
149 and
150 .I sysvgroups
151 of the ext2 filesystem, as described in
152 .BR mount (8)).
153 .TP
154 .B O_DIRECT
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
160 .BR read (2)
161 or
162 .BR write (2),
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
167 suffices.
168 .\" Alignment should satisfy requirements for the underlying device
169 .\" There may be coherency problems.
170 .sp
171 A semantically similar (but deprecated) interface for block devices
172 is described in
173 .BR raw (8).
174 .TP
175 .B O_DIRECTORY
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.
185 .TP
186 .B O_EXCL
187 When used with
188 .BR O_CREAT ,
189 if the file already exists it is an error and the
190 .BR open ()
191 will fail. In this context, a symbolic link exists, regardless
192 of where it points to.
193 .B O_EXCL
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
198 .BR link (2)
199 to make a link to the lockfile. If \fBlink\fP() returns 0, the lock is
200 successful. Otherwise, use
201 .BR stat (2)
202 on the unique file to check if its link count has increased to 2,
203 in which case the lock is also successful.
204 .TP
205 .B O_LARGEFILE
206 (LFS)
207 Allow files whose sizes cannot be represented in an
208 .I off_t
209 (but can be represented in an
210 .IR off64_t )
211 to be opened.
212 .TP
213 .B O_NOATIME
214 (Since Linux 2.6.8)
215 Do not update the file last access time (st_atime in the inode)
216 when the file is
217 .BR read (2).
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.
224 .TP
225 .B O_NOCTTY
226 If
227 .I pathname
228 refers to a terminal device \(em see
229 .BR tty (4)
230 \(em it will not become the process's controlling terminal even if the
231 process does not have one.
232 .TP
233 .B O_NOFOLLOW
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
237 followed.
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
240 .\" used\fR.
241 .TP
242 .BR O_NONBLOCK " or " O_NDELAY
243 When possible, the file is opened in non-blocking mode. Neither the
244 .BR open ()
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
248 .BR fifo (7).
249 For a discussion of the effect of
250 .BR O_NONBLOCK
251 in conjunction with mandatory file locks and with file leases, see
252 .BR fcntl (2).
253
254 .TP
255 .B O_SYNC
256 The file is opened for synchronous I/O. Any
257 .BR write ()s
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" .
261 .TP
262 .B O_TRUNC
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.
267 .PP
268 Some of these optional flags can be altered using
269 .BR fcntl ()
270 after the file has been opened.
271
272 The argument
273 .I mode
274 specifies the permissions to use in case a new file is created. It is
275 modified by the process's
276 .BR umask
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
281 .BR open ()
282 call that creates a read-only file may well return a read/write
283 file descriptor.
284 .PP
285 The following symbolic constants are provided for
286 .IR mode :
287 .TP
288 .B S_IRWXU
289 00700 user (file owner) has read, write and execute permission
290 .TP
291 .B S_IRUSR
292 00400 user has read permission
293 .TP
294 .B S_IWUSR
295 00200 user has write permission
296 .TP
297 .B S_IXUSR
298 00100 user has execute permission
299 .TP
300 .B S_IRWXG
301 00070 group has read, write and execute permission
302 .TP
303 .B S_IRGRP
304 00040 group has read permission
305 .TP
306 .B S_IWGRP
307 00020 group has write permission
308 .TP
309 .B S_IXGRP
310 00010 group has execute permission
311 .TP
312 .B S_IRWXO
313 00007 others have read, write and execute permission
314 .TP
315 .B S_IROTH
316 00004 others have read permission
317 .TP
318 .B S_IWOTH
319 00002 others have write permission
320 .TP
321 .B S_IXOTH
322 00001 others have execute permission
323 .PP
324 .I mode
325 must be specified when
326 .B O_CREAT
327 is in the
328 .IR flags ,
329 and is ignored otherwise.
330
331 .BR creat ()
332 is equivalent to
333 .BR open ()
334 with
335 .I flags
336 equal to
337 .BR O_CREAT|O_WRONLY|O_TRUNC .
338 .SH "RETURN VALUE"
339 .BR open ()
340 and
341 .BR creat ()
342 return the new file descriptor, or \-1 if an error occurred
343 (in which case,
344 .I errno
345 is set appropriately).
346 .SH NOTES
347 Note that
348 .BR open ()
349 can open device special files, but
350 .BR creat ()
351 cannot create them; use
352 .BR mknod (2)
353 instead.
354 .LP
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
357 with \fBEACCES\fP.
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.
361
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
365 .BR stat (2))
366 are set
367 to the current time, and so are the st_ctime and st_mtime fields of the
368 parent directory.
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.
371 .SH ERRORS
372 .TP
373 .B EACCES
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
376 .IR pathname ,
377 or the file did not exist yet and write access to the parent directory
378 is not allowed.
379 (See also
380 .BR path_resolution (2).)
381 .TP
382 .B EEXIST
383 .I pathname
384 already exists and
385 .BR O_CREAT " and " O_EXCL
386 were used.
387 .TP
388 .B EFAULT
389 .IR pathname
390 points outside your accessible address space.
391 .TP
392 .B EISDIR
393 .I pathname
394 refers to a directory and the access requested involved writing
395 (that is,
396 .B O_WRONLY
397 or
398 .B O_RDWR
399 is set).
400 .TP
401 .B ELOOP
402 Too many symbolic links were encountered in resolving
403 .IR pathname ,
404 or \fBO_NOFOLLOW\fR was specified but
405 .I pathname
406 was a symbolic link.
407 .TP
408 .B EMFILE
409 The process already has the maximum number of files open.
410 .TP
411 .B ENAMETOOLONG
412 .IR pathname
413 was too long.
414 .TP
415 .B ENFILE
416 The system limit on the total number of open files has been reached.
417 .TP
418 .B ENODEV
419 .I pathname
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.)
422 .TP
423 .B ENOENT
424 O_CREAT is not set and the named file does not exist.
425 Or, a directory component in
426 .I pathname
427 does not exist or is a dangling symbolic link.
428 .TP
429 .B ENOMEM
430 Insufficient kernel memory was available.
431 .TP
432 .B ENOSPC
433 .I pathname
434 was to be created but the device containing
435 .I pathname
436 has no room for the new file.
437 .TP
438 .B ENOTDIR
439 A component used as a directory in
440 .I pathname
441 is not, in fact, a directory, or \fBO_DIRECTORY\fR was specified and
442 .I pathname
443 was not a directory.
444 .TP
445 .B ENXIO
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.
449 .TP
450 .B EOVERFLOW
451 .I pathname
452 refers to a regular file, too large to be opened; see O_LARGEFILE above.
453 .TP
454 .B EPERM
455 The
456 .B O_NOATIME
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
460 .RB ( CAP_FOWNER ).
461 .TP
462 .B EROFS
463 .I pathname
464 refers to a file on a read-only filesystem and write access was
465 requested.
466 .TP
467 .B ETXTBSY
468 .I pathname
469 refers to an executable image which is currently being executed and
470 write access was requested.
471 .TP
472 .B EWOULDBLOCK
473 The
474 .B O_NONBLOCK
475 flag was specified, and an incompatible lease was held on the file
476 (see
477 .BR fcntl (2)).
478 .SH NOTE
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
482 for use with
483 .BR ioctl (2).
484 .SH "CONFORMING TO"
485 SVr4, SVID, POSIX, X/OPEN, 4.3BSD.
486 The
487 .BR O_NOATIME ,
488 .BR O_NOFOLLOW ,
489 and
490 .B O_DIRECTORY
491 flags are Linux-specific.
492 One may have to define the
493 .B _GNU_SOURCE
494 macro to get their definitions.
495 .LP
496 The (undefined) effect of
497 .B O_RDONLY | O_TRUNC
498 varies among implementations. On many systems the file is actually
499 truncated.
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
506 .LP
507 The
508 .B O_DIRECT
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
516 .B _GNU_SOURCE
517 macro to get its definition.
518 .SH BUGS
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
522
523 Currently, it is not possible to enable signal-driven
524 I/O by specifying
525 .B O_ASYNC
526 when calling
527 .BR open ();
528 use
529 .BR fcntl (2)
530 to enable this flag.
531 .\" FIXME Check bugzilla report on open(O_ASYNC)
532 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=5993
533 .SH RESTRICTIONS
534 There are many infelicities in the protocol underlying NFS, affecting
535 amongst others
536 .BR O_SYNC " and " O_NDELAY .
537
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.
541 .SH "SEE ALSO"
542 .BR close (2),
543 .BR dup (2),
544 .BR fcntl (2),
545 .BR link (2),
546 .BR lseek (2),
547 .BR mknod (2),
548 .BR mount (2),
549 .BR mmap (2),
550 .BR openat (2),
551 .BR path_resolution (2),
552 .BR read (2),
553 .BR socket (2),
554 .BR stat (2),
555 .BR umask (2),
556 .BR unlink (2),
557 .BR write (2),
558 .BR fopen (3),
559 .BR fifo (7),
560 .BR feature_test_macros (7)