]> git.saurik.com Git - apple/xnu.git/blame - bsd/man/man2/open.2
xnu-4903.270.47.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
fe8ab488 63.Nm open , openat
9bccf70c
A
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
fe8ab488
A
74.Ft int
75.Fn openat "int fd" "const char *path" "int oflag" "..."
9bccf70c
A
76.Sh DESCRIPTION
77The file name specified by
78.Fa path
79is opened
2d21ac55
A
80for reading and/or writing,
81as specified by the argument
82.Fa oflag ;
83the file descriptor is returned to the calling process.
84.Pp
9bccf70c 85The
2d21ac55
A
86.Fa oflag
87argument may indicate that the file is to be
9bccf70c
A
88created if it does not exist (by specifying the
89.Dv O_CREAT
2d21ac55 90flag). In this case,
fe8ab488
A
91.Fn open
92and
93.Fn openat
94require an additional argument
2d21ac55
A
95.Fa "mode_t mode" ;
96the file is created with mode
9bccf70c
A
97.Fa mode
98as described in
99.Xr chmod 2
100and modified by the process' umask value (see
101.Xr umask 2 ) .
102.Pp
fe8ab488
A
103The
104.Fn openat
105function is equivalent to the
106.Fn open
107function except in the case where the
108.Fa path
109specifies a relative path.
110In this case the file to be opened is determined relative to the directory
111associated with the file descriptor
112.Fa fd
113instead of the current working directory.
114The
115.Fa oflag
116argument and the optional fourth argument correspond exactly to
117the arguments for
118.Fn open .
119If
120.Fn openat
121is passed the special value
122.Dv AT_FDCWD
123in the
124.Fa fd
125argument, the current working directory is used
126and the behavior is identical to a call to
127.Fn open .
128.Pp
129The flags specified
130for the
131.Fa oflag
132argument are formed by
9bccf70c 133.Em or Ns 'ing
2d21ac55 134the following values:
9bccf70c
A
135.Pp
136.Bd -literal -offset indent -compact
137O_RDONLY open for reading only
138O_WRONLY open for writing only
139O_RDWR open for reading and writing
140O_NONBLOCK do not block on open or for data to become available
141O_APPEND append on each write
142O_CREAT create file if it does not exist
143O_TRUNC truncate size to 0
2d21ac55 144O_EXCL error if O_CREAT and the file exists
9bccf70c
A
145O_SHLOCK atomically obtain a shared lock
146O_EXLOCK atomically obtain an exclusive lock
2d21ac55
A
147O_NOFOLLOW do not follow symlinks
148O_SYMLINK allow open of symlinks
b0d623f7 149O_EVTONLY descriptor requested for event notifications only
6d2010ae 150O_CLOEXEC mark as close-on-exec
9bccf70c
A
151.Ed
152.Pp
153Opening a file with
154.Dv O_APPEND
2d21ac55 155set causes each write on the file to be appended to the end. If
9bccf70c
A
156.Dv O_TRUNC
157is specified and the
158file exists, the file is truncated to zero length.
159If
160.Dv O_EXCL
161is set with
162.Dv O_CREAT
163and the file already
164exists,
165.Fn open
2d21ac55
A
166returns an error.
167This may be used to implement a simple exclusive-access locking mechanism.
9bccf70c
A
168If
169.Dv O_EXCL
6d2010ae
A
170is set with
171.Dv O_CREAT
172and the last component of the pathname is a symbolic link,
9bccf70c 173.Fn open
2d21ac55
A
174will fail even if the symbolic link points to a non-existent name.
175.Pp
9bccf70c
A
176If the
177.Dv O_NONBLOCK
2d21ac55
A
178flag is specified, do not wait for the device or file
179to be ready or available. If the
9bccf70c
A
180.Fn open
181call would result
2d21ac55
A
182in the process being blocked for some reason
183(e.g., waiting for carrier on a dialup line),
9bccf70c
A
184.Fn open
185returns immediately.
2d21ac55
A
186This flag also has the effect of making all subsequent I/O
187on the open file non-blocking.
9bccf70c
A
188.Pp
189When opening a file, a lock with
190.Xr flock 2
191semantics can be obtained by setting
192.Dv O_SHLOCK
193for a shared lock, or
194.Dv O_EXLOCK
195for an exclusive lock.
196If creating a file with
197.Dv O_CREAT ,
198the request for the lock will never fail
199(provided that the underlying filesystem supports locking).
200.Pp
2d21ac55
A
201If
202.Dv O_NOFOLLOW
203is used in the mask and the target file passed to
204.Fn open
205is a symbolic link then the
206.Fn open
207will fail.
208.Pp
209If
210.Dv O_SYMLINK
211is used in the mask and the target file passed to
212.Fn open
213is a symbolic link then the
214.Fn open
215will be for the symbolic link itself, not what it links to.
216.Pp
b0d623f7
A
217The
218.Dv O_EVTONLY
219flag is only intended for monitoring a file for changes (e.g. kqueue). Note: when
220this flag is used, the opened file will not prevent an unmount
221of the volume that contains the file.
222.Pp
6d2010ae
A
223The
224.Dv O_CLOEXEC
225flag causes the file descriptor to be marked as close-on-exec,
226setting the
227.Dv FD_CLOEXEC
228flag. The state of the file descriptor flags can be inspected
229using the F_GETFD fcntl. See
230.Xr fcntl 2 .
231.Pp
9bccf70c
A
232If successful,
233.Fn open
234returns a non-negative integer, termed a file descriptor.
235It returns -1 on failure.
2d21ac55
A
236The file pointer (used to mark the current position within the file)
237is set to the beginning of the file.
9bccf70c 238.Pp
2d21ac55
A
239When a new file is created,
240it is given the group of the directory which contains it.
9bccf70c
A
241.Pp
242The new descriptor is set to remain open across
243.Xr execve
244system calls; see
245.Xr close 2
246and
247.Xr fcntl 2 .
248.Pp
249The system imposes a limit on the number of file descriptors
2d21ac55 250that can be held open simultaneously by one process.
9bccf70c
A
251.Xr Getdtablesize 2
252returns the current system limit.
2d21ac55
A
253.Sh RETURN VALUES
254If successful,
255.Fn open
256returns a non-negative integer, termed a file descriptor.
257It returns -1 on failure, and sets
258.Va errno
259to indicate the error.
9bccf70c
A
260.Sh ERRORS
261The named file is opened unless:
262.Bl -tag -width Er
2d21ac55 263.\" ===========
9bccf70c
A
264.It Bq Er EACCES
265Search permission is denied for a component of the path prefix.
2d21ac55 266.\" ===========
9bccf70c
A
267.It Bq Er EACCES
268The required permissions (for reading and/or writing)
269are denied for the given flags.
2d21ac55 270.\" ===========
9bccf70c
A
271.It Bq Er EACCES
272.Dv O_CREAT
273is specified,
274the file does not exist,
275and the directory in which it is to be created
276does not permit writing.
2d21ac55
A
277.\" ===========
278.It Bq Er EACCES
279.Dv O_TRUNC
280is specified and write permission is denied.
281.\" ===========
282.It Bq Er EAGAIN
283.Fa path
284specifies the slave side of a locked pseudo-terminal device.
285.\" ===========
286.It Bq Er EDQUOT
287.Dv O_CREAT
288is specified,
289the file does not exist,
290and the directory in which the entry for the new file
291is being placed cannot be extended because the
292user's quota of disk blocks on the file system
293containing the directory has been exhausted.
294.\" ===========
295.It Bq Er EDQUOT
296.Dv O_CREAT
297is specified,
298the file does not exist,
299and the user's quota of inodes on the file system
300on which the file is being created has been exhausted.
301.\" ===========
302.It Bq Er EEXIST
303.Dv O_CREAT
304and
305.Dv O_EXCL
306are specified and the file exists.
307.\" ===========
308.It Bq Er EFAULT
309.Fa Path
310points outside the process's allocated address space.
311.\" ===========
312.It Bq Er EINTR
313The
314.Fn open
315operation is interrupted by a signal.
316.\" ===========
317.It Bq Er EINVAL
318The value of
319.Fa oflag
320is not valid.
321.\" ===========
322.It Bq Er EIO
323An I/O error occurs while making the directory entry or
324allocating the inode for
325.Dv O_CREAT .
326.\" ===========
9bccf70c
A
327.It Bq Er EISDIR
328The named file is a directory, and the arguments specify
2d21ac55
A
329that it is to be opened for writing.
330.\" ===========
331.It Bq Er ELOOP
332Too many symbolic links are encountered in translating the pathname.
333This is taken to be indicative of a looping symbolic link.
334.\" ===========
9bccf70c
A
335.It Bq Er EMFILE
336The process has already reached its limit for open file descriptors.
2d21ac55
A
337.\" ===========
338.It Bq Er ENAMETOOLONG
339A component of a pathname exceeds
340.Dv {NAME_MAX}
341characters, or an entire path name exceeded
342.Dv {PATH_MAX}
343characters.
344.\" ===========
9bccf70c
A
345.It Bq Er ENFILE
346The system file table is full.
2d21ac55
A
347.\" ===========
348.It Bq Er ELOOP
349.Dv O_NOFOLLOW
350was specified and the target is a symbolic link.
351.\" ===========
352.It Bq Er ENOENT
353.Dv O_CREAT
354is not set and the named file does not exist.
355.\" ===========
356.It Bq Er ENOENT
357A component of the path name that must exist does not exist.
358.\" ===========
9bccf70c
A
359.It Bq Er ENOSPC
360.Dv O_CREAT
361is specified,
362the file does not exist,
363and the directory in which the entry for the new file is being placed
364cannot be extended because there is no space left on the file
365system containing the directory.
2d21ac55 366.\" ===========
9bccf70c
A
367.It Bq Er ENOSPC
368.Dv O_CREAT
369is specified,
370the file does not exist,
371and there are no free inodes on the file system on which the
372file is being created.
2d21ac55
A
373.\" ===========
374.It Bq Er ENOTDIR
375A component of the path prefix is not a directory.
376.\" ===========
377.It Bq Er ENXIO
378The named file is a character-special or block-special file
379and the device associated with this special file does not exist.
380.\" ===========
381.It Bq Er ENXIO
382O_NONBLOCK and O_WRONLY are set, the file is a FIFO,
383and no process has it open for reading.
384.\" ===========
385.It Bq Er EOPNOTSUPP
386.Dv O_SHLOCK
387or
388.Dv O_EXLOCK
389is specified, but the underlying filesystem does not support locking.
390.\" ===========
391.It Bq Er EOPNOTSUPP
392An attempt is made to open a socket (not currently implemented).
393.\" ===========
394.It Bq Er EOVERFLOW
395The named file is a regular file
396and its size does not fit in an object of type off_t.
397.\" ===========
398.It Bq Er EROFS
399The named file resides on a read-only file system,
400and the file is to be modified.
401.\" ===========
9bccf70c
A
402.It Bq Er ETXTBSY
403The file is a pure procedure (shared text) file that is being
404executed and the
405.Fn open
406call requests write access.
fe8ab488
A
407.It Bq Eq EBADF
408The
409.Fa path
410argument does not specify an absolute path and the
411.Fa fd
412argument is
413neither
414.Dv AT_FDCWD
415nor a valid file descriptor open for searching.
416.It Bq Eq ENOTDIR
417The
418.Fa path
419argument is not an absolute path and
420.Fa fd
421is neither
422.Dv AT_FDCWD
423nor a file descriptor associated with a directory.
9bccf70c 424.El
2d21ac55
A
425.Sh COMPATIBILITY
426.Fn open
427on a terminal device (i.e., /dev/console)
428will now make that device a controlling terminal for the process.
429Use the O_NOCTTY flag to open a terminal device
430without changing your controlling terminal.
9bccf70c
A
431.Sh SEE ALSO
432.Xr chmod 2 ,
433.Xr close 2 ,
434.Xr dup 2 ,
435.Xr getdtablesize 2 ,
436.Xr lseek 2 ,
437.Xr read 2 ,
2d21ac55
A
438.Xr umask 2 ,
439.Xr write 2
9bccf70c
A
440.Sh HISTORY
441An
442.Fn open
443function call appeared in
444.At v6 .
fe8ab488
A
445The
446.Fn openat
447function was introduced in OS X 10.10