process-stdio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include <assert.h>
  22. #include <io.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include "uv.h"
  26. #include "internal.h"
  27. #include "handle-inl.h"
  28. /*
  29. * The `child_stdio_buffer` buffer has the following layout:
  30. * int number_of_fds
  31. * unsigned char crt_flags[number_of_fds]
  32. * HANDLE os_handle[number_of_fds]
  33. */
  34. #define CHILD_STDIO_SIZE(count) \
  35. (sizeof(int) + \
  36. sizeof(unsigned char) * (count) + \
  37. sizeof(uintptr_t) * (count))
  38. #define CHILD_STDIO_COUNT(buffer) \
  39. *((unsigned int*) (buffer))
  40. #define CHILD_STDIO_CRT_FLAGS(buffer, fd) \
  41. *((unsigned char*) (buffer) + sizeof(int) + fd)
  42. #define CHILD_STDIO_HANDLE(buffer, fd) \
  43. *((HANDLE*) ((unsigned char*) (buffer) + \
  44. sizeof(int) + \
  45. sizeof(unsigned char) * \
  46. CHILD_STDIO_COUNT((buffer)) + \
  47. sizeof(HANDLE) * (fd)))
  48. /* CRT file descriptor mode flags */
  49. #define FOPEN 0x01
  50. #define FEOFLAG 0x02
  51. #define FCRLF 0x04
  52. #define FPIPE 0x08
  53. #define FNOINHERIT 0x10
  54. #define FAPPEND 0x20
  55. #define FDEV 0x40
  56. #define FTEXT 0x80
  57. /*
  58. * Clear the HANDLE_FLAG_INHERIT flag from all HANDLEs that were inherited
  59. * the parent process. Don't check for errors - the stdio handles may not be
  60. * valid, or may be closed already. There is no guarantee that this function
  61. * does a perfect job.
  62. */
  63. void uv_disable_stdio_inheritance(void) {
  64. HANDLE handle;
  65. STARTUPINFOW si;
  66. /* Make the windows stdio handles non-inheritable. */
  67. handle = GetStdHandle(STD_INPUT_HANDLE);
  68. if (handle != NULL && handle != INVALID_HANDLE_VALUE)
  69. SetHandleInformation(handle, HANDLE_FLAG_INHERIT, 0);
  70. handle = GetStdHandle(STD_OUTPUT_HANDLE);
  71. if (handle != NULL && handle != INVALID_HANDLE_VALUE)
  72. SetHandleInformation(handle, HANDLE_FLAG_INHERIT, 0);
  73. handle = GetStdHandle(STD_ERROR_HANDLE);
  74. if (handle != NULL && handle != INVALID_HANDLE_VALUE)
  75. SetHandleInformation(handle, HANDLE_FLAG_INHERIT, 0);
  76. /* Make inherited CRT FDs non-inheritable. */
  77. GetStartupInfoW(&si);
  78. if (uv__stdio_verify(si.lpReserved2, si.cbReserved2))
  79. uv__stdio_noinherit(si.lpReserved2);
  80. }
  81. static int uv__create_stdio_pipe_pair(uv_loop_t* loop,
  82. uv_pipe_t* server_pipe, HANDLE* child_pipe_ptr, unsigned int flags) {
  83. char pipe_name[64];
  84. SECURITY_ATTRIBUTES sa;
  85. DWORD server_access = 0;
  86. DWORD client_access = 0;
  87. HANDLE child_pipe = INVALID_HANDLE_VALUE;
  88. int err;
  89. if (flags & UV_READABLE_PIPE) {
  90. /* The server needs inbound access too, otherwise CreateNamedPipe() */
  91. /* won't give us the FILE_READ_ATTRIBUTES permission. We need that to */
  92. /* probe the state of the write buffer when we're trying to shutdown */
  93. /* the pipe. */
  94. server_access |= PIPE_ACCESS_OUTBOUND | PIPE_ACCESS_INBOUND;
  95. client_access |= GENERIC_READ | FILE_WRITE_ATTRIBUTES;
  96. }
  97. if (flags & UV_WRITABLE_PIPE) {
  98. server_access |= PIPE_ACCESS_INBOUND;
  99. client_access |= GENERIC_WRITE | FILE_READ_ATTRIBUTES;
  100. }
  101. /* Create server pipe handle. */
  102. err = uv_stdio_pipe_server(loop,
  103. server_pipe,
  104. server_access,
  105. pipe_name,
  106. sizeof(pipe_name));
  107. if (err)
  108. goto error;
  109. /* Create child pipe handle. */
  110. sa.nLength = sizeof sa;
  111. sa.lpSecurityDescriptor = NULL;
  112. sa.bInheritHandle = TRUE;
  113. child_pipe = CreateFileA(pipe_name,
  114. client_access,
  115. 0,
  116. &sa,
  117. OPEN_EXISTING,
  118. server_pipe->ipc ? FILE_FLAG_OVERLAPPED : 0,
  119. NULL);
  120. if (child_pipe == INVALID_HANDLE_VALUE) {
  121. err = GetLastError();
  122. goto error;
  123. }
  124. #ifndef NDEBUG
  125. /* Validate that the pipe was opened in the right mode. */
  126. {
  127. DWORD mode;
  128. BOOL r = GetNamedPipeHandleState(child_pipe,
  129. &mode,
  130. NULL,
  131. NULL,
  132. NULL,
  133. NULL,
  134. 0);
  135. assert(r == TRUE);
  136. assert(mode == (PIPE_READMODE_BYTE | PIPE_WAIT));
  137. }
  138. #endif
  139. /* Do a blocking ConnectNamedPipe. This should not block because we have */
  140. /* both ends of the pipe created. */
  141. if (!ConnectNamedPipe(server_pipe->handle, NULL)) {
  142. if (GetLastError() != ERROR_PIPE_CONNECTED) {
  143. err = GetLastError();
  144. goto error;
  145. }
  146. }
  147. /* The server end is now readable and/or writable. */
  148. if (flags & UV_READABLE_PIPE)
  149. server_pipe->flags |= UV_HANDLE_WRITABLE;
  150. if (flags & UV_WRITABLE_PIPE)
  151. server_pipe->flags |= UV_HANDLE_READABLE;
  152. *child_pipe_ptr = child_pipe;
  153. return 0;
  154. error:
  155. if (server_pipe->handle != INVALID_HANDLE_VALUE) {
  156. uv_pipe_cleanup(loop, server_pipe);
  157. }
  158. if (child_pipe != INVALID_HANDLE_VALUE) {
  159. CloseHandle(child_pipe);
  160. }
  161. return err;
  162. }
  163. static int uv__duplicate_handle(uv_loop_t* loop, HANDLE handle, HANDLE* dup) {
  164. HANDLE current_process;
  165. /* _get_osfhandle will sometimes return -2 in case of an error. This seems */
  166. /* to happen when fd <= 2 and the process' corresponding stdio handle is */
  167. /* set to NULL. Unfortunately DuplicateHandle will happily duplicate */
  168. /* (HANDLE) -2, so this situation goes unnoticed until someone tries to */
  169. /* use the duplicate. Therefore we filter out known-invalid handles here. */
  170. if (handle == INVALID_HANDLE_VALUE ||
  171. handle == NULL ||
  172. handle == (HANDLE) -2) {
  173. *dup = INVALID_HANDLE_VALUE;
  174. return ERROR_INVALID_HANDLE;
  175. }
  176. current_process = GetCurrentProcess();
  177. if (!DuplicateHandle(current_process,
  178. handle,
  179. current_process,
  180. dup,
  181. 0,
  182. TRUE,
  183. DUPLICATE_SAME_ACCESS)) {
  184. *dup = INVALID_HANDLE_VALUE;
  185. return GetLastError();
  186. }
  187. return 0;
  188. }
  189. static int uv__duplicate_fd(uv_loop_t* loop, int fd, HANDLE* dup) {
  190. HANDLE handle;
  191. if (fd == -1) {
  192. *dup = INVALID_HANDLE_VALUE;
  193. return ERROR_INVALID_HANDLE;
  194. }
  195. handle = uv__get_osfhandle(fd);
  196. return uv__duplicate_handle(loop, handle, dup);
  197. }
  198. int uv__create_nul_handle(HANDLE* handle_ptr,
  199. DWORD access) {
  200. HANDLE handle;
  201. SECURITY_ATTRIBUTES sa;
  202. sa.nLength = sizeof sa;
  203. sa.lpSecurityDescriptor = NULL;
  204. sa.bInheritHandle = TRUE;
  205. handle = CreateFileW(L"NUL",
  206. access,
  207. FILE_SHARE_READ | FILE_SHARE_WRITE,
  208. &sa,
  209. OPEN_EXISTING,
  210. 0,
  211. NULL);
  212. if (handle == INVALID_HANDLE_VALUE) {
  213. return GetLastError();
  214. }
  215. *handle_ptr = handle;
  216. return 0;
  217. }
  218. int uv__stdio_create(uv_loop_t* loop,
  219. const uv_process_options_t* options,
  220. BYTE** buffer_ptr) {
  221. BYTE* buffer;
  222. int count, i;
  223. int err;
  224. count = options->stdio_count;
  225. if (count < 0 || count > 255) {
  226. /* Only support FDs 0-255 */
  227. return ERROR_NOT_SUPPORTED;
  228. } else if (count < 3) {
  229. /* There should always be at least 3 stdio handles. */
  230. count = 3;
  231. }
  232. /* Allocate the child stdio buffer */
  233. buffer = (BYTE*) uv__malloc(CHILD_STDIO_SIZE(count));
  234. if (buffer == NULL) {
  235. return ERROR_OUTOFMEMORY;
  236. }
  237. /* Prepopulate the buffer with INVALID_HANDLE_VALUE handles so we can */
  238. /* clean up on failure. */
  239. CHILD_STDIO_COUNT(buffer) = count;
  240. for (i = 0; i < count; i++) {
  241. CHILD_STDIO_CRT_FLAGS(buffer, i) = 0;
  242. CHILD_STDIO_HANDLE(buffer, i) = INVALID_HANDLE_VALUE;
  243. }
  244. for (i = 0; i < count; i++) {
  245. uv_stdio_container_t fdopt;
  246. if (i < options->stdio_count) {
  247. fdopt = options->stdio[i];
  248. } else {
  249. fdopt.flags = UV_IGNORE;
  250. }
  251. switch (fdopt.flags & (UV_IGNORE | UV_CREATE_PIPE | UV_INHERIT_FD |
  252. UV_INHERIT_STREAM)) {
  253. case UV_IGNORE:
  254. /* Starting a process with no stdin/stout/stderr can confuse it. */
  255. /* So no matter what the user specified, we make sure the first */
  256. /* three FDs are always open in their typical modes, e.g. stdin */
  257. /* be readable and stdout/err should be writable. For FDs > 2, don't */
  258. /* do anything - all handles in the stdio buffer are initialized with */
  259. /* INVALID_HANDLE_VALUE, which should be okay. */
  260. if (i <= 2) {
  261. DWORD access = (i == 0) ? FILE_GENERIC_READ :
  262. FILE_GENERIC_WRITE | FILE_READ_ATTRIBUTES;
  263. err = uv__create_nul_handle(&CHILD_STDIO_HANDLE(buffer, i),
  264. access);
  265. if (err)
  266. goto error;
  267. CHILD_STDIO_CRT_FLAGS(buffer, i) = FOPEN | FDEV;
  268. }
  269. break;
  270. case UV_CREATE_PIPE: {
  271. /* Create a pair of two connected pipe ends; one end is turned into */
  272. /* an uv_pipe_t for use by the parent. The other one is given to */
  273. /* the child. */
  274. uv_pipe_t* parent_pipe = (uv_pipe_t*) fdopt.data.stream;
  275. HANDLE child_pipe = INVALID_HANDLE_VALUE;
  276. /* Create a new, connected pipe pair. stdio[i].stream should point */
  277. /* to an uninitialized, but not connected pipe handle. */
  278. assert(fdopt.data.stream->type == UV_NAMED_PIPE);
  279. assert(!(fdopt.data.stream->flags & UV_HANDLE_CONNECTION));
  280. assert(!(fdopt.data.stream->flags & UV_HANDLE_PIPESERVER));
  281. err = uv__create_stdio_pipe_pair(loop,
  282. parent_pipe,
  283. &child_pipe,
  284. fdopt.flags);
  285. if (err)
  286. goto error;
  287. CHILD_STDIO_HANDLE(buffer, i) = child_pipe;
  288. CHILD_STDIO_CRT_FLAGS(buffer, i) = FOPEN | FPIPE;
  289. break;
  290. }
  291. case UV_INHERIT_FD: {
  292. /* Inherit a raw FD. */
  293. HANDLE child_handle;
  294. /* Make an inheritable duplicate of the handle. */
  295. err = uv__duplicate_fd(loop, fdopt.data.fd, &child_handle);
  296. if (err) {
  297. /* If fdopt.data.fd is not valid and fd fd <= 2, then ignore the */
  298. /* error. */
  299. if (fdopt.data.fd <= 2 && err == ERROR_INVALID_HANDLE) {
  300. CHILD_STDIO_CRT_FLAGS(buffer, i) = 0;
  301. CHILD_STDIO_HANDLE(buffer, i) = INVALID_HANDLE_VALUE;
  302. break;
  303. }
  304. goto error;
  305. }
  306. /* Figure out what the type is. */
  307. switch (GetFileType(child_handle)) {
  308. case FILE_TYPE_DISK:
  309. CHILD_STDIO_CRT_FLAGS(buffer, i) = FOPEN;
  310. break;
  311. case FILE_TYPE_PIPE:
  312. CHILD_STDIO_CRT_FLAGS(buffer, i) = FOPEN | FPIPE;
  313. break;
  314. case FILE_TYPE_CHAR:
  315. case FILE_TYPE_REMOTE:
  316. CHILD_STDIO_CRT_FLAGS(buffer, i) = FOPEN | FDEV;
  317. break;
  318. case FILE_TYPE_UNKNOWN:
  319. if (GetLastError() != 0) {
  320. err = GetLastError();
  321. CloseHandle(child_handle);
  322. goto error;
  323. }
  324. CHILD_STDIO_CRT_FLAGS(buffer, i) = FOPEN | FDEV;
  325. break;
  326. default:
  327. assert(0);
  328. return -1;
  329. }
  330. CHILD_STDIO_HANDLE(buffer, i) = child_handle;
  331. break;
  332. }
  333. case UV_INHERIT_STREAM: {
  334. /* Use an existing stream as the stdio handle for the child. */
  335. HANDLE stream_handle, child_handle;
  336. unsigned char crt_flags;
  337. uv_stream_t* stream = fdopt.data.stream;
  338. /* Leech the handle out of the stream. */
  339. if (stream->type == UV_TTY) {
  340. stream_handle = ((uv_tty_t*) stream)->handle;
  341. crt_flags = FOPEN | FDEV;
  342. } else if (stream->type == UV_NAMED_PIPE &&
  343. stream->flags & UV_HANDLE_CONNECTION) {
  344. stream_handle = ((uv_pipe_t*) stream)->handle;
  345. crt_flags = FOPEN | FPIPE;
  346. } else {
  347. stream_handle = INVALID_HANDLE_VALUE;
  348. crt_flags = 0;
  349. }
  350. if (stream_handle == NULL ||
  351. stream_handle == INVALID_HANDLE_VALUE) {
  352. /* The handle is already closed, or not yet created, or the */
  353. /* stream type is not supported. */
  354. err = ERROR_NOT_SUPPORTED;
  355. goto error;
  356. }
  357. /* Make an inheritable copy of the handle. */
  358. err = uv__duplicate_handle(loop, stream_handle, &child_handle);
  359. if (err)
  360. goto error;
  361. CHILD_STDIO_HANDLE(buffer, i) = child_handle;
  362. CHILD_STDIO_CRT_FLAGS(buffer, i) = crt_flags;
  363. break;
  364. }
  365. default:
  366. assert(0);
  367. return -1;
  368. }
  369. }
  370. *buffer_ptr = buffer;
  371. return 0;
  372. error:
  373. uv__stdio_destroy(buffer);
  374. return err;
  375. }
  376. void uv__stdio_destroy(BYTE* buffer) {
  377. int i, count;
  378. count = CHILD_STDIO_COUNT(buffer);
  379. for (i = 0; i < count; i++) {
  380. HANDLE handle = CHILD_STDIO_HANDLE(buffer, i);
  381. if (handle != INVALID_HANDLE_VALUE) {
  382. CloseHandle(handle);
  383. }
  384. }
  385. uv__free(buffer);
  386. }
  387. void uv__stdio_noinherit(BYTE* buffer) {
  388. int i, count;
  389. count = CHILD_STDIO_COUNT(buffer);
  390. for (i = 0; i < count; i++) {
  391. HANDLE handle = CHILD_STDIO_HANDLE(buffer, i);
  392. if (handle != INVALID_HANDLE_VALUE) {
  393. SetHandleInformation(handle, HANDLE_FLAG_INHERIT, 0);
  394. }
  395. }
  396. }
  397. int uv__stdio_verify(BYTE* buffer, WORD size) {
  398. unsigned int count;
  399. /* Check the buffer pointer. */
  400. if (buffer == NULL)
  401. return 0;
  402. /* Verify that the buffer is at least big enough to hold the count. */
  403. if (size < CHILD_STDIO_SIZE(0))
  404. return 0;
  405. /* Verify if the count is within range. */
  406. count = CHILD_STDIO_COUNT(buffer);
  407. if (count > 256)
  408. return 0;
  409. /* Verify that the buffer size is big enough to hold info for N FDs. */
  410. if (size < CHILD_STDIO_SIZE(count))
  411. return 0;
  412. return 1;
  413. }
  414. WORD uv__stdio_size(BYTE* buffer) {
  415. return (WORD) CHILD_STDIO_SIZE(CHILD_STDIO_COUNT((buffer)));
  416. }
  417. HANDLE uv__stdio_handle(BYTE* buffer, int fd) {
  418. return CHILD_STDIO_HANDLE(buffer, fd);
  419. }