xref: /openbsd/lib/libc/sys/open.2 (revision 891d7ab6)
1.\"	$OpenBSD: open.2,v 1.36 2011/02/18 13:22:53 millert Exp $
2.\"	$NetBSD: open.2,v 1.8 1995/02/27 12:35:14 cgd Exp $
3.\"
4.\" Copyright (c) 1980, 1991, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\"     @(#)open.2	8.2 (Berkeley) 11/16/93
32.\"
33.Dd $Mdocdate: February 18 2011 $
34.Dt OPEN 2
35.Os
36.Sh NAME
37.Nm open
38.Nd open or create a file for reading or writing
39.Sh SYNOPSIS
40.Fd #include <fcntl.h>
41.Ft int
42.Fn open "const char *path" "int flags" "mode_t mode"
43.Sh DESCRIPTION
44The file name specified by
45.Fa path
46is opened
47for reading and/or writing as specified by the
48argument
49.Fa flags
50and the file descriptor returned to the calling process.
51The
52.Fa flags
53argument may indicate the file is to be
54created if it does not exist (by specifying the
55.Dv O_CREAT
56flag), in which case the file is created with mode
57.Fa mode
58as described in
59.Xr chmod 2
60and modified by the process' umask value (see
61.Xr umask 2 ) .
62.Pp
63The flags specified are formed by
64.Tn OR Ns 'ing
65the following values:
66.Pp
67.Bl -tag -width O_NONBLOCK -offset indent -compact
68.It Dv O_RDONLY
69Open for reading only.
70.It Dv O_WRONLY
71Open for writing only.
72.It Dv O_RDWR
73Open for reading and writing.
74.It Dv O_NONBLOCK
75Do not block on open or for data to become available.
76.It Dv O_APPEND
77Append on each write.
78.It Dv O_CREAT
79Create file if it does not exist.
80.It Dv O_TRUNC
81Truncate size to 0.
82.It Dv O_EXCL
83Error if create and file exists.
84.It Dv O_SYNC
85Perform synchronous I/O operations.
86.It Dv O_SHLOCK
87Atomically obtain a shared lock.
88.It Dv O_EXLOCK
89Atomically obtain an exclusive lock.
90.It Dv O_NOFOLLOW
91If last path element is a symlink, don't follow it.
92.El
93.Pp
94Opening a file with
95.Dv O_APPEND
96set causes each write on the file
97to be appended to the end.
98If
99.Dv O_TRUNC
100and a writing mode are specified and the
101file exists, the file is truncated to zero length.
102If
103.Dv O_EXCL
104is set with
105.Dv O_CREAT
106and the file already
107exists,
108.Fn open
109returns an error.
110This may be used to implement a simple exclusive access locking mechanism.
111If either of
112.Dv O_EXCL
113or
114.Dv O_NOFOLLOW
115are set and the last component of the pathname is
116a symbolic link,
117.Fn open
118will fail even if the symbolic
119link points to a non-existent name.
120If the
121.Dv O_NONBLOCK
122flag is specified, do not wait for the device or file to be ready or
123available.
124If the
125.Fn open
126call would result
127in the process being blocked for some reason (e.g., waiting for
128carrier on a dialup line),
129.Fn open
130returns immediately.
131This flag also has the effect of making all subsequent I/O on the open file
132non-blocking.
133If the
134.Dv O_SYNC
135flag is set, all I/O operations on the file will be done synchronously.
136.Pp
137A FIFO should either be opened with
138.Dv O_RDONLY
139or with
140.Dv O_WRONLY .
141The behavior for opening a FIFO with
142.Dv O_RDWR
143is undefined.
144.Pp
145When opening a file, a lock with
146.Xr flock 2
147semantics can be obtained by setting
148.Dv O_SHLOCK
149for a shared lock, or
150.Dv O_EXLOCK
151for an exclusive lock.
152If creating a file with
153.Dv O_CREAT ,
154the request for the lock will never fail
155(provided that the underlying filesystem supports locking).
156.Pp
157If
158.Fn open
159is successful, the file pointer used to mark the current position within
160the file is set to the beginning of the file.
161.Pp
162When a new file is created it is given the group of the directory
163which contains it.
164.Pp
165The new descriptor is set to remain open across
166.Xr execve 2
167system calls; see
168.Xr close 2
169and
170.Xr fcntl 2 .
171.Pp
172The system imposes a limit on the number of file descriptors
173open simultaneously by one process.
174.Xr getdtablesize 3
175returns the current system limit.
176.Sh RETURN VALUES
177If successful,
178.Fn open
179returns a non-negative integer, termed a file descriptor.
180Otherwise, a value of \-1 is returned and
181.Va errno
182is set to indicate the error.
183.Sh ERRORS
184The named file is opened unless:
185.Bl -tag -width Er
186.It Bq Er ENOTDIR
187A component of the path prefix is not a directory.
188.It Bq Er ENAMETOOLONG
189A component of a pathname exceeded
190.Dv {NAME_MAX}
191characters, or an entire path name exceeded
192.Dv {PATH_MAX}
193characters.
194.It Bq Er ENOENT
195.Dv O_CREAT
196is not set and the named file does not exist.
197.It Bq Er ENOENT
198A component of the path name that must exist does not exist.
199.It Bq Er EACCES
200Search permission is denied for a component of the path prefix.
201.It Bq Er EACCES
202The required permissions (for reading and/or writing)
203are denied for the given flags.
204.It Bq Er EACCES
205.Dv O_CREAT
206is specified,
207the file does not exist,
208and the directory in which it is to be created
209does not permit writing.
210.It Bq Er ELOOP
211Too many symbolic links were encountered in translating the pathname,
212or the
213.Dv O_NOFOLLOW
214flag was specified and the target is a symbolic link.
215.It Bq Er EISDIR
216The named file is a directory, and the arguments specify
217it is to be opened for writing.
218.It Bq Er EINVAL
219The flags specified for opening the file are not valid.
220.It Bq Er EROFS
221The named file resides on a read-only file system,
222and the file is to be modified.
223.It Bq Er EMFILE
224The process has already reached its limit for open file descriptors.
225.It Bq Er ENFILE
226The system file table is full.
227.It Bq Er ENXIO
228The named file is a character special or block
229special file, and the device associated with this special file
230does not exist.
231.It Bq Er ENXIO
232The named file is a FIFO, the
233.Dv O_NONBLOCK
234and
235.Dv O_WRONLY
236flags are set, and no process has the file open for reading.
237.It Bq Er EINTR
238The
239.Fn open
240operation was interrupted by a signal.
241.It Bq Er EOPNOTSUPP
242.Dv O_SHLOCK
243or
244.Dv O_EXLOCK
245is specified but the underlying filesystem does not support locking.
246.It Bq Er EWOULDBLOCK
247.Dv O_NONBLOCK
248and one of
249.Dv O_SHLOCK
250or
251.Dv O_EXLOCK
252is specified and the file is already locked.
253.It Bq Er ENOSPC
254.Dv O_CREAT
255is specified,
256the file does not exist,
257and the directory in which the entry for the new file is being placed
258cannot be extended because there is no space left on the file
259system containing the directory.
260.It Bq Er ENOSPC
261.Dv O_CREAT
262is specified,
263the file does not exist,
264and there are no free inodes on the file system on which the
265file is being created.
266.It Bq Er EDQUOT
267.Dv O_CREAT
268is specified,
269the file does not exist,
270and the directory in which the entry for the new file
271is being placed cannot be extended because the
272user's quota of disk blocks on the file system
273containing the directory has been exhausted.
274.It Bq Er EDQUOT
275.Dv O_CREAT
276is specified,
277the file does not exist,
278and the user's quota of inodes on the file system on
279which the file is being created has been exhausted.
280.It Bq Er EIO
281An I/O error occurred while making the directory entry or
282allocating the inode for
283.Dv O_CREAT .
284.It Bq Er ETXTBSY
285The file is a pure procedure (shared text) file that is being
286executed and the
287.Fn open
288call requests write access.
289.It Bq Er EFAULT
290.Fa path
291points outside the process's allocated address space.
292.It Bq Er EEXIST
293.Dv O_CREAT
294and
295.Dv O_EXCL
296were specified and the file exists.
297.It Bq Er EPERM
298The file named by
299.Fa path
300is flagged append-only but
301.Dv O_APPEND
302was not specified in
303.Fa flags .
304.It Bq Er EOPNOTSUPP
305An attempt was made to open a socket (not currently implemented).
306.It Bq Er EBUSY
307An attempt was made to open a terminal device that requires exclusive
308access and the specified device has already be opened.
309.El
310.Sh SEE ALSO
311.Xr chflags 2 ,
312.Xr chmod 2 ,
313.Xr close 2 ,
314.Xr dup 2 ,
315.Xr flock 2 ,
316.Xr lseek 2 ,
317.Xr read 2 ,
318.Xr umask 2 ,
319.Xr write 2 ,
320.Xr getdtablesize 3
321.Sh STANDARDS
322The
323.Fn open
324function conforms to
325.St -p1003.1-2008 .
326.Pp
327.Dv POSIX
328specifies three different flavors for synchronous I/O:
329.Dv O_SYNC ,
330.Dv O_DSYNC ,
331and
332.Dv O_RSYNC .
333In
334.Ox ,
335these are all equivalent.
336.Pp
337The
338.Dv O_SHLOCK
339and
340.Dv O_EXLOCK
341flags are non-standard extensions and should not be used if portability
342is of concern.
343.Sh HISTORY
344An
345.Fn open
346function call appeared in
347.At v2 .
348.Sh CAVEATS
349The
350.Dv O_TRUNC
351flag requires that one of
352.Dv O_RDWR
353or
354.Dv O_WRONLY
355also be specified, else
356.Dv EINVAL
357is returned.
358