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.\" 3. All advertising materials mentioning features or use of this software 13.\" must display the following acknowledgement: 14.\" This product includes software developed by the University of 15.\" California, Berkeley and its contributors. 16.\" 4. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" @(#)open.2 8.2 (Berkeley) 11/16/93 33.\" $FreeBSD$ 34.\" 35.Dd November 16, 1993 36.Dt OPEN 2 37.Os 38.Sh NAME 39.Nm open 40.Nd open or create a file for reading or writing 41.Sh LIBRARY 42.Lb libc 43.Sh SYNOPSIS 44.Fd #include <fcntl.h> 45.Ft int 46.Fn open "const char *path" "int flags" "..." 47.Sh DESCRIPTION 48The file name specified by 49.Fa path 50is opened 51for reading and/or writing as specified by the 52argument 53.Fa flags 54and the file descriptor returned to the calling process. 55The 56.Fa flags 57argument may indicate the file is to be 58created if it does not exist (by specifying the 59.Dv O_CREAT 60flag). 61In this case 62.Nm 63requires a third 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 flags specified are formed by 73.Em or Ns 'ing 74the following values 75.Pp 76.Bd -literal -offset indent -compact 77O_RDONLY open for reading only 78O_WRONLY open for writing only 79O_RDWR open for reading and writing 80O_NONBLOCK do not block on open 81O_APPEND append on each write 82O_CREAT create file if it does not exist 83O_TRUNC truncate size to 0 84O_EXCL error if create and file exists 85O_SHLOCK atomically obtain a shared lock 86O_EXLOCK atomically obtain an exclusive lock 87O_DIRECT eliminate or reduce cache effects 88O_FSYNC synchronous writes 89O_NOFOLLOW do not follow symlinks 90.Ed 91.Pp 92Opening a file with 93.Dv O_APPEND 94set causes each write on the file 95to be appended to the end. 96If 97.Dv O_TRUNC 98is specified and the 99file exists, the file is truncated to zero length. 100If 101.Dv O_EXCL 102is set with 103.Dv O_CREAT 104and the file already 105exists, 106.Fn open 107returns an error. 108This may be used to 109implement a simple exclusive access locking mechanism. 110If 111.Dv O_EXCL 112is set and the last component of the pathname is 113a symbolic link, 114.Fn open 115will fail even if the symbolic 116link points to a non-existent name. 117If the 118.Dv O_NONBLOCK 119flag is specified and the 120.Fn open 121call would result 122in the process being blocked for some reason (e.g., waiting for 123carrier on a dialup line), 124.Fn open 125returns immediately. 126The first time the process attempts to perform I/O on the open 127file it will block (not currently implemented). 128.Pp 129If 130.Dv O_FSYNC 131is used in the mask, all writes will 132immediately be written to disk, 133the kernel will not cache written data 134and all writes on the descriptor will not return until 135the data to be written completes. 136.Pp 137If 138.Dv O_NOFOLLOW 139is used in the mask and the target file passed to 140.Fn open 141is a symbolic link then the 142.Fn open 143will fail. 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 157.Dv O_DIRECT 158may be used to minimize or eliminate the cache effects of reading and writing. 159The system will attempt to avoid caching the data you read or write. 160If it cannot avoid caching the data, 161it will minimize the impact the data has on the cache. 162Use of this flag can drastically reduce performance if not used with care. 163.Pp 164If successful, 165.Fn open 166returns a non-negative integer, termed a file descriptor. 167It returns -1 on failure. 168The file pointer used to mark the current position within the 169file is set to the beginning of the file. 170.Pp 171When a new file is created it is given the group of the directory 172which contains it. 173.Pp 174The new descriptor is set to remain open across 175.Xr execve 2 176system calls; see 177.Xr close 2 178and 179.Xr fcntl 2 . 180.Pp 181The system imposes a limit on the number of file descriptors 182open simultaneously by one process. 183.Xr Getdtablesize 2 184returns the current system limit. 185.Sh IMPLEMENTATION NOTES 186In the non-threaded library 187.Fn open 188is implemented as the 189.Va open 190syscall. 191.Pp 192In the threaded library, the 193.Va open 194syscall is assembled to 195.Fn _thread_sys_open 196and 197.Fn open 198is implemented as a function which disables thread rescheduling 199and calls 200.Fn _thread_sys_open . 201Before returning, 202.Fn open 203enables thread rescheduling. 204.Sh RETURN VALUES 205If successful, 206.Fn open 207returns a non-negative integer, termed a file descriptor. 208It returns -1 on failure, and sets 209.Va errno 210to indicate the error. 211.Sh ERRORS 212The named file is opened unless: 213.Bl -tag -width Er 214.It Bq Er ENOTDIR 215A component of the path prefix is not a directory. 216.It Bq Er ENAMETOOLONG 217A component of a pathname exceeded 255 characters, 218or an entire path name exceeded 1023 characters. 219.It Bq Er ENOENT 220.Dv O_CREAT 221is not set and the named file does not exist. 222.It Bq Er ENOENT 223A component of the path name that must exist does not exist. 224.It Bq Er EACCES 225Search permission is denied for a component of the path prefix. 226.It Bq Er EACCES 227The required permissions (for reading and/or writing) 228are denied for the given flags. 229.It Bq Er EACCES 230.Dv O_CREAT 231is specified, 232the file does not exist, 233and the directory in which it is to be created 234does not permit writing. 235.It Bq Er ELOOP 236Too many symbolic links were encountered in translating the pathname. 237.It Bq Er EISDIR 238The named file is a directory, and the arguments specify 239it is to be opened for writing. 240.It Bq Er EROFS 241The named file resides on a read-only file system, 242and the file is to be modified. 243.It Bq Er EMFILE 244The process has already reached its limit for open file descriptors. 245.It Bq Er ENFILE 246The system file table is full. 247.It Bq Er EMLINK 248.Dv O_NOFOLLOW 249was specified and the target is a symbolic link. 250.It Bq Er ENXIO 251The named file is a character special or block 252special file, and the device associated with this special file 253does not exist. 254.It Bq Er ENXIO 255The named file is a fifo, no process has 256it open for reading, and the arguments specify it is 257to be opened for writing. 258.It Bq Er EINTR 259The 260.Fn open 261operation was interrupted by a signal. 262.It Bq Er EOPNOTSUPP 263.Dv O_SHLOCK 264or 265.Dv O_EXLOCK 266is specified but the underlying filesystem does not support locking. 267.It Bq Er EWOULDBLOCK 268.Dv O_NONBLOCK 269and one of 270.Dv O_SHLOCK 271or 272.Dv O_EXLOCK 273is specified and the file is locked. 274.It Bq Er ENOSPC 275.Dv O_CREAT 276is specified, 277the file does not exist, 278and the directory in which the entry for the new file is being placed 279cannot be extended because there is no space left on the file 280system containing the directory. 281.It Bq Er ENOSPC 282.Dv O_CREAT 283is specified, 284the file does not exist, 285and there are no free inodes on the file system on which the 286file is being created. 287.It Bq Er EDQUOT 288.Dv O_CREAT 289is specified, 290the file does not exist, 291and the directory in which the entry for the new file 292is being placed cannot be extended because the 293user's quota of disk blocks on the file system 294containing the directory has been exhausted. 295.It Bq Er EDQUOT 296.Dv O_CREAT 297is specified, 298the file does not exist, 299and the user's quota of inodes on the file system on 300which the file is being created has been exhausted. 301.It Bq Er EIO 302An I/O error occurred while making the directory entry or 303allocating the inode for 304.Dv O_CREAT . 305.It Bq Er ETXTBSY 306The file is a pure procedure (shared text) file that is being 307executed and the 308.Fn open 309call requests write access. 310.It Bq Er EFAULT 311.Fa Path 312points outside the process's allocated address space. 313.It Bq Er EEXIST 314.Dv O_CREAT 315and 316.Dv O_EXCL 317were specified and the file exists. 318.It Bq Er EOPNOTSUPP 319An attempt was made to open a socket (not currently implemented). 320.It Bq Er EINVAL 321An attempt was made to open a descriptor with an illegal combination 322of 323.Dv O_RDONLY , 324.Dv O_WRONLY , 325and 326.Dv O_RDWR . 327.El 328.Sh SEE ALSO 329.Xr chmod 2 , 330.Xr close 2 , 331.Xr dup 2 , 332.Xr getdtablesize 2 , 333.Xr lseek 2 , 334.Xr read 2 , 335.Xr umask 2 , 336.Xr write 2 337.Sh HISTORY 338An 339.Fn open 340function call appeared in 341.At v6 . 342