]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/open.2
Renamed RESTRICTIONS section to NOTES, or moved text in a RESTRICTIONS
[thirdparty/man-pages.git] / man2 / open.2
CommitLineData
fea681da
MK
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.
c13182ef 14.\"
fea681da
MK
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.
c13182ef 22.\"
fea681da
MK
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
305a0578
MK
34.\" Modified 2002-05-07 by Michael Kerrisk <mtk-manpages@gmx.net>
35.\" Modified 2004-06-23 by Michael Kerrisk <mtk-manpages@gmx.net>
1c1e15ed
MK
36.\" 2004-12-08, mtk, reordered flags list alphabetically
37.\" 2004-12-08, Martin Pool <mbp@sourcefrog.net> (& mtk), added O_NOATIME
fea681da 38.\"
e366dbc4 39.TH OPEN 2 2005-06-22 "Linux 2.6.12" "Linux Programmer's Manual"
fea681da
MK
40.SH NAME
41open, 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
e366dbc4 53Given a
c13182ef 54.IR pathname
e366dbc4 55for a file,
1f6ceb40 56.BR open ()
c13182ef 57returns a file descriptor, a small, non-negative integer
e366dbc4
MK
58for use in subsequent system calls
59.RB ( read "(2), " write "(2), " lseek "(2), " fcntl "(2), etc.)."
60The file descriptor returned by a successful call will be
2c4bff36 61the lowest-numbered file descriptor not currently open for the process.
e366dbc4 62.PP
c13182ef 63The new file descriptor is set to remain open across an
e366dbc4 64.BR execve (2)
1f6ceb40
MK
65(i.e., the
66.B FD_CLOEXEC
67file descriptor flag described in
68.BR fcntl (2)
69is initially disabled).
70The file offset is set to the beginning of the file (see
c13182ef 71.BR lseek (2)).
e366dbc4
MK
72.PP
73A call to
74.BR open ()
75creates a new
76.IR "open file description" ,
77an entry in the system-wide table of open files.
e366dbc4
MK
78This entry records the file offset and the file status flags
79(modifiable via the
0bfa087b 80.BR fcntl (2)
e366dbc4
MK
81.B F_SETFL
82operation).
2c4bff36
MK
83A file descriptor is a reference to one of these entries;
84this reference is unaffected if
85.I pathname
86is subsequently removed or modified to refer to a different file.
e366dbc4 87The new open file description is initially not shared
2c4bff36
MK
88with any other process,
89but sharing may arise via
90.BR fork (2).
e366dbc4 91.PP
fea681da
MK
92The parameter
93.I flags
e366dbc4
MK
94must include one of the following
95.IR "access modes" :
96.BR O_RDONLY ", " O_WRONLY ", or " O_RDWR.
97These request opening the file read-only, write-only, or read/write,
98respectively.
bfe9ba67
MK
99
100In addition, zero or more file creation flags and file status flags
c13182ef 101can be
fea681da 102.RI bitwise- or 'd
e366dbc4 103in
bfe9ba67 104.IR flags .
c13182ef
MK
105The
106.I file creation flags
107are
bfe9ba67 108.BR O_CREAT ", " O_EXCL ", " O_NOCTTY ", and " O_TRUNC .
c13182ef
MK
109The
110.I file status flags
bfe9ba67
MK
111are all of the remaining flags listed below.
112The distinction between these two groups of flags is that
113the file status flags can be retrieved and (in some cases)
114modified using
115.BR fcntl (2).
116The full list of file creation flags and file status flags is as follows:
fea681da 117.TP
1c1e15ed 118.B O_APPEND
c13182ef
MK
119The file is opened in append mode.
120Before each
0bfa087b 121.BR write (2),
1e568304 122the file offset is positioned at the end of the file,
1c1e15ed 123as if with
0bfa087b 124.BR lseek (2).
1c1e15ed
MK
125.B O_APPEND
126may lead to corrupted files on NFS file systems if more than one process
c13182ef
MK
127appends data to a file at once.
128This is because NFS does not support
1c1e15ed
MK
129appending to a file, so the client kernel has to simulate it, which
130can't be done without a race condition.
131.TP
132.B O_ASYNC
b50582eb
MK
133Enable signal-driven I/O:
134generate a signal (SIGIO by default, but this can be changed via
1c1e15ed
MK
135.BR fcntl (2))
136when input or output becomes possible on this file descriptor.
c13182ef 137This feature is only available for terminals, pseudo-terminals,
1f6ceb40
MK
138sockets, and (since Linux 2.6) pipes and FIFOs.
139See
1c1e15ed
MK
140.BR fcntl (2)
141for further details.
142.TP
fea681da
MK
143.B O_CREAT
144If the file does not exist it will be created.
145The owner (user ID) of the file is set to the effective user ID
c13182ef
MK
146of the process.
147The group ownership (group ID) is set either to
fea681da
MK
148the effective group ID of the process or to the group ID of the
149parent directory (depending on filesystem type and mount options,
150and the mode of the parent directory, see, e.g., the mount options
151.I bsdgroups
152and
153.I sysvgroups
154of the ext2 filesystem, as described in
155.BR mount (8)).
156.TP
1c1e15ed
MK
157.B O_DIRECT
158Try to minimize cache effects of the I/O to and from this file.
159In general this will degrade performance, but it is useful in
160special situations, such as when applications do their own caching.
161File I/O is done directly to/from user space buffers.
e366dbc4 162The I/O is synchronous, i.e., at the completion of a
1c1e15ed
MK
163.BR read (2)
164or
e366dbc4
MK
165.BR write (2),
166data is guaranteed to have been transferred.
1c1e15ed
MK
167Under Linux 2.4 transfer sizes, and the alignment of user buffer
168and file offset must all be multiples of the logical block size
c13182ef
MK
169of the file system.
170Under Linux 2.6 alignment to 512-byte boundaries
1c1e15ed
MK
171suffices.
172.\" Alignment should satisfy requirements for the underlying device
173.\" There may be coherency problems.
9b54d4fa 174.sp
c13182ef 175A semantically similar (but deprecated) interface for block devices
9b54d4fa 176is described in
1c1e15ed
MK
177.BR raw (8).
178.TP
179.B O_DIRECTORY
c13182ef 180If \fIpathname\fR is not a directory, cause the open to fail.
9f8d688a
MK
181.\" But see the following and its replies:
182.\" http://marc.theaimsgroup.com/?t=112748702800001&r=1&w=2
183.\" [PATCH] open: O_DIRECTORY and O_CREAT together should fail
184.\" O_DIRECTORY | O_CREAT causes O_DIRECTORY to be ignored.
185This flag is Linux-specific, and was added in kernel version 2.1.126, to
60a90ecd
MK
186avoid denial-of-service problems if
187.BR opendir (3)
188is called on a
1c1e15ed 189FIFO or tape device, but should not be used outside of the
9e370fba
MK
190implementation of
191.BR opendir (3).
1c1e15ed 192.TP
fea681da
MK
193.B O_EXCL
194When used with
195.BR O_CREAT ,
196if the file already exists it is an error and the
1c1e15ed 197.BR open ()
c13182ef
MK
198will fail.
199In this context, a symbolic link exists, regardless
1f6ceb40 200of where it points to.
fea681da 201.B O_EXCL
1f6ceb40 202is broken on NFS file systems; programs which rely on it for performing
c13182ef
MK
203locking tasks will contain a race condition.
204The solution for performing
1c1e15ed
MK
205atomic file locking using a lockfile is to create a unique file on
206the same file system (e.g., incorporating hostname and pid), use
fea681da 207.BR link (2)
c13182ef 208to make a link to the lockfile.
60a90ecd
MK
209If
210.BR link (2)
211returns 0, the lock is
c13182ef
MK
212successful.
213Otherwise, use
fea681da
MK
214.BR stat (2)
215on the unique file to check if its link count has increased to 2,
216in which case the lock is also successful.
217.TP
1c1e15ed
MK
218.B O_LARGEFILE
219(LFS)
220Allow files whose sizes cannot be represented in an
8478ee02 221.I off_t
1c1e15ed 222(but can be represented in an
8478ee02 223.IR off64_t )
1c1e15ed 224to be opened.
c13182ef 225The
bcdd964e
MK
226.B _LARGEFILE64_SOURCE
227macro must be defined in order to obtain this definition.
c13182ef 228Setting the
bcdd964e 229.B _FILE_OFFSET_BITS
9f3d8b28
MK
230feature test macro to 64 (rather than using
231.BR O_LARGEFILE )
232is the preferred method of obtaining
233method of accessing large files on 32-bit systems (see
2dcbf4f7 234.BR feature_test_macros (7)).
1c1e15ed
MK
235.TP
236.B O_NOATIME
237(Since Linux 2.6.8)
310b7919
MK
238Do not update the file last access time (st_atime in the inode)
239when the file is
1c1e15ed
MK
240.BR read (2).
241This flag is intended for use by indexing or backup programs,
242where its use can significantly reduce the amount of disk activity.
243This flag may not be effective on all filesystems.
244One example is NFS, where the server maintains the access time.
c13182ef 245.\" FIXME? The O_NOATIME flag also affects the treatment of st_atime
92057f4d 246.\" by mmap() and readdir(2), MTK, Dec 04.
1c1e15ed 247.TP
fea681da
MK
248.B O_NOCTTY
249If
250.I pathname
251refers to a terminal device \(em see
252.BR tty (4)
253\(em it will not become the process's controlling terminal even if the
254process does not have one.
255.TP
1c1e15ed 256.B O_NOFOLLOW
c13182ef
MK
257If \fIpathname\fR is a symbolic link, then the open fails.
258This is a FreeBSD extension, which was added to Linux in version 2.1.126.
1c1e15ed 259Symbolic links in earlier components of the pathname will still be
e366dbc4
MK
260followed.
261.\" The headers from glibc 2.0.100 and later include a
262.\" definition of this flag; \fIkernels before 2.1.126 will ignore it if
263.\" used\fR.
fea681da
MK
264.TP
265.BR O_NONBLOCK " or " O_NDELAY
c13182ef
MK
266When possible, the file is opened in non-blocking mode.
267Neither the
1c1e15ed 268.BR open ()
fea681da
MK
269nor any subsequent operations on the file descriptor which is
270returned will cause the calling process to wait.
271For the handling of FIFOs (named pipes), see also
af5b2ef2 272.BR fifo (7).
db28bfac
MK
273For a discussion of the effect of
274.BR O_NONBLOCK
275in conjunction with mandatory file locks and with file leases, see
276.BR fcntl (2).
fea681da
MK
277.TP
278.B O_SYNC
c13182ef
MK
279The file is opened for synchronous I/O.
280Any
0bfa087b 281.BR write (2)s
fea681da
MK
282on the resulting file descriptor will block the calling process until
283the data has been physically written to the underlying hardware.
b07cd0a9 284.IR "But see NOTES below" .
fea681da 285.TP
1c1e15ed
MK
286.B O_TRUNC
287If the file already exists and is a regular file and the open mode allows
288writing (i.e., is O_RDWR or O_WRONLY) it will be truncated to length 0.
289If the file is a FIFO or terminal device file, the O_TRUNC
c13182ef
MK
290flag is ignored.
291Otherwise the effect of O_TRUNC is unspecified.
fea681da
MK
292.PP
293Some of these optional flags can be altered using
0bfa087b 294.BR fcntl (2)
fea681da
MK
295after the file has been opened.
296
297The argument
298.I mode
c13182ef
MK
299specifies the permissions to use in case a new file is created.
300It is
fea681da
MK
301modified by the process's
302.BR umask
303in the usual way: the permissions of the created file are
304.BR "(mode & ~umask)" .
305Note that this mode only applies to future accesses of the
306newly created file; the
1c1e15ed 307.BR open ()
fea681da
MK
308call that creates a read-only file may well return a read/write
309file descriptor.
310.PP
311The following symbolic constants are provided for
312.IR mode :
313.TP
314.B S_IRWXU
31500700 user (file owner) has read, write and execute permission
316.TP
cc513f7f 317.B S_IRUSR
fea681da
MK
31800400 user has read permission
319.TP
cc513f7f 320.B S_IWUSR
fea681da
MK
32100200 user has write permission
322.TP
cc513f7f 323.B S_IXUSR
fea681da
MK
32400100 user has execute permission
325.TP
326.B S_IRWXG
32700070 group has read, write and execute permission
328.TP
329.B S_IRGRP
33000040 group has read permission
331.TP
332.B S_IWGRP
33300020 group has write permission
334.TP
335.B S_IXGRP
33600010 group has execute permission
337.TP
338.B S_IRWXO
33900007 others have read, write and execute permission
340.TP
341.B S_IROTH
34200004 others have read permission
343.TP
344.B S_IWOTH
d301ee6c 34500002 others have write permission
fea681da
MK
346.TP
347.B S_IXOTH
34800001 others have execute permission
349.PP
350.I mode
351must be specified when
352.B O_CREAT
353is in the
354.IR flags ,
355and is ignored otherwise.
356
1c1e15ed 357.BR creat ()
fea681da 358is equivalent to
1c1e15ed 359.BR open ()
fea681da
MK
360with
361.I flags
362equal to
363.BR O_CREAT|O_WRONLY|O_TRUNC .
364.SH "RETURN VALUE"
c13182ef
MK
365.BR open ()
366and
e1d6264d 367.BR creat ()
1c1e15ed
MK
368return the new file descriptor, or \-1 if an error occurred
369(in which case,
fea681da
MK
370.I errno
371is set appropriately).
28c54d45 372.SH NOTES
b07cd0a9
MK
373There are many infelicities in the protocol underlying NFS, affecting
374amongst others
375.BR O_SYNC " and " O_NDELAY .
376
377POSIX provides for three different variants of synchronised I/O,
378corresponding to the flags \fBO_SYNC\fR, \fBO_DSYNC\fR and
379\fBO_RSYNC\fR.
380Currently (2.1.130) these are all synonymous under Linux.
381
fea681da 382Note that
1c1e15ed 383.BR open ()
fea681da 384can open device special files, but
1c1e15ed 385.BR creat ()
e9496f74 386cannot create them; use
fea681da
MK
387.BR mknod (2)
388instead.
389.LP
60a90ecd
MK
390On NFS file systems with UID mapping enabled,
391.BR open ()
392may
393return a file descriptor but e.g.
394.BR read (2)
395requests are denied
1c1e15ed 396with \fBEACCES\fP.
60a90ecd
MK
397This is because the client performs
398.BR open ()
399by checking the
1c1e15ed
MK
400permissions, but UID mapping is performed by the server upon
401read and write requests.
fea681da 402
310b7919
MK
403If the file is newly created, its st_atime, st_ctime, st_mtime fields
404(respectively, time of last access, time of last status change, and
405time of last modification; see
406.BR stat (2))
407are set
408to the current time, and so are the st_ctime and st_mtime fields of the
fea681da
MK
409parent directory.
410Otherwise, if the file is modified because of the O_TRUNC flag,
310b7919 411its st_ctime and st_mtime fields are set to the current time.
fea681da
MK
412.SH ERRORS
413.TP
414.B EACCES
415The requested access to the file is not allowed, or search permission
416is denied for one of the directories in the path prefix of
417.IR pathname ,
418or the file did not exist yet and write access to the parent directory
419is not allowed.
420(See also
421.BR path_resolution (2).)
422.TP
423.B EEXIST
424.I pathname
425already exists and
426.BR O_CREAT " and " O_EXCL
427were used.
428.TP
429.B EFAULT
c13182ef 430.IR pathname
e1d6264d 431points outside your accessible address space.
fea681da 432.TP
9f5773f7
MK
433.B EFBIG
434.I pathname
435refers to a regular file, too large to be opened; see O_LARGEFILE above.
c13182ef 436(POSIX.1-2001 specifies the error
9f5773f7
MK
437.B EOVERFLOW
438for this case.)
439.\" FIXME Maybe this deviation from the standard will get repaired.
440.\" See http://bugzilla.kernel.org/show_bug.cgi?id=7253
441.\" "Open of a large file on 32-bit fails with EFBIG, should be EOVERFLOW"
442.\" Reported 2006-10-03
443.TP
fea681da
MK
444.B EISDIR
445.I pathname
446refers to a directory and the access requested involved writing
447(that is,
448.B O_WRONLY
449or
450.B O_RDWR
451is set).
452.TP
453.B ELOOP
454Too many symbolic links were encountered in resolving
455.IR pathname ,
456or \fBO_NOFOLLOW\fR was specified but
457.I pathname
458was a symbolic link.
459.TP
460.B EMFILE
461The process already has the maximum number of files open.
462.TP
463.B ENAMETOOLONG
e1d6264d
MK
464.IR pathname
465was too long.
fea681da
MK
466.TP
467.B ENFILE
468The system limit on the total number of open files has been reached.
469.TP
470.B ENODEV
471.I pathname
472refers to a device special file and no corresponding device exists.
e9496f74 473(This is a Linux kernel bug; in this situation ENXIO must be returned.)
fea681da
MK
474.TP
475.B ENOENT
476O_CREAT is not set and the named file does not exist.
477Or, a directory component in
478.I pathname
479does not exist or is a dangling symbolic link.
480.TP
481.B ENOMEM
482Insufficient kernel memory was available.
483.TP
484.B ENOSPC
485.I pathname
486was to be created but the device containing
487.I pathname
488has no room for the new file.
489.TP
490.B ENOTDIR
491A component used as a directory in
492.I pathname
493is not, in fact, a directory, or \fBO_DIRECTORY\fR was specified and
494.I pathname
495was not a directory.
496.TP
497.B ENXIO
498O_NONBLOCK | O_WRONLY is set, the named file is a FIFO and
499no process has the file open for reading.
500Or, the file is a device special file and no corresponding device exists.
501.TP
1c1e15ed
MK
502.B EPERM
503The
504.B O_NOATIME
505flag was specified, but the effective user ID of the caller
506.\" Strictly speaking, it's the file system UID... (MTK)
507did not match the owner of the file and the caller was not privileged
508.RB ( CAP_FOWNER ).
509.TP
fea681da
MK
510.B EROFS
511.I pathname
512refers to a file on a read-only filesystem and write access was
513requested.
514.TP
515.B ETXTBSY
516.I pathname
517refers to an executable image which is currently being executed and
518write access was requested.
d3952311
MK
519.TP
520.B EWOULDBLOCK
521The
522.B O_NONBLOCK
523flag was specified, and an incompatible lease was held on the file
524(see
525.BR fcntl (2)).
19c98696 526.SH NOTES
fea681da
MK
527Under Linux, the O_NONBLOCK flag indicates that one wants to open
528but does not necessarily have the intention to read or write.
529This is typically used to open devices in order to get a file descriptor
530for use with
531.BR ioctl (2).
532.SH "CONFORMING TO"
97c1eac8 533SVr4, 4.3BSD, POSIX.1-2001.
fea681da 534The
1c1e15ed
MK
535.BR O_NOATIME ,
536.BR O_NOFOLLOW ,
fea681da
MK
537and
538.B O_DIRECTORY
75b48e9d 539flags are Linux specific.
fea681da
MK
540One may have to define the
541.B _GNU_SOURCE
542macro to get their definitions.
543.LP
544The (undefined) effect of
545.B O_RDONLY | O_TRUNC
c13182ef 546varies among implementations.
bcdd964e 547On many systems the file is actually truncated.
fea681da
MK
548.\" Linux 2.0, 2.5: truncate
549.\" Solaris 5.7, 5.8: truncate
550.\" Irix 6.5: truncate
551.\" Tru64 5.1B: truncate
552.\" HP-UX 11.22: truncate
553.\" FreeBSD 4.7: truncate
554.LP
555The
556.B O_DIRECT
557flag was introduced in SGI IRIX, where it has alignment restrictions
c13182ef
MK
558similar to those of Linux 2.4.
559IRIX has also a fcntl(2) call to
560query appropriate alignments, and sizes.
561FreeBSD 4.x introduced
fea681da
MK
562a flag of same name, but without alignment restrictions.
563Support was added under Linux in kernel version 2.4.10.
564Older Linux kernels simply ignore this flag.
1f6ceb40
MK
565One may have to define the
566.B _GNU_SOURCE
567macro to get its definition.
fea681da
MK
568.SH BUGS
569"The thing that has always disturbed me about O_DIRECT is that the whole
570interface is just stupid, and was probably designed by a deranged monkey
e9496f74 571on some serious mind-controlling substances." \(em Linus
b50582eb
MK
572
573Currently, it is not possible to enable signal-driven
574I/O by specifying
575.B O_ASYNC
c13182ef 576when calling
b50582eb
MK
577.BR open ();
578use
579.BR fcntl (2)
580to enable this flag.
92057f4d
MK
581.\" FIXME Check bugzilla report on open(O_ASYNC)
582.\" See http://bugzilla.kernel.org/show_bug.cgi?id=5993
fea681da
MK
583.SH "SEE ALSO"
584.BR close (2),
e366dbc4 585.BR dup (2),
fea681da
MK
586.BR fcntl (2),
587.BR link (2),
1f6ceb40 588.BR lseek (2),
fea681da
MK
589.BR mknod (2),
590.BR mount (2),
e366dbc4 591.BR mmap (2),
28c54d45 592.BR openat (2),
fea681da
MK
593.BR path_resolution (2),
594.BR read (2),
595.BR socket (2),
596.BR stat (2),
597.BR umask (2),
598.BR unlink (2),
599.BR write (2),
600.BR fopen (3),
50e5322c 601.BR fifo (7),
a8e7c990 602.BR feature_test_macros (7)