]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - lib/libc/sys/open.2
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.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 .\" 4. Neither the name of the University nor the names of its contributors
13 .\"    may be used to endorse or promote products derived from this software
14 .\"    without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     @(#)open.2      8.2 (Berkeley) 11/16/93
29 .\" $FreeBSD$
30 .\"
31 .Dd February 7, 2013
32 .Dt OPEN 2
33 .Os
34 .Sh NAME
35 .Nm open , openat
36 .Nd open or create a file for reading, writing or executing
37 .Sh LIBRARY
38 .Lb libc
39 .Sh SYNOPSIS
40 .In fcntl.h
41 .Ft int
42 .Fn open "const char *path" "int flags" "..."
43 .Ft int
44 .Fn openat "int fd" "const char *path" "int flags" "..."
45 .Sh DESCRIPTION
46 The file name specified by
47 .Fa path
48 is opened
49 for either execution or reading and/or writing as specified by the
50 argument
51 .Fa flags
52 and the file descriptor returned to the calling process.
53 The
54 .Fa flags
55 argument may indicate the file is to be
56 created if it does not exist (by specifying the
57 .Dv O_CREAT
58 flag).
59 In this case
60 .Fn open
61 and
62 .Fn openat
63 require an additional argument
64 .Fa "mode_t mode" ,
65 and the file is created with mode
66 .Fa mode
67 as described in
68 .Xr chmod 2
69 and modified by the process' umask value (see
70 .Xr umask 2 ) .
71 .Pp
72 The
73 .Fn openat
74 function is equivalent to the
75 .Fn open
76 function except in the case where the
77 .Fa path
78 specifies a relative path.
79 In this case the file to be opened is determined relative to the directory
80 associated with the file descriptor
81 .Fa fd
82 instead of the current working directory.
83 The
84 .Fa flag
85 parameter and the optional fourth parameter correspond exactly to
86 the parameters of
87 .Fn open .
88 If
89 .Fn openat
90 is passed the special value
91 .Dv AT_FDCWD
92 in the
93 .Fa fd
94 parameter, the current working directory is used
95 and the behavior is identical to a call to
96 .Fn open .
97 .Pp
98 The flags specified are formed by
99 .Em or Ns 'ing
100 the following values
101 .Pp
102 .Bd -literal -offset indent -compact
103 O_RDONLY        open for reading only
104 O_WRONLY        open for writing only
105 O_RDWR          open for reading and writing
106 O_EXEC          open for execute only
107 O_NONBLOCK      do not block on open
108 O_APPEND        append on each write
109 O_CREAT         create file if it does not exist
110 O_TRUNC         truncate size to 0
111 O_EXCL          error if create and file exists
112 O_SHLOCK        atomically obtain a shared lock
113 O_EXLOCK        atomically obtain an exclusive lock
114 O_DIRECT        eliminate or reduce cache effects
115 O_FSYNC         synchronous writes
116 O_SYNC          synchronous writes
117 O_NOFOLLOW      do not follow symlinks
118 O_NOCTTY        don't assign controlling terminal
119 O_TTY_INIT      restore default terminal attributes
120 O_DIRECTORY     error if file is not a directory
121 O_CLOEXEC       set FD_CLOEXEC upon open
122 .Ed
123 .Pp
124 Opening a file with
125 .Dv O_APPEND
126 set causes each write on the file
127 to be appended to the end.
128 If
129 .Dv O_TRUNC
130 is specified and the
131 file exists, the file is truncated to zero length.
132 If
133 .Dv O_EXCL
134 is set with
135 .Dv O_CREAT
136 and the file already
137 exists,
138 .Fn open
139 returns an error.
140 This may be used to
141 implement a simple exclusive access locking mechanism.
142 If
143 .Dv O_EXCL
144 is set and the last component of the pathname is
145 a symbolic link,
146 .Fn open
147 will fail even if the symbolic
148 link points to a non-existent name.
149 If the
150 .Dv O_NONBLOCK
151 flag is specified and the
152 .Fn open
153 system call would result
154 in the process being blocked for some reason (e.g., waiting for
155 carrier on a dialup line),
156 .Fn open
157 returns immediately.
158 The descriptor remains in non-blocking mode for subsequent operations.
159 .Pp
160 If
161 .Dv O_FSYNC
162 is used in the mask, all writes will
163 immediately be written to disk,
164 the kernel will not cache written data
165 and all writes on the descriptor will not return until
166 the data to be written completes.
167 .Pp
168 .Dv O_SYNC
169 is a synonym for
170 .Dv O_FSYNC
171 required by
172 .Tn POSIX .
173 .Pp
174 If
175 .Dv O_NOFOLLOW
176 is used in the mask and the target file passed to
177 .Fn open
178 is a symbolic link then the
179 .Fn open
180 will fail.
181 .Pp
182 When opening a file, a lock with
183 .Xr flock 2
184 semantics can be obtained by setting
185 .Dv O_SHLOCK
186 for a shared lock, or
187 .Dv O_EXLOCK
188 for an exclusive lock.
189 If creating a file with
190 .Dv O_CREAT ,
191 the request for the lock will never fail
192 (provided that the underlying file system supports locking).
193 .Pp
194 .Dv O_DIRECT
195 may be used to minimize or eliminate the cache effects of reading and writing.
196 The system will attempt to avoid caching the data you read or write.
197 If it cannot avoid caching the data,
198 it will minimize the impact the data has on the cache.
199 Use of this flag can drastically reduce performance if not used with care.
200 .Pp
201 .Dv O_NOCTTY
202 may be used to ensure the OS does not assign this file as the
203 controlling terminal when it opens a tty device.
204 This is the default on
205 .Fx ,
206 but is present for
207 .Tn POSIX
208 compatibility.
209 The
210 .Fn open
211 system call will not assign controlling terminals on
212 .Fx .
213 .Pp
214 .Dv O_TTY_INIT
215 may be used to ensure the OS restores the terminal attributes when
216 initially opening a TTY.
217 This is the default on
218 .Fx ,
219 but is present for
220 .Tn POSIX
221 compatibility.
222 The initial call to
223 .Fn open
224 on a TTY will always restore default terminal attributes on
225 .Fx .
226 .Pp
227 .Dv O_DIRECTORY
228 may be used to ensure the resulting file descriptor refers to a
229 directory.
230 This flag can be used to prevent applications with elevated privileges
231 from opening files which are even unsafe to open with
232 .Dv O_RDONLY ,
233 such as device nodes.
234 .Pp
235 .Dv O_CLOEXEC
236 may be used to set
237 .Dv FD_CLOEXEC
238 flag for the newly returned file descriptor.
239 .Pp
240 If successful,
241 .Fn open
242 returns a non-negative integer, termed a file descriptor.
243 It returns \-1 on failure.
244 The file pointer used to mark the current position within the
245 file is set to the beginning of the file.
246 .Pp
247 If a sleeping open of a device node from
248 .Xr devfs 5
249 is interrupted by a signal, the call always fails with
250 .Er EINTR ,
251 even if the
252 .Dv SA_RESTART
253 flag is set for the signal.
254 A sleeping open of a fifo (see
255 .Xr mkfifo 2 )
256 is restarted as normal.
257 .Pp
258 When a new file is created it is given the group of the directory
259 which contains it.
260 .Pp
261 Unless
262 .Dv O_CLOEXEC
263 flag was specified,
264 the new descriptor is set to remain open across
265 .Xr execve 2
266 system calls; see
267 .Xr close 2 ,
268 .Xr fcntl 2
269 and
270 .Dv O_CLOEXEC
271 description.
272 .Pp
273 The system imposes a limit on the number of file descriptors
274 open simultaneously by one process.
275 The
276 .Xr getdtablesize 2
277 system call returns the current system limit.
278 .Sh RETURN VALUES
279 If successful,
280 .Fn open
281 and
282 .Fn openat
283 return a non-negative integer, termed a file descriptor.
284 They return \-1 on failure, and set
285 .Va errno
286 to indicate the error.
287 .Sh ERRORS
288 The named file is opened unless:
289 .Bl -tag -width Er
290 .It Bq Er ENOTDIR
291 A component of the path prefix is not a directory.
292 .It Bq Er ENAMETOOLONG
293 A component of a pathname exceeded 255 characters,
294 or an entire path name exceeded 1023 characters.
295 .It Bq Er ENOENT
296 .Dv O_CREAT
297 is not set and the named file does not exist.
298 .It Bq Er ENOENT
299 A component of the path name that must exist does not exist.
300 .It Bq Er EACCES
301 Search permission is denied for a component of the path prefix.
302 .It Bq Er EACCES
303 The required permissions (for reading and/or writing)
304 are denied for the given flags.
305 .It Bq Er EACCES
306 .Dv O_TRUNC
307 is specified and write permission is denied.
308 .It Bq Er EACCES
309 .Dv O_CREAT
310 is specified,
311 the file does not exist,
312 and the directory in which it is to be created
313 does not permit writing.
314 .It Bq Er EPERM
315 .Dv O_CREAT
316 is specified, the file does not exist, and the directory in which it is to be
317 created has its immutable flag set, see the
318 .Xr chflags 2
319 manual page for more information.
320 .It Bq Er EPERM
321 The named file has its immutable flag set and the file is to be modified.
322 .It Bq Er EPERM
323 The named file has its append-only flag set, the file is to be modified, and
324 .Dv O_TRUNC
325 is specified or
326 .Dv O_APPEND
327 is not specified.
328 .It Bq Er ELOOP
329 Too many symbolic links were encountered in translating the pathname.
330 .It Bq Er EISDIR
331 The named file is a directory, and the arguments specify
332 it is to be modified.
333 .It Bq Er EROFS
334 The named file resides on a read-only file system,
335 and the file is to be modified.
336 .It Bq Er EROFS
337 .Dv O_CREAT
338 is specified and the named file would reside on a read-only file system.
339 .It Bq Er EMFILE
340 The process has already reached its limit for open file descriptors.
341 .It Bq Er ENFILE
342 The system file table is full.
343 .It Bq Er EMLINK
344 .Dv O_NOFOLLOW
345 was specified and the target is a symbolic link.
346 .It Bq Er ENXIO
347 The named file is a character special or block
348 special file, and the device associated with this special file
349 does not exist.
350 .It Bq Er ENXIO
351 .Dv O_NONBLOCK
352 is set, the named file is a fifo,
353 .Dv O_WRONLY
354 is set, and no process has the file open for reading.
355 .It Bq Er EINTR
356 The
357 .Fn open
358 operation was interrupted by a signal.
359 .It Bq Er EOPNOTSUPP
360 .Dv O_SHLOCK
361 or
362 .Dv O_EXLOCK
363 is specified but the underlying file system does not support locking.
364 .It Bq Er EOPNOTSUPP
365 The named file is a special file mounted through a file system that
366 does not support access to it (e.g.\& NFS).
367 .It Bq Er EWOULDBLOCK
368 .Dv O_NONBLOCK
369 and one of
370 .Dv O_SHLOCK
371 or
372 .Dv O_EXLOCK
373 is specified and the file is locked.
374 .It Bq Er ENOSPC
375 .Dv O_CREAT
376 is specified,
377 the file does not exist,
378 and the directory in which the entry for the new file is being placed
379 cannot be extended because there is no space left on the file
380 system containing the directory.
381 .It Bq Er ENOSPC
382 .Dv O_CREAT
383 is specified,
384 the file does not exist,
385 and there are no free inodes on the file system on which the
386 file is being created.
387 .It Bq Er EDQUOT
388 .Dv O_CREAT
389 is specified,
390 the file does not exist,
391 and the directory in which the entry for the new file
392 is being placed cannot be extended because the
393 user's quota of disk blocks on the file system
394 containing the directory has been exhausted.
395 .It Bq Er EDQUOT
396 .Dv O_CREAT
397 is specified,
398 the file does not exist,
399 and the user's quota of inodes on the file system on
400 which the file is being created has been exhausted.
401 .It Bq Er EIO
402 An I/O error occurred while making the directory entry or
403 allocating the inode for
404 .Dv O_CREAT .
405 .It Bq Er ETXTBSY
406 The file is a pure procedure (shared text) file that is being
407 executed and the
408 .Fn open
409 system call requests write access.
410 .It Bq Er EFAULT
411 The
412 .Fa path
413 argument
414 points outside the process's allocated address space.
415 .It Bq Er EEXIST
416 .Dv O_CREAT
417 and
418 .Dv O_EXCL
419 were specified and the file exists.
420 .It Bq Er EOPNOTSUPP
421 An attempt was made to open a socket (not currently implemented).
422 .It Bq Er EINVAL
423 An attempt was made to open a descriptor with an illegal combination
424 of
425 .Dv O_RDONLY ,
426 .Dv O_WRONLY ,
427 .Dv O_RDWR
428 and
429 .Dv O_EXEC .
430 .It Bq Eq EBADF
431 The
432 .Fa path
433 argument does not specify an absolute path and the
434 .Fa fd
435 argument is
436 neither
437 .Dv AT_FDCWD
438 nor a valid file descriptor open for searching.
439 .It Bq Eq ENOTDIR
440 The
441 .Fa path
442 argument is not an absolute path and
443 .Fa fd
444 is neither
445 .Dv AT_FDCWD
446 nor a file descriptor associated with a directory.
447 .It Bq Eq ENOTDIR
448 .Dv O_DIRECTORY
449 is specified and the file is not a directory.
450 .El
451 .Sh SEE ALSO
452 .Xr chmod 2 ,
453 .Xr close 2 ,
454 .Xr dup 2 ,
455 .Xr fexecve 2 ,
456 .Xr fhopen 2 ,
457 .Xr getdtablesize 2 ,
458 .Xr getfh 2 ,
459 .Xr lgetfh 2 ,
460 .Xr lseek 2 ,
461 .Xr read 2 ,
462 .Xr umask 2 ,
463 .Xr write 2 ,
464 .Xr fopen 3
465 .Sh HISTORY
466 The
467 .Fn open
468 function appeared in
469 .At v6 .
470 The
471 .Fn openat
472 function was introduced in
473 .Fx 8.0 .
474 .Sh BUGS
475 The Open Group Extended API Set 2 specification requires that the test
476 for whether
477 .Fa fd
478 is searchable is based on whether
479 .Fa fd
480 is open for searching, not whether the underlying directory currently
481 permits searches.
482 The present implementation of the
483 .Fa openat
484 checks the current permissions of directory instead.