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