jcs's openbsd hax
openbsd
at jcs 470 lines 12 kB view raw
1.\" $OpenBSD: open.2,v 1.52 2025/08/04 04:59:31 guenther Exp $ 2.\" $NetBSD: open.2,v 1.8 1995/02/27 12:35:14 cgd Exp $ 3.\" 4.\" Copyright (c) 1980, 1991, 1993 5.\" The Regents of the University of California. All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. Neither the name of the University nor the names of its contributors 16.\" may be used to endorse or promote products derived from this software 17.\" without specific prior written permission. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" SUCH DAMAGE. 30.\" 31.\" @(#)open.2 8.2 (Berkeley) 11/16/93 32.\" 33.Dd $Mdocdate: August 4 2025 $ 34.Dt OPEN 2 35.Os 36.Sh NAME 37.Nm open , 38.Nm openat 39.Nd open or create a file for reading or writing 40.Sh SYNOPSIS 41.In fcntl.h 42.Ft int 43.Fn open "const char *path" "int flags" ... 44.Ft int 45.Fn openat "int fd" "const char *path" "int flags" ... 46.Sh DESCRIPTION 47The file name specified by 48.Fa path 49is opened 50for reading and/or writing as specified by the 51argument 52.Fa flags 53and the file descriptor returned to the calling process. 54The 55.Fa flags 56argument may indicate the file is to be 57created if it does not exist (by specifying the 58.Dv O_CREAT 59flag), in which case the file is created with a mode 60specified by an additional argument of type 61.Vt mode_t 62as described in 63.Xr chmod 2 64and modified by the process' umask value (see 65.Xr umask 2 ) . 66.Pp 67The 68.Fa flags 69specified are a bitwise OR of the following values. 70Exactly one of the first three values (file access modes) must be specified: 71.Pp 72.Bl -tag -width O_DIRECTORY -offset indent -compact 73.It Dv O_RDONLY 74Open for reading only. 75.It Dv O_WRONLY 76Open for writing only. 77.It Dv O_RDWR 78Open for reading and writing. 79.El 80.Pp 81Any combination of the following flags may additionally be used: 82.Pp 83.Bl -tag -width O_DIRECTORY -offset indent -compact 84.It Dv O_NONBLOCK 85Do not block on open or for data to become available. 86.It Dv O_APPEND 87Append on each write. 88.It Dv O_CREAT 89Create file if it does not exist. 90An additional argument of type 91.Vt mode_t 92must be supplied to the call. 93.It Dv O_TRUNC 94Truncate size to 0. 95.It Dv O_EXCL 96Error if 97.Dv O_CREAT 98is set and file exists. 99.It Dv O_SYNC 100Perform synchronous I/O operations. 101.It Dv O_SHLOCK 102Atomically obtain a shared lock. 103.It Dv O_EXLOCK 104Atomically obtain an exclusive lock. 105.It Dv O_NOFOLLOW 106If last path element is a symlink, don't follow it. 107.It Dv O_CLOEXEC 108Set 109.Dv FD_CLOEXEC 110(the close-on-exec flag) 111on the new file descriptor. 112.It Dv O_CLOFORK 113Set 114.Dv FD_CLOFORK 115(the close-on-fork flag) 116on the new file descriptor. 117.It Dv O_DIRECTORY 118Error if 119.Fa path 120does not name a directory. 121.El 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 129and a writing mode are 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 implement a simple exclusive access locking mechanism. 140If either of 141.Dv O_EXCL 142or 143.Dv O_NOFOLLOW 144are set and the last component of the pathname is 145a symbolic link, 146.Fn open 147will fail even if the symbolic 148link points to a non-existent name. 149If the 150.Dv O_NONBLOCK 151flag is specified, do not wait for the device or file to be ready or 152available. 153If the 154.Fn open 155call would result 156in the process being blocked for some reason (e.g., waiting for 157carrier on a dialup line), 158.Fn open 159returns immediately. 160This flag also has the effect of making all subsequent I/O on the open file 161non-blocking. 162If the 163.Dv O_SYNC 164flag is set, all I/O operations on the file will be done synchronously. 165.Pp 166A FIFO should either be opened with 167.Dv O_RDONLY 168or with 169.Dv O_WRONLY . 170The behavior for opening a FIFO with 171.Dv O_RDWR 172is undefined. 173.Pp 174When opening a file, a lock with 175.Xr flock 2 176semantics can be obtained by setting 177.Dv O_SHLOCK 178for a shared lock, or 179.Dv O_EXLOCK 180for an exclusive lock. 181If creating a file with 182.Dv O_CREAT , 183the request for the lock will never fail 184(provided that the underlying filesystem supports locking). 185.Pp 186If 187.Fn open 188is successful, the file pointer used to mark the current position within 189the file is set to the beginning of the file. 190.Pp 191When a new file is created, it is given the group of the directory 192which contains it. 193.Pp 194The new descriptor is set to remain open across 195.Xr execve 2 196system calls; see 197.Xr close 2 198and 199.Xr fcntl 2 . 200.Pp 201The system imposes a limit on the number of file descriptors 202open simultaneously by one process. 203.Xr getdtablesize 3 204returns the current system limit. 205.Pp 206The 207.Fn openat 208function is equivalent to 209.Fn open 210except that where 211.Fa path 212specifies a relative path, 213the file to be opened is determined relative to 214the directory associated with file descriptor 215.Fa fd 216instead of the current working directory. 217.Pp 218If 219.Fn openat 220is passed the special value 221.Dv AT_FDCWD 222(defined in 223.In fcntl.h ) 224in the 225.Fa fd 226parameter, the current working directory is used 227and the behavior is identical to a call to 228.Fn open . 229.Sh RETURN VALUES 230If successful, 231.Fn open 232returns a non-negative integer, termed a file descriptor. 233Otherwise, a value of \-1 is returned and 234.Va errno 235is set to indicate the error. 236.Sh ERRORS 237The 238.Fn open 239and 240.Fn openat 241functions will fail if: 242.Bl -tag -width Er 243.It Bq Er ENOTDIR 244A component of the path prefix is not a directory. 245.It Bq Er ENOTDIR 246.Dv O_DIRECTORY 247is specified and 248.Fa path 249does not name a directory. 250.It Bq Er ENAMETOOLONG 251A component of a pathname exceeded 252.Dv NAME_MAX 253characters, or an entire pathname (including the terminating NUL) 254exceeded 255.Dv PATH_MAX 256bytes. 257.It Bq Er ENOENT 258.Dv O_CREAT 259is not set and the named file does not exist. 260.It Bq Er ENOENT 261A component of the pathname that must exist does not exist. 262.It Bq Er EACCES 263Search permission is denied for a component of the path prefix. 264.It Bq Er EACCES 265The required permissions (for reading and/or writing) 266are denied for the given 267.Fa flags . 268.It Bq Er EACCES 269.Dv O_CREAT 270is specified, 271the file does not exist, 272and the directory in which it is to be created 273does not permit writing. 274.It Bq Er ELOOP 275Too many symbolic links were encountered in translating the pathname, 276or the 277.Dv O_NOFOLLOW 278flag was specified and the target is a symbolic link. 279.It Bq Er EISDIR 280The named file is a directory, and the arguments specify 281it is to be opened for writing. 282.It Bq Er EINVAL 283The 284.Fa flags 285specified for opening the file are not valid. 286.It Bq Er EROFS 287The named file resides on a read-only file system, 288and the file is to be modified. 289.It Bq Er EMFILE 290The process has already reached its limit for open file descriptors. 291.It Bq Er ENFILE 292The system file table is full. 293.It Bq Er ENXIO 294The named file is a character special or block 295special file, and the device associated with this special file 296does not exist. 297.It Bq Er ENXIO 298The named file is a FIFO, the 299.Dv O_NONBLOCK 300and 301.Dv O_WRONLY 302flags are set, and no process has the file open for reading. 303.It Bq Er EINTR 304The 305.Fn open 306operation was interrupted by a signal. 307.It Bq Er EOPNOTSUPP 308.Dv O_SHLOCK 309or 310.Dv O_EXLOCK 311is specified but the underlying filesystem does not support locking. 312.It Bq Er EWOULDBLOCK 313.Dv O_NONBLOCK 314and one of 315.Dv O_SHLOCK 316or 317.Dv O_EXLOCK 318is specified and the file is already locked. 319.It Bq Er ENOSPC 320.Dv O_CREAT 321is specified, 322the file does not exist, 323and the directory in which the entry for the new file is being placed 324cannot be extended because there is no space left on the file 325system containing the directory. 326.It Bq Er ENOSPC 327.Dv O_CREAT 328is specified, 329the file does not exist, 330and there are no free inodes on the file system on which the 331file is being created. 332.It Bq Er EDQUOT 333.Dv O_CREAT 334is specified, 335the file does not exist, 336and the directory in which the entry for the new file 337is being placed cannot be extended because the 338user's quota of disk blocks on the file system 339containing the directory has been exhausted. 340.It Bq Er EDQUOT 341.Dv O_CREAT 342is specified, 343the file does not exist, 344and the user's quota of inodes on the file system on 345which the file is being created has been exhausted. 346.It Bq Er EIO 347An I/O error occurred while making the directory entry or 348allocating the inode for 349.Dv O_CREAT . 350.It Bq Er ETXTBSY 351The file is a pure procedure (shared text) file that is being 352executed and the 353.Fn open 354call requests write access. 355.It Bq Er EFAULT 356.Fa path 357points outside the process's allocated address space. 358.It Bq Er EEXIST 359.Dv O_CREAT 360and 361.Dv O_EXCL 362were specified and the file exists. 363.It Bq Er EPERM 364The file named by 365.Fa path 366is flagged append-only but 367.Dv O_APPEND 368was not specified in 369.Fa flags . 370.It Bq Er EOPNOTSUPP 371An attempt was made to open a socket (not currently implemented). 372.It Bq Er EBUSY 373An attempt was made to open a terminal device that requires exclusive 374access and the specified device has already be opened. 375.El 376.Pp 377Additionally, the 378.Fn openat 379function will fail if: 380.Bl -tag -width Er 381.It Bq Er EBADF 382The 383.Fa path 384argument specifies a relative path and the 385.Fa fd 386argument is neither 387.Dv AT_FDCWD 388nor a valid file descriptor. 389.It Bq Er ENOTDIR 390The 391.Fa path 392argument specifies a relative path and the 393.Fa fd 394argument is a valid file descriptor but it does not reference a directory. 395.It Bq Er EACCES 396The 397.Fa path 398argument specifies a relative path but search permission is denied 399for the directory which the 400.Fa fd 401file descriptor references. 402.El 403.Sh SEE ALSO 404.Xr chflags 2 , 405.Xr chmod 2 , 406.Xr close 2 , 407.Xr dup 2 , 408.Xr flock 2 , 409.Xr lseek 2 , 410.Xr read 2 , 411.Xr umask 2 , 412.Xr write 2 , 413.Xr getdtablesize 3 414.Sh STANDARDS 415The 416.Fn open 417and 418.Fn openat 419functions conform to 420.St -p1003.1-2008 . 421.Pp 422.Dv POSIX 423specifies three different flavors for synchronous I/O: 424.Dv O_SYNC , 425.Dv O_DSYNC , 426and 427.Dv O_RSYNC . 428In 429.Ox , 430these are all equivalent. 431.Pp 432The 433.Dv O_SHLOCK 434and 435.Dv O_EXLOCK 436flags are non-standard extensions and should not be used if portability 437is of concern. 438.Sh HISTORY 439An 440.Fn open 441system call first appeared in 442.At v1 . 443The 444.Fa flags 445argument has been supported since 446.Bx 4.2 . 447Before that, a dedicated 448.Fn creat 449system call had to be used to create new files; 450it appeared in 451.At v1 , 452was deprecated in 453.Bx 4.3 Reno , 454and removed in 455.Ox 5.0 . 456.Pp 457The 458.Fn openat 459system call has been available since 460.Ox 5.0 . 461.Sh CAVEATS 462The 463.Dv O_TRUNC 464flag requires that one of 465.Dv O_RDWR 466or 467.Dv O_WRONLY 468also be specified, else 469.Er EINVAL 470is returned.