xref: /freebsd-14.2/lib/libc/sys/open.2 (revision 9b50d902)
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. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     @(#)open.2	8.2 (Berkeley) 11/16/93
33.\"
34.Dd November 16, 1993
35.Dt OPEN 2
36.Os BSD 4
37.Sh NAME
38.Nm open
39.Nd open or create a file for reading or writing
40.Sh SYNOPSIS
41.Fd #include <fcntl.h>
42.Ft int
43.Fn open "const char *path" "int flags" "mode_t mode"
44.Sh DESCRIPTION
45The file name specified by
46.Fa path
47is opened
48for reading and/or writing as specified by the
49argument
50.Fa flags
51and the file descriptor returned to the calling process.
52The
53.Fa flags
54argument may indicate the file is to be
55created if it does not exist (by specifying the
56.Dv O_CREAT
57flag), in which case the file is created with mode
58.Fa mode
59as described in
60.Xr chmod 2
61and modified by the process' umask value (see
62.Xr umask 2 ) .
63.Pp
64The flags specified are formed by
65.Em or Ns 'ing
66the following values
67.Pp
68.Bd -literal -offset indent -compact
69O_RDONLY	open for reading only
70O_WRONLY	open for writing only
71O_RDWR		open for reading and writing
72O_NONBLOCK	do not block on open
73O_APPEND	append on each write
74O_CREAT		create file if it does not exist
75O_TRUNC		truncate size to 0
76O_EXCL		error if create and file exists
77O_SHLOCK	atomically obtain a shared lock
78O_EXLOCK	atomically obtain an exclusive lock
79.Ed
80.Pp
81Opening a file with
82.Dv O_APPEND
83set causes each write on the file
84to be appended to the end.  If
85.Dv O_TRUNC
86is specified and the
87file exists, the file is truncated to zero length.
88If
89.Dv O_EXCL
90is set with
91.Dv O_CREAT
92and the file already
93exists,
94.Fn open
95returns an error.  This may be used to
96implement a simple exclusive access locking mechanism.
97If
98.Dv O_EXCL
99is set and the last component of the pathname is
100a symbolic link,
101.Fn open
102will fail even if the symbolic
103link points to a non-existent name.
104If the
105.Dv O_NONBLOCK
106flag is specified and the
107.Fn open
108call would result
109in the process being blocked for some reason (e.g., waiting for
110carrier on a dialup line),
111.Fn open
112returns immediately.
113The first time the process attempts to perform I/O on the open
114file it will block (not currently implemented).
115.Pp
116When opening a file, a lock with
117.Xr flock 2
118semantics can be obtained by setting
119.Dv O_SHLOCK
120for a shared lock, or
121.Dv O_EXLOCK
122for an exclusive lock.
123If creating a file with
124.Dv O_CREAT ,
125the request for the lock will never fail
126(provided that the underlying filesystem supports locking).
127.Pp
128If successful,
129.Fn open
130returns a non-negative integer, termed a file descriptor.
131It returns -1 on failure.
132The file pointer used to mark the current position within the
133file is set to the beginning of the file.
134.Pp
135When a new file is created it is given the group of the directory
136which contains it.
137.Pp
138The new descriptor is set to remain open across
139.Xr execve
140system calls; see
141.Xr close 2
142and
143.Xr fcntl 2 .
144.Pp
145The system imposes a limit on the number of file descriptors
146open simultaneously by one process.
147.Xr Getdtablesize 2
148returns the current system limit.
149.Sh ERRORS
150The named file is opened unless:
151.Bl -tag -width Er
152.It Bq Er ENOTDIR
153A component of the path prefix is not a directory.
154.It Bq Er ENAMETOOLONG
155A component of a pathname exceeded 255 characters,
156or an entire path name exceeded 1023 characters.
157.It Bq Er ENOENT
158.Dv O_CREAT
159is not set and the named file does not exist.
160.It Bq Er ENOENT
161A component of the path name that must exist does not exist.
162.It Bq Er EACCES
163Search permission is denied for a component of the path prefix.
164.It Bq Er EACCES
165The required permissions (for reading and/or writing)
166are denied for the given flags.
167.It Bq Er EACCES
168.Dv O_CREAT
169is specified,
170the file does not exist,
171and the directory in which it is to be created
172does not permit writing.
173.It Bq Er ELOOP
174Too many symbolic links were encountered in translating the pathname.
175.It Bq Er EISDIR
176The named file is a directory, and the arguments specify
177it is to be opened for writing.
178.It Bq Er EROFS
179The named file resides on a read-only file system,
180and the file is to be modified.
181.It Bq Er EMFILE
182The process has already reached its limit for open file descriptors.
183.It Bq Er ENFILE
184The system file table is full.
185.It Bq Er ENXIO
186The named file is a character special or block
187special file, and the device associated with this special file
188does not exist.
189.It Bq Er EINTR
190The
191.Nm
192operation was interrupted by a signal.
193.It Bq Er EOPNOTSUPP
194.Dv O_SHLOCK
195or
196.Dv O_EXLOCK
197is specified but the underlying filesystem does not support locking.
198.It Bq Er ENOSPC
199.Dv O_CREAT
200is specified,
201the file does not exist,
202and the directory in which the entry for the new file is being placed
203cannot be extended because there is no space left on the file
204system containing the directory.
205.It Bq Er ENOSPC
206.Dv O_CREAT
207is specified,
208the file does not exist,
209and there are no free inodes on the file system on which the
210file is being created.
211.It Bq Er EDQUOT
212.Dv O_CREAT
213is specified,
214the file does not exist,
215and the directory in which the entry for the new file
216is being placed cannot be extended because the
217user's quota of disk blocks on the file system
218containing the directory has been exhausted.
219.It Bq Er EDQUOT
220.Dv O_CREAT
221is specified,
222the file does not exist,
223and the user's quota of inodes on the file system on
224which the file is being created has been exhausted.
225.It Bq Er EIO
226An I/O error occurred while making the directory entry or
227allocating the inode for
228.Dv O_CREAT .
229.It Bq Er ETXTBSY
230The file is a pure procedure (shared text) file that is being
231executed and the
232.Fn open
233call requests write access.
234.It Bq Er EFAULT
235.Fa Path
236points outside the process's allocated address space.
237.It Bq Er EEXIST
238.Dv O_CREAT
239and
240.Dv O_EXCL
241were specified and the file exists.
242.It Bq Er EOPNOTSUPP
243An attempt was made to open a socket (not currently implemented).
244.El
245.Sh SEE ALSO
246.Xr chmod 2 ,
247.Xr close 2 ,
248.Xr dup 2 ,
249.Xr getdtablesize 2 ,
250.Xr lseek 2 ,
251.Xr read 2 ,
252.Xr write 2 ,
253.Xr umask 2
254.Sh HISTORY
255An
256.Nm
257function call appeared in Version 6 AT&T UNIX.
258