You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

child.hpp 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /// \file child.hpp
  2. //
  3. // Copyright (C) 2014 MicroNeil Research Corporation.
  4. //
  5. // This program is part of the MicroNeil Research Open Library Project. For
  6. // more information go to http://www.microneil.com/OpenLibrary/index.html
  7. //
  8. // This program is free software; you can redistribute it and/or modify it
  9. // under the terms of the GNU General Public License as published by the
  10. // Free Software Foundation; either version 2 of the License, or (at your
  11. // option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful, but WITHOUT
  14. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. // more details.
  17. //
  18. // You should have received a copy of the GNU General Public License along with
  19. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  20. // Place, Suite 330, Boston, MA 02111-1307 USA
  21. //==============================================================================
  22. /*
  23. \brief The child module provides classes to spawn and communicate
  24. with child processes.
  25. */
  26. #ifndef CHILD_HPP
  27. #define CHILD_HPP
  28. #ifdef WIN32
  29. #include <windows.h>
  30. #endif
  31. #include <cstdint>
  32. #include <streambuf>
  33. #include <istream>
  34. #include <ostream>
  35. #include <string>
  36. #include <vector>
  37. namespace CodeDweller {
  38. /**
  39. \namespace CodeDweller
  40. The CodeDweller namespace contains components providing high-level
  41. functionality for applications.
  42. */
  43. /** Class that abstracts the creation of child processes.
  44. This class provides functionality to create a child process,
  45. communicate with the child process via streams and signals, and
  46. obtain the exit code of the child process.
  47. */
  48. class Child {
  49. private:
  50. /// Streambuf class for reading the standard output of the child
  51. /// process.
  52. class ReadStreambuf : public std::streambuf {
  53. public:
  54. /// Reader streambuf constructor.
  55. //
  56. // \param[in] bufferSize is the size in bytes of the input
  57. // buffer.
  58. //
  59. explicit ReadStreambuf(std::size_t bufferSize = 4096);
  60. #ifdef WIN32
  61. /// Set the handle to read the standard output of the child
  62. /// process.
  63. //
  64. // \param[in] inHandle is the input handle for the standard
  65. // output of the child process.
  66. //
  67. void setInputHandle(HANDLE inHandle);
  68. #else
  69. /// Set the file descriptor to read the standard output of the
  70. /// child process.
  71. //
  72. // \param[in] inFd is the input file descriptor for the standard
  73. // output of the child process.
  74. //
  75. void setInputFileDescriptor(int inFd);
  76. #endif
  77. private:
  78. /// Override streambuf::underflow().
  79. int_type underflow();
  80. /// Copy constructor not implemented.
  81. ReadStreambuf(const ReadStreambuf &);
  82. /// Copy constructor not implemented.
  83. ReadStreambuf &operator=(const ReadStreambuf &);
  84. /// Input handle.
  85. #ifdef WIN32
  86. HANDLE inputHandle;
  87. #else
  88. int inputFileDescriptor;
  89. #endif
  90. /// Read buffer.
  91. std::vector<char> buffer;
  92. };
  93. /// Streambuf class for writing to the standard input of the child
  94. /// process.
  95. //
  96. // Note: If an error occurs when writing the output from the
  97. // parent process, the output buffer is cleared.
  98. //
  99. class WriteStreambuf : public std::streambuf {
  100. public:
  101. /// Writer streambuf constructor.
  102. //
  103. // \param[in] bufferSize is the size in bytes of the input
  104. // buffer.
  105. //
  106. explicit WriteStreambuf(std::size_t bufferSize = 4096);
  107. #ifdef WIN32
  108. /// Set the handle to write the standard input of the child
  109. /// process.
  110. //
  111. // \param[in] outHandle is the output handle for the standard
  112. // input of the child process.
  113. //
  114. void setOutputHandle(HANDLE outHandle);
  115. #else
  116. /// Set the file descriptor to write the standard input of the
  117. /// child process.
  118. //
  119. // \param[in] outFd is the output file descriptor for the
  120. // standard input of the child process.
  121. //
  122. void setOutputFileDescriptor(int outFd);
  123. #endif
  124. private:
  125. /// Flush the output buffer.
  126. void flushBuffer();
  127. /// Override streambuf::overflow().
  128. int_type overflow(int_type ch);
  129. /// Override streambuf::sync().
  130. int sync();
  131. /// Copy constructor not implemented.
  132. WriteStreambuf(const WriteStreambuf &);
  133. /// Copy constructor not implemented.
  134. WriteStreambuf &operator=(const WriteStreambuf &);
  135. /// Output handle.
  136. #ifdef WIN32
  137. HANDLE outputHandle;
  138. #else
  139. int outputFileDescriptor;
  140. #endif
  141. /// Write buffer.
  142. std::vector<char> buffer;
  143. };
  144. /// Stream buffer for reading to the stdout of the child process;
  145. ReadStreambuf readStreambuf;
  146. /// Stream buffer for writing to the stdin of the child process;
  147. WriteStreambuf writeStreambuf;
  148. public:
  149. /** Constructor for spawning with command-line parameters.
  150. The constructor configures the object, but doesn't spawn the
  151. child process.
  152. \param[in] args contains the child executable file name and
  153. command-line parameters. args[0] contains the full path of the
  154. executable, and args[1] thru args[n] are the command-line
  155. parameters.
  156. \param[in] bufSize is the buffer size of the reader and writer
  157. streams used to communicate with the child process.
  158. */
  159. Child(std::vector<std::string> args, size_t bufSize = 4096);
  160. /** Constructor for spawning without command-line parameters.
  161. The constructor configures the object, but doesn't spawn the
  162. child process.
  163. \param[in] childpath contains the child executable file name.
  164. \param[in] bufSize is the buffer size of the reader and writer
  165. streams used to communicate with the child process.
  166. */
  167. Child(std::string childpath, size_t bufSize = 4096);
  168. /** Destructor terminates the child process. */
  169. ~Child();
  170. /// Input stream to read data from the child's standard output.
  171. std::istream reader;
  172. /// Output stream to write data to the child's standard input.
  173. std::ostream writer;
  174. /** Spawn the child process.
  175. \throws runtime_error if an error occurs.
  176. */
  177. void run();
  178. /** Terminite the child process.
  179. \throws runtime_error if an error occurs.
  180. \throws logic_error if the child process is not running.
  181. */
  182. void terminate();
  183. /** Check whether the child process has exited.
  184. \returns True if the child process has exited, false
  185. otherwise.
  186. \throws runtime_error if an error occurs.
  187. \throws logic_error if the child process is not running.
  188. */
  189. bool isDone();
  190. /** Get the exit value of the child process.
  191. \returns The exit value of the child process if the child
  192. process has exited.
  193. \throws runtime_error if an error occurs.
  194. \throws logic_error if the child process has not exited.
  195. \throws logic_error if the child process is not running.
  196. */
  197. int32_t result();
  198. private:
  199. /// Exit code to use when terminating the child process.
  200. static const uint32_t terminateExitCode = 0;
  201. /// True if the child process was successfully started.
  202. bool childStarted;
  203. /// True if the child process has exited.
  204. bool childExited;
  205. /// Initialize data members.
  206. void init();
  207. /// Child executable path and command-line parameters.
  208. std::vector<std::string> cmdArgs;
  209. /// Child executable path and command-line parameters.
  210. std::string cmdline;
  211. #ifdef WIN32
  212. /// Child's process handle.
  213. HANDLE childProcess;
  214. /// Child's thread handle.
  215. HANDLE childThread;
  216. #else
  217. /// Child process ID.
  218. pid_t childPid;
  219. #endif
  220. /// Exit value of the process.
  221. int32_t exitCode;
  222. /// True if the exit code has been obtained.
  223. bool exitCodeObtainedFlag;
  224. /// Return text for the most recent error.
  225. //
  226. // \returns Human-readable description of the most recent error.
  227. //
  228. static std::string getErrorText();
  229. };
  230. }
  231. #endif // CHILD_HPP