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