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