]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/open.2
ffix
[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" "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 (2)
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.
120 Before each
121 .BR write (2),
122 the file offset is positioned at the end of the file,
123 as if with
124 .BR lseek (2).
125 .B O_APPEND
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.
131 .TP
132 .B O_ASYNC
133 Enable signal-driven I/O:
134 generate a signal
135 .RB ( SIGIO
136 by default, but this can be changed via
137 .BR fcntl (2))
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.
141 See
142 .BR fcntl (2)
143 for further details.
144 .TP
145 .B O_CREAT
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
148 of the process.
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
153 .I bsdgroups
154 and
155 .I sysvgroups
156 of the ext2 filesystem, as described in
157 .BR mount (8)).
158 .TP
159 .B O_DIRECT
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
165 .BR read (2)
166 or
167 .BR write (2),
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
171 of the file system.
172 Under Linux 2.6 alignment to 512-byte boundaries
173 suffices.
174 .\" Alignment should satisfy requirements for the underlying device
175 .\" There may be coherency problems.
176 .sp
177 A semantically similar (but deprecated) interface for block devices
178 is described in
179 .BR raw (8).
180 .TP
181 .B O_DIRECTORY
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
189 .BR opendir (3)
190 is called on a
191 FIFO or tape device, but should not be used outside of the
192 implementation of
193 .BR opendir (3).
194 .TP
195 .B O_EXCL
196 When used with
197 .BR O_CREAT ,
198 if the file already exists it is an error and the
199 .BR open ()
200 will fail.
201 In this context, a symbolic link exists, regardless
202 of where it points to.
203 .B O_EXCL
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
209 .BR link (2)
210 to make a link to the lockfile.
211 If
212 .BR link (2)
213 returns 0, the lock is
214 successful.
215 Otherwise, use
216 .BR stat (2)
217 on the unique file to check if its link count has increased to 2,
218 in which case the lock is also successful.
219 .TP
220 .B O_LARGEFILE
221 (LFS)
222 Allow files whose sizes cannot be represented in an
223 .I off_t
224 (but can be represented in an
225 .IR off64_t )
226 to be opened.
227 The
228 .B _LARGEFILE64_SOURCE
229 macro must be defined in order to obtain this definition.
230 Setting the
231 .B _FILE_OFFSET_BITS
232 feature test macro to 64 (rather than using
233 .BR O_LARGEFILE )
234 is the preferred method of obtaining
235 method of accessing large files on 32-bit systems (see
236 .BR feature_test_macros (7)).
237 .TP
238 .B O_NOATIME
239 (Since Linux 2.6.8)
240 Do not update the file last access time (st_atime in the inode)
241 when the file is
242 .BR read (2).
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.
249 .TP
250 .B O_NOCTTY
251 If
252 .I pathname
253 refers to a terminal device \(em see
254 .BR tty (4)
255 \(em it will not become the process's controlling terminal even if the
256 process does not have one.
257 .TP
258 .B O_NOFOLLOW
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
262 followed.
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
265 .\" used\fR.
266 .TP
267 .BR O_NONBLOCK " or " O_NDELAY
268 When possible, the file is opened in non-blocking mode.
269 Neither the
270 .BR open ()
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
274 .BR fifo (7).
275 For a discussion of the effect of
276 .BR O_NONBLOCK
277 in conjunction with mandatory file locks and with file leases, see
278 .BR fcntl (2).
279 .TP
280 .B O_SYNC
281 The file is opened for synchronous I/O.
282 Any
283 .BR write (2)s
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" .
287 .TP
288 .B O_TRUNC
289 If the file already exists and is a regular file and the open mode allows
290 writing (i.e., is
291 .B O_RDWR
292 or
293 .BR O_WRONLY )
294 it will be truncated to length 0.
295 If the file is a FIFO or terminal device file, the
296 .B O_TRUNC
297 flag is ignored.
298 Otherwise the effect of
299 .B O_TRUNC
300 is unspecified.
301 .PP
302 Some of these optional flags can be altered using
303 .BR fcntl (2)
304 after the file has been opened.
305
306 The argument
307 .I mode
308 specifies the permissions to use in case a new file is created.
309 It is
310 modified by the process's
311 .IR umask
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
316 .BR open ()
317 call that creates a read-only file may well return a read/write
318 file descriptor.
319 .PP
320 The following symbolic constants are provided for
321 .IR mode :
322 .TP
323 .B S_IRWXU
324 00700 user (file owner) has read, write and execute permission
325 .TP
326 .B S_IRUSR
327 00400 user has read permission
328 .TP
329 .B S_IWUSR
330 00200 user has write permission
331 .TP
332 .B S_IXUSR
333 00100 user has execute permission
334 .TP
335 .B S_IRWXG
336 00070 group has read, write and execute permission
337 .TP
338 .B S_IRGRP
339 00040 group has read permission
340 .TP
341 .B S_IWGRP
342 00020 group has write permission
343 .TP
344 .B S_IXGRP
345 00010 group has execute permission
346 .TP
347 .B S_IRWXO
348 00007 others have read, write and execute permission
349 .TP
350 .B S_IROTH
351 00004 others have read permission
352 .TP
353 .B S_IWOTH
354 00002 others have write permission
355 .TP
356 .B S_IXOTH
357 00001 others have execute permission
358 .PP
359 .I mode
360 must be specified when
361 .B O_CREAT
362 is in the
363 .IR flags ,
364 and is ignored otherwise.
365
366 .BR creat ()
367 is equivalent to
368 .BR open ()
369 with
370 .I flags
371 equal to
372 .BR O_CREAT|O_WRONLY|O_TRUNC .
373 .SH "RETURN VALUE"
374 .BR open ()
375 and
376 .BR creat ()
377 return the new file descriptor, or \-1 if an error occurred
378 (in which case,
379 .I errno
380 is set appropriately).
381 .SH ERRORS
382 .TP
383 .B EACCES
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
386 .IR pathname ,
387 or the file did not exist yet and write access to the parent directory
388 is not allowed.
389 (See also
390 .BR path_resolution (7).)
391 .TP
392 .B EEXIST
393 .I pathname
394 already exists and
395 .BR O_CREAT " and " O_EXCL
396 were used.
397 .TP
398 .B EFAULT
399 .IR pathname
400 points outside your accessible address space.
401 .TP
402 .B EFBIG
403 .I pathname
404 refers to a regular file, too large to be opened; see
405 .B O_LARGEFILE
406 above.
407 (POSIX.1-2001 specifies the error
408 .B EOVERFLOW
409 for this case.)
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
414 .TP
415 .B EISDIR
416 .I pathname
417 refers to a directory and the access requested involved writing
418 (that is,
419 .B O_WRONLY
420 or
421 .B O_RDWR
422 is set).
423 .TP
424 .B ELOOP
425 Too many symbolic links were encountered in resolving
426 .IR pathname ,
427 or \fBO_NOFOLLOW\fR was specified but
428 .I pathname
429 was a symbolic link.
430 .TP
431 .B EMFILE
432 The process already has the maximum number of files open.
433 .TP
434 .B ENAMETOOLONG
435 .IR pathname
436 was too long.
437 .TP
438 .B ENFILE
439 The system limit on the total number of open files has been reached.
440 .TP
441 .B ENODEV
442 .I pathname
443 refers to a device special file and no corresponding device exists.
444 (This is a Linux kernel bug; in this situation
445 .B ENXIO
446 must be returned.)
447 .TP
448 .B ENOENT
449 .B O_CREAT
450 is not set and the named file does not exist.
451 Or, a directory component in
452 .I pathname
453 does not exist or is a dangling symbolic link.
454 .TP
455 .B ENOMEM
456 Insufficient kernel memory was available.
457 .TP
458 .B ENOSPC
459 .I pathname
460 was to be created but the device containing
461 .I pathname
462 has no room for the new file.
463 .TP
464 .B ENOTDIR
465 A component used as a directory in
466 .I pathname
467 is not, in fact, a directory, or \fBO_DIRECTORY\fR was specified and
468 .I pathname
469 was not a directory.
470 .TP
471 .B ENXIO
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.
476 .TP
477 .B EPERM
478 The
479 .B O_NOATIME
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
483 .RB ( CAP_FOWNER ).
484 .TP
485 .B EROFS
486 .I pathname
487 refers to a file on a read-only filesystem and write access was
488 requested.
489 .TP
490 .B ETXTBSY
491 .I pathname
492 refers to an executable image which is currently being executed and
493 write access was requested.
494 .TP
495 .B EWOULDBLOCK
496 The
497 .B O_NONBLOCK
498 flag was specified, and an incompatible lease was held on the file
499 (see
500 .BR fcntl (2)).
501 .SH "CONFORMING TO"
502 SVr4, 4.3BSD, POSIX.1-2001.
503 The
504 .BR O_NOATIME ,
505 .BR O_NOFOLLOW ,
506 and
507 .B O_DIRECTORY
508 flags are Linux specific.
509 One may have to define the
510 .B _GNU_SOURCE
511 macro to get their definitions.
512 .SH NOTES
513 Under Linux, the
514 .B O_NONBLOCK
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
518 for use with
519 .BR ioctl (2).
520 .LP
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
531 .LP
532 The
533 .B O_DIRECT
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
543 .B _GNU_SOURCE
544 macro to get its definition.
545 .PP
546 There are many infelicities in the protocol underlying NFS, affecting
547 amongst others
548 .BR O_SYNC " and " O_NDELAY .
549
550 POSIX provides for three different variants of synchronized I/O,
551 corresponding to the flags \fBO_SYNC\fR, \fBO_DSYNC\fR and
552 \fBO_RSYNC\fR.
553 Currently (2.1.130) these are all synonymous under Linux.
554
555 Note that
556 .BR open ()
557 can open device special files, but
558 .BR creat ()
559 cannot create them; use
560 .BR mknod (2)
561 instead.
562 .LP
563 On NFS file systems with UID mapping enabled,
564 .BR open ()
565 may
566 return a file descriptor but, for example,
567 .BR read (2)
568 requests are denied
569 with \fBEACCES\fP.
570 This is because the client performs
571 .BR open ()
572 by checking the
573 permissions, but UID mapping is performed by the server upon
574 read and write requests.
575
576 If the file is newly created, its
577 .IR st_atime ,
578 .IR st_ctime ,
579 .I st_mtime
580 fields
581 (respectively, time of last access, time of last status change, and
582 time of last modification; see
583 .BR stat (2))
584 are set
585 to the current time, and so are the
586 .I st_ctime
587 and
588 .I st_mtime
589 fields of the
590 parent directory.
591 Otherwise, if the file is modified because of the
592 .B O_TRUNC
593 flag, its st_ctime and st_mtime fields are set to the current time.
594 .SH BUGS
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
598
599 Currently, it is not possible to enable signal-driven
600 I/O by specifying
601 .B O_ASYNC
602 when calling
603 .BR open ();
604 use
605 .BR fcntl (2)
606 to enable this flag.
607 .\" FIXME . Check bugzilla report on open(O_ASYNC)
608 .\" See http://bugzilla.kernel.org/show_bug.cgi?id=5993
609 .SH "SEE ALSO"
610 .BR close (2),
611 .BR dup (2),
612 .BR fcntl (2),
613 .BR link (2),
614 .BR lseek (2),
615 .BR mknod (2),
616 .BR mount (2),
617 .BR mmap (2),
618 .BR openat (2),
619 .BR read (2),
620 .BR socket (2),
621 .BR stat (2),
622 .BR umask (2),
623 .BR unlink (2),
624 .BR write (2),
625 .BR fopen (3),
626 .BR fifo (7),
627 .BR feature_test_macros (7),
628 .BR path_resolution (7)