summaryrefslogtreecommitdiff
path: root/usr/src/man/man2/open.2
blob: 3096f4cc9e2da92e1d06e1a42caa3a5391746f68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
.\"
.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
.\" permission to reproduce portions of its copyrighted documentation.
.\" Original documentation from The Open Group can be obtained online at
.\" http://www.opengroup.org/bookstore/.
.\"
.\" The Institute of Electrical and Electronics Engineers and The Open
.\" Group, have given us permission to reprint portions of their
.\" documentation.
.\"
.\" In the following statement, the phrase ``this text'' refers to portions
.\" of the system documentation.
.\"
.\" Portions of this text are reprinted and reproduced in electronic form
.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
.\" Standard for Information Technology -- Portable Operating System
.\" Interface (POSIX), The Open Group Base Specifications Issue 6,
.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
.\" Engineers, Inc and The Open Group.  In the event of any discrepancy
.\" between these versions and the original IEEE and The Open Group
.\" Standard, the original IEEE and The Open Group Standard is the referee
.\" document.  The original Standard can be obtained online at
.\" http://www.opengroup.org/unix/online.html.
.\"
.\" This notice shall appear on any product containing this material.
.\"
.\" The contents of this file are subject to the terms of the
.\" Common Development and Distribution License (the "License").
.\" You may not use this file except in compliance with the License.
.\"
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
.\" or http://www.opensolaris.org/os/licensing.
.\" See the License for the specific language governing permissions
.\" and limitations under the License.
.\"
.\" When distributing Covered Code, include this CDDL HEADER in each
.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
.\" If applicable, add the following below this CDDL HEADER, with the
.\" fields enclosed by brackets "[]" replaced with your own identifying
.\" information: Portions Copyright [yyyy] [name of copyright owner]
.\"
.\"
.\" Copyright 1989 AT&T
.\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
.\" Copyright (c) 2008, Sun Microsystems, Inc.  All Rights Reserved.
.\" Portions Copyright (c) 2013, OmniTI Computer Consulting, Inc.
.\" All Rights Reserved.
.\" Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
.\" Copyright 2020 Joyent, Inc.
.\" Copyright 2022 Oxide Computer Company
.\"
.Dd February 5, 2022
.Dt OPEN 2
.Os
.Sh NAME
.Nm open ,
.Nm openat
.Nd open a file
.Sh SYNOPSIS
.In sys/types.h
.In sys/stat.h
.In fcntl.h
.Ft int
.Fo open
.Fa "const char *path"
.Fa "int oflag"
.Op , Fa "mode_t mode"
.Fc
.Ft int
.Fo openat
.Fa "int fildes"
.Fa "const char *path"
.Fa "int oflag"
.Op , Fa "mode_t mode"
.Fc
.Sh DESCRIPTION
The
.Fn open
function establishes the connection between a file and a file descriptor.
It creates an open file description that refers to a file and a file descriptor
that refers to that open file description.
The file descriptor is used by other I/O functions to refer to that file.
The
.Fa path
argument points to a pathname naming the file.
.Pp
The
.Fn openat
function is identical to the
.Fn open
function except
that the
.Fa path
argument is interpreted relative to the starting point
implied by the
.Fa fildes
argument.
If the
.Fa fildes
argument has the special value
.Dv AT_FDCWD ,
a relative path argument will be resolved relative to the current working
directory.
If the
.Fa path
argument is absolute, the
.Fa fildes
argument is ignored.
.Pp
The
.Fn open
function returns a file descriptor for the named file that is the lowest file
descriptor not currently open for that process.
The open file description is new, and therefore the file descriptor does not
share it with any other process in the system.
The
.Dv FD_CLOEXEC
file descriptor flag associated with the new file descriptor is cleared.
.Pp
The file offset used to mark the current position within the file is set to the
beginning of the file.
.Pp
The file status flags and file access modes of the open file description are
set according to the value of
.Fa oflag .
The
.Fa mode
argument is used only
when
.Dv O_CREAT
is specified
.Pq "see below" .
.Pp
Values for
.Fa oflag
are constructed by a bitwise-inclusive-OR of flags from
the following list, defined in
.Xr fcntl.h 3HEAD .
Applications must specify exactly one of the first three values (file access
modes) below in the value of
.Fa oflag :
.Bl -tag -width Ds
.It Dv O_RDONLY
Open for reading only.
.It Dv O_WRONLY
Open for writing only.
.It Dv O_RDWR
Open for reading and writing.
The result is undefined if this flag is applied to a FIFO.
.El
.Pp
Any combination of the following may be used:
.Bl -tag -width Ds
.It Dv O_APPEND
If set, the file offset is set to the end of the file prior to each write.
.It Dv O_CREAT
Create the file if it does not exist.
This flag requires that the
.Fa mode
argument be specified.
.Pp
If the file exists, this flag has no effect except as noted under
.Dv O_EXCL
below.
Otherwise, the file is created with the user ID of the file set to the
effective user ID of the process.
The group ID of the file is set to the effective group IDs of the process, or
if the
.Dv S_ISGID
bit is set in the directory in which the file is being created, the file's
group ID is set to the group ID of its parent directory.
If the group ID of the new file does not match the effective group
ID or one of the supplementary groups IDs, the
.Dv S_ISGID bit is cleared.
.Pp
The access permission bits
.Po
see
.Xr stat.h 3HEAD
.Pc
of the file mode are set to the value of
.Fa mode ,
modified as follows
.Po
see
.Xr creat 2
.Pc :
a bitwise-AND is performed on the file-mode bits and the corresponding bits in
the complement of the process's file mode creation mask.
Thus, all bits set in the process's file mode creation mask
.Po
see
.Xr umask 2
.Pc
are correspondingly cleared in the file's permission mask.
The
.Dq save text image after execution bit
of the mode is cleared
.Po
see
.Xr chmod 2
.Pc .
When bits other than the file permission bits are set, the effect is
unspecified.
The
.Fa mode
argument does not affect whether the file is open for reading, writing or for
both.
.It Dv O_DIRECT
Indicates that the file data is not going to be reused in the near future.
When possible, data is read or written directly between the application's
memory and the device when the data is accessed with
.Xr read 2
and
.Xr write 2
operations.
See
.Xr directio 3C
for more details.
.It Dv O_DIRECTORY
Indicates that attempts to open
.Fa path
should fail unless
.Fa path
is a directory.
If both
.Dv O_CREAT
and
.Dv O_DIRECTORY
are specified then the call will fail if it would result in a file being
created.
If a directory already exists at
.Fa path
then it will behave as if the
.Dv O_DIRECTORY
flag had not been present.
If the
.Dv O_EXCL
and
.Dv O_CREAT
flags are specified, then the call will always fail as they imply a file should
always be created.
.It Dv O_DSYNC
Write I/O operations on the file descriptor complete as defined by synchronized
I/O data integrity completion.
.It Dv O_EXCL
If
.Dv O_CREAT
and
.Dv O_EXCL
are set,
.Fn open
fails if the file exists.
The check for the existence of the file and the creation of the file if
it does not exist is atomic with respect to other threads executing
.Fn open
naming the same filename in the same directory with
.Dv O_EXCL
and
.Dv O_CREAT
set.
If
.Dv O_EXCL
and
.Dv O_CREAT
are set, and
.Fa path
names a symbolic link,
.Fn open
fails and sets
.Va errno
to
.Er EEXIST ,
regardless of the contents of the symbolic link.
If
.Dv O_EXCL
is set and
.Dv O_CREAT
is not set, the result is undefined.
.It Dv O_EXEC
If set, indicates that the file should be opened for execute permission.
This option is only valid for regular files; an error will be returned if the
target is not a regular file.
.It Dv O_LARGEFILE
If set, the offset maximum in the open file description is the largest value
that can be represented correctly in an object of type
.Vt off64_t .
.It Dv O_NOCTTY
If set and
.Fa path
identifies a terminal device,
.Fn open
does not cause the terminal device to become the controlling terminal for the
process.
.It Dv O_NOFOLLOW
If the path names a symbolic link,
.Fn open
fails and sets
.Va errno
to
.Er ELOOP .
.It Dv O_NOLINKS
If the link count of the named file is greater than
.Sy 1 ,
.Fn open
fails and sets
.Va errno
to
.Er EMLINK .
.It Dv O_CLOEXEC
If set, the file descriptor returned will be closed prior to any future
.Xr exec 2
calls.
.It Dv O_NONBLOCK O_NDELAY
These flags can affect subsequent reads and writes
.Po
see
.Xr read 2
and
.Xr write 2
.Pc .
If both
.Dv O_NDELAY
and
.Dv O_NONBLOCK
are set,
.Dv O_NONBLOCK
takes precedence.
.Pp
When opening a FIFO with
.Dv O_RDONLY
or
.Dv O_WRONLY
set:
.Bl -bullet
.It
If
.Dv O_NONBLOCK
or
.Dv O_NDELAY
is set, an
.Fn open
for reading only returns without delay.
An
.Fn open
for writing only returns an error if no process currently has the file open for
reading.
.It
If
.Dv O_NONBLOCK
and
.Dv O_NDELAY
are clear, an
.Fn open
for reading only blocks until a thread opens the file for writing.
An
.Fn open
for writing only blocks the calling thread until a thread opens the file for
reading.
.El
.Pp
After both ends of a FIFO have been opened once, there is no guarantee that
further calls to
.Fn open
.Dv O_RDONLY
.Pq Dv O_WRONLY
will synchronize with later calls to
.Fn open
.Dv O_WRONLY
.Pq Dv O_RDONLY
until both ends of the FIFO have been closed by all readers and writers.
Any data written into a FIFO will be lost if both ends of the FIFO are closed
before the data is read.
.Pp
When opening a block special or character special file that supports
non-blocking opens:
.Bl -bullet
.It
If
.Dv O_NONBLOCK
or
.Dv O_NDELAY
is set, the
.Fn open
function returns without blocking for the device to be ready or available.
Subsequent behavior of the device is device-specific.
.It
If
.Dv O_NONBLOCK
and
.Dv O_NDELAY
are clear, the
.Fn open
function blocks the calling thread until the device is ready or available
before returning.
.El
.Pp
Otherwise, the behavior of
.Dv O_NONBLOCK
and
.Dv O_NDELAY
is unspecified.
.It Dv O_RSYNC
Read I/O operations on the file descriptor complete at the same level of
integrity as specified by the
.Dv O_DSYNC
and
.Dv O_SYNC
flags.
If both
.Dv O_DSYNC
and
.Dv O_RSYNC
are set in
.Fa oflag ,
all I/O operations on the file descriptor complete as defined by synchronized
I/O data integrity completion.
If both
.Dv O_SYNC
and
.Dv O_RSYNC
are set in
.Fa oflag ,
all I/O operations on the file descriptor complete as defined by synchronized
I/O file integrity completion.
.It Dv O_SEARCH
If set, indicates that the directory should be opened for searching.
This option is only valid for a directory; an error will be returned if the
target is not a directory.
.It Dv O_SYNC
Write I/O operations on the file descriptor complete as defined by synchronized
I/O file integrity completion
.Po
see
.Xr fcntl.h 3HEAD
.Pc
definition of
.Dv O_SYNC .
.It Dv O_TRUNC
If the file exists and is a regular file, and the file is successfully opened
.Dv O_RDWR
or
.Dv O_WRONLY ,
its length is truncated to
.Sy 0
and the mode and owner are unchanged.
It has no effect on FIFO special files or terminal device files.
Its effect on other file types is implementation-dependent.
The result of using
.Dv O_TRUNC
with
.Dv O_RDONLY
is undefined.
.It Dv O_XATTR
If set in
.Fn openat ,
a relative path argument is interpreted as a reference to an extended attribute
of the file associated with the supplied file descriptor.
This flag therefore requires the presence of a legal
.Fa fildes
argument.
If set in
.Fn open ,
the implied file descriptor is that for the current working directory.
Extended attributes must be referenced with a relative path; providing an
absolute path results in a normal file reference.
.El
.Pp
If
.Dv O_CREAT
is set and the file did not previously exist, upon successful completion,
.Fn open
marks for update the
.Fa st_atime ,
.Fa st_ctime ,
and
.Fa st_mtime
fields of the file and the
.Fa st_ctime
and
.Fa st_mtime
fields of the parent directory.
.Pp
If
.Dv O_TRUNC
is set and the file did previously exist, upon successful completion,
.Fn open
marks for update the
.Fa st_ctime
and
.Fa st_mtime
fields of the file.
.Pp
If both the
.Dv O_SYNC
and
.Dv O_DSYNC
flags are set, the effect is as if only the
.Dv O_SYNC
flag was set.
.Pp
If
.Fa path
refers to a STREAMS file,
.Fa oflag
may be constructed from
.Dv O_NONBLOCK
or
.Dv O_NODELAY
OR-ed with either
.Dv O_RDONLY ,
.Dv O_WRONLY ,
or
.Dv O_RDWR .
Other flag values are not applicable to STREAMS devices and have no effect on
them.
The values
.Dv O_NONBLOCK
and
.Dv O_NODELAY
affect the operation of STREAMS drivers and certain functions
.Po
see
.Xr read 2 ,
.Xr getmsg 2 ,
.Xr putmsg 2 ,
and
.Xr write 2
.Pc
applied to file descriptors associated with STREAMS files.
For STREAMS drivers, the implementation of
.Dv O_NONBLOCK
and
.Dv O_NODELAY
is device-specific.
.Pp
When
.Fn open
is invoked to open a named stream, and the
.Xr connld 4M
module has been pushed on the pipe,
.Fn open
blocks until the server process has issued an
.Dv I_RECVFD
.Xr ioctl 2
.Po
see
.Xr streamio 4I
.Pc
to receive the file descriptor.
.Pp
If
.Fa path
names the manager side of a pseudo-terminal device, then it is unspecified
whether
.Fn open
locks the subsidiary side so that it cannot be opened.
Portable applications must call
.Xr unlockpt 3C
before opening the subsidiary side.
.Pp
If the file is a regular file and the local file system is mounted with the
.Cm nbmand
mount option, then a mandatory share reservation is automatically obtained on
the file.
The share reservation is obtained as if
.Xr fcntl 2
were called with
.Fa cmd
.Dv F_SHARE_NBMAND
and the
.Vt fshare_t
values set as follows:
.Bl -tag -width Ds -offset Ds
.It Fa f_access
Set to the type of read/write access for which the file is opened.
.It Fa f_deny
.Dv F_NODNY
.It Fa f_id
The file descriptor value returned from
.Fn open .
.El
.Pp
If
.Fa path
is a symbolic link and
.Dv O_CREAT
and
.Dv O_EXCL
are set, the link is not followed.
.Pp
Certain flag values can be set following
.Fn open
as described in
.Xr fcntl 2 .
.Pp
The largest value that can be represented correctly in an object of type
.Vt off_t
is established as the offset maximum in the open file description.
.Sh RETURN VALUES
The
.Fn open
and
.Fn openat
functions open the file and, if successful, return a non-negative integer
representing the lowest numbered unused file descriptor; otherwise the
value
.Sy -1
is returned and the global variable
.Va errno
is set to indicate the error and no files are created or modified.
.Sh EXAMPLES
.Sy Example 1
Open a file for writing by the owner.
.Pp
The following example opens the file
.Pa /tmp/file ,
either by creating it if it does not already exist, or by truncating its length
to
.Sy 0
if it does exist.
If the call creates a new file, the access permission bits in the file mode of
the file are set to permit reading and writing by the owner, and to permit
reading only by group members and others.
.Pp
If the call to
.Fn open
is successful, the file is opened for writing.
.Bd -literal -offset Ds
#include <fcntl.h>
\&...
int fd;
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
char *filename = "/tmp/file";
\&...
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, mode);
\&...
.Ed
.Pp
.Sy Example 2
Open a file using an existence check.
.Pp
The following example uses the
.Fn open
function to try to create the
.Dv LOCKFILE
file and open it for writing.
Since the
.Fn open
function specifies the
.Dv O_EXCL
flag, the call fails if the file already exists.
In that case, the application assumes that someone else is updating the
password file and exits.
.Bd -literal -offset Ds
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
\&...
#define LOCKFILE "/etc/ptmp"
\&...
int pfd; /* Integer for file descriptor returned by open() call. */
\&...
if ((pfd = open(LOCKFILE, O_WRONLY | O_CREAT | O_EXCL,
    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
        err(1, "Cannot open %s. Try again later.", LOCKFILE);
}
\&...
.Ed
.Pp
.Sy Example 3
Open a file for writing.
.Pp
The following example opens a file for writing, creating the file if it does
not already exist.
If the file does exist, the system truncates the file to zero bytes.
.Bd -literal -offset Ds
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <err.h>
\&...
int pfd;
char filename[PATH_MAX+1];
\&...
if ((pfd = open(filename, O_WRONLY | O_CREAT | O_TRUNC,
    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
        err(1, "Cannot open output file");
}
\&...
.Ed
.Sh ERRORS
The
.Fn open
and
.Fn openat
functions will fail if:
.Bl -tag -width Er
.It Er EACCES
Search permission is denied on a component of the path prefix.
.Pp
The file exists and the permissions specified by
.Fa oflag
are denied.
.Pp
The file does not exist and write permission is denied for the parent directory
of the file to be created.
.Pp
.Dv O_TRUNC
is specified and write permission is denied.
.Pp
The
.Brq Dv PRIV_FILE_DAC_SEARCH
privilege allows processes to search directories regardless of permission bits.
The
.Brq Dv PRIV_FILE_DAC_WRITE
privilege allows processes to open files for writing regardless of permission
bits.
See
.Xr privileges 7
for special considerations when opening files owned by user ID
.Sy 0
for writing.
The
.Brq Dv PRIV_FILE_DAC_READ
privilege allows
processes to open files for reading regardless of permission bits.
.It Er EAGAIN
A mandatory share reservation could not be obtained because the desired access
conflicts with an existing
.Fa f_deny
share reservation
.Po
see
.Xr fcntl 2
.Pc .
.It Er EDQUOT
The file does not exist,
.Dv O_CREAT
is specified, and either the directory where the new file entry is being placed
cannot be extended because the user's quota of disk blocks on that file system
has been exhausted, or the user's quota of inodes on the file system where the
file is being created has been exhausted.
.It Er EEXIST
The
.Dv O_CREAT
and
.Dv O_EXCL
flags are set and the named file already exists.
.It Er EILSEQ
The
.Fa path
argument includes bytes that are not valid UTF-8 characters, and the file
system accepts only file names where all characters are part of the UTF-8
character codeset.
.It Er EINTR
A signal was caught during
.Fn open .
.It Er EFAULT
The
.Fa path
argument points to an illegal address.
.It Er EINVAL
Either the system does not support synchronized or direct I/O for this file, or
the
.Dv O_XATTR
flag was supplied and the underlying file system does not support extended file
attributes.
.It Er EIO
The
.Fa path
argument names a STREAMS file and a hangup or error occurred during the
.Fn open .
.It Er EISDIR
The named file is a directory and
.Fa oflag
includes
.Dv O_WRONLY
or
.Dv O_RDWR .
.It Er ELOOP
Too many symbolic links were encountered in resolving
.Fa path .
.Pp
A loop exists in symbolic links encountered during resolution of the
.Fa path
argument.
.Pp
The
.Dv O_NOFOLLOW
flag is set and the final component of path is a symbolic link.
.It Er EMFILE
There are currently
.Brq Dv OPEN_MAX
file descriptors open in the calling process.
.It Er EMLINK
The
.Dv O_NOLINKS
flag is set and the named file has a link count greater than
.Sy 1 .
.It Er EMULTIHOP
Components of
.Fa path
require hopping to multiple remote machines and the file system does not allow
it.
.It Er ENAMETOOLONG
The length of the
.Fa path
argument exceeds
.Brq Dv PATH_MAX
or a pathname component is longer than
.Brq Dv NAME_MAX .
.It Er ENFILE
The maximum allowable number of files is currently open in the system.
.It Er ENOENT
The
.Dv O_CREAT
flag is not set and the named file does not exist; or the
.Dv O_CREAT
flag is set and either the path prefix does not exist or the
.Fa path
argument points to an empty string.
.Pp
The
.Dv O_CREAT
and
.Dv O_DIRECTORY
flags were both set and
.Fa path
did not point to a file.
.It Er ENOEXEC
The
.Dv O_EXEC
flag is set and
.Fa path
does not point to a regular file.
.It Er ENOLINK
The
.Fa path
argument points to a remote machine, and the link to that machine is no longer
active.
.It Er ENOSR
Th
.Fa path
argument names a STREAMS-based file and the system is unable to allocate a
STREAM.
.It Er ENOSPC
The directory or file system that would contain the new file cannot be
expanded, the file does not exist, and
.Dv O_CREAT
is specified.
.It Er ENOSYS
The device specified by
.Fa path
does not support the open operation.
.It Er ENOTDIR
A component of the path prefix is not a directory or a relative path was
supplied to
.Fn openat ,
the
.Dv O_XATTR
flag was not supplied, and the file descriptor does not refer to a directory.
The
.Dv O_SEARCH
flag was passed and
.Fa path
does not refer to a directory.
.Pp
The
.Dv O_DIRECTORY
flag was set and the file was not a directory.
.It Er ENXIO
The
.Dv O_NONBLOCK
flag is set, the named file is a FIFO, the
.Dv O_WRONLY
flag is set, and no process has the file open for reading; or the named file is
a character special or block special file and the device associated with this
special file does not exist or has been retired by the fault management
framework.
.It Er EOPNOTSUPP
An attempt was made to open a path that corresponds to an
.Dv AF_UNIX
socket.
.It Er EOVERFLOW
The named file is a regular file and either
.Dv O_LARGEFILE
is not set and the size of the file cannot be represented correctly in an
object of type
.Vt off_t
or
.Dv O_LARGEFILE
is set and the size of the file cannot be represented correctly in an object of
type
.Vt off64_t .
.It Er EROFS
The named file resides on a read-only file system and either
.Dv O_WRONLY ,
.Dv O_RDWR ,
.Dv O_CREAT
(if file does not exist), or
.Dv O_TRUNC
is set in the
.Fa oflag
argument.
.El
.Pp
The
.Fn openat
function will fail if:
.Bl -tag -width Er
.It Er EBADF
The
.Fa fildes
argument is not a valid open file descriptor or is not
.Dv AT_FTCWD .
.El
.Pp
The
.Fn open
function may fail if:
.Bl -tag -width Er
.It Er EAGAIN
The
.Fa path
argument names the subsidiary side of a pseudo-terminal device that is locked.
.It Er EINVAL
The value of the
.Fa oflag
argument is not valid.
.It Er ENAMETOOLONG
Pathname resolution of a symbolic link produced an intermediate result whose
length exceeds
.Brq Dv PATH_MAX .
.It Er ENOMEM
The
.Fa path
argument names a STREAMS file and the system is unable to allocate resources.
.It Er ETXTBSY
The file is a pure procedure (shared text) file that is being executed and
.Fa oflag
is
.Dv O_WRONLY
or
.Dv O_RDWR .
.El
.Sh USAGE
The
.Fn open
function has a transitional interface for 64-bit file offsets.
See
.Xr lf64 7 .
Note that using
.Fn open64
is equivalent to using
.Fn open with
.Dv O_LARGEFILE
set in
.Fa oflag .
.Sh INTERFACE STABILITY
.Sy Committed
.Sh MT LEVEL
.Sy Async-Signal-Safe
.Sh SEE ALSO
.Xr chmod 2 ,
.Xr close 2 ,
.Xr creat 2 ,
.Xr dup 2 ,
.Xr exec 2 ,
.Xr fcntl 2 ,
.Xr getmsg 2 ,
.Xr getrlimit 2 ,
.Xr Intro 2 ,
.Xr lseek 2 ,
.Xr putmsg 2 ,
.Xr read 2 ,
.Xr stat 2 ,
.Xr umask 2 ,
.Xr write 2 ,
.Xr attropen 3C ,
.Xr directio 3C ,
.Xr unlockpt 3C ,
.Xr fcntl.h 3HEAD ,
.Xr stat.h 3HEAD ,
.Xr streamio 4I ,
.Xr connld 4M ,
.Xr attributes 7 ,
.Xr lf64 7 ,
.Xr privileges 7 ,
.Xr standards 7
.Sh NOTES
Hierarchical Storage Management
.Pq HSM
file systems can sometimes cause long delays when opening a file, since HSM
files must be recalled from secondary storage.