]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/sys/open.2
Implement O_BENEATH and AT_BENEATH.
[FreeBSD/FreeBSD.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. 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 October 20, 2018
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 In
99 .Xr capsicum 4
100 capability mode,
101 .Fn open
102 is not permitted.
103 The
104 .Fa path
105 argument to
106 .Fn openat
107 must be strictly relative to a file descriptor
108 .Fa fd ,
109 as defined in
110 .Pa sys/kern/vfs_lookup.c .
111 .Fa path
112 must not be an absolute path and must not contain ".." components.
113 Additionally, no symbolic link in
114 .Fa path
115 may contain ".." components either.
116 .Fa fd
117 must not be
118 .Dv AT_FDCWD .
119 .Pp
120 The flags specified are formed by
121 .Em or Ns 'ing
122 the following values
123 .Pp
124 .Bd -literal -offset indent -compact
125 O_RDONLY        open for reading only
126 O_WRONLY        open for writing only
127 O_RDWR          open for reading and writing
128 O_EXEC          open for execute only
129 O_NONBLOCK      do not block on open
130 O_APPEND        append on each write
131 O_CREAT         create file if it does not exist
132 O_TRUNC         truncate size to 0
133 O_EXCL          error if create and file exists
134 O_SHLOCK        atomically obtain a shared lock
135 O_EXLOCK        atomically obtain an exclusive lock
136 O_DIRECT        eliminate or reduce cache effects
137 O_FSYNC         synchronous writes
138 O_SYNC          synchronous writes
139 O_NOFOLLOW      do not follow symlinks
140 O_NOCTTY        ignored
141 O_TTY_INIT      ignored
142 O_DIRECTORY     error if file is not a directory
143 O_CLOEXEC       set FD_CLOEXEC upon open
144 O_VERIFY        verify the contents of the file
145 O_BENEATH       require path to be strictly relative to starting directory
146 .Ed
147 .Pp
148 Opening a file with
149 .Dv O_APPEND
150 set causes each write on the file
151 to be appended to the end.
152 If
153 .Dv O_TRUNC
154 is specified and the
155 file exists, the file is truncated to zero length.
156 If
157 .Dv O_EXCL
158 is set with
159 .Dv O_CREAT
160 and the file already
161 exists,
162 .Fn open
163 returns an error.
164 This may be used to
165 implement a simple exclusive access locking mechanism.
166 If
167 .Dv O_EXCL
168 is set and the last component of the pathname is
169 a symbolic link,
170 .Fn open
171 will fail even if the symbolic
172 link points to a non-existent name.
173 If the
174 .Dv O_NONBLOCK
175 flag is specified and the
176 .Fn open
177 system call would result
178 in the process being blocked for some reason (e.g., waiting for
179 carrier on a dialup line),
180 .Fn open
181 returns immediately.
182 The descriptor remains in non-blocking mode for subsequent operations.
183 .Pp
184 If
185 .Dv O_FSYNC
186 is used in the mask, all writes will
187 immediately be written to disk,
188 the kernel will not cache written data
189 and all writes on the descriptor will not return until
190 the data to be written completes.
191 .Pp
192 .Dv O_SYNC
193 is a synonym for
194 .Dv O_FSYNC
195 required by
196 .Tn POSIX .
197 .Pp
198 If
199 .Dv O_NOFOLLOW
200 is used in the mask and the target file passed to
201 .Fn open
202 is a symbolic link then the
203 .Fn open
204 will fail.
205 .Pp
206 When opening a file, a lock with
207 .Xr flock 2
208 semantics can be obtained by setting
209 .Dv O_SHLOCK
210 for a shared lock, or
211 .Dv O_EXLOCK
212 for an exclusive lock.
213 If creating a file with
214 .Dv O_CREAT ,
215 the request for the lock will never fail
216 (provided that the underlying file system supports locking).
217 .Pp
218 .Dv O_DIRECT
219 may be used to minimize or eliminate the cache effects of reading and writing.
220 The system will attempt to avoid caching the data you read or write.
221 If it cannot avoid caching the data,
222 it will minimize the impact the data has on the cache.
223 Use of this flag can drastically reduce performance if not used with care.
224 .Pp
225 .Dv O_NOCTTY
226 may be used to ensure the OS does not assign this file as the
227 controlling terminal when it opens a tty device.
228 This is the default on
229 .Fx ,
230 but is present for
231 .Tn POSIX
232 compatibility.
233 The
234 .Fn open
235 system call will not assign controlling terminals on
236 .Fx .
237 .Pp
238 .Dv O_TTY_INIT
239 may be used to ensure the OS restores the terminal attributes when
240 initially opening a TTY.
241 This is the default on
242 .Fx ,
243 but is present for
244 .Tn POSIX
245 compatibility.
246 The initial call to
247 .Fn open
248 on a TTY will always restore default terminal attributes on
249 .Fx .
250 .Pp
251 .Dv O_DIRECTORY
252 may be used to ensure the resulting file descriptor refers to a
253 directory.
254 This flag can be used to prevent applications with elevated privileges
255 from opening files which are even unsafe to open with
256 .Dv O_RDONLY ,
257 such as device nodes.
258 .Pp
259 .Dv O_CLOEXEC
260 may be used to set
261 .Dv FD_CLOEXEC
262 flag for the newly returned file descriptor.
263 .Pp
264 .Dv O_VERIFY
265 may be used to indicate to the kernel that the contents of the file should
266 be verified before allowing the open to proceed.
267 The details of what
268 .Dq verified
269 means is implementation specific.
270 The run-time linker (rtld) uses this flag to ensure shared objects have
271 been verified before operating on them.
272 .Pp
273 .Dv O_BENEATH
274 returns
275 .Er ENOTCAPABLE
276 if the specified path, after resolving all symlinks and ".." references
277 in it, does not reside in the directory hierarchy of children beneath
278 the starting directory, or is an absolute path.
279 Starting directory is the process current directory if relative
280 .Fa path
281 is used for
282 .Fn open ,
283 and the directory referenced by the
284 .Fa fd
285 argument when specifying relative
286 .Fa path
287 for
288 .Fn openat .
289 .Pp
290 If successful,
291 .Fn open
292 returns a non-negative integer, termed a file descriptor.
293 It returns \-1 on failure.
294 The file pointer used to mark the current position within the
295 file is set to the beginning of the file.
296 .Pp
297 If a sleeping open of a device node from
298 .Xr devfs 5
299 is interrupted by a signal, the call always fails with
300 .Er EINTR ,
301 even if the
302 .Dv SA_RESTART
303 flag is set for the signal.
304 A sleeping open of a fifo (see
305 .Xr mkfifo 2 )
306 is restarted as normal.
307 .Pp
308 When a new file is created it is given the group of the directory
309 which contains it.
310 .Pp
311 Unless
312 .Dv O_CLOEXEC
313 flag was specified,
314 the new descriptor is set to remain open across
315 .Xr execve 2
316 system calls; see
317 .Xr close 2 ,
318 .Xr fcntl 2
319 and
320 .Dv O_CLOEXEC
321 description.
322 .Pp
323 The system imposes a limit on the number of file descriptors
324 open simultaneously by one process.
325 The
326 .Xr getdtablesize 2
327 system call returns the current system limit.
328 .Sh RETURN VALUES
329 If successful,
330 .Fn open
331 and
332 .Fn openat
333 return a non-negative integer, termed a file descriptor.
334 They return \-1 on failure, and set
335 .Va errno
336 to indicate the error.
337 .Sh ERRORS
338 The named file is opened unless:
339 .Bl -tag -width Er
340 .It Bq Er ENOTDIR
341 A component of the path prefix is not a directory.
342 .It Bq Er ENAMETOOLONG
343 A component of a pathname exceeded 255 characters,
344 or an entire path name exceeded 1023 characters.
345 .It Bq Er ENOENT
346 .Dv O_CREAT
347 is not set and the named file does not exist.
348 .It Bq Er ENOENT
349 A component of the path name that must exist does not exist.
350 .It Bq Er EACCES
351 Search permission is denied for a component of the path prefix.
352 .It Bq Er EACCES
353 The required permissions (for reading and/or writing)
354 are denied for the given flags.
355 .It Bq Er EACCES
356 .Dv O_TRUNC
357 is specified and write permission is denied.
358 .It Bq Er EACCES
359 .Dv O_CREAT
360 is specified,
361 the file does not exist,
362 and the directory in which it is to be created
363 does not permit writing.
364 .It Bq Er EPERM
365 .Dv O_CREAT
366 is specified, the file does not exist, and the directory in which it is to be
367 created has its immutable flag set, see the
368 .Xr chflags 2
369 manual page for more information.
370 .It Bq Er EPERM
371 The named file has its immutable flag set and the file is to be modified.
372 .It Bq Er EPERM
373 The named file has its append-only flag set, the file is to be modified, and
374 .Dv O_TRUNC
375 is specified or
376 .Dv O_APPEND
377 is not specified.
378 .It Bq Er ELOOP
379 Too many symbolic links were encountered in translating the pathname.
380 .It Bq Er EISDIR
381 The named file is a directory, and the arguments specify
382 it is to be modified.
383 .It Bq Er EROFS
384 The named file resides on a read-only file system,
385 and the file is to be modified.
386 .It Bq Er EROFS
387 .Dv O_CREAT
388 is specified and the named file would reside on a read-only file system.
389 .It Bq Er EMFILE
390 The process has already reached its limit for open file descriptors.
391 .It Bq Er ENFILE
392 The system file table is full.
393 .It Bq Er EMLINK
394 .Dv O_NOFOLLOW
395 was specified and the target is a symbolic link.
396 .It Bq Er ENXIO
397 The named file is a character special or block
398 special file, and the device associated with this special file
399 does not exist.
400 .It Bq Er ENXIO
401 .Dv O_NONBLOCK
402 is set, the named file is a fifo,
403 .Dv O_WRONLY
404 is set, and no process has the file open for reading.
405 .It Bq Er EINTR
406 The
407 .Fn open
408 operation was interrupted by a signal.
409 .It Bq Er EOPNOTSUPP
410 .Dv O_SHLOCK
411 or
412 .Dv O_EXLOCK
413 is specified but the underlying file system does not support locking.
414 .It Bq Er EOPNOTSUPP
415 The named file is a special file mounted through a file system that
416 does not support access to it (e.g.\& NFS).
417 .It Bq Er EWOULDBLOCK
418 .Dv O_NONBLOCK
419 and one of
420 .Dv O_SHLOCK
421 or
422 .Dv O_EXLOCK
423 is specified and the file is locked.
424 .It Bq Er ENOSPC
425 .Dv O_CREAT
426 is specified,
427 the file does not exist,
428 and the directory in which the entry for the new file is being placed
429 cannot be extended because there is no space left on the file
430 system containing the directory.
431 .It Bq Er ENOSPC
432 .Dv O_CREAT
433 is specified,
434 the file does not exist,
435 and there are no free inodes on the file system on which the
436 file is being created.
437 .It Bq Er EDQUOT
438 .Dv O_CREAT
439 is specified,
440 the file does not exist,
441 and the directory in which the entry for the new file
442 is being placed cannot be extended because the
443 user's quota of disk blocks on the file system
444 containing the directory has been exhausted.
445 .It Bq Er EDQUOT
446 .Dv O_CREAT
447 is specified,
448 the file does not exist,
449 and the user's quota of inodes on the file system on
450 which the file is being created has been exhausted.
451 .It Bq Er EIO
452 An I/O error occurred while making the directory entry or
453 allocating the inode for
454 .Dv O_CREAT .
455 .It Bq Er ETXTBSY
456 The file is a pure procedure (shared text) file that is being
457 executed and the
458 .Fn open
459 system call requests write access.
460 .It Bq Er EFAULT
461 The
462 .Fa path
463 argument
464 points outside the process's allocated address space.
465 .It Bq Er EEXIST
466 .Dv O_CREAT
467 and
468 .Dv O_EXCL
469 were specified and the file exists.
470 .It Bq Er EOPNOTSUPP
471 An attempt was made to open a socket (not currently implemented).
472 .It Bq Er EINVAL
473 An attempt was made to open a descriptor with an illegal combination
474 of
475 .Dv O_RDONLY ,
476 .Dv O_WRONLY ,
477 .Dv O_RDWR
478 and
479 .Dv O_EXEC .
480 .It Bq Er EBADF
481 The
482 .Fa path
483 argument does not specify an absolute path and the
484 .Fa fd
485 argument is
486 neither
487 .Dv AT_FDCWD
488 nor a valid file descriptor open for searching.
489 .It Bq Er ENOTDIR
490 The
491 .Fa path
492 argument is not an absolute path and
493 .Fa fd
494 is neither
495 .Dv AT_FDCWD
496 nor a file descriptor associated with a directory.
497 .It Bq Er ENOTDIR
498 .Dv O_DIRECTORY
499 is specified and the file is not a directory.
500 .It Bq Er ECAPMODE
501 .Dv AT_FDCWD
502 is specified and the process is in capability mode.
503 .It Bq Er ECAPMODE
504 .Fn open
505 was called and the process is in capability mode.
506 .It Bq Er ENOTCAPABLE
507 .Fa path
508 is an absolute path,
509 or contained a ".." component leading to a
510 directory outside of the directory hierarchy specified by
511 .Fa fd ,
512 and the process is in capability mode or the
513 .Dv O_BENEATH
514 flag was provided.
515 .El
516 .Sh SEE ALSO
517 .Xr chmod 2 ,
518 .Xr close 2 ,
519 .Xr dup 2 ,
520 .Xr fexecve 2 ,
521 .Xr fhopen 2 ,
522 .Xr getdtablesize 2 ,
523 .Xr getfh 2 ,
524 .Xr lgetfh 2 ,
525 .Xr lseek 2 ,
526 .Xr read 2 ,
527 .Xr umask 2 ,
528 .Xr write 2 ,
529 .Xr fopen 3 ,
530 .Xr capsicum 4
531 .Sh STANDARDS
532 These functions are specified by
533 .St -p1003.1-2008 .
534 .Fx
535 sets
536 .Va errno
537 to
538 .Er EMLINK instead of
539 .Er ELOOP
540 as specified by
541 .Tn POSIX
542 when
543 .Dv O_NOFOLLOW
544 is set in flags and the final component of pathname is a symbolic link
545 to distinguish it from the case of too many symbolic link traversals
546 in one of its non-final components.
547 .Sh HISTORY
548 The
549 .Fn open
550 function appeared in
551 .At v1 .
552 The
553 .Fn openat
554 function was introduced in
555 .Fx 8.0 .
556 .Sh BUGS
557 The Open Group Extended API Set 2 specification requires that the test
558 for whether
559 .Fa fd
560 is searchable is based on whether
561 .Fa fd
562 is open for searching, not whether the underlying directory currently
563 permits searches.
564 The present implementation of the
565 .Fa openat
566 checks the current permissions of directory instead.