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