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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 creation of child processes.
  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 Child {
  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 Child;
  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, but doesn't spawn the
  133. child 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 buffer size of the reader and writer
  139. streams used to communicate with the child process.
  140. */
  141. Child(std::vector<std::string> args, size_t bufSize = 4096);
  142. /** Constructor for spawning without command-line parameters.
  143. The constructor configures the object, but doesn't spawn the
  144. child process.
  145. \param[in] childpath contains the child executable file name.
  146. \param[in] bufSize is the buffer size of the reader and writer
  147. streams used to communicate with the child process.
  148. */
  149. Child(std::string childpath, size_t bufSize = 4096);
  150. /** Destructor terminates the child process. */
  151. ~Child();
  152. /// I/O stream to read data from the child's standard output and
  153. /// write data to the child's standard input.
  154. std::iostream childStream;
  155. /** Get the number of bytes available for input.
  156. @returns number of bytes that can be read from reader without
  157. blocking.
  158. */
  159. size_t numBytesAvailable() const;
  160. /** Spawn the child process.
  161. \throws runtime_error if an error occurs.
  162. */
  163. void run();
  164. /** Terminite the child process.
  165. \throws runtime_error if an error occurs.
  166. \throws logic_error if the child process is not running.
  167. */
  168. void terminate();
  169. /** Check whether the child process has exited.
  170. \returns True if the child process has exited, false
  171. otherwise.
  172. \throws runtime_error if an error occurs.
  173. \throws logic_error if the child process is not running.
  174. */
  175. bool isDone();
  176. /** Get the exit value of the child process.
  177. \returns The exit value of the child process if the child
  178. process has exited.
  179. \throws runtime_error if an error occurs.
  180. \throws logic_error if the child process has not exited.
  181. \throws logic_error if the child process is not running.
  182. */
  183. int32_t result();
  184. private:
  185. /// Exit code to use when terminating the child process.
  186. static const uint32_t terminateExitCode = 0;
  187. /// True if the child process was successfully started.
  188. bool childStarted;
  189. /// True if the child process has exited.
  190. bool childExited;
  191. /// Initialize data members.
  192. void init();
  193. /// Child executable path and command-line parameters.
  194. std::vector<std::string> cmdArgs;
  195. /// Child executable path and command-line parameters.
  196. std::string cmdline;
  197. #ifdef _WIN32
  198. /// Child's process handle.
  199. HANDLE childProcess;
  200. /// Child's thread handle.
  201. HANDLE childThread;
  202. #else
  203. /// Child process ID.
  204. pid_t childPid;
  205. #endif
  206. /// Exit value of the process.
  207. int32_t exitCode;
  208. /// True if the exit code has been obtained.
  209. bool exitCodeObtainedFlag;
  210. /// Return text for the most recent error.
  211. //
  212. // \returns Human-readable description of the most recent error.
  213. //
  214. static std::string getErrorText();
  215. };
  216. }
  217. #endif // CHILD_HPP