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

dup.3p (6278B)


  1. '\" et
  2. .TH DUP "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. dup,
  12. dup2
  13. \(em duplicate an open file descriptor
  14. .SH SYNOPSIS
  15. .LP
  16. .nf
  17. #include <unistd.h>
  18. .P
  19. int dup(int \fIfildes\fP);
  20. int dup2(int \fIfildes\fP, int \fIfildes2\fP);
  21. .fi
  22. .SH DESCRIPTION
  23. The
  24. \fIdup\fR()
  25. function provides an alternative interface to the service provided by
  26. \fIfcntl\fR()
  27. using the F_DUPFD command. The call
  28. .IR dup ( fildes )
  29. shall be equivalent to:
  30. .sp
  31. .RS 4
  32. .nf
  33. fcntl(fildes, F_DUPFD, 0);
  34. .fi
  35. .P
  36. .RE
  37. .P
  38. The
  39. \fIdup2\fR()
  40. function shall cause the file descriptor
  41. .IR fildes2
  42. to refer to the same open file description as the file descriptor
  43. .IR fildes
  44. and to share any locks, and shall return
  45. .IR fildes2 .
  46. If
  47. .IR fildes2
  48. is already a valid open file descriptor, it shall be closed first, unless
  49. .IR fildes
  50. is equal to
  51. .IR fildes2
  52. in which case
  53. \fIdup2\fR()
  54. shall return
  55. .IR fildes2
  56. without closing it. If the close operation fails to close
  57. .IR fildes2 ,
  58. \fIdup2\fR()
  59. shall return \-1 without changing the open file description to which
  60. .IR fildes2
  61. refers. If
  62. .IR fildes
  63. is not a valid file descriptor,
  64. \fIdup2\fR()
  65. shall return \-1 and shall not close
  66. .IR fildes2 .
  67. If
  68. .IR fildes2
  69. is less than 0 or greater than or equal to
  70. {OPEN_MAX},
  71. \fIdup2\fR()
  72. shall return \-1 with
  73. .IR errno
  74. set to
  75. .BR [EBADF] .
  76. .P
  77. Upon successful completion, if
  78. .IR fildes
  79. is not equal to
  80. .IR fildes2 ,
  81. the FD_CLOEXEC flag associated with
  82. .IR fildes2
  83. shall be cleared. If
  84. .IR fildes
  85. is equal to
  86. .IR fildes2 ,
  87. the FD_CLOEXEC flag associated with
  88. .IR fildes2
  89. shall not be changed.
  90. .P
  91. If
  92. .IR fildes
  93. refers to a typed memory object, the result of the
  94. \fIdup2\fR()
  95. function is unspecified.
  96. .SH "RETURN VALUE"
  97. Upon successful completion a non-negative integer, namely the file
  98. descriptor, shall be returned; otherwise, \-1 shall be returned
  99. and
  100. .IR errno
  101. set to indicate the error.
  102. .SH ERRORS
  103. The
  104. \fIdup\fR()
  105. function shall fail if:
  106. .TP
  107. .BR EBADF
  108. The
  109. .IR fildes
  110. argument is not a valid open file descriptor.
  111. .TP
  112. .BR EMFILE
  113. All file descriptors available to the process are currently open.
  114. .P
  115. The
  116. \fIdup2\fR()
  117. function shall fail if:
  118. .TP
  119. .BR EBADF
  120. The
  121. .IR fildes
  122. argument is not a valid open file descriptor or the argument
  123. .IR fildes2
  124. is negative or greater than or equal to
  125. {OPEN_MAX}.
  126. .TP
  127. .BR EINTR
  128. The
  129. \fIdup2\fR()
  130. function was interrupted by a signal.
  131. .P
  132. The
  133. \fIdup2\fR()
  134. function may fail if:
  135. .TP
  136. .BR EIO
  137. An I/O error occurred while attempting to close
  138. .IR fildes2 .
  139. .LP
  140. .IR "The following sections are informative."
  141. .SH EXAMPLES
  142. .SS "Redirecting Standard Output to a File" S
  143. .P
  144. The following example closes standard output for the current processes,
  145. re-assigns standard output to go to the file referenced by
  146. .IR pfd ,
  147. and closes the original file descriptor to clean up.
  148. .sp
  149. .RS 4
  150. .nf
  151. #include <unistd.h>
  152. \&...
  153. int pfd;
  154. \&...
  155. close(1);
  156. dup(pfd);
  157. close(pfd);
  158. \&...
  159. .fi
  160. .P
  161. .RE
  162. .SS "Redirecting Error Messages"
  163. .P
  164. The following example redirects messages from
  165. .IR stderr
  166. to
  167. .IR stdout .
  168. .sp
  169. .RS 4
  170. .nf
  171. #include <unistd.h>
  172. \&...
  173. dup2(1, 2);
  174. \&...
  175. .fi
  176. .P
  177. .RE
  178. .SH "APPLICATION USAGE"
  179. Implementations may use file descriptors that must be inherited into
  180. child processes for the child process to remain conforming, such as for
  181. message catalog or tracing purposes. Therefore, an application that calls
  182. \fIdup2\fR()
  183. with an arbitrary integer for
  184. .IR fildes2
  185. risks non-conforming behavior, and
  186. \fIdup2\fR()
  187. can only portably be used to overwrite file descriptor values that the
  188. application has obtained through explicit actions, or for the three file
  189. descriptors corresponding to the standard file streams. In order to avoid
  190. a race condition of leaking an unintended file descriptor into a child
  191. process, an application should consider opening all file descriptors
  192. with the FD_CLOEXEC bit set unless the file descriptor is intended to
  193. be inherited across
  194. .IR exec .
  195. .SH RATIONALE
  196. The
  197. \fIdup\fR()
  198. function is redundant. Its services are also provided by the
  199. \fIfcntl\fR()
  200. function. It has been included in this volume of POSIX.1\(hy2017 primarily for historical reasons,
  201. since many existing applications use it. On the other hand, the
  202. \fIdup2\fR()
  203. function provides unique services, as no other interface is able to
  204. atomically replace an existing file descriptor.
  205. .P
  206. The
  207. \fIdup2\fR()
  208. function is not marked obsolescent because it presents a type-safe
  209. version of functionality provided in a type-unsafe version by
  210. \fIfcntl\fR().
  211. It is used in the POSIX Ada binding.
  212. .P
  213. The
  214. \fIdup2\fR()
  215. function is not intended for use in critical regions as a
  216. synchronization mechanism.
  217. .P
  218. In the description of
  219. .BR [EBADF] ,
  220. the case of
  221. .IR fildes
  222. being out of range is covered by the given case of
  223. .IR fildes
  224. not being valid. The descriptions for
  225. .IR fildes
  226. and
  227. .IR fildes2
  228. are different because the only kind of invalidity that is relevant for
  229. .IR fildes2
  230. is whether it is out of range; that is, it does not matter whether
  231. .IR fildes2
  232. refers to an open file when the
  233. \fIdup2\fR()
  234. call is made.
  235. .SH "FUTURE DIRECTIONS"
  236. None.
  237. .SH "SEE ALSO"
  238. .IR "\fIclose\fR\^(\|)",
  239. .IR "\fIfcntl\fR\^(\|)",
  240. .IR "\fIopen\fR\^(\|)"
  241. .P
  242. The Base Definitions volume of POSIX.1\(hy2017,
  243. .IR "\fB<unistd.h>\fP"
  244. .\"
  245. .SH COPYRIGHT
  246. Portions of this text are reprinted and reproduced in electronic form
  247. from IEEE Std 1003.1-2017, Standard for Information Technology
  248. -- Portable Operating System Interface (POSIX), The Open Group Base
  249. Specifications Issue 7, 2018 Edition,
  250. Copyright (C) 2018 by the Institute of
  251. Electrical and Electronics Engineers, Inc and The Open Group.
  252. In the event of any discrepancy between this version and the original IEEE and
  253. The Open Group Standard, the original IEEE and The Open Group Standard
  254. is the referee document. The original Standard can be obtained online at
  255. http://www.opengroup.org/unix/online.html .
  256. .PP
  257. Any typographical or formatting errors that appear
  258. in this page are most likely
  259. to have been introduced during the conversion of the source files to
  260. man page format. To report such errors, see
  261. https://www.kernel.org/doc/man-pages/reporting_bugs.html .