1 .\" $NetBSD: open.2,v 1.38 2006/10/23 14:04:05 rillig 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
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.
152 set causes each write on the file
153 to be appended to the end.
157 file exists, the file is truncated to zero length.
167 This may be used to implement a simple exclusive access locking mechanism.
170 is set and the last component of the pathname is
173 will fail even if the symbolic
174 link points to a non-existent name.
178 flag is specified, do not wait for the device or file to be ready or
183 in the process being blocked for some reason (e.g., waiting for
184 carrier on a dialup line),
187 This flag also has the effect of making all subsequent I/O on the open file non-blocking.
189 When opening a file, a lock with
191 semantics can be obtained by setting
193 for a shared lock, or
195 for an exclusive lock.
196 If creating a file with
198 the request for the lock will never fail
199 (provided that the underlying filesystem supports locking).
203 is successful, the file pointer used to mark the current position within
204 the file is set to the beginning of the file.
206 When a new file is created it is given the group of the directory
209 The new descriptor is set to remain open across
216 The system imposes a limit on the number of file descriptors
217 open simultaneously by one process.
220 returns the current system limit.
224 returns a non-negative integer, termed a file descriptor.
225 Otherwise, a value of \-1 is returned and
227 is set to indicate the error.
229 The named file is opened unless:
232 The file's flags (see
234 don't allow the file to be opened.
236 A component of the path prefix is not a directory.
237 .It Bq Er ENAMETOOLONG
238 A component of a pathname exceeded
240 characters, or an entire path name exceeded
245 is not set and the named file does not exist, or
246 a component of the path name that must exist does not exist.
248 Search permission is denied for a component of the path prefix,
249 the required permissions (for reading and/or writing)
250 are denied for the given flags, or
253 the file does not exist,
254 and the directory in which it is to be created
255 does not permit writing.
257 Too many symbolic links were encountered in translating the pathname.
259 The named file is a directory, and the arguments specify
260 it is to be opened for writing.
262 The named file resides on a read-only file system,
263 and the file is to be modified.
265 The process has already reached its limit for open file descriptors.
267 The system file table is full.
269 The named file is a character special or block
270 special file, and the device associated with this special file
277 is set and no process has the file open for reading.
281 operation was interrupted by a signal.
286 is specified but the underlying filesystem does not support locking.
290 the file does not exist,
291 and the directory in which the entry for the new file is being placed
292 cannot be extended because there is no space left on the file
293 system containing the directory.
297 the file does not exist,
298 and there are no free inodes on the file system on which the
299 file is being created.
303 the file does not exist,
304 and the directory in which the entry for the new file
305 is being placed cannot be extended because the
306 user's quota of disk blocks on the file system
307 containing the directory has been exhausted.
311 the file does not exist,
312 and the user's quota of inodes on the file system on
313 which the file is being created has been exhausted.
315 An I/O error occurred while making the directory entry or
316 allocating the inode for
319 The file is a pure procedure (shared text) file that is being
322 call requests write access.
325 points outside the process's allocated address space.
330 were specified and the file exists.
332 An attempt was made to open a socket (not currently implemented).
355 are extensions defined in
363 flags are non-standard extensions and should not be used if portability
368 function call appeared in