1
0

test-ipc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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 <stdio.h>
  24. #include <string.h>
  25. static uv_pipe_t channel;
  26. static uv_tcp_t tcp_server;
  27. static uv_tcp_t tcp_connection;
  28. static int exit_cb_called;
  29. static int read2_cb_called;
  30. static int tcp_write_cb_called;
  31. static int tcp_read_cb_called;
  32. static int on_pipe_read_called;
  33. static int local_conn_accepted;
  34. static int remote_conn_accepted;
  35. static int tcp_server_listening;
  36. static uv_write_t write_req;
  37. static uv_pipe_t channel;
  38. static uv_tcp_t tcp_server;
  39. static uv_write_t conn_notify_req;
  40. static int close_cb_called;
  41. static int connection_accepted;
  42. static int tcp_conn_read_cb_called;
  43. static int tcp_conn_write_cb_called;
  44. typedef struct {
  45. uv_connect_t conn_req;
  46. uv_write_t tcp_write_req;
  47. uv_tcp_t conn;
  48. } tcp_conn;
  49. #define CONN_COUNT 100
  50. static void close_server_conn_cb(uv_handle_t* handle) {
  51. free(handle);
  52. }
  53. static void on_connection(uv_stream_t* server, int status) {
  54. uv_tcp_t* conn;
  55. int r;
  56. if (!local_conn_accepted) {
  57. /* Accept the connection and close it. Also and close the server. */
  58. ASSERT(status == 0);
  59. ASSERT((uv_stream_t*)&tcp_server == server);
  60. conn = malloc(sizeof(*conn));
  61. ASSERT(conn);
  62. r = uv_tcp_init(server->loop, conn);
  63. ASSERT(r == 0);
  64. r = uv_accept(server, (uv_stream_t*)conn);
  65. ASSERT(r == 0);
  66. uv_close((uv_handle_t*)conn, close_server_conn_cb);
  67. uv_close((uv_handle_t*)server, NULL);
  68. local_conn_accepted = 1;
  69. }
  70. }
  71. static void exit_cb(uv_process_t* process, int exit_status, int term_signal) {
  72. printf("exit_cb\n");
  73. exit_cb_called++;
  74. ASSERT(exit_status == 0);
  75. uv_close((uv_handle_t*)process, NULL);
  76. }
  77. static uv_buf_t on_alloc(uv_handle_t* handle, size_t suggested_size) {
  78. return uv_buf_init(malloc(suggested_size), suggested_size);
  79. }
  80. static void close_client_conn_cb(uv_handle_t* handle) {
  81. tcp_conn* p = (tcp_conn*)handle->data;
  82. free(p);
  83. }
  84. static void connect_cb(uv_connect_t* req, int status) {
  85. uv_close((uv_handle_t*)req->handle, close_client_conn_cb);
  86. }
  87. static void make_many_connections(void) {
  88. tcp_conn* conn;
  89. struct sockaddr_in addr;
  90. int r, i;
  91. for (i = 0; i < CONN_COUNT; i++) {
  92. conn = malloc(sizeof(*conn));
  93. ASSERT(conn);
  94. r = uv_tcp_init(uv_default_loop(), &conn->conn);
  95. ASSERT(r == 0);
  96. addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
  97. r = uv_tcp_connect(&conn->conn_req, (uv_tcp_t*)&conn->conn, addr, connect_cb);
  98. ASSERT(r == 0);
  99. conn->conn.data = conn;
  100. }
  101. }
  102. static void on_read(uv_pipe_t* pipe, ssize_t nread, uv_buf_t buf,
  103. uv_handle_type pending) {
  104. int r;
  105. uv_buf_t outbuf;
  106. uv_err_t err;
  107. if (nread == 0) {
  108. /* Everything OK, but nothing read. */
  109. free(buf.base);
  110. return;
  111. }
  112. if (nread < 0) {
  113. err = uv_last_error(pipe->loop);
  114. if (err.code == UV_EOF) {
  115. free(buf.base);
  116. return;
  117. }
  118. printf("error recving on channel: %s\n", uv_strerror(err));
  119. abort();
  120. }
  121. fprintf(stderr, "got %d bytes\n", (int)nread);
  122. if (!tcp_server_listening) {
  123. ASSERT(nread > 0 && buf.base && pending != UV_UNKNOWN_HANDLE);
  124. read2_cb_called++;
  125. /* Accept the pending TCP server, and start listening on it. */
  126. ASSERT(pending == UV_TCP);
  127. r = uv_tcp_init(uv_default_loop(), &tcp_server);
  128. ASSERT(r == 0);
  129. r = uv_accept((uv_stream_t*)pipe, (uv_stream_t*)&tcp_server);
  130. ASSERT(r == 0);
  131. r = uv_listen((uv_stream_t*)&tcp_server, 12, on_connection);
  132. ASSERT(r == 0);
  133. tcp_server_listening = 1;
  134. /* Make sure that the expected data is correctly multiplexed. */
  135. ASSERT(memcmp("hello\n", buf.base, nread) == 0);
  136. outbuf = uv_buf_init("world\n", 6);
  137. r = uv_write(&write_req, (uv_stream_t*)pipe, &outbuf, 1, NULL);
  138. ASSERT(r == 0);
  139. /* Create a bunch of connections to get both servers to accept. */
  140. make_many_connections();
  141. } else if (memcmp("accepted_connection\n", buf.base, nread) == 0) {
  142. /* Remote server has accepted a connection. Close the channel. */
  143. ASSERT(pending == UV_UNKNOWN_HANDLE);
  144. remote_conn_accepted = 1;
  145. uv_close((uv_handle_t*)&channel, NULL);
  146. }
  147. free(buf.base);
  148. }
  149. void spawn_helper(uv_pipe_t* channel,
  150. uv_process_t* process,
  151. const char* helper) {
  152. uv_process_options_t options;
  153. size_t exepath_size;
  154. char exepath[1024];
  155. char* args[3];
  156. int r;
  157. uv_stdio_container_t stdio[1];
  158. r = uv_pipe_init(uv_default_loop(), channel, 1);
  159. ASSERT(r == 0);
  160. ASSERT(channel->ipc);
  161. exepath_size = sizeof(exepath);
  162. r = uv_exepath(exepath, &exepath_size);
  163. ASSERT(r == 0);
  164. exepath[exepath_size] = '\0';
  165. args[0] = exepath;
  166. args[1] = (char*)helper;
  167. args[2] = NULL;
  168. memset(&options, 0, sizeof(options));
  169. options.file = exepath;
  170. options.args = args;
  171. options.exit_cb = exit_cb;
  172. options.stdio = stdio;
  173. options.stdio[0].flags = UV_CREATE_PIPE |
  174. UV_READABLE_PIPE | UV_WRITABLE_PIPE;
  175. options.stdio[0].data.stream = (uv_stream_t*)channel;
  176. options.stdio_count = 1;
  177. r = uv_spawn(uv_default_loop(), process, options);
  178. ASSERT(r == 0);
  179. }
  180. static void on_tcp_write(uv_write_t* req, int status) {
  181. ASSERT(status == 0);
  182. ASSERT(req->handle == (uv_stream_t*)&tcp_connection);
  183. tcp_write_cb_called++;
  184. }
  185. static uv_buf_t on_read_alloc(uv_handle_t* handle, size_t suggested_size) {
  186. uv_buf_t buf;
  187. buf.base = (char*)malloc(suggested_size);
  188. buf.len = suggested_size;
  189. return buf;
  190. }
  191. static void on_tcp_read(uv_stream_t* tcp, ssize_t nread, uv_buf_t buf) {
  192. ASSERT(nread > 0);
  193. ASSERT(memcmp("hello again\n", buf.base, nread) == 0);
  194. ASSERT(tcp == (uv_stream_t*)&tcp_connection);
  195. free(buf.base);
  196. tcp_read_cb_called++;
  197. uv_close((uv_handle_t*)tcp, NULL);
  198. uv_close((uv_handle_t*)&channel, NULL);
  199. }
  200. static void on_read_connection(uv_pipe_t* pipe, ssize_t nread, uv_buf_t buf,
  201. uv_handle_type pending) {
  202. int r;
  203. uv_buf_t outbuf;
  204. uv_err_t err;
  205. if (nread == 0) {
  206. /* Everything OK, but nothing read. */
  207. free(buf.base);
  208. return;
  209. }
  210. if (nread < 0) {
  211. err = uv_last_error(pipe->loop);
  212. if (err.code == UV_EOF) {
  213. free(buf.base);
  214. return;
  215. }
  216. printf("error recving on channel: %s\n", uv_strerror(err));
  217. abort();
  218. }
  219. fprintf(stderr, "got %d bytes\n", (int)nread);
  220. ASSERT(nread > 0 && buf.base && pending != UV_UNKNOWN_HANDLE);
  221. read2_cb_called++;
  222. /* Accept the pending TCP connection */
  223. ASSERT(pending == UV_TCP);
  224. r = uv_tcp_init(uv_default_loop(), &tcp_connection);
  225. ASSERT(r == 0);
  226. r = uv_accept((uv_stream_t*)pipe, (uv_stream_t*)&tcp_connection);
  227. ASSERT(r == 0);
  228. /* Make sure that the expected data is correctly multiplexed. */
  229. ASSERT(memcmp("hello\n", buf.base, nread) == 0);
  230. /* Write/read to/from the connection */
  231. outbuf = uv_buf_init("world\n", 6);
  232. r = uv_write(&write_req, (uv_stream_t*)&tcp_connection, &outbuf, 1,
  233. on_tcp_write);
  234. ASSERT(r == 0);
  235. r = uv_read_start((uv_stream_t*)&tcp_connection, on_read_alloc, on_tcp_read);
  236. ASSERT(r == 0);
  237. free(buf.base);
  238. }
  239. static int run_ipc_test(const char* helper, uv_read2_cb read_cb) {
  240. uv_process_t process;
  241. int r;
  242. spawn_helper(&channel, &process, helper);
  243. uv_read2_start((uv_stream_t*)&channel, on_alloc, read_cb);
  244. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  245. ASSERT(r == 0);
  246. MAKE_VALGRIND_HAPPY();
  247. return 0;
  248. }
  249. TEST_IMPL(ipc_listen_before_write) {
  250. int r = run_ipc_test("ipc_helper_listen_before_write", on_read);
  251. ASSERT(local_conn_accepted == 1);
  252. ASSERT(remote_conn_accepted == 1);
  253. ASSERT(read2_cb_called == 1);
  254. ASSERT(exit_cb_called == 1);
  255. return r;
  256. }
  257. TEST_IMPL(ipc_listen_after_write) {
  258. int r = run_ipc_test("ipc_helper_listen_after_write", on_read);
  259. ASSERT(local_conn_accepted == 1);
  260. ASSERT(remote_conn_accepted == 1);
  261. ASSERT(read2_cb_called == 1);
  262. ASSERT(exit_cb_called == 1);
  263. return r;
  264. }
  265. TEST_IMPL(ipc_tcp_connection) {
  266. int r = run_ipc_test("ipc_helper_tcp_connection", on_read_connection);
  267. ASSERT(read2_cb_called == 1);
  268. ASSERT(tcp_write_cb_called == 1);
  269. ASSERT(tcp_read_cb_called == 1);
  270. ASSERT(exit_cb_called == 1);
  271. return r;
  272. }
  273. #ifdef _WIN32
  274. TEST_IMPL(listen_with_simultaneous_accepts) {
  275. uv_tcp_t server;
  276. int r;
  277. struct sockaddr_in addr = uv_ip4_addr("0.0.0.0", TEST_PORT);
  278. r = uv_tcp_init(uv_default_loop(), &server);
  279. ASSERT(r == 0);
  280. r = uv_tcp_bind(&server, addr);
  281. ASSERT(r == 0);
  282. r = uv_tcp_simultaneous_accepts(&server, 1);
  283. ASSERT(r == 0);
  284. r = uv_listen((uv_stream_t*)&server, SOMAXCONN, NULL);
  285. ASSERT(r == 0);
  286. ASSERT(server.reqs_pending == 32);
  287. MAKE_VALGRIND_HAPPY();
  288. return 0;
  289. }
  290. TEST_IMPL(listen_no_simultaneous_accepts) {
  291. uv_tcp_t server;
  292. int r;
  293. struct sockaddr_in addr = uv_ip4_addr("0.0.0.0", TEST_PORT);
  294. r = uv_tcp_init(uv_default_loop(), &server);
  295. ASSERT(r == 0);
  296. r = uv_tcp_bind(&server, addr);
  297. ASSERT(r == 0);
  298. r = uv_tcp_simultaneous_accepts(&server, 0);
  299. ASSERT(r == 0);
  300. r = uv_listen((uv_stream_t*)&server, SOMAXCONN, NULL);
  301. ASSERT(r == 0);
  302. ASSERT(server.reqs_pending == 1);
  303. MAKE_VALGRIND_HAPPY();
  304. return 0;
  305. }
  306. #endif
  307. /* Everything here runs in a child process. */
  308. static tcp_conn conn;
  309. static void close_cb(uv_handle_t* handle) {
  310. close_cb_called++;
  311. }
  312. static void conn_notify_write_cb(uv_write_t* req, int status) {
  313. uv_close((uv_handle_t*)&tcp_server, close_cb);
  314. uv_close((uv_handle_t*)&channel, close_cb);
  315. }
  316. static void tcp_connection_write_cb(uv_write_t* req, int status) {
  317. ASSERT((uv_handle_t*)&conn.conn == (uv_handle_t*)req->handle);
  318. uv_close((uv_handle_t*)req->handle, close_cb);
  319. uv_close((uv_handle_t*)&channel, close_cb);
  320. uv_close((uv_handle_t*)&tcp_server, close_cb);
  321. tcp_conn_write_cb_called++;
  322. }
  323. static void on_tcp_child_process_read(uv_stream_t* tcp, ssize_t nread, uv_buf_t buf) {
  324. uv_buf_t outbuf;
  325. int r;
  326. if (nread < 0) {
  327. if (uv_last_error(tcp->loop).code == UV_EOF) {
  328. free(buf.base);
  329. return;
  330. }
  331. printf("error recving on tcp connection: %s\n",
  332. uv_strerror(uv_last_error(tcp->loop)));
  333. abort();
  334. }
  335. ASSERT(nread > 0);
  336. ASSERT(memcmp("world\n", buf.base, nread) == 0);
  337. on_pipe_read_called++;
  338. free(buf.base);
  339. /* Write to the socket */
  340. outbuf = uv_buf_init("hello again\n", 12);
  341. r = uv_write(&conn.tcp_write_req, tcp, &outbuf, 1, tcp_connection_write_cb);
  342. ASSERT(r == 0);
  343. tcp_conn_read_cb_called++;
  344. }
  345. static void connect_child_process_cb(uv_connect_t* req, int status) {
  346. int r;
  347. ASSERT(status == 0);
  348. r = uv_read_start(req->handle, on_read_alloc, on_tcp_child_process_read);
  349. ASSERT(r == 0);
  350. }
  351. static void ipc_on_connection(uv_stream_t* server, int status) {
  352. int r;
  353. uv_buf_t buf;
  354. if (!connection_accepted) {
  355. /*
  356. * Accept the connection and close it. Also let the other
  357. * side know.
  358. */
  359. ASSERT(status == 0);
  360. ASSERT((uv_stream_t*)&tcp_server == server);
  361. r = uv_tcp_init(server->loop, &conn.conn);
  362. ASSERT(r == 0);
  363. r = uv_accept(server, (uv_stream_t*)&conn.conn);
  364. ASSERT(r == 0);
  365. uv_close((uv_handle_t*)&conn.conn, close_cb);
  366. buf = uv_buf_init("accepted_connection\n", 20);
  367. r = uv_write2(&conn_notify_req, (uv_stream_t*)&channel, &buf, 1,
  368. NULL, conn_notify_write_cb);
  369. ASSERT(r == 0);
  370. connection_accepted = 1;
  371. }
  372. }
  373. static void ipc_on_connection_tcp_conn(uv_stream_t* server, int status) {
  374. int r;
  375. uv_buf_t buf;
  376. uv_tcp_t* conn;
  377. ASSERT(status == 0);
  378. ASSERT((uv_stream_t*)&tcp_server == server);
  379. conn = malloc(sizeof(*conn));
  380. ASSERT(conn);
  381. r = uv_tcp_init(server->loop, conn);
  382. ASSERT(r == 0);
  383. r = uv_accept(server, (uv_stream_t*)conn);
  384. ASSERT(r == 0);
  385. /* Send the accepted connection to the other process */
  386. buf = uv_buf_init("hello\n", 6);
  387. r = uv_write2(&conn_notify_req, (uv_stream_t*)&channel, &buf, 1,
  388. (uv_stream_t*)conn, NULL);
  389. ASSERT(r == 0);
  390. r = uv_read_start((uv_stream_t*)conn, on_read_alloc, on_tcp_child_process_read);
  391. ASSERT(r == 0);
  392. uv_close((uv_handle_t*)conn, close_cb);
  393. }
  394. int ipc_helper(int listen_after_write) {
  395. /*
  396. * This is launched from test-ipc.c. stdin is a duplex channel that we
  397. * over which a handle will be transmitted.
  398. */
  399. uv_write_t write_req;
  400. int r;
  401. uv_buf_t buf;
  402. r = uv_pipe_init(uv_default_loop(), &channel, 1);
  403. ASSERT(r == 0);
  404. uv_pipe_open(&channel, 0);
  405. ASSERT(uv_is_readable((uv_stream_t*) &channel));
  406. ASSERT(uv_is_writable((uv_stream_t*) &channel));
  407. ASSERT(!uv_is_closing((uv_handle_t*) &channel));
  408. r = uv_tcp_init(uv_default_loop(), &tcp_server);
  409. ASSERT(r == 0);
  410. r = uv_tcp_bind(&tcp_server, uv_ip4_addr("0.0.0.0", TEST_PORT));
  411. ASSERT(r == 0);
  412. if (!listen_after_write) {
  413. r = uv_listen((uv_stream_t*)&tcp_server, 12, ipc_on_connection);
  414. ASSERT(r == 0);
  415. }
  416. buf = uv_buf_init("hello\n", 6);
  417. r = uv_write2(&write_req, (uv_stream_t*)&channel, &buf, 1,
  418. (uv_stream_t*)&tcp_server, NULL);
  419. ASSERT(r == 0);
  420. if (listen_after_write) {
  421. r = uv_listen((uv_stream_t*)&tcp_server, 12, ipc_on_connection);
  422. ASSERT(r == 0);
  423. }
  424. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  425. ASSERT(r == 0);
  426. ASSERT(connection_accepted == 1);
  427. ASSERT(close_cb_called == 3);
  428. MAKE_VALGRIND_HAPPY();
  429. return 0;
  430. }
  431. int ipc_helper_tcp_connection(void) {
  432. /*
  433. * This is launched from test-ipc.c. stdin is a duplex channel that we
  434. * over which a handle will be transmitted.
  435. */
  436. int r;
  437. struct sockaddr_in addr;
  438. r = uv_pipe_init(uv_default_loop(), &channel, 1);
  439. ASSERT(r == 0);
  440. uv_pipe_open(&channel, 0);
  441. ASSERT(uv_is_readable((uv_stream_t*)&channel));
  442. ASSERT(uv_is_writable((uv_stream_t*)&channel));
  443. ASSERT(!uv_is_closing((uv_handle_t*)&channel));
  444. r = uv_tcp_init(uv_default_loop(), &tcp_server);
  445. ASSERT(r == 0);
  446. r = uv_tcp_bind(&tcp_server, uv_ip4_addr("0.0.0.0", TEST_PORT));
  447. ASSERT(r == 0);
  448. r = uv_listen((uv_stream_t*)&tcp_server, 12, ipc_on_connection_tcp_conn);
  449. ASSERT(r == 0);
  450. /* Make a connection to the server */
  451. r = uv_tcp_init(uv_default_loop(), &conn.conn);
  452. ASSERT(r == 0);
  453. addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
  454. r = uv_tcp_connect(&conn.conn_req, (uv_tcp_t*)&conn.conn, addr, connect_child_process_cb);
  455. ASSERT(r == 0);
  456. r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  457. ASSERT(r == 0);
  458. ASSERT(tcp_conn_read_cb_called == 1);
  459. ASSERT(tcp_conn_write_cb_called == 1);
  460. ASSERT(close_cb_called == 4);
  461. MAKE_VALGRIND_HAPPY();
  462. return 0;
  463. }