1
0

test-spawn.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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 "uv.h"
  22. #include "task.h"
  23. #include <fcntl.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #ifndef _WIN32
  28. #include <unistd.h>
  29. #endif
  30. static int close_cb_called;
  31. static int exit_cb_called;
  32. static uv_process_t process;
  33. static uv_timer_t timer;
  34. static uv_process_options_t options;
  35. static char exepath[1024];
  36. static size_t exepath_size = 1024;
  37. static char* args[3];
  38. static int no_term_signal;
  39. #define OUTPUT_SIZE 1024
  40. static char output[OUTPUT_SIZE];
  41. static int output_used;
  42. static void close_cb(uv_handle_t* handle) {
  43. printf("close_cb\n");
  44. close_cb_called++;
  45. }
  46. static void exit_cb(uv_process_t* process,
  47. int64_t exit_status,
  48. int term_signal) {
  49. printf("exit_cb\n");
  50. exit_cb_called++;
  51. ASSERT(exit_status == 1);
  52. ASSERT(term_signal == 0);
  53. uv_close((uv_handle_t*)process, close_cb);
  54. }
  55. static void fail_cb(uv_process_t* process,
  56. int64_t exit_status,
  57. int term_signal) {
  58. ASSERT(0 && "fail_cb called");
  59. }
  60. static void kill_cb(uv_process_t* process,
  61. int64_t exit_status,
  62. int term_signal) {
  63. int err;
  64. printf("exit_cb\n");
  65. exit_cb_called++;
  66. #ifdef _WIN32
  67. ASSERT(exit_status == 1);
  68. #else
  69. ASSERT(exit_status == 0);
  70. #endif
  71. ASSERT(no_term_signal || term_signal == 15);
  72. uv_close((uv_handle_t*)process, close_cb);
  73. /*
  74. * Sending signum == 0 should check if the
  75. * child process is still alive, not kill it.
  76. * This process should be dead.
  77. */
  78. err = uv_kill(process->pid, 0);
  79. ASSERT(err == UV_ESRCH);
  80. }
  81. static void detach_failure_cb(uv_process_t* process,
  82. int64_t exit_status,
  83. int term_signal) {
  84. printf("detach_cb\n");
  85. exit_cb_called++;
  86. }
  87. static void on_alloc(uv_handle_t* handle,
  88. size_t suggested_size,
  89. uv_buf_t* buf) {
  90. buf->base = output + output_used;
  91. buf->len = OUTPUT_SIZE - output_used;
  92. }
  93. static void on_read(uv_stream_t* tcp, ssize_t nread, const uv_buf_t* buf) {
  94. if (nread > 0) {
  95. output_used += nread;
  96. } else if (nread < 0) {
  97. ASSERT(nread == UV_EOF);
  98. uv_close((uv_handle_t*)tcp, close_cb);
  99. }
  100. }
  101. static void write_cb(uv_write_t* req, int status) {
  102. ASSERT(status == 0);
  103. uv_close((uv_handle_t*)req->handle, close_cb);
  104. }
  105. static void init_process_options(char* test, uv_exit_cb exit_cb) {
  106. /* Note spawn_helper1 defined in test/run-tests.c */
  107. int r = uv_exepath(exepath, &exepath_size);
  108. ASSERT(r == 0);
  109. exepath[exepath_size] = '\0';
  110. args[0] = exepath;
  111. args[1] = test;
  112. args[2] = NULL;
  113. options.file = exepath;
  114. options.args = args;
  115. options.exit_cb = exit_cb;
  116. options.flags = 0;
  117. }
  118. static void timer_cb(uv_timer_t* handle, int status) {
  119. uv_process_kill(&process, /* SIGTERM */ 15);
  120. uv_close((uv_handle_t*)handle, close_cb);
  121. }
  122. TEST_IMPL(spawn_fails) {
  123. int r;
  124. init_process_options("", fail_cb);
  125. options.file = options.args[0] = "program-that-had-better-not-exist";
  126. r = uv_spawn(uv_default_loop(), &process, &options);
  127. ASSERT(r == UV_ENOENT || r == UV_EACCES);
  128. ASSERT(0 == uv_is_active((uv_handle_t*) &process));
  129. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
  130. MAKE_VALGRIND_HAPPY();
  131. return 0;
  132. }
  133. TEST_IMPL(spawn_exit_code) {
  134. int r;
  135. init_process_options("spawn_helper1", exit_cb);
  136. r = uv_spawn(uv_default_loop(), &process, &options);
  137. ASSERT(r == 0);
  138. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  139. ASSERT(r == 0);
  140. ASSERT(exit_cb_called == 1);
  141. ASSERT(close_cb_called == 1);
  142. MAKE_VALGRIND_HAPPY();
  143. return 0;
  144. }
  145. TEST_IMPL(spawn_stdout) {
  146. int r;
  147. uv_pipe_t out;
  148. uv_stdio_container_t stdio[2];
  149. init_process_options("spawn_helper2", exit_cb);
  150. uv_pipe_init(uv_default_loop(), &out, 0);
  151. options.stdio = stdio;
  152. options.stdio[0].flags = UV_IGNORE;
  153. options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
  154. options.stdio[1].data.stream = (uv_stream_t*)&out;
  155. options.stdio_count = 2;
  156. r = uv_spawn(uv_default_loop(), &process, &options);
  157. ASSERT(r == 0);
  158. r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read);
  159. ASSERT(r == 0);
  160. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  161. ASSERT(r == 0);
  162. ASSERT(exit_cb_called == 1);
  163. ASSERT(close_cb_called == 2); /* Once for process once for the pipe. */
  164. printf("output is: %s", output);
  165. ASSERT(strcmp("hello world\n", output) == 0);
  166. MAKE_VALGRIND_HAPPY();
  167. return 0;
  168. }
  169. TEST_IMPL(spawn_stdout_to_file) {
  170. int r;
  171. uv_file file;
  172. uv_fs_t fs_req;
  173. uv_stdio_container_t stdio[2];
  174. /* Setup. */
  175. unlink("stdout_file");
  176. init_process_options("spawn_helper2", exit_cb);
  177. r = uv_fs_open(uv_default_loop(), &fs_req, "stdout_file", O_CREAT | O_RDWR,
  178. S_IRUSR | S_IWUSR, NULL);
  179. ASSERT(r != -1);
  180. uv_fs_req_cleanup(&fs_req);
  181. file = r;
  182. options.stdio = stdio;
  183. options.stdio[0].flags = UV_IGNORE;
  184. options.stdio[1].flags = UV_INHERIT_FD;
  185. options.stdio[1].data.fd = file;
  186. options.stdio_count = 2;
  187. r = uv_spawn(uv_default_loop(), &process, &options);
  188. ASSERT(r == 0);
  189. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  190. ASSERT(r == 0);
  191. ASSERT(exit_cb_called == 1);
  192. ASSERT(close_cb_called == 1);
  193. r = uv_fs_read(uv_default_loop(), &fs_req, file, output, sizeof(output),
  194. 0, NULL);
  195. ASSERT(r == 12);
  196. uv_fs_req_cleanup(&fs_req);
  197. r = uv_fs_close(uv_default_loop(), &fs_req, file, NULL);
  198. ASSERT(r == 0);
  199. uv_fs_req_cleanup(&fs_req);
  200. printf("output is: %s", output);
  201. ASSERT(strcmp("hello world\n", output) == 0);
  202. /* Cleanup. */
  203. unlink("stdout_file");
  204. MAKE_VALGRIND_HAPPY();
  205. return 0;
  206. }
  207. TEST_IMPL(spawn_stdout_and_stderr_to_file) {
  208. int r;
  209. uv_file file;
  210. uv_fs_t fs_req;
  211. uv_stdio_container_t stdio[3];
  212. /* Setup. */
  213. unlink("stdout_file");
  214. init_process_options("spawn_helper6", exit_cb);
  215. r = uv_fs_open(uv_default_loop(), &fs_req, "stdout_file", O_CREAT | O_RDWR,
  216. S_IRUSR | S_IWUSR, NULL);
  217. ASSERT(r != -1);
  218. uv_fs_req_cleanup(&fs_req);
  219. file = r;
  220. options.stdio = stdio;
  221. options.stdio[0].flags = UV_IGNORE;
  222. options.stdio[1].flags = UV_INHERIT_FD;
  223. options.stdio[1].data.fd = file;
  224. options.stdio[2].flags = UV_INHERIT_FD;
  225. options.stdio[2].data.fd = file;
  226. options.stdio_count = 3;
  227. r = uv_spawn(uv_default_loop(), &process, &options);
  228. ASSERT(r == 0);
  229. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  230. ASSERT(r == 0);
  231. ASSERT(exit_cb_called == 1);
  232. ASSERT(close_cb_called == 1);
  233. r = uv_fs_read(uv_default_loop(), &fs_req, file, output, sizeof(output),
  234. 0, NULL);
  235. ASSERT(r == 27);
  236. uv_fs_req_cleanup(&fs_req);
  237. r = uv_fs_close(uv_default_loop(), &fs_req, file, NULL);
  238. ASSERT(r == 0);
  239. uv_fs_req_cleanup(&fs_req);
  240. printf("output is: %s", output);
  241. ASSERT(strcmp("hello world\nhello errworld\n", output) == 0);
  242. /* Cleanup. */
  243. unlink("stdout_file");
  244. MAKE_VALGRIND_HAPPY();
  245. return 0;
  246. }
  247. TEST_IMPL(spawn_stdin) {
  248. int r;
  249. uv_pipe_t out;
  250. uv_pipe_t in;
  251. uv_write_t write_req;
  252. uv_buf_t buf;
  253. uv_stdio_container_t stdio[2];
  254. char buffer[] = "hello-from-spawn_stdin";
  255. init_process_options("spawn_helper3", exit_cb);
  256. uv_pipe_init(uv_default_loop(), &out, 0);
  257. uv_pipe_init(uv_default_loop(), &in, 0);
  258. options.stdio = stdio;
  259. options.stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE;
  260. options.stdio[0].data.stream = (uv_stream_t*)&in;
  261. options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
  262. options.stdio[1].data.stream = (uv_stream_t*)&out;
  263. options.stdio_count = 2;
  264. r = uv_spawn(uv_default_loop(), &process, &options);
  265. ASSERT(r == 0);
  266. buf.base = buffer;
  267. buf.len = sizeof(buffer);
  268. r = uv_write(&write_req, (uv_stream_t*)&in, &buf, 1, write_cb);
  269. ASSERT(r == 0);
  270. r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read);
  271. ASSERT(r == 0);
  272. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  273. ASSERT(r == 0);
  274. ASSERT(exit_cb_called == 1);
  275. ASSERT(close_cb_called == 3); /* Once for process twice for the pipe. */
  276. ASSERT(strcmp(buffer, output) == 0);
  277. MAKE_VALGRIND_HAPPY();
  278. return 0;
  279. }
  280. TEST_IMPL(spawn_stdio_greater_than_3) {
  281. int r;
  282. uv_pipe_t pipe;
  283. uv_stdio_container_t stdio[4];
  284. init_process_options("spawn_helper5", exit_cb);
  285. uv_pipe_init(uv_default_loop(), &pipe, 0);
  286. options.stdio = stdio;
  287. options.stdio[0].flags = UV_IGNORE;
  288. options.stdio[1].flags = UV_IGNORE;
  289. options.stdio[2].flags = UV_IGNORE;
  290. options.stdio[3].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
  291. options.stdio[3].data.stream = (uv_stream_t*)&pipe;
  292. options.stdio_count = 4;
  293. r = uv_spawn(uv_default_loop(), &process, &options);
  294. ASSERT(r == 0);
  295. r = uv_read_start((uv_stream_t*) &pipe, on_alloc, on_read);
  296. ASSERT(r == 0);
  297. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  298. ASSERT(r == 0);
  299. ASSERT(exit_cb_called == 1);
  300. ASSERT(close_cb_called == 2); /* Once for process once for the pipe. */
  301. printf("output from stdio[3] is: %s", output);
  302. ASSERT(strcmp("fourth stdio!\n", output) == 0);
  303. MAKE_VALGRIND_HAPPY();
  304. return 0;
  305. }
  306. TEST_IMPL(spawn_ignored_stdio) {
  307. int r;
  308. init_process_options("spawn_helper6", exit_cb);
  309. options.stdio = NULL;
  310. options.stdio_count = 0;
  311. r = uv_spawn(uv_default_loop(), &process, &options);
  312. ASSERT(r == 0);
  313. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  314. ASSERT(r == 0);
  315. ASSERT(exit_cb_called == 1);
  316. ASSERT(close_cb_called == 1);
  317. MAKE_VALGRIND_HAPPY();
  318. return 0;
  319. }
  320. TEST_IMPL(spawn_and_kill) {
  321. int r;
  322. init_process_options("spawn_helper4", kill_cb);
  323. r = uv_spawn(uv_default_loop(), &process, &options);
  324. ASSERT(r == 0);
  325. r = uv_timer_init(uv_default_loop(), &timer);
  326. ASSERT(r == 0);
  327. r = uv_timer_start(&timer, timer_cb, 500, 0);
  328. ASSERT(r == 0);
  329. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  330. ASSERT(r == 0);
  331. ASSERT(exit_cb_called == 1);
  332. ASSERT(close_cb_called == 2); /* Once for process and once for timer. */
  333. MAKE_VALGRIND_HAPPY();
  334. return 0;
  335. }
  336. TEST_IMPL(spawn_preserve_env) {
  337. int r;
  338. uv_pipe_t out;
  339. uv_stdio_container_t stdio[2];
  340. init_process_options("spawn_helper7", exit_cb);
  341. uv_pipe_init(uv_default_loop(), &out, 0);
  342. options.stdio = stdio;
  343. options.stdio[0].flags = UV_IGNORE;
  344. options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
  345. options.stdio[1].data.stream = (uv_stream_t*) &out;
  346. options.stdio_count = 2;
  347. r = putenv("ENV_TEST=testval");
  348. ASSERT(r == 0);
  349. /* Explicitly set options.env to NULL to test for env clobbering. */
  350. options.env = NULL;
  351. r = uv_spawn(uv_default_loop(), &process, &options);
  352. ASSERT(r == 0);
  353. r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read);
  354. ASSERT(r == 0);
  355. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  356. ASSERT(r == 0);
  357. ASSERT(exit_cb_called == 1);
  358. ASSERT(close_cb_called == 2);
  359. printf("output is: %s", output);
  360. ASSERT(strcmp("testval", output) == 0);
  361. MAKE_VALGRIND_HAPPY();
  362. return 0;
  363. }
  364. TEST_IMPL(spawn_detached) {
  365. int r;
  366. init_process_options("spawn_helper4", detach_failure_cb);
  367. options.flags |= UV_PROCESS_DETACHED;
  368. r = uv_spawn(uv_default_loop(), &process, &options);
  369. ASSERT(r == 0);
  370. uv_unref((uv_handle_t*)&process);
  371. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  372. ASSERT(r == 0);
  373. ASSERT(exit_cb_called == 0);
  374. r = uv_kill(process.pid, 0);
  375. ASSERT(r == 0);
  376. r = uv_kill(process.pid, 15);
  377. ASSERT(r == 0);
  378. MAKE_VALGRIND_HAPPY();
  379. return 0;
  380. }
  381. TEST_IMPL(spawn_and_kill_with_std) {
  382. int r;
  383. uv_pipe_t in, out, err;
  384. uv_write_t write;
  385. char message[] = "Nancy's joining me because the message this evening is "
  386. "not my message but ours.";
  387. uv_buf_t buf;
  388. uv_stdio_container_t stdio[3];
  389. init_process_options("spawn_helper4", kill_cb);
  390. r = uv_pipe_init(uv_default_loop(), &in, 0);
  391. ASSERT(r == 0);
  392. r = uv_pipe_init(uv_default_loop(), &out, 0);
  393. ASSERT(r == 0);
  394. r = uv_pipe_init(uv_default_loop(), &err, 0);
  395. ASSERT(r == 0);
  396. options.stdio = stdio;
  397. options.stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE;
  398. options.stdio[0].data.stream = (uv_stream_t*)&in;
  399. options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
  400. options.stdio[1].data.stream = (uv_stream_t*)&out;
  401. options.stdio[2].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
  402. options.stdio[2].data.stream = (uv_stream_t*)&err;
  403. options.stdio_count = 3;
  404. r = uv_spawn(uv_default_loop(), &process, &options);
  405. ASSERT(r == 0);
  406. buf = uv_buf_init(message, sizeof message);
  407. r = uv_write(&write, (uv_stream_t*) &in, &buf, 1, write_cb);
  408. ASSERT(r == 0);
  409. r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read);
  410. ASSERT(r == 0);
  411. r = uv_read_start((uv_stream_t*) &err, on_alloc, on_read);
  412. ASSERT(r == 0);
  413. r = uv_timer_init(uv_default_loop(), &timer);
  414. ASSERT(r == 0);
  415. r = uv_timer_start(&timer, timer_cb, 500, 0);
  416. ASSERT(r == 0);
  417. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  418. ASSERT(r == 0);
  419. ASSERT(exit_cb_called == 1);
  420. ASSERT(close_cb_called == 5); /* process x 1, timer x 1, stdio x 3. */
  421. MAKE_VALGRIND_HAPPY();
  422. return 0;
  423. }
  424. TEST_IMPL(spawn_and_ping) {
  425. uv_write_t write_req;
  426. uv_pipe_t in, out;
  427. uv_buf_t buf;
  428. uv_stdio_container_t stdio[2];
  429. int r;
  430. init_process_options("spawn_helper3", exit_cb);
  431. buf = uv_buf_init("TEST", 4);
  432. uv_pipe_init(uv_default_loop(), &out, 0);
  433. uv_pipe_init(uv_default_loop(), &in, 0);
  434. options.stdio = stdio;
  435. options.stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE;
  436. options.stdio[0].data.stream = (uv_stream_t*)&in;
  437. options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
  438. options.stdio[1].data.stream = (uv_stream_t*)&out;
  439. options.stdio_count = 2;
  440. r = uv_spawn(uv_default_loop(), &process, &options);
  441. ASSERT(r == 0);
  442. /* Sending signum == 0 should check if the
  443. * child process is still alive, not kill it.
  444. */
  445. r = uv_process_kill(&process, 0);
  446. ASSERT(r == 0);
  447. r = uv_write(&write_req, (uv_stream_t*)&in, &buf, 1, write_cb);
  448. ASSERT(r == 0);
  449. r = uv_read_start((uv_stream_t*)&out, on_alloc, on_read);
  450. ASSERT(r == 0);
  451. ASSERT(exit_cb_called == 0);
  452. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  453. ASSERT(r == 0);
  454. ASSERT(exit_cb_called == 1);
  455. ASSERT(strcmp(output, "TEST") == 0);
  456. MAKE_VALGRIND_HAPPY();
  457. return 0;
  458. }
  459. TEST_IMPL(spawn_same_stdout_stderr) {
  460. uv_write_t write_req;
  461. uv_pipe_t in, out;
  462. uv_buf_t buf;
  463. uv_stdio_container_t stdio[3];
  464. int r;
  465. init_process_options("spawn_helper3", exit_cb);
  466. buf = uv_buf_init("TEST", 4);
  467. uv_pipe_init(uv_default_loop(), &out, 0);
  468. uv_pipe_init(uv_default_loop(), &in, 0);
  469. options.stdio = stdio;
  470. options.stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE;
  471. options.stdio[0].data.stream = (uv_stream_t*)&in;
  472. options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
  473. options.stdio[1].data.stream = (uv_stream_t*)&out;
  474. options.stdio_count = 2;
  475. r = uv_spawn(uv_default_loop(), &process, &options);
  476. ASSERT(r == 0);
  477. /* Sending signum == 0 should check if the
  478. * child process is still alive, not kill it.
  479. */
  480. r = uv_process_kill(&process, 0);
  481. ASSERT(r == 0);
  482. r = uv_write(&write_req, (uv_stream_t*)&in, &buf, 1, write_cb);
  483. ASSERT(r == 0);
  484. r = uv_read_start((uv_stream_t*)&out, on_alloc, on_read);
  485. ASSERT(r == 0);
  486. ASSERT(exit_cb_called == 0);
  487. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  488. ASSERT(r == 0);
  489. ASSERT(exit_cb_called == 1);
  490. ASSERT(strcmp(output, "TEST") == 0);
  491. MAKE_VALGRIND_HAPPY();
  492. return 0;
  493. }
  494. TEST_IMPL(kill) {
  495. int r;
  496. #ifdef _WIN32
  497. no_term_signal = 1;
  498. #endif
  499. init_process_options("spawn_helper4", kill_cb);
  500. r = uv_spawn(uv_default_loop(), &process, &options);
  501. ASSERT(r == 0);
  502. /* Sending signum == 0 should check if the
  503. * child process is still alive, not kill it.
  504. */
  505. r = uv_kill(process.pid, 0);
  506. ASSERT(r == 0);
  507. /* Kill the process. */
  508. r = uv_kill(process.pid, /* SIGTERM */ 15);
  509. ASSERT(r == 0);
  510. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  511. ASSERT(r == 0);
  512. ASSERT(exit_cb_called == 1);
  513. ASSERT(close_cb_called == 1);
  514. MAKE_VALGRIND_HAPPY();
  515. return 0;
  516. }
  517. #ifdef _WIN32
  518. TEST_IMPL(spawn_detect_pipe_name_collisions_on_windows) {
  519. int r;
  520. uv_pipe_t out;
  521. char name[64];
  522. HANDLE pipe_handle;
  523. uv_stdio_container_t stdio[2];
  524. init_process_options("spawn_helper2", exit_cb);
  525. uv_pipe_init(uv_default_loop(), &out, 0);
  526. options.stdio = stdio;
  527. options.stdio[0].flags = UV_IGNORE;
  528. options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
  529. options.stdio[1].data.stream = (uv_stream_t*)&out;
  530. options.stdio_count = 2;
  531. /* Create a pipe that'll cause a collision. */
  532. _snprintf(name,
  533. sizeof(name),
  534. "\\\\.\\pipe\\uv\\%p-%d",
  535. &out,
  536. GetCurrentProcessId());
  537. pipe_handle = CreateNamedPipeA(name,
  538. PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED,
  539. PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
  540. 10,
  541. 65536,
  542. 65536,
  543. 0,
  544. NULL);
  545. ASSERT(pipe_handle != INVALID_HANDLE_VALUE);
  546. r = uv_spawn(uv_default_loop(), &process, &options);
  547. ASSERT(r == 0);
  548. r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read);
  549. ASSERT(r == 0);
  550. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  551. ASSERT(r == 0);
  552. ASSERT(exit_cb_called == 1);
  553. ASSERT(close_cb_called == 2); /* Once for process once for the pipe. */
  554. printf("output is: %s", output);
  555. ASSERT(strcmp("hello world\n", output) == 0);
  556. MAKE_VALGRIND_HAPPY();
  557. return 0;
  558. }
  559. int make_program_args(char** args, int verbatim_arguments, WCHAR** dst_ptr);
  560. WCHAR* quote_cmd_arg(const WCHAR *source, WCHAR *target);
  561. TEST_IMPL(argument_escaping) {
  562. const WCHAR* test_str[] = {
  563. L"HelloWorld",
  564. L"Hello World",
  565. L"Hello\"World",
  566. L"Hello World\\",
  567. L"Hello\\\"World",
  568. L"Hello\\World",
  569. L"Hello\\\\World",
  570. L"Hello World\\",
  571. L"c:\\path\\to\\node.exe --eval \"require('c:\\\\path\\\\to\\\\test.js')\""
  572. };
  573. const int count = sizeof(test_str) / sizeof(*test_str);
  574. WCHAR** test_output;
  575. WCHAR* command_line;
  576. WCHAR** cracked;
  577. size_t total_size = 0;
  578. int i;
  579. int num_args;
  580. int result;
  581. char* verbatim[] = {
  582. "cmd.exe",
  583. "/c",
  584. "c:\\path\\to\\node.exe --eval \"require('c:\\\\path\\\\to\\\\test.js')\"",
  585. NULL
  586. };
  587. WCHAR* verbatim_output;
  588. WCHAR* non_verbatim_output;
  589. test_output = calloc(count, sizeof(WCHAR*));
  590. for (i = 0; i < count; ++i) {
  591. test_output[i] = calloc(2 * (wcslen(test_str[i]) + 2), sizeof(WCHAR));
  592. quote_cmd_arg(test_str[i], test_output[i]);
  593. wprintf(L"input : %s\n", test_str[i]);
  594. wprintf(L"output: %s\n", test_output[i]);
  595. total_size += wcslen(test_output[i]) + 1;
  596. }
  597. command_line = calloc(total_size + 1, sizeof(WCHAR));
  598. for (i = 0; i < count; ++i) {
  599. wcscat(command_line, test_output[i]);
  600. wcscat(command_line, L" ");
  601. }
  602. command_line[total_size - 1] = L'\0';
  603. wprintf(L"command_line: %s\n", command_line);
  604. cracked = CommandLineToArgvW(command_line, &num_args);
  605. for (i = 0; i < num_args; ++i) {
  606. wprintf(L"%d: %s\t%s\n", i, test_str[i], cracked[i]);
  607. ASSERT(wcscmp(test_str[i], cracked[i]) == 0);
  608. }
  609. LocalFree(cracked);
  610. for (i = 0; i < count; ++i) {
  611. free(test_output[i]);
  612. }
  613. result = make_program_args(verbatim, 1, &verbatim_output);
  614. ASSERT(result == 0);
  615. result = make_program_args(verbatim, 0, &non_verbatim_output);
  616. ASSERT(result == 0);
  617. wprintf(L" verbatim_output: %s\n", verbatim_output);
  618. wprintf(L"non_verbatim_output: %s\n", non_verbatim_output);
  619. ASSERT(wcscmp(verbatim_output,
  620. L"cmd.exe /c c:\\path\\to\\node.exe --eval "
  621. L"\"require('c:\\\\path\\\\to\\\\test.js')\"") == 0);
  622. ASSERT(wcscmp(non_verbatim_output,
  623. L"cmd.exe /c \"c:\\path\\to\\node.exe --eval "
  624. L"\\\"require('c:\\\\path\\\\to\\\\test.js')\\\"\"") == 0);
  625. free(verbatim_output);
  626. free(non_verbatim_output);
  627. return 0;
  628. }
  629. int make_program_env(char** env_block, WCHAR** dst_ptr);
  630. TEST_IMPL(environment_creation) {
  631. int i;
  632. char* environment[] = {
  633. "FOO=BAR",
  634. "SYSTEM=ROOT", /* substring of a supplied var name */
  635. "SYSTEMROOTED=OMG", /* supplied var name is a substring */
  636. "TEMP=C:\\Temp",
  637. "BAZ=QUX",
  638. NULL
  639. };
  640. WCHAR expected[512];
  641. WCHAR* ptr = expected;
  642. int result;
  643. WCHAR* str;
  644. WCHAR* env;
  645. for (i = 0; i < sizeof(environment) / sizeof(environment[0]) - 1; i++) {
  646. ptr += uv_utf8_to_utf16(environment[i],
  647. ptr,
  648. expected + sizeof(expected) - ptr);
  649. }
  650. memcpy(ptr, L"SYSTEMROOT=", sizeof(L"SYSTEMROOT="));
  651. ptr += sizeof(L"SYSTEMROOT=")/sizeof(WCHAR) - 1;
  652. ptr += GetEnvironmentVariableW(L"SYSTEMROOT",
  653. ptr,
  654. expected + sizeof(expected) - ptr);
  655. ++ptr;
  656. memcpy(ptr, L"SYSTEMDRIVE=", sizeof(L"SYSTEMDRIVE="));
  657. ptr += sizeof(L"SYSTEMDRIVE=")/sizeof(WCHAR) - 1;
  658. ptr += GetEnvironmentVariableW(L"SYSTEMDRIVE",
  659. ptr,
  660. expected + sizeof(expected) - ptr);
  661. ++ptr;
  662. *ptr = '\0';
  663. result = make_program_env(environment, &env);
  664. ASSERT(result == 0);
  665. for (str = env; *str; str += wcslen(str) + 1) {
  666. wprintf(L"%s\n", str);
  667. }
  668. ASSERT(wcscmp(expected, env) == 0);
  669. return 0;
  670. }
  671. #endif
  672. #ifndef _WIN32
  673. TEST_IMPL(spawn_setuid_setgid) {
  674. int r;
  675. /* if not root, then this will fail. */
  676. uv_uid_t uid = getuid();
  677. if (uid != 0) {
  678. fprintf(stderr, "spawn_setuid_setgid skipped: not root\n");
  679. return 0;
  680. }
  681. init_process_options("spawn_helper1", exit_cb);
  682. /* become the "nobody" user. */
  683. struct passwd* pw;
  684. pw = getpwnam("nobody");
  685. ASSERT(pw != NULL);
  686. options.uid = pw->pw_uid;
  687. options.gid = pw->pw_gid;
  688. options.flags = UV_PROCESS_SETUID | UV_PROCESS_SETGID;
  689. r = uv_spawn(uv_default_loop(), &process, &options);
  690. ASSERT(r == 0);
  691. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  692. ASSERT(r == 0);
  693. ASSERT(exit_cb_called == 1);
  694. ASSERT(close_cb_called == 1);
  695. MAKE_VALGRIND_HAPPY();
  696. return 0;
  697. }
  698. #endif
  699. #ifndef _WIN32
  700. TEST_IMPL(spawn_setuid_fails) {
  701. int r;
  702. /* if root, become nobody. */
  703. uv_uid_t uid = getuid();
  704. if (uid == 0) {
  705. struct passwd* pw;
  706. pw = getpwnam("nobody");
  707. ASSERT(pw != NULL);
  708. ASSERT(0 == setgid(pw->pw_gid));
  709. ASSERT(0 == setuid(pw->pw_uid));
  710. }
  711. init_process_options("spawn_helper1", fail_cb);
  712. options.flags |= UV_PROCESS_SETUID;
  713. options.uid = 0;
  714. r = uv_spawn(uv_default_loop(), &process, &options);
  715. ASSERT(r == UV_EPERM);
  716. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  717. ASSERT(r == 0);
  718. ASSERT(close_cb_called == 0);
  719. MAKE_VALGRIND_HAPPY();
  720. return 0;
  721. }
  722. TEST_IMPL(spawn_setgid_fails) {
  723. int r;
  724. /* if root, become nobody. */
  725. uv_uid_t uid = getuid();
  726. if (uid == 0) {
  727. struct passwd* pw;
  728. pw = getpwnam("nobody");
  729. ASSERT(pw != NULL);
  730. ASSERT(0 == setgid(pw->pw_gid));
  731. ASSERT(0 == setuid(pw->pw_uid));
  732. }
  733. init_process_options("spawn_helper1", fail_cb);
  734. options.flags |= UV_PROCESS_SETGID;
  735. options.gid = 0;
  736. r = uv_spawn(uv_default_loop(), &process, &options);
  737. ASSERT(r == UV_EPERM);
  738. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  739. ASSERT(r == 0);
  740. ASSERT(close_cb_called == 0);
  741. MAKE_VALGRIND_HAPPY();
  742. return 0;
  743. }
  744. #endif
  745. #ifdef _WIN32
  746. static void exit_cb_unexpected(uv_process_t* process,
  747. int64_t exit_status,
  748. int term_signal) {
  749. ASSERT(0 && "should not have been called");
  750. }
  751. TEST_IMPL(spawn_setuid_fails) {
  752. int r;
  753. init_process_options("spawn_helper1", exit_cb_unexpected);
  754. options.flags |= UV_PROCESS_SETUID;
  755. options.uid = (uv_uid_t) -42424242;
  756. r = uv_spawn(uv_default_loop(), &process, &options);
  757. ASSERT(r == UV_ENOTSUP);
  758. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  759. ASSERT(r == 0);
  760. ASSERT(close_cb_called == 0);
  761. MAKE_VALGRIND_HAPPY();
  762. return 0;
  763. }
  764. TEST_IMPL(spawn_setgid_fails) {
  765. int r;
  766. init_process_options("spawn_helper1", exit_cb_unexpected);
  767. options.flags |= UV_PROCESS_SETGID;
  768. options.gid = (uv_gid_t) -42424242;
  769. r = uv_spawn(uv_default_loop(), &process, &options);
  770. ASSERT(r == UV_ENOTSUP);
  771. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  772. ASSERT(r == 0);
  773. ASSERT(close_cb_called == 0);
  774. MAKE_VALGRIND_HAPPY();
  775. return 0;
  776. }
  777. #endif
  778. TEST_IMPL(spawn_auto_unref) {
  779. init_process_options("spawn_helper1", NULL);
  780. ASSERT(0 == uv_spawn(uv_default_loop(), &process, &options));
  781. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
  782. ASSERT(0 == uv_is_closing((uv_handle_t*) &process));
  783. uv_close((uv_handle_t*) &process, NULL);
  784. ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
  785. ASSERT(1 == uv_is_closing((uv_handle_t*) &process));
  786. MAKE_VALGRIND_HAPPY();
  787. return 0;
  788. }