1 .\" $NetBSD: open.2,v 1.47 2010/09/22 17:58:09 wiz Exp $
3 .\" Copyright (c) 1980, 1991, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. Neither the name of the University nor the names of its contributors
15 .\" may be used to endorse or promote products derived from this software
16 .\" without specific prior written permission.
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" @(#)open.2 8.2 (Berkeley) 11/16/93
32 .Dd September 22, 2010
37 .Nd open or create a file for reading or writing
43 .Fn open "const char *path" "int flags" "mode_t mode"
45 The file name specified by
48 for reading and/or writing as specified by the
51 and the file descriptor returned to the calling process.
56 the values listed below.
57 Applications must specify exactly one of the first three values
58 (file access methods):
59 .Bl -tag -offset indent -width O_NONBLOCK
61 Open for reading only.
63 Open for writing only.
65 Open for reading and writing.
68 Any combination of the following may be used:
69 .Bl -tag -offset indent -width O_NONBLOCK
71 Do not block on open or for data to become available.
73 Append to the file on each write.
75 Create the file if it does not exist, in which case the file is
80 and modified by the process' umask value (see
87 and the file already exists.
89 Atomically obtain a shared lock.
91 Atomically obtain an exclusive lock.
93 If last path element is a symlink, don't follow it.
94 This option is provided for compatibility with other operating
95 systems, but its security value is questionable.
97 If set, write operations will be performed according to synchronized
98 I/O data integrity completion:
99 each write will wait for the file data to be committed to stable
102 If set, write operations will be performed according to synchronized
103 I/O file integrity completion:
104 each write will wait for both the file data and file status to be
105 committed to stable storage.
107 If set, read operations will complete at the same level of
108 integrity which is in effect for write operations:
109 if specified together with
111 each read will wait for the file status to be committed to stable
118 only, or specifying it without any other synchronized I/O integrity
119 completion flag set, has no further effect.
121 Alternate I/O semantics will be used for read and write operations
122 on the file descriptor.
123 Alternate semantics are defined by the underlying layers and will not
124 have any alternate effect in most cases.
126 If the file is a terminal device, the opened device is not
127 made the controlling terminal for the session.
128 This flag has no effect on
130 since the system defaults to the abovementioned behaviour.
131 The flag is present only for standards conformance.
133 If set on a regular file, data I/O operations will not buffer the data
134 being transferred in the kernel's cache, but rather transfer the data
135 directly between user memory and the underlying device driver if possible.
136 This flag is advisory; the request may be performed in the normal
137 buffered fashion if certain conditions are not met, e.g. if the request
138 is not sufficiently aligned or if the file is mapped.
140 To meet the alignment requirements for direct I/O, the file offset,
141 the length of the I/O and the address of the buffer in memory must all
145 If the I/O request is made
146 using an interface that supports scatter/gather via struct iovec, each
147 element of the request must meet the above alignment constraints.
149 Fail if the file is not a directory.
154 set causes each write on the file
155 to be appended to the end.
159 file exists, the file is truncated to zero length.
169 This may be used to implement a simple exclusive access locking mechanism.
172 is set and the last component of the pathname is
175 will fail even if the symbolic
176 link points to a non-existent name.
180 flag is specified, do not wait for the device or file to be ready or
185 in the process being blocked for some reason (e.g., waiting for
186 carrier on a dialup line),
189 This flag also has the effect of making all subsequent I/O on the open file non-blocking.
191 When opening a file, a lock with
193 semantics can be obtained by setting
195 for a shared lock, or
197 for an exclusive lock.
198 If creating a file with
200 the request for the lock will never fail
201 (provided that the underlying filesystem supports locking).
205 is successful, the file pointer used to mark the current position within
206 the file is set to the beginning of the file.
208 When a new file is created it is given the group of the directory
211 The new descriptor is set to remain open across
218 The system imposes a limit on the number of file descriptors
219 open simultaneously by one process.
222 returns the current system limit.
226 returns a non-negative integer, termed a file descriptor.
227 Otherwise, a value of \-1 is returned and
229 is set to indicate the error.
231 The named file is opened unless:
234 Search permission is denied for a component of the path prefix,
235 the required permissions (for reading and/or writing)
236 are denied for the given flags, or
239 the file does not exist,
240 and the directory in which it is to be created
241 does not permit writing.
245 the file does not exist,
246 and the directory in which the entry for the new file
247 is being placed cannot be extended because the
248 user's quota of disk blocks on the file system
249 containing the directory has been exhausted; or
252 the file does not exist,
253 and the user's quota of inodes on the file system on
254 which the file is being created has been exhausted.
259 were specified and the file exists.
262 points outside the process's allocated address space.
265 was specified, but the last path component is a symlink.
274 operation was interrupted by a signal.
276 An I/O error occurred while making the directory entry or
277 allocating the inode for
280 The named file is a directory, and the arguments specify
281 it is to be opened for writing.
283 Too many symbolic links were encountered in translating the pathname.
285 The process has already reached its limit for open file descriptors.
286 .It Bq Er ENAMETOOLONG
287 A component of a pathname exceeded
289 characters, or an entire path name exceeded
293 The system file table is full.
296 is not set and the named file does not exist, or
297 a component of the path name that must exist does not exist.
301 the file does not exist,
302 and the directory in which the entry for the new file is being placed
303 cannot be extended because there is no space left on the file
304 system containing the directory; or
307 the file does not exist,
308 and there are no free inodes on the file system on which the
309 file is being created.
311 A component of the path prefix is not a directory; or
313 is specified and the last path component is not a directory.
315 The named file is a character special or block
316 special file, and the device associated with this special file
323 is set and no process has the file open for reading.
328 is specified but the underlying filesystem does not support locking; or
329 an attempt was made to open a socket (not currently implemented).
331 The file's flags (see
333 don't allow the file to be opened.
335 The named file resides on a read-only file system,
336 and the file is to be modified.
338 The file is a pure procedure (shared text) file that is being
341 call requests write access.
364 are extensions defined in
371 flags are non-standard extensions and should not be used if portability
376 function call appeared in