xref: /freebsd/lib/libsys/open.2 (revision ce08af63788da219c0c5826fc3f2345fb2ce29f4)
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.Dd May 17, 2025
29.Dt OPEN 2
30.Os
31.Sh NAME
32.Nm open , openat
33.Nd open or create a file for reading, writing or executing
34.Sh LIBRARY
35.Lb libc
36.Sh SYNOPSIS
37.In fcntl.h
38.Ft int
39.Fn open "const char *path" "int flags" "..."
40.Ft int
41.Fn openat "int fd" "const char *path" "int flags" "..."
42.Sh DESCRIPTION
43The file name specified by
44.Fa path
45is opened
46for either execution or reading and/or writing as specified by the
47argument
48.Fa flags
49and the file descriptor returned to the calling process.
50The
51.Fa flags
52argument may indicate the file is to be
53created if it does not exist (by specifying the
54.Dv O_CREAT
55flag).
56In this case
57.Fn open
58and
59.Fn openat
60require an additional argument
61.Fa "mode_t mode" ,
62and the file is created with mode
63.Fa mode
64as described in
65.Xr chmod 2
66and modified by the process' umask value (see
67.Xr umask 2 ) .
68.Pp
69The
70.Fn openat
71function is equivalent to the
72.Fn open
73function except in the case where the
74.Fa path
75specifies a relative path or the
76.Va O_EMPTY_PATH
77flag is specified.
78For
79.Fn openat
80and relative
81.Fa path ,
82when
83.Fa fd
84references a directory, and the
85.Va O_EMPTY_PATH
86flag is not specified,
87the file to be opened is determined relative to the directory
88associated with the file descriptor
89.Fa fd
90instead of the current working directory.
91The
92.Fa flag
93parameter and the optional fourth parameter correspond exactly to
94the parameters of
95.Fn open .
96If
97.Fn openat
98is passed the special value
99.Dv AT_FDCWD
100in the
101.Fa fd
102parameter, the current working directory is used
103and the behavior is identical to a call to
104.Fn open .
105.Pp
106When
107.Fn openat
108is called with an absolute
109.Fa path ,
110it ignores the
111.Fa fd
112argument.
113.Pp
114When
115.Fn openat
116is called with the
117.Fa fd
118argument that does not reference a directory, the call fails unless
119.Va O_EMPTY_PATH
120flag is specified, see below.
121.Pp
122In
123.Xr capsicum 4
124capability mode,
125.Fn open
126is not permitted.
127The
128.Fa path
129argument to
130.Fn openat
131must be strictly relative to a file descriptor
132.Fa fd ;
133that is,
134.Fa path
135must not be an absolute path and must not contain ".." components
136which cause the path resolution to escape the directory hierarchy
137starting at
138.Fa fd .
139Additionally, no symbolic link in
140.Fa path
141may target absolute path or contain escaping ".." components.
142.Fa fd
143must not be
144.Dv AT_FDCWD .
145.Pp
146If the
147.Dv vfs.lookup_cap_dotdot
148.Xr sysctl 3
149MIB is set to zero, ".." components in the paths,
150used in capability mode,
151are completely disabled.
152If the
153.Dv vfs.lookup_cap_dotdot_nonlocal
154MIB is set to zero, ".." is not allowed if found on non-local filesystem.
155.Pp
156The
157.Fa flags
158are formed by
159.Em or Ns 'ing
160the following values:
161.Pp
162.Bl -tag -width O_RESOLVE_BENEATH
163.It Dv O_RDONLY
164open for reading only
165.It Dv O_WRONLY
166open for writing only
167.It Dv O_RDWR
168open for reading and writing
169.It Dv O_EXEC
170open for execute only
171.It Dv O_SEARCH
172open for search only
173(an alias for
174.Dv O_EXEC
175typically used with
176.Dv O_DIRECTORY )
177.It Dv O_NONBLOCK
178do not block on open
179.It Dv O_APPEND
180set file pointer to the end of the file before each write
181.It Dv O_CREAT
182create file if it does not exist
183.It Dv O_TRUNC
184truncate size to 0
185.It Dv O_EXCL
186fail if
187.Dv O_CREAT
188is set and the file exists
189.It Dv O_SHLOCK
190atomically obtain a shared lock
191.It Dv O_EXLOCK
192atomically obtain an exclusive lock
193.It Dv O_DIRECT
194read and write directly from the backing store
195.It Dv O_FSYNC
196synchronous data and metadata writes
197.Pq historical synonym for Dv O_SYNC
198.It Dv O_SYNC
199synchronous data and metadata writes
200.It Dv O_DSYNC
201synchronous data writes
202.It Dv O_NOFOLLOW
203do not follow symlinks
204.It Dv O_NOCTTY
205ignored
206.It Dv O_TTY_INIT
207ignored
208.It Dv O_DIRECTORY
209error if file is not a directory
210.It Dv O_CLOEXEC
211automatically close file on
212.Xr execve 2
213.It Dv O_CLOFORK
214automatically close file on any child process created with
215.Fn fork 2
216.It Dv O_VERIFY
217verify the contents of the file with
218.Xr mac_veriexec 4
219.It Dv O_RESOLVE_BENEATH
220.Pq Xr openat 2 only
221path resolution must not cross the
222.Fa fd
223directory
224.It Dv O_PATH
225record only the target path in the opened descriptor
226.It Dv O_EMPTY_PATH
227.Pq Xr openat 2 only
228open file referenced by
229.Fa fd
230if path is empty
231.It Dv O_NAMEDATTR
232open a named attribute or named attribute directory
233.El
234.Pp
235Exactly one of the flags
236.Dv O_RDONLY ,
237.Dv O_WRONLY ,
238.Dv O_RDWR ,
239or
240.Dv O_EXEC
241must be provided.
242.Pp
243Opening a file with
244.Dv O_APPEND
245set causes each write on the resulting file descriptor
246to be appended to the end of the file.
247.Pp
248If
249.Dv O_TRUNC
250is specified and the
251file exists, the file is truncated to zero length.
252.Pp
253If
254.Dv O_CREAT
255is set, but file already exists,
256this flag has no effect except when
257.Dv O_EXCL
258is set too, in this case
259.Fn open
260fails with
261.Er EEXIST .
262This may be used to
263implement a simple exclusive access locking mechanism.
264In all other cases, the file is created
265and the access permission bits (see
266.Xr chmod 2)
267of the file mode
268are set to the value of the third argument taken as
269.Fa "mode_t mode"
270and passed through the
271.Xr umask 2 .
272This argument does not affect whether the file is opened
273for reading, writing, or for both.
274The open' request for a lock on the file, created with
275.Dv O_CREAT ,
276will never fail
277provided that the underlying file system supports locking;
278see also
279.Dv O_SHLOCK
280and
281.Dv O_EXLOCK
282below.
283.Pp
284If
285.Dv O_EXCL
286is set and the last component of the pathname is
287a symbolic link,
288.Fn open
289will fail even if the symbolic
290link points to a non-existent name.
291.Pp
292If
293.Dv O_NONBLOCK
294is specified and the
295.Fn open
296system call would
297block for some reason (for example, waiting for
298carrier on a dialup line),
299.Fn open
300returns immediately.
301The descriptor remains in non-blocking mode for subsequent operations.
302.Pp
303If
304.Dv O_SYNC
305is used in the mask, all writes will
306immediately and synchronously be written to disk.
307.Dv O_FSYNC
308is an historical synonym for
309.Dv O_SYNC .
310.Pp
311If
312.Dv O_DSYNC
313is used in the mask, all data and metadata required to read the data will be
314synchronously written to disk, but changes to metadata such as file access and
315modification timestamps may be written later.
316.Pp
317If
318.Dv O_NOFOLLOW
319is used in the mask and the target file passed to
320.Fn open
321is a symbolic link then the
322.Fn open
323will fail.
324.Pp
325When opening a file, a lock with
326.Xr flock 2
327semantics can be obtained by setting
328.Dv O_SHLOCK
329for a shared lock, or
330.Dv O_EXLOCK
331for an exclusive lock.
332.Pp
333.Dv O_DIRECT
334may be used to minimize or eliminate the cache effects of reading and writing.
335The system will attempt to avoid caching the data you read or write.
336If it cannot avoid caching the data,
337it will minimize the impact the data has on the cache.
338Use of this flag can drastically reduce performance if not used with care.
339The semantics of this flag are filesystem dependent,
340and some filesystems may ignore it entirely.
341.Pp
342.Dv O_NOCTTY
343may be used to ensure the OS does not assign this file as the
344controlling terminal when it opens a tty device.
345This is the default on
346.Fx ,
347but is present for
348POSIX
349compatibility.
350The
351.Fn open
352system call will not assign controlling terminals on
353.Fx .
354.Pp
355.Dv O_TTY_INIT
356may be used to ensure the OS restores the terminal attributes when
357initially opening a TTY.
358This is the default on
359.Fx ,
360but is present for
361POSIX
362compatibility.
363The initial call to
364.Fn open
365on a TTY will always restore default terminal attributes on
366.Fx .
367.Pp
368.Dv O_DIRECTORY
369may be used to ensure the resulting file descriptor refers to a
370directory.
371This flag can be used to prevent applications with elevated privileges
372from opening files which are even unsafe to open with
373.Dv O_RDONLY ,
374such as device nodes.
375.Pp
376.Dv O_CLOEXEC
377may be used to set
378.Dv FD_CLOEXEC
379flag for the newly returned file descriptor.
380.Pp
381.Dv O_CLOFORK
382may be used to set
383.Dv FD_CLOFORK
384flag for the newly returned file descriptor.
385The file will be closed on any child process created with
386.Fn fork 2 ,
387.Fn vfork 2
388or
389.Fn rfork 2
390with the
391.Dv RFFDG
392flag, remaining open in the parent.
393Both the
394.Dv O_CLOEXEC
395and
396.Dv O_CLOFORK
397flags can be modified with the
398.Dv F_SETFD
399.Fn fcntl 2
400command.
401.Pp
402.Dv O_VERIFY
403may be used to indicate to the kernel that the contents of the file should
404be verified before allowing the open to proceed.
405The details of what
406.Dq verified
407means is implementation specific.
408The run-time linker (rtld) uses this flag to ensure shared objects have
409been verified before operating on them.
410.Pp
411.Dv O_RESOLVE_BENEATH
412returns
413.Er ENOTCAPABLE
414if any intermediate component of the specified relative path does not
415reside in the directory hierarchy beneath the starting directory.
416Absolute paths or even the temporal escape from beneath of the starting
417directory is not allowed.
418.Pp
419When a directory
420is opened with
421.Dv O_SEARCH ,
422execute permissions are checked at open time.
423The returned file descriptor
424may not be used for any read operations like
425.Xr getdirentries 2 .
426The primary use of this descriptor is as the lookup descriptor for the
427.Fn *at
428family of functions.
429If
430.Dv O_SEARCH
431was not requested at open time, then the
432.Fn *at
433functions use the current directory permissions for the directory referenced
434by the descriptor at the time of the
435.Fn *at
436call.
437.Pp
438.Dv O_PATH
439returns a file descriptor that can be used as the first argument for
440.Fn openat
441and other filesystem-related system calls collectively named
442.Fn *at
443taking a file descriptor argument, like
444.Xr fstatat 2
445and others.
446The other functionality of the returned file descriptor is limited to
447the following descriptor-level operations:
448.Pp
449.Bl -tag -width __acl_aclcheck_fd -offset indent -compact
450.It Xr connectat 2
451for unix domain socket
452.Pq see Xr unix 4
453.It Xr fcntl 2
454but advisory locking is not allowed
455.It Xr dup 2
456.It Xr close 2
457.It Xr fstat 2
458.It Xr fstatfs 2
459.It Xr fchdir 2
460.It Xr fchroot 2
461.It Xr fexecve 2
462.It Xr funlinkat 2
463can be passed as the third argument
464.It Dv SCM_RIGHTS
465can be passed over a
466.Xr unix 4
467socket using a
468.Dv SCM_RIGHTS
469message
470.It Xr kqueue 2
471only with
472.Dv EVFILT_VNODE
473.It Xr __acl_get_fd 2
474.It Xr __acl_aclcheck_fd 2
475.It Xr extattr 2
476.It Xr capsicum 4
477can be passed to
478.Fn cap_*_limit
479and
480.Fn cap_*_get
481system calls (such as
482.Xr cap_rights_limit 2 ) .
483.El
484.Pp
485Other operations like
486.Xr read 2 ,
487.Xr ftruncate 2 ,
488and any other that operate on file and not on file descriptor (except
489.Xr fstat 2 ) ,
490are not allowed.
491.Pp
492A file descriptor created with the
493.Dv O_PATH
494flag can be opened as a normal (operable) file descriptor by
495specifying it as the
496.Fa fd
497argument to
498.Fn openat
499with an empty
500.Fa path
501and the
502.Dv O_EMPTY_PATH
503flag.
504Such an open behaves as if the current path of the file referenced by
505.Fa fd
506is passed, except that path walk permissions are not checked.
507See also the description of
508.Dv AT_EMPTY_PATH
509flag for
510.Xr fstatat 2
511and related syscalls.
512.Pp
513Conversely, a file descriptor
514.Dv fd
515referencing a filesystem file can be converted to the
516.Dv O_PATH
517type of descriptor by using the following call
518.Dl opath_fd = openat(fd, \[dq]\[dq], O_EMPTY_PATH | O_PATH);
519.Pp
520If successful,
521.Fn open
522returns a non-negative integer, termed a file descriptor.
523It returns \-1 on failure.
524The file descriptor value returned is the lowest numbered descriptor
525currently not in use by the process.
526The file pointer used to mark the current position within the
527file is set to the beginning of the file.
528.Pp
529If a sleeping open of a device node from
530.Xr devfs 4
531is interrupted by a signal, the call always fails with
532.Er EINTR ,
533even if the
534.Dv SA_RESTART
535flag is set for the signal.
536A sleeping open of a fifo (see
537.Xr mkfifo 2 )
538is restarted as normal.
539.Pp
540When a new file is created, it is assigned the group of the directory
541which contains it.
542.Pp
543Unless
544.Dv O_CLOEXEC
545flag was specified,
546the new descriptor is set to remain open across
547.Xr execve 2
548system calls; see
549.Xr close 2 ,
550.Xr fcntl 2
551and the description of the
552.Dv O_CLOEXEC
553flag.
554.Pp
555When the
556.Dv O_NAMEDATTR
557flag is specified for an
558.Fn openat
559where the
560.Fa fd
561argument is for a file object,
562a named attribute for the file object
563is opened and not the file object itself.
564If the
565.Dv O_CREAT
566flag has been specified as well, the named attribute will be
567created if it does not exist.
568When the
569.Dv O_NAMEDATTR
570flag is specified for a
571.Fn open ,
572a named attribute for the current working directory is opened and
573not the current working directory.
574The
575.Fa path
576argument for this
577.Fn openat
578or
579.Fn open
580must be a single component name with no embedded
581.Ql / .
582If the
583.Fa path
584argument is
585.Ql .\&
586then the named attribute directory for the file object is opened.
587(See
588.Xr named_attribute 7
589for more information.)
590.Pp
591The system imposes a limit on the number of file descriptors
592open simultaneously by one process.
593The
594.Xr getdtablesize 2
595system call returns the current system limit.
596.Sh RETURN VALUES
597If successful,
598.Fn open
599and
600.Fn openat
601return a non-negative integer, termed a file descriptor.
602They return \-1 on failure, and set
603.Va errno
604to indicate the error.
605.Sh ERRORS
606The named file is opened unless:
607.Bl -tag -width Er
608.It Bq Er ENOTDIR
609A component of the path prefix is not a directory.
610.It Bq Er ENAMETOOLONG
611A component of a pathname exceeded 255 characters,
612or an entire path name exceeded 1023 characters.
613.It Bq Er ENOENT
614.Dv O_CREAT
615is not set and the named file does not exist.
616.It Bq Er ENOENT
617A component of the path name that must exist does not exist.
618.It Bq Er EACCES
619Search permission is denied for a component of the path prefix.
620.It Bq Er EACCES
621The required permissions (for reading and/or writing)
622are denied for the given flags.
623.It Bq Er EACCES
624.Dv O_TRUNC
625is specified and write permission is denied.
626.It Bq Er EACCES
627.Dv O_CREAT
628is specified,
629the file does not exist,
630and the directory in which it is to be created
631does not permit writing.
632.It Bq Er EPERM
633.Dv O_CREAT
634is specified, the file does not exist, and the directory in which it is to be
635created has its immutable flag set, see the
636.Xr chflags 2
637manual page for more information.
638.It Bq Er EPERM
639The named file has its immutable flag set and the file is to be modified.
640.It Bq Er EPERM
641The named file has its append-only flag set, the file is to be modified, and
642.Dv O_TRUNC
643is specified or
644.Dv O_APPEND
645is not specified.
646.It Bq Er ELOOP
647Too many symbolic links were encountered in translating the pathname.
648.It Bq Er EISDIR
649The named file is a directory, and the arguments specify
650it is to be modified.
651.It Bq Er EISDIR
652The named file is a directory, and the flags specified
653.Dv O_CREAT
654without
655.Dv O_DIRECTORY .
656.It Bq Er EROFS
657The named file resides on a read-only file system,
658and the file is to be modified.
659.It Bq Er EROFS
660.Dv O_CREAT
661is specified and the named file would reside on a read-only file system.
662.It Bq Er EMFILE
663The process has already reached its limit for open file descriptors.
664.It Bq Er ENFILE
665The system file table is full.
666.It Bq Er EMLINK
667.Dv O_NOFOLLOW
668was specified and the target is a symbolic link.
669POSIX
670specifies a different error for this case; see the note in
671.Sx STANDARDS
672below.
673.It Bq Er ENXIO
674The named file is a character special or block
675special file, and the device associated with this special file
676does not exist.
677.It Bq Er ENXIO
678.Dv O_NONBLOCK
679is set, the named file is a fifo,
680.Dv O_WRONLY
681is set, and no process has the file open for reading.
682.It Bq Er EINTR
683The
684.Fn open
685operation was interrupted by a signal.
686.It Bq Er EOPNOTSUPP
687.Dv O_SHLOCK
688or
689.Dv O_EXLOCK
690is specified but the underlying file system does not support locking.
691.It Bq Er EOPNOTSUPP
692The named file is a special file mounted through a file system that
693does not support access to it (for example, NFS).
694.It Bq Er EWOULDBLOCK
695.Dv O_NONBLOCK
696and one of
697.Dv O_SHLOCK
698or
699.Dv O_EXLOCK
700is specified and the file is locked.
701.It Bq Er ENOSPC
702.Dv O_CREAT
703is specified,
704the file does not exist,
705and the directory in which the entry for the new file is being placed
706cannot be extended because there is no space left on the file
707system containing the directory.
708.It Bq Er ENOSPC
709.Dv O_CREAT
710is specified,
711the file does not exist,
712and there are no free inodes on the file system on which the
713file is being created.
714.It Bq Er EDQUOT
715.Dv O_CREAT
716is specified,
717the file does not exist,
718and the directory in which the entry for the new file
719is being placed cannot be extended because the
720user's quota of disk blocks on the file system
721containing the directory has been exhausted.
722.It Bq Er EDQUOT
723.Dv O_CREAT
724is specified,
725the file does not exist,
726and the user's quota of inodes on the file system on
727which the file is being created has been exhausted.
728.It Bq Er EIO
729An I/O error occurred while making the directory entry or
730allocating the inode for
731.Dv O_CREAT .
732.It Bq Er EINTEGRITY
733Corrupted data was detected while reading from the file system.
734.It Bq Er ETXTBSY
735The file is a pure procedure (shared text) file that is being
736executed and the
737.Fn open
738system call requests write access.
739.It Bq Er EFAULT
740The
741.Fa path
742argument
743points outside the process's allocated address space.
744.It Bq Er EEXIST
745.Dv O_CREAT
746and
747.Dv O_EXCL
748were specified and the file exists.
749.It Bq Er EOPNOTSUPP
750An attempt was made to open a socket (not currently implemented).
751.It Bq Er EINVAL
752An attempt was made to open a descriptor with an illegal combination
753of
754.Dv O_RDONLY ,
755.Dv O_WRONLY ,
756or
757.Dv O_RDWR ,
758and
759.Dv O_EXEC
760or
761.Dv O_SEARCH .
762.It Bq Er EINVAL
763.Dv O_CREAT
764is specified,
765and the last component of the
766.Fa path
767argument is invalid on the file system on which the file is being created.
768.It Bq Er EBADF
769The
770.Fa path
771argument does not specify an absolute path and the
772.Fa fd
773argument is
774neither
775.Dv AT_FDCWD
776nor a valid file descriptor open for searching.
777.It Bq Er ENOTDIR
778The
779.Fa path
780argument is not an absolute path and
781.Fa fd
782is neither
783.Dv AT_FDCWD
784nor a file descriptor associated with a directory.
785.It Bq Er ENOTDIR
786.Dv O_DIRECTORY
787is specified and the file is not a directory.
788.It Bq Er ECAPMODE
789.Dv AT_FDCWD
790is specified and the process is in capability mode.
791.It Bq Er ECAPMODE
792.Fn open
793was called and the process is in capability mode.
794.It Bq Er ENOTCAPABLE
795.Fa path
796is an absolute path and the process is in capability mode.
797.It Bq Er ENOTCAPABLE
798.Fa path
799is an absolute path and
800.Dv O_RESOLVE_BENEATH
801is specified.
802.It Bq Er ENOTCAPABLE
803.Fa path
804contains a ".." component leading to a directory outside
805of the directory hierarchy specified by
806.Fa fd
807and the process is in capability mode.
808.It Bq Er ENOTCAPABLE
809.Fa path
810contains a ".." component leading to a directory outside
811of the directory hierarchy specified by
812.Fa fd
813and
814.Dv O_RESOLVE_BENEATH
815is specified.
816.It Bq Er ENOTCAPABLE
817.Fa path
818contains a ".." component, the
819.Dv vfs.lookup_cap_dotdot
820.Xr sysctl 3
821is set, and the process is in capability mode.
822.It Bq Er ENOATTR
823.Dv O_NAMEDATTR
824has been specified and the file object is not a named attribute
825directory or named attribute.
826.El
827.Sh SEE ALSO
828.Xr chmod 2 ,
829.Xr close 2 ,
830.Xr dup 2 ,
831.Xr fexecve 2 ,
832.Xr fhopen 2 ,
833.Xr getdtablesize 2 ,
834.Xr getfh 2 ,
835.Xr lgetfh 2 ,
836.Xr lseek 2 ,
837.Xr read 2 ,
838.Xr umask 2 ,
839.Xr write 2 ,
840.Xr fopen 3 ,
841.Xr capsicum 4 ,
842.Xr unix 4 ,
843.Xr named_attribute 7
844.Sh STANDARDS
845These functions are specified by
846.St -p1003.1-2008 .
847.Pp
848.Fx
849sets
850.Va errno
851to
852.Er EMLINK instead of
853.Er ELOOP
854as specified by
855POSIX
856when
857.Dv O_NOFOLLOW
858is set in flags and the final component of pathname is a symbolic link
859to distinguish it from the case of too many symbolic link traversals
860in one of its non-final components.
861.Pp
862The Open Group Extended API Set 2 specification, that introduced the
863.Fn *at
864API, required that the test for whether
865.Fa fd
866is searchable is based on whether
867.Fa fd
868is open for searching, not whether the underlying directory currently
869permits searches.
870The present implementation of the
871.Fa openat
872system call is believed to be compatible with
873.\" .St -p1003.1-2017 ,
874.\" XXX: This should be replaced in the future when an appropriate argument to
875.\" the St macro is available: -p1003.1-2017
876.No IEEE Std 1003.1-2008, 2017 Edition ("POSIX.1") ,
877which specifies that behavior for
878.Dv O_SEARCH ,
879in the absence of the flag the implementation checks the current
880permissions of a directory.
881.Sh HISTORY
882The
883.Fn open
884function appeared in
885.At v1 .
886The
887.Fn openat
888function was introduced in
889.Fx 8.0 .
890.Dv O_DSYNC
891appeared in 13.0.
892.Dv O_NAMEDATTR
893appeared in 15.0.
894.Dv O_CLOFORK
895appeared in
896.Fx 15.0 .
897.Sh BUGS
898The
899.Fa mode
900argument is variadic and may result in different calling conventions
901than might otherwise be expected.
902