uv-win.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. #ifndef _WIN32_WINNT
  22. # define _WIN32_WINNT 0x0502
  23. #endif
  24. #if !defined(_SSIZE_T_) && !defined(_SSIZE_T_DEFINED)
  25. typedef intptr_t ssize_t;
  26. # define _SSIZE_T_
  27. # define _SSIZE_T_DEFINED
  28. #endif
  29. #include <winsock2.h>
  30. #include <mswsock.h>
  31. #include <ws2tcpip.h>
  32. #include <windows.h>
  33. #include <process.h>
  34. #include <signal.h>
  35. #include <sys/stat.h>
  36. #if defined(_MSC_VER) && _MSC_VER < 1600
  37. # include "uv-private/stdint-msvc2008.h"
  38. #else
  39. # include <stdint.h>
  40. #endif
  41. #include "tree.h"
  42. #include "ngx-queue.h"
  43. #define MAX_PIPENAME_LEN 256
  44. #ifndef S_IFLNK
  45. # define S_IFLNK 0xA000
  46. #endif
  47. /* Additional signals supported by uv_signal and or uv_kill. The CRT defines
  48. * the following signals already:
  49. *
  50. * #define SIGINT 2
  51. * #define SIGILL 4
  52. * #define SIGABRT_COMPAT 6
  53. * #define SIGFPE 8
  54. * #define SIGSEGV 11
  55. * #define SIGTERM 15
  56. * #define SIGBREAK 21
  57. * #define SIGABRT 22
  58. *
  59. * The additional signals have values that are common on other Unix
  60. * variants (Linux and Darwin)
  61. */
  62. #define SIGHUP 1
  63. #define SIGKILL 9
  64. #define SIGWINCH 28
  65. /* The CRT defines SIGABRT_COMPAT as 6, which equals SIGABRT on many */
  66. /* unix-like platforms. However MinGW doesn't define it, so we do. */
  67. #ifndef SIGABRT_COMPAT
  68. # define SIGABRT_COMPAT 6
  69. #endif
  70. /*
  71. * Guids and typedefs for winsock extension functions
  72. * Mingw32 doesn't have these :-(
  73. */
  74. #ifndef WSAID_ACCEPTEX
  75. # define WSAID_ACCEPTEX \
  76. {0xb5367df1, 0xcbac, 0x11cf, \
  77. {0x95, 0xca, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92}}
  78. # define WSAID_CONNECTEX \
  79. {0x25a207b9, 0xddf3, 0x4660, \
  80. {0x8e, 0xe9, 0x76, 0xe5, 0x8c, 0x74, 0x06, 0x3e}}
  81. # define WSAID_GETACCEPTEXSOCKADDRS \
  82. {0xb5367df2, 0xcbac, 0x11cf, \
  83. {0x95, 0xca, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92}}
  84. # define WSAID_DISCONNECTEX \
  85. {0x7fda2e11, 0x8630, 0x436f, \
  86. {0xa0, 0x31, 0xf5, 0x36, 0xa6, 0xee, 0xc1, 0x57}}
  87. # define WSAID_TRANSMITFILE \
  88. {0xb5367df0, 0xcbac, 0x11cf, \
  89. {0x95, 0xca, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92}}
  90. typedef BOOL PASCAL (*LPFN_ACCEPTEX)
  91. (SOCKET sListenSocket,
  92. SOCKET sAcceptSocket,
  93. PVOID lpOutputBuffer,
  94. DWORD dwReceiveDataLength,
  95. DWORD dwLocalAddressLength,
  96. DWORD dwRemoteAddressLength,
  97. LPDWORD lpdwBytesReceived,
  98. LPOVERLAPPED lpOverlapped);
  99. typedef BOOL PASCAL (*LPFN_CONNECTEX)
  100. (SOCKET s,
  101. const struct sockaddr* name,
  102. int namelen,
  103. PVOID lpSendBuffer,
  104. DWORD dwSendDataLength,
  105. LPDWORD lpdwBytesSent,
  106. LPOVERLAPPED lpOverlapped);
  107. typedef void PASCAL (*LPFN_GETACCEPTEXSOCKADDRS)
  108. (PVOID lpOutputBuffer,
  109. DWORD dwReceiveDataLength,
  110. DWORD dwLocalAddressLength,
  111. DWORD dwRemoteAddressLength,
  112. LPSOCKADDR* LocalSockaddr,
  113. LPINT LocalSockaddrLength,
  114. LPSOCKADDR* RemoteSockaddr,
  115. LPINT RemoteSockaddrLength);
  116. typedef BOOL PASCAL (*LPFN_DISCONNECTEX)
  117. (SOCKET hSocket,
  118. LPOVERLAPPED lpOverlapped,
  119. DWORD dwFlags,
  120. DWORD reserved);
  121. typedef BOOL PASCAL (*LPFN_TRANSMITFILE)
  122. (SOCKET hSocket,
  123. HANDLE hFile,
  124. DWORD nNumberOfBytesToWrite,
  125. DWORD nNumberOfBytesPerSend,
  126. LPOVERLAPPED lpOverlapped,
  127. LPTRANSMIT_FILE_BUFFERS lpTransmitBuffers,
  128. DWORD dwFlags);
  129. typedef PVOID RTL_SRWLOCK;
  130. typedef RTL_SRWLOCK SRWLOCK, *PSRWLOCK;
  131. #endif
  132. typedef int (WSAAPI* LPFN_WSARECV)
  133. (SOCKET socket,
  134. LPWSABUF buffers,
  135. DWORD buffer_count,
  136. LPDWORD bytes,
  137. LPDWORD flags,
  138. LPWSAOVERLAPPED overlapped,
  139. LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_routine);
  140. typedef int (WSAAPI* LPFN_WSARECVFROM)
  141. (SOCKET socket,
  142. LPWSABUF buffers,
  143. DWORD buffer_count,
  144. LPDWORD bytes,
  145. LPDWORD flags,
  146. struct sockaddr* addr,
  147. LPINT addr_len,
  148. LPWSAOVERLAPPED overlapped,
  149. LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_routine);
  150. #ifndef _NTDEF_
  151. typedef LONG NTSTATUS;
  152. typedef NTSTATUS *PNTSTATUS;
  153. #endif
  154. #ifndef RTL_CONDITION_VARIABLE_INIT
  155. typedef PVOID CONDITION_VARIABLE, *PCONDITION_VARIABLE;
  156. #endif
  157. typedef struct _AFD_POLL_HANDLE_INFO {
  158. HANDLE Handle;
  159. ULONG Events;
  160. NTSTATUS Status;
  161. } AFD_POLL_HANDLE_INFO, *PAFD_POLL_HANDLE_INFO;
  162. typedef struct _AFD_POLL_INFO {
  163. LARGE_INTEGER Timeout;
  164. ULONG NumberOfHandles;
  165. ULONG Exclusive;
  166. AFD_POLL_HANDLE_INFO Handles[1];
  167. } AFD_POLL_INFO, *PAFD_POLL_INFO;
  168. #define UV_MSAFD_PROVIDER_COUNT 3
  169. /**
  170. * It should be possible to cast uv_buf_t[] to WSABUF[]
  171. * see http://msdn.microsoft.com/en-us/library/ms741542(v=vs.85).aspx
  172. */
  173. typedef struct uv_buf_t {
  174. ULONG len;
  175. char* base;
  176. } uv_buf_t;
  177. typedef int uv_file;
  178. typedef struct _stati64 uv_statbuf_t;
  179. typedef SOCKET uv_os_sock_t;
  180. typedef HANDLE uv_thread_t;
  181. typedef HANDLE uv_sem_t;
  182. typedef CRITICAL_SECTION uv_mutex_t;
  183. /* This condition variable implementation is based on the SetEvent solution
  184. * (section 3.2) at http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
  185. * We could not use the SignalObjectAndWait solution (section 3.4) because
  186. * it want the 2nd argument (type uv_mutex_t) of uv_cond_wait() and
  187. * uv_cond_timedwait() to be HANDLEs, but we use CRITICAL_SECTIONs.
  188. */
  189. typedef union {
  190. CONDITION_VARIABLE cond_var;
  191. struct {
  192. unsigned int waiters_count;
  193. CRITICAL_SECTION waiters_count_lock;
  194. HANDLE signal_event;
  195. HANDLE broadcast_event;
  196. } fallback;
  197. } uv_cond_t;
  198. typedef union {
  199. /* srwlock_ has type SRWLOCK, but not all toolchains define this type in */
  200. /* windows.h. */
  201. SRWLOCK srwlock_;
  202. struct {
  203. uv_mutex_t read_mutex_;
  204. uv_mutex_t write_mutex_;
  205. unsigned int num_readers_;
  206. } fallback_;
  207. } uv_rwlock_t;
  208. typedef struct {
  209. unsigned int n;
  210. unsigned int count;
  211. uv_mutex_t mutex;
  212. uv_sem_t turnstile1;
  213. uv_sem_t turnstile2;
  214. } uv_barrier_t;
  215. #define UV_ONCE_INIT { 0, NULL }
  216. typedef struct uv_once_s {
  217. unsigned char ran;
  218. HANDLE event;
  219. } uv_once_t;
  220. /* Platform-specific definitions for uv_spawn support. */
  221. typedef unsigned char uv_uid_t;
  222. typedef unsigned char uv_gid_t;
  223. /* Platform-specific definitions for uv_dlopen support. */
  224. #define UV_DYNAMIC FAR WINAPI
  225. typedef struct {
  226. HMODULE handle;
  227. char* errmsg;
  228. } uv_lib_t;
  229. RB_HEAD(uv_timer_tree_s, uv_timer_s);
  230. #define UV_LOOP_PRIVATE_FIELDS \
  231. /* The loop's I/O completion port */ \
  232. HANDLE iocp; \
  233. /* The current time according to the event loop. in msecs. */ \
  234. uint64_t time; \
  235. /* Tail of a single-linked circular queue of pending reqs. If the queue */ \
  236. /* is empty, tail_ is NULL. If there is only one item, */ \
  237. /* tail_->next_req == tail_ */ \
  238. uv_req_t* pending_reqs_tail; \
  239. /* Head of a single-linked list of closed handles */ \
  240. uv_handle_t* endgame_handles; \
  241. /* The head of the timers tree */ \
  242. struct uv_timer_tree_s timers; \
  243. /* Lists of active loop (prepare / check / idle) watchers */ \
  244. uv_prepare_t* prepare_handles; \
  245. uv_check_t* check_handles; \
  246. uv_idle_t* idle_handles; \
  247. /* This pointer will refer to the prepare/check/idle handle whose */ \
  248. /* callback is scheduled to be called next. This is needed to allow */ \
  249. /* safe removal from one of the lists above while that list being */ \
  250. /* iterated over. */ \
  251. uv_prepare_t* next_prepare_handle; \
  252. uv_check_t* next_check_handle; \
  253. uv_idle_t* next_idle_handle; \
  254. /* This handle holds the peer sockets for the fast variant of uv_poll_t */ \
  255. SOCKET poll_peer_sockets[UV_MSAFD_PROVIDER_COUNT]; \
  256. /* Counter to keep track of active tcp streams */ \
  257. unsigned int active_tcp_streams; \
  258. /* Counter to keep track of active udp streams */ \
  259. unsigned int active_udp_streams; \
  260. /* Counter to started timer */ \
  261. uint64_t timer_counter;
  262. #define UV_REQ_TYPE_PRIVATE \
  263. /* TODO: remove the req suffix */ \
  264. UV_ACCEPT, \
  265. UV_FS_EVENT_REQ, \
  266. UV_POLL_REQ, \
  267. UV_PROCESS_EXIT, \
  268. UV_READ, \
  269. UV_UDP_RECV, \
  270. UV_WAKEUP, \
  271. UV_SIGNAL_REQ,
  272. #define UV_REQ_PRIVATE_FIELDS \
  273. union { \
  274. /* Used by I/O operations */ \
  275. struct { \
  276. OVERLAPPED overlapped; \
  277. size_t queued_bytes; \
  278. }; \
  279. }; \
  280. struct uv_req_s* next_req;
  281. #define UV_WRITE_PRIVATE_FIELDS \
  282. int ipc_header; \
  283. uv_buf_t write_buffer; \
  284. HANDLE event_handle; \
  285. HANDLE wait_handle;
  286. #define UV_CONNECT_PRIVATE_FIELDS \
  287. /* empty */
  288. #define UV_SHUTDOWN_PRIVATE_FIELDS \
  289. /* empty */
  290. #define UV_UDP_SEND_PRIVATE_FIELDS \
  291. /* empty */
  292. #define UV_PRIVATE_REQ_TYPES \
  293. typedef struct uv_pipe_accept_s { \
  294. UV_REQ_FIELDS \
  295. HANDLE pipeHandle; \
  296. struct uv_pipe_accept_s* next_pending; \
  297. } uv_pipe_accept_t; \
  298. \
  299. typedef struct uv_tcp_accept_s { \
  300. UV_REQ_FIELDS \
  301. SOCKET accept_socket; \
  302. char accept_buffer[sizeof(struct sockaddr_storage) * 2 + 32]; \
  303. HANDLE event_handle; \
  304. HANDLE wait_handle; \
  305. struct uv_tcp_accept_s* next_pending; \
  306. } uv_tcp_accept_t; \
  307. \
  308. typedef struct uv_read_s { \
  309. UV_REQ_FIELDS \
  310. HANDLE event_handle; \
  311. HANDLE wait_handle; \
  312. } uv_read_t;
  313. #define uv_stream_connection_fields \
  314. unsigned int write_reqs_pending; \
  315. uv_shutdown_t* shutdown_req;
  316. #define uv_stream_server_fields \
  317. uv_connection_cb connection_cb;
  318. #define UV_STREAM_PRIVATE_FIELDS \
  319. unsigned int reqs_pending; \
  320. int activecnt; \
  321. uv_read_t read_req; \
  322. union { \
  323. struct { uv_stream_connection_fields }; \
  324. struct { uv_stream_server_fields }; \
  325. };
  326. #define uv_tcp_server_fields \
  327. uv_tcp_accept_t* accept_reqs; \
  328. unsigned int processed_accepts; \
  329. uv_tcp_accept_t* pending_accepts; \
  330. LPFN_ACCEPTEX func_acceptex;
  331. #define uv_tcp_connection_fields \
  332. uv_buf_t read_buffer; \
  333. LPFN_CONNECTEX func_connectex;
  334. #define UV_TCP_PRIVATE_FIELDS \
  335. SOCKET socket; \
  336. int bind_error; \
  337. union { \
  338. struct { uv_tcp_server_fields }; \
  339. struct { uv_tcp_connection_fields }; \
  340. };
  341. #define UV_UDP_PRIVATE_FIELDS \
  342. SOCKET socket; \
  343. unsigned int reqs_pending; \
  344. int activecnt; \
  345. uv_req_t recv_req; \
  346. uv_buf_t recv_buffer; \
  347. struct sockaddr_storage recv_from; \
  348. int recv_from_len; \
  349. uv_udp_recv_cb recv_cb; \
  350. uv_alloc_cb alloc_cb; \
  351. LPFN_WSARECV func_wsarecv; \
  352. LPFN_WSARECVFROM func_wsarecvfrom;
  353. #define uv_pipe_server_fields \
  354. int pending_instances; \
  355. uv_pipe_accept_t* accept_reqs; \
  356. uv_pipe_accept_t* pending_accepts;
  357. #define uv_pipe_connection_fields \
  358. uv_timer_t* eof_timer; \
  359. uv_write_t ipc_header_write_req; \
  360. int ipc_pid; \
  361. uint64_t remaining_ipc_rawdata_bytes; \
  362. unsigned char reserved[sizeof(void*)]; \
  363. struct { \
  364. WSAPROTOCOL_INFOW* socket_info; \
  365. int tcp_connection; \
  366. } pending_ipc_info; \
  367. uv_write_t* non_overlapped_writes_tail;
  368. #define UV_PIPE_PRIVATE_FIELDS \
  369. HANDLE handle; \
  370. WCHAR* name; \
  371. union { \
  372. struct { uv_pipe_server_fields }; \
  373. struct { uv_pipe_connection_fields }; \
  374. };
  375. /* TODO: put the parser states in an union - TTY handles are always */
  376. /* half-duplex so read-state can safely overlap write-state. */
  377. #define UV_TTY_PRIVATE_FIELDS \
  378. HANDLE handle; \
  379. union { \
  380. struct { \
  381. /* Used for readable TTY handles */ \
  382. HANDLE read_line_handle; \
  383. uv_buf_t read_line_buffer; \
  384. HANDLE read_raw_wait; \
  385. /* Fields used for translating win keystrokes into vt100 characters */ \
  386. char last_key[8]; \
  387. unsigned char last_key_offset; \
  388. unsigned char last_key_len; \
  389. WCHAR last_utf16_high_surrogate; \
  390. INPUT_RECORD last_input_record; \
  391. }; \
  392. struct { \
  393. /* Used for writable TTY handles */ \
  394. /* utf8-to-utf16 conversion state */ \
  395. unsigned int utf8_codepoint; \
  396. unsigned char utf8_bytes_left; \
  397. /* eol conversion state */ \
  398. unsigned char previous_eol; \
  399. /* ansi parser state */ \
  400. unsigned char ansi_parser_state; \
  401. unsigned char ansi_csi_argc; \
  402. unsigned short ansi_csi_argv[4]; \
  403. COORD saved_position; \
  404. WORD saved_attributes; \
  405. }; \
  406. };
  407. #define UV_POLL_PRIVATE_FIELDS \
  408. SOCKET socket; \
  409. /* Used in fast mode */ \
  410. SOCKET peer_socket; \
  411. AFD_POLL_INFO afd_poll_info_1; \
  412. AFD_POLL_INFO afd_poll_info_2; \
  413. /* Used in fast and slow mode. */ \
  414. uv_req_t poll_req_1; \
  415. uv_req_t poll_req_2; \
  416. unsigned char submitted_events_1; \
  417. unsigned char submitted_events_2; \
  418. unsigned char mask_events_1; \
  419. unsigned char mask_events_2; \
  420. unsigned char events;
  421. #define UV_TIMER_PRIVATE_FIELDS \
  422. RB_ENTRY(uv_timer_s) tree_entry; \
  423. uint64_t due; \
  424. uint64_t repeat; \
  425. uint64_t start_id; \
  426. uv_timer_cb timer_cb;
  427. #define UV_ASYNC_PRIVATE_FIELDS \
  428. struct uv_req_s async_req; \
  429. uv_async_cb async_cb; \
  430. /* char to avoid alignment issues */ \
  431. char volatile async_sent;
  432. #define UV_PREPARE_PRIVATE_FIELDS \
  433. uv_prepare_t* prepare_prev; \
  434. uv_prepare_t* prepare_next; \
  435. uv_prepare_cb prepare_cb;
  436. #define UV_CHECK_PRIVATE_FIELDS \
  437. uv_check_t* check_prev; \
  438. uv_check_t* check_next; \
  439. uv_check_cb check_cb;
  440. #define UV_IDLE_PRIVATE_FIELDS \
  441. uv_idle_t* idle_prev; \
  442. uv_idle_t* idle_next; \
  443. uv_idle_cb idle_cb;
  444. #define UV_HANDLE_PRIVATE_FIELDS \
  445. uv_handle_t* endgame_next; \
  446. unsigned int flags;
  447. #define UV_GETADDRINFO_PRIVATE_FIELDS \
  448. uv_getaddrinfo_cb getaddrinfo_cb; \
  449. void* alloc; \
  450. WCHAR* node; \
  451. WCHAR* service; \
  452. struct addrinfoW* hints; \
  453. struct addrinfoW* res; \
  454. int retcode;
  455. #define UV_PROCESS_PRIVATE_FIELDS \
  456. struct uv_process_exit_s { \
  457. UV_REQ_FIELDS \
  458. } exit_req; \
  459. BYTE* child_stdio_buffer; \
  460. uv_err_t spawn_error; \
  461. int exit_signal; \
  462. HANDLE wait_handle; \
  463. HANDLE process_handle; \
  464. volatile char exit_cb_pending;
  465. #define UV_FS_PRIVATE_FIELDS \
  466. int flags; \
  467. DWORD sys_errno_; \
  468. union { \
  469. /* TODO: remove me in 0.9. */ \
  470. WCHAR* pathw; \
  471. int fd; \
  472. }; \
  473. union { \
  474. struct { \
  475. int mode; \
  476. WCHAR* new_pathw; \
  477. int file_flags; \
  478. int fd_out; \
  479. void* buf; \
  480. size_t length; \
  481. int64_t offset; \
  482. }; \
  483. struct { \
  484. double atime; \
  485. double mtime; \
  486. }; \
  487. };
  488. #define UV_WORK_PRIVATE_FIELDS \
  489. #define UV_FS_EVENT_PRIVATE_FIELDS \
  490. struct uv_fs_event_req_s { \
  491. UV_REQ_FIELDS \
  492. } req; \
  493. HANDLE dir_handle; \
  494. int req_pending; \
  495. uv_fs_event_cb cb; \
  496. WCHAR* filew; \
  497. WCHAR* short_filew; \
  498. WCHAR* dirw; \
  499. char* buffer;
  500. #define UV_SIGNAL_PRIVATE_FIELDS \
  501. RB_ENTRY(uv_signal_s) tree_entry; \
  502. struct uv_req_s signal_req; \
  503. unsigned long pending_signum;
  504. int uv_utf16_to_utf8(const WCHAR* utf16Buffer, size_t utf16Size,
  505. char* utf8Buffer, size_t utf8Size);
  506. int uv_utf8_to_utf16(const char* utf8Buffer, WCHAR* utf16Buffer,
  507. size_t utf16Size);