kdump(1)/truss(1): Fix occasional quickworld breakage.
[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.3 2005/07/29 23:16:04 hsu Exp $
35 .\"
36 .Dd July 31, 2012
37 .Dt OPEN 2
38 .Os
39 .Sh NAME
40 .Nm open , openat
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 .Ft int
49 .Fn openat "int fd" "const char *path" "int flags" "..."
50 .Sh DESCRIPTION
51 The file name specified by
52 .Fa path
53 is opened
54 for reading and/or writing as specified by the
55 argument
56 .Fa flags
57 and the lowest unused file descriptor in the process' file descriptor table
58 is returned.
59 The
60 .Fa flags
61 argument may indicate the file is to be
62 created if it does not exist (by specifying the
63 .Dv O_CREAT
64 flag).
65 In this case
66 .Fn open
67 and
68 .Fn openat
69 require a third argument
70 .Fa "mode_t mode" ,
71 and the file is created with mode
72 .Fa mode
73 as described in
74 .Xr chmod 2
75 and modified by the process' umask value (see
76 .Xr umask 2 ) .
77 .Pp
78 The
79 .Fn openat
80 function is equivalent to the
81 .Fn open
82 function except in the case where the
83 .Fa path
84 specifies a relative path.
85 In this case the file to be opened is determined relative to the directory
86 associated with the file descriptor
87 .Fa fd
88 instead of the current working directory.
89 The
90 .Fa flag
91 parameter and the optional fourth parameter correspond exactly to
92 the parameters of
93 .Fn open .
94 If
95 .Fn openat
96 is passed the special value
97 .Dv AT_FDCWD
98 in the
99 .Fa fd
100 parameter, the current working directory is used
101 and the behavior is identical to a call to
102 .Fn open .
103 .Pp
104 The flags specified are formed by
105 .Em or Ns 'ing
106 the following values
107 .Pp
108 .Bd -literal -offset indent -compact
109 O_RDONLY        open for reading only
110 O_WRONLY        open for writing only
111 O_RDWR          open for reading and writing
112 O_NONBLOCK      do not block on open
113 O_APPEND        append on each write
114 O_CREAT         create file if it does not exist
115 O_TRUNC         truncate size to 0
116 O_EXCL          error if create and file exists
117 O_SHLOCK        atomically obtain a shared lock
118 O_EXLOCK        atomically obtain an exclusive lock
119 O_DIRECT        eliminate or reduce cache effects
120 O_FSYNC         synchronous writes
121 O_NOFOLLOW      do not follow symlinks
122 O_DIRECTORY     error if file is not a directory
123 .Ed
124 .Pp
125 Opening a file with
126 .Dv O_APPEND
127 set causes each write on the file
128 to be appended to the end.
129 If
130 .Dv O_TRUNC
131 is specified and the
132 file exists, the file is truncated to zero length.
133 If
134 .Dv O_EXCL
135 is set with
136 .Dv O_CREAT
137 and the file already
138 exists,
139 .Fn open
140 returns an error.
141 This may be used to
142 implement a simple exclusive access locking mechanism.
143 If
144 .Dv O_EXCL
145 is set and the last component of the pathname is
146 a symbolic link,
147 .Fn open
148 will fail even if the symbolic
149 link points to a non-existent name.
150 If the
151 .Dv O_NONBLOCK
152 flag is specified and the
153 .Fn open
154 call would result
155 in the process being blocked for some reason (e.g., waiting for
156 carrier on a dialup line),
157 .Fn open
158 returns immediately.
159 The first time the process attempts to perform I/O on the open
160 file it will block (not currently implemented).
161 .Pp
162 If
163 .Dv O_FSYNC
164 is used in the mask, all writes will
165 immediately be written to disk,
166 the kernel will not cache written data
167 and all writes on the descriptor will not return until
168 the data to be written completes.
169 .Pp
170 If
171 .Dv O_NOFOLLOW
172 is used in the mask and the target file passed to
173 .Fn open
174 is a symbolic link then the
175 .Fn open
176 will fail.
177 .Pp
178 When opening a file, a lock with
179 .Xr flock 2
180 semantics can be obtained by setting
181 .Dv O_SHLOCK
182 for a shared lock, or
183 .Dv O_EXLOCK
184 for an exclusive lock.
185 If creating a file with
186 .Dv O_CREAT ,
187 the request for the lock will never fail
188 (provided that the underlying filesystem supports locking).
189 .Pp
190 .Dv O_DIRECT
191 may be used to minimize or eliminate the cache effects of reading and writing.
192 The system will attempt to avoid caching the data you read or write.
193 If it cannot avoid caching the data,
194 it will minimize the impact the data has on the cache.
195 Use of this flag can drastically reduce performance if not used with care.
196 .Pp
197 .Dv O_DIRECTORY
198 may be used to ensure the resulting file descriptor refers to a directory.
199 This flag can be used to prevent applications with elevated privileges
200 from opening files which are even unsafe to open with
201 .Dv O_RDONLY ,
202 such as device nodes.
203 .Pp
204 If successful,
205 .Fn open
206 and
207 .Fn openat
208 return a non-negative integer, termed a file descriptor.
209 It returns -1 on failure.
210 The file pointer used to mark the current position within the
211 file is set to the beginning of the file.
212 .Pp
213 When a new file is created it is given the group of the directory
214 which contains it.
215 .Pp
216 The new descriptor is set to remain open across
217 .Xr execve 2
218 system calls; see
219 .Xr close 2
220 and
221 .Xr fcntl 2 .
222 .Pp
223 The system imposes a limit on the number of file descriptors
224 open simultaneously by one process.
225 .Xr Getdtablesize 2
226 returns the current system limit.
227 .Sh RETURN VALUES
228 If successful,
229 .Fn open
230 and
231 .Fn openat
232 return a non-negative integer, termed a file descriptor.
233 They return -1 on failure, and set
234 .Va errno
235 to indicate the error.
236 .Sh ERRORS
237 The named file is opened unless:
238 .Bl -tag -width Er
239 .It Bq Er ENOTDIR
240 A component of the path prefix is not a directory or the
241 .Fa path
242 argument is not an absolute path and the
243 .Fa fd
244 argument is neither
245 .Dv AT_FDCWD
246 nor a file descriptor associated with a directory or
247 .Dv O_DIRECTORY
248 is specified and the file is not a directory.
249 .It Bq Er ENAMETOOLONG
250 A component of a pathname exceeded 255 characters,
251 or an entire path name exceeded 1023 characters.
252 .It Bq Er ENOENT
253 .Dv O_CREAT
254 is not set and the named file does not exist.
255 .It Bq Er ENOENT
256 A component of the path name that must exist does not exist.
257 .It Bq Er EACCES
258 Search permission is denied for a component of the path prefix.
259 .It Bq Er EACCES
260 The required permissions (for reading and/or writing)
261 are denied for the given flags.
262 .It Bq Er EACCES
263 .Dv O_CREAT
264 is specified,
265 the file does not exist,
266 and the directory in which it is to be created
267 does not permit writing.
268 .It Bq Er ELOOP
269 Too many symbolic links were encountered in translating the pathname.
270 .It Bq Er EISDIR
271 The named file is a directory, and the arguments specify
272 it is to be opened for writing.
273 .It Bq Er EROFS
274 The named file resides on a read-only file system,
275 and the file is to be modified.
276 .It Bq Er EMFILE
277 The process has already reached its limit for open file descriptors.
278 .It Bq Er ENFILE
279 The system file table is full.
280 .It Bq Er EMLINK
281 .Dv O_NOFOLLOW
282 was specified and the target is a symbolic link.
283 .It Bq Er ENXIO
284 The named file is a character special or block
285 special file, and the device associated with this special file
286 does not exist.
287 .It Bq Er ENXIO
288 The named file is a fifo, no process has
289 it open for reading, and the arguments specify it is
290 to be opened for writing.
291 .It Bq Er EINTR
292 The
293 .Fn open
294 operation was interrupted by a signal.
295 .It Bq Er EOPNOTSUPP
296 .Dv O_SHLOCK
297 or
298 .Dv O_EXLOCK
299 is specified but the underlying filesystem does not support locking.
300 .It Bq Er EWOULDBLOCK
301 .Dv O_NONBLOCK
302 and one of
303 .Dv O_SHLOCK
304 or
305 .Dv O_EXLOCK
306 is specified and the file is locked.
307 .It Bq Er ENOSPC
308 .Dv O_CREAT
309 is specified,
310 the file does not exist,
311 and the directory in which the entry for the new file is being placed
312 cannot be extended because there is no space left on the file
313 system containing the directory.
314 .It Bq Er ENOSPC
315 .Dv O_CREAT
316 is specified,
317 the file does not exist,
318 and there are no free inodes on the file system on which the
319 file is being created.
320 .It Bq Er EDQUOT
321 .Dv O_CREAT
322 is specified,
323 the file does not exist,
324 and the directory in which the entry for the new file
325 is being placed cannot be extended because the
326 user's quota of disk blocks on the file system
327 containing the directory has been exhausted.
328 .It Bq Er EDQUOT
329 .Dv O_CREAT
330 is specified,
331 the file does not exist,
332 and the user's quota of inodes on the file system on
333 which the file is being created has been exhausted.
334 .It Bq Er EIO
335 An I/O error occurred while making the directory entry or
336 allocating the inode for
337 .Dv O_CREAT .
338 .It Bq Er ETXTBSY
339 The file is a pure procedure (shared text) file that is being
340 executed and the
341 .Fn open
342 call requests write access.
343 .It Bq Er EFAULT
344 .Fa Path
345 points outside the process's allocated address space.
346 .It Bq Er EEXIST
347 .Dv O_CREAT
348 and
349 .Dv O_EXCL
350 were specified and the file exists.
351 .It Bq Er EOPNOTSUPP
352 An attempt was made to open a socket (not currently implemented).
353 .It Bq Er EINVAL
354 An attempt was made to open a descriptor with an illegal combination
355 of
356 .Dv O_RDONLY ,
357 .Dv O_WRONLY ,
358 and
359 .Dv O_RDWR .
360 .El
361 .Sh SEE ALSO
362 .Xr chmod 2 ,
363 .Xr close 2 ,
364 .Xr dup 2 ,
365 .Xr getdtablesize 2 ,
366 .Xr lseek 2 ,
367 .Xr read 2 ,
368 .Xr umask 2 ,
369 .Xr write 2
370 .Sh HISTORY
371 An
372 .Fn open
373 function call appeared in
374 .At v6 .
375 An
376 .Fn openat
377 function call appeared first in Solaris and was ported to
378 .Dx 2.3 .
379 .Sh BUGS
380 The Open Group Extended API Set 2 specification requires that the test
381 for
382 .Fa fd Ap s
383 searchability is based on whether it is open for searching,
384 and not whether the underlying directory currently permits searches.
385 The present implementation of
386 .Fn openat
387 checks the current permissions of directory instead.