foolio 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (261) hide show
  1. data/.gitignore +22 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE +22 -0
  4. data/README.md +29 -0
  5. data/Rakefile +21 -0
  6. data/examples/timer.rb +20 -0
  7. data/ext/foolio/extconf.rb +34 -0
  8. data/ext/foolio/foolio_ext.c +921 -0
  9. data/ext/foolio/gen.rb +50 -0
  10. data/ext/foolio/make_table.rb +12 -0
  11. data/ext/foolio/templ +243 -0
  12. data/ext/libuv/.gitignore +33 -0
  13. data/ext/libuv/.mailmap +13 -0
  14. data/ext/libuv/.travis.yml +9 -0
  15. data/ext/libuv/AUTHORS +61 -0
  16. data/ext/libuv/LICENSE +44 -0
  17. data/ext/libuv/Makefile +71 -0
  18. data/ext/libuv/README.md +90 -0
  19. data/ext/libuv/common.gypi +178 -0
  20. data/ext/libuv/gyp_uv +73 -0
  21. data/ext/libuv/include/uv-private/eio.h +403 -0
  22. data/ext/libuv/include/uv-private/ev.h +838 -0
  23. data/ext/libuv/include/uv-private/ngx-queue.h +108 -0
  24. data/ext/libuv/include/uv-private/tree.h +768 -0
  25. data/ext/libuv/include/uv-private/uv-unix.h +324 -0
  26. data/ext/libuv/include/uv-private/uv-win.h +517 -0
  27. data/ext/libuv/include/uv.h +1838 -0
  28. data/ext/libuv/src/fs-poll.c +235 -0
  29. data/ext/libuv/src/inet.c +293 -0
  30. data/ext/libuv/src/unix/async.c +148 -0
  31. data/ext/libuv/src/unix/core.c +696 -0
  32. data/ext/libuv/src/unix/cygwin.c +83 -0
  33. data/ext/libuv/src/unix/darwin.c +342 -0
  34. data/ext/libuv/src/unix/dl.c +83 -0
  35. data/ext/libuv/src/unix/eio/Changes +63 -0
  36. data/ext/libuv/src/unix/eio/LICENSE +36 -0
  37. data/ext/libuv/src/unix/eio/Makefile.am +15 -0
  38. data/ext/libuv/src/unix/eio/aclocal.m4 +8957 -0
  39. data/ext/libuv/src/unix/eio/autogen.sh +3 -0
  40. data/ext/libuv/src/unix/eio/config.h.in +86 -0
  41. data/ext/libuv/src/unix/eio/config_cygwin.h +80 -0
  42. data/ext/libuv/src/unix/eio/config_darwin.h +141 -0
  43. data/ext/libuv/src/unix/eio/config_freebsd.h +81 -0
  44. data/ext/libuv/src/unix/eio/config_linux.h +94 -0
  45. data/ext/libuv/src/unix/eio/config_netbsd.h +81 -0
  46. data/ext/libuv/src/unix/eio/config_openbsd.h +137 -0
  47. data/ext/libuv/src/unix/eio/config_sunos.h +84 -0
  48. data/ext/libuv/src/unix/eio/configure.ac +22 -0
  49. data/ext/libuv/src/unix/eio/demo.c +194 -0
  50. data/ext/libuv/src/unix/eio/ecb.h +370 -0
  51. data/ext/libuv/src/unix/eio/eio.3 +3428 -0
  52. data/ext/libuv/src/unix/eio/eio.c +2593 -0
  53. data/ext/libuv/src/unix/eio/eio.pod +969 -0
  54. data/ext/libuv/src/unix/eio/libeio.m4 +195 -0
  55. data/ext/libuv/src/unix/eio/xthread.h +164 -0
  56. data/ext/libuv/src/unix/error.c +105 -0
  57. data/ext/libuv/src/unix/ev/Changes +388 -0
  58. data/ext/libuv/src/unix/ev/LICENSE +36 -0
  59. data/ext/libuv/src/unix/ev/Makefile.am +18 -0
  60. data/ext/libuv/src/unix/ev/Makefile.in +771 -0
  61. data/ext/libuv/src/unix/ev/README +58 -0
  62. data/ext/libuv/src/unix/ev/aclocal.m4 +8957 -0
  63. data/ext/libuv/src/unix/ev/autogen.sh +6 -0
  64. data/ext/libuv/src/unix/ev/config.guess +1526 -0
  65. data/ext/libuv/src/unix/ev/config.h.in +125 -0
  66. data/ext/libuv/src/unix/ev/config.sub +1658 -0
  67. data/ext/libuv/src/unix/ev/config_cygwin.h +123 -0
  68. data/ext/libuv/src/unix/ev/config_darwin.h +122 -0
  69. data/ext/libuv/src/unix/ev/config_freebsd.h +120 -0
  70. data/ext/libuv/src/unix/ev/config_linux.h +141 -0
  71. data/ext/libuv/src/unix/ev/config_netbsd.h +120 -0
  72. data/ext/libuv/src/unix/ev/config_openbsd.h +126 -0
  73. data/ext/libuv/src/unix/ev/config_sunos.h +122 -0
  74. data/ext/libuv/src/unix/ev/configure +13037 -0
  75. data/ext/libuv/src/unix/ev/configure.ac +18 -0
  76. data/ext/libuv/src/unix/ev/depcomp +630 -0
  77. data/ext/libuv/src/unix/ev/ev++.h +816 -0
  78. data/ext/libuv/src/unix/ev/ev.3 +5311 -0
  79. data/ext/libuv/src/unix/ev/ev.c +3925 -0
  80. data/ext/libuv/src/unix/ev/ev.pod +5243 -0
  81. data/ext/libuv/src/unix/ev/ev_epoll.c +266 -0
  82. data/ext/libuv/src/unix/ev/ev_kqueue.c +235 -0
  83. data/ext/libuv/src/unix/ev/ev_poll.c +148 -0
  84. data/ext/libuv/src/unix/ev/ev_port.c +179 -0
  85. data/ext/libuv/src/unix/ev/ev_select.c +310 -0
  86. data/ext/libuv/src/unix/ev/ev_vars.h +203 -0
  87. data/ext/libuv/src/unix/ev/ev_win32.c +153 -0
  88. data/ext/libuv/src/unix/ev/ev_wrap.h +196 -0
  89. data/ext/libuv/src/unix/ev/event.c +402 -0
  90. data/ext/libuv/src/unix/ev/event.h +170 -0
  91. data/ext/libuv/src/unix/ev/install-sh +294 -0
  92. data/ext/libuv/src/unix/ev/libev.m4 +39 -0
  93. data/ext/libuv/src/unix/ev/ltmain.sh +8413 -0
  94. data/ext/libuv/src/unix/ev/missing +336 -0
  95. data/ext/libuv/src/unix/ev/mkinstalldirs +111 -0
  96. data/ext/libuv/src/unix/freebsd.c +326 -0
  97. data/ext/libuv/src/unix/fs.c +739 -0
  98. data/ext/libuv/src/unix/internal.h +188 -0
  99. data/ext/libuv/src/unix/kqueue.c +120 -0
  100. data/ext/libuv/src/unix/linux/inotify.c +239 -0
  101. data/ext/libuv/src/unix/linux/linux-core.c +557 -0
  102. data/ext/libuv/src/unix/linux/syscalls.c +388 -0
  103. data/ext/libuv/src/unix/linux/syscalls.h +124 -0
  104. data/ext/libuv/src/unix/loop-watcher.c +62 -0
  105. data/ext/libuv/src/unix/loop.c +94 -0
  106. data/ext/libuv/src/unix/netbsd.c +108 -0
  107. data/ext/libuv/src/unix/openbsd.c +295 -0
  108. data/ext/libuv/src/unix/pipe.c +259 -0
  109. data/ext/libuv/src/unix/poll.c +114 -0
  110. data/ext/libuv/src/unix/process.c +495 -0
  111. data/ext/libuv/src/unix/signal.c +269 -0
  112. data/ext/libuv/src/unix/stream.c +990 -0
  113. data/ext/libuv/src/unix/sunos.c +481 -0
  114. data/ext/libuv/src/unix/tcp.c +393 -0
  115. data/ext/libuv/src/unix/thread.c +251 -0
  116. data/ext/libuv/src/unix/timer.c +136 -0
  117. data/ext/libuv/src/unix/tty.c +145 -0
  118. data/ext/libuv/src/unix/udp.c +659 -0
  119. data/ext/libuv/src/unix/uv-eio.c +107 -0
  120. data/ext/libuv/src/unix/uv-eio.h +13 -0
  121. data/ext/libuv/src/uv-common.c +380 -0
  122. data/ext/libuv/src/uv-common.h +170 -0
  123. data/ext/libuv/src/win/async.c +100 -0
  124. data/ext/libuv/src/win/atomicops-inl.h +56 -0
  125. data/ext/libuv/src/win/core.c +278 -0
  126. data/ext/libuv/src/win/dl.c +86 -0
  127. data/ext/libuv/src/win/error.c +155 -0
  128. data/ext/libuv/src/win/fs-event.c +510 -0
  129. data/ext/libuv/src/win/fs.c +1948 -0
  130. data/ext/libuv/src/win/getaddrinfo.c +365 -0
  131. data/ext/libuv/src/win/handle-inl.h +149 -0
  132. data/ext/libuv/src/win/handle.c +154 -0
  133. data/ext/libuv/src/win/internal.h +343 -0
  134. data/ext/libuv/src/win/loop-watcher.c +122 -0
  135. data/ext/libuv/src/win/pipe.c +1672 -0
  136. data/ext/libuv/src/win/poll.c +616 -0
  137. data/ext/libuv/src/win/process-stdio.c +500 -0
  138. data/ext/libuv/src/win/process.c +1013 -0
  139. data/ext/libuv/src/win/req-inl.h +220 -0
  140. data/ext/libuv/src/win/req.c +25 -0
  141. data/ext/libuv/src/win/signal.c +57 -0
  142. data/ext/libuv/src/win/stream-inl.h +67 -0
  143. data/ext/libuv/src/win/stream.c +167 -0
  144. data/ext/libuv/src/win/tcp.c +1394 -0
  145. data/ext/libuv/src/win/thread.c +372 -0
  146. data/ext/libuv/src/win/threadpool.c +74 -0
  147. data/ext/libuv/src/win/timer.c +224 -0
  148. data/ext/libuv/src/win/tty.c +1799 -0
  149. data/ext/libuv/src/win/udp.c +716 -0
  150. data/ext/libuv/src/win/util.c +864 -0
  151. data/ext/libuv/src/win/winapi.c +132 -0
  152. data/ext/libuv/src/win/winapi.h +4452 -0
  153. data/ext/libuv/src/win/winsock.c +557 -0
  154. data/ext/libuv/src/win/winsock.h +171 -0
  155. data/ext/libuv/test/benchmark-async-pummel.c +97 -0
  156. data/ext/libuv/test/benchmark-async.c +137 -0
  157. data/ext/libuv/test/benchmark-fs-stat.c +135 -0
  158. data/ext/libuv/test/benchmark-getaddrinfo.c +94 -0
  159. data/ext/libuv/test/benchmark-list.h +127 -0
  160. data/ext/libuv/test/benchmark-loop-count.c +88 -0
  161. data/ext/libuv/test/benchmark-million-timers.c +65 -0
  162. data/ext/libuv/test/benchmark-ping-pongs.c +213 -0
  163. data/ext/libuv/test/benchmark-pound.c +324 -0
  164. data/ext/libuv/test/benchmark-pump.c +462 -0
  165. data/ext/libuv/test/benchmark-sizes.c +44 -0
  166. data/ext/libuv/test/benchmark-spawn.c +162 -0
  167. data/ext/libuv/test/benchmark-tcp-write-batch.c +140 -0
  168. data/ext/libuv/test/benchmark-thread.c +64 -0
  169. data/ext/libuv/test/benchmark-udp-packet-storm.c +247 -0
  170. data/ext/libuv/test/blackhole-server.c +118 -0
  171. data/ext/libuv/test/dns-server.c +321 -0
  172. data/ext/libuv/test/echo-server.c +378 -0
  173. data/ext/libuv/test/fixtures/empty_file +0 -0
  174. data/ext/libuv/test/fixtures/load_error.node +1 -0
  175. data/ext/libuv/test/run-benchmarks.c +64 -0
  176. data/ext/libuv/test/run-tests.c +138 -0
  177. data/ext/libuv/test/runner-unix.c +295 -0
  178. data/ext/libuv/test/runner-unix.h +36 -0
  179. data/ext/libuv/test/runner-win.c +285 -0
  180. data/ext/libuv/test/runner-win.h +42 -0
  181. data/ext/libuv/test/runner.c +355 -0
  182. data/ext/libuv/test/runner.h +159 -0
  183. data/ext/libuv/test/task.h +112 -0
  184. data/ext/libuv/test/test-async.c +118 -0
  185. data/ext/libuv/test/test-callback-order.c +76 -0
  186. data/ext/libuv/test/test-callback-stack.c +203 -0
  187. data/ext/libuv/test/test-connection-fail.c +148 -0
  188. data/ext/libuv/test/test-cwd-and-chdir.c +64 -0
  189. data/ext/libuv/test/test-delayed-accept.c +188 -0
  190. data/ext/libuv/test/test-dlerror.c +58 -0
  191. data/ext/libuv/test/test-error.c +59 -0
  192. data/ext/libuv/test/test-fail-always.c +29 -0
  193. data/ext/libuv/test/test-fs-event.c +474 -0
  194. data/ext/libuv/test/test-fs-poll.c +146 -0
  195. data/ext/libuv/test/test-fs.c +1843 -0
  196. data/ext/libuv/test/test-get-currentexe.c +63 -0
  197. data/ext/libuv/test/test-get-loadavg.c +36 -0
  198. data/ext/libuv/test/test-get-memory.c +38 -0
  199. data/ext/libuv/test/test-getaddrinfo.c +122 -0
  200. data/ext/libuv/test/test-getsockname.c +342 -0
  201. data/ext/libuv/test/test-hrtime.c +54 -0
  202. data/ext/libuv/test/test-idle.c +81 -0
  203. data/ext/libuv/test/test-ipc-send-recv.c +209 -0
  204. data/ext/libuv/test/test-ipc.c +620 -0
  205. data/ext/libuv/test/test-list.h +427 -0
  206. data/ext/libuv/test/test-loop-handles.c +336 -0
  207. data/ext/libuv/test/test-multiple-listen.c +102 -0
  208. data/ext/libuv/test/test-mutexes.c +63 -0
  209. data/ext/libuv/test/test-pass-always.c +28 -0
  210. data/ext/libuv/test/test-ping-pong.c +253 -0
  211. data/ext/libuv/test/test-pipe-bind-error.c +140 -0
  212. data/ext/libuv/test/test-pipe-connect-error.c +96 -0
  213. data/ext/libuv/test/test-platform-output.c +87 -0
  214. data/ext/libuv/test/test-poll-close.c +72 -0
  215. data/ext/libuv/test/test-poll.c +573 -0
  216. data/ext/libuv/test/test-process-title.c +49 -0
  217. data/ext/libuv/test/test-ref.c +338 -0
  218. data/ext/libuv/test/test-run-once.c +48 -0
  219. data/ext/libuv/test/test-semaphore.c +111 -0
  220. data/ext/libuv/test/test-shutdown-close.c +103 -0
  221. data/ext/libuv/test/test-shutdown-eof.c +183 -0
  222. data/ext/libuv/test/test-signal.c +162 -0
  223. data/ext/libuv/test/test-spawn.c +863 -0
  224. data/ext/libuv/test/test-stdio-over-pipes.c +246 -0
  225. data/ext/libuv/test/test-tcp-bind-error.c +191 -0
  226. data/ext/libuv/test/test-tcp-bind6-error.c +154 -0
  227. data/ext/libuv/test/test-tcp-close-while-connecting.c +80 -0
  228. data/ext/libuv/test/test-tcp-close.c +129 -0
  229. data/ext/libuv/test/test-tcp-connect-error-after-write.c +95 -0
  230. data/ext/libuv/test/test-tcp-connect-error.c +70 -0
  231. data/ext/libuv/test/test-tcp-connect-timeout.c +85 -0
  232. data/ext/libuv/test/test-tcp-connect6-error.c +68 -0
  233. data/ext/libuv/test/test-tcp-flags.c +51 -0
  234. data/ext/libuv/test/test-tcp-shutdown-after-write.c +131 -0
  235. data/ext/libuv/test/test-tcp-unexpected-read.c +113 -0
  236. data/ext/libuv/test/test-tcp-write-error.c +168 -0
  237. data/ext/libuv/test/test-tcp-write-to-half-open-connection.c +135 -0
  238. data/ext/libuv/test/test-tcp-writealot.c +170 -0
  239. data/ext/libuv/test/test-thread.c +183 -0
  240. data/ext/libuv/test/test-threadpool.c +57 -0
  241. data/ext/libuv/test/test-timer-again.c +141 -0
  242. data/ext/libuv/test/test-timer.c +152 -0
  243. data/ext/libuv/test/test-tty.c +110 -0
  244. data/ext/libuv/test/test-udp-dgram-too-big.c +86 -0
  245. data/ext/libuv/test/test-udp-ipv6.c +156 -0
  246. data/ext/libuv/test/test-udp-multicast-join.c +139 -0
  247. data/ext/libuv/test/test-udp-multicast-ttl.c +86 -0
  248. data/ext/libuv/test/test-udp-options.c +86 -0
  249. data/ext/libuv/test/test-udp-send-and-recv.c +208 -0
  250. data/ext/libuv/test/test-util.c +97 -0
  251. data/ext/libuv/test/test-walk-handles.c +77 -0
  252. data/ext/libuv/uv.gyp +375 -0
  253. data/ext/libuv/vcbuild.bat +105 -0
  254. data/foolio.gemspec +18 -0
  255. data/lib/foolio.rb +9 -0
  256. data/lib/foolio/handle.rb +27 -0
  257. data/lib/foolio/listener.rb +26 -0
  258. data/lib/foolio/loop.rb +79 -0
  259. data/lib/foolio/stream.rb +109 -0
  260. data/lib/foolio/version.rb +3 -0
  261. metadata +309 -0
