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
36 .Nd open or create a file for reading, writing or executing
42 .Fn open "const char *path" "int flags" "..."
44 .Fn openat "int fd" "const char *path" "int flags" "..."
46 The file name specified by
49 for either execution or reading and/or writing as specified by the
52 and the file descriptor returned to the calling process.
55 argument may indicate the file is to be
56 created if it does not exist (by specifying the
63 require an additional argument
65 and the file is created with mode
69 and modified by the process' umask value (see
74 function is equivalent to the
76 function except in the case where the
78 specifies a relative path, or the
85 the file to be opened is determined relative to the directory
86 associated with the file descriptor
88 instead of the current working directory.
91 parameter and the optional fourth parameter correspond exactly to
96 is passed the special value
100 parameter, the current working directory is used
101 and the behavior is identical to a call to
106 is called with an absolute
115 is specified with an absolute
117 a directory passed by the
119 argument is used as the topping point for the resolution.
120 See the definition of the
133 must be strictly relative to a file descriptor
136 .Pa sys/kern/vfs_lookup.c .
138 must not be an absolute path and must not contain ".." components
139 which cause the path resolution to escape the directory hierarchy
142 Additionally, no symbolic link in
144 may target absolute path or contain escaping ".." components.
150 .Dv vfs.lookup_cap_dotdot
152 MIB is set to zero, ".." components in the paths,
153 used in capability mode, or with the
155 flag, are completely disabled.
157 .Dv vfs.lookup_cap_dotdot_nonlocal
158 MIB is set to zero, ".." is not allowed if found on non-local filesystem.
160 The flags specified are formed by
164 .Bd -literal -offset indent -compact
165 O_RDONLY open for reading only
166 O_WRONLY open for writing only
167 O_RDWR open for reading and writing
168 O_EXEC open for execute only
169 O_SEARCH open for search only, an alias for O_EXEC
170 O_NONBLOCK do not block on open
171 O_APPEND append on each write
172 O_CREAT create file if it does not exist
173 O_TRUNC truncate size to 0
174 O_EXCL error if create and file exists
175 O_SHLOCK atomically obtain a shared lock
176 O_EXLOCK atomically obtain an exclusive lock
177 O_DIRECT eliminate or reduce cache effects
178 O_FSYNC synchronous writes
179 O_SYNC synchronous writes
180 O_NOFOLLOW do not follow symlinks
183 O_DIRECTORY error if file is not a directory
184 O_CLOEXEC set FD_CLOEXEC upon open
185 O_VERIFY verify the contents of the file
186 O_BENEATH require path to be strictly relative to topping directory
191 set causes each write on the file
192 to be appended to the end.
196 file exists, the file is truncated to zero length.
206 implement a simple exclusive access locking mechanism.
209 is set and the last component of the pathname is
212 will fail even if the symbolic
213 link points to a non-existent name.
216 flag is specified and the
218 system call would result
219 in the process being blocked for some reason (e.g., waiting for
220 carrier on a dialup line),
223 The descriptor remains in non-blocking mode for subsequent operations.
227 is used in the mask, all writes will
228 immediately and synchronously be written to disk.
238 is used in the mask and the target file passed to
240 is a symbolic link then the
244 When opening a file, a lock with
246 semantics can be obtained by setting
248 for a shared lock, or
250 for an exclusive lock.
251 If creating a file with
253 the request for the lock will never fail
254 (provided that the underlying file system supports locking).
257 may be used to minimize or eliminate the cache effects of reading and writing.
258 The system will attempt to avoid caching the data you read or write.
259 If it cannot avoid caching the data,
260 it will minimize the impact the data has on the cache.
261 Use of this flag can drastically reduce performance if not used with care.
264 may be used to ensure the OS does not assign this file as the
265 controlling terminal when it opens a tty device.
266 This is the default on
273 system call will not assign controlling terminals on
277 may be used to ensure the OS restores the terminal attributes when
278 initially opening a TTY.
279 This is the default on
286 on a TTY will always restore default terminal attributes on
290 may be used to ensure the resulting file descriptor refers to a
292 This flag can be used to prevent applications with elevated privileges
293 from opening files which are even unsafe to open with
295 such as device nodes.
300 flag for the newly returned file descriptor.
303 may be used to indicate to the kernel that the contents of the file should
304 be verified before allowing the open to proceed.
307 means is implementation specific.
308 The run-time linker (rtld) uses this flag to ensure shared objects have
309 been verified before operating on them.
314 if the specified relative path, after resolving all symlinks and ".."
315 references, does not reside in the directory hierarchy of
316 children beneath the topping directory.
317 Topping directory is the process current directory if relative
321 and the directory referenced by the
325 If the specified path is absolute,
327 allows arbitrary prefix that ends up at the topping directory,
328 after which all further resolved components must be under it.
334 execute permissions are checked at open time.
337 may not be used for any read operations like
338 .Xr getdirentries 2 .
339 The primary use for this descriptor will be as the lookup descriptor for the
345 returns a non-negative integer, termed a file descriptor.
346 It returns \-1 on failure.
347 The file pointer used to mark the current position within the
348 file is set to the beginning of the file.
350 If a sleeping open of a device node from
352 is interrupted by a signal, the call always fails with
356 flag is set for the signal.
357 A sleeping open of a fifo (see
359 is restarted as normal.
361 When a new file is created it is given the group of the directory
367 the new descriptor is set to remain open across
376 The system imposes a limit on the number of file descriptors
377 open simultaneously by one process.
380 system call returns the current system limit.
386 return a non-negative integer, termed a file descriptor.
387 They return \-1 on failure, and set
389 to indicate the error.
391 The named file is opened unless:
394 A component of the path prefix is not a directory.
395 .It Bq Er ENAMETOOLONG
396 A component of a pathname exceeded 255 characters,
397 or an entire path name exceeded 1023 characters.
400 is not set and the named file does not exist.
402 A component of the path name that must exist does not exist.
404 Search permission is denied for a component of the path prefix.
406 The required permissions (for reading and/or writing)
407 are denied for the given flags.
410 is specified and write permission is denied.
414 the file does not exist,
415 and the directory in which it is to be created
416 does not permit writing.
419 is specified, the file does not exist, and the directory in which it is to be
420 created has its immutable flag set, see the
422 manual page for more information.
424 The named file has its immutable flag set and the file is to be modified.
426 The named file has its append-only flag set, the file is to be modified, and
432 Too many symbolic links were encountered in translating the pathname.
434 The named file is a directory, and the arguments specify
435 it is to be modified.
437 The named file is a directory, and the flags specified
442 The named file resides on a read-only file system,
443 and the file is to be modified.
446 is specified and the named file would reside on a read-only file system.
448 The process has already reached its limit for open file descriptors.
450 The system file table is full.
453 was specified and the target is a symbolic link.
455 The named file is a character special or block
456 special file, and the device associated with this special file
460 is set, the named file is a fifo,
462 is set, and no process has the file open for reading.
466 operation was interrupted by a signal.
471 is specified but the underlying file system does not support locking.
473 The named file is a special file mounted through a file system that
474 does not support access to it (e.g.\& NFS).
475 .It Bq Er EWOULDBLOCK
481 is specified and the file is locked.
485 the file does not exist,
486 and the directory in which the entry for the new file is being placed
487 cannot be extended because there is no space left on the file
488 system containing the directory.
492 the file does not exist,
493 and there are no free inodes on the file system on which the
494 file is being created.
498 the file does not exist,
499 and the directory in which the entry for the new file
500 is being placed cannot be extended because the
501 user's quota of disk blocks on the file system
502 containing the directory has been exhausted.
506 the file does not exist,
507 and the user's quota of inodes on the file system on
508 which the file is being created has been exhausted.
510 An I/O error occurred while making the directory entry or
511 allocating the inode for
514 Corrupted data was detected while reading from the file system.
516 The file is a pure procedure (shared text) file that is being
519 system call requests write access.
524 points outside the process's allocated address space.
529 were specified and the file exists.
531 An attempt was made to open a socket (not currently implemented).
533 An attempt was made to open a descriptor with an illegal combination
546 argument does not specify an absolute path and the
551 nor a valid file descriptor open for searching.
555 argument is not an absolute path and
559 nor a file descriptor associated with a directory.
562 is specified and the file is not a directory.
565 is specified and the process is in capability mode.
568 was called and the process is in capability mode.
569 .It Bq Er ENOTCAPABLE
572 or contained a ".." component leading to a
573 directory outside of the directory hierarchy specified by
575 and the process is in capability mode.
576 .It Bq Er ENOTCAPABLE
579 flag was provided, and the absolute
581 does not have its tail fully contained under the topping directory,
592 .Xr getdtablesize 2 ,
602 These functions are specified by
608 .Er EMLINK instead of
614 is set in flags and the final component of pathname is a symbolic link
615 to distinguish it from the case of too many symbolic link traversals
616 in one of its non-final components.
624 function was introduced in
627 The Open Group Extended API Set 2 specification requires that the test
630 is searchable is based on whether
632 is open for searching, not whether the underlying directory currently
634 The present implementation of the
636 checks the current permissions of directory instead.
640 argument is variadic and may result in different calling conventions
641 than might otherwise be expected.