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