]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/open.2
sync
[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
1c1e15ed
MK
186avoid denial-of-service problems if \fBopendir\fR(3) is called on a
187FIFO or tape device, but should not be used outside of the
188implementation of \fBopendir\fR.
189.TP
fea681da
MK
190.B O_EXCL
191When used with
192.BR O_CREAT ,
193if the file already exists it is an error and the
1c1e15ed 194.BR open ()
c13182ef
MK
195will fail.
196In this context, a symbolic link exists, regardless
1f6ceb40 197of where it points to.
fea681da 198.B O_EXCL
1f6ceb40 199is broken on NFS file systems; programs which rely on it for performing
c13182ef
MK
200locking tasks will contain a race condition.
201The solution for performing
1c1e15ed
MK
202atomic file locking using a lockfile is to create a unique file on
203the same file system (e.g., incorporating hostname and pid), use
fea681da 204.BR link (2)
c13182ef 205to make a link to the lockfile.
0bfa087b 206If \fBlink\fP(2) returns 0, the lock is
c13182ef
MK
207successful.
208Otherwise, use
fea681da
MK
209.BR stat (2)
210on the unique file to check if its link count has increased to 2,
211in which case the lock is also successful.
212.TP
1c1e15ed
MK
213.B O_LARGEFILE
214(LFS)
215Allow files whose sizes cannot be represented in an
8478ee02 216.I off_t
1c1e15ed 217(but can be represented in an
8478ee02 218.IR off64_t )
1c1e15ed 219to be opened.
c13182ef 220The
bcdd964e
MK
221.B _LARGEFILE64_SOURCE
222macro must be defined in order to obtain this definition.
c13182ef 223Setting the
bcdd964e 224.B _FILE_OFFSET_BITS
9f3d8b28
MK
225feature test macro to 64 (rather than using
226.BR O_LARGEFILE )
227is the preferred method of obtaining
228method of accessing large files on 32-bit systems (see
2dcbf4f7 229.BR feature_test_macros (7)).
1c1e15ed
MK
230.TP
231.B O_NOATIME
232(Since Linux 2.6.8)
310b7919
MK
233Do not update the file last access time (st_atime in the inode)
234when the file is
1c1e15ed
MK
235.BR read (2).
236This flag is intended for use by indexing or backup programs,
237where its use can significantly reduce the amount of disk activity.
238This flag may not be effective on all filesystems.
239One example is NFS, where the server maintains the access time.
c13182ef 240.\" FIXME? The O_NOATIME flag also affects the treatment of st_atime
92057f4d 241.\" by mmap() and readdir(2), MTK, Dec 04.
1c1e15ed 242.TP
fea681da
MK
243.B O_NOCTTY
244If
245.I pathname
246refers to a terminal device \(em see
247.BR tty (4)
248\(em it will not become the process's controlling terminal even if the
249process does not have one.
250.TP
1c1e15ed 251.B O_NOFOLLOW
c13182ef
MK
252If \fIpathname\fR is a symbolic link, then the open fails.
253This is a FreeBSD extension, which was added to Linux in version 2.1.126.
1c1e15ed 254Symbolic links in earlier components of the pathname will still be
e366dbc4
MK
255followed.
256.\" The headers from glibc 2.0.100 and later include a
257.\" definition of this flag; \fIkernels before 2.1.126 will ignore it if
258.\" used\fR.
fea681da
MK
259.TP
260.BR O_NONBLOCK " or " O_NDELAY
c13182ef
MK
261When possible, the file is opened in non-blocking mode.
262Neither the
1c1e15ed 263.BR open ()
fea681da
MK
264nor any subsequent operations on the file descriptor which is
265returned will cause the calling process to wait.
266For the handling of FIFOs (named pipes), see also
af5b2ef2 267.BR fifo (7).
db28bfac
MK
268For a discussion of the effect of
269.BR O_NONBLOCK
270in conjunction with mandatory file locks and with file leases, see
271.BR fcntl (2).
fea681da
MK
272.TP
273.B O_SYNC
c13182ef
MK
274The file is opened for synchronous I/O.
275Any
0bfa087b 276.BR write (2)s
fea681da
MK
277on the resulting file descriptor will block the calling process until
278the data has been physically written to the underlying hardware.
db28bfac 279.IR "But see RESTRICTIONS below" .
fea681da 280.TP
1c1e15ed
MK
281.B O_TRUNC
282If the file already exists and is a regular file and the open mode allows
283writing (i.e., is O_RDWR or O_WRONLY) it will be truncated to length 0.
284If the file is a FIFO or terminal device file, the O_TRUNC
c13182ef
MK
285flag is ignored.
286Otherwise the effect of O_TRUNC is unspecified.
fea681da
MK
287.PP
288Some of these optional flags can be altered using
0bfa087b 289.BR fcntl (2)
fea681da
MK
290after the file has been opened.
291
292The argument
293.I mode
c13182ef
MK
294specifies the permissions to use in case a new file is created.
295It is
fea681da
MK
296modified by the process's
297.BR umask
298in the usual way: the permissions of the created file are
299.BR "(mode & ~umask)" .
300Note that this mode only applies to future accesses of the
301newly created file; the
1c1e15ed 302.BR open ()
fea681da
MK
303call that creates a read-only file may well return a read/write
304file descriptor.
305.PP
306The following symbolic constants are provided for
307.IR mode :
308.TP
309.B S_IRWXU
31000700 user (file owner) has read, write and execute permission
311.TP
cc513f7f 312.B S_IRUSR
fea681da
MK
31300400 user has read permission
314.TP
cc513f7f 315.B S_IWUSR
fea681da
MK
31600200 user has write permission
317.TP
cc513f7f 318.B S_IXUSR
fea681da
MK
31900100 user has execute permission
320.TP
321.B S_IRWXG
32200070 group has read, write and execute permission
323.TP
324.B S_IRGRP
32500040 group has read permission
326.TP
327.B S_IWGRP
32800020 group has write permission
329.TP
330.B S_IXGRP
33100010 group has execute permission
332.TP
333.B S_IRWXO
33400007 others have read, write and execute permission
335.TP
336.B S_IROTH
33700004 others have read permission
338.TP
339.B S_IWOTH
d301ee6c 34000002 others have write permission
fea681da
MK
341.TP
342.B S_IXOTH
34300001 others have execute permission
344.PP
345.I mode
346must be specified when
347.B O_CREAT
348is in the
349.IR flags ,
350and is ignored otherwise.
351
1c1e15ed 352.BR creat ()
fea681da 353is equivalent to
1c1e15ed 354.BR open ()
fea681da
MK
355with
356.I flags
357equal to
358.BR O_CREAT|O_WRONLY|O_TRUNC .
359.SH "RETURN VALUE"
c13182ef
MK
360.BR open ()
361and
e1d6264d 362.BR creat ()
1c1e15ed
MK
363return the new file descriptor, or \-1 if an error occurred
364(in which case,
fea681da
MK
365.I errno
366is set appropriately).
28c54d45 367.SH NOTES
fea681da 368Note that
1c1e15ed 369.BR open ()
fea681da 370can open device special files, but
1c1e15ed 371.BR creat ()
e9496f74 372cannot create them; use
fea681da
MK
373.BR mknod (2)
374instead.
375.LP
c13182ef 376On NFS file systems with UID mapping enabled, \fBopen\fP() may
1c1e15ed
MK
377return a file descriptor but e.g. \fBread\fP(2) requests are denied
378with \fBEACCES\fP.
e366dbc4 379This is because the client performs \fBopen\fP() by checking the
1c1e15ed
MK
380permissions, but UID mapping is performed by the server upon
381read and write requests.
fea681da 382
310b7919
MK
383If the file is newly created, its st_atime, st_ctime, st_mtime fields
384(respectively, time of last access, time of last status change, and
385time of last modification; see
386.BR stat (2))
387are set
388to the current time, and so are the st_ctime and st_mtime fields of the
fea681da
MK
389parent directory.
390Otherwise, if the file is modified because of the O_TRUNC flag,
310b7919 391its st_ctime and st_mtime fields are set to the current time.
fea681da
MK
392.SH ERRORS
393.TP
394.B EACCES
395The requested access to the file is not allowed, or search permission
396is denied for one of the directories in the path prefix of
397.IR pathname ,
398or the file did not exist yet and write access to the parent directory
399is not allowed.
400(See also
401.BR path_resolution (2).)
402.TP
403.B EEXIST
404.I pathname
405already exists and
406.BR O_CREAT " and " O_EXCL
407were used.
408.TP
409.B EFAULT
c13182ef 410.IR pathname
e1d6264d 411points outside your accessible address space.
fea681da 412.TP
9f5773f7
MK
413.B EFBIG
414.I pathname
415refers to a regular file, too large to be opened; see O_LARGEFILE above.
c13182ef 416(POSIX.1-2001 specifies the error
9f5773f7
MK
417.B EOVERFLOW
418for this case.)
419.\" FIXME Maybe this deviation from the standard will get repaired.
420.\" See http://bugzilla.kernel.org/show_bug.cgi?id=7253
421.\" "Open of a large file on 32-bit fails with EFBIG, should be EOVERFLOW"
422.\" Reported 2006-10-03
423.TP
fea681da
MK
424.B EISDIR
425.I pathname
426refers to a directory and the access requested involved writing
427(that is,
428.B O_WRONLY
429or
430.B O_RDWR
431is set).
432.TP
433.B ELOOP
434Too many symbolic links were encountered in resolving
435.IR pathname ,
436or \fBO_NOFOLLOW\fR was specified but
437.I pathname
438was a symbolic link.
439.TP
440.B EMFILE
441The process already has the maximum number of files open.
442.TP
443.B ENAMETOOLONG
e1d6264d
MK
444.IR pathname
445was too long.
fea681da
MK
446.TP
447.B ENFILE
448The system limit on the total number of open files has been reached.
449.TP
450.B ENODEV
451.I pathname
452refers to a device special file and no corresponding device exists.
e9496f74 453(This is a Linux kernel bug; in this situation ENXIO must be returned.)
fea681da
MK
454.TP
455.B ENOENT
456O_CREAT is not set and the named file does not exist.
457Or, a directory component in
458.I pathname
459does not exist or is a dangling symbolic link.
460.TP
461.B ENOMEM
462Insufficient kernel memory was available.
463.TP
464.B ENOSPC
465.I pathname
466was to be created but the device containing
467.I pathname
468has no room for the new file.
469.TP
470.B ENOTDIR
471A component used as a directory in
472.I pathname
473is not, in fact, a directory, or \fBO_DIRECTORY\fR was specified and
474.I pathname
475was not a directory.
476.TP
477.B ENXIO
478O_NONBLOCK | O_WRONLY is set, the named file is a FIFO and
479no process has the file open for reading.
480Or, the file is a device special file and no corresponding device exists.
481.TP
1c1e15ed
MK
482.B EPERM
483The
484.B O_NOATIME
485flag was specified, but the effective user ID of the caller
486.\" Strictly speaking, it's the file system UID... (MTK)
487did not match the owner of the file and the caller was not privileged
488.RB ( CAP_FOWNER ).
489.TP
fea681da
MK
490.B EROFS
491.I pathname
492refers to a file on a read-only filesystem and write access was
493requested.
494.TP
495.B ETXTBSY
496.I pathname
497refers to an executable image which is currently being executed and
498write access was requested.
d3952311
MK
499.TP
500.B EWOULDBLOCK
501The
502.B O_NONBLOCK
503flag was specified, and an incompatible lease was held on the file
504(see
505.BR fcntl (2)).
fea681da
MK
506.SH NOTE
507Under Linux, the O_NONBLOCK flag indicates that one wants to open
508but does not necessarily have the intention to read or write.
509This is typically used to open devices in order to get a file descriptor
510for use with
511.BR ioctl (2).
512.SH "CONFORMING TO"
97c1eac8 513SVr4, 4.3BSD, POSIX.1-2001.
fea681da 514The
1c1e15ed
MK
515.BR O_NOATIME ,
516.BR O_NOFOLLOW ,
fea681da
MK
517and
518.B O_DIRECTORY
75b48e9d 519flags are Linux specific.
fea681da
MK
520One may have to define the
521.B _GNU_SOURCE
522macro to get their definitions.
523.LP
524The (undefined) effect of
525.B O_RDONLY | O_TRUNC
c13182ef 526varies among implementations.
bcdd964e 527On many systems the file is actually truncated.
fea681da
MK
528.\" Linux 2.0, 2.5: truncate
529.\" Solaris 5.7, 5.8: truncate
530.\" Irix 6.5: truncate
531.\" Tru64 5.1B: truncate
532.\" HP-UX 11.22: truncate
533.\" FreeBSD 4.7: truncate
534.LP
535The
536.B O_DIRECT
537flag was introduced in SGI IRIX, where it has alignment restrictions
c13182ef
MK
538similar to those of Linux 2.4.
539IRIX has also a fcntl(2) call to
540query appropriate alignments, and sizes.
541FreeBSD 4.x introduced
fea681da
MK
542a flag of same name, but without alignment restrictions.
543Support was added under Linux in kernel version 2.4.10.
544Older Linux kernels simply ignore this flag.
1f6ceb40
MK
545One may have to define the
546.B _GNU_SOURCE
547macro to get its definition.
fea681da
MK
548.SH BUGS
549"The thing that has always disturbed me about O_DIRECT is that the whole
550interface is just stupid, and was probably designed by a deranged monkey
e9496f74 551on some serious mind-controlling substances." \(em Linus
b50582eb
MK
552
553Currently, it is not possible to enable signal-driven
554I/O by specifying
555.B O_ASYNC
c13182ef 556when calling
b50582eb
MK
557.BR open ();
558use
559.BR fcntl (2)
560to enable this flag.
92057f4d
MK
561.\" FIXME Check bugzilla report on open(O_ASYNC)
562.\" See http://bugzilla.kernel.org/show_bug.cgi?id=5993
fea681da
MK
563.SH RESTRICTIONS
564There are many infelicities in the protocol underlying NFS, affecting
565amongst others
566.BR O_SYNC " and " O_NDELAY .
567
568POSIX provides for three different variants of synchronised I/O,
569corresponding to the flags \fBO_SYNC\fR, \fBO_DSYNC\fR and
c13182ef
MK
570\fBO_RSYNC\fR.
571Currently (2.1.130) these are all synonymous under Linux.
fea681da
MK
572.SH "SEE ALSO"
573.BR close (2),
e366dbc4 574.BR dup (2),
fea681da
MK
575.BR fcntl (2),
576.BR link (2),
1f6ceb40 577.BR lseek (2),
fea681da
MK
578.BR mknod (2),
579.BR mount (2),
e366dbc4 580.BR mmap (2),
28c54d45 581.BR openat (2),
fea681da
MK
582.BR path_resolution (2),
583.BR read (2),
584.BR socket (2),
585.BR stat (2),
586.BR umask (2),
587.BR unlink (2),
588.BR write (2),
589.BR fopen (3),
50e5322c 590.BR fifo (7),
a8e7c990 591.BR feature_test_macros (7)