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.
79 In this case the file to be opened is determined relative to the directory
80 associated with the file descriptor
82 instead of the current working directory.
85 parameter and the optional fourth parameter correspond exactly to
90 is passed the special value
94 parameter, the current working directory is used
95 and the behavior is identical to a call to
107 must be strictly relative to a file descriptor
110 .Pa sys/kern/vfs_lookup.c .
112 must not be an absolute path and must not contain ".." components.
113 Additionally, no symbolic link in
115 may contain ".." components either.
120 The flags specified are formed by
124 .Bd -literal -offset indent -compact
125 O_RDONLY open for reading only
126 O_WRONLY open for writing only
127 O_RDWR open for reading and writing
128 O_EXEC open for execute only
129 O_NONBLOCK do not block on open
130 O_APPEND append on each write
131 O_CREAT create file if it does not exist
132 O_TRUNC truncate size to 0
133 O_EXCL error if create and file exists
134 O_SHLOCK atomically obtain a shared lock
135 O_EXLOCK atomically obtain an exclusive lock
136 O_DIRECT eliminate or reduce cache effects
137 O_FSYNC synchronous writes
138 O_SYNC synchronous writes
139 O_NOFOLLOW do not follow symlinks
142 O_DIRECTORY error if file is not a directory
143 O_CLOEXEC set FD_CLOEXEC upon open
144 O_VERIFY verify the contents of the file
145 O_BENEATH require path to be strictly relative to starting directory
150 set causes each write on the file
151 to be appended to the end.
155 file exists, the file is truncated to zero length.
165 implement a simple exclusive access locking mechanism.
168 is set and the last component of the pathname is
171 will fail even if the symbolic
172 link points to a non-existent name.
175 flag is specified and the
177 system call would result
178 in the process being blocked for some reason (e.g., waiting for
179 carrier on a dialup line),
182 The descriptor remains in non-blocking mode for subsequent operations.
186 is used in the mask, all writes will
187 immediately be written to disk,
188 the kernel will not cache written data
189 and all writes on the descriptor will not return until
190 the data to be written completes.
200 is used in the mask and the target file passed to
202 is a symbolic link then the
206 When opening a file, a lock with
208 semantics can be obtained by setting
210 for a shared lock, or
212 for an exclusive lock.
213 If creating a file with
215 the request for the lock will never fail
216 (provided that the underlying file system supports locking).
219 may be used to minimize or eliminate the cache effects of reading and writing.
220 The system will attempt to avoid caching the data you read or write.
221 If it cannot avoid caching the data,
222 it will minimize the impact the data has on the cache.
223 Use of this flag can drastically reduce performance if not used with care.
226 may be used to ensure the OS does not assign this file as the
227 controlling terminal when it opens a tty device.
228 This is the default on
235 system call will not assign controlling terminals on
239 may be used to ensure the OS restores the terminal attributes when
240 initially opening a TTY.
241 This is the default on
248 on a TTY will always restore default terminal attributes on
252 may be used to ensure the resulting file descriptor refers to a
254 This flag can be used to prevent applications with elevated privileges
255 from opening files which are even unsafe to open with
257 such as device nodes.
262 flag for the newly returned file descriptor.
265 may be used to indicate to the kernel that the contents of the file should
266 be verified before allowing the open to proceed.
269 means is implementation specific.
270 The run-time linker (rtld) uses this flag to ensure shared objects have
271 been verified before operating on them.
276 if the specified path, after resolving all symlinks and ".." references
277 in it, does not reside in the directory hierarchy of children beneath
278 the starting directory, or is an absolute path.
279 Starting directory is the process current directory if relative
283 and the directory referenced by the
285 argument when specifying relative
292 returns a non-negative integer, termed a file descriptor.
293 It returns \-1 on failure.
294 The file pointer used to mark the current position within the
295 file is set to the beginning of the file.
297 If a sleeping open of a device node from
299 is interrupted by a signal, the call always fails with
303 flag is set for the signal.
304 A sleeping open of a fifo (see
306 is restarted as normal.
308 When a new file is created it is given the group of the directory
314 the new descriptor is set to remain open across
323 The system imposes a limit on the number of file descriptors
324 open simultaneously by one process.
327 system call returns the current system limit.
333 return a non-negative integer, termed a file descriptor.
334 They return \-1 on failure, and set
336 to indicate the error.
338 The named file is opened unless:
341 A component of the path prefix is not a directory.
342 .It Bq Er ENAMETOOLONG
343 A component of a pathname exceeded 255 characters,
344 or an entire path name exceeded 1023 characters.
347 is not set and the named file does not exist.
349 A component of the path name that must exist does not exist.
351 Search permission is denied for a component of the path prefix.
353 The required permissions (for reading and/or writing)
354 are denied for the given flags.
357 is specified and write permission is denied.
361 the file does not exist,
362 and the directory in which it is to be created
363 does not permit writing.
366 is specified, the file does not exist, and the directory in which it is to be
367 created has its immutable flag set, see the
369 manual page for more information.
371 The named file has its immutable flag set and the file is to be modified.
373 The named file has its append-only flag set, the file is to be modified, and
379 Too many symbolic links were encountered in translating the pathname.
381 The named file is a directory, and the arguments specify
382 it is to be modified.
384 The named file resides on a read-only file system,
385 and the file is to be modified.
388 is specified and the named file would reside on a read-only file system.
390 The process has already reached its limit for open file descriptors.
392 The system file table is full.
395 was specified and the target is a symbolic link.
397 The named file is a character special or block
398 special file, and the device associated with this special file
402 is set, the named file is a fifo,
404 is set, and no process has the file open for reading.
408 operation was interrupted by a signal.
413 is specified but the underlying file system does not support locking.
415 The named file is a special file mounted through a file system that
416 does not support access to it (e.g.\& NFS).
417 .It Bq Er EWOULDBLOCK
423 is specified and the file is locked.
427 the file does not exist,
428 and the directory in which the entry for the new file is being placed
429 cannot be extended because there is no space left on the file
430 system containing the directory.
434 the file does not exist,
435 and there are no free inodes on the file system on which the
436 file is being created.
440 the file does not exist,
441 and the directory in which the entry for the new file
442 is being placed cannot be extended because the
443 user's quota of disk blocks on the file system
444 containing the directory has been exhausted.
448 the file does not exist,
449 and the user's quota of inodes on the file system on
450 which the file is being created has been exhausted.
452 An I/O error occurred while making the directory entry or
453 allocating the inode for
456 The file is a pure procedure (shared text) file that is being
459 system call requests write access.
464 points outside the process's allocated address space.
469 were specified and the file exists.
471 An attempt was made to open a socket (not currently implemented).
473 An attempt was made to open a descriptor with an illegal combination
483 argument does not specify an absolute path and the
488 nor a valid file descriptor open for searching.
492 argument is not an absolute path and
496 nor a file descriptor associated with a directory.
499 is specified and the file is not a directory.
502 is specified and the process is in capability mode.
505 was called and the process is in capability mode.
506 .It Bq Er ENOTCAPABLE
509 or contained a ".." component leading to a
510 directory outside of the directory hierarchy specified by
512 and the process is in capability mode or the
522 .Xr getdtablesize 2 ,
532 These functions are specified by
538 .Er EMLINK instead of
544 is set in flags and the final component of pathname is a symbolic link
545 to distinguish it from the case of too many symbolic link traversals
546 in one of its non-final components.
554 function was introduced in
557 The Open Group Extended API Set 2 specification requires that the test
560 is searchable is based on whether
562 is open for searching, not whether the underlying directory currently
564 The present implementation of the
566 checks the current permissions of directory instead.