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