Deleted Added
sdiff udiff text old ( 165903 ) new ( 178245 )
full compact
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.\" 4. 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.\" @(#)open.2 8.2 (Berkeley) 11/16/93
29.\" $FreeBSD: head/lib/libc/sys/open.2 165903 2007-01-09 00:28:16Z imp $
30.\"
31.Dd January 7, 2007
32.Dt OPEN 2
33.Os
34.Sh NAME
35.Nm open
36.Nd open or create a file for reading or writing
37.Sh LIBRARY
38.Lb libc
39.Sh SYNOPSIS
40.In fcntl.h
41.Ft int
42.Fn open "const char *path" "int flags" "..."
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).
57In this case
58.Fn open
59requires a third argument
60.Fa "mode_t mode" ,
61and the file is created with mode
62.Fa mode
63as described in
64.Xr chmod 2
65and modified by the process' umask value (see
66.Xr umask 2 ) .
67.Pp
68The flags specified are formed by
69.Em or Ns 'ing
70the following values
71.Pp
72.Bd -literal -offset indent -compact
73O_RDONLY open for reading only
74O_WRONLY open for writing only
75O_RDWR open for reading and writing
76O_NONBLOCK do not block on open
77O_APPEND append on each write
78O_CREAT create file if it does not exist
79O_TRUNC truncate size to 0
80O_EXCL error if create and file exists
81O_SHLOCK atomically obtain a shared lock
82O_EXLOCK atomically obtain an exclusive lock
83O_DIRECT eliminate or reduce cache effects
84O_FSYNC synchronous writes
85O_SYNC synchronous writes
86O_NOFOLLOW do not follow symlinks
87O_NOCTTY don't assign controlling terminal
88.Ed
89.Pp
90Opening a file with
91.Dv O_APPEND
92set causes each write on the file
93to be appended to the end.
94If
95.Dv O_TRUNC
96is specified and the
97file exists, the file is truncated to zero length.
98If
99.Dv O_EXCL
100is set with
101.Dv O_CREAT
102and the file already
103exists,
104.Fn open
105returns an error.
106This may be used to
107implement a simple exclusive access locking mechanism.
108If
109.Dv O_EXCL
110is set and the last component of the pathname is
111a symbolic link,
112.Fn open
113will fail even if the symbolic
114link points to a non-existent name.
115If the
116.Dv O_NONBLOCK
117flag is specified and the
118.Fn open
119system call would result
120in the process being blocked for some reason (e.g., waiting for
121carrier on a dialup line),
122.Fn open
123returns immediately.
124The descriptor remains in non-blocking mode for subsequent operations.
125.Pp
126If
127.Dv O_FSYNC
128is used in the mask, all writes will
129immediately be written to disk,
130the kernel will not cache written data
131and all writes on the descriptor will not return until
132the data to be written completes.
133.Pp
134.Dv O_SYNC
135is a synonym for
136.Dv O_FSYNC
137required by
138.Tn POSIX .
139.Pp
140If
141.Dv O_NOFOLLOW
142is used in the mask and the target file passed to
143.Fn open
144is a symbolic link then the
145.Fn open
146will fail.
147.Pp
148When opening a file, a lock with
149.Xr flock 2
150semantics can be obtained by setting
151.Dv O_SHLOCK
152for a shared lock, or
153.Dv O_EXLOCK
154for an exclusive lock.
155If creating a file with
156.Dv O_CREAT ,
157the request for the lock will never fail
158(provided that the underlying file system supports locking).
159.Pp
160.Dv O_DIRECT
161may be used to minimize or eliminate the cache effects of reading and writing.
162The system will attempt to avoid caching the data you read or write.
163If it cannot avoid caching the data,
164it will minimize the impact the data has on the cache.
165Use of this flag can drastically reduce performance if not used with care.
166.Pp
167.Dv O_NOCTTY
168may be used to ensure the OS does not assign this file as the
169controlling terminal when it opens a tty device.
170This is the default on
171.Fx ,
172but is present for
173.Tn POSIX
174compatibility.
175The
176.Fn open
177system call will not assign controlling terminals on
178.Fx .
179.Pp
180If successful,
181.Fn open
182returns a non-negative integer, termed a file descriptor.
183It returns -1 on failure.
184The file pointer used to mark the current position within the
185file is set to the beginning of the file.
186.Pp
187When a new file is created it is given the group of the directory
188which contains it.
189.Pp
190The new descriptor is set to remain open across
191.Xr execve 2
192system calls; see
193.Xr close 2
194and
195.Xr fcntl 2 .
196.Pp
197The system imposes a limit on the number of file descriptors
198open simultaneously by one process.
199The
200.Xr getdtablesize 2
201system call returns the current system limit.
202.Sh RETURN VALUES
203If successful,
204.Fn open
205returns a non-negative integer, termed a file descriptor.
206It returns -1 on failure, and sets
207.Va errno
208to indicate the error.
209.Sh ERRORS
210The named file is opened unless:
211.Bl -tag -width Er
212.It Bq Er ENOTDIR
213A component of the path prefix is not a directory.
214.It Bq Er ENAMETOOLONG
215A component of a pathname exceeded 255 characters,
216or an entire path name exceeded 1023 characters.
217.It Bq Er ENOENT
218.Dv O_CREAT
219is not set and the named file does not exist.
220.It Bq Er ENOENT
221A component of the path name that must exist does not exist.
222.It Bq Er EACCES
223Search permission is denied for a component of the path prefix.
224.It Bq Er EACCES
225The required permissions (for reading and/or writing)
226are denied for the given flags.
227.It Bq Er EACCES
228.Dv O_TRUNC
229is specified and write permission is denied.
230.It Bq Er EACCES
231.Dv O_CREAT
232is specified,
233the file does not exist,
234and the directory in which it is to be created
235does not permit writing.
236.It Bq Er EPERM
237.Dv O_CREAT
238is specified, the file does not exist, and the directory in which it is to be
239created has its immutable flag set, see the
240.Xr chflags 2
241manual page for more information.
242.It Bq Er EPERM
243.Dv The named file has its immutable flag set and the file is to be modified.
244.It Bq Er EPERM
245.Dv The named file has its append-only flag set, the file is to be modified, and
246.Dv O_TRUNC
247is specified or
248.Dv O_APPEND
249is not specified.
250.It Bq Er ELOOP
251Too many symbolic links were encountered in translating the pathname.
252.It Bq Er EISDIR
253The named file is a directory, and the arguments specify
254it is to be modified.
255.It Bq Er EROFS
256The named file resides on a read-only file system,
257and the file is to be modified.
258.It Bq Er EROFS
259.Dv O_CREAT
260is specified and the named file would reside on a read-only file system.
261.It Bq Er EMFILE
262The process has already reached its limit for open file descriptors.
263.It Bq Er ENFILE
264The system file table is full.
265.It Bq Er EMLINK
266.Dv O_NOFOLLOW
267was specified and the target is a symbolic link.
268.It Bq Er ENXIO
269The named file is a character special or block
270special file, and the device associated with this special file
271does not exist.
272.It Bq Er ENXIO
273.Dv O_NONBLOCK
274is set, the named file is a fifo,
275.Dv O_WRONLY
276is set, and no process has the file open for reading.
277.It Bq Er EINTR
278The
279.Fn open
280operation was interrupted by a signal.
281.It Bq Er EOPNOTSUPP
282.Dv O_SHLOCK
283or
284.Dv O_EXLOCK
285is specified but the underlying file system does not support locking.
286.It Bq Er EOPNOTSUPP
287The named file is a special file mounted through a file system that
288does not support access to it (e.g.\& NFS).
289.It Bq Er EWOULDBLOCK
290.Dv O_NONBLOCK
291and one of
292.Dv O_SHLOCK
293or
294.Dv O_EXLOCK
295is specified and the file is locked.
296.It Bq Er ENOSPC
297.Dv O_CREAT
298is specified,
299the file does not exist,
300and the directory in which the entry for the new file is being placed
301cannot be extended because there is no space left on the file
302system containing the directory.
303.It Bq Er ENOSPC
304.Dv O_CREAT
305is specified,
306the file does not exist,
307and there are no free inodes on the file system on which the
308file is being created.
309.It Bq Er EDQUOT
310.Dv O_CREAT
311is specified,
312the file does not exist,
313and the directory in which the entry for the new file
314is being placed cannot be extended because the
315user's quota of disk blocks on the file system
316containing the directory has been exhausted.
317.It Bq Er EDQUOT
318.Dv O_CREAT
319is specified,
320the file does not exist,
321and the user's quota of inodes on the file system on
322which the file is being created has been exhausted.
323.It Bq Er EIO
324An I/O error occurred while making the directory entry or
325allocating the inode for
326.Dv O_CREAT .
327.It Bq Er ETXTBSY
328The file is a pure procedure (shared text) file that is being
329executed and the
330.Fn open
331system call requests write access.
332.It Bq Er EFAULT
333The
334.Fa path
335argument
336points outside the process's allocated address space.
337.It Bq Er EEXIST
338.Dv O_CREAT
339and
340.Dv O_EXCL
341were specified and the file exists.
342.It Bq Er EOPNOTSUPP
343An attempt was made to open a socket (not currently implemented).
344.It Bq Er EINVAL
345An attempt was made to open a descriptor with an illegal combination
346of
347.Dv O_RDONLY ,
348.Dv O_WRONLY ,
349and
350.Dv O_RDWR .
351.El
352.Sh SEE ALSO
353.Xr chmod 2 ,
354.Xr close 2 ,
355.Xr dup 2 ,
356.Xr fhopen 2 ,
357.Xr getdtablesize 2 ,
358.Xr getfh 2 ,
359.Xr lgetfh 2 ,
360.Xr lseek 2 ,
361.Xr read 2 ,
362.Xr umask 2 ,
363.Xr write 2 ,
364.Xr fopen 3
365.Sh HISTORY
366The
367.Fn open
368function appeared in
369.At v6 .