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