0001-to-head.diff 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. diff --git a/.clang-tidy b/.clang-tidy
  2. index 35bb58f..de92997 100644
  3. --- a/.clang-tidy
  4. +++ b/.clang-tidy
  5. @@ -34,6 +34,7 @@ Checks:
  6. -performance-no-int-to-ptr,
  7. -readability-else-after-return,
  8. -readability-function-cognitive-complexity,
  9. +-readability-identifier-length,
  10. -readability-magic-numbers,
  11. '
  12. HeaderFilterRegex: '.*reproc\+\+.*$'
  13. diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
  14. index 4000454..3858f59 100644
  15. --- a/.github/workflows/main.yml
  16. +++ b/.github/workflows/main.yml
  17. @@ -74,7 +74,7 @@ jobs:
  18. - name: Install (Windows)
  19. if: runner.os == 'Windows'
  20. run: |
  21. - Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh')
  22. + iex "& {$(irm get.scoop.sh)} -RunAsAdmin"
  23. scoop install ninja llvm --global
  24. if ("${{ matrix.compiler }}" -eq "gcc") {
  25. diff --git a/README.md b/README.md
  26. index 62cc299..e7e940d 100644
  27. --- a/README.md
  28. +++ b/README.md
  29. @@ -225,6 +225,16 @@ occurred. You can test against these error codes using values from the
  30. See the examples for more information on how to handle errors when using reproc.
  31. +Note:
  32. +
  33. +Both reproc and reproc++ APIs take `options` argument that may define one or more
  34. +`stop` actions such as `terminate` or `kill`.
  35. +For that reason if the child process is being terminated or killed using a signal
  36. +on POSIX, the error code will **not** reflect an error.
  37. +
  38. +It's up to the downstream project to *interpret* status codes reflecting unexpected
  39. +behaviors alongside error codes (see this [example](https://github.com/DaanDeMeyer/reproc/issues/68#issuecomment-959074504)).
  40. +
  41. ## Multithreading
  42. Don't call the same operation on the same child process from more than one
  43. diff --git a/reproc++/include/reproc++/reproc.hpp b/reproc++/include/reproc++/reproc.hpp
  44. index ab6f139..f722245 100644
  45. --- a/reproc++/include/reproc++/reproc.hpp
  46. +++ b/reproc++/include/reproc++/reproc.hpp
  47. @@ -88,18 +88,18 @@ struct redirect {
  48. struct options {
  49. struct {
  50. - env::type behavior;
  51. + reproc::env::type behavior;
  52. /*! Implicitly converts from any STL container of string pairs to the
  53. environment format expected by `reproc_start`. */
  54. - class env extra;
  55. + reproc::env extra;
  56. } env = {};
  57. const char *working_directory = nullptr;
  58. struct {
  59. - redirect in;
  60. - redirect out;
  61. - redirect err;
  62. + struct redirect in;
  63. + struct redirect out;
  64. + struct redirect err;
  65. bool parent;
  66. bool discard;
  67. FILE *file;
  68. @@ -138,30 +138,12 @@ enum class stream {
  69. err,
  70. };
  71. -class process;
  72. -
  73. namespace event {
  74. -enum {
  75. - in = 1 << 0,
  76. - out = 1 << 1,
  77. - err = 1 << 2,
  78. - exit = 1 << 3,
  79. - deadline = 1 << 4,
  80. -};
  81. -
  82. -struct source {
  83. - class process &process;
  84. - int interests;
  85. - int events;
  86. -};
  87. +struct source;
  88. }
  89. -REPROCXX_EXPORT std::error_code poll(event::source *sources,
  90. - size_t num_sources,
  91. - milliseconds timeout = infinite);
  92. -
  93. /*! Improves on reproc's API by adding RAII and changing the API of some
  94. functions to be more idiomatic C++. */
  95. class process {
  96. @@ -220,4 +202,26 @@ private:
  97. std::unique_ptr<reproc_t, reproc_t *(*) (reproc_t *)> impl_;
  98. };
  99. +namespace event {
  100. +
  101. +enum {
  102. + in = 1 << 0,
  103. + out = 1 << 1,
  104. + err = 1 << 2,
  105. + exit = 1 << 3,
  106. + deadline = 1 << 4,
  107. +};
  108. +
  109. +struct source {
  110. + class process process;
  111. + int interests;
  112. + int events;
  113. +};
  114. +
  115. +}
  116. +
  117. +REPROCXX_EXPORT std::error_code poll(event::source *sources,
  118. + size_t num_sources,
  119. + milliseconds timeout = infinite);
  120. +
  121. }
  122. diff --git a/reproc++/src/reproc.cpp b/reproc++/src/reproc.cpp
  123. index e4eed1a..534e9fb 100644
  124. --- a/reproc++/src/reproc.cpp
  125. +++ b/reproc++/src/reproc.cpp
  126. @@ -86,8 +86,9 @@ std::pair<bool, std::error_code> process::fork(const options &options) noexcept
  127. std::pair<int, std::error_code> process::poll(int interests,
  128. milliseconds timeout)
  129. {
  130. - event::source source{ *this, interests, 0 };
  131. + event::source source{ std::move(*this), interests, 0 };
  132. std::error_code ec = ::reproc::poll(&source, 1, timeout);
  133. + *this = std::move(source.process);
  134. return { source.events, ec };
  135. }
  136. diff --git a/reproc/CMakeLists.txt b/reproc/CMakeLists.txt
  137. index 949cc88..1bb4798 100644
  138. --- a/reproc/CMakeLists.txt
  139. +++ b/reproc/CMakeLists.txt
  140. @@ -1,6 +1,6 @@
  141. if(WIN32)
  142. set(REPROC_WINSOCK_LIBRARY ws2_32)
  143. -elseif(NOT APPLE)
  144. +elseif(CMAKE_SYSTEM_NAME MATCHES Linux)
  145. set(REPROC_RT_LIBRARY rt) # clock_gettime
  146. endif()
  147. diff --git a/reproc/src/clock.windows.c b/reproc/src/clock.windows.c
  148. index 3130f85..8c6c85a 100644
  149. --- a/reproc/src/clock.windows.c
  150. +++ b/reproc/src/clock.windows.c
  151. @@ -1,4 +1,8 @@
  152. -#define _WIN32_WINNT _WIN32_WINNT_VISTA
  153. +#ifndef _WIN32_WINNT
  154. + #define _WIN32_WINNT 0x0600 // _WIN32_WINNT_VISTA
  155. +#elif _WIN32_WINNT < 0x0600
  156. + #error "_WIN32_WINNT must be greater than _WIN32_WINNT_VISTA (0x0600)"
  157. +#endif
  158. #include "clock.h"
  159. diff --git a/reproc/src/error.windows.c b/reproc/src/error.windows.c
  160. index b8d8234..9459027 100644
  161. --- a/reproc/src/error.windows.c
  162. +++ b/reproc/src/error.windows.c
  163. @@ -1,4 +1,8 @@
  164. -#define _WIN32_WINNT _WIN32_WINNT_VISTA
  165. +#ifndef _WIN32_WINNT
  166. + #define _WIN32_WINNT 0x0600 // _WIN32_WINNT_VISTA
  167. +#elif _WIN32_WINNT < 0x0600
  168. + #error "_WIN32_WINNT must be greater than _WIN32_WINNT_VISTA (0x0600)"
  169. +#endif
  170. #include "error.h"
  171. diff --git a/reproc/src/handle.windows.c b/reproc/src/handle.windows.c
  172. index e0cd500..f0fbe56 100644
  173. --- a/reproc/src/handle.windows.c
  174. +++ b/reproc/src/handle.windows.c
  175. @@ -1,4 +1,8 @@
  176. -#define _WIN32_WINNT _WIN32_WINNT_VISTA
  177. +#ifndef _WIN32_WINNT
  178. + #define _WIN32_WINNT 0x0600 // _WIN32_WINNT_VISTA
  179. +#elif _WIN32_WINNT < 0x0600
  180. + #error "_WIN32_WINNT must be greater than _WIN32_WINNT_VISTA (0x0600)"
  181. +#endif
  182. #include "handle.h"
  183. diff --git a/reproc/src/init.windows.c b/reproc/src/init.windows.c
  184. index 8357b7c..52519bf 100644
  185. --- a/reproc/src/init.windows.c
  186. +++ b/reproc/src/init.windows.c
  187. @@ -1,4 +1,8 @@
  188. -#define _WIN32_WINNT _WIN32_WINNT_VISTA
  189. +#ifndef _WIN32_WINNT
  190. + #define _WIN32_WINNT 0x0600 // _WIN32_WINNT_VISTA
  191. +#elif _WIN32_WINNT < 0x0600
  192. + #error "_WIN32_WINNT must be greater than _WIN32_WINNT_VISTA (0x0600)"
  193. +#endif
  194. #include "init.h"
  195. diff --git a/reproc/src/pipe.windows.c b/reproc/src/pipe.windows.c
  196. index bb355be..befeaf1 100644
  197. --- a/reproc/src/pipe.windows.c
  198. +++ b/reproc/src/pipe.windows.c
  199. @@ -1,4 +1,8 @@
  200. -#define _WIN32_WINNT _WIN32_WINNT_VISTA
  201. +#ifndef _WIN32_WINNT
  202. + #define _WIN32_WINNT 0x0600 // _WIN32_WINNT_VISTA
  203. +#elif _WIN32_WINNT < 0x0600
  204. + #error "_WIN32_WINNT must be greater than _WIN32_WINNT_VISTA (0x0600)"
  205. +#endif
  206. #include "pipe.h"
  207. diff --git a/reproc/src/process.posix.c b/reproc/src/process.posix.c
  208. index 0f0fe0d..8dcbfd1 100644
  209. --- a/reproc/src/process.posix.c
  210. +++ b/reproc/src/process.posix.c
  211. @@ -17,6 +17,8 @@
  212. #include "pipe.h"
  213. #include "strv.h"
  214. +#define CWD_BUF_SIZE_INCREMENT 4096
  215. +
  216. const pid_t PROCESS_INVALID = -1;
  217. static int signal_mask(int how, const sigset_t *newmask, sigset_t *oldmask)
  218. @@ -51,7 +53,7 @@ static char *path_prepend_cwd(const char *path)
  219. ASSERT(path);
  220. size_t path_size = strlen(path);
  221. - size_t cwd_size = PATH_MAX;
  222. + size_t cwd_size = CWD_BUF_SIZE_INCREMENT;
  223. // We always allocate sufficient space for `path` but do not include this
  224. // space in `cwd_size` so we can be sure that when `getcwd` succeeds there is
  225. @@ -70,7 +72,7 @@ static char *path_prepend_cwd(const char *path)
  226. return NULL;
  227. }
  228. - cwd_size += PATH_MAX;
  229. + cwd_size += CWD_BUF_SIZE_INCREMENT;
  230. char *result = realloc(cwd, cwd_size + path_size + 1);
  231. if (result == NULL) {
  232. diff --git a/reproc/src/process.windows.c b/reproc/src/process.windows.c
  233. index 666f3cb..6e28589 100644
  234. --- a/reproc/src/process.windows.c
  235. +++ b/reproc/src/process.windows.c
  236. @@ -1,4 +1,8 @@
  237. -#define _WIN32_WINNT _WIN32_WINNT_VISTA
  238. +#ifndef _WIN32_WINNT
  239. + #define _WIN32_WINNT 0x0600 // _WIN32_WINNT_VISTA
  240. +#elif _WIN32_WINNT < 0x0600
  241. + #error "_WIN32_WINNT must be greater than _WIN32_WINNT_VISTA (0x0600)"
  242. +#endif
  243. #include "process.h"
  244. diff --git a/reproc/src/redirect.windows.c b/reproc/src/redirect.windows.c
  245. index c634145..151f407 100644
  246. --- a/reproc/src/redirect.windows.c
  247. +++ b/reproc/src/redirect.windows.c
  248. @@ -1,4 +1,8 @@
  249. -#define _WIN32_WINNT _WIN32_WINNT_VISTA
  250. +#ifndef _WIN32_WINNT
  251. + #define _WIN32_WINNT 0x0600 // _WIN32_WINNT_VISTA
  252. +#elif _WIN32_WINNT < 0x0600
  253. + #error "_WIN32_WINNT must be greater than _WIN32_WINNT_VISTA (0x0600)"
  254. +#endif
  255. #include "redirect.h"