logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

open.3p (26004B)


  1. '\" et
  2. .TH OPEN "3P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
  3. .\"
  4. .SH PROLOG
  5. This manual page is part of the POSIX Programmer's Manual.
  6. The Linux implementation of this interface may differ (consult
  7. the corresponding Linux manual page for details of Linux behavior),
  8. or the interface may not be implemented on Linux.
  9. .\"
  10. .SH NAME
  11. open, openat
  12. \(em open file
  13. .SH SYNOPSIS
  14. .LP
  15. .nf
  16. #include <sys/stat.h>
  17. #include <fcntl.h>
  18. .P
  19. int open(const char *\fIpath\fP, int \fIoflag\fP, ...);
  20. int openat(int \fIfd\fP, const char *\fIpath\fP, int \fIoflag\fP, ...);
  21. .fi
  22. .SH DESCRIPTION
  23. The
  24. \fIopen\fR()
  25. function shall establish the connection between a file and a file
  26. descriptor. It shall create an open file description that refers to a
  27. file and a file descriptor that refers to that open file description.
  28. The file descriptor is used by other I/O functions to refer to that
  29. file. The
  30. .IR path
  31. argument points to a pathname naming the file.
  32. .P
  33. The
  34. \fIopen\fR()
  35. function shall return a file descriptor for the named file, allocated
  36. as described in
  37. .IR "Section 2.14" ", " "File Descriptor Allocation".
  38. The open file description is new, and therefore the file descriptor
  39. shall not share it with any other process in the
  40. system. The FD_CLOEXEC file descriptor flag associated with the new
  41. file descriptor shall be cleared unless the O_CLOEXEC flag is set in
  42. .IR oflag .
  43. .P
  44. The file offset used to mark the current position within the file shall
  45. be set to the beginning of the file.
  46. .P
  47. The file status flags and file access modes of the open file
  48. description shall be set according to the value of
  49. .IR oflag .
  50. .P
  51. Values for
  52. .IR oflag
  53. are constructed by a bitwise-inclusive OR of flags from the following
  54. list, defined in
  55. .IR <fcntl.h> .
  56. Applications shall specify exactly one of the first five values
  57. (file access modes) below in the value of
  58. .IR oflag :
  59. .IP O_EXEC 14
  60. Open for execute only (non-directory files). The result is unspecified
  61. if this flag is applied to a directory.
  62. .IP O_RDONLY 14
  63. Open for reading only.
  64. .IP O_RDWR 14
  65. Open for reading and writing. The result is undefined if this flag is
  66. applied to a FIFO.
  67. .IP O_SEARCH 14
  68. Open directory for search only. The result is unspecified if this flag
  69. is applied to a non-directory file.
  70. .IP O_WRONLY 14
  71. Open for writing only.
  72. .P
  73. Any combination of the following may be used:
  74. .IP O_APPEND 14
  75. If set, the file offset shall be set to the end of the file prior
  76. to each write.
  77. .IP O_CLOEXEC 14
  78. If set, the FD_CLOEXEC flag for the new file descriptor shall be set.
  79. .IP O_CREAT 14
  80. If the file exists, this flag has no effect except as noted under O_EXCL
  81. below. Otherwise, if O_DIRECTORY is not set the file shall be created as
  82. a regular file; the user ID of the file shall be set to the effective
  83. user ID of the process; the group ID of the file shall be set to
  84. the group ID of the file's parent directory or to the effective
  85. group ID of the process; and the access permission bits (see
  86. .IR <sys/stat.h> )
  87. of the file mode shall be set to the value of the argument following the
  88. .IR oflag
  89. argument taken as type
  90. .BR mode_t
  91. modified as follows: a bitwise AND is performed on the file-mode bits
  92. and the corresponding bits in the complement of the process' file mode
  93. creation mask. Thus, all bits in the file mode whose corresponding bit
  94. in the file mode creation mask is set are cleared. When bits other than
  95. the file permission bits are set, the effect is unspecified. The argument
  96. following the
  97. .IR oflag
  98. argument does not affect whether the file is open for reading, writing,
  99. or for both. Implementations shall provide a way to initialize the file's
  100. group ID to the group ID of the parent directory. Implementations may,
  101. but need not, provide an implementation-defined way to initialize the
  102. file's group ID to the effective group ID of the calling process.
  103. .IP O_DIRECTORY 14
  104. If
  105. .IR path
  106. resolves to a non-directory file, fail and set
  107. .IR errno
  108. to
  109. .BR [ENOTDIR] .
  110. .IP O_DSYNC 14
  111. Write I/O operations on the file descriptor shall complete as defined
  112. by synchronized I/O data integrity completion.
  113. .IP O_EXCL 14
  114. If O_CREAT and O_EXCL are set,
  115. \fIopen\fR()
  116. shall fail if the file exists. The check for the existence of the file
  117. and the creation of the file if it does not exist shall be atomic with
  118. respect to other threads executing
  119. \fIopen\fR()
  120. naming the same filename in the same directory with O_EXCL and O_CREAT
  121. set. If O_EXCL and O_CREAT are set, and
  122. .IR path
  123. names a symbolic link,
  124. \fIopen\fR()
  125. shall fail and set
  126. .IR errno
  127. to
  128. .BR [EEXIST] ,
  129. regardless of the contents of the symbolic link. If O_EXCL is set and
  130. O_CREAT is not set, the result is undefined.
  131. .IP O_NOCTTY 14
  132. If set and
  133. .IR path
  134. identifies a terminal device,
  135. \fIopen\fR()
  136. shall not cause the terminal device to become the controlling terminal
  137. for the process. If
  138. .IR path
  139. does not identify a terminal device, O_NOCTTY shall be ignored.
  140. .IP O_NOFOLLOW 14
  141. If
  142. .IR path
  143. names a symbolic link, fail and set
  144. .IR errno
  145. to
  146. .BR [ELOOP] .
  147. .IP O_NONBLOCK 14
  148. When opening a FIFO with O_RDONLY or O_WRONLY set:
  149. .RS 14
  150. .IP " *" 4
  151. If O_NONBLOCK is set, an
  152. \fIopen\fR()
  153. for reading-only shall return without delay. An
  154. \fIopen\fR()
  155. for writing-only shall return an error if no process currently has the
  156. file open for reading.
  157. .IP " *" 4
  158. If O_NONBLOCK is clear, an
  159. \fIopen\fR()
  160. for reading-only shall block the calling thread until a thread opens
  161. the file for writing. An
  162. \fIopen\fR()
  163. for writing-only shall block the calling thread until a thread opens
  164. the file for reading.
  165. .P
  166. When opening a block special or character special file that supports
  167. non-blocking opens:
  168. .IP " *" 4
  169. If O_NONBLOCK is set, the
  170. \fIopen\fR()
  171. function shall return without blocking for the device to be ready or
  172. available. Subsequent behavior of the device is device-specific.
  173. .IP " *" 4
  174. If O_NONBLOCK is clear, the
  175. \fIopen\fR()
  176. function shall block the calling thread until the device is ready or
  177. available before returning.
  178. .P
  179. Otherwise, the O_NONBLOCK flag shall not cause an error, but it is
  180. unspecified whether the file status flags will include the O_NONBLOCK
  181. flag.
  182. .RE
  183. .IP O_RSYNC 14
  184. Read I/O operations on the file descriptor shall complete at the same
  185. level of integrity as specified by the O_DSYNC and
  186. O_SYNC flags. If both O_DSYNC and O_RSYNC are set in
  187. .IR oflag ,
  188. all I/O operations on the file descriptor shall complete as defined by
  189. synchronized I/O data integrity completion. If both O_SYNC and O_RSYNC
  190. are set in flags, all I/O operations on the file descriptor shall
  191. complete as defined by synchronized I/O file integrity completion.
  192. .IP O_SYNC 14
  193. Write I/O operations on the file descriptor shall complete as defined
  194. by synchronized I/O file integrity completion.
  195. .RS 14
  196. .P
  197. The O_SYNC flag shall be supported for regular files, even if the
  198. Synchronized Input and Output option is not supported.
  199. .RE
  200. .IP O_TRUNC 14
  201. If the file exists and is a regular file, and the file is successfully
  202. opened O_RDWR or O_WRONLY, its length shall be truncated to 0, and
  203. the mode and owner shall be unchanged. It shall have no effect on FIFO
  204. special files or terminal device files. Its effect on other file types
  205. is implementation-defined. The result of using O_TRUNC without either
  206. O_RDWR or O_WRONLY is undefined.
  207. .IP O_TTY_INIT 14
  208. If
  209. .IR path
  210. identifies a terminal device other than a pseudo-terminal, the device
  211. is not already open in any process, and either O_TTY_INIT is set in
  212. .IR oflag
  213. or O_TTY_INIT has the value zero,
  214. \fIopen\fR()
  215. shall set any non-standard
  216. .BR termios
  217. structure terminal parameters to a state that provides conforming
  218. behavior; see the Base Definitions volume of POSIX.1\(hy2017,
  219. .IR "Section 11.2" ", " "Parameters that Can be Set".
  220. It is unspecified whether O_TTY_INIT has any effect if the device is
  221. already open in any process. If
  222. .IR path
  223. identifies the slave side of a pseudo-terminal that is not already open
  224. in any process,
  225. \fIopen\fR()
  226. shall set any non-standard
  227. .BR termios
  228. structure terminal parameters to a state that provides conforming
  229. behavior, regardless of whether O_TTY_INIT is set. If
  230. .IR path
  231. does not identify a terminal device, O_TTY_INIT shall be ignored.
  232. .P
  233. If O_CREAT and O_DIRECTORY are set and the requested access mode is
  234. neither O_WRONLY nor O_RDWR, the result is unspecified.
  235. .P
  236. If O_CREAT is set and the file did not previously exist, upon successful
  237. completion,
  238. \fIopen\fR()
  239. shall mark for update the last data access, last data modification,
  240. and last file status change timestamps of the file and the last data
  241. modification and last file status change timestamps of the parent
  242. directory.
  243. .P
  244. If O_TRUNC is set and the file did previously exist, upon successful
  245. completion,
  246. \fIopen\fR()
  247. shall mark for update the last data modification and last file status
  248. change timestamps of the file.
  249. .P
  250. If both the O_SYNC and O_DSYNC flags are set, the effect is as if only
  251. the O_SYNC flag was set.
  252. .P
  253. If
  254. .IR path
  255. refers to a STREAMS file,
  256. .IR oflag
  257. may be constructed from O_NONBLOCK OR'ed with either O_RDONLY, O_WRONLY,
  258. or O_RDWR. Other flag values are not applicable to STREAMS devices and
  259. shall have no effect on them. The value O_NONBLOCK affects the operation
  260. of STREAMS drivers and certain functions applied to file descriptors
  261. associated with STREAMS files. For STREAMS drivers, the implementation
  262. of O_NONBLOCK is device-specific.
  263. .P
  264. The application shall ensure that it specifies the O_TTY_INIT flag on the
  265. first open of a terminal device since system boot or since the device
  266. was closed by the process that last had it open. The application need
  267. not specify the O_TTY_INIT flag when opening pseudo-terminals.
  268. If
  269. .IR path
  270. names the master side of a pseudo-terminal device, then it is unspecified
  271. whether
  272. \fIopen\fR()
  273. locks the slave side so that it cannot be opened. Conforming applications
  274. shall call
  275. \fIunlockpt\fR()
  276. before opening the slave side.
  277. .P
  278. The largest value that can be represented correctly in an object of type
  279. .BR off_t
  280. shall be established as the offset maximum in the open file description.
  281. .P
  282. The
  283. \fIopenat\fR()
  284. function shall be equivalent to the
  285. \fIopen\fR()
  286. function except in the case where
  287. .IR path
  288. specifies a relative path. In this case the file to be opened is
  289. determined relative to the directory associated with the file descriptor
  290. .IR fd
  291. instead of the current working directory. If the access mode of the
  292. open file description associated with the file descriptor is not
  293. O_SEARCH, the function shall check whether directory searches are
  294. permitted using the current permissions of the directory underlying
  295. the file descriptor. If the access mode is O_SEARCH, the function
  296. shall not perform the check.
  297. .P
  298. The
  299. .IR oflag
  300. parameter and the optional fourth parameter correspond exactly to the
  301. parameters of
  302. \fIopen\fR().
  303. .P
  304. If
  305. \fIopenat\fR()
  306. is passed the special value AT_FDCWD in the
  307. .IR fd
  308. parameter, the current working directory shall be used and the behavior
  309. shall be identical to a call to
  310. \fIopen\fR().
  311. .SH "RETURN VALUE"
  312. Upon successful completion, these functions shall open the file and
  313. return a non-negative integer representing the file descriptor.
  314. Otherwise, these functions shall return \-1 and set
  315. .IR errno
  316. to indicate the error. If \-1 is returned, no files shall be created
  317. or modified.
  318. .br
  319. .SH ERRORS
  320. These functions shall fail if:
  321. .TP
  322. .BR EACCES
  323. Search permission is denied on a component of the path prefix, or the
  324. file exists and the permissions specified by
  325. .IR oflag
  326. are denied, or the file does not exist and write permission is denied
  327. for the parent directory of the file to be created, or O_TRUNC is
  328. specified and write permission is denied.
  329. .TP
  330. .BR EEXIST
  331. O_CREAT and O_EXCL are set, and the named file exists.
  332. .TP
  333. .BR EINTR
  334. A signal was caught during
  335. \fIopen\fR().
  336. .TP
  337. .BR EINVAL
  338. The implementation does not support synchronized I/O for this file.
  339. .TP
  340. .BR EIO
  341. The
  342. .IR path
  343. argument names a STREAMS file and a hangup or error occurred during the
  344. \fIopen\fR().
  345. .TP
  346. .BR EISDIR
  347. The named file is a directory and
  348. .IR oflag
  349. includes O_WRONLY or O_RDWR, or includes O_CREAT without O_DIRECTORY.
  350. .TP
  351. .BR ELOOP
  352. A loop exists in symbolic links encountered during resolution of the
  353. .IR path
  354. argument, or O_NOFOLLOW was specified and the
  355. .IR path
  356. argument names a symbolic link.
  357. .TP
  358. .BR EMFILE
  359. All file descriptors available to the process are currently open.
  360. .TP
  361. .BR ENAMETOOLONG
  362. .br
  363. The length of a component of a pathname is longer than
  364. {NAME_MAX}.
  365. .TP
  366. .BR ENFILE
  367. The maximum allowable number of files is currently open in the system.
  368. .TP
  369. .BR ENOENT
  370. O_CREAT is not set and a component of
  371. .IR path
  372. does not name an existing file, or O_CREAT is set and a component of
  373. the path prefix of
  374. .IR path
  375. does not name an existing file, or
  376. .IR path
  377. points to an empty string.
  378. .TP
  379. .BR ENOENT " or " ENOTDIR
  380. .br
  381. O_CREAT is set, and the
  382. .IR path
  383. argument contains at least one non-\c
  384. <slash>
  385. character and ends with one or more trailing
  386. <slash>
  387. characters. If
  388. .IR path
  389. without the trailing
  390. <slash>
  391. characters would name an existing file, an
  392. .BR [ENOENT]
  393. error shall not occur.
  394. .TP
  395. .BR ENOSR
  396. The
  397. .IR path
  398. argument names a STREAMS-based file and the system is unable to
  399. allocate a STREAM.
  400. .TP
  401. .BR ENOSPC
  402. The directory or file system that would contain the new file cannot be
  403. expanded, the file does not exist, and O_CREAT is specified.
  404. .TP
  405. .BR ENOTDIR
  406. A component of the path prefix names an existing file that is neither
  407. a directory nor a symbolic link to a directory; or O_CREAT and O_EXCL
  408. are not specified, the
  409. .IR path
  410. argument contains at least one non-\c
  411. <slash>
  412. character and ends with one or more trailing
  413. <slash>
  414. characters, and the last pathname component names an existing file that
  415. is neither a directory nor a symbolic link to a directory; or O_DIRECTORY
  416. was specified and the
  417. .IR path
  418. argument resolves to a non-directory file.
  419. .TP
  420. .BR ENXIO
  421. O_NONBLOCK is set, the named file is a FIFO, O_WRONLY is set, and no
  422. process has the file open for reading.
  423. .TP
  424. .BR ENXIO
  425. The named file is a character special or block special file, and the
  426. device associated with this special file does not exist.
  427. .TP
  428. .BR EOVERFLOW
  429. The named file is a regular file and the size of the file cannot be
  430. represented correctly in an object of type
  431. .BR off_t .
  432. .TP
  433. .BR EROFS
  434. The named file resides on a read-only file system and either O_WRONLY,
  435. O_RDWR, O_CREAT (if the file does not exist), or O_TRUNC is set in the
  436. .IR oflag
  437. argument.
  438. .P
  439. The
  440. \fIopenat\fR()
  441. function shall fail if:
  442. .TP
  443. .BR EACCES
  444. The access mode of the open file description associated with
  445. .IR fd
  446. is not O_SEARCH and the permissions of the directory underlying
  447. .IR fd
  448. do not permit directory searches.
  449. .TP
  450. .BR EBADF
  451. The
  452. .IR path
  453. argument does not specify an absolute path and the
  454. .IR fd
  455. argument is neither AT_FDCWD nor a valid file descriptor open for
  456. reading or searching.
  457. .TP
  458. .BR ENOTDIR
  459. The
  460. .IR path
  461. argument is not an absolute path and
  462. .IR fd
  463. is a file descriptor associated with a non-directory file.
  464. .P
  465. These functions may fail if:
  466. .TP
  467. .BR EAGAIN
  468. The
  469. .IR path
  470. argument names the slave side of a pseudo-terminal device that is locked.
  471. .TP
  472. .BR EINVAL
  473. The value of the
  474. .IR oflag
  475. argument is not valid.
  476. .TP
  477. .BR ELOOP
  478. More than
  479. {SYMLOOP_MAX}
  480. symbolic links were encountered during resolution of the
  481. .IR path
  482. argument.
  483. .TP
  484. .BR ENAMETOOLONG
  485. .br
  486. The length of a pathname exceeds
  487. {PATH_MAX},
  488. or pathname resolution of a symbolic link produced an intermediate
  489. result with a length that exceeds
  490. {PATH_MAX}.
  491. .TP
  492. .BR ENOMEM
  493. The
  494. .IR path
  495. argument names a STREAMS file and the system is unable to allocate
  496. resources.
  497. .TP
  498. .BR EOPNOTSUPP
  499. The
  500. .IR path
  501. argument names a socket.
  502. .TP
  503. .BR ETXTBSY
  504. The file is a pure procedure (shared text) file that is being executed
  505. and
  506. .IR oflag
  507. is O_WRONLY or O_RDWR.
  508. .LP
  509. .IR "The following sections are informative."
  510. .SH EXAMPLES
  511. .SS "Opening a File for Writing by the Owner"
  512. .P
  513. The following example opens the file
  514. .BR /tmp/file ,
  515. either by creating it (if it does not already exist), or by truncating
  516. its length to 0 (if it does exist). In the former case, if the call
  517. creates a new file, the access permission bits in the file mode of the
  518. file are set to permit reading and writing by the owner, and to permit
  519. reading only by group members and others.
  520. .P
  521. If the call to
  522. \fIopen\fR()
  523. is successful, the file is opened for writing.
  524. .sp
  525. .RS 4
  526. .nf
  527. #include <fcntl.h>
  528. \&...
  529. int fd;
  530. mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
  531. char *pathname = "/tmp/file";
  532. \&...
  533. fd = open(pathname, O_WRONLY | O_CREAT | O_TRUNC, mode);
  534. \&...
  535. .fi
  536. .P
  537. .RE
  538. .SS "Opening a File Using an Existence Check"
  539. .P
  540. The following example uses the
  541. \fIopen\fR()
  542. function to try to create the
  543. .BR LOCKFILE
  544. file and open it for writing. Since the
  545. \fIopen\fR()
  546. function specifies the O_EXCL flag, the call fails if the file already
  547. exists. In that case, the program assumes that someone else is updating
  548. the password file and exits.
  549. .sp
  550. .RS 4
  551. .nf
  552. #include <fcntl.h>
  553. #include <stdio.h>
  554. #include <stdlib.h>
  555. .P
  556. #define LOCKFILE "/etc/ptmp"
  557. \&...
  558. int pfd; /* Integer for file descriptor returned by open() call. */
  559. \&...
  560. if ((pfd = open(LOCKFILE, O_WRONLY | O_CREAT | O_EXCL,
  561. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
  562. {
  563. fprintf(stderr, "Cannot open /etc/ptmp. Try again later.\en");
  564. exit(1);
  565. }
  566. \&...
  567. .fi
  568. .P
  569. .RE
  570. .SS "Opening a File for Writing"
  571. .P
  572. The following example opens a file for writing, creating the file if it
  573. does not already exist. If the file does exist, the system truncates
  574. the file to zero bytes.
  575. .sp
  576. .RS 4
  577. .nf
  578. #include <fcntl.h>
  579. #include <stdio.h>
  580. #include <stdlib.h>
  581. .P
  582. #define LOCKFILE "/etc/ptmp"
  583. \&...
  584. int pfd;
  585. char pathname[PATH_MAX+1];
  586. \&...
  587. if ((pfd = open(pathname, O_WRONLY | O_CREAT | O_TRUNC,
  588. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
  589. {
  590. perror("Cannot open output file\en"); exit(1);
  591. }
  592. \&...
  593. .fi
  594. .P
  595. .RE
  596. .SH "APPLICATION USAGE"
  597. POSIX.1\(hy2008 does not require that terminal parameters be automatically set to
  598. any state on first open, nor that they be reset after the last close. It
  599. is possible for a non-conforming application to leave a terminal device
  600. in a state where the next process to use that device finds it in a
  601. non-conforming state, but has no way of determining this. To ensure
  602. that the device is set to a conforming initial state, applications which
  603. perform a first open of a terminal (other than a pseudo-terminal) should
  604. do so using the O_TTY_INIT flag to set the parameters associated with
  605. the terminal to a conforming state.
  606. .P
  607. Except as specified in this volume of POSIX.1\(hy2017, the flags allowed in
  608. .IR oflag
  609. are not mutually-exclusive and any number of them may be used
  610. simultaneously. Not all combinations of flags make sense. For example,
  611. using O_SEARCH | O_CREAT will successfully open a pre-existing directory
  612. for searching, but if there is no existing file by that name, then
  613. it is unspecified whether a regular file will be created. Likewise,
  614. if a non-directory file descriptor is successfully returned, it is
  615. unspecified whether that descriptor will have execute permissions as if
  616. by O_EXEC (note that it is unspecified whether O_EXEC and O_SEARCH have
  617. the same value).
  618. .SH RATIONALE
  619. Some implementations permit opening FIFOs with O_RDWR. Since FIFOs could
  620. be implemented in other ways, and since two file descriptors can be used
  621. to the same effect, this possibility is left as undefined.
  622. .P
  623. See
  624. .IR "\fIgetgroups\fR\^(\|)"
  625. about the group of a newly created file.
  626. .P
  627. The use of
  628. \fIopen\fR()
  629. to create a regular file is preferable to the use of
  630. \fIcreat\fR(),
  631. because the latter is redundant and included only for historical
  632. reasons.
  633. .P
  634. The use of the O_TRUNC flag on FIFOs and directories (pipes cannot be
  635. \fIopen\fR()-ed)
  636. must be permissible without unexpected side-effects (for example,
  637. \fIcreat\fR()
  638. on a FIFO must not remove data). Since terminal special files might have
  639. type-ahead data stored in the buffer, O_TRUNC should not affect their
  640. content, particularly if a program that normally opens a regular file
  641. should open the current controlling terminal instead. Other file types,
  642. particularly implementation-defined ones, are left implementation-defined.
  643. .P
  644. POSIX.1\(hy2008 permits
  645. .BR [EACCES]
  646. to be returned for conditions other than those explicitly listed.
  647. .P
  648. The O_NOCTTY flag was added to allow applications to avoid unintentionally
  649. acquiring a controlling terminal as a side-effect of opening a terminal
  650. file. This volume of POSIX.1\(hy2017 does not specify how a controlling terminal is acquired,
  651. but it allows an implementation to provide this on
  652. \fIopen\fR()
  653. if the O_NOCTTY flag is not set and other conditions specified in the Base Definitions volume of POSIX.1\(hy2017,
  654. .IR "Chapter 11" ", " "General Terminal Interface"
  655. are met.
  656. .P
  657. In historical implementations the value of O_RDONLY is zero. Because of
  658. that, it is not possible to detect the presence of O_RDONLY and another
  659. option. Future implementations should encode O_RDONLY and O_WRONLY as
  660. bit flags so that:
  661. .sp
  662. .RS 4
  663. .nf
  664. O_RDONLY | O_WRONLY == O_RDWR
  665. .fi
  666. .P
  667. .RE
  668. .P
  669. O_EXEC and O_SEARCH are specified as two of the five file access modes.
  670. Since O_EXEC does not apply to directories, and O_SEARCH only applies
  671. to directories, their values need not be distinct. Since O_RDONLY
  672. has historically had the value zero, implementations are not able to
  673. distinguish between O_SEARCH and O_SEARCH | O_RDONLY, and similarly
  674. for O_EXEC.
  675. .P
  676. In general, the
  677. \fIopen\fR()
  678. function follows the symbolic link if
  679. .IR path
  680. names a symbolic link. However, the
  681. \fIopen\fR()
  682. function, when called with O_CREAT and O_EXCL, is required to fail with
  683. .BR [EEXIST]
  684. if
  685. .IR path
  686. names an existing symbolic link, even if the symbolic link refers
  687. to a nonexistent file. This behavior is required so that privileged
  688. applications can create a new file in a known location without the
  689. possibility that a symbolic link might cause the file to be created in
  690. a different location.
  691. .P
  692. For example, a privileged application that must create a file with a
  693. predictable name in a user-writable directory, such as the user's home
  694. directory, could be compromised if the user creates a symbolic link
  695. with that name that refers to a nonexistent file in a system
  696. directory. If the user can influence the contents of a file, the user
  697. could compromise the system by creating a new system configuration or
  698. spool file that would then be interpreted by the system. The test for a
  699. symbolic link which refers to a nonexisting file must be atomic with
  700. the creation of a new file.
  701. .P
  702. In addition, the
  703. \fIopen\fR()
  704. function refuses to open non-directories if the O_DIRECTORY flag is
  705. set. This avoids race conditions whereby a user might compromise the
  706. system by substituting a hard link to a sensitive file (e.g., a device
  707. or a FIFO) while a privileged application is running, where opening a
  708. file even for read access might have undesirable side-effects.
  709. .P
  710. In addition, the
  711. \fIopen\fR()
  712. function does not follow symbolic links if the O_NOFOLLOW flag is set.
  713. This avoids race conditions whereby a user might compromise the system
  714. by substituting a symbolic link to a sensitive file (e.g., a device)
  715. while a privileged application is running, where opening a file even
  716. for read access might have undesirable side-effects.
  717. .P
  718. The POSIX.1\(hy1990 standard required that the group ID of a newly created file be set to
  719. the group ID of its parent directory or to the effective group ID of
  720. the creating process. FIPS 151\(hy2 required that implementations provide a way
  721. to have the group ID be set to the group ID of the containing
  722. directory, but did not prohibit implementations also supporting a way
  723. to set the group ID to the effective group ID of the creating process.
  724. Conforming applications should not assume which group ID will be used. If
  725. it matters, an application can use
  726. \fIchown\fR()
  727. to set the group ID after the file is created, or determine under what
  728. conditions the implementation will set the desired group ID.
  729. .P
  730. The purpose of the
  731. \fIopenat\fR()
  732. function is to enable opening files in directories other than the
  733. current working directory without exposure to race conditions. Any part
  734. of the path of a file could be changed in parallel to a call to
  735. \fIopen\fR(),
  736. resulting in unspecified behavior. By opening a file descriptor for
  737. the target directory and using the
  738. \fIopenat\fR()
  739. function it can be guaranteed that the opened file is located relative
  740. to the desired directory. Some implementations use the
  741. \fIopenat\fR()
  742. function for other purposes as well. In some cases, if the
  743. .IR oflag
  744. parameter has the O_XATTR bit set, the returned file descriptor provides
  745. access to extended attributes. This functionality is not standardized
  746. here.
  747. .SH "FUTURE DIRECTIONS"
  748. None.
  749. .SH "SEE ALSO"
  750. .IR "\fIchmod\fR\^(\|)",
  751. .IR "\fIclose\fR\^(\|)",
  752. .IR "\fIcreat\fR\^(\|)",
  753. .IR "\fIdirfd\fR\^(\|)",
  754. .IR "\fIdup\fR\^(\|)",
  755. .IR "\fIexec\fR\^",
  756. .IR "\fIfcntl\fR\^(\|)",
  757. .IR "\fIfdopendir\fR\^(\|)",
  758. .IR "\fIlink\fR\^(\|)",
  759. .IR "\fIlseek\fR\^(\|)",
  760. .IR "\fImkdtemp\fR\^(\|)",
  761. .IR "\fImknod\fR\^(\|)",
  762. .IR "\fIread\fR\^(\|)",
  763. .IR "\fIsymlink\fR\^(\|)",
  764. .IR "\fIumask\fR\^(\|)",
  765. .IR "\fIunlockpt\fR\^(\|)",
  766. .IR "\fIwrite\fR\^(\|)"
  767. .P
  768. The Base Definitions volume of POSIX.1\(hy2017,
  769. .IR "Chapter 11" ", " "General Terminal Interface",
  770. .IR "\fB<fcntl.h>\fP",
  771. .IR "\fB<sys_stat.h>\fP",
  772. .IR "\fB<sys_types.h>\fP"
  773. .\"
  774. .SH COPYRIGHT
  775. Portions of this text are reprinted and reproduced in electronic form
  776. from IEEE Std 1003.1-2017, Standard for Information Technology
  777. -- Portable Operating System Interface (POSIX), The Open Group Base
  778. Specifications Issue 7, 2018 Edition,
  779. Copyright (C) 2018 by the Institute of
  780. Electrical and Electronics Engineers, Inc and The Open Group.
  781. In the event of any discrepancy between this version and the original IEEE and
  782. The Open Group Standard, the original IEEE and The Open Group Standard
  783. is the referee document. The original Standard can be obtained online at
  784. http://www.opengroup.org/unix/online.html .
  785. .PP
  786. Any typographical or formatting errors that appear
  787. in this page are most likely
  788. to have been introduced during the conversion of the source files to
  789. man page format. To report such errors, see
  790. https://www.kernel.org/doc/man-pages/reporting_bugs.html .