]> git.saurik.com Git - apple/xnu.git/blame - bsd/man/man2/open.2
xnu-2050.48.11.tar.gz
[apple/xnu.git] / bsd / man / man2 / open.2
CommitLineData
b0d623f7 1.\"
6d2010ae 2.\" Copyright (c) 2010 Apple Inc. All rights reserved.
b0d623f7
A
3.\"
4.\" @APPLE_LICENSE_HEADER_START@
5.\"
6.\" This file contains Original Code and/or Modifications of Original Code
7.\" as defined in and that are subject to the Apple Public Source License
8.\" Version 2.0 (the 'License'). You may not use this file except in
9.\" compliance with the License. Please obtain a copy of the License at
10.\" http://www.opensource.apple.com/apsl/ and read it before using this
11.\" file.
12.\"
13.\" The Original Code and all software distributed under the License are
14.\" distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15.\" EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16.\" INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17.\" FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18.\" Please see the License for the specific language governing rights and
19.\" limitations under the License.
20.\"
21.\" @APPLE_LICENSE_HEADER_END@
22.\"
23.\"
9bccf70c
A
24.\" $NetBSD: open.2,v 1.8 1995/02/27 12:35:14 cgd Exp $
25.\"
26.\" Copyright (c) 1980, 1991, 1993
27.\" The Regents of the University of California. All rights reserved.
28.\"
29.\" Redistribution and use in source and binary forms, with or without
30.\" modification, are permitted provided that the following conditions
31.\" are met:
32.\" 1. Redistributions of source code must retain the above copyright
33.\" notice, this list of conditions and the following disclaimer.
34.\" 2. Redistributions in binary form must reproduce the above copyright
35.\" notice, this list of conditions and the following disclaimer in the
36.\" documentation and/or other materials provided with the distribution.
37.\" 3. All advertising materials mentioning features or use of this software
38.\" must display the following acknowledgement:
39.\" This product includes software developed by the University of
40.\" California, Berkeley and its contributors.
41.\" 4. Neither the name of the University nor the names of its contributors
42.\" may be used to endorse or promote products derived from this software
43.\" without specific prior written permission.
44.\"
45.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55.\" SUCH DAMAGE.
56.\"
57.\" @(#)open.2 8.2 (Berkeley) 11/16/93
58.\"
6d2010ae 59.Dd November 10, 2010
9bccf70c
A
60.Dt OPEN 2
61.Os BSD 4
62.Sh NAME
63.Nm open
64.Nd open or create a file for reading or writing
65.Sh SYNOPSIS
2d21ac55 66.\" OH??? .Fd #include <sys/stat.h>
9bccf70c
A
67.Fd #include <fcntl.h>
68.Ft int
2d21ac55
A
69.Fo open
70.Fa "const char *path"
71.Fa "int oflag"
72.Fa "..."
73.Fc
9bccf70c
A
74.Sh DESCRIPTION
75The file name specified by
76.Fa path
77is opened
2d21ac55
A
78for reading and/or writing,
79as specified by the argument
80.Fa oflag ;
81the file descriptor is returned to the calling process.
82.Pp
9bccf70c 83The
2d21ac55
A
84.Fa oflag
85argument may indicate that the file is to be
9bccf70c
A
86created if it does not exist (by specifying the
87.Dv O_CREAT
2d21ac55
A
88flag). In this case,
89.Nm
90requires a third argument
91.Fa "mode_t mode" ;
92the file is created with mode
9bccf70c
A
93.Fa mode
94as described in
95.Xr chmod 2
96and modified by the process' umask value (see
97.Xr umask 2 ) .
98.Pp
99The flags specified are formed by
100.Em or Ns 'ing
2d21ac55 101the following values:
9bccf70c
A
102.Pp
103.Bd -literal -offset indent -compact
104O_RDONLY open for reading only
105O_WRONLY open for writing only
106O_RDWR open for reading and writing
107O_NONBLOCK do not block on open or for data to become available
108O_APPEND append on each write
109O_CREAT create file if it does not exist
110O_TRUNC truncate size to 0
2d21ac55 111O_EXCL error if O_CREAT and the file exists
9bccf70c
A
112O_SHLOCK atomically obtain a shared lock
113O_EXLOCK atomically obtain an exclusive lock
2d21ac55
A
114O_NOFOLLOW do not follow symlinks
115O_SYMLINK allow open of symlinks
b0d623f7 116O_EVTONLY descriptor requested for event notifications only
6d2010ae 117O_CLOEXEC mark as close-on-exec
9bccf70c
A
118.Ed
119.Pp
120Opening a file with
121.Dv O_APPEND
2d21ac55 122set causes each write on the file to be appended to the end. If
9bccf70c
A
123.Dv O_TRUNC
124is specified and the
125file exists, the file is truncated to zero length.
126If
127.Dv O_EXCL
128is set with
129.Dv O_CREAT
130and the file already
131exists,
132.Fn open
2d21ac55
A
133returns an error.
134This may be used to implement a simple exclusive-access locking mechanism.
9bccf70c
A
135If
136.Dv O_EXCL
6d2010ae
A
137is set with
138.Dv O_CREAT
139and the last component of the pathname is a symbolic link,
9bccf70c 140.Fn open
2d21ac55
A
141will fail even if the symbolic link points to a non-existent name.
142.Pp
9bccf70c
A
143If the
144.Dv O_NONBLOCK
2d21ac55
A
145flag is specified, do not wait for the device or file
146to be ready or available. If the
9bccf70c
A
147.Fn open
148call would result
2d21ac55
A
149in the process being blocked for some reason
150(e.g., waiting for carrier on a dialup line),
9bccf70c
A
151.Fn open
152returns immediately.
2d21ac55
A
153This flag also has the effect of making all subsequent I/O
154on the open file non-blocking.
9bccf70c
A
155.Pp
156When opening a file, a lock with
157.Xr flock 2
158semantics can be obtained by setting
159.Dv O_SHLOCK
160for a shared lock, or
161.Dv O_EXLOCK
162for an exclusive lock.
163If creating a file with
164.Dv O_CREAT ,
165the request for the lock will never fail
166(provided that the underlying filesystem supports locking).
167.Pp
2d21ac55
A
168If
169.Dv O_NOFOLLOW
170is used in the mask and the target file passed to
171.Fn open
172is a symbolic link then the
173.Fn open
174will fail.
175.Pp
176If
177.Dv O_SYMLINK
178is used in the mask and the target file passed to
179.Fn open
180is a symbolic link then the
181.Fn open
182will be for the symbolic link itself, not what it links to.
183.Pp
b0d623f7
A
184The
185.Dv O_EVTONLY
186flag is only intended for monitoring a file for changes (e.g. kqueue). Note: when
187this flag is used, the opened file will not prevent an unmount
188of the volume that contains the file.
189.Pp
6d2010ae
A
190The
191.Dv O_CLOEXEC
192flag causes the file descriptor to be marked as close-on-exec,
193setting the
194.Dv FD_CLOEXEC
195flag. The state of the file descriptor flags can be inspected
196using the F_GETFD fcntl. See
197.Xr fcntl 2 .
198.Pp
9bccf70c
A
199If successful,
200.Fn open
201returns a non-negative integer, termed a file descriptor.
202It returns -1 on failure.
2d21ac55
A
203The file pointer (used to mark the current position within the file)
204is set to the beginning of the file.
9bccf70c 205.Pp
2d21ac55
A
206When a new file is created,
207it is given the group of the directory which contains it.
9bccf70c
A
208.Pp
209The new descriptor is set to remain open across
210.Xr execve
211system calls; see
212.Xr close 2
213and
214.Xr fcntl 2 .
215.Pp
216The system imposes a limit on the number of file descriptors
2d21ac55 217that can be held open simultaneously by one process.
9bccf70c
A
218.Xr Getdtablesize 2
219returns the current system limit.
2d21ac55
A
220.Sh RETURN VALUES
221If successful,
222.Fn open
223returns a non-negative integer, termed a file descriptor.
224It returns -1 on failure, and sets
225.Va errno
226to indicate the error.
9bccf70c
A
227.Sh ERRORS
228The named file is opened unless:
229.Bl -tag -width Er
2d21ac55 230.\" ===========
9bccf70c
A
231.It Bq Er EACCES
232Search permission is denied for a component of the path prefix.
2d21ac55 233.\" ===========
9bccf70c
A
234.It Bq Er EACCES
235The required permissions (for reading and/or writing)
236are denied for the given flags.
2d21ac55 237.\" ===========
9bccf70c
A
238.It Bq Er EACCES
239.Dv O_CREAT
240is specified,
241the file does not exist,
242and the directory in which it is to be created
243does not permit writing.
2d21ac55
A
244.\" ===========
245.It Bq Er EACCES
246.Dv O_TRUNC
247is specified and write permission is denied.
248.\" ===========
249.It Bq Er EAGAIN
250.Fa path
251specifies the slave side of a locked pseudo-terminal device.
252.\" ===========
253.It Bq Er EDQUOT
254.Dv O_CREAT
255is specified,
256the file does not exist,
257and the directory in which the entry for the new file
258is being placed cannot be extended because the
259user's quota of disk blocks on the file system
260containing the directory has been exhausted.
261.\" ===========
262.It Bq Er EDQUOT
263.Dv O_CREAT
264is specified,
265the file does not exist,
266and the user's quota of inodes on the file system
267on which the file is being created has been exhausted.
268.\" ===========
269.It Bq Er EEXIST
270.Dv O_CREAT
271and
272.Dv O_EXCL
273are specified and the file exists.
274.\" ===========
275.It Bq Er EFAULT
276.Fa Path
277points outside the process's allocated address space.
278.\" ===========
279.It Bq Er EINTR
280The
281.Fn open
282operation is interrupted by a signal.
283.\" ===========
284.It Bq Er EINVAL
285The value of
286.Fa oflag
287is not valid.
288.\" ===========
289.It Bq Er EIO
290An I/O error occurs while making the directory entry or
291allocating the inode for
292.Dv O_CREAT .
293.\" ===========
9bccf70c
A
294.It Bq Er EISDIR
295The named file is a directory, and the arguments specify
2d21ac55
A
296that it is to be opened for writing.
297.\" ===========
298.It Bq Er ELOOP
299Too many symbolic links are encountered in translating the pathname.
300This is taken to be indicative of a looping symbolic link.
301.\" ===========
9bccf70c
A
302.It Bq Er EMFILE
303The process has already reached its limit for open file descriptors.
2d21ac55
A
304.\" ===========
305.It Bq Er ENAMETOOLONG
306A component of a pathname exceeds
307.Dv {NAME_MAX}
308characters, or an entire path name exceeded
309.Dv {PATH_MAX}
310characters.
311.\" ===========
9bccf70c
A
312.It Bq Er ENFILE
313The system file table is full.
2d21ac55
A
314.\" ===========
315.It Bq Er ELOOP
316.Dv O_NOFOLLOW
317was specified and the target is a symbolic link.
318.\" ===========
319.It Bq Er ENOENT
320.Dv O_CREAT
321is not set and the named file does not exist.
322.\" ===========
323.It Bq Er ENOENT
324A component of the path name that must exist does not exist.
325.\" ===========
9bccf70c
A
326.It Bq Er ENOSPC
327.Dv O_CREAT
328is specified,
329the file does not exist,
330and the directory in which the entry for the new file is being placed
331cannot be extended because there is no space left on the file
332system containing the directory.
2d21ac55 333.\" ===========
9bccf70c
A
334.It Bq Er ENOSPC
335.Dv O_CREAT
336is specified,
337the file does not exist,
338and there are no free inodes on the file system on which the
339file is being created.
2d21ac55
A
340.\" ===========
341.It Bq Er ENOTDIR
342A component of the path prefix is not a directory.
343.\" ===========
344.It Bq Er ENXIO
345The named file is a character-special or block-special file
346and the device associated with this special file does not exist.
347.\" ===========
348.It Bq Er ENXIO
349O_NONBLOCK and O_WRONLY are set, the file is a FIFO,
350and no process has it open for reading.
351.\" ===========
352.It Bq Er EOPNOTSUPP
353.Dv O_SHLOCK
354or
355.Dv O_EXLOCK
356is specified, but the underlying filesystem does not support locking.
357.\" ===========
358.It Bq Er EOPNOTSUPP
359An attempt is made to open a socket (not currently implemented).
360.\" ===========
361.It Bq Er EOVERFLOW
362The named file is a regular file
363and its size does not fit in an object of type off_t.
364.\" ===========
365.It Bq Er EROFS
366The named file resides on a read-only file system,
367and the file is to be modified.
368.\" ===========
9bccf70c
A
369.It Bq Er ETXTBSY
370The file is a pure procedure (shared text) file that is being
371executed and the
372.Fn open
373call requests write access.
9bccf70c 374.El
2d21ac55
A
375.Sh COMPATIBILITY
376.Fn open
377on a terminal device (i.e., /dev/console)
378will now make that device a controlling terminal for the process.
379Use the O_NOCTTY flag to open a terminal device
380without changing your controlling terminal.
9bccf70c
A
381.Sh SEE ALSO
382.Xr chmod 2 ,
383.Xr close 2 ,
384.Xr dup 2 ,
385.Xr getdtablesize 2 ,
386.Xr lseek 2 ,
387.Xr read 2 ,
2d21ac55
A
388.Xr umask 2 ,
389.Xr write 2
9bccf70c
A
390.Sh HISTORY
391An
392.Fn open
393function call appeared in
394.At v6 .