1 OPEN(2) System Calls OPEN(2) 2 3 NAME 4 open, openat - open a file 5 6 SYNOPSIS 7 #include <sys/types.h> 8 #include <sys/stat.h> 9 #include <fcntl.h> 10 11 int 12 open(const char *path, int oflag [, mode_t mode]); 13 14 int 15 openat(int fildes, const char *path, int oflag [, mode_t mode]); 16 17 DESCRIPTION 18 The open() function establishes the connection between a file and a file 19 descriptor. It creates an open file description that refers to a file 20 and a file descriptor that refers to that open file description. The 21 file descriptor is used by other I/O functions to refer to that file. 22 The path argument points to a pathname naming the file. 23 24 The openat() function is identical to the open() function except that the 25 path argument is interpreted relative to the starting point implied by 26 the fildes argument. If the fildes argument has the special value 27 AT_FDCWD, a relative path argument will be resolved relative to the 28 current working directory. If the path argument is absolute, the fildes 29 argument is ignored. 30 31 The open() function returns a file descriptor for the named file that is 32 the lowest file descriptor not currently open for that process. The open 33 file description is new, and therefore the file descriptor does not share 34 it with any other process in the system. The FD_CLOEXEC file descriptor 35 flag associated with the new file descriptor is cleared. 36 37 The file offset used to mark the current position within the file is set 38 to the beginning of the file. 39 40 The file status flags and file access modes of the open file description 41 are set according to the value of oflag. The mode argument is used only 42 when O_CREAT is specified (see below). 43 44 Values for oflag are constructed by a bitwise-inclusive-OR of flags from 45 the following list, defined in fcntl.h(3HEAD). Applications must specify 46 exactly one of the first three values (file access modes) below in the 47 value of oflag: 48 49 O_RDONLY 50 Open for reading only. 51 52 O_WRONLY 53 Open for writing only. 54 55 O_RDWR Open for reading and writing. The result is undefined if this 56 flag is applied to a FIFO. 57 58 Any combination of the following may be used: 59 60 O_APPEND 61 If set, the file offset is set to the end of the file prior to 62 each write. 63 64 O_CREAT 65 Create the file if it does not exist. This flag requires that 66 the mode argument be specified. 67 68 If the file exists, this flag has no effect except as noted under 69 O_EXCL below. Otherwise, the file is created with the user ID of 70 the file set to the effective user ID of the process. The group 71 ID of the file is set to the effective group IDs of the process, 72 or if the S_ISGID bit is set in the directory in which the file 73 is being created, the file's group ID is set to the group ID of 74 its parent directory. If the group ID of the new file does not 75 match the effective group ID or one of the supplementary groups 76 IDs, the S_ISGID bit is cleared. 77 78 The access permission bits (see stat.h(3HEAD)) of the file mode 79 are set to the value of mode, modified as follows (see creat(2)): 80 a bitwise-AND is performed on the file-mode bits and the 81 corresponding bits in the complement of the process's file mode 82 creation mask. Thus, all bits set in the process's file mode 83 creation mask (see umask(2)) are correspondingly cleared in the 84 file's permission mask. The "save text image after execution 85 bit" of the mode is cleared (see chmod(2)). When bits other than 86 the file permission bits are set, the effect is unspecified. The 87 mode argument does not affect whether the file is open for 88 reading, writing or for both. 89 90 O_DIRECT 91 Indicates that the file data is not going to be reused in the 92 near future. When possible, data is read or written directly 93 between the application's memory and the device when the data is 94 accessed with read(2) and write(2) operations. See directio(3C) 95 for more details. 96 97 O_DIRECTORY 98 Indicates that attempts to open path should fail unless path is a 99 directory. If both O_CREAT and O_DIRECTORY are specified then 100 the call will fail if it would result in a file being created. 101 If a directory already exists at path then it will behave as if 102 the O_DIRECTORY flag had not been present. If the O_EXCL and 103 O_CREAT flags are specified, then the call will always fail as 104 they imply a file should always be created. 105 106 O_DSYNC 107 Write I/O operations on the file descriptor complete as defined 108 by synchronized I/O data integrity completion. 109 110 O_EXCL If O_CREAT and O_EXCL are set, open() fails if the file exists. 111 The check for the existence of the file and the creation of the 112 file if it does not exist is atomic with respect to other threads 113 executing open() naming the same filename in the same directory 114 with O_EXCL and O_CREAT set. If O_EXCL and O_CREAT are set, and 115 path names a symbolic link, open() fails and sets errno to 116 EEXIST, regardless of the contents of the symbolic link. If 117 O_EXCL is set and O_CREAT is not set, the result is undefined. 118 119 O_EXEC If set, indicates that the file should be opened for execute 120 permission. This option is only valid for regular files; an 121 error will be returned if the target is not a regular file. 122 123 O_LARGEFILE 124 If set, the offset maximum in the open file description is the 125 largest value that can be represented correctly in an object of 126 type off64_t. 127 128 O_NOCTTY 129 If set and path identifies a terminal device, open() does not 130 cause the terminal device to become the controlling terminal for 131 the process. 132 133 O_NOFOLLOW 134 If the path names a symbolic link, open() fails and sets errno to 135 ELOOP. 136 137 O_NOLINKS 138 If the link count of the named file is greater than 1, open() 139 fails and sets errno to EMLINK. 140 141 O_CLOEXEC 142 If set, the file descriptor returned will be closed prior to any 143 future exec(2) calls. 144 145 O_NONBLOCK O_NDELAY 146 These flags can affect subsequent reads and writes (see read(2) 147 and write(2)). If both O_NDELAY and O_NONBLOCK are set, 148 O_NONBLOCK takes precedence. 149 150 When opening a FIFO with O_RDONLY or O_WRONLY set: 151 152 o If O_NONBLOCK or O_NDELAY is set, an open() for reading only 153 returns without delay. An open() for writing only returns an 154 error if no process currently has the file open for reading. 155 156 o If O_NONBLOCK and O_NDELAY are clear, an open() for reading 157 only blocks until a thread opens the file for writing. An 158 open() for writing only blocks the calling thread until a 159 thread opens the file for reading. 160 161 After both ends of a FIFO have been opened once, there is no 162 guarantee that further calls to open() O_RDONLY (O_WRONLY) will 163 synchronize with later calls to open() O_WRONLY (O_RDONLY) until 164 both ends of the FIFO have been closed by all readers and 165 writers. Any data written into a FIFO will be lost if both ends 166 of the FIFO are closed before the data is read. 167 168 When opening a block special or character special file that 169 supports non-blocking opens: 170 171 o If O_NONBLOCK or O_NDELAY is set, the open() function returns 172 without blocking for the device to be ready or available. 173 Subsequent behavior of the device is device-specific. 174 175 o If O_NONBLOCK and O_NDELAY are clear, the open() function 176 blocks the calling thread until the device is ready or 177 available before returning. 178 179 Otherwise, the behavior of O_NONBLOCK and O_NDELAY is 180 unspecified. 181 182 O_RSYNC 183 Read I/O operations on the file descriptor complete at the same 184 level of integrity as specified by the O_DSYNC and O_SYNC flags. 185 If both O_DSYNC and O_RSYNC are set in oflag, all I/O operations 186 on the file descriptor complete as defined by synchronized I/O 187 data integrity completion. If both O_SYNC and O_RSYNC are set in 188 oflag, all I/O operations on the file descriptor complete as 189 defined by synchronized I/O file integrity completion. 190 191 O_SEARCH 192 If set, indicates that the directory should be opened for 193 searching. This option is only valid for a directory; an error 194 will be returned if the target is not a directory. 195 196 O_SYNC Write I/O operations on the file descriptor complete as defined 197 by synchronized I/O file integrity completion (see 198 fcntl.h(3HEAD)) definition of O_SYNC. 199 200 O_TRUNC 201 If the file exists and is a regular file, and the file is 202 successfully opened O_RDWR or O_WRONLY, its length is truncated 203 to 0 and the mode and owner are unchanged. It has no effect on 204 FIFO special files or terminal device files. Its effect on other 205 file types is implementation-dependent. The result of using 206 O_TRUNC with O_RDONLY is undefined. 207 208 O_XATTR 209 If set in openat(), a relative path argument is interpreted as a 210 reference to an extended attribute of the file associated with 211 the supplied file descriptor. This flag therefore requires the 212 presence of a legal fildes argument. If set in open(), the 213 implied file descriptor is that for the current working 214 directory. Extended attributes must be referenced with a 215 relative path; providing an absolute path results in a normal 216 file reference. 217 218 If O_CREAT is set and the file did not previously exist, upon successful 219 completion, open() marks for update the st_atime, st_ctime, and st_mtime 220 fields of the file and the st_ctime and st_mtime fields of the parent 221 directory. 222 223 If O_TRUNC is set and the file did previously exist, upon successful 224 completion, open() marks for update the st_ctime and st_mtime fields of 225 the file. 226 227 If both the O_SYNC and O_DSYNC flags are set, the effect is as if only 228 the O_SYNC flag was set. 229 230 If path refers to a STREAMS file, oflag may be constructed from 231 O_NONBLOCK or O_NODELAY OR-ed with either O_RDONLY, O_WRONLY, or O_RDWR. 232 Other flag values are not applicable to STREAMS devices and have no 233 effect on them. The values O_NONBLOCK and O_NODELAY affect the operation 234 of STREAMS drivers and certain functions (see read(2), getmsg(2), 235 putmsg(2), and write(2)) applied to file descriptors associated with 236 STREAMS files. For STREAMS drivers, the implementation of O_NONBLOCK and 237 O_NODELAY is device-specific. 238 239 When open() is invoked to open a named stream, and the connld(7M) module 240 has been pushed on the pipe, open() blocks until the server process has 241 issued an I_RECVFD ioctl(2) (see streamio(7I)) to receive the file 242 descriptor. 243 244 If path names the manager side of a pseudo-terminal device, then it is 245 unspecified whether open() locks the subsidiary side so that it cannot be 246 opened. Portable applications must call unlockpt(3C) before opening the 247 subsidiary side. 248 249 If the file is a regular file and the local file system is mounted with 250 the nbmand mount option, then a mandatory share reservation is 251 automatically obtained on the file. The share reservation is obtained as 252 if fcntl(2) were called with cmd F_SHARE_NBMAND and the fshare_t values 253 set as follows: 254 255 f_access 256 Set to the type of read/write access for which the file is 257 opened. 258 259 f_deny F_NODNY 260 261 f_id The file descriptor value returned from open(). 262 263 If path is a symbolic link and O_CREAT and O_EXCL are set, the link is 264 not followed. 265 266 Certain flag values can be set following open() as described in fcntl(2). 267 268 The largest value that can be represented correctly in an object of type 269 off_t is established as the offset maximum in the open file description. 270 271 RETURN VALUES 272 The open() and openat() functions open the file and, if successful, 273 return a non-negative integer representing the lowest numbered unused 274 file descriptor; otherwise the value -1 is returned and the global 275 variable errno is set to indicate the error and no files are created or 276 modified. 277 278 EXAMPLES 279 Example 1 Open a file for writing by the owner. 280 281 The following example opens the file /tmp/file, either by creating it if 282 it does not already exist, or by truncating its length to 0 if it does 283 exist. If the call creates a new file, the access permission bits in the 284 file mode of the file are set to permit reading and writing by the owner, 285 and to permit reading only by group members and others. 286 287 If the call to open() is successful, the file is opened for writing. 288 289 #include <fcntl.h> 290 ... 291 int fd; 292 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; 293 char *filename = "/tmp/file"; 294 ... 295 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, mode); 296 ... 297 298 Example 2 Open a file using an existence check. 299 300 The following example uses the open() function to try to create the 301 LOCKFILE file and open it for writing. Since the open() function 302 specifies the O_EXCL flag, the call fails if the file already exists. In 303 that case, the application assumes that someone else is updating the 304 password file and exits. 305 306 #include <fcntl.h> 307 #include <stdio.h> 308 #include <stdlib.h> 309 #include <err.h> 310 ... 311 #define LOCKFILE "/etc/ptmp" 312 ... 313 int pfd; /* Integer for file descriptor returned by open() call. */ 314 ... 315 if ((pfd = open(LOCKFILE, O_WRONLY | O_CREAT | O_EXCL, 316 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) { 317 err(1, "Cannot open %s. Try again later.", LOCKFILE); 318 } 319 ... 320 321 Example 3 Open a file for writing. 322 323 The following example opens a file for writing, creating the file if it 324 does not already exist. If the file does exist, the system truncates the 325 file to zero bytes. 326 327 #include <fcntl.h> 328 #include <stdio.h> 329 #include <stdlib.h> 330 #include <err.h> 331 ... 332 int pfd; 333 char filename[PATH_MAX+1]; 334 ... 335 if ((pfd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 336 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) { 337 err(1, "Cannot open output file"); 338 } 339 ... 340 341 ERRORS 342 The open() and openat() functions will fail if: 343 344 EACCES Search permission is denied on a component of the path 345 prefix. 346 347 The file exists and the permissions specified by oflag 348 are denied. 349 350 The file does not exist and write permission is denied 351 for the parent directory of the file to be created. 352 353 O_TRUNC is specified and write permission is denied. 354 355 The {PRIV_FILE_DAC_SEARCH} privilege allows processes 356 to search directories regardless of permission bits. 357 The {PRIV_FILE_DAC_WRITE} privilege allows processes 358 to open files for writing regardless of permission 359 bits. See privileges(5) for special considerations 360 when opening files owned by user ID 0 for writing. 361 The {PRIV_FILE_DAC_READ} privilege allows processes to 362 open files for reading regardless of permission bits. 363 364 EAGAIN A mandatory share reservation could not be obtained 365 because the desired access conflicts with an existing 366 f_deny share reservation (see fcntl(2)). 367 368 EDQUOT The file does not exist, O_CREAT is specified, and 369 either the directory where the new file entry is being 370 placed cannot be extended because the user's quota of 371 disk blocks on that file system has been exhausted, or 372 the user's quota of inodes on the file system where 373 the file is being created has been exhausted. 374 375 EEXIST The O_CREAT and O_EXCL flags are set and the named 376 file already exists. 377 378 EILSEQ The path argument includes bytes that are not valid 379 UTF-8 characters, and the file system accepts only 380 file names where all characters are part of the UTF-8 381 character codeset. 382 383 EINTR A signal was caught during open(). 384 385 EFAULT The path argument points to an illegal address. 386 387 EINVAL Either the system does not support synchronized or 388 direct I/O for this file, or the O_XATTR flag was 389 supplied and the underlying file system does not 390 support extended file attributes. 391 392 EIO The path argument names a STREAMS file and a hangup or 393 error occurred during the open(). 394 395 EISDIR The named file is a directory and oflag includes 396 O_WRONLY or O_RDWR. 397 398 ELOOP Too many symbolic links were encountered in resolving 399 path. 400 401 A loop exists in symbolic links encountered during 402 resolution of the path argument. 403 404 The O_NOFOLLOW flag is set and the final component of 405 path is a symbolic link. 406 407 EMFILE There are currently {OPEN_MAX} file descriptors open 408 in the calling process. 409 410 EMLINK The O_NOLINKS flag is set and the named file has a 411 link count greater than 1. 412 413 EMULTIHOP Components of path require hopping to multiple remote 414 machines and the file system does not allow it. 415 416 ENAMETOOLONG The length of the path argument exceeds {PATH_MAX} or 417 a pathname component is longer than {NAME_MAX}. 418 419 ENFILE The maximum allowable number of files is currently 420 open in the system. 421 422 ENOENT The O_CREAT flag is not set and the named file does 423 not exist; or the O_CREAT flag is set and either the 424 path prefix does not exist or the path argument points 425 to an empty string. 426 427 The O_CREAT and O_DIRECTORY flags were both set and 428 path did not point to a file. 429 430 ENOEXEC The O_EXEC flag is set and path does not point to a 431 regular file. 432 433 ENOLINK The path argument points to a remote machine, and the 434 link to that machine is no longer active. 435 436 ENOSR Th path argument names a STREAMS-based file and the 437 system is unable to allocate a STREAM. 438 439 ENOSPC The directory or file system that would contain the 440 new file cannot be expanded, the file does not exist, 441 and O_CREAT is specified. 442 443 ENOSYS The device specified by path does not support the open 444 operation. 445 446 ENOTDIR A component of the path prefix is not a directory or a 447 relative path was supplied to openat(), the O_XATTR 448 flag was not supplied, and the file descriptor does 449 not refer to a directory. The O_SEARCH flag was 450 passed and path does not refer to a directory. 451 452 The O_DIRECTORY flag was set and the file was not a 453 directory. 454 455 ENXIO The O_NONBLOCK flag is set, the named file is a FIFO, 456 the O_WRONLY flag is set, and no process has the file 457 open for reading; or the named file is a character 458 special or block special file and the device 459 associated with this special file does not exist or 460 has been retired by the fault management framework. 461 462 EOPNOTSUPP An attempt was made to open a path that corresponds to 463 an AF_UNIX socket. 464 465 EOVERFLOW The named file is a regular file and either 466 O_LARGEFILE is not set and the size of the file cannot 467 be represented correctly in an object of type off_t or 468 O_LARGEFILE is set and the size of the file cannot be 469 represented correctly in an object of type off64_t. 470 471 EROFS The named file resides on a read-only file system and 472 either O_WRONLY, O_RDWR, O_CREAT (if file does not 473 exist), or O_TRUNC is set in the oflag argument. 474 475 The openat() function will fail if: 476 477 EBADF The fildes argument is not a valid open file 478 descriptor or is not AT_FTCWD. 479 480 The open() function may fail if: 481 482 EAGAIN The path argument names the subsidiary side of a 483 pseudo-terminal device that is locked. 484 485 EINVAL The value of the oflag argument is not valid. 486 487 ENAMETOOLONG Pathname resolution of a symbolic link produced an 488 intermediate result whose length exceeds {PATH_MAX}. 489 490 ENOMEM The path argument names a STREAMS file and the system 491 is unable to allocate resources. 492 493 ETXTBSY The file is a pure procedure (shared text) file that 494 is being executed and oflag is O_WRONLY or O_RDWR. 495 496 USAGE 497 The open() function has a transitional interface for 64-bit file offsets. 498 See lf64(5). Note that using open64() is equivalent to using open(with) 499 O_LARGEFILE set in oflag. 500 501 INTERFACE STABILITY 502 Committed 503 504 MT LEVEL 505 Async-Signal-Safe 506 507 SEE ALSO 508 chmod(2), close(2), creat(2), dup(2), exec(2), fcntl(2), getmsg(2), 509 getrlimit(2), Intro(2), lseek(2), putmsg(2), read(2), stat(2), umask(2), 510 write(2), attropen(3C), directio(3C), unlockpt(3C), fcntl.h(3HEAD), 511 stat.h(3HEAD), attributes(5), lf64(5), privileges(5), standards(5), 512 streamio(7I), connld(7M) 513 514 NOTES 515 Hierarchical Storage Management (HSM) file systems can sometimes cause 516 long delays when opening a file, since HSM files must be recalled from 517 secondary storage. 518 519 illumos February 5, 2022 illumos