Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

child.hpp 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 <iostream>
  34. #include <string>
  35. #include <vector>
  36. namespace CodeDweller {
  37. /**
  38. \namespace CodeDweller
  39. The CodeDweller namespace contains components providing high-level
  40. functionality for applications.
  41. */
  42. /** Class that abstracts the communication with a child process.
  43. This class provides functionality to create a child process,
  44. communicate with the child process via streams and signals, and
  45. obtain the exit code of the child process.
  46. */
  47. class ChildStream : public std::iostream {
  48. private:
  49. /// Streambuf class for reading from the standard output and
  50. /// writing to the standard input of the child process.
  51. class ChildStreambuf : public std::streambuf {
  52. friend class ChildStream;
  53. public:
  54. /// Constructor.
  55. //
  56. // \param[in] bufferSize is the size in bytes of the input
  57. // buffer and output buffer.
  58. //
  59. explicit ChildStreambuf(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. /// Set the handle to write the standard input of the child
  69. /// process.
  70. //
  71. // \param[in] outHandle is the output handle for the standard
  72. // input of the child process.
  73. //
  74. void setOutputHandle(HANDLE outHandle);
  75. #else
  76. /// Set the file descriptor to read the standard output of the
  77. /// child process.
  78. //
  79. // \param[in] inFd is the input file descriptor for the standard
  80. // output of the child process.
  81. //
  82. void setInputFileDescriptor(int inFd);
  83. /// Set the file descriptor to write the standard input of the
  84. /// child process.
  85. //
  86. // \param[in] outFd is the output file descriptor for the
  87. // standard input of the child process.
  88. //
  89. void setOutputFileDescriptor(int outFd);
  90. #endif
  91. private:
  92. /** Return the number of bytes that can be read without
  93. blocking.
  94. This method checks if any input is available from the pipe,
  95. and returns the number of bytes in the input buffer plus 1.
  96. Reading that number of bytes will not block. Reading a
  97. larger number of bytes might block.
  98. \returns minimum number of bytes that can be read without
  99. blocking.
  100. */
  101. size_t numBytesAvailable() const;
  102. /// Override streambuf::underflow().
  103. int_type underflow();
  104. /// Flush the output buffer.
  105. void flushBuffer();
  106. /// Override streambuf::overflow().
  107. int_type overflow(int_type ch);
  108. /// Override streambuf::sync().
  109. int sync();
  110. /// Copy constructor not implemented.
  111. ChildStreambuf(const ChildStreambuf &) = delete;
  112. /// Assignment operator not implemented.
  113. ChildStreambuf &operator=(const ChildStreambuf &) = delete;
  114. /// Input and output handles.
  115. #ifdef _WIN32
  116. HANDLE inputHandle;
  117. HANDLE outputHandle;
  118. #else
  119. int inputFileDescriptor;
  120. int outputFileDescriptor;
  121. #endif
  122. /// Read buffer.
  123. std::vector<char> readBuffer;
  124. /// Write buffer.
  125. std::vector<char> writeBuffer;
  126. };
  127. /// Stream buffer for reading from the stdout and writing to the
  128. /// stdin of the child process.
  129. ChildStreambuf childStreambuf;
  130. public:
  131. /** Constructor for spawning with command-line parameters.
  132. The constructor configures the object, and spawns the child
  133. process.
  134. \param[in] args contains the child executable file name and
  135. command-line parameters. args[0] contains the full path of the
  136. executable, and args[1] thru args[n] are the command-line
  137. parameters.
  138. \param[in] bufSize is the input and output buffer size of the
  139. stream used to communicate with the child process.
  140. \throws runtime_error if an error occurs.
  141. */
  142. ChildStream(std::vector<std::string> const &args, size_t bufSize = 4096);
  143. /** Constructor for spawning without command-line parameters.
  144. The constructor configures the object, and spawns the child
  145. process.
  146. \param[in] childpath contains the child executable file name.
  147. \param[in] bufSize is the input and output buffer size of the
  148. stream used to communicate with the child process.
  149. \throws runtime_error if an error occurs.
  150. */
  151. ChildStream(std::string const &childpath, size_t bufSize = 4096);
  152. /** Constructor.
  153. The constructor configures the I/O buffers, but doesn't spawn
  154. any child process.
  155. \param[in] bufSize is the input and output buffer size of the
  156. stream used to communicate with the child process.
  157. */
  158. ChildStream(size_t bufSize = 4096);
  159. /** Destructor terminates the child process. */
  160. ~ChildStream();
  161. /** Spawn the child process.
  162. \param[in] args contains the child executable file name and
  163. command-line parameters. args[0] contains the full path of the
  164. executable, and args[1] thru args[n] are the command-line
  165. parameters.
  166. \throws runtime_error if an error occurs.
  167. \throws runtime_error if an error occurs.
  168. */
  169. void open(std::vector<std::string> const &args);
  170. /** Spawn the child process.
  171. \param[in] childpath contains the child executable file name.
  172. \throws runtime_error if an error occurs.
  173. */
  174. void open(std::string const &childpath);
  175. /** Get the number of bytes available for input.
  176. @returns number of bytes that can be read without blocking.
  177. */
  178. size_t numBytesAvailable() const;
  179. /** Check whether the child process is running.
  180. \returns True if the child process is running, false
  181. otherwise.
  182. */
  183. bool isRunning() const;
  184. /** Terminite the child process.
  185. \throws runtime_error if an error occurs.
  186. \throws logic_error if the child process is not running.
  187. */
  188. void close();
  189. /** Check whether the child process has exited.
  190. \returns True if the child process has exited, false
  191. otherwise.
  192. \throws runtime_error if an error occurs.
  193. \throws logic_error if the child process is not running.
  194. */
  195. bool isDone();
  196. /** Get the exit value of the child process.
  197. \returns The exit value of the child process if the child
  198. process has exited.
  199. \throws runtime_error if an error occurs.
  200. \throws logic_error if the child process has not exited.
  201. \throws logic_error if the child process is not running.
  202. */
  203. int32_t result();
  204. private:
  205. /** Spawn the child process.
  206. \throws runtime_error if an error occurs.
  207. */
  208. void run();
  209. /// Exit code to use when terminating the child process.
  210. static const uint32_t terminateExitCode = 0;
  211. /// True if the child process was successfully started.
  212. bool childStarted;
  213. /// True if the child process has exited.
  214. bool childExited;
  215. /// Initialize data members.
  216. void init();
  217. /// Child executable path and command-line parameters.
  218. std::vector<std::string> cmdArgs;
  219. #ifdef _WIN32
  220. /// Child's process handle.
  221. HANDLE childProcess;
  222. /// Child's thread handle.
  223. HANDLE childThread;
  224. #else
  225. /// Child process ID.
  226. pid_t childPid;
  227. #endif
  228. /// Exit value of the process.
  229. int32_t exitCode;
  230. /// True if the exit code has been obtained.
  231. bool exitCodeObtainedFlag;
  232. /// Return text for the most recent error.
  233. //
  234. // \returns Human-readable description of the most recent error.
  235. //
  236. static std::string getErrorText();
  237. };
  238. }
  239. #endif // CHILD_HPP