racer 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (336) hide show
  1. data/LICENSE +22 -0
  2. data/bin/racer +33 -0
  3. data/ext/libuv/AUTHORS +45 -0
  4. data/ext/libuv/LICENSE +42 -0
  5. data/ext/libuv/Makefile +119 -0
  6. data/ext/libuv/README.md +88 -0
  7. data/ext/libuv/build/gcc_version.py +20 -0
  8. data/ext/libuv/common.gypi +176 -0
  9. data/ext/libuv/config-mingw.mk +61 -0
  10. data/ext/libuv/config-unix.mk +171 -0
  11. data/ext/libuv/gyp_uv +64 -0
  12. data/ext/libuv/include/ares.h +591 -0
  13. data/ext/libuv/include/ares_version.h +24 -0
  14. data/ext/libuv/include/uv-private/eio.h +403 -0
  15. data/ext/libuv/include/uv-private/ev.h +838 -0
  16. data/ext/libuv/include/uv-private/ngx-queue.h +108 -0
  17. data/ext/libuv/include/uv-private/tree.h +768 -0
  18. data/ext/libuv/include/uv-private/uv-unix.h +310 -0
  19. data/ext/libuv/include/uv-private/uv-win.h +510 -0
  20. data/ext/libuv/include/uv.h +1796 -0
  21. data/ext/libuv/src/ares/AUTHORS +37 -0
  22. data/ext/libuv/src/ares/CHANGES +1218 -0
  23. data/ext/libuv/src/ares/CMakeLists.txt +22 -0
  24. data/ext/libuv/src/ares/NEWS +21 -0
  25. data/ext/libuv/src/ares/README +60 -0
  26. data/ext/libuv/src/ares/README.cares +13 -0
  27. data/ext/libuv/src/ares/README.msvc +142 -0
  28. data/ext/libuv/src/ares/README.node +21 -0
  29. data/ext/libuv/src/ares/RELEASE-NOTES +26 -0
  30. data/ext/libuv/src/ares/TODO +23 -0
  31. data/ext/libuv/src/ares/ares__close_sockets.c +66 -0
  32. data/ext/libuv/src/ares/ares__get_hostent.c +263 -0
  33. data/ext/libuv/src/ares/ares__read_line.c +71 -0
  34. data/ext/libuv/src/ares/ares__timeval.c +111 -0
  35. data/ext/libuv/src/ares/ares_cancel.c +63 -0
  36. data/ext/libuv/src/ares/ares_data.c +190 -0
  37. data/ext/libuv/src/ares/ares_data.h +65 -0
  38. data/ext/libuv/src/ares/ares_destroy.c +105 -0
  39. data/ext/libuv/src/ares/ares_dns.h +90 -0
  40. data/ext/libuv/src/ares/ares_expand_name.c +200 -0
  41. data/ext/libuv/src/ares/ares_expand_string.c +75 -0
  42. data/ext/libuv/src/ares/ares_fds.c +63 -0
  43. data/ext/libuv/src/ares/ares_free_hostent.c +42 -0
  44. data/ext/libuv/src/ares/ares_free_string.c +25 -0
  45. data/ext/libuv/src/ares/ares_getenv.c +30 -0
  46. data/ext/libuv/src/ares/ares_getenv.h +26 -0
  47. data/ext/libuv/src/ares/ares_gethostbyaddr.c +301 -0
  48. data/ext/libuv/src/ares/ares_gethostbyname.c +523 -0
  49. data/ext/libuv/src/ares/ares_getnameinfo.c +427 -0
  50. data/ext/libuv/src/ares/ares_getopt.c +122 -0
  51. data/ext/libuv/src/ares/ares_getopt.h +53 -0
  52. data/ext/libuv/src/ares/ares_getsock.c +72 -0
  53. data/ext/libuv/src/ares/ares_init.c +1809 -0
  54. data/ext/libuv/src/ares/ares_iphlpapi.h +221 -0
  55. data/ext/libuv/src/ares/ares_ipv6.h +78 -0
  56. data/ext/libuv/src/ares/ares_library_init.c +142 -0
  57. data/ext/libuv/src/ares/ares_library_init.h +42 -0
  58. data/ext/libuv/src/ares/ares_llist.c +86 -0
  59. data/ext/libuv/src/ares/ares_llist.h +42 -0
  60. data/ext/libuv/src/ares/ares_mkquery.c +195 -0
  61. data/ext/libuv/src/ares/ares_nowarn.c +181 -0
  62. data/ext/libuv/src/ares/ares_nowarn.h +55 -0
  63. data/ext/libuv/src/ares/ares_options.c +248 -0
  64. data/ext/libuv/src/ares/ares_parse_a_reply.c +263 -0
  65. data/ext/libuv/src/ares/ares_parse_aaaa_reply.c +259 -0
  66. data/ext/libuv/src/ares/ares_parse_mx_reply.c +170 -0
  67. data/ext/libuv/src/ares/ares_parse_ns_reply.c +182 -0
  68. data/ext/libuv/src/ares/ares_parse_ptr_reply.c +217 -0
  69. data/ext/libuv/src/ares/ares_parse_srv_reply.c +179 -0
  70. data/ext/libuv/src/ares/ares_parse_txt_reply.c +201 -0
  71. data/ext/libuv/src/ares/ares_platform.c +11035 -0
  72. data/ext/libuv/src/ares/ares_platform.h +43 -0
  73. data/ext/libuv/src/ares/ares_private.h +355 -0
  74. data/ext/libuv/src/ares/ares_process.c +1295 -0
  75. data/ext/libuv/src/ares/ares_query.c +183 -0
  76. data/ext/libuv/src/ares/ares_rules.h +144 -0
  77. data/ext/libuv/src/ares/ares_search.c +321 -0
  78. data/ext/libuv/src/ares/ares_send.c +134 -0
  79. data/ext/libuv/src/ares/ares_setup.h +199 -0
  80. data/ext/libuv/src/ares/ares_strcasecmp.c +66 -0
  81. data/ext/libuv/src/ares/ares_strcasecmp.h +30 -0
  82. data/ext/libuv/src/ares/ares_strdup.c +42 -0
  83. data/ext/libuv/src/ares/ares_strdup.h +26 -0
  84. data/ext/libuv/src/ares/ares_strerror.c +56 -0
  85. data/ext/libuv/src/ares/ares_timeout.c +80 -0
  86. data/ext/libuv/src/ares/ares_version.c +11 -0
  87. data/ext/libuv/src/ares/ares_writev.c +79 -0
  88. data/ext/libuv/src/ares/ares_writev.h +36 -0
  89. data/ext/libuv/src/ares/bitncmp.c +59 -0
  90. data/ext/libuv/src/ares/bitncmp.h +26 -0
  91. data/ext/libuv/src/ares/config_cygwin/ares_config.h +512 -0
  92. data/ext/libuv/src/ares/config_darwin/ares_config.h +512 -0
  93. data/ext/libuv/src/ares/config_freebsd/ares_config.h +512 -0
  94. data/ext/libuv/src/ares/config_linux/ares_config.h +512 -0
  95. data/ext/libuv/src/ares/config_netbsd/ares_config.h +512 -0
  96. data/ext/libuv/src/ares/config_openbsd/ares_config.h +512 -0
  97. data/ext/libuv/src/ares/config_sunos/ares_config.h +512 -0
  98. data/ext/libuv/src/ares/config_win32/ares_config.h +369 -0
  99. data/ext/libuv/src/ares/get_ver.awk +35 -0
  100. data/ext/libuv/src/ares/inet_net_pton.c +451 -0
  101. data/ext/libuv/src/ares/inet_net_pton.h +31 -0
  102. data/ext/libuv/src/ares/inet_ntop.c +208 -0
  103. data/ext/libuv/src/ares/inet_ntop.h +26 -0
  104. data/ext/libuv/src/ares/nameser.h +203 -0
  105. data/ext/libuv/src/ares/setup_once.h +504 -0
  106. data/ext/libuv/src/ares/windows_port.c +22 -0
  107. data/ext/libuv/src/cares.c +225 -0
  108. data/ext/libuv/src/fs-poll.c +237 -0
  109. data/ext/libuv/src/unix/async.c +119 -0
  110. data/ext/libuv/src/unix/core.c +647 -0
  111. data/ext/libuv/src/unix/cygwin.c +84 -0
  112. data/ext/libuv/src/unix/darwin.c +341 -0
  113. data/ext/libuv/src/unix/dl.c +83 -0
  114. data/ext/libuv/src/unix/eio/Changes +63 -0
  115. data/ext/libuv/src/unix/eio/LICENSE +36 -0
  116. data/ext/libuv/src/unix/eio/Makefile.am +15 -0
  117. data/ext/libuv/src/unix/eio/aclocal.m4 +8957 -0
  118. data/ext/libuv/src/unix/eio/autogen.sh +3 -0
  119. data/ext/libuv/src/unix/eio/config.h.in +86 -0
  120. data/ext/libuv/src/unix/eio/config_cygwin.h +80 -0
  121. data/ext/libuv/src/unix/eio/config_darwin.h +141 -0
  122. data/ext/libuv/src/unix/eio/config_freebsd.h +81 -0
  123. data/ext/libuv/src/unix/eio/config_linux.h +94 -0
  124. data/ext/libuv/src/unix/eio/config_netbsd.h +81 -0
  125. data/ext/libuv/src/unix/eio/config_openbsd.h +137 -0
  126. data/ext/libuv/src/unix/eio/config_sunos.h +84 -0
  127. data/ext/libuv/src/unix/eio/configure.ac +22 -0
  128. data/ext/libuv/src/unix/eio/demo.c +194 -0
  129. data/ext/libuv/src/unix/eio/ecb.h +370 -0
  130. data/ext/libuv/src/unix/eio/eio.3 +3428 -0
  131. data/ext/libuv/src/unix/eio/eio.c +2593 -0
  132. data/ext/libuv/src/unix/eio/eio.pod +969 -0
  133. data/ext/libuv/src/unix/eio/libeio.m4 +195 -0
  134. data/ext/libuv/src/unix/eio/xthread.h +164 -0
  135. data/ext/libuv/src/unix/error.c +104 -0
  136. data/ext/libuv/src/unix/ev/Changes +388 -0
  137. data/ext/libuv/src/unix/ev/LICENSE +36 -0
  138. data/ext/libuv/src/unix/ev/Makefile.am +18 -0
  139. data/ext/libuv/src/unix/ev/Makefile.in +771 -0
  140. data/ext/libuv/src/unix/ev/README +58 -0
  141. data/ext/libuv/src/unix/ev/aclocal.m4 +8957 -0
  142. data/ext/libuv/src/unix/ev/autogen.sh +6 -0
  143. data/ext/libuv/src/unix/ev/config.guess +1526 -0
  144. data/ext/libuv/src/unix/ev/config.h.in +125 -0
  145. data/ext/libuv/src/unix/ev/config.sub +1658 -0
  146. data/ext/libuv/src/unix/ev/config_cygwin.h +123 -0
  147. data/ext/libuv/src/unix/ev/config_darwin.h +122 -0
  148. data/ext/libuv/src/unix/ev/config_freebsd.h +120 -0
  149. data/ext/libuv/src/unix/ev/config_linux.h +141 -0
  150. data/ext/libuv/src/unix/ev/config_netbsd.h +120 -0
  151. data/ext/libuv/src/unix/ev/config_openbsd.h +126 -0
  152. data/ext/libuv/src/unix/ev/config_sunos.h +122 -0
  153. data/ext/libuv/src/unix/ev/configure +13037 -0
  154. data/ext/libuv/src/unix/ev/configure.ac +18 -0
  155. data/ext/libuv/src/unix/ev/depcomp +630 -0
  156. data/ext/libuv/src/unix/ev/ev++.h +816 -0
  157. data/ext/libuv/src/unix/ev/ev.3 +5311 -0
  158. data/ext/libuv/src/unix/ev/ev.c +3925 -0
  159. data/ext/libuv/src/unix/ev/ev.pod +5243 -0
  160. data/ext/libuv/src/unix/ev/ev_epoll.c +266 -0
  161. data/ext/libuv/src/unix/ev/ev_kqueue.c +235 -0
  162. data/ext/libuv/src/unix/ev/ev_poll.c +148 -0
  163. data/ext/libuv/src/unix/ev/ev_port.c +179 -0
  164. data/ext/libuv/src/unix/ev/ev_select.c +310 -0
  165. data/ext/libuv/src/unix/ev/ev_vars.h +203 -0
  166. data/ext/libuv/src/unix/ev/ev_win32.c +153 -0
  167. data/ext/libuv/src/unix/ev/ev_wrap.h +196 -0
  168. data/ext/libuv/src/unix/ev/event.c +402 -0
  169. data/ext/libuv/src/unix/ev/event.h +170 -0
  170. data/ext/libuv/src/unix/ev/install-sh +294 -0
  171. data/ext/libuv/src/unix/ev/libev.m4 +39 -0
  172. data/ext/libuv/src/unix/ev/ltmain.sh +8413 -0
  173. data/ext/libuv/src/unix/ev/missing +336 -0
  174. data/ext/libuv/src/unix/ev/mkinstalldirs +111 -0
  175. data/ext/libuv/src/unix/freebsd.c +312 -0
  176. data/ext/libuv/src/unix/fs.c +699 -0
  177. data/ext/libuv/src/unix/internal.h +180 -0
  178. data/ext/libuv/src/unix/kqueue.c +122 -0
  179. data/ext/libuv/src/unix/linux/core.c +474 -0
  180. data/ext/libuv/src/unix/linux/inotify.c +209 -0
  181. data/ext/libuv/src/unix/linux/syscalls.c +268 -0
  182. data/ext/libuv/src/unix/linux/syscalls.h +93 -0
  183. data/ext/libuv/src/unix/loop-watcher.c +63 -0
  184. data/ext/libuv/src/unix/loop.c +81 -0
  185. data/ext/libuv/src/unix/netbsd.c +108 -0
  186. data/ext/libuv/src/unix/openbsd.c +295 -0
  187. data/ext/libuv/src/unix/pipe.c +261 -0
  188. data/ext/libuv/src/unix/poll.c +116 -0
  189. data/ext/libuv/src/unix/process.c +447 -0
  190. data/ext/libuv/src/unix/stream.c +1026 -0
  191. data/ext/libuv/src/unix/sunos.c +483 -0
  192. data/ext/libuv/src/unix/tcp.c +328 -0
  193. data/ext/libuv/src/unix/thread.c +211 -0
  194. data/ext/libuv/src/unix/timer.c +138 -0
  195. data/ext/libuv/src/unix/tty.c +146 -0
  196. data/ext/libuv/src/unix/udp.c +661 -0
  197. data/ext/libuv/src/unix/uv-eio.c +107 -0
  198. data/ext/libuv/src/unix/uv-eio.h +13 -0
  199. data/ext/libuv/src/uv-common.c +384 -0
  200. data/ext/libuv/src/uv-common.h +171 -0
  201. data/ext/libuv/src/win/async.c +130 -0
  202. data/ext/libuv/src/win/core.c +283 -0
  203. data/ext/libuv/src/win/dl.c +86 -0
  204. data/ext/libuv/src/win/error.c +150 -0
  205. data/ext/libuv/src/win/fs-event.c +512 -0
  206. data/ext/libuv/src/win/fs.c +1856 -0
  207. data/ext/libuv/src/win/getaddrinfo.c +365 -0
  208. data/ext/libuv/src/win/handle-inl.h +145 -0
  209. data/ext/libuv/src/win/handle.c +150 -0
  210. data/ext/libuv/src/win/internal.h +337 -0
  211. data/ext/libuv/src/win/loop-watcher.c +123 -0
  212. data/ext/libuv/src/win/pipe.c +1675 -0
  213. data/ext/libuv/src/win/poll.c +618 -0
  214. data/ext/libuv/src/win/process-stdio.c +479 -0
  215. data/ext/libuv/src/win/process.c +1013 -0
  216. data/ext/libuv/src/win/req-inl.h +225 -0
  217. data/ext/libuv/src/win/req.c +25 -0
  218. data/ext/libuv/src/win/stream-inl.h +69 -0
  219. data/ext/libuv/src/win/stream.c +167 -0
  220. data/ext/libuv/src/win/tcp.c +1400 -0
  221. data/ext/libuv/src/win/thread.c +372 -0
  222. data/ext/libuv/src/win/threadpool.c +74 -0
  223. data/ext/libuv/src/win/timer.c +226 -0
  224. data/ext/libuv/src/win/tty.c +1801 -0
  225. data/ext/libuv/src/win/udp.c +718 -0
  226. data/ext/libuv/src/win/util.c +864 -0
  227. data/ext/libuv/src/win/winapi.c +132 -0
  228. data/ext/libuv/src/win/winapi.h +4443 -0
  229. data/ext/libuv/src/win/winsock.c +557 -0
  230. data/ext/libuv/src/win/winsock.h +171 -0
  231. data/ext/libuv/test/benchmark-ares.c +117 -0
  232. data/ext/libuv/test/benchmark-fs-stat.c +164 -0
  233. data/ext/libuv/test/benchmark-getaddrinfo.c +94 -0
  234. data/ext/libuv/test/benchmark-list.h +114 -0
  235. data/ext/libuv/test/benchmark-loop-count.c +88 -0
  236. data/ext/libuv/test/benchmark-million-timers.c +65 -0
  237. data/ext/libuv/test/benchmark-ping-pongs.c +213 -0
  238. data/ext/libuv/test/benchmark-pound.c +324 -0
  239. data/ext/libuv/test/benchmark-pump.c +462 -0
  240. data/ext/libuv/test/benchmark-sizes.c +42 -0
  241. data/ext/libuv/test/benchmark-spawn.c +162 -0
  242. data/ext/libuv/test/benchmark-tcp-write-batch.c +140 -0
  243. data/ext/libuv/test/benchmark-thread.c +64 -0
  244. data/ext/libuv/test/benchmark-udp-packet-storm.c +247 -0
  245. data/ext/libuv/test/blackhole-server.c +118 -0
  246. data/ext/libuv/test/dns-server.c +321 -0
  247. data/ext/libuv/test/echo-server.c +378 -0
  248. data/ext/libuv/test/fixtures/empty_file +0 -0
  249. data/ext/libuv/test/fixtures/load_error.node +1 -0
  250. data/ext/libuv/test/run-benchmarks.c +64 -0
  251. data/ext/libuv/test/run-tests.c +138 -0
  252. data/ext/libuv/test/runner-unix.c +290 -0
  253. data/ext/libuv/test/runner-unix.h +36 -0
  254. data/ext/libuv/test/runner-win.c +285 -0
  255. data/ext/libuv/test/runner-win.h +42 -0
  256. data/ext/libuv/test/runner.c +326 -0
  257. data/ext/libuv/test/runner.h +159 -0
  258. data/ext/libuv/test/task.h +109 -0
  259. data/ext/libuv/test/test-async.c +118 -0
  260. data/ext/libuv/test/test-callback-order.c +76 -0
  261. data/ext/libuv/test/test-callback-stack.c +203 -0
  262. data/ext/libuv/test/test-connection-fail.c +148 -0
  263. data/ext/libuv/test/test-counters-init.c +215 -0
  264. data/ext/libuv/test/test-cwd-and-chdir.c +64 -0
  265. data/ext/libuv/test/test-delayed-accept.c +197 -0
  266. data/ext/libuv/test/test-dlerror.c +58 -0
  267. data/ext/libuv/test/test-error.c +59 -0
  268. data/ext/libuv/test/test-fail-always.c +29 -0
  269. data/ext/libuv/test/test-fs-event.c +442 -0
  270. data/ext/libuv/test/test-fs-poll.c +146 -0
  271. data/ext/libuv/test/test-fs.c +1843 -0
  272. data/ext/libuv/test/test-get-currentexe.c +63 -0
  273. data/ext/libuv/test/test-get-loadavg.c +36 -0
  274. data/ext/libuv/test/test-get-memory.c +38 -0
  275. data/ext/libuv/test/test-getaddrinfo.c +122 -0
  276. data/ext/libuv/test/test-gethostbyname.c +189 -0
  277. data/ext/libuv/test/test-getsockname.c +342 -0
  278. data/ext/libuv/test/test-hrtime.c +54 -0
  279. data/ext/libuv/test/test-idle.c +81 -0
  280. data/ext/libuv/test/test-ipc-send-recv.c +209 -0
  281. data/ext/libuv/test/test-ipc.c +620 -0
  282. data/ext/libuv/test/test-list.h +421 -0
  283. data/ext/libuv/test/test-loop-handles.c +336 -0
  284. data/ext/libuv/test/test-multiple-listen.c +102 -0
  285. data/ext/libuv/test/test-mutexes.c +63 -0
  286. data/ext/libuv/test/test-pass-always.c +28 -0
  287. data/ext/libuv/test/test-ping-pong.c +253 -0
  288. data/ext/libuv/test/test-pipe-bind-error.c +140 -0
  289. data/ext/libuv/test/test-pipe-connect-error.c +96 -0
  290. data/ext/libuv/test/test-platform-output.c +87 -0
  291. data/ext/libuv/test/test-poll-close.c +77 -0
  292. data/ext/libuv/test/test-poll.c +573 -0
  293. data/ext/libuv/test/test-process-title.c +42 -0
  294. data/ext/libuv/test/test-ref.c +338 -0
  295. data/ext/libuv/test/test-run-once.c +48 -0
  296. data/ext/libuv/test/test-semaphore.c +111 -0
  297. data/ext/libuv/test/test-shutdown-close.c +103 -0
  298. data/ext/libuv/test/test-shutdown-eof.c +183 -0
  299. data/ext/libuv/test/test-spawn.c +847 -0
  300. data/ext/libuv/test/test-stdio-over-pipes.c +246 -0
  301. data/ext/libuv/test/test-tcp-bind-error.c +191 -0
  302. data/ext/libuv/test/test-tcp-bind6-error.c +154 -0
  303. data/ext/libuv/test/test-tcp-close-while-connecting.c +80 -0
  304. data/ext/libuv/test/test-tcp-close.c +129 -0
  305. data/ext/libuv/test/test-tcp-connect-error-after-write.c +95 -0
  306. data/ext/libuv/test/test-tcp-connect-error.c +70 -0
  307. data/ext/libuv/test/test-tcp-connect-timeout.c +85 -0
  308. data/ext/libuv/test/test-tcp-connect6-error.c +68 -0
  309. data/ext/libuv/test/test-tcp-flags.c +51 -0
  310. data/ext/libuv/test/test-tcp-shutdown-after-write.c +131 -0
  311. data/ext/libuv/test/test-tcp-write-error.c +168 -0
  312. data/ext/libuv/test/test-tcp-write-to-half-open-connection.c +135 -0
  313. data/ext/libuv/test/test-tcp-writealot.c +170 -0
  314. data/ext/libuv/test/test-thread.c +183 -0
  315. data/ext/libuv/test/test-threadpool.c +57 -0
  316. data/ext/libuv/test/test-timer-again.c +141 -0
  317. data/ext/libuv/test/test-timer.c +152 -0
  318. data/ext/libuv/test/test-tty.c +110 -0
  319. data/ext/libuv/test/test-udp-dgram-too-big.c +86 -0
  320. data/ext/libuv/test/test-udp-ipv6.c +156 -0
  321. data/ext/libuv/test/test-udp-multicast-join.c +139 -0
  322. data/ext/libuv/test/test-udp-multicast-ttl.c +86 -0
  323. data/ext/libuv/test/test-udp-options.c +86 -0
  324. data/ext/libuv/test/test-udp-send-and-recv.c +208 -0
  325. data/ext/libuv/test/test-util.c +97 -0
  326. data/ext/libuv/test/test-walk-handles.c +77 -0
  327. data/ext/libuv/uv.gyp +452 -0
  328. data/ext/libuv/vcbuild.bat +105 -0
  329. data/ext/racer/extconf.rb +20 -0
  330. data/ext/racer/http_parser.c +2059 -0
  331. data/ext/racer/http_parser.h +318 -0
  332. data/ext/racer/mkmf.log +22 -0
  333. data/ext/racer/racer.c +551 -0
  334. data/lib/racer.rb +2 -0
  335. data/lib/racer/version.rb +3 -0
  336. metadata +382 -0
