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
|
'\" te
.\" Copyright (c) 2008, Sun Microsystems, Inc. All Rights Reserved.
.\" Copyright 1989 AT&T
.\" Portions Copyright (c) 1992, X/Open Company Limited. All Rights Reserved.
.\" Portions Copyright (c) 2013, OmniTI Computer Consulting, Inc. All Rights Reserved.
.\" 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 Sun OS 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]
.TH OPEN 2 "Nov 16, 2013"
.SH NAME
open, openat \- open a file
.SH SYNOPSIS
.LP
.nf
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
\fBint\fR \fBopen\fR(\fBconst char *\fR\fIpath\fR, \fBint\fR \fIoflag\fR, \fB/* mode_t\fR \fImode\fR */);
.fi
.LP
.nf
\fBint\fR \fBopenat\fR(\fBint\fR \fIfildes\fR, \fBconst char *\fR\fIpath\fR, \fBint\fR \fIoflag\fR,
\fB/* mode_t\fR \fImode\fR */);
.fi
.SH DESCRIPTION
.sp
.LP
The \fBopen()\fR 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 \fIpath\fR argument
points to a pathname naming the file.
.sp
.LP
The \fBopenat()\fR function is identical to the \fBopen()\fR function except
that the \fIpath\fR argument is interpreted relative to the starting point
implied by the \fIfildes\fR argument. If the \fIfildes\fR argument has the
special value \fBAT_FDCWD\fR, a relative path argument will be resolved
relative to the current working directory. If the \fIpath\fR argument is
absolute, the \fIfildes\fR argument is ignored.
.sp
.LP
The \fBopen()\fR 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 \fBFD_CLOEXEC\fR file descriptor flag
associated with the new file descriptor is cleared.
.sp
.LP
The file offset used to mark the current position within the file is set to the
beginning of the file.
.sp
.LP
The file status flags and file access modes of the open file description are
set according to the value of \fIoflag\fR. The \fImode\fR argument is used only
when \fBO_CREAT\fR is specified (see below.)
.sp
.LP
Values for \fIoflag\fR are constructed by a bitwise-inclusive-OR of flags from
the following list, defined in <\fBfcntl.h\fR>. Applications must specify
exactly one of the first three values (file access modes) below in the value of
\fIoflag\fR:
.sp
.ne 2
.na
\fB\fBO_RDONLY\fR\fR
.ad
.RS 12n
Open for reading only.
.RE
.sp
.ne 2
.na
\fB\fBO_WRONLY\fR\fR
.ad
.RS 12n
Open for writing only.
.RE
.sp
.ne 2
.na
\fB\fBO_RDWR\fR\fR
.ad
.RS 12n
Open for reading and writing. The result is undefined if this flag is applied
to a FIFO.
.RE
.sp
.LP
Any combination of the following may be used:
.sp
.ne 2
.na
\fB\fBO_APPEND\fR\fR
.ad
.sp .6
.RS 4n
If set, the file offset is set to the end of the file prior to each write.
.RE
.sp
.ne 2
.na
\fB\fBO_CREAT\fR\fR
.ad
.sp .6
.RS 4n
Create the file if it does not exist. This flag requires that the \fImode\fR
argument be specified.
.sp
If the file exists, this flag has no effect except as noted under \fBO_EXCL\fR
below. Otherwise, the file is created with the user \fBID\fR of the file set
to the effective user \fBID\fR of the process. The group \fBID\fR of the file
is set to the effective group \fBIDs\fR of the process, or if the \fBS_ISGID\fR
bit is set in the directory in which the file is being created, the file's
group \fBID\fR is set to the group \fBID\fR of its parent directory. If the
group \fBID\fR of the new file does not match the effective group \fBID\fR or
one of the supplementary groups IDs, the \fBS_ISGID\fR bit is cleared. The
access permission bits (see \fB<sys/stat.h>\fR) of the file mode are set to the
value of \fImode\fR, modified as follows (see \fBcreat\fR(2)): 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 (see \fBumask\fR(2)) are correspondingly cleared in the
file's permission mask. The "save text image after execution bit" of the mode
is cleared (see \fBchmod\fR(2)). When bits other than the file permission bits
are set, the effect is unspecified. The \fImode\fR argument does not affect
whether the file is open for reading, writing or for both.
.RE
.sp
.ne 2
.na
\fB\fBO_DSYNC\fR\fR
.ad
.sp .6
.RS 4n
Write I/O operations on the file descriptor complete as defined by synchronized
I/O data integrity completion.
.RE
.sp
.ne 2
.na
\fB\fBO_EXCL\fR\fR
.ad
.sp .6
.RS 4n
If \fBO_CREAT\fR and \fBO_EXCL\fR are set, \fBopen()\fR 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
\fBopen()\fR naming the same filename in the same directory with \fBO_EXCL\fR
and \fBO_CREAT\fR set. If \fBO_EXCL\fR and \fBO_CREAT\fR are set, and path
names a symbolic link, \fBopen()\fR fails and sets \fBerrno\fR to \fBEEXIST\fR,
regardless of the contents of the symbolic link. If \fBO_EXCL\fR is set and
\fBO_CREAT\fR is not set, the result is undefined.
.RE
.sp
.ne 2
.na
\fB\fBO_LARGEFILE\fR\fR
.ad
.sp .6
.RS 4n
If set, the offset maximum in the open file description is the largest value
that can be represented correctly in an object of type \fBoff64_t\fR.
.RE
.sp
.ne 2
.na
\fB\fBO_NOCTTY\fR\fR
.ad
.sp .6
.RS 4n
If set and \fIpath\fR identifies a terminal device, \fBopen()\fR does not cause
the terminal device to become the controlling terminal for the process.
.RE
.sp
.ne 2
.na
\fB\fBO_NOFOLLOW\fR\fR
.ad
.sp .6
.RS 4n
If the path names a symbolic link, \fBopen()\fR fails and sets \fBerrno\fR to
\fBELOOP\fR.
.RE
.sp
.ne 2
.na
\fB\fBO_NOLINKS\fR\fR
.ad
.sp .6
.RS 4n
If the link count of the named file is greater than 1, \fBopen()\fR fails and
sets \fBerrno\fR to \fBEMLINK\fR.
.RE
.sp
.ne 2
.na
\fB\fBO_CLOEXEC\fR\fR
.ad
.sp .6
.RS 4n
If set, the file descriptor returned will be closed prior to any future
\fBexec()\fR calls.
.RE
.sp
.ne 2
.na
\fB\fBO_NONBLOCK\fR or \fBO_NDELAY\fR\fR
.ad
.sp .6
.RS 4n
These flags can affect subsequent reads and writes (see \fBread\fR(2) and
\fBwrite\fR(2)). If both \fBO_NDELAY\fR and \fBO_NONBLOCK\fR are set,
\fBO_NONBLOCK\fR takes precedence.
.sp
When opening a \fBFIFO\fR with \fBO_RDONLY\fR or \fBO_WRONLY\fR set:
.RS +4
.TP
.ie t \(bu
.el o
If \fBO_NONBLOCK\fR or \fBO_NDELAY\fR is set, an \fBopen()\fR for reading only
returns without delay. An \fBopen()\fR for writing only returns an error if no
process currently has the file open for reading.
.RE
.RS +4
.TP
.ie t \(bu
.el o
If \fBO_NONBLOCK\fR and \fBO_NDELAY\fR are clear, an \fBopen()\fR for reading
only blocks until a thread opens the file for writing. An \fBopen()\fR for
writing only blocks the calling thread until a thread opens the file for
reading.
.RE
After both ends of a \fBFIFO\fR have been opened, there is no guarantee that
further calls to \fBopen()\fR \fBO_RDONLY\fR (\fBO_WRONLY\fR) will synchronize
with later calls to \fBopen()\fR \fBO_WRONLY\fR (\fBO_RDONLY\fR) until both
ends of the \fBFIFO\fR have been closed by all readers and writers. Any data
written into a \fBFIFO\fR will be lost if both ends of the \fBFIFO\fR are
closed before the data is read.
.sp
When opening a block special or character special file that supports
non-blocking opens:
.RS +4
.TP
.ie t \(bu
.el o
If \fBO_NONBLOCK\fR or \fBO_NDELAY\fR is set, the \fBopen()\fR function returns
without blocking for the device to be ready or available. Subsequent behavior
of the device is device-specific.
.RE
.RS +4
.TP
.ie t \(bu
.el o
If \fBO_NONBLOCK\fR and \fBO_NDELAY\fR are clear, the \fBopen()\fR function
blocks the calling thread until the device is ready or available before
returning.
.RE
Otherwise, the behavior of \fBO_NONBLOCK\fR and \fBO_NDELAY\fR is unspecified.
.RE
.sp
.ne 2
.na
\fB\fBO_RSYNC\fR\fR
.ad
.sp .6
.RS 4n
Read I/O operations on the file descriptor complete at the same level of
integrity as specified by the \fBO_DSYNC\fR and \fBO_SYNC\fR flags. If both
\fBO_DSYNC\fR and \fBO_RSYNC\fR are set in \fIoflag\fR, all I/O operations on
the file descriptor complete as defined by synchronized I/O data integrity
completion. If both \fBO_SYNC\fR and \fBO_RSYNC\fR are set in \fIoflag\fR, all
I/O operations on the file descriptor complete as defined by synchronized I/O
file integrity completion.
.RE
.sp
.ne 2
.na
\fB\fBO_SYNC\fR\fR
.ad
.sp .6
.RS 4n
Write I/O operations on the file descriptor complete as defined by synchronized
I/O file integrity completion (see \fBfcntl.h\fR(3HEAD) definition of
\fBO_SYNC\fR).
.RE
.sp
.ne 2
.na
\fB\fBO_TRUNC\fR\fR
.ad
.sp .6
.RS 4n
If the file exists and is a regular file, and the file is successfully opened
\fBO_RDWR\fR or \fBO_WRONLY\fR, its length is truncated to 0 and the mode and
owner are unchanged. It has no effect on \fBFIFO\fR special files or terminal
device files. Its effect on other file types is implementation-dependent. The
result of using \fBO_TRUNC\fR with \fBO_RDONLY\fR is undefined.
.RE
.sp
.ne 2
.na
\fB\fBO_XATTR\fR\fR
.ad
.sp .6
.RS 4n
If set in \fBopenat()\fR, 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
\fIfildes\fR argument. If set in \fBopen()\fR, 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.
.RE
.sp
.LP
If \fBO_CREAT\fR is set and the file did not previously exist, upon successful
completion, \fBopen()\fR marks for update the \fBst_atime\fR, \fBst_ctime\fR,
and \fBst_mtime\fR fields of the file and the \fBst_ctime\fR and \fBst_mtime\fR
fields of the parent directory.
.sp
.LP
If \fBO_TRUNC\fR is set and the file did previously exist, upon successful
completion, \fBopen()\fR marks for update the \fBst_ctime\fR and \fBst_mtime\fR
fields of the file.
.sp
.LP
If both the \fBO_SYNC\fR and \fBO_DSYNC\fR flags are set, the effect is as if
only the \fBO_SYNC\fR flag was set.
.sp
.LP
If \fIpath\fR refers to a \fBSTREAMS\fR file, \fIoflag\fR may be constructed
from \fBO_NONBLOCK\fR or \fBO_NODELAY\fR OR-ed with either \fBO_RDONLY\fR,
\fBO_WRONLY\fR, or \fBO_RDWR\fR. Other flag values are not applicable to
\fBSTREAMS\fR devices and have no effect on them. The values \fBO_NONBLOCK\fR
and \fBO_NODELAY\fR affect the operation of \fBSTREAMS\fR drivers and certain
functions (see \fBread\fR(2), \fBgetmsg\fR(2), \fBputmsg\fR(2), and
\fBwrite\fR(2)) applied to file descriptors associated with \fBSTREAMS\fR
files. For \fBSTREAMS\fR drivers, the implementation of \fBO_NONBLOCK\fR and
\fBO_NODELAY\fR is device-specific.
.sp
.LP
When \fBopen()\fR is invoked to open a named stream, and the \fBconnld\fR
module (see \fBconnld\fR(7M)) has been pushed on the pipe, \fBopen()\fR blocks
until the server process has issued an \fBI_RECVFD\fR \fBioctl()\fR (see
\fBstreamio\fR(7I)) to receive the file descriptor.
.sp
.LP
If \fIpath\fR names the master side of a pseudo-terminal device, then it is
unspecified whether \fBopen()\fR locks the slave side so that it cannot be
opened. Portable applications must call \fBunlockpt\fR(3C) before opening the
slave side.
.sp
.LP
If the file is a regular file and the local file system is mounted with the
\fBnbmand\fR mount option, then a mandatory share reservation is automatically
obtained on the file. The share reservation is obtained as if \fBfcntl\fR(2)
were called with \fIcmd\fR \fBF_SHARE_NBMAND\fR and the \fBfshare_t\fR values
set as follows:
.sp
.ne 2
.na
\fB\fBf_access\fR\fR
.ad
.RS 12n
Set to the type of read/write access for which the file is opened.
.RE
.sp
.ne 2
.na
\fB\fBf_deny\fR\fR
.ad
.RS 12n
\fBF_NODNY\fR
.RE
.sp
.ne 2
.na
\fB\fBf_id\fR\fR
.ad
.RS 12n
The file descriptor value returned from \fBopen()\fR.
.RE
.sp
.LP
If \fIpath\fR is a symbolic link and \fBO_CREAT\fR and \fBO_EXCL\fR are set,
the link is not followed.
.sp
.LP
Certain flag values can be set following \fBopen()\fR as described in
\fBfcntl\fR(2).
.sp
.LP
The largest value that can be represented correctly in an object of type
\fBoff_t\fR is established as the offset maximum in the open file description.
.SH RETURN VALUES
.sp
.LP
Upon successful completion, the \fBopen()\fR function opens the file and return
a non-negative integer representing the lowest numbered unused file descriptor.
Otherwise, \fB\(mi1\fR is returned, \fBerrno\fR is set to indicate the error,
and no files are created or modified.
.SH ERRORS
.sp
.LP
The \fBopen()\fR and \fBopenat()\fR functions will fail if:
.sp
.ne 2
.na
\fB\fBEACCES\fR\fR
.ad
.RS 16n
Search permission is denied on a component of the path prefix.
.sp
The file exists and the permissions specified by \fIoflag\fR are denied.
.sp
The file does not exist and write permission is denied for the parent directory
of the file to be created.
.sp
\fBO_TRUNC\fR is specified and write permission is denied.
.sp
The {\fBPRIV_FILE_DAC_SEARCH\fR} privilege allows processes to search
directories regardless of permission bits. The {\fBPRIV_FILE_DAC_WRITE\fR}
privilege allows processes to open files for writing regardless of permission
bits. See \fBprivileges\fR(5) for special considerations when opening files
owned by UID 0 for writing. The {\fBPRIV_FILE_DAC_READ\fR} privilege allows
processes to open files for reading regardless of permission bits.
.RE
.sp
.ne 2
.na
\fB\fBEAGAIN\fR\fR
.ad
.RS 16n
A mandatory share reservation could not be obtained because the desired access
conflicts with an existing \fBf_deny\fR share reservation.
.RE
.sp
.ne 2
.na
\fB\fBEBADF\fR\fR
.ad
.RS 16n
The file descriptor provided to \fBopenat()\fR is invalid.
.RE
.sp
.ne 2
.na
\fB\fBEDQUOT\fR\fR
.ad
.RS 16n
The file does not exist, \fBO_CREAT\fR 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.
.RE
.sp
.ne 2
.na
\fB\fBEEXIST\fR\fR
.ad
.RS 16n
The \fBO_CREAT\fR and \fBO_EXCL\fR flags are set and the named file exists.
.RE
.sp
.ne 2
.na
\fB\fBEILSEQ\fR\fR
.ad
.RS 16n
The \fIpath\fR argument includes non-UTF8 characters and the file system
accepts only file names where all characters are part of the UTF-8 character
codeset.
.RE
.sp
.ne 2
.na
\fB\fBEINTR\fR\fR
.ad
.RS 16n
A signal was caught during \fBopen()\fR.
.RE
.sp
.ne 2
.na
\fB\fBEFAULT\fR\fR
.ad
.RS 16n
The \fIpath\fR argument points to an illegal address.
.RE
.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 16n
The system does not support synchronized I/O for this file, or the
\fBO_XATTR\fR flag was supplied and the underlying file system does not support
extended file attributes.
.RE
.sp
.ne 2
.na
\fB\fBEIO\fR\fR
.ad
.RS 16n
The \fIpath\fR argument names a \fBSTREAMS\fR file and a hangup or error
occurred during the \fBopen()\fR.
.RE
.sp
.ne 2
.na
\fB\fBEISDIR\fR\fR
.ad
.RS 16n
The named file is a directory and \fIoflag\fR includes \fBO_WRONLY\fR or
\fBO_RDWR\fR.
.RE
.sp
.ne 2
.na
\fB\fBELOOP\fR\fR
.ad
.RS 16n
Too many symbolic links were encountered in resolving \fIpath\fR.
.sp
A loop exists in symbolic links encountered during resolution of the \fIpath\fR
argument.
.sp
The \fBO_NOFOLLOW\fR flag is set and the final component of path is a symbolic
link.
.RE
.sp
.ne 2
.na
\fB\fBEMFILE\fR\fR
.ad
.RS 16n
There are currently {\fBOPEN_MAX\fR} file descriptors open in the calling
process.
.RE
.sp
.ne 2
.na
\fB\fBEMLINK\fR\fR
.ad
.RS 16n
The \fBO_NOLINKS\fR flag is set and the named file has a link count greater
than 1.
.RE
.sp
.ne 2
.na
\fB\fBEMULTIHOP\fR\fR
.ad
.RS 16n
Components of \fIpath\fR require hopping to multiple remote machines and the
file system does not allow it.
.RE
.sp
.ne 2
.na
\fB\fBENAMETOOLONG\fR\fR
.ad
.RS 16n
The length of the \fIpath\fR argument exceeds {\fBPATH_MAX\fR} or a pathname
component is longer than {\fBNAME_MAX\fR}.
.RE
.sp
.ne 2
.na
\fB\fBENFILE\fR\fR
.ad
.RS 16n
The maximum allowable number of files is currently open in the system.
.RE
.sp
.ne 2
.na
\fB\fBENOENT\fR\fR
.ad
.RS 16n
The \fBO_CREAT\fR flag is not set and the named file does not exist; or the
\fBO_CREAT\fR flag is set and either the path prefix does not exist or the
\fIpath\fR argument points to an empty string.
.RE
.sp
.ne 2
.na
\fB\fBENOLINK\fR\fR
.ad
.RS 16n
The \fIpath\fR argument points to a remote machine, and the link to that
machine is no longer active.
.RE
.sp
.ne 2
.na
\fB\fBENOSR\fR\fR
.ad
.RS 16n
The \fIpath\fR argument names a STREAMS-based file and the system is unable to
allocate a STREAM.
.RE
.sp
.ne 2
.na
\fB\fBENOSPC\fR\fR
.ad
.RS 16n
The directory or file system that would contain the new file cannot be
expanded, the file does not exist, and \fBO_CREAT\fR is specified.
.RE
.sp
.ne 2
.na
\fB\fBENOSYS\fR\fR
.ad
.RS 16n
The device specified by \fIpath\fR does not support the open operation.
.RE
.sp
.ne 2
.na
\fB\fBENOTDIR\fR\fR
.ad
.RS 16n
A component of the path prefix is not a directory or a relative path was
supplied to \fBopenat()\fR, the \fBO_XATTR\fR flag was not supplied, and the
file descriptor does not refer to a directory.
.RE
.sp
.ne 2
.na
\fB\fBENXIO\fR\fR
.ad
.RS 16n
The \fBO_NONBLOCK\fR flag is set, the named file is a FIFO, the \fBO_WRONLY\fR
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 .
.RE
.sp
.ne 2
.na
\fB\fBEOPNOTSUPP\fR\fR
.ad
.RS 16n
An attempt was made to open a path that corresponds to a \fBAF_UNIX\fR socket.
.RE
.sp
.ne 2
.na
\fB\fBEOVERFLOW\fR\fR
.ad
.RS 16n
The named file is a regular file and either \fBO_LARGEFILE\fR is not set and
the size of the file cannot be represented correctly in an object of type
\fBoff_t\fR or \fBO_LARGEFILE\fR is set and the size of the file cannot be
represented correctly in an object of type \fBoff64_t\fR.
.RE
.sp
.ne 2
.na
\fB\fBEROFS\fR\fR
.ad
.RS 16n
The named file resides on a read-only file system and either \fBO_WRONLY\fR,
\fBO_RDWR\fR, \fBO_CREAT\fR (if file does not exist), or \fBO_TRUNC\fR is set
in the \fIoflag\fR argument.
.RE
.sp
.LP
The \fBopenat()\fR function will fail if:
.sp
.ne 2
.na
\fB\fBEBADF\fR\fR
.ad
.RS 9n
The \fIfildes\fR argument is not a valid open file descriptor or is not
\fBAT_FTCWD\fR.
.RE
.sp
.LP
The \fBopen()\fR function may fail if:
.sp
.ne 2
.na
\fB\fBEAGAIN\fR\fR
.ad
.RS 16n
The \fIpath\fR argument names the slave side of a pseudo-terminal device that
is locked.
.RE
.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 16n
The value of the \fIoflag\fR argument is not valid.
.RE
.sp
.ne 2
.na
\fB\fBENAMETOOLONG\fR\fR
.ad
.RS 16n
Pathname resolution of a symbolic link produced an intermediate result whose
length exceeds {\fBPATH_MAX\fR}.
.RE
.sp
.ne 2
.na
\fB\fBENOMEM\fR\fR
.ad
.RS 16n
The \fIpath\fR argument names a \fBSTREAMS\fR file and the system is unable to
allocate resources.
.RE
.sp
.ne 2
.na
\fB\fBETXTBSY\fR\fR
.ad
.RS 16n
The file is a pure procedure (shared text) file that is being executed and
\fIoflag\fR is \fBO_WRONLY\fR or \fBO_RDWR\fR.
.RE
.SH EXAMPLES
.LP
\fBExample 1 \fROpen a file for writing by the owner.
.sp
.LP
The following example opens the file \fB/tmp/file\fR, either by creating it if
it does not already exist, or by truncating its length to 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.
.sp
.LP
If the call to \fBopen()\fR is successful, the file is opened for writing.
.sp
.in +2
.nf
#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);
\&...
.fi
.in -2
.LP
\fBExample 2 \fROpen a file using an existence check.
.sp
.LP
The following example uses the \fBopen()\fR function to try to create the
\fBLOCKFILE\fR file and open it for writing. Since the \fBopen()\fR function
specifies the \fBO_EXCL\fR 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.
.sp
.in +2
.nf
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.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)) == -1)
{
fprintf(stderr, "Cannot open /etc/ptmp. Try again later.\en");
exit(1);
}
\&...
.fi
.in -2
.LP
\fBExample 3 \fROpen a file for writing.
.sp
.LP
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.
.sp
.in +2
.nf
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#define LOCKFILE "/etc/ptmp"
\&...
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)) == -1)
{
perror("Cannot open output file\en"); exit(1);
}
\&...
.fi
.in -2
.SH USAGE
.sp
.LP
The \fBopen()\fR function has a transitional interface for 64-bit file offsets.
See \fBlf64\fR(5). Note that using \fBopen64()\fR is equivalent to using
\fBopen()\fR with \fBO_LARGEFILE\fR set in \fIoflag\fR.
.SH ATTRIBUTES
.sp
.LP
See \fBattributes\fR(5) for descriptions of the following attributes:
.sp
.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE ATTRIBUTE VALUE
_
Interface Stability Committed
_
MT-Level Async-Signal-Safe
_
Standard For \fBopen()\fR, see \fBstandards\fR(5).
.TE
.SH SEE ALSO
.sp
.LP
\fBIntro\fR(2), \fBchmod\fR(2), \fBclose\fR(2), \fBcreat\fR(2), \fBdup\fR(2),
\fBexec\fR(2), \fBfcntl\fR(2), \fBgetmsg\fR(2), \fBgetrlimit\fR(2),
\fBlseek\fR(2), \fBputmsg\fR(2), \fBread\fR(2), \fBstat\fR(2), \fBumask\fR(2),
\fBwrite\fR(2), \fBattropen\fR(3C), \fBfcntl.h\fR(3HEAD), \fBstat.h\fR(3HEAD),
\fBunlockpt\fR(3C), \fBattributes\fR(5), \fBlf64\fR(5), \fBprivileges\fR(5),
\fBstandards\fR(5), \fBconnld\fR(7M), \fBstreamio\fR(7I)
.SH NOTES
.sp
.LP
Hierarchical Storage Management (HSM) file systems can sometimes cause long
delays when opening a file, since HSM files must be recalled from secondary
storage.
|