1 .\" Copyright (c) 1980, 1991, 1993
2 .\" The Regents of the University of California. All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
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.
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
28 .\" @(#)open.2 8.2 (Berkeley) 11/16/93
35 .Nd open or create a file for reading, writing or executing
41 .Fn open "const char *path" "int flags" "..."
43 .Fn openat "int fd" "const char *path" "int flags" "..."
45 The file name specified by
48 for either execution or reading and/or writing as specified by the
51 and the file descriptor returned to the calling process.
54 argument may indicate the file is to be
55 created if it does not exist (by specifying the
62 require an additional argument
64 and the file is created with mode
68 and modified by the process' umask value (see
73 function is equivalent to the
75 function except in the case where the
77 specifies a relative path.
82 the file to be opened is determined relative to the directory
83 associated with the file descriptor
85 instead of the current working directory.
88 parameter and the optional fourth parameter correspond exactly to
93 is passed the special value
97 parameter, the current working directory is used
98 and the behavior is identical to a call to
103 is called with an absolute
118 must be strictly relative to a file descriptor
121 must not be an absolute path and must not contain ".." components
122 which cause the path resolution to escape the directory hierarchy
125 Additionally, no symbolic link in
127 may target absolute path or contain escaping ".." components.
133 .Dv vfs.lookup_cap_dotdot
135 MIB is set to zero, ".." components in the paths,
136 used in capability mode,
137 are completely disabled.
139 .Dv vfs.lookup_cap_dotdot_nonlocal
140 MIB is set to zero, ".." is not allowed if found on non-local filesystem.
142 The flags specified are formed by
146 .Bd -literal -offset indent -compact
147 O_RDONLY open for reading only
148 O_WRONLY open for writing only
149 O_RDWR open for reading and writing
150 O_EXEC open for execute only
151 O_SEARCH open for search only, an alias for O_EXEC
152 O_NONBLOCK do not block on open
153 O_APPEND append on each write
154 O_CREAT create file if it does not exist
155 O_TRUNC truncate size to 0
156 O_EXCL error if create and file exists
157 O_SHLOCK atomically obtain a shared lock
158 O_EXLOCK atomically obtain an exclusive lock
159 O_DIRECT eliminate or reduce cache effects
160 O_FSYNC synchronous writes (historical synonym for O_SYNC)
161 O_SYNC synchronous writes
162 O_DSYNC synchronous data writes
163 O_NOFOLLOW do not follow symlinks
166 O_DIRECTORY error if file is not a directory
167 O_CLOEXEC set FD_CLOEXEC upon open
168 O_VERIFY verify the contents of the file
169 O_RESOLVE_BENEATH path resolution must not cross the fd directory
170 O_PATH record only the target path in the opened descriptor
171 O_EMPTY_PATH openat, open file referenced by fd if path is empty
176 set causes each write on the file
177 to be appended to the end.
181 file exists, the file is truncated to zero length.
191 implement a simple exclusive access locking mechanism.
194 is set and the last component of the pathname is
197 will fail even if the symbolic
198 link points to a non-existent name.
201 flag is specified and the
203 system call would result
204 in the process being blocked for some reason (e.g., waiting for
205 carrier on a dialup line),
208 The descriptor remains in non-blocking mode for subsequent operations.
212 is used in the mask, all writes will
213 immediately and synchronously be written to disk.
215 is an historical synonym for
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.
226 is used in the mask and the target file passed to
228 is a symbolic link then the
232 When opening a file, a lock with
234 semantics can be obtained by setting
236 for a shared lock, or
238 for an exclusive lock.
239 If creating a file with
241 the request for the lock will never fail
242 (provided that the underlying file system supports locking).
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.
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
261 system call will not assign controlling terminals on
265 may be used to ensure the OS restores the terminal attributes when
266 initially opening a TTY.
267 This is the default on
274 on a TTY will always restore default terminal attributes on
278 may be used to ensure the resulting file descriptor refers to a
280 This flag can be used to prevent applications with elevated privileges
281 from opening files which are even unsafe to open with
283 such as device nodes.
288 flag for the newly returned file descriptor.
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.
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.
299 .Dv O_RESOLVE_BENEATH
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.
311 execute permissions are checked at open time.
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
321 returns a file descriptor that can be used as a directory file descriptor for
323 and other system calls taking a file descriptor argument, like
326 The other functionality of the returned file descriptor is limited to
327 the descriptor-level operations.
329 .Bl -tag -width readlinkat(2) -offset indent -compact
331 but advisory locking is not allowed
346 .It Xr __acl_get_fd 2 , Xr __acl_aclcheck_fd 2
351 and any other that operate on file and not on file descriptor (except
355 A file descriptor created with the
357 flag can be opened into normal (operable) file descriptor by
366 Such an open behaves as if the current path of the file referenced by
368 is passed, except that the path walk permissions are not checked.
369 See also the description of
373 and related syscalls.
377 returns a non-negative integer, termed a file descriptor.
378 It returns \-1 on failure.
379 The file pointer used to mark the current position within the
380 file is set to the beginning of the file.
382 If a sleeping open of a device node from
384 is interrupted by a signal, the call always fails with
388 flag is set for the signal.
389 A sleeping open of a fifo (see
391 is restarted as normal.
393 When a new file is created it is given the group of the directory
399 the new descriptor is set to remain open across
408 The system imposes a limit on the number of file descriptors
409 open simultaneously by one process.
412 system call returns the current system limit.
418 return a non-negative integer, termed a file descriptor.
419 They return \-1 on failure, and set
421 to indicate the error.
423 The named file is opened unless:
426 A component of the path prefix is not a directory.
427 .It Bq Er ENAMETOOLONG
428 A component of a pathname exceeded 255 characters,
429 or an entire path name exceeded 1023 characters.
432 is not set and the named file does not exist.
434 A component of the path name that must exist does not exist.
436 Search permission is denied for a component of the path prefix.
438 The required permissions (for reading and/or writing)
439 are denied for the given flags.
442 is specified and write permission is denied.
446 the file does not exist,
447 and the directory in which it is to be created
448 does not permit writing.
451 is specified, the file does not exist, and the directory in which it is to be
452 created has its immutable flag set, see the
454 manual page for more information.
456 The named file has its immutable flag set and the file is to be modified.
458 The named file has its append-only flag set, the file is to be modified, and
464 Too many symbolic links were encountered in translating the pathname.
466 The named file is a directory, and the arguments specify
467 it is to be modified.
469 The named file is a directory, and the flags specified
474 The named file resides on a read-only file system,
475 and the file is to be modified.
478 is specified and the named file would reside on a read-only file system.
480 The process has already reached its limit for open file descriptors.
482 The system file table is full.
485 was specified and the target is a symbolic link.
487 The named file is a character special or block
488 special file, and the device associated with this special file
492 is set, the named file is a fifo,
494 is set, and no process has the file open for reading.
498 operation was interrupted by a signal.
503 is specified but the underlying file system does not support locking.
505 The named file is a special file mounted through a file system that
506 does not support access to it (e.g.\& NFS).
507 .It Bq Er EWOULDBLOCK
513 is specified and the file is locked.
517 the file does not exist,
518 and the directory in which the entry for the new file is being placed
519 cannot be extended because there is no space left on the file
520 system containing the directory.
524 the file does not exist,
525 and there are no free inodes on the file system on which the
526 file is being created.
530 the file does not exist,
531 and the directory in which the entry for the new file
532 is being placed cannot be extended because the
533 user's quota of disk blocks on the file system
534 containing the directory has been exhausted.
538 the file does not exist,
539 and the user's quota of inodes on the file system on
540 which the file is being created has been exhausted.
542 An I/O error occurred while making the directory entry or
543 allocating the inode for
546 Corrupted data was detected while reading from the file system.
548 The file is a pure procedure (shared text) file that is being
551 system call requests write access.
556 points outside the process's allocated address space.
561 were specified and the file exists.
563 An attempt was made to open a socket (not currently implemented).
565 An attempt was made to open a descriptor with an illegal combination
578 argument does not specify an absolute path and the
583 nor a valid file descriptor open for searching.
587 argument is not an absolute path and
591 nor a file descriptor associated with a directory.
594 is specified and the file is not a directory.
597 is specified and the process is in capability mode.
600 was called and the process is in capability mode.
601 .It Bq Er ENOTCAPABLE
603 is an absolute path and the process is in capability mode.
604 .It Bq Er ENOTCAPABLE
606 is an absolute path and
607 .Dv O_RESOLVE_BENEATH
609 .It Bq Er ENOTCAPABLE
611 contains a ".." component leading to a directory outside
612 of the directory hierarchy specified by
614 and the process is in capability mode.
615 .It Bq Er ENOTCAPABLE
617 contains a ".." component leading to a directory outside
618 of the directory hierarchy specified by
621 .Dv O_RESOLVE_BENEATH
623 .It Bq Er ENOTCAPABLE
625 contains a ".." component, the
626 .Dv vfs.lookup_cap_dotdot
628 is set, and the process is in capability mode.
636 .Xr getdtablesize 2 ,
646 These functions are specified by
652 .Er EMLINK instead of
658 is set in flags and the final component of pathname is a symbolic link
659 to distinguish it from the case of too many symbolic link traversals
660 in one of its non-final components.
668 function was introduced in
673 The Open Group Extended API Set 2 specification requires that the test
676 is searchable is based on whether
678 is open for searching, not whether the underlying directory currently
680 The present implementation of the
682 checks the current permissions of directory instead.
686 argument is variadic and may result in different calling conventions
687 than might otherwise be expected.