@@ -0,0 +1,1013 @@
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 <assert.h>
23
+ #include <io.h>
24
+ #include <stdio.h>
25
+ #include <stdlib.h>
26
+ #include <signal.h>
27
+
28
+ #include "uv.h"
29
+ #include "internal.h"
30
+ #include "handle-inl.h"
31
+ #include "req-inl.h"
32
+
33
+
34
+ #define SIGKILL 9
35
+
36
+
37
+ typedef struct env_var {
38
+ const char* narrow;
39
+ const wchar_t* wide;
40
+ int len; /* including null or '=' */
41
+ int supplied;
42
+ int value_len;
43
+ } env_var_t;
44
+
45
+ #define E_V(str) { str "=", L##str, sizeof(str), 0, 0 }
46
+
47
+
48
+ #define UTF8_TO_UTF16(s, t) \
49
+ size = uv_utf8_to_utf16(s, NULL, 0) * sizeof(wchar_t); \
50
+ t = (wchar_t*)malloc(size); \
51
+ if (!t) { \
52
+ uv_fatal_error(ERROR_OUTOFMEMORY, "malloc"); \
53
+ } \
54
+ if (!uv_utf8_to_utf16(s, t, size / sizeof(wchar_t))) { \
55
+ uv__set_sys_error(loop, GetLastError()); \
56
+ err = -1; \
57
+ goto done; \
58
+ }
59
+
60
+
61
+ static void uv_process_init(uv_loop_t* loop, uv_process_t* handle) {
62
+ uv__handle_init(loop, (uv_handle_t*) handle, UV_PROCESS);
63
+ handle->exit_cb = NULL;
64
+ handle->pid = 0;
65
+ handle->exit_signal = 0;
66
+ handle->wait_handle = INVALID_HANDLE_VALUE;
67
+ handle->process_handle = INVALID_HANDLE_VALUE;
68
+ handle->close_handle = INVALID_HANDLE_VALUE;
69
+ handle->child_stdio_buffer = NULL;
70
+
71
+ uv_req_init(loop, (uv_req_t*)&handle->exit_req);
72
+ handle->exit_req.type = UV_PROCESS_EXIT;
73
+ handle->exit_req.data = handle;
74
+ uv_req_init(loop, (uv_req_t*)&handle->close_req);
75
+ handle->close_req.type = UV_PROCESS_CLOSE;
76
+ handle->close_req.data = handle;
77
+
78
+ loop->counters.handle_init++;
79
+ loop->counters.process_init++;
80
+ }
81
+
82
+
83
+ /*
84
+ * Path search functions
85
+ */
86
+
87
+ /*
88
+ * Helper function for search_path
89
+ */
90
+ static wchar_t* search_path_join_test(const wchar_t* dir,
91
+ int dir_len,
92
+ const wchar_t* name,
93
+ int name_len,
94
+ const wchar_t* ext,
95
+ int ext_len,
96
+ const wchar_t* cwd,
97
+ int cwd_len) {
98
+ wchar_t *result, *result_pos;
99
+ DWORD attrs;
100
+
101
+ if (dir_len >= 1 && (dir[0] == L'/' || dir[0] == L'\\')) {
102
+ /* It's a full path without drive letter, use cwd's drive letter only */
103
+ cwd_len = 2;
104
+ } else if (dir_len >= 2 && dir[1] == L':' &&
105
+ (dir_len < 3 || (dir[2] != L'/' && dir[2] != L'\\'))) {
106
+ /* It's a relative path with drive letter (ext.g. D:../some/file)
107
+ * Replace drive letter in dir by full cwd if it points to the same drive,
108
+ * otherwise use the dir only.
109
+ */
110
+ if (cwd_len < 2 || _wcsnicmp(cwd, dir, 2) != 0) {
111
+ cwd_len = 0;
112
+ } else {
113
+ dir += 2;
114
+ dir_len -= 2;
115
+ }
116
+ } else if (dir_len > 2 && dir[1] == L':') {
117
+ /* It's an absolute path with drive letter
118
+ * Don't use the cwd at all
119
+ */
120
+ cwd_len = 0;
121
+ }
122
+
123
+ /* Allocate buffer for output */
124
+ result = result_pos = (wchar_t*)malloc(sizeof(wchar_t) *
125
+ (cwd_len + 1 + dir_len + 1 + name_len + 1 + ext_len + 1));
126
+
127
+ /* Copy cwd */
128
+ wcsncpy(result_pos, cwd, cwd_len);
129
+ result_pos += cwd_len;
130
+
131
+ /* Add a path separator if cwd didn't end with one */
132
+ if (cwd_len && wcsrchr(L"\\/:", result_pos[-1]) == NULL) {
133
+ result_pos[0] = L'\\';
134
+ result_pos++;
135
+ }
136
+
137
+ /* Copy dir */
138
+ wcsncpy(result_pos, dir, dir_len);
139
+ result_pos += dir_len;
140
+
141
+ /* Add a separator if the dir didn't end with one */
142
+ if (dir_len && wcsrchr(L"\\/:", result_pos[-1]) == NULL) {
143
+ result_pos[0] = L'\\';
144
+ result_pos++;
145
+ }
146
+
147
+ /* Copy filename */
148
+ wcsncpy(result_pos, name, name_len);
149
+ result_pos += name_len;
150
+
151
+ if (ext_len) {
152
+ /* Add a dot if the filename didn't end with one */
153
+ if (name_len && result_pos[-1] != '.') {
154
+ result_pos[0] = L'.';
155
+ result_pos++;
156
+ }
157
+
158
+ /* Copy extension */
159
+ wcsncpy(result_pos, ext, ext_len);
160
+ result_pos += ext_len;
161
+ }
162
+
163
+ /* Null terminator */
164
+ result_pos[0] = L'\0';
165
+
166
+ attrs = GetFileAttributesW(result);
167
+
168
+ if (attrs != INVALID_FILE_ATTRIBUTES &&
169
+ !(attrs & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT))) {
170
+ return result;
171
+ }
172
+
173
+ free(result);
174
+ return NULL;
175
+ }
176
+
177
+
178
+ /*
179
+ * Helper function for search_path
180
+ */
181
+ static wchar_t* path_search_walk_ext(const wchar_t *dir,
182
+ int dir_len,
183
+ const wchar_t *name,
184
+ int name_len,
185
+ wchar_t *cwd,
186
+ int cwd_len,
187
+ int name_has_ext) {
188
+ wchar_t* result;
189
+
190
+ /* If the name itself has a nonempty extension, try this extension first */
191
+ if (name_has_ext) {
192
+ result = search_path_join_test(dir, dir_len,
193
+ name, name_len,
194
+ L"", 0,
195
+ cwd, cwd_len);
196
+ if (result != NULL) {
197
+ return result;
198
+ }
199
+ }
200
+
201
+ /* Try .com extension */
202
+ result = search_path_join_test(dir, dir_len,
203
+ name, name_len,
204
+ L"com", 3,
205
+ cwd, cwd_len);
206
+ if (result != NULL) {
207
+ return result;
208
+ }
209
+
210
+ /* Try .exe extension */
211
+ result = search_path_join_test(dir, dir_len,
212
+ name, name_len,
213
+ L"exe", 3,
214
+ cwd, cwd_len);
215
+ if (result != NULL) {
216
+ return result;
217
+ }
218
+
219
+ return NULL;
220
+ }
221
+
222
+
223
+ /*
224
+ * search_path searches the system path for an executable filename -
225
+ * the windows API doesn't provide this as a standalone function nor as an
226
+ * option to CreateProcess.
227
+ *
228
+ * It tries to return an absolute filename.
229
+ *
230
+ * Furthermore, it tries to follow the semantics that cmd.exe, with this
231
+ * exception that PATHEXT environment variable isn't used. Since CreateProcess
232
+ * can start only .com and .exe files, only those extensions are tried. This
233
+ * behavior equals that of msvcrt's spawn functions.
234
+ *
235
+ * - Do not search the path if the filename already contains a path (either
236
+ * relative or absolute).
237
+ *
238
+ * - If there's really only a filename, check the current directory for file,
239
+ * then search all path directories.
240
+ *
241
+ * - If filename specified has *any* extension, search for the file with the
242
+ * specified extension first.
243
+ *
244
+ * - If the literal filename is not found in a directory, try *appending*
245
+ * (not replacing) .com first and then .exe.
246
+ *
247
+ * - The path variable may contain relative paths; relative paths are relative
248
+ * to the cwd.
249
+ *
250
+ * - Directories in path may or may not end with a trailing backslash.
251
+ *
252
+ * - CMD does not trim leading/trailing whitespace from path/pathex entries
253
+ * nor from the environment variables as a whole.
254
+ *
255
+ * - When cmd.exe cannot read a directory, it will just skip it and go on
256
+ * searching. However, unlike posix-y systems, it will happily try to run a
257
+ * file that is not readable/executable; if the spawn fails it will not
258
+ * continue searching.
259
+ *
260
+ * TODO: correctly interpret UNC paths
261
+ */
262
+ static wchar_t* search_path(const wchar_t *file,
263
+ wchar_t *cwd,
264
+ const wchar_t *path) {
265
+ int file_has_dir;
266
+ wchar_t* result = NULL;
267
+ wchar_t *file_name_start;
268
+ wchar_t *dot;
269
+ const wchar_t *dir_start, *dir_end, *dir_path;
270
+ int dir_len;
271
+ int name_has_ext;
272
+
273
+ int file_len = wcslen(file);
274
+ int cwd_len = wcslen(cwd);
275
+
276
+ /* If the caller supplies an empty filename,
277
+ * we're not gonna return c:\windows\.exe -- GFY!
278
+ */
279
+ if (file_len == 0
280
+ || (file_len == 1 && file[0] == L'.')) {
281
+ return NULL;
282
+ }
283
+
284
+ /* Find the start of the filename so we can split the directory from the */
285
+ /* name. */
286
+ for (file_name_start = (wchar_t*)file + file_len;
287
+ file_name_start > file
288
+ && file_name_start[-1] != L'\\'
289
+ && file_name_start[-1] != L'/'
290
+ && file_name_start[-1] != L':';
291
+ file_name_start--);
292
+
293
+ file_has_dir = file_name_start != file;
294
+
295
+ /* Check if the filename includes an extension */
296
+ dot = wcschr(file_name_start, L'.');
297
+ name_has_ext = (dot != NULL && dot[1] != L'\0');
298
+
299
+ if (file_has_dir) {
300
+ /* The file has a path inside, don't use path */
301
+ result = path_search_walk_ext(
302
+ file, file_name_start - file,
303
+ file_name_start, file_len - (file_name_start - file),
304
+ cwd, cwd_len,
305
+ name_has_ext);
306
+
307
+ } else {
308
+ dir_end = path;
309
+
310
+ /* The file is really only a name; look in cwd first, then scan path */
311
+ result = path_search_walk_ext(L"", 0,
312
+ file, file_len,
313
+ cwd, cwd_len,
314
+ name_has_ext);
315
+
316
+ while (result == NULL) {
317
+ if (*dir_end == L'\0') {
318
+ break;
319
+ }
320
+
321
+ /* Skip the separator that dir_end now points to */
322
+ if (dir_end != path) {
323
+ dir_end++;
324
+ }
325
+
326
+ /* Next slice starts just after where the previous one ended */
327
+ dir_start = dir_end;
328
+
329
+ /* Slice until the next ; or \0 is found */
330
+ dir_end = wcschr(dir_start, L';');
331
+ if (dir_end == NULL) {
332
+ dir_end = wcschr(dir_start, L'\0');
333
+ }
334
+
335
+ /* If the slice is zero-length, don't bother */
336
+ if (dir_end - dir_start == 0) {
337
+ continue;
338
+ }
339
+
340
+ dir_path = dir_start;
341
+ dir_len = dir_end - dir_start;
342
+
343
+ /* Adjust if the path is quoted. */
344
+ if (dir_path[0] == '"' || dir_path[0] == '\'') {
345
+ ++dir_path;
346
+ --dir_len;
347
+ }
348
+
349
+ if (dir_path[dir_len - 1] == '"' || dir_path[dir_len - 1] == '\'') {
350
+ --dir_len;
351
+ }
352
+
353
+ result = path_search_walk_ext(dir_path, dir_len,
354
+ file, file_len,
355
+ cwd, cwd_len,
356
+ name_has_ext);
357
+ }
358
+ }
359
+
360
+ return result;
361
+ }
362
+
363
+
364
+ /*
365
+ * Quotes command line arguments
366
+ * Returns a pointer to the end (next char to be written) of the buffer
367
+ */
368
+ wchar_t* quote_cmd_arg(const wchar_t *source, wchar_t *target) {
369
+ int len = wcslen(source),
370
+ i, quote_hit;
371
+ wchar_t* start;
372
+
373
+ /*
374
+ * Check if the string must be quoted;
375
+ * if unnecessary, don't do it, it may only confuse older programs.
376
+ */
377
+ if (len == 0) {
378
+ return target;
379
+ }
380
+
381
+ if (NULL == wcspbrk(source, L" \t\"")) {
382
+ /* No quotation needed */
383
+ wcsncpy(target, source, len);
384
+ target += len;
385
+ return target;
386
+ }
387
+
388
+ if (NULL == wcspbrk(source, L"\"\\")) {
389
+ /*
390
+ * No embedded double quotes or backlashes, so I can just wrap
391
+ * quote marks around the whole thing.
392
+ */
393
+ *(target++) = L'"';
394
+ wcsncpy(target, source, len);
395
+ target += len;
396
+ *(target++) = L'"';
397
+ return target;
398
+ }
399
+
400
+ /*
401
+ * Expected input/output:
402
+ * input : hello"world
403
+ * output: "hello\"world"
404
+ * input : hello""world
405
+ * output: "hello\"\"world"
406
+ * input : hello\world
407
+ * output: hello\world
408
+ * input : hello\\world
409
+ * output: hello\\world
410
+ * input : hello\"world
411
+ * output: "hello\\\"world"
412
+ * input : hello\\"world
413
+ * output: "hello\\\\\"world"
414
+ * input : hello world\
415
+ * output: "hello world\"
416
+ */
417
+
418
+ *(target++) = L'"';
419
+ start = target;
420
+ quote_hit = 1;
421
+
422
+ for (i = len; i > 0; --i) {
423
+ *(target++) = source[i - 1];
424
+
425
+ if (quote_hit && source[i - 1] == L'\\') {
426
+ *(target++) = L'\\';
427
+ } else if(source[i - 1] == L'"') {
428
+ quote_hit = 1;
429
+ *(target++) = L'\\';
430
+ } else {
431
+ quote_hit = 0;
432
+ }
433
+ }
434
+ target[0] = L'\0';
435
+ wcsrev(start);
436
+ *(target++) = L'"';
437
+ return target;
438
+ }
439
+
440
+
441
+ wchar_t* make_program_args(char** args, int verbatim_arguments) {
442
+ wchar_t* dst;
443
+ wchar_t* ptr;
444
+ char** arg;
445
+ size_t size = 0;
446
+ size_t len;
447
+ int arg_count = 0;
448
+ wchar_t* buffer;
449
+ int arg_size;
450
+ int buffer_size = 0;
451
+
452
+ /* Count the required size. */
453
+ for (arg = args; *arg; arg++) {
454
+ arg_size = uv_utf8_to_utf16(*arg, NULL, 0) * sizeof(wchar_t);
455
+ size += arg_size;
456
+ buffer_size = arg_size > buffer_size ? arg_size : buffer_size;
457
+ arg_count++;
458
+ }
459
+
460
+ /* Adjust for potential quotes. Also assume the worst-case scenario
461
+ /* that every character needs escaping, so we need twice as much space. */
462
+ size = size * 2 + arg_count * 2;
463
+
464
+ dst = (wchar_t*)malloc(size);
465
+ if (!dst) {
466
+ uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
467
+ }
468
+
469
+ buffer = (wchar_t*)malloc(buffer_size);
470
+ if (!buffer) {
471
+ uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
472
+ }
473
+
474
+ ptr = dst;
475
+ for (arg = args; *arg; arg++) {
476
+ len = uv_utf8_to_utf16(*arg, buffer, (size_t)(size - (ptr - dst)));
477
+ if (!len) {
478
+ goto error;
479
+ }
480
+ if (verbatim_arguments) {
481
+ wcscpy(ptr, buffer);
482
+ ptr += len - 1;
483
+ } else {
484
+ ptr = quote_cmd_arg(buffer, ptr);
485
+ }
486
+ *ptr++ = *(arg + 1) ? L' ' : L'\0';
487
+ }
488
+
489
+ free(buffer);
490
+ return dst;
491
+
492
+ error:
493
+ free(dst);
494
+ free(buffer);
495
+ return NULL;
496
+ }
497
+
498
+
499
+ /*
500
+ * If we learn that people are passing in huge environment blocks
501
+ * then we should probably qsort() the array and then bsearch()
502
+ * to see if it contains this variable. But there are ownership
503
+ * issues associated with that solution; this is the caller's
504
+ * char**, and modifying it is rude.
505
+ */
506
+ static void check_required_vars_contains_var(env_var_t* required, int size,
507
+ const char* var) {
508
+ int i;
509
+ for (i = 0; i < size; ++i) {
510
+ if (_strnicmp(required[i].narrow, var, required[i].len) == 0) {
511
+ required[i].supplied = 1;
512
+ return;
513
+ }
514
+ }
515
+ }
516
+
517
+
518
+ /*
519
+ * The way windows takes environment variables is different than what C does;
520
+ * Windows wants a contiguous block of null-terminated strings, terminated
521
+ * with an additional null.
522
+ *
523
+ * Windows has a few "essential" environment variables. winsock will fail
524
+ * to initialize if SYSTEMROOT is not defined; some APIs make reference to
525
+ * TEMP. SYSTEMDRIVE is probably also important. We therefore ensure that
526
+ * these get defined if the input environment block does not contain any
527
+ * values for them.
528
+ */
529
+ wchar_t* make_program_env(char** env_block) {
530
+ wchar_t* dst;
531
+ wchar_t* ptr;
532
+ char** env;
533
+ int env_len = 1 * sizeof(wchar_t); /* room for closing null */
534
+ int len;
535
+ int i;
536
+ DWORD var_size;
537
+
538
+ env_var_t required_vars[] = {
539
+ E_V("SYSTEMROOT"),
540
+ E_V("SYSTEMDRIVE"),
541
+ E_V("TEMP"),
542
+ };
543
+
544
+ for (env = env_block; *env; env++) {
545
+ check_required_vars_contains_var(required_vars,
546
+ ARRAY_SIZE(required_vars),
547
+ *env);
548
+ env_len += (uv_utf8_to_utf16(*env, NULL, 0) * sizeof(wchar_t));
549
+ }
550
+
551
+ for (i = 0; i < ARRAY_SIZE(required_vars); ++i) {
552
+ if (!required_vars[i].supplied) {
553
+ env_len += required_vars[i].len * sizeof(wchar_t);
554
+ var_size = GetEnvironmentVariableW(required_vars[i].wide, NULL, 0);
555
+ if (var_size == 0) {
556
+ uv_fatal_error(GetLastError(), "GetEnvironmentVariableW");
557
+ }
558
+ required_vars[i].value_len = (int)var_size;
559
+ env_len += (int)var_size * sizeof(wchar_t);
560
+ }
561
+ }
562
+
563
+ dst = malloc(env_len);
564
+ if (!dst) {
565
+ uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
566
+ }
567
+
568
+ ptr = dst;
569
+
570
+ for (env = env_block; *env; env++, ptr += len) {
571
+ len = uv_utf8_to_utf16(*env, ptr, (size_t)(env_len - (ptr - dst)));
572
+ if (!len) {
573
+ free(dst);
574
+ return NULL;
575
+ }
576
+ }
577
+
578
+ for (i = 0; i < ARRAY_SIZE(required_vars); ++i) {
579
+ if (!required_vars[i].supplied) {
580
+ wcscpy(ptr, required_vars[i].wide);
581
+ ptr += required_vars[i].len - 1;
582
+ *ptr++ = L'=';
583
+ var_size = GetEnvironmentVariableW(required_vars[i].wide,
584
+ ptr,
585
+ required_vars[i].value_len);
586
+ if (var_size == 0) {
587
+ uv_fatal_error(GetLastError(), "GetEnvironmentVariableW");
588
+ }
589
+ ptr += required_vars[i].value_len;
590
+ }
591
+ }
592
+
593
+ *ptr = L'\0';
594
+ return dst;
595
+ }
596
+
597
+
598
+ /*
599
+ * Called on Windows thread-pool thread to indicate that
600
+ * a child process has exited.
601
+ */
602
+ static void CALLBACK exit_wait_callback(void* data, BOOLEAN didTimeout) {
603
+ uv_process_t* process = (uv_process_t*)data;
604
+ uv_loop_t* loop = process->loop;
605
+
606
+ assert(didTimeout == FALSE);
607
+ assert(process);
608
+
609
+ /* Post completed */
610
+ POST_COMPLETION_FOR_REQ(loop, &process->exit_req);
611
+ }
612
+
613
+
614
+ /*
615
+ * Called on Windows thread-pool thread to indicate that
616
+ * UnregisterWaitEx has completed.
617
+ */
618
+ static void CALLBACK close_wait_callback(void* data, BOOLEAN didTimeout) {
619
+ uv_process_t* process = (uv_process_t*)data;
620
+ uv_loop_t* loop = process->loop;
621
+
622
+ assert(didTimeout == FALSE);
623
+ assert(process);
624
+
625
+ /* Post completed */
626
+ POST_COMPLETION_FOR_REQ(loop, &process->close_req);
627
+ }
628
+
629
+
630
+ /*
631
+ * Called on windows thread pool when CreateProcess failed. It writes an error
632
+ * message to the process' intended stderr and then posts a PROCESS_EXIT
633
+ * packet to the completion port.
634
+ */
635
+ static DWORD WINAPI spawn_failure(void* data) {
636
+ char syscall[] = "CreateProcessW: ";
637
+ char unknown[] = "unknown error\n";
638
+ uv_process_t* process = (uv_process_t*) data;
639
+ uv_loop_t* loop = process->loop;
640
+ HANDLE child_stderr = uv__stdio_handle(process->child_stdio_buffer, 2);
641
+ char* buf = NULL;
642
+ DWORD count, written;
643
+
644
+ if (child_stderr != INVALID_HANDLE_VALUE) {
645
+ WriteFile(child_stderr, syscall, sizeof(syscall) - 1, &written, NULL);
646
+
647
+ count = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
648
+ FORMAT_MESSAGE_FROM_SYSTEM |
649
+ FORMAT_MESSAGE_IGNORE_INSERTS,
650
+ NULL,
651
+ process->spawn_errno,
652
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
653
+ (LPSTR) &buf,
654
+ 0,
655
+ NULL);
656
+
657
+ if (buf != NULL && count > 0) {
658
+ WriteFile(child_stderr, buf, count, &written, NULL);
659
+ LocalFree(buf);
660
+ } else {
661
+ WriteFile(child_stderr, unknown, sizeof(unknown) - 1, &written, NULL);
662
+ }
663
+
664
+ FlushFileBuffers(child_stderr);
665
+ }
666
+
667
+ /* Post completed */
668
+ POST_COMPLETION_FOR_REQ(loop, &process->exit_req);
669
+
670
+ return 0;
671
+ }
672
+
673
+
674
+ /* Called on main thread after a child process has exited. */
675
+ void uv_process_proc_exit(uv_loop_t* loop, uv_process_t* handle) {
676
+ DWORD exit_code;
677
+
678
+ /* FIXME: race condition. */
679
+ if (handle->flags & UV_HANDLE_CLOSING) {
680
+ return;
681
+ }
682
+
683
+ /* Unregister from process notification. */
684
+ if (handle->wait_handle != INVALID_HANDLE_VALUE) {
685
+ UnregisterWait(handle->wait_handle);
686
+ handle->wait_handle = INVALID_HANDLE_VALUE;
687
+ }
688
+
689
+ if (handle->process_handle == INVALID_HANDLE_VALUE ||
690
+ !GetExitCodeProcess(handle->process_handle, &exit_code)) {
691
+ /* The process never even started in the first place, or we were unable */
692
+ /* to obtain the exit code. */
693
+ exit_code = 127;
694
+ }
695
+
696
+ /* Set the handle to inactive: no callbacks will be made after the exit */
697
+ /* callback.*/
698
+ uv__handle_stop(handle);
699
+
700
+ /* Fire the exit callback. */
701
+ if (handle->exit_cb) {
702
+ handle->exit_cb(handle, exit_code, handle->exit_signal);
703
+ }
704
+ }
705
+
706
+
707
+ /* Called on main thread after UnregisterWaitEx finishes. */
708
+ void uv_process_proc_close(uv_loop_t* loop, uv_process_t* handle) {
709
+ uv_want_endgame(loop, (uv_handle_t*)handle);
710
+ }
711
+
712
+
713
+ void uv_process_close(uv_loop_t* loop, uv_process_t* handle) {
714
+ uv__handle_start(handle);
715
+
716
+ if (handle->wait_handle != INVALID_HANDLE_VALUE) {
717
+ handle->close_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
718
+ UnregisterWaitEx(handle->wait_handle, handle->close_handle);
719
+ handle->wait_handle = NULL;
720
+
721
+ RegisterWaitForSingleObject(&handle->wait_handle, handle->close_handle,
722
+ close_wait_callback, (void*)handle, INFINITE,
723
+ WT_EXECUTEINWAITTHREAD | WT_EXECUTEONLYONCE);
724
+ } else {
725
+ uv_want_endgame(loop, (uv_handle_t*)handle);
726
+ }
727
+ }
728
+
729
+
730
+ void uv_process_endgame(uv_loop_t* loop, uv_process_t* handle) {
731
+ if (handle->flags & UV_HANDLE_CLOSING) {
732
+ assert(!(handle->flags & UV_HANDLE_CLOSED));
733
+ uv__handle_stop(handle);
734
+
735
+ /* Clean-up the process handle. */
736
+ CloseHandle(handle->process_handle);
737
+
738
+ /* Clean up the child stdio ends that may have been left open. */
739
+ if (handle->child_stdio_buffer != NULL) {
740
+ uv__stdio_destroy(handle->child_stdio_buffer);
741
+ }
742
+
743
+ uv__handle_close(handle);
744
+ }
745
+ }
746
+
747
+
748
+ int uv_spawn(uv_loop_t* loop, uv_process_t* process,
749
+ uv_process_options_t options) {
750
+ int i, size, err = 0, keep_child_stdio_open = 0;
751
+ wchar_t* path = NULL;
752
+ BOOL result;
753
+ wchar_t* application_path = NULL, *application = NULL, *arguments = NULL,
754
+ *env = NULL, *cwd = NULL;
755
+ STARTUPINFOW startup;
756
+ PROCESS_INFORMATION info;
757
+ DWORD process_flags;
758
+
759
+ if (options.flags & (UV_PROCESS_SETGID | UV_PROCESS_SETUID)) {
760
+ uv__set_artificial_error(loop, UV_ENOTSUP);
761
+ return -1;
762
+ }
763
+
764
+ assert(options.file != NULL);
765
+ assert(!(options.flags & ~(UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS |
766
+ UV_PROCESS_DETACHED |
767
+ UV_PROCESS_SETGID |
768
+ UV_PROCESS_SETUID)));
769
+
770
+ uv_process_init(loop, process);
771
+
772
+ process->exit_cb = options.exit_cb;
773
+ UTF8_TO_UTF16(options.file, application);
774
+ arguments = options.args ? make_program_args(options.args,
775
+ options.flags & UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS) : NULL;
776
+ env = options.env ? make_program_env(options.env) : NULL;
777
+
778
+ if (options.cwd) {
779
+ UTF8_TO_UTF16(options.cwd, cwd);
780
+ } else {
781
+ size = GetCurrentDirectoryW(0, NULL) * sizeof(wchar_t);
782
+ if (size) {
783
+ cwd = (wchar_t*)malloc(size);
784
+ if (!cwd) {
785
+ uv__set_artificial_error(loop, UV_ENOMEM);
786
+ err = -1;
787
+ goto done;
788
+ }
789
+
790
+ GetCurrentDirectoryW(size, cwd);
791
+ } else {
792
+ uv__set_sys_error(loop, GetLastError());
793
+ err = -1;
794
+ goto done;
795
+ }
796
+ }
797
+
798
+ /* Get PATH env. variable. */
799
+ size = GetEnvironmentVariableW(L"PATH", NULL, 0) + 1;
800
+ path = (wchar_t*)malloc(size * sizeof(wchar_t));
801
+ if (!path) {
802
+ uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
803
+ }
804
+ GetEnvironmentVariableW(L"PATH", path, size * sizeof(wchar_t));
805
+ path[size - 1] = L'\0';
806
+
807
+ application_path = search_path(application,
808
+ cwd,
809
+ path);
810
+
811
+ if (!application_path) {
812
+ /* CreateProcess will fail, but this allows us to pass this error to */
813
+ /* the user asynchronously. */
814
+ application_path = application;
815
+ }
816
+
817
+
818
+ if (uv__stdio_create(loop, &options, &process->child_stdio_buffer) < 0) {
819
+ err = -1;
820
+ goto done;
821
+ }
822
+
823
+ startup.cb = sizeof(startup);
824
+ startup.lpReserved = NULL;
825
+ startup.lpDesktop = NULL;
826
+ startup.lpTitle = NULL;
827
+ startup.dwFlags = STARTF_USESTDHANDLES;
828
+ startup.cbReserved2 = uv__stdio_size(process->child_stdio_buffer);
829
+ startup.lpReserved2 = (BYTE*) process->child_stdio_buffer;
830
+ startup.hStdInput = uv__stdio_handle(process->child_stdio_buffer, 0);
831
+ startup.hStdOutput = uv__stdio_handle(process->child_stdio_buffer, 1);
832
+ startup.hStdError = uv__stdio_handle(process->child_stdio_buffer, 2);
833
+
834
+ process_flags = CREATE_UNICODE_ENVIRONMENT;
835
+ if (options.flags & UV_PROCESS_DETACHED) {
836
+ process_flags |= DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP;
837
+ }
838
+
839
+ if (CreateProcessW(application_path,
840
+ arguments,
841
+ NULL,
842
+ NULL,
843
+ 1,
844
+ process_flags,
845
+ env,
846
+ cwd,
847
+ &startup,
848
+ &info)) {
849
+ /* Spawn succeeded */
850
+ process->process_handle = info.hProcess;
851
+ process->pid = info.dwProcessId;
852
+
853
+ /* Set IPC pid to all IPC pipes. */
854
+ for (i = 0; i < options.stdio_count; i++) {
855
+ const uv_stdio_container_t* fdopt = &options.stdio[i];
856
+ if (fdopt->flags & UV_CREATE_PIPE &&
857
+ fdopt->data.stream->type == UV_NAMED_PIPE &&
858
+ ((uv_pipe_t*) fdopt->data.stream)->ipc) {
859
+ ((uv_pipe_t*) fdopt->data.stream)->ipc_pid = info.dwProcessId;
860
+ }
861
+ }
862
+
863
+ /* Setup notifications for when the child process exits. */
864
+ result = RegisterWaitForSingleObject(&process->wait_handle,
865
+ process->process_handle, exit_wait_callback, (void*)process, INFINITE,
866
+ WT_EXECUTEINWAITTHREAD | WT_EXECUTEONLYONCE);
867
+ if (!result) {
868
+ uv_fatal_error(GetLastError(), "RegisterWaitForSingleObject");
869
+ }
870
+
871
+ CloseHandle(info.hThread);
872
+
873
+ } else {
874
+ /* CreateProcessW failed, but this failure should be delivered */
875
+ /* asynchronously to retain unix compatibility. So pretend spawn */
876
+ /* succeeded, and start a thread instead that prints an error */
877
+ /* to the child's intended stderr. */
878
+ process->spawn_errno = GetLastError();
879
+ keep_child_stdio_open = 1;
880
+ if (!QueueUserWorkItem(spawn_failure, process, WT_EXECUTEDEFAULT)) {
881
+ uv_fatal_error(GetLastError(), "QueueUserWorkItem");
882
+ }
883
+ }
884
+
885
+ done:
886
+ free(application);
887
+ if (application_path != application) {
888
+ free(application_path);
889
+ }
890
+ free(arguments);
891
+ free(cwd);
892
+ free(env);
893
+ free(path);
894
+
895
+ /* Under normal circumstances we should close the stdio handles now - the */
896
+ /* the child now has its own duplicates, or something went horribly wrong */
897
+ /* The only exception is when CreateProcess has failed, then we actually */
898
+ /* need to keep the stdio handles to report the error asynchronously. */
899
+ if (process->child_stdio_buffer == NULL) {
900
+ /* Something went wrong before child stdio was initialized. */
901
+ } else if (!keep_child_stdio_open) {
902
+ uv__stdio_destroy(process->child_stdio_buffer);
903
+ process->child_stdio_buffer = NULL;
904
+ } else {
905
+ /* We're keeping the handles open, the thread pool is going to have */
906
+ /* it's way with them. But at least make them non-inheritable. */
907
+ uv__stdio_noinherit(process->child_stdio_buffer);
908
+ }
909
+
910
+ if (err == 0) {
911
+ /* Spawn was succesful. The handle will be active until the exit */
912
+ /* is made or the handle is closed, whichever happens first. */
913
+ uv__handle_start(process);
914
+ } else {
915
+ /* Spawn was not successful. Clean up. */
916
+ if (process->wait_handle != INVALID_HANDLE_VALUE) {
917
+ UnregisterWait(process->wait_handle);
918
+ process->wait_handle = INVALID_HANDLE_VALUE;
919
+ }
920
+
921
+ if (process->process_handle != INVALID_HANDLE_VALUE) {
922
+ CloseHandle(process->process_handle);
923
+ process->process_handle = INVALID_HANDLE_VALUE;
924
+ }
925
+ }
926
+
927
+ return err;
928
+ }
929
+
930
+
931
+ static uv_err_t uv__kill(HANDLE process_handle, int signum) {
932
+ switch (signum) {
933
+ case SIGTERM:
934
+ case SIGKILL:
935
+ case SIGINT: {
936
+ /* Unconditionally terminate the process. On Windows, killed processes */
937
+ /* normally return 1. */
938
+ DWORD error, status;
939
+
940
+ if (TerminateProcess(process_handle, 1))
941
+ return uv_ok_;
942
+
943
+ /* If the process already exited before TerminateProcess was called, */
944
+ /* TerminateProcess will fail with ERROR_ACESS_DENIED. */
945
+ error = GetLastError();
946
+ if (error == ERROR_ACCESS_DENIED &&
947
+ GetExitCodeProcess(process_handle, &status) &&
948
+ status != STILL_ACTIVE) {
949
+ return uv__new_artificial_error(UV_ESRCH);
950
+ }
951
+
952
+ return uv__new_sys_error(error);
953
+ }
954
+
955
+ case 0: {
956
+ /* Health check: is the process still alive? */
957
+ DWORD status;
958
+
959
+ if (!GetExitCodeProcess(process_handle, &status))
960
+ return uv__new_sys_error(GetLastError());
961
+
962
+ if (status != STILL_ACTIVE)
963
+ return uv__new_artificial_error(UV_ESRCH);
964
+
965
+ return uv_ok_;
966
+ }
967
+
968
+ default:
969
+ /* Unsupported signal. */
970
+ return uv__new_artificial_error(UV_ENOSYS);
971
+ }
972
+ }
973
+
974
+
975
+ int uv_process_kill(uv_process_t* process, int signum) {
976
+ uv_err_t err;
977
+
978
+ if (process->process_handle == INVALID_HANDLE_VALUE) {
979
+ uv__set_artificial_error(process->loop, UV_EINVAL);
980
+ return -1;
981
+ }
982
+
983
+ err = uv__kill(process->process_handle, signum);
984
+
985
+ if (err.code != UV_OK) {
986
+ uv__set_error(process->loop, err.code, err.sys_errno_);
987
+ return -1;
988
+ }
989
+
990
+ process->exit_signal = signum;
991
+
992
+ return 0;
993
+ }
994
+
995
+
996
+ uv_err_t uv_kill(int pid, int signum) {
997
+ uv_err_t err;
998
+ HANDLE process_handle = OpenProcess(PROCESS_TERMINATE |
999
+ PROCESS_QUERY_INFORMATION, FALSE, pid);
1000
+
1001
+ if (process_handle == NULL) {
1002
+ if (GetLastError() == ERROR_INVALID_PARAMETER) {
1003
+ return uv__new_artificial_error(UV_ESRCH);
1004
+ } else {
1005
+ return uv__new_sys_error(GetLastError());
1006
+ }
1007
+ }
1008
+
1009
+ err = uv__kill(process_handle, signum);
1010
+ CloseHandle(process_handle);
1011
+
1012
+ return err;
1013
+ }