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

pclose.3p (6028B)


  1. '\" et
  2. .TH PCLOSE "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. pclose
  12. \(em close a pipe stream to or from a process
  13. .SH SYNOPSIS
  14. .LP
  15. .nf
  16. #include <stdio.h>
  17. .P
  18. int pclose(FILE *\fIstream\fP);
  19. .fi
  20. .SH DESCRIPTION
  21. The
  22. \fIpclose\fR()
  23. function shall close a stream that was opened by
  24. \fIpopen\fR(),
  25. wait for the command to terminate, and return the termination status
  26. of the process that was running the command language interpreter.
  27. However, if a call caused the termination status to be unavailable to
  28. \fIpclose\fR(),
  29. then
  30. \fIpclose\fR()
  31. shall return \-1 with
  32. .IR errno
  33. set to
  34. .BR [ECHILD]
  35. to report this situation. This can happen if the application calls one
  36. of the following functions:
  37. .IP " *" 4
  38. \fIwait\fR()
  39. .IP " *" 4
  40. \fIwaitpid\fR()
  41. with a
  42. .IR pid
  43. argument less than or equal to 0 or equal to the process ID of the
  44. command line interpreter
  45. .IP " *" 4
  46. Any other function not defined in this volume of POSIX.1\(hy2017 that could do one of the above
  47. .P
  48. In any case,
  49. \fIpclose\fR()
  50. shall not return before the child process created by
  51. \fIpopen\fR()
  52. has terminated.
  53. .P
  54. If the command language interpreter cannot be executed, the child
  55. termination status returned by
  56. \fIpclose\fR()
  57. shall be as if the command language interpreter terminated using
  58. .IR exit (127)
  59. or
  60. .IR _exit (127).
  61. .P
  62. The
  63. \fIpclose\fR()
  64. function shall not affect the termination status of any child of the
  65. calling process other than the one created by
  66. \fIpopen\fR()
  67. for the associated stream.
  68. .P
  69. If the argument
  70. .IR stream
  71. to
  72. \fIpclose\fR()
  73. is not a pointer to a stream created by
  74. \fIpopen\fR(),
  75. the result of
  76. \fIpclose\fR()
  77. is undefined.
  78. .P
  79. If a thread is canceled during execution of
  80. \fIpclose\fR(),
  81. the behavior is undefined.
  82. .SH "RETURN VALUE"
  83. Upon successful return,
  84. \fIpclose\fR()
  85. shall return the termination status of the command language
  86. interpreter. Otherwise,
  87. \fIpclose\fR()
  88. shall return \-1 and set
  89. .IR errno
  90. to indicate the error.
  91. .SH ERRORS
  92. The
  93. \fIpclose\fR()
  94. function shall fail if:
  95. .TP
  96. .BR ECHILD
  97. The status of the child process could not be obtained, as described
  98. above.
  99. .LP
  100. .IR "The following sections are informative."
  101. .SH EXAMPLES
  102. None.
  103. .SH "APPLICATION USAGE"
  104. None.
  105. .SH RATIONALE
  106. There is a requirement that
  107. \fIpclose\fR()
  108. not return before the child process terminates. This is intended to
  109. disallow implementations that return
  110. .BR [EINTR]
  111. if a signal is received while waiting. If
  112. \fIpclose\fR()
  113. returned before the child terminated, there would be no way for the
  114. application to discover which child used to be associated with the
  115. stream, and it could not do the cleanup itself.
  116. .P
  117. If the stream pointed to by
  118. .IR stream
  119. was not created by
  120. \fIpopen\fR(),
  121. historical implementations of
  122. \fIpclose\fR()
  123. return \-1 without setting
  124. .IR errno .
  125. To avoid requiring
  126. \fIpclose\fR()
  127. to set
  128. .IR errno
  129. in this case, POSIX.1\(hy2008 makes the behavior unspecified. An application
  130. should not use
  131. \fIpclose\fR()
  132. to close any stream that was not created by
  133. \fIpopen\fR().
  134. .P
  135. Some historical implementations of
  136. \fIpclose\fR()
  137. either block or ignore the signals SIGINT, SIGQUIT, and SIGHUP while
  138. waiting for the child process to terminate. Since this behavior is not
  139. described for the
  140. \fIpclose\fR()
  141. function in POSIX.1\(hy2008, such implementations are not conforming. Also, some
  142. historical implementations return
  143. .BR [EINTR]
  144. if a signal is received, even though the child process has not
  145. terminated. Such implementations are also considered non-conforming.
  146. .P
  147. Consider, for example, an application that uses:
  148. .sp
  149. .RS 4
  150. .nf
  151. popen("command", "r")
  152. .fi
  153. .P
  154. .RE
  155. .P
  156. to start
  157. .IR command ,
  158. which is part of the same application. The parent writes a prompt to
  159. its standard output (presumably the terminal) and then reads from the
  160. \fIpopen\fR()ed
  161. stream. The child reads the response from the user, does some
  162. transformation on the response (pathname expansion, perhaps) and
  163. writes the result to its standard output. The parent process reads the
  164. result from the pipe, does something with it, and prints another
  165. prompt. The cycle repeats. Assuming that both processes do appropriate
  166. buffer flushing, this would be expected to work.
  167. .P
  168. To conform to POSIX.1\(hy2008,
  169. \fIpclose\fR()
  170. must use
  171. \fIwaitpid\fR(),
  172. or some similar function, instead of
  173. \fIwait\fR().
  174. .P
  175. The code sample below illustrates how the
  176. \fIpclose\fR()
  177. function might be implemented on a system conforming to POSIX.1\(hy2008.
  178. .sp
  179. .RS 4
  180. .nf
  181. int pclose(FILE *stream)
  182. {
  183. int stat;
  184. pid_t pid;
  185. .P
  186. pid = <pid for process created for stream by popen()>
  187. (void) fclose(stream);
  188. while (waitpid(pid, &stat, 0) == -1) {
  189. if (errno != EINTR){
  190. stat = -1;
  191. break;
  192. }
  193. }
  194. return(stat);
  195. }
  196. .fi
  197. .P
  198. .RE
  199. .SH "FUTURE DIRECTIONS"
  200. None.
  201. .SH "SEE ALSO"
  202. .IR "\fIfork\fR\^(\|)",
  203. .IR "\fIpopen\fR\^(\|)",
  204. .IR "\fIwait\fR\^(\|)"
  205. .P
  206. The Base Definitions volume of POSIX.1\(hy2017,
  207. .IR "\fB<stdio.h>\fP"
  208. .\"
  209. .SH COPYRIGHT
  210. Portions of this text are reprinted and reproduced in electronic form
  211. from IEEE Std 1003.1-2017, Standard for Information Technology
  212. -- Portable Operating System Interface (POSIX), The Open Group Base
  213. Specifications Issue 7, 2018 Edition,
  214. Copyright (C) 2018 by the Institute of
  215. Electrical and Electronics Engineers, Inc and The Open Group.
  216. In the event of any discrepancy between this version and the original IEEE and
  217. The Open Group Standard, the original IEEE and The Open Group Standard
  218. is the referee document. The original Standard can be obtained online at
  219. http://www.opengroup.org/unix/online.html .
  220. .PP
  221. Any typographical or formatting errors that appear
  222. in this page are most likely
  223. to have been introduced during the conversion of the source files to
  224. man page format. To report such errors, see
  225. https://www.kernel.org/doc/man-pages/reporting_bugs.html .