Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / lib / libc / sys / open.2
1 .\" Copyright (c) 1980, 1991, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 3. All advertising materials mentioning features or use of this software
13 .\"    must display the following acknowledgement:
14 .\"     This product includes software developed by the University of
15 .\"     California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\"    may be used to endorse or promote products derived from this software
18 .\"    without specific prior written permission.
19 .\"
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" SUCH DAMAGE.
31 .\"
32 .\"     @(#)open.2      8.2 (Berkeley) 11/16/93
33 .\" $FreeBSD: src/lib/libc/sys/open.2,v 1.11.2.9 2001/12/14 18:34:01 ru Exp $
34 .\" $DragonFly: src/lib/libc/sys/open.2,v 1.2 2003/06/17 04:26:47 dillon Exp $
35 .\"
36 .Dd November 16, 1993
37 .Dt OPEN 2
38 .Os
39 .Sh NAME
40 .Nm open
41 .Nd open or create a file for reading or writing
42 .Sh LIBRARY
43 .Lb libc
44 .Sh SYNOPSIS
45 .In fcntl.h
46 .Ft int
47 .Fn open "const char *path" "int flags" "..."
48 .Sh DESCRIPTION
49 The file name specified by
50 .Fa path
51 is opened
52 for reading and/or writing as specified by the
53 argument
54 .Fa flags
55 and the file descriptor returned to the calling process.
56 The
57 .Fa flags
58 argument may indicate the file is to be
59 created if it does not exist (by specifying the
60 .Dv O_CREAT
61 flag).
62 In this case
63 .Nm
64 requires a third argument
65 .Fa "mode_t mode" ,
66 and the file is created with mode
67 .Fa mode
68 as described in
69 .Xr chmod 2
70 and modified by the process' umask value (see
71 .Xr umask 2 ) .
72 .Pp
73 The flags specified are formed by
74 .Em or Ns 'ing
75 the following values
76 .Pp
77 .Bd -literal -offset indent -compact
78 O_RDONLY        open for reading only
79 O_WRONLY        open for writing only
80 O_RDWR          open for reading and writing
81 O_NONBLOCK      do not block on open
82 O_APPEND        append on each write
83 O_CREAT         create file if it does not exist
84 O_TRUNC         truncate size to 0
85 O_EXCL          error if create and file exists
86 O_SHLOCK        atomically obtain a shared lock
87 O_EXLOCK        atomically obtain an exclusive lock
88 O_DIRECT        eliminate or reduce cache effects
89 O_FSYNC         synchronous writes
90 O_NOFOLLOW      do not follow symlinks
91 .Ed
92 .Pp
93 Opening a file with
94 .Dv O_APPEND
95 set causes each write on the file
96 to be appended to the end.
97 If
98 .Dv O_TRUNC
99 is specified and the
100 file exists, the file is truncated to zero length.
101 If
102 .Dv O_EXCL
103 is set with
104 .Dv O_CREAT
105 and the file already
106 exists,
107 .Fn open
108 returns an error.
109 This may be used to
110 implement a simple exclusive access locking mechanism.
111 If
112 .Dv O_EXCL
113 is set and the last component of the pathname is
114 a symbolic link,
115 .Fn open
116 will fail even if the symbolic
117 link points to a non-existent name.
118 If the
119 .Dv O_NONBLOCK
120 flag is specified and the
121 .Fn open
122 call would result
123 in the process being blocked for some reason (e.g., waiting for
124 carrier on a dialup line),
125 .Fn open
126 returns immediately.
127 The first time the process attempts to perform I/O on the open
128 file it will block (not currently implemented).
129 .Pp
130 If
131 .Dv O_FSYNC
132 is used in the mask, all writes will
133 immediately be written to disk,
134 the kernel will not cache written data
135 and all writes on the descriptor will not return until
136 the data to be written completes.
137 .Pp
138 If
139 .Dv O_NOFOLLOW
140 is used in the mask and the target file passed to
141 .Fn open
142 is a symbolic link then the
143 .Fn open
144 will fail.
145 .Pp
146 When opening a file, a lock with
147 .Xr flock 2
148 semantics can be obtained by setting
149 .Dv O_SHLOCK
150 for a shared lock, or
151 .Dv O_EXLOCK
152 for an exclusive lock.
153 If creating a file with
154 .Dv O_CREAT ,
155 the request for the lock will never fail
156 (provided that the underlying filesystem supports locking).
157 .Pp
158 .Dv O_DIRECT
159 may be used to minimize or eliminate the cache effects of reading and writing.
160 The system will attempt to avoid caching the data you read or write.
161 If it cannot avoid caching the data,
162 it will minimize the impact the data has on the cache.
163 Use of this flag can drastically reduce performance if not used with care.
164 .Pp
165 If successful,
166 .Fn open
167 returns a non-negative integer, termed a file descriptor.
168 It returns -1 on failure.
169 The file pointer used to mark the current position within the
170 file is set to the beginning of the file.
171 .Pp
172 When a new file is created it is given the group of the directory
173 which contains it.
174 .Pp
175 The new descriptor is set to remain open across
176 .Xr execve 2
177 system calls; see
178 .Xr close 2
179 and
180 .Xr fcntl 2 .
181 .Pp
182 The system imposes a limit on the number of file descriptors
183 open simultaneously by one process.
184 .Xr Getdtablesize 2
185 returns the current system limit.
186 .Sh RETURN VALUES
187 If successful,
188 .Fn open
189 returns a non-negative integer, termed a file descriptor.
190 It returns -1 on failure, and sets
191 .Va errno
192 to indicate the error.
193 .Sh ERRORS
194 The named file is opened unless:
195 .Bl -tag -width Er
196 .It Bq Er ENOTDIR
197 A component of the path prefix is not a directory.
198 .It Bq Er ENAMETOOLONG
199 A component of a pathname exceeded 255 characters,
200 or an entire path name exceeded 1023 characters.
201 .It Bq Er ENOENT
202 .Dv O_CREAT
203 is not set and the named file does not exist.
204 .It Bq Er ENOENT
205 A component of the path name that must exist does not exist.
206 .It Bq Er EACCES
207 Search permission is denied for a component of the path prefix.
208 .It Bq Er EACCES
209 The required permissions (for reading and/or writing)
210 are denied for the given flags.
211 .It Bq Er EACCES
212 .Dv O_CREAT
213 is specified,
214 the file does not exist,
215 and the directory in which it is to be created
216 does not permit writing.
217 .It Bq Er ELOOP
218 Too many symbolic links were encountered in translating the pathname.
219 .It Bq Er EISDIR
220 The named file is a directory, and the arguments specify
221 it is to be opened for writing.
222 .It Bq Er EROFS
223 The named file resides on a read-only file system,
224 and the file is to be modified.
225 .It Bq Er EMFILE
226 The process has already reached its limit for open file descriptors.
227 .It Bq Er ENFILE
228 The system file table is full.
229 .It Bq Er EMLINK
230 .Dv O_NOFOLLOW
231 was specified and the target is a symbolic link.
232 .It Bq Er ENXIO
233 The named file is a character special or block
234 special file, and the device associated with this special file
235 does not exist.
236 .It Bq Er ENXIO
237 The named file is a fifo, no process has
238 it open for reading, and the arguments specify it is
239 to be opened for writing.
240 .It Bq Er EINTR
241 The
242 .Fn open
243 operation was interrupted by a signal.
244 .It Bq Er EOPNOTSUPP
245 .Dv O_SHLOCK
246 or
247 .Dv O_EXLOCK
248 is specified but the underlying filesystem does not support locking.
249 .It Bq Er EWOULDBLOCK
250 .Dv O_NONBLOCK
251 and one of
252 .Dv O_SHLOCK
253 or
254 .Dv O_EXLOCK
255 is specified and the file is locked.
256 .It Bq Er ENOSPC
257 .Dv O_CREAT
258 is specified,
259 the file does not exist,
260 and the directory in which the entry for the new file is being placed
261 cannot be extended because there is no space left on the file
262 system containing the directory.
263 .It Bq Er ENOSPC
264 .Dv O_CREAT
265 is specified,
266 the file does not exist,
267 and there are no free inodes on the file system on which the
268 file is being created.
269 .It Bq Er EDQUOT
270 .Dv O_CREAT
271 is specified,
272 the file does not exist,
273 and the directory in which the entry for the new file
274 is being placed cannot be extended because the
275 user's quota of disk blocks on the file system
276 containing the directory has been exhausted.
277 .It Bq Er EDQUOT
278 .Dv O_CREAT
279 is specified,
280 the file does not exist,
281 and the user's quota of inodes on the file system on
282 which the file is being created has been exhausted.
283 .It Bq Er EIO
284 An I/O error occurred while making the directory entry or
285 allocating the inode for
286 .Dv O_CREAT .
287 .It Bq Er ETXTBSY
288 The file is a pure procedure (shared text) file that is being
289 executed and the
290 .Fn open
291 call requests write access.
292 .It Bq Er EFAULT
293 .Fa Path
294 points outside the process's allocated address space.
295 .It Bq Er EEXIST
296 .Dv O_CREAT
297 and
298 .Dv O_EXCL
299 were specified and the file exists.
300 .It Bq Er EOPNOTSUPP
301 An attempt was made to open a socket (not currently implemented).
302 .It Bq Er EINVAL
303 An attempt was made to open a descriptor with an illegal combination
304 of
305 .Dv O_RDONLY ,
306 .Dv O_WRONLY ,
307 and
308 .Dv O_RDWR .
309 .El
310 .Sh SEE ALSO
311 .Xr chmod 2 ,
312 .Xr close 2 ,
313 .Xr dup 2 ,
314 .Xr getdtablesize 2 ,
315 .Xr lseek 2 ,
316 .Xr read 2 ,
317 .Xr umask 2 ,
318 .Xr write 2
319 .Sh HISTORY
320 An
321 .Fn open
322 function call appeared in
323 .At v6 .