]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/sys/open.2
MFC r318314
[FreeBSD/FreeBSD.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 November 22, 2016
32 .Dt OPEN 2
33 .Os
34 .Sh NAME
35 .Nm open , openat
36 .Nd open or create a file for reading, writing or executing
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 .Ft int
44 .Fn openat "int fd" "const char *path" "int flags" "..."
45 .Sh DESCRIPTION
46 The file name specified by
47 .Fa path
48 is opened
49 for either execution or reading and/or writing as specified by the
50 argument
51 .Fa flags
52 and the file descriptor returned to the calling process.
53 The
54 .Fa flags
55 argument may indicate the file is to be
56 created if it does not exist (by specifying the
57 .Dv O_CREAT
58 flag).
59 In this case
60 .Fn open
61 and
62 .Fn openat
63 require an additional argument
64 .Fa "mode_t mode" ,
65 and the file is created with mode
66 .Fa mode
67 as described in
68 .Xr chmod 2
69 and modified by the process' umask value (see
70 .Xr umask 2 ) .
71 .Pp
72 The
73 .Fn openat
74 function is equivalent to the
75 .Fn open
76 function except in the case where the
77 .Fa path
78 specifies a relative path.
79 In this case the file to be opened is determined relative to the directory
80 associated with the file descriptor
81 .Fa fd
82 instead of the current working directory.
83 The
84 .Fa flag
85 parameter and the optional fourth parameter correspond exactly to
86 the parameters of
87 .Fn open .
88 If
89 .Fn openat
90 is passed the special value
91 .Dv AT_FDCWD
92 in the
93 .Fa fd
94 parameter, the current working directory is used
95 and the behavior is identical to a call to
96 .Fn open .
97 .Pp
98 The flags specified are formed by
99 .Em or Ns 'ing
100 the following values
101 .Pp
102 .Bd -literal -offset indent -compact
103 O_RDONLY        open for reading only
104 O_WRONLY        open for writing only
105 O_RDWR          open for reading and writing
106 O_EXEC          open for execute only
107 O_NONBLOCK      do not block on open
108 O_APPEND        append on each write
109 O_CREAT         create file if it does not exist
110 O_TRUNC         truncate size to 0
111 O_EXCL          error if create and file exists
112 O_SHLOCK        atomically obtain a shared lock
113 O_EXLOCK        atomically obtain an exclusive lock
114 O_DIRECT        eliminate or reduce cache effects
115 O_FSYNC         synchronous writes
116 O_SYNC          synchronous writes
117 O_NOFOLLOW      do not follow symlinks
118 O_NOCTTY        ignored
119 O_TTY_INIT      ignored
120 O_DIRECTORY     error if file is not a directory
121 O_CLOEXEC       set FD_CLOEXEC upon open
122 O_VERIFY        verify the contents of the file
123 .Ed
124 .Pp
125 Opening a file with
126 .Dv O_APPEND
127 set causes each write on the file
128 to be appended to the end.
129 If
130 .Dv O_TRUNC
131 is specified and the
132 file exists, the file is truncated to zero length.
133 If
134 .Dv O_EXCL
135 is set with
136 .Dv O_CREAT
137 and the file already
138 exists,
139 .Fn open
140 returns an error.
141 This may be used to
142 implement a simple exclusive access locking mechanism.
143 If
144 .Dv O_EXCL
145 is set and the last component of the pathname is
146 a symbolic link,
147 .Fn open
148 will fail even if the symbolic
149 link points to a non-existent name.
150 If the
151 .Dv O_NONBLOCK
152 flag is specified and the
153 .Fn open
154 system call would result
155 in the process being blocked for some reason (e.g., waiting for
156 carrier on a dialup line),
157 .Fn open
158 returns immediately.
159 The descriptor remains in non-blocking mode for subsequent operations.
160 .Pp
161 If
162 .Dv O_FSYNC
163 is used in the mask, all writes will
164 immediately be written to disk,
165 the kernel will not cache written data
166 and all writes on the descriptor will not return until
167 the data to be written completes.
168 .Pp
169 .Dv O_SYNC
170 is a synonym for
171 .Dv O_FSYNC
172 required by
173 .Tn POSIX .
174 .Pp
175 If
176 .Dv O_NOFOLLOW
177 is used in the mask and the target file passed to
178 .Fn open
179 is a symbolic link then the
180 .Fn open
181 will fail.
182 .Pp
183 When opening a file, a lock with
184 .Xr flock 2
185 semantics can be obtained by setting
186 .Dv O_SHLOCK
187 for a shared lock, or
188 .Dv O_EXLOCK
189 for an exclusive lock.
190 If creating a file with
191 .Dv O_CREAT ,
192 the request for the lock will never fail
193 (provided that the underlying file system supports locking).
194 .Pp
195 .Dv O_DIRECT
196 may be used to minimize or eliminate the cache effects of reading and writing.
197 The system will attempt to avoid caching the data you read or write.
198 If it cannot avoid caching the data,
199 it will minimize the impact the data has on the cache.
200 Use of this flag can drastically reduce performance if not used with care.
201 .Pp
202 .Dv O_NOCTTY
203 may be used to ensure the OS does not assign this file as the
204 controlling terminal when it opens a tty device.
205 This is the default on
206 .Fx ,
207 but is present for
208 .Tn POSIX
209 compatibility.
210 The
211 .Fn open
212 system call will not assign controlling terminals on
213 .Fx .
214 .Pp
215 .Dv O_TTY_INIT
216 may be used to ensure the OS restores the terminal attributes when
217 initially opening a TTY.
218 This is the default on
219 .Fx ,
220 but is present for
221 .Tn POSIX
222 compatibility.
223 The initial call to
224 .Fn open
225 on a TTY will always restore default terminal attributes on
226 .Fx .
227 .Pp
228 .Dv O_DIRECTORY
229 may be used to ensure the resulting file descriptor refers to a
230 directory.
231 This flag can be used to prevent applications with elevated privileges
232 from opening files which are even unsafe to open with
233 .Dv O_RDONLY ,
234 such as device nodes.
235 .Pp
236 .Dv O_CLOEXEC
237 may be used to set
238 .Dv FD_CLOEXEC
239 flag for the newly returned file descriptor.
240 .Pp
241 .Dv O_VERIFY
242 may be used to indicate to the kernel that the contents of the file should
243 be verified before allowing the open to proceed.
244 The details of what
245 .Dq verified
246 means is implementation specific.
247 The run-time linker (rtld) uses this flag to ensure shared objects have
248 been verified before operating on them.
249 .Pp
250 If successful,
251 .Fn open
252 returns a non-negative integer, termed a file descriptor.
253 It returns \-1 on failure.
254 The file pointer used to mark the current position within the
255 file is set to the beginning of the file.
256 .Pp
257 If a sleeping open of a device node from
258 .Xr devfs 5
259 is interrupted by a signal, the call always fails with
260 .Er EINTR ,
261 even if the
262 .Dv SA_RESTART
263 flag is set for the signal.
264 A sleeping open of a fifo (see
265 .Xr mkfifo 2 )
266 is restarted as normal.
267 .Pp
268 When a new file is created it is given the group of the directory
269 which contains it.
270 .Pp
271 Unless
272 .Dv O_CLOEXEC
273 flag was specified,
274 the new descriptor is set to remain open across
275 .Xr execve 2
276 system calls; see
277 .Xr close 2 ,
278 .Xr fcntl 2
279 and
280 .Dv O_CLOEXEC
281 description.
282 .Pp
283 The system imposes a limit on the number of file descriptors
284 open simultaneously by one process.
285 The
286 .Xr getdtablesize 2
287 system call returns the current system limit.
288 .Sh RETURN VALUES
289 If successful,
290 .Fn open
291 and
292 .Fn openat
293 return a non-negative integer, termed a file descriptor.
294 They return \-1 on failure, and set
295 .Va errno
296 to indicate the error.
297 .Sh ERRORS
298 The named file is opened unless:
299 .Bl -tag -width Er
300 .It Bq Er ENOTDIR
301 A component of the path prefix is not a directory.
302 .It Bq Er ENAMETOOLONG
303 A component of a pathname exceeded 255 characters,
304 or an entire path name exceeded 1023 characters.
305 .It Bq Er ENOENT
306 .Dv O_CREAT
307 is not set and the named file does not exist.
308 .It Bq Er ENOENT
309 A component of the path name that must exist does not exist.
310 .It Bq Er EACCES
311 Search permission is denied for a component of the path prefix.
312 .It Bq Er EACCES
313 The required permissions (for reading and/or writing)
314 are denied for the given flags.
315 .It Bq Er EACCES
316 .Dv O_TRUNC
317 is specified and write permission is denied.
318 .It Bq Er EACCES
319 .Dv O_CREAT
320 is specified,
321 the file does not exist,
322 and the directory in which it is to be created
323 does not permit writing.
324 .It Bq Er EPERM
325 .Dv O_CREAT
326 is specified, the file does not exist, and the directory in which it is to be
327 created has its immutable flag set, see the
328 .Xr chflags 2
329 manual page for more information.
330 .It Bq Er EPERM
331 The named file has its immutable flag set and the file is to be modified.
332 .It Bq Er EPERM
333 The named file has its append-only flag set, the file is to be modified, and
334 .Dv O_TRUNC
335 is specified or
336 .Dv O_APPEND
337 is not specified.
338 .It Bq Er ELOOP
339 Too many symbolic links were encountered in translating the pathname.
340 .It Bq Er EISDIR
341 The named file is a directory, and the arguments specify
342 it is to be modified.
343 .It Bq Er EROFS
344 The named file resides on a read-only file system,
345 and the file is to be modified.
346 .It Bq Er EROFS
347 .Dv O_CREAT
348 is specified and the named file would reside on a read-only file system.
349 .It Bq Er EMFILE
350 The process has already reached its limit for open file descriptors.
351 .It Bq Er ENFILE
352 The system file table is full.
353 .It Bq Er EMLINK
354 .Dv O_NOFOLLOW
355 was specified and the target is a symbolic link.
356 .It Bq Er ENXIO
357 The named file is a character special or block
358 special file, and the device associated with this special file
359 does not exist.
360 .It Bq Er ENXIO
361 .Dv O_NONBLOCK
362 is set, the named file is a fifo,
363 .Dv O_WRONLY
364 is set, and no process has the file open for reading.
365 .It Bq Er EINTR
366 The
367 .Fn open
368 operation was interrupted by a signal.
369 .It Bq Er EOPNOTSUPP
370 .Dv O_SHLOCK
371 or
372 .Dv O_EXLOCK
373 is specified but the underlying file system does not support locking.
374 .It Bq Er EOPNOTSUPP
375 The named file is a special file mounted through a file system that
376 does not support access to it (e.g.\& NFS).
377 .It Bq Er EWOULDBLOCK
378 .Dv O_NONBLOCK
379 and one of
380 .Dv O_SHLOCK
381 or
382 .Dv O_EXLOCK
383 is specified and the file is locked.
384 .It Bq Er ENOSPC
385 .Dv O_CREAT
386 is specified,
387 the file does not exist,
388 and the directory in which the entry for the new file is being placed
389 cannot be extended because there is no space left on the file
390 system containing the directory.
391 .It Bq Er ENOSPC
392 .Dv O_CREAT
393 is specified,
394 the file does not exist,
395 and there are no free inodes on the file system on which the
396 file is being created.
397 .It Bq Er EDQUOT
398 .Dv O_CREAT
399 is specified,
400 the file does not exist,
401 and the directory in which the entry for the new file
402 is being placed cannot be extended because the
403 user's quota of disk blocks on the file system
404 containing the directory has been exhausted.
405 .It Bq Er EDQUOT
406 .Dv O_CREAT
407 is specified,
408 the file does not exist,
409 and the user's quota of inodes on the file system on
410 which the file is being created has been exhausted.
411 .It Bq Er EIO
412 An I/O error occurred while making the directory entry or
413 allocating the inode for
414 .Dv O_CREAT .
415 .It Bq Er ETXTBSY
416 The file is a pure procedure (shared text) file that is being
417 executed and the
418 .Fn open
419 system call requests write access.
420 .It Bq Er EFAULT
421 The
422 .Fa path
423 argument
424 points outside the process's allocated address space.
425 .It Bq Er EEXIST
426 .Dv O_CREAT
427 and
428 .Dv O_EXCL
429 were specified and the file exists.
430 .It Bq Er EOPNOTSUPP
431 An attempt was made to open a socket (not currently implemented).
432 .It Bq Er EINVAL
433 An attempt was made to open a descriptor with an illegal combination
434 of
435 .Dv O_RDONLY ,
436 .Dv O_WRONLY ,
437 .Dv O_RDWR
438 and
439 .Dv O_EXEC .
440 .It Bq Er EBADF
441 The
442 .Fa path
443 argument does not specify an absolute path and the
444 .Fa fd
445 argument is
446 neither
447 .Dv AT_FDCWD
448 nor a valid file descriptor open for searching.
449 .It Bq Er ENOTDIR
450 The
451 .Fa path
452 argument is not an absolute path and
453 .Fa fd
454 is neither
455 .Dv AT_FDCWD
456 nor a file descriptor associated with a directory.
457 .It Bq Er ENOTDIR
458 .Dv O_DIRECTORY
459 is specified and the file is not a directory.
460 .El
461 .Sh SEE ALSO
462 .Xr chmod 2 ,
463 .Xr close 2 ,
464 .Xr dup 2 ,
465 .Xr fexecve 2 ,
466 .Xr fhopen 2 ,
467 .Xr getdtablesize 2 ,
468 .Xr getfh 2 ,
469 .Xr lgetfh 2 ,
470 .Xr lseek 2 ,
471 .Xr read 2 ,
472 .Xr umask 2 ,
473 .Xr write 2 ,
474 .Xr fopen 3
475 .Sh HISTORY
476 The
477 .Fn open
478 function appeared in
479 .At v6 .
480 The
481 .Fn openat
482 function was introduced in
483 .Fx 8.0 .
484 .Sh STANDARDS
485 These functions are specified by
486 .St -p1003.1-2008 .
487 .Fx
488 sets
489 .Va errno
490 to
491 .Er EMLINK instead of
492 .Er ELOOP
493 as specified by
494 .Tn POSIX
495 when
496 .Dv O_NOFOLLOW
497 is set in flags and the final component of pathname is a symbolic link
498 to distinguish it from the case of too many symbolic link traversals
499 in one of its non-final components.
500 .Sh BUGS
501 The Open Group Extended API Set 2 specification requires that the test
502 for whether
503 .Fa fd
504 is searchable is based on whether
505 .Fa fd
506 is open for searching, not whether the underlying directory currently
507 permits searches.
508 The present implementation of the
509 .Fa openat
510 checks the current permissions of directory instead.