@@ -0,0 +1,103 @@
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
+
22
+ /*
23
+ * These tests verify that the uv_shutdown callback is always made, even when
24
+ * it is immediately followed by an uv_close call.
25
+ */
26
+
27
+ #include "uv.h"
28
+ #include "task.h"
29
+
30
+
31
+ static uv_shutdown_t shutdown_req;
32
+ static uv_connect_t connect_req;
33
+
34
+ static int connect_cb_called = 0;
35
+ static int shutdown_cb_called = 0;
36
+ static int close_cb_called = 0;
37
+
38
+
39
+ static void shutdown_cb(uv_shutdown_t* req, int status) {
40
+ int err = uv_last_error(uv_default_loop()).code;
41
+ ASSERT(req == &shutdown_req);
42
+ ASSERT(status == 0 || (status == -1 && err == UV_ECANCELED));
43
+ shutdown_cb_called++;
44
+ }
45
+
46
+
47
+ static void close_cb(uv_handle_t* handle) {
48
+ close_cb_called++;
49
+ }
50
+
51
+
52
+ static void connect_cb(uv_connect_t* req, int status) {
53
+ int r;
54
+
55
+ ASSERT(req == &connect_req);
56
+ ASSERT(status == 0);
57
+
58
+ r = uv_shutdown(&shutdown_req, req->handle, shutdown_cb);
59
+ ASSERT(r == 0);
60
+ ASSERT(!uv_is_closing((uv_handle_t*) req->handle));
61
+ uv_close((uv_handle_t*) req->handle, close_cb);
62
+ ASSERT(uv_is_closing((uv_handle_t*) req->handle));
63
+
64
+ connect_cb_called++;
65
+ }
66
+
67
+
68
+ TEST_IMPL(shutdown_close_tcp) {
69
+ struct sockaddr_in addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
70
+ uv_tcp_t h;
71
+ int r;
72
+
73
+ r = uv_tcp_init(uv_default_loop(), &h);
74
+ ASSERT(r == 0);
75
+ r = uv_tcp_connect(&connect_req, &h, addr, connect_cb);
76
+ ASSERT(r == 0);
77
+ r = uv_run(uv_default_loop());
78
+ ASSERT(r == 0);
79
+
80
+ ASSERT(connect_cb_called == 1);
81
+ ASSERT(shutdown_cb_called == 1);
82
+ ASSERT(close_cb_called == 1);
83
+
84
+ return 0;
85
+ }
86
+
87
+
88
+ TEST_IMPL(shutdown_close_pipe) {
89
+ uv_pipe_t h;
90
+ int r;
91
+
92
+ r = uv_pipe_init(uv_default_loop(), &h, 0);
93
+ ASSERT(r == 0);
94
+ uv_pipe_connect(&connect_req, &h, TEST_PIPENAME, connect_cb);
95
+ r = uv_run(uv_default_loop());
96
+ ASSERT(r == 0);
97
+
98
+ ASSERT(connect_cb_called == 1);
99
+ ASSERT(shutdown_cb_called == 1);
100
+ ASSERT(close_cb_called == 1);
101
+
102
+ return 0;
103
+ }
@@ -0,0 +1,183 @@
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
+
22
+ #include "uv.h"
23
+ #include "task.h"
24
+ #include <stdio.h>
25
+ #include <stdlib.h>
26
+
27
+ static uv_timer_t timer;
28
+ static uv_tcp_t tcp;
29
+ static uv_connect_t connect_req;
30
+ static uv_write_t write_req;
31
+ static uv_shutdown_t shutdown_req;
32
+ static uv_buf_t qbuf;
33
+ static int got_q;
34
+ static int got_eof;
35
+ static int called_connect_cb;
36
+ static int called_shutdown_cb;
37
+ static int called_tcp_close_cb;
38
+ static int called_timer_close_cb;
39
+ static int called_timer_cb;
40
+
41
+
42
+ static uv_buf_t alloc_cb(uv_handle_t* handle, size_t size) {
43
+ uv_buf_t buf;
44
+ buf.base = (char*)malloc(size);
45
+ buf.len = size;
46
+ return buf;
47
+ }
48
+
49
+
50
+ static void read_cb(uv_stream_t* t, ssize_t nread, uv_buf_t buf) {
51
+ uv_err_t err = uv_last_error(uv_default_loop());
52
+
53
+ ASSERT((uv_tcp_t*)t == &tcp);
54
+
55
+ if (nread == 0) {
56
+ ASSERT(err.code == UV_EAGAIN);
57
+ free(buf.base);
58
+ return;
59
+ }
60
+
61
+ if (!got_q) {
62
+ ASSERT(nread == 1);
63
+ ASSERT(!got_eof);
64
+ ASSERT(buf.base[0] == 'Q');
65
+ free(buf.base);
66
+ got_q = 1;
67
+ puts("got Q");
68
+ } else {
69
+ ASSERT(err.code == UV_EOF);
70
+ if (buf.base) {
71
+ free(buf.base);
72
+ }
73
+ got_eof = 1;
74
+ puts("got EOF");
75
+ }
76
+ }
77
+
78
+
79
+ static void shutdown_cb(uv_shutdown_t *req, int status) {
80
+ ASSERT(req == &shutdown_req);
81
+
82
+ ASSERT(called_connect_cb == 1);
83
+ ASSERT(!got_eof);
84
+ ASSERT(called_tcp_close_cb == 0);
85
+ ASSERT(called_timer_close_cb == 0);
86
+ ASSERT(called_timer_cb == 0);
87
+
88
+ called_shutdown_cb++;
89
+ }
90
+
91
+
92
+ static void connect_cb(uv_connect_t *req, int status) {
93
+ ASSERT(status == 0);
94
+ ASSERT(req == &connect_req);
95
+
96
+ /* Start reading from our connection so we can receive the EOF. */
97
+ uv_read_start((uv_stream_t*)&tcp, alloc_cb, read_cb);
98
+
99
+ /*
100
+ * Write the letter 'Q' to gracefully kill the echo-server. This will not
101
+ * effect our connection.
102
+ */
103
+ uv_write(&write_req, (uv_stream_t*) &tcp, &qbuf, 1, NULL);
104
+
105
+ /* Shutdown our end of the connection. */
106
+ uv_shutdown(&shutdown_req, (uv_stream_t*) &tcp, shutdown_cb);
107
+
108
+ called_connect_cb++;
109
+ ASSERT(called_shutdown_cb == 0);
110
+ }
111
+
112
+
113
+ void tcp_close_cb(uv_handle_t* handle) {
114
+ ASSERT(handle == (uv_handle_t*) &tcp);
115
+
116
+ ASSERT(called_connect_cb == 1);
117
+ ASSERT(got_q);
118
+ ASSERT(got_eof);
119
+ ASSERT(called_timer_cb == 1);
120
+
121
+ called_tcp_close_cb++;
122
+ }
123
+
124
+
125
+ void timer_close_cb(uv_handle_t* handle) {
126
+ ASSERT(handle == (uv_handle_t*) &timer);
127
+ called_timer_close_cb++;
128
+ }
129
+
130
+
131
+ void timer_cb(uv_timer_t* handle, int status) {
132
+ ASSERT(handle == &timer);
133
+ uv_close((uv_handle_t*) handle, timer_close_cb);
134
+
135
+ /*
136
+ * The most important assert of the test: we have not received
137
+ * tcp_close_cb yet.
138
+ */
139
+ ASSERT(called_tcp_close_cb == 0);
140
+ uv_close((uv_handle_t*) &tcp, tcp_close_cb);
141
+
142
+ called_timer_cb++;
143
+ }
144
+
145
+
146
+ /*
147
+ * This test has a client which connects to the echo_server and immediately
148
+ * issues a shutdown. The echo-server, in response, will also shutdown their
149
+ * connection. We check, with a timer, that libuv is not automatically
150
+ * calling uv_close when the client receives the EOF from echo-server.
151
+ */
152
+ TEST_IMPL(shutdown_eof) {
153
+ struct sockaddr_in server_addr;
154
+ int r;
155
+
156
+ qbuf.base = "Q";
157
+ qbuf.len = 1;
158
+
159
+ r = uv_timer_init(uv_default_loop(), &timer);
160
+ ASSERT(r == 0);
161
+
162
+ uv_timer_start(&timer, timer_cb, 100, 0);
163
+
164
+ server_addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
165
+ r = uv_tcp_init(uv_default_loop(), &tcp);
166
+ ASSERT(!r);
167
+
168
+ r = uv_tcp_connect(&connect_req, &tcp, server_addr, connect_cb);
169
+ ASSERT(!r);
170
+
171
+ uv_run(uv_default_loop());
172
+
173
+ ASSERT(called_connect_cb == 1);
174
+ ASSERT(called_shutdown_cb == 1);
175
+ ASSERT(got_eof);
176
+ ASSERT(got_q);
177
+ ASSERT(called_tcp_close_cb == 1);
178
+ ASSERT(called_timer_close_cb == 1);
179
+ ASSERT(called_timer_cb == 1);
180
+
181
+ return 0;
182
+ }
183
+
@@ -0,0 +1,162 @@
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
+
22
+ #include "uv.h"
23
+ #include "task.h"
24
+
25
+ #ifdef _WIN32
26
+
27
+ TEST_IMPL(we_get_signal) {
28
+ return 0;
29
+ }
30
+
31
+
32
+ TEST_IMPL(we_get_signals) {
33
+ return 0;
34
+ }
35
+
36
+ #else /* !_WIN32 */
37
+
38
+ #include <stdio.h>
39
+ #include <stdlib.h>
40
+ #include <stdarg.h>
41
+ #include <string.h>
42
+ #include <errno.h>
43
+
44
+ /* This test does not pretend to be cross-platform. */
45
+ #include <pthread.h>
46
+ #include <signal.h>
47
+ #include <unistd.h>
48
+
49
+ #define NSIGNALS 10
50
+
51
+ struct timer_ctx {
52
+ unsigned int ncalls;
53
+ uv_timer_t handle;
54
+ int signum;
55
+ };
56
+
57
+ struct signal_ctx {
58
+ enum { CLOSE, STOP } stop_or_close;
59
+ unsigned int ncalls;
60
+ uv_signal_t handle;
61
+ int signum;
62
+ };
63
+
64
+
65
+ static void signal_cb(uv_signal_t* handle, int signum) {
66
+ struct signal_ctx* ctx = container_of(handle, struct signal_ctx, handle);
67
+ ASSERT(signum == ctx->signum);
68
+
69
+ if (++ctx->ncalls == NSIGNALS) {
70
+ if (ctx->stop_or_close == STOP)
71
+ uv_signal_stop(handle);
72
+ else if (ctx->stop_or_close == CLOSE)
73
+ uv_close((uv_handle_t*)handle, NULL);
74
+ else
75
+ ASSERT(0);
76
+ }
77
+ }
78
+
79
+
80
+ static void timer_cb(uv_timer_t* handle, int status) {
81
+ struct timer_ctx* ctx = container_of(handle, struct timer_ctx, handle);
82
+
83
+ raise(ctx->signum);
84
+
85
+ if (++ctx->ncalls == NSIGNALS)
86
+ uv_close((uv_handle_t*)handle, NULL);
87
+ }
88
+
89
+
90
+ static void start_watcher(uv_loop_t* loop, int signum, struct signal_ctx* ctx) {
91
+ ctx->ncalls = 0;
92
+ ctx->signum = signum;
93
+ ctx->stop_or_close = CLOSE;
94
+ ASSERT(0 == uv_signal_init(loop, &ctx->handle));
95
+ ASSERT(0 == uv_signal_start(&ctx->handle, signal_cb, signum));
96
+ }
97
+
98
+
99
+ static void start_timer(uv_loop_t* loop, int signum, struct timer_ctx* ctx) {
100
+ ctx->ncalls = 0;
101
+ ctx->signum = signum;
102
+ ASSERT(0 == uv_timer_init(loop, &ctx->handle));
103
+ ASSERT(0 == uv_timer_start(&ctx->handle, timer_cb, 5, 5));
104
+ }
105
+
106
+
107
+ TEST_IMPL(we_get_signal) {
108
+ struct signal_ctx sc;
109
+ struct timer_ctx tc;
110
+ uv_loop_t* loop;
111
+
112
+ loop = uv_default_loop();
113
+ start_timer(loop, SIGCHLD, &tc);
114
+ start_watcher(loop, SIGCHLD, &sc);
115
+ sc.stop_or_close = STOP; /* stop, don't close the signal handle */
116
+ ASSERT(0 == uv_run(loop));
117
+ ASSERT(tc.ncalls == NSIGNALS);
118
+ ASSERT(sc.ncalls == NSIGNALS);
119
+
120
+ start_timer(loop, SIGCHLD, &tc);
121
+ ASSERT(0 == uv_run(loop));
122
+ ASSERT(tc.ncalls == NSIGNALS);
123
+ ASSERT(sc.ncalls == NSIGNALS);
124
+
125
+ sc.ncalls = 0;
126
+ sc.stop_or_close = CLOSE; /* now close it when it's done */
127
+ uv_signal_start(&sc.handle, signal_cb, SIGCHLD);
128
+
129
+ start_timer(loop, SIGCHLD, &tc);
130
+ ASSERT(0 == uv_run(loop));
131
+ ASSERT(tc.ncalls == NSIGNALS);
132
+ ASSERT(sc.ncalls == NSIGNALS);
133
+
134
+ return 0;
135
+ }
136
+
137
+
138
+ TEST_IMPL(we_get_signals) {
139
+ struct signal_ctx sc[4];
140
+ struct timer_ctx tc[2];
141
+ uv_loop_t* loop;
142
+ unsigned int i;
143
+
144
+ loop = uv_default_loop();
145
+ start_watcher(loop, SIGUSR1, sc + 0);
146
+ start_watcher(loop, SIGUSR1, sc + 1);
147
+ start_watcher(loop, SIGUSR2, sc + 2);
148
+ start_watcher(loop, SIGUSR2, sc + 3);
149
+ start_timer(loop, SIGUSR1, tc + 0);
150
+ start_timer(loop, SIGUSR2, tc + 1);
151
+ ASSERT(0 == uv_run(loop));
152
+
153
+ for (i = 0; i < ARRAY_SIZE(sc); i++)
154
+ ASSERT(sc[i].ncalls == NSIGNALS);
155
+
156
+ for (i = 0; i < ARRAY_SIZE(tc); i++)
157
+ ASSERT(tc[i].ncalls == NSIGNALS);
158
+
159
+ return 0;
160
+ }
161
+
162
+ #endif /* _WIN32 */
@@ -0,0 +1,863 @@
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
+
22
+ #include "uv.h"
23
+ #include "task.h"
24
+ #include <fcntl.h>
25
+ #include <stdio.h>
26
+ #include <stdlib.h>
27
+ #include <string.h>
28
+
29
+ #ifndef _WIN32
30
+ #include <unistd.h>
31
+ #endif
32
+
33
+
34
+ static int close_cb_called;
35
+ static int exit_cb_called;
36
+ static uv_process_t process;
37
+ static uv_timer_t timer;
38
+ static uv_process_options_t options;
39
+ static char exepath[1024];
40
+ static size_t exepath_size = 1024;
41
+ static char* args[3];
42
+ static int no_term_signal;
43
+
44
+ #define OUTPUT_SIZE 1024
45
+ static char output[OUTPUT_SIZE];
46
+ static int output_used;
47
+
48
+
49
+ static void close_cb(uv_handle_t* handle) {
50
+ printf("close_cb\n");
51
+ close_cb_called++;
52
+ }
53
+
54
+
55
+ static void exit_cb(uv_process_t* process, int exit_status, int term_signal) {
56
+ printf("exit_cb\n");
57
+ exit_cb_called++;
58
+ ASSERT(exit_status == 1);
59
+ ASSERT(term_signal == 0);
60
+ uv_close((uv_handle_t*)process, close_cb);
61
+ }
62
+
63
+
64
+ static void exit_cb_failure_expected(uv_process_t* process, int exit_status,
65
+ int term_signal) {
66
+ printf("exit_cb\n");
67
+ exit_cb_called++;
68
+ ASSERT(exit_status == -1);
69
+ ASSERT(term_signal == 0);
70
+ uv_close((uv_handle_t*)process, close_cb);
71
+ }
72
+
73
+
74
+ static void kill_cb(uv_process_t* process, int exit_status, int term_signal) {
75
+ uv_err_t err;
76
+
77
+ printf("exit_cb\n");
78
+ exit_cb_called++;
79
+ #ifdef _WIN32
80
+ ASSERT(exit_status == 1);
81
+ #else
82
+ ASSERT(exit_status == 0);
83
+ #endif
84
+ ASSERT(no_term_signal || term_signal == 15);
85
+ uv_close((uv_handle_t*)process, close_cb);
86
+
87
+ /*
88
+ * Sending signum == 0 should check if the
89
+ * child process is still alive, not kill it.
90
+ * This process should be dead.
91
+ */
92
+ err = uv_kill(process->pid, 0);
93
+ ASSERT(err.code == UV_ESRCH);
94
+ }
95
+
96
+ static void detach_failure_cb(uv_process_t* process, int exit_status, int term_signal) {
97
+ printf("detach_cb\n");
98
+ exit_cb_called++;
99
+ }
100
+
101
+ static uv_buf_t on_alloc(uv_handle_t* handle, size_t suggested_size) {
102
+ uv_buf_t buf;
103
+ buf.base = output + output_used;
104
+ buf.len = OUTPUT_SIZE - output_used;
105
+ return buf;
106
+ }
107
+
108
+
109
+ void on_read(uv_stream_t* tcp, ssize_t nread, uv_buf_t buf) {
110
+ uv_err_t err = uv_last_error(uv_default_loop());
111
+
112
+ if (nread > 0) {
113
+ output_used += nread;
114
+ } else if (nread < 0) {
115
+ ASSERT(err.code == UV_EOF);
116
+ uv_close((uv_handle_t*)tcp, close_cb);
117
+ }
118
+ }
119
+
120
+
121
+ void write_cb(uv_write_t* req, int status) {
122
+ ASSERT(status == 0);
123
+ uv_close((uv_handle_t*)req->handle, close_cb);
124
+ }
125
+
126
+
127
+ static void init_process_options(char* test, uv_exit_cb exit_cb) {
128
+ /* Note spawn_helper1 defined in test/run-tests.c */
129
+ int r = uv_exepath(exepath, &exepath_size);
130
+ ASSERT(r == 0);
131
+ exepath[exepath_size] = '\0';
132
+ args[0] = exepath;
133
+ args[1] = test;
134
+ args[2] = NULL;
135
+ options.file = exepath;
136
+ options.args = args;
137
+ options.exit_cb = exit_cb;
138
+ options.flags = 0;
139
+ }
140
+
141
+
142
+ static void timer_cb(uv_timer_t* handle, int status) {
143
+ uv_process_kill(&process, /* SIGTERM */ 15);
144
+ uv_close((uv_handle_t*)handle, close_cb);
145
+ }
146
+
147
+
148
+ TEST_IMPL(spawn_fails) {
149
+ init_process_options("", exit_cb_failure_expected);
150
+ options.file = options.args[0] = "program-that-had-better-not-exist";
151
+ ASSERT(0 == uv_spawn(uv_default_loop(), &process, options));
152
+ ASSERT(0 != uv_is_active((uv_handle_t*)&process));
153
+ ASSERT(0 == uv_run(uv_default_loop()));
154
+ ASSERT(uv_last_error(uv_default_loop()).code == UV_ENOENT);
155
+ return 0;
156
+ }
157
+
158
+
159
+ TEST_IMPL(spawn_exit_code) {
160
+ int r;
161
+
162
+ init_process_options("spawn_helper1", exit_cb);
163
+
164
+ r = uv_spawn(uv_default_loop(), &process, options);
165
+ ASSERT(r == 0);
166
+
167
+ r = uv_run(uv_default_loop());
168
+ ASSERT(r == 0);
169
+
170
+ ASSERT(exit_cb_called == 1);
171
+ ASSERT(close_cb_called == 1);
172
+
173
+ return 0;
174
+ }
175
+
176
+
177
+ TEST_IMPL(spawn_stdout) {
178
+ int r;
179
+ uv_pipe_t out;
180
+ uv_stdio_container_t stdio[2];
181
+
182
+ init_process_options("spawn_helper2", exit_cb);
183
+
184
+ uv_pipe_init(uv_default_loop(), &out, 0);
185
+ options.stdio = stdio;
186
+ options.stdio[0].flags = UV_IGNORE;
187
+ options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
188
+ options.stdio[1].data.stream = (uv_stream_t*)&out;
189
+ options.stdio_count = 2;
190
+
191
+ r = uv_spawn(uv_default_loop(), &process, options);
192
+ ASSERT(r == 0);
193
+
194
+ r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read);
195
+ ASSERT(r == 0);
196
+
197
+ r = uv_run(uv_default_loop());
198
+ ASSERT(r == 0);
199
+
200
+ ASSERT(exit_cb_called == 1);
201
+ ASSERT(close_cb_called == 2); /* Once for process once for the pipe. */
202
+ printf("output is: %s", output);
203
+ ASSERT(strcmp("hello world\n", output) == 0);
204
+
205
+ return 0;
206
+ }
207
+
208
+
209
+ TEST_IMPL(spawn_stdout_to_file) {
210
+ int r;
211
+ uv_file file;
212
+ uv_fs_t fs_req;
213
+ uv_stdio_container_t stdio[2];
214
+
215
+ /* Setup. */
216
+ unlink("stdout_file");
217
+
218
+ init_process_options("spawn_helper2", exit_cb);
219
+
220
+ r = uv_fs_open(uv_default_loop(), &fs_req, "stdout_file", O_CREAT | O_RDWR,
221
+ S_IREAD | S_IWRITE, NULL);
222
+ ASSERT(r != -1);
223
+ uv_fs_req_cleanup(&fs_req);
224
+
225
+ file = r;
226
+
227
+ options.stdio = stdio;
228
+ options.stdio[0].flags = UV_IGNORE;
229
+ options.stdio[1].flags = UV_INHERIT_FD;
230
+ options.stdio[1].data.fd = file;
231
+ options.stdio_count = 2;
232
+
233
+ r = uv_spawn(uv_default_loop(), &process, options);
234
+ ASSERT(r == 0);
235
+
236
+ r = uv_run(uv_default_loop());
237
+ ASSERT(r == 0);
238
+
239
+ ASSERT(exit_cb_called == 1);
240
+ ASSERT(close_cb_called == 1);
241
+
242
+ r = uv_fs_read(uv_default_loop(), &fs_req, file, output, sizeof(output),
243
+ 0, NULL);
244
+ ASSERT(r == 12);
245
+ uv_fs_req_cleanup(&fs_req);
246
+
247
+ r = uv_fs_close(uv_default_loop(), &fs_req, file, NULL);
248
+ ASSERT(r == 0);
249
+ uv_fs_req_cleanup(&fs_req);
250
+
251
+ printf("output is: %s", output);
252
+ ASSERT(strcmp("hello world\n", output) == 0);
253
+
254
+ /* Cleanup. */
255
+ unlink("stdout_file");
256
+
257
+ return 0;
258
+ }
259
+
260
+
261
+ TEST_IMPL(spawn_stdin) {
262
+ int r;
263
+ uv_pipe_t out;
264
+ uv_pipe_t in;
265
+ uv_write_t write_req;
266
+ uv_buf_t buf;
267
+ uv_stdio_container_t stdio[2];
268
+ char buffer[] = "hello-from-spawn_stdin";
269
+
270
+ init_process_options("spawn_helper3", exit_cb);
271
+
272
+ uv_pipe_init(uv_default_loop(), &out, 0);
273
+ uv_pipe_init(uv_default_loop(), &in, 0);
274
+ options.stdio = stdio;
275
+ options.stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE;
276
+ options.stdio[0].data.stream = (uv_stream_t*)&in;
277
+ options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
278
+ options.stdio[1].data.stream = (uv_stream_t*)&out;
279
+ options.stdio_count = 2;
280
+
281
+ r = uv_spawn(uv_default_loop(), &process, options);
282
+ ASSERT(r == 0);
283
+
284
+ buf.base = buffer;
285
+ buf.len = sizeof(buffer);
286
+ r = uv_write(&write_req, (uv_stream_t*)&in, &buf, 1, write_cb);
287
+ ASSERT(r == 0);
288
+
289
+ r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read);
290
+ ASSERT(r == 0);
291
+
292
+ r = uv_run(uv_default_loop());
293
+ ASSERT(r == 0);
294
+
295
+ ASSERT(exit_cb_called == 1);
296
+ ASSERT(close_cb_called == 3); /* Once for process twice for the pipe. */
297
+ ASSERT(strcmp(buffer, output) == 0);
298
+
299
+ return 0;
300
+ }
301
+
302
+
303
+ TEST_IMPL(spawn_stdio_greater_than_3) {
304
+ int r;
305
+ uv_pipe_t pipe;
306
+ uv_stdio_container_t stdio[4];
307
+
308
+ init_process_options("spawn_helper5", exit_cb);
309
+
310
+ uv_pipe_init(uv_default_loop(), &pipe, 0);
311
+ options.stdio = stdio;
312
+ options.stdio[0].flags = UV_IGNORE;
313
+ options.stdio[1].flags = UV_IGNORE;
314
+ options.stdio[2].flags = UV_IGNORE;
315
+ options.stdio[3].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
316
+ options.stdio[3].data.stream = (uv_stream_t*)&pipe;
317
+ options.stdio_count = 4;
318
+
319
+ r = uv_spawn(uv_default_loop(), &process, options);
320
+ ASSERT(r == 0);
321
+
322
+ r = uv_read_start((uv_stream_t*) &pipe, on_alloc, on_read);
323
+ ASSERT(r == 0);
324
+
325
+ r = uv_run(uv_default_loop());
326
+ ASSERT(r == 0);
327
+
328
+ ASSERT(exit_cb_called == 1);
329
+ ASSERT(close_cb_called == 2); /* Once for process once for the pipe. */
330
+ printf("output from stdio[3] is: %s", output);
331
+ ASSERT(strcmp("fourth stdio!\n", output) == 0);
332
+
333
+ return 0;
334
+ }
335
+
336
+
337
+ TEST_IMPL(spawn_ignored_stdio) {
338
+ int r;
339
+
340
+ init_process_options("spawn_helper6", exit_cb);
341
+
342
+ options.stdio = NULL;
343
+ options.stdio_count = 0;
344
+
345
+ r = uv_spawn(uv_default_loop(), &process, options);
346
+ ASSERT(r == 0);
347
+
348
+ r = uv_run(uv_default_loop());
349
+ ASSERT(r == 0);
350
+
351
+ ASSERT(exit_cb_called == 1);
352
+ ASSERT(close_cb_called == 1);
353
+
354
+ return 0;
355
+ }
356
+
357
+
358
+ TEST_IMPL(spawn_and_kill) {
359
+ int r;
360
+
361
+ init_process_options("spawn_helper4", kill_cb);
362
+
363
+ r = uv_spawn(uv_default_loop(), &process, options);
364
+ ASSERT(r == 0);
365
+
366
+ r = uv_timer_init(uv_default_loop(), &timer);
367
+ ASSERT(r == 0);
368
+
369
+ r = uv_timer_start(&timer, timer_cb, 500, 0);
370
+ ASSERT(r == 0);
371
+
372
+ r = uv_run(uv_default_loop());
373
+ ASSERT(r == 0);
374
+
375
+ ASSERT(exit_cb_called == 1);
376
+ ASSERT(close_cb_called == 2); /* Once for process and once for timer. */
377
+
378
+ return 0;
379
+ }
380
+
381
+ TEST_IMPL(spawn_detached) {
382
+ int r;
383
+ uv_err_t err;
384
+
385
+ init_process_options("spawn_helper4", detach_failure_cb);
386
+
387
+ options.flags |= UV_PROCESS_DETACHED;
388
+
389
+ r = uv_spawn(uv_default_loop(), &process, options);
390
+ ASSERT(r == 0);
391
+
392
+ uv_unref((uv_handle_t*)&process);
393
+
394
+ r = uv_run(uv_default_loop());
395
+ ASSERT(r == 0);
396
+
397
+ ASSERT(exit_cb_called == 0);
398
+
399
+ err = uv_kill(process.pid, 0);
400
+ ASSERT(err.code == 0);
401
+
402
+ err = uv_kill(process.pid, 15);
403
+ ASSERT(err.code == 0);
404
+
405
+ return 0;
406
+ }
407
+
408
+ TEST_IMPL(spawn_and_kill_with_std) {
409
+ int r;
410
+ uv_pipe_t in, out, err;
411
+ uv_write_t write;
412
+ char message[] = "Nancy's joining me because the message this evening is "
413
+ "not my message but ours.";
414
+ uv_buf_t buf;
415
+ uv_stdio_container_t stdio[3];
416
+
417
+ init_process_options("spawn_helper4", kill_cb);
418
+
419
+ r = uv_pipe_init(uv_default_loop(), &in, 0);
420
+ ASSERT(r == 0);
421
+
422
+ r = uv_pipe_init(uv_default_loop(), &out, 0);
423
+ ASSERT(r == 0);
424
+
425
+ r = uv_pipe_init(uv_default_loop(), &err, 0);
426
+ ASSERT(r == 0);
427
+
428
+ options.stdio = stdio;
429
+ options.stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE;
430
+ options.stdio[0].data.stream = (uv_stream_t*)&in;
431
+ options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
432
+ options.stdio[1].data.stream = (uv_stream_t*)&out;
433
+ options.stdio[2].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
434
+ options.stdio[2].data.stream = (uv_stream_t*)&err;
435
+ options.stdio_count = 3;
436
+
437
+ r = uv_spawn(uv_default_loop(), &process, options);
438
+ ASSERT(r == 0);
439
+
440
+ buf = uv_buf_init(message, sizeof message);
441
+ r = uv_write(&write, (uv_stream_t*) &in, &buf, 1, write_cb);
442
+ ASSERT(r == 0);
443
+
444
+ r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read);
445
+ ASSERT(r == 0);
446
+
447
+ r = uv_read_start((uv_stream_t*) &err, on_alloc, on_read);
448
+ ASSERT(r == 0);
449
+
450
+ r = uv_timer_init(uv_default_loop(), &timer);
451
+ ASSERT(r == 0);
452
+
453
+ r = uv_timer_start(&timer, timer_cb, 500, 0);
454
+ ASSERT(r == 0);
455
+
456
+ r = uv_run(uv_default_loop());
457
+ ASSERT(r == 0);
458
+
459
+ ASSERT(exit_cb_called == 1);
460
+ ASSERT(close_cb_called == 5); /* process x 1, timer x 1, stdio x 3. */
461
+
462
+ return 0;
463
+ }
464
+
465
+
466
+ TEST_IMPL(spawn_and_ping) {
467
+ uv_write_t write_req;
468
+ uv_pipe_t in, out;
469
+ uv_buf_t buf;
470
+ uv_stdio_container_t stdio[2];
471
+ int r;
472
+
473
+ init_process_options("spawn_helper3", exit_cb);
474
+ buf = uv_buf_init("TEST", 4);
475
+
476
+ uv_pipe_init(uv_default_loop(), &out, 0);
477
+ uv_pipe_init(uv_default_loop(), &in, 0);
478
+ options.stdio = stdio;
479
+ options.stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE;
480
+ options.stdio[0].data.stream = (uv_stream_t*)&in;
481
+ options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
482
+ options.stdio[1].data.stream = (uv_stream_t*)&out;
483
+ options.stdio_count = 2;
484
+
485
+ r = uv_spawn(uv_default_loop(), &process, options);
486
+ ASSERT(r == 0);
487
+
488
+ /* Sending signum == 0 should check if the
489
+ * child process is still alive, not kill it.
490
+ */
491
+ r = uv_process_kill(&process, 0);
492
+ ASSERT(r == 0);
493
+
494
+ r = uv_write(&write_req, (uv_stream_t*)&in, &buf, 1, write_cb);
495
+ ASSERT(r == 0);
496
+
497
+ r = uv_read_start((uv_stream_t*)&out, on_alloc, on_read);
498
+ ASSERT(r == 0);
499
+
500
+ ASSERT(exit_cb_called == 0);
501
+
502
+ r = uv_run(uv_default_loop());
503
+ ASSERT(r == 0);
504
+
505
+ ASSERT(exit_cb_called == 1);
506
+ ASSERT(strcmp(output, "TEST") == 0);
507
+
508
+ return 0;
509
+ }
510
+
511
+
512
+ TEST_IMPL(kill) {
513
+ int r;
514
+ uv_err_t err;
515
+
516
+ #ifdef _WIN32
517
+ no_term_signal = 1;
518
+ #endif
519
+
520
+ init_process_options("spawn_helper4", kill_cb);
521
+
522
+ r = uv_spawn(uv_default_loop(), &process, options);
523
+ ASSERT(r == 0);
524
+
525
+ /* Sending signum == 0 should check if the
526
+ * child process is still alive, not kill it.
527
+ */
528
+ err = uv_kill(process.pid, 0);
529
+ ASSERT(err.code == UV_OK);
530
+
531
+ /* Kill the process. */
532
+ err = uv_kill(process.pid, /* SIGTERM */ 15);
533
+ ASSERT(err.code == UV_OK);
534
+
535
+ r = uv_run(uv_default_loop());
536
+ ASSERT(r == 0);
537
+
538
+ ASSERT(exit_cb_called == 1);
539
+ ASSERT(close_cb_called == 1);
540
+
541
+ return 0;
542
+ }
543
+
544
+
545
+ #ifdef _WIN32
546
+ TEST_IMPL(spawn_detect_pipe_name_collisions_on_windows) {
547
+ int r;
548
+ uv_pipe_t out;
549
+ char name[64];
550
+ HANDLE pipe_handle;
551
+ uv_stdio_container_t stdio[2];
552
+
553
+ init_process_options("spawn_helper2", exit_cb);
554
+
555
+ uv_pipe_init(uv_default_loop(), &out, 0);
556
+ options.stdio = stdio;
557
+ options.stdio[0].flags = UV_IGNORE;
558
+ options.stdio[1].flags = UV_CREATE_PIPE | UV_WRITABLE_PIPE;
559
+ options.stdio[1].data.stream = (uv_stream_t*)&out;
560
+ options.stdio_count = 2;
561
+
562
+ /* Create a pipe that'll cause a collision. */
563
+ _snprintf(name, sizeof(name), "\\\\.\\pipe\\uv\\%p-%d", &out, GetCurrentProcessId());
564
+ pipe_handle = CreateNamedPipeA(name,
565
+ PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED,
566
+ PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
567
+ 10,
568
+ 65536,
569
+ 65536,
570
+ 0,
571
+ NULL);
572
+ ASSERT(pipe_handle != INVALID_HANDLE_VALUE);
573
+
574
+ r = uv_spawn(uv_default_loop(), &process, options);
575
+ ASSERT(r == 0);
576
+
577
+ r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read);
578
+ ASSERT(r == 0);
579
+
580
+ r = uv_run(uv_default_loop());
581
+ ASSERT(r == 0);
582
+
583
+ ASSERT(exit_cb_called == 1);
584
+ ASSERT(close_cb_called == 2); /* Once for process once for the pipe. */
585
+ printf("output is: %s", output);
586
+ ASSERT(strcmp("hello world\n", output) == 0);
587
+
588
+ return 0;
589
+ }
590
+
591
+
592
+ uv_err_t make_program_args(char** args, int verbatim_arguments, WCHAR** dst_ptr);
593
+ WCHAR* quote_cmd_arg(const WCHAR *source, WCHAR *target);
594
+
595
+ TEST_IMPL(argument_escaping) {
596
+ const WCHAR* test_str[] = {
597
+ L"HelloWorld",
598
+ L"Hello World",
599
+ L"Hello\"World",
600
+ L"Hello World\\",
601
+ L"Hello\\\"World",
602
+ L"Hello\\World",
603
+ L"Hello\\\\World",
604
+ L"Hello World\\",
605
+ L"c:\\path\\to\\node.exe --eval \"require('c:\\\\path\\\\to\\\\test.js')\""
606
+ };
607
+ const int count = sizeof(test_str) / sizeof(*test_str);
608
+ WCHAR** test_output;
609
+ WCHAR* command_line;
610
+ WCHAR** cracked;
611
+ size_t total_size = 0;
612
+ int i;
613
+ int num_args;
614
+ uv_err_t result;
615
+
616
+ char* verbatim[] = {
617
+ "cmd.exe",
618
+ "/c",
619
+ "c:\\path\\to\\node.exe --eval \"require('c:\\\\path\\\\to\\\\test.js')\"",
620
+ NULL
621
+ };
622
+ WCHAR* verbatim_output;
623
+ WCHAR* non_verbatim_output;
624
+
625
+ test_output = calloc(count, sizeof(WCHAR*));
626
+ for (i = 0; i < count; ++i) {
627
+ test_output[i] = calloc(2 * (wcslen(test_str[i]) + 2), sizeof(WCHAR));
628
+ quote_cmd_arg(test_str[i], test_output[i]);
629
+ wprintf(L"input : %s\n", test_str[i]);
630
+ wprintf(L"output: %s\n", test_output[i]);
631
+ total_size += wcslen(test_output[i]) + 1;
632
+ }
633
+ command_line = calloc(total_size + 1, sizeof(WCHAR));
634
+ for (i = 0; i < count; ++i) {
635
+ wcscat(command_line, test_output[i]);
636
+ wcscat(command_line, L" ");
637
+ }
638
+ command_line[total_size - 1] = L'\0';
639
+
640
+ wprintf(L"command_line: %s\n", command_line);
641
+
642
+ cracked = CommandLineToArgvW(command_line, &num_args);
643
+ for (i = 0; i < num_args; ++i) {
644
+ wprintf(L"%d: %s\t%s\n", i, test_str[i], cracked[i]);
645
+ ASSERT(wcscmp(test_str[i], cracked[i]) == 0);
646
+ }
647
+
648
+ LocalFree(cracked);
649
+ for (i = 0; i < count; ++i) {
650
+ free(test_output[i]);
651
+ }
652
+
653
+ result = make_program_args(verbatim, 1, &verbatim_output);
654
+ ASSERT(result.code == UV_OK);
655
+ result = make_program_args(verbatim, 0, &non_verbatim_output);
656
+ ASSERT(result.code == UV_OK);
657
+
658
+ wprintf(L" verbatim_output: %s\n", verbatim_output);
659
+ wprintf(L"non_verbatim_output: %s\n", non_verbatim_output);
660
+
661
+ ASSERT(wcscmp(verbatim_output, L"cmd.exe /c c:\\path\\to\\node.exe --eval \"require('c:\\\\path\\\\to\\\\test.js')\"") == 0);
662
+ ASSERT(wcscmp(non_verbatim_output, L"cmd.exe /c \"c:\\path\\to\\node.exe --eval \\\"require('c:\\\\path\\\\to\\\\test.js')\\\"\"") == 0);
663
+
664
+ free(verbatim_output);
665
+ free(non_verbatim_output);
666
+
667
+ return 0;
668
+ }
669
+
670
+ WCHAR* make_program_env(char** env_block);
671
+
672
+ TEST_IMPL(environment_creation) {
673
+ int i;
674
+ char* environment[] = {
675
+ "FOO=BAR",
676
+ "SYSTEM=ROOT", /* substring of a supplied var name */
677
+ "SYSTEMROOTED=OMG", /* supplied var name is a substring */
678
+ "TEMP=C:\\Temp",
679
+ "BAZ=QUX",
680
+ NULL
681
+ };
682
+
683
+ WCHAR expected[512];
684
+ WCHAR* ptr = expected;
685
+ WCHAR* result;
686
+ WCHAR* str;
687
+
688
+ for (i = 0; i < sizeof(environment) / sizeof(environment[0]) - 1; i++) {
689
+ ptr += uv_utf8_to_utf16(environment[i], ptr, expected + sizeof(expected) - ptr);
690
+ }
691
+
692
+ memcpy(ptr, L"SYSTEMROOT=", sizeof(L"SYSTEMROOT="));
693
+ ptr += sizeof(L"SYSTEMROOT=")/sizeof(WCHAR) - 1;
694
+ ptr += GetEnvironmentVariableW(L"SYSTEMROOT", ptr, expected + sizeof(expected) - ptr);
695
+ ++ptr;
696
+
697
+ memcpy(ptr, L"SYSTEMDRIVE=", sizeof(L"SYSTEMDRIVE="));
698
+ ptr += sizeof(L"SYSTEMDRIVE=")/sizeof(WCHAR) - 1;
699
+ ptr += GetEnvironmentVariableW(L"SYSTEMDRIVE", ptr, expected + sizeof(expected) - ptr);
700
+ ++ptr;
701
+ *ptr = '\0';
702
+
703
+ result = make_program_env(environment);
704
+
705
+ for (str = result; *str; str += wcslen(str) + 1) {
706
+ wprintf(L"%s\n", str);
707
+ }
708
+
709
+ ASSERT(wcscmp(expected, result) == 0);
710
+
711
+ return 0;
712
+ }
713
+ #endif
714
+
715
+ #ifndef _WIN32
716
+ TEST_IMPL(spawn_setuid_setgid) {
717
+ int r;
718
+
719
+ /* if not root, then this will fail. */
720
+ uv_uid_t uid = getuid();
721
+ if (uid != 0) {
722
+ fprintf(stderr, "spawn_setuid_setgid skipped: not root\n");
723
+ return 0;
724
+ }
725
+
726
+ init_process_options("spawn_helper1", exit_cb);
727
+
728
+ /* become the "nobody" user. */
729
+ struct passwd* pw;
730
+ pw = getpwnam("nobody");
731
+ ASSERT(pw != NULL);
732
+ options.uid = pw->pw_uid;
733
+ options.gid = pw->pw_gid;
734
+ options.flags = UV_PROCESS_SETUID | UV_PROCESS_SETGID;
735
+
736
+ r = uv_spawn(uv_default_loop(), &process, options);
737
+ ASSERT(r == 0);
738
+
739
+ r = uv_run(uv_default_loop());
740
+ ASSERT(r == 0);
741
+
742
+ ASSERT(exit_cb_called == 1);
743
+ ASSERT(close_cb_called == 1);
744
+
745
+ return 0;
746
+ }
747
+ #endif
748
+
749
+
750
+ #ifndef _WIN32
751
+ TEST_IMPL(spawn_setuid_fails) {
752
+ int r;
753
+
754
+ /* if root, become nobody. */
755
+ uv_uid_t uid = getuid();
756
+ if (uid == 0) {
757
+ struct passwd* pw;
758
+ pw = getpwnam("nobody");
759
+ ASSERT(pw != NULL);
760
+ r = setuid(pw->pw_uid);
761
+ ASSERT(r == 0);
762
+ }
763
+
764
+ init_process_options("spawn_helper1", exit_cb_failure_expected);
765
+
766
+ options.flags |= UV_PROCESS_SETUID;
767
+ options.uid = (uv_uid_t) -42424242;
768
+
769
+ r = uv_spawn(uv_default_loop(), &process, options);
770
+ ASSERT(r == 0);
771
+
772
+ r = uv_run(uv_default_loop());
773
+ ASSERT(r == 0);
774
+
775
+ ASSERT(exit_cb_called == 1);
776
+ ASSERT(close_cb_called == 1);
777
+
778
+ return 0;
779
+ }
780
+
781
+
782
+ TEST_IMPL(spawn_setgid_fails) {
783
+ int r;
784
+
785
+ /* if root, become nobody. */
786
+ uv_uid_t uid = getuid();
787
+ if (uid == 0) {
788
+ struct passwd* pw;
789
+ pw = getpwnam("nobody");
790
+ ASSERT(pw != NULL);
791
+ r = setuid(pw->pw_uid);
792
+ ASSERT(r == 0);
793
+ }
794
+
795
+ init_process_options("spawn_helper1", exit_cb_failure_expected);
796
+
797
+ options.flags |= UV_PROCESS_SETGID;
798
+ options.gid = (uv_gid_t) -42424242;
799
+
800
+ r = uv_spawn(uv_default_loop(), &process, options);
801
+ ASSERT(r == 0);
802
+
803
+ r = uv_run(uv_default_loop());
804
+ ASSERT(r == 0);
805
+
806
+ ASSERT(exit_cb_called == 1);
807
+ ASSERT(close_cb_called == 1);
808
+
809
+ return 0;
810
+ }
811
+ #endif
812
+
813
+
814
+ #ifdef _WIN32
815
+
816
+ static void exit_cb_unexpected(uv_process_t* process,
817
+ int exit_status,
818
+ int term_signal) {
819
+ ASSERT(0 && "should not have been called");
820
+ }
821
+
822
+
823
+ TEST_IMPL(spawn_setuid_fails) {
824
+ int r;
825
+
826
+ init_process_options("spawn_helper1", exit_cb_unexpected);
827
+
828
+ options.flags |= UV_PROCESS_SETUID;
829
+ options.uid = (uv_uid_t) -42424242;
830
+
831
+ r = uv_spawn(uv_default_loop(), &process, options);
832
+ ASSERT(r == -1);
833
+ ASSERT(uv_last_error(uv_default_loop()).code == UV_ENOTSUP);
834
+
835
+ r = uv_run(uv_default_loop());
836
+ ASSERT(r == 0);
837
+
838
+ ASSERT(close_cb_called == 0);
839
+
840
+ return 0;
841
+ }
842
+
843
+
844
+ TEST_IMPL(spawn_setgid_fails) {
845
+ int r;
846
+
847
+ init_process_options("spawn_helper1", exit_cb_unexpected);
848
+
849
+ options.flags |= UV_PROCESS_SETGID;
850
+ options.gid = (uv_gid_t) -42424242;
851
+
852
+ r = uv_spawn(uv_default_loop(), &process, options);
853
+ ASSERT(r == -1);
854
+ ASSERT(uv_last_error(uv_default_loop()).code == UV_ENOTSUP);
855
+
856
+ r = uv_run(uv_default_loop());
857
+ ASSERT(r == 0);
858
+
859
+ ASSERT(close_cb_called == 0);
860
+
861
+ return 0;
862
+ }
863
+ #endif