reproc.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #pragma once
  2. #include <chrono>
  3. #include <cstdint>
  4. #include <cstdio>
  5. #include <memory>
  6. #include <system_error>
  7. #include <utility>
  8. #include <reproc++/arguments.hpp>
  9. #include <reproc++/env.hpp>
  10. #include <reproc++/export.hpp>
  11. #include <reproc++/input.hpp>
  12. // Forward declare `reproc_t` so we don't have to include reproc.h in the
  13. // header.
  14. struct reproc_t;
  15. /*! The `reproc` namespace wraps all reproc++ declarations. `process` wraps
  16. reproc's API inside a C++ class. To avoid exposing reproc's API when using
  17. reproc++ all structs, enums and constants of reproc have a replacement in
  18. reproc++. Only differences in behaviour compared to reproc are documented. Refer
  19. to reproc.h and the examples for general information on how to use reproc. */
  20. namespace reproc {
  21. /*! Conversion from reproc `errno` constants to `std::errc` constants:
  22. https://en.cppreference.com/w/cpp/error/errc */
  23. using error = std::errc;
  24. namespace signal {
  25. REPROCXX_EXPORT extern const int kill;
  26. REPROCXX_EXPORT extern const int terminate;
  27. }
  28. /*! Timeout values are passed as `reproc::milliseconds` instead of `int` in
  29. reproc++. */
  30. using milliseconds = std::chrono::duration<int, std::milli>;
  31. REPROCXX_EXPORT extern const milliseconds infinite;
  32. REPROCXX_EXPORT extern const milliseconds deadline;
  33. enum class stop {
  34. noop,
  35. wait,
  36. terminate,
  37. kill,
  38. };
  39. struct stop_action {
  40. stop action;
  41. milliseconds timeout;
  42. };
  43. struct stop_actions {
  44. stop_action first;
  45. stop_action second;
  46. stop_action third;
  47. };
  48. #if defined(_WIN32)
  49. using handle = void *;
  50. #else
  51. using handle = int;
  52. #endif
  53. struct redirect {
  54. enum type {
  55. default_, // Unfortunately, both `default` and `auto` are keywords.
  56. pipe,
  57. parent,
  58. discard,
  59. // stdout would conflict with a macro on Windows.
  60. stdout_,
  61. // Unfortunately, class members and nested enum members can't have the same
  62. // name.
  63. handle_,
  64. file_,
  65. path_,
  66. };
  67. enum type type;
  68. reproc::handle handle;
  69. FILE *file;
  70. const char *path;
  71. };
  72. struct options {
  73. struct {
  74. env::type behavior;
  75. /*! Implicitly converts from any STL container of string pairs to the
  76. environment format expected by `reproc_start`. */
  77. class env extra;
  78. } env = {};
  79. const char *working_directory = nullptr;
  80. struct {
  81. redirect in;
  82. redirect out;
  83. redirect err;
  84. bool parent;
  85. bool discard;
  86. FILE *file;
  87. const char *path;
  88. } redirect = {};
  89. struct stop_actions stop = {};
  90. reproc::milliseconds timeout = reproc::milliseconds(0);
  91. reproc::milliseconds deadline = reproc::milliseconds(0);
  92. /*! Implicitly converts from string literals to the pointer size pair expected
  93. by `reproc_start`. */
  94. class input input;
  95. bool nonblocking = false;
  96. /*! Make a shallow copy of `options`. */
  97. static options clone(const options &other)
  98. {
  99. struct options clone;
  100. clone.env.behavior = other.env.behavior;
  101. // Make sure we make a shallow copy of `environment`.
  102. clone.env.extra = other.env.extra.data();
  103. clone.working_directory = other.working_directory;
  104. clone.redirect = other.redirect;
  105. clone.stop = other.stop;
  106. clone.timeout = other.timeout;
  107. clone.deadline = other.deadline;
  108. clone.input = other.input;
  109. return clone;
  110. }
  111. };
  112. enum class stream {
  113. in,
  114. out,
  115. err,
  116. };
  117. class process;
  118. namespace event {
  119. enum {
  120. in = 1 << 0,
  121. out = 1 << 1,
  122. err = 1 << 2,
  123. exit = 1 << 3,
  124. deadline = 1 << 4,
  125. };
  126. struct source {
  127. class process &process;
  128. int interests;
  129. int events;
  130. };
  131. }
  132. REPROCXX_EXPORT std::error_code poll(event::source *sources,
  133. size_t num_sources,
  134. milliseconds timeout = infinite);
  135. /*! Improves on reproc's API by adding RAII and changing the API of some
  136. functions to be more idiomatic C++. */
  137. class process {
  138. public:
  139. REPROCXX_EXPORT process();
  140. REPROCXX_EXPORT ~process() noexcept;
  141. // Enforce unique ownership of child processes.
  142. REPROCXX_EXPORT process(process &&other) noexcept;
  143. REPROCXX_EXPORT process &operator=(process &&other) noexcept;
  144. /*! `reproc_start` but implicitly converts from STL containers to the
  145. arguments format expected by `reproc_start`. */
  146. REPROCXX_EXPORT std::error_code start(const arguments &arguments,
  147. const options &options = {}) noexcept;
  148. REPROCXX_EXPORT std::pair<int, std::error_code> pid() noexcept;
  149. /*! Sets the `fork` option in `reproc_options` and calls `start`. Returns
  150. `true` in the child process and `false` in the parent process. */
  151. REPROCXX_EXPORT std::pair<bool, std::error_code>
  152. fork(const options &options = {}) noexcept;
  153. /*! Shorthand for `reproc::poll` that only polls this process. Returns a pair
  154. of (events, error). */
  155. REPROCXX_EXPORT std::pair<int, std::error_code>
  156. poll(int interests, milliseconds timeout = infinite);
  157. /*! `reproc_read` but returns a pair of (bytes read, error). */
  158. REPROCXX_EXPORT std::pair<size_t, std::error_code>
  159. read(stream stream, uint8_t *buffer, size_t size) noexcept;
  160. /*! reproc_write` but returns a pair of (bytes_written, error). */
  161. REPROCXX_EXPORT std::pair<size_t, std::error_code>
  162. write(const uint8_t *buffer, size_t size) noexcept;
  163. REPROCXX_EXPORT std::error_code close(stream stream) noexcept;
  164. /*! `reproc_wait` but returns a pair of (status, error). */
  165. REPROCXX_EXPORT std::pair<int, std::error_code>
  166. wait(milliseconds timeout) noexcept;
  167. REPROCXX_EXPORT std::error_code terminate() noexcept;
  168. REPROCXX_EXPORT std::error_code kill() noexcept;
  169. /*! `reproc_stop` but returns a pair of (status, error). */
  170. REPROCXX_EXPORT std::pair<int, std::error_code>
  171. stop(stop_actions stop) noexcept;
  172. private:
  173. REPROCXX_EXPORT friend std::error_code
  174. poll(event::source *sources, size_t num_sources, milliseconds timeout);
  175. std::unique_ptr<reproc_t, reproc_t *(*) (reproc_t *)> impl_;
  176. };
  177. }