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 #ifndef _WIN32_WINNT
23 # define _WIN32_WINNT   0x0600
24 #endif
25 
26 #if !defined(_SSIZE_T_) && !defined(_SSIZE_T_DEFINED)
27 typedef intptr_t ssize_t;
28 # define SSIZE_MAX INTPTR_MAX
29 # define _SSIZE_T_
30 # define _SSIZE_T_DEFINED
31 #endif
32 
33 #include <winsock2.h>
34 
35 #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
36 typedef struct pollfd {
37   SOCKET fd;
38   short  events;
39   short  revents;
40 } WSAPOLLFD, *PWSAPOLLFD, *LPWSAPOLLFD;
41 #endif
42 
43 #ifndef LOCALE_INVARIANT
44 # define LOCALE_INVARIANT 0x007f
45 #endif
46 
47 #include <mswsock.h>
48 #include <ws2tcpip.h>
49 #include <windows.h>
50 
51 #include <process.h>
52 #include <signal.h>
53 #include <fcntl.h>
54 #include <sys/stat.h>
55 
56 #if defined(_MSC_VER) && _MSC_VER < 1600
57 # include "uv/stdint-msvc2008.h"
58 #else
59 # include <stdint.h>
60 #endif
61 
62 #include "uv/tree.h"
63 #include "uv/threadpool.h"
64 
65 #define MAX_PIPENAME_LEN 256
66 
67 #ifndef S_IFLNK
68 # define S_IFLNK 0xA000
69 #endif
70 
71 /* Additional signals supported by uv_signal and or uv_kill. The CRT defines
72  * the following signals already:
73  *
74  *   #define SIGINT           2
75  *   #define SIGILL           4
76  *   #define SIGABRT_COMPAT   6
77  *   #define SIGFPE           8
78  *   #define SIGSEGV         11
79  *   #define SIGTERM         15
80  *   #define SIGBREAK        21
81  *   #define SIGABRT         22
82  *
83  * The additional signals have values that are common on other Unix
84  * variants (Linux and Darwin)
85  */
86 #define SIGHUP                1
87 #define SIGKILL               9
88 #define SIGWINCH             28
89 
90 /* Redefine NSIG to take SIGWINCH into consideration */
91 #if defined(NSIG) && NSIG <= SIGWINCH
92 # undef NSIG
93 #endif
94 #ifndef NSIG
95 # define NSIG SIGWINCH + 1
96 #endif
97 
98 /* The CRT defines SIGABRT_COMPAT as 6, which equals SIGABRT on many unix-like
99  * platforms. However MinGW doesn't define it, so we do. */
100 #ifndef SIGABRT_COMPAT
101 # define SIGABRT_COMPAT       6
102 #endif
103 
104 /*
105  * Guids and typedefs for winsock extension functions
106  * Mingw32 doesn't have these :-(
107  */
108 #ifndef WSAID_ACCEPTEX
109 # define WSAID_ACCEPTEX                                                       \
110          {0xb5367df1, 0xcbac, 0x11cf,                                         \
111          {0x95, 0xca, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92}}
112 
113 # define WSAID_CONNECTEX                                                      \
114          {0x25a207b9, 0xddf3, 0x4660,                                         \
115          {0x8e, 0xe9, 0x76, 0xe5, 0x8c, 0x74, 0x06, 0x3e}}
116 
117 # define WSAID_GETACCEPTEXSOCKADDRS                                           \
118          {0xb5367df2, 0xcbac, 0x11cf,                                         \
119          {0x95, 0xca, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92}}
120 
121 # define WSAID_DISCONNECTEX                                                   \
122          {0x7fda2e11, 0x8630, 0x436f,                                         \
123          {0xa0, 0x31, 0xf5, 0x36, 0xa6, 0xee, 0xc1, 0x57}}
124 
125 # define WSAID_TRANSMITFILE                                                   \
126          {0xb5367df0, 0xcbac, 0x11cf,                                         \
127          {0x95, 0xca, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92}}
128 
129   typedef BOOL (PASCAL *LPFN_ACCEPTEX)
130                       (SOCKET sListenSocket,
131                        SOCKET sAcceptSocket,
132                        PVOID lpOutputBuffer,
133                        DWORD dwReceiveDataLength,
134                        DWORD dwLocalAddressLength,
135                        DWORD dwRemoteAddressLength,
136                        LPDWORD lpdwBytesReceived,
137                        LPOVERLAPPED lpOverlapped);
138 
139   typedef BOOL (PASCAL *LPFN_CONNECTEX)
140                       (SOCKET s,
141                        const struct sockaddr* name,
142                        int namelen,
143                        PVOID lpSendBuffer,
144                        DWORD dwSendDataLength,
145                        LPDWORD lpdwBytesSent,
146                        LPOVERLAPPED lpOverlapped);
147 
148   typedef void (PASCAL *LPFN_GETACCEPTEXSOCKADDRS)
149                       (PVOID lpOutputBuffer,
150                        DWORD dwReceiveDataLength,
151                        DWORD dwLocalAddressLength,
152                        DWORD dwRemoteAddressLength,
153                        LPSOCKADDR* LocalSockaddr,
154                        LPINT LocalSockaddrLength,
155                        LPSOCKADDR* RemoteSockaddr,
156                        LPINT RemoteSockaddrLength);
157 
158   typedef BOOL (PASCAL *LPFN_DISCONNECTEX)
159                       (SOCKET hSocket,
160                        LPOVERLAPPED lpOverlapped,
161                        DWORD dwFlags,
162                        DWORD reserved);
163 
164   typedef BOOL (PASCAL *LPFN_TRANSMITFILE)
165                       (SOCKET hSocket,
166                        HANDLE hFile,
167                        DWORD nNumberOfBytesToWrite,
168                        DWORD nNumberOfBytesPerSend,
169                        LPOVERLAPPED lpOverlapped,
170                        LPTRANSMIT_FILE_BUFFERS lpTransmitBuffers,
171                        DWORD dwFlags);
172 
173   typedef PVOID RTL_SRWLOCK;
174   typedef RTL_SRWLOCK SRWLOCK, *PSRWLOCK;
175 #endif
176 
177 typedef int (WSAAPI* LPFN_WSARECV)
178             (SOCKET socket,
179              LPWSABUF buffers,
180              DWORD buffer_count,
181              LPDWORD bytes,
182              LPDWORD flags,
183              LPWSAOVERLAPPED overlapped,
184              LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_routine);
185 
186 typedef int (WSAAPI* LPFN_WSARECVFROM)
187             (SOCKET socket,
188              LPWSABUF buffers,
189              DWORD buffer_count,
190              LPDWORD bytes,
191              LPDWORD flags,
192              struct sockaddr* addr,
193              LPINT addr_len,
194              LPWSAOVERLAPPED overlapped,
195              LPWSAOVERLAPPED_COMPLETION_ROUTINE completion_routine);
196 
197 #ifndef _NTDEF_
198   typedef LONG NTSTATUS;
199   typedef NTSTATUS *PNTSTATUS;
200 #endif
201 
202 #ifndef RTL_CONDITION_VARIABLE_INIT
203   typedef PVOID CONDITION_VARIABLE, *PCONDITION_VARIABLE;
204 #endif
205 
206 typedef struct _AFD_POLL_HANDLE_INFO {
207   HANDLE Handle;
208   ULONG Events;
209   NTSTATUS Status;
210 } AFD_POLL_HANDLE_INFO, *PAFD_POLL_HANDLE_INFO;
211 
212 typedef struct _AFD_POLL_INFO {
213   LARGE_INTEGER Timeout;
214   ULONG NumberOfHandles;
215   ULONG Exclusive;
216   AFD_POLL_HANDLE_INFO Handles[1];
217 } AFD_POLL_INFO, *PAFD_POLL_INFO;
218 
219 #define UV_MSAFD_PROVIDER_COUNT 3
220 
221 
222 /**
223  * It should be possible to cast uv_buf_t[] to WSABUF[]
224  * see http://msdn.microsoft.com/en-us/library/ms741542(v=vs.85).aspx
225  */
226 typedef struct uv_buf_t {
227   ULONG len;
228   char* base;
229 } uv_buf_t;
230 
231 typedef int uv_file;
232 typedef SOCKET uv_os_sock_t;
233 typedef HANDLE uv_os_fd_t;
234 typedef int uv_pid_t;
235 
236 typedef HANDLE uv_thread_t;
237 
238 typedef HANDLE uv_sem_t;
239 
240 typedef CRITICAL_SECTION uv_mutex_t;
241 
242 /* This condition variable implementation is based on the SetEvent solution
243  * (section 3.2) at http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
244  * We could not use the SignalObjectAndWait solution (section 3.4) because
245  * it want the 2nd argument (type uv_mutex_t) of uv_cond_wait() and
246  * uv_cond_timedwait() to be HANDLEs, but we use CRITICAL_SECTIONs.
247  */
248 
249 typedef union {
250   CONDITION_VARIABLE cond_var;
251   struct {
252     unsigned int waiters_count;
253     CRITICAL_SECTION waiters_count_lock;
254     HANDLE signal_event;
255     HANDLE broadcast_event;
256   } unused_; /* TODO: retained for ABI compatibility; remove me in v2.x. */
257 } uv_cond_t;
258 
259 typedef union {
260   struct {
261     unsigned int num_readers_;
262     CRITICAL_SECTION num_readers_lock_;
263     HANDLE write_semaphore_;
264   } state_;
265   /* TODO: remove me in v2.x. */
266   struct {
267     SRWLOCK unused_;
268   } unused1_;
269   /* TODO: remove me in v2.x. */
270   struct {
271     uv_mutex_t unused1_;
272     uv_mutex_t unused2_;
273   } unused2_;
274 } uv_rwlock_t;
275 
276 typedef struct {
277   unsigned int n;
278   unsigned int count;
279   uv_mutex_t mutex;
280   uv_sem_t turnstile1;
281   uv_sem_t turnstile2;
282 } uv_barrier_t;
283 
284 typedef struct {
285   DWORD tls_index;
286 } uv_key_t;
287 
288 #define UV_ONCE_INIT { 0, NULL }
289 
290 typedef struct uv_once_s {
291   unsigned char ran;
292   HANDLE event;
293 } uv_once_t;
294 
295 /* Platform-specific definitions for uv_spawn support. */
296 typedef unsigned char uv_uid_t;
297 typedef unsigned char uv_gid_t;
298 
299 typedef struct uv__dirent_s {
300   int d_type;
301   char d_name[1];
302 } uv__dirent_t;
303 
304 #define UV_DIR_PRIVATE_FIELDS \
305   HANDLE dir_handle;          \
306   WIN32_FIND_DATAW find_data; \
307   BOOL need_find_call;
308 
309 #define HAVE_DIRENT_TYPES
310 #define UV__DT_DIR     UV_DIRENT_DIR
311 #define UV__DT_FILE    UV_DIRENT_FILE
312 #define UV__DT_LINK    UV_DIRENT_LINK
313 #define UV__DT_FIFO    UV_DIRENT_FIFO
314 #define UV__DT_SOCKET  UV_DIRENT_SOCKET
315 #define UV__DT_CHAR    UV_DIRENT_CHAR
316 #define UV__DT_BLOCK   UV_DIRENT_BLOCK
317 
318 /* Platform-specific definitions for uv_dlopen support. */
319 #define UV_DYNAMIC FAR WINAPI
320 typedef struct {
321   HMODULE handle;
322   char* errmsg;
323 } uv_lib_t;
324 
325 #define UV_LOOP_PRIVATE_FIELDS                                                \
326     /* The loop's I/O completion port */                                      \
327   HANDLE iocp;                                                                \
328   /* The current time according to the event loop. in msecs. */               \
329   uint64_t time;                                                              \
330   /* Tail of a single-linked circular queue of pending reqs. If the queue */  \
331   /* is empty, tail_ is NULL. If there is only one item, */                   \
332   /* tail_->next_req == tail_ */                                              \
333   uv_req_t* pending_reqs_tail;                                                \
334   /* Head of a single-linked list of closed handles */                        \
335   uv_handle_t* endgame_handles;                                               \
336   /* TODO(bnoordhuis) Stop heap-allocating |timer_heap| in libuv v2.x. */     \
337   void* timer_heap;                                                           \
338     /* Lists of active loop (prepare / check / idle) watchers */              \
339   uv_prepare_t* prepare_handles;                                              \
340   uv_check_t* check_handles;                                                  \
341   uv_idle_t* idle_handles;                                                    \
342   /* This pointer will refer to the prepare/check/idle handle whose */        \
343   /* callback is scheduled to be called next. This is needed to allow */      \
344   /* safe removal from one of the lists above while that list being */        \
345   /* iterated over. */                                                        \
346   uv_prepare_t* next_prepare_handle;                                          \
347   uv_check_t* next_check_handle;                                              \
348   uv_idle_t* next_idle_handle;                                                \
349   /* This handle holds the peer sockets for the fast variant of uv_poll_t */  \
350   SOCKET poll_peer_sockets[UV_MSAFD_PROVIDER_COUNT];                          \
351   /* Counter to keep track of active tcp streams */                           \
352   unsigned int active_tcp_streams;                                            \
353   /* Counter to keep track of active udp streams */                           \
354   unsigned int active_udp_streams;                                            \
355   /* Counter to started timer */                                              \
356   uint64_t timer_counter;                                                     \
357   /* Threadpool */                                                            \
358   void* wq[2];                                                                \
359   uv_mutex_t wq_mutex;                                                        \
360   uv_async_t wq_async;
361 
362 #define UV_REQ_TYPE_PRIVATE                                                   \
363   /* TODO: remove the req suffix */                                           \
364   UV_ACCEPT,                                                                  \
365   UV_FS_EVENT_REQ,                                                            \
366   UV_POLL_REQ,                                                                \
367   UV_PROCESS_EXIT,                                                            \
368   UV_READ,                                                                    \
369   UV_UDP_RECV,                                                                \
370   UV_WAKEUP,                                                                  \
371   UV_SIGNAL_REQ,
372 
373 #define UV_REQ_PRIVATE_FIELDS                                                 \
374   union {                                                                     \
375     /* Used by I/O operations */                                              \
376     struct {                                                                  \
377       OVERLAPPED overlapped;                                                  \
378       size_t queued_bytes;                                                    \
379     } io;                                                                     \
380   } u;                                                                        \
381   struct uv_req_s* next_req;
382 
383 #define UV_WRITE_PRIVATE_FIELDS \
384   int coalesced;                \
385   uv_buf_t write_buffer;        \
386   HANDLE event_handle;          \
387   HANDLE wait_handle;
388 
389 #define UV_CONNECT_PRIVATE_FIELDS                                             \
390   /* empty */
391 
392 #define UV_SHUTDOWN_PRIVATE_FIELDS                                            \
393   /* empty */
394 
395 #define UV_UDP_SEND_PRIVATE_FIELDS                                            \
396   /* empty */
397 
398 #define UV_PRIVATE_REQ_TYPES                                                  \
399   typedef struct uv_pipe_accept_s {                                           \
400     UV_REQ_FIELDS                                                             \
401     HANDLE pipeHandle;                                                        \
402     struct uv_pipe_accept_s* next_pending;                                    \
403   } uv_pipe_accept_t;                                                         \
404                                                                               \
405   typedef struct uv_tcp_accept_s {                                            \
406     UV_REQ_FIELDS                                                             \
407     SOCKET accept_socket;                                                     \
408     char accept_buffer[sizeof(struct sockaddr_storage) * 2 + 32];             \
409     HANDLE event_handle;                                                      \
410     HANDLE wait_handle;                                                       \
411     struct uv_tcp_accept_s* next_pending;                                     \
412   } uv_tcp_accept_t;                                                          \
413                                                                               \
414   typedef struct uv_read_s {                                                  \
415     UV_REQ_FIELDS                                                             \
416     HANDLE event_handle;                                                      \
417     HANDLE wait_handle;                                                       \
418   } uv_read_t;
419 
420 #define uv_stream_connection_fields                                           \
421   unsigned int write_reqs_pending;                                            \
422   uv_shutdown_t* shutdown_req;
423 
424 #define uv_stream_server_fields                                               \
425   uv_connection_cb connection_cb;
426 
427 #define UV_STREAM_PRIVATE_FIELDS                                              \
428   unsigned int reqs_pending;                                                  \
429   int activecnt;                                                              \
430   uv_read_t read_req;                                                         \
431   union {                                                                     \
432     struct { uv_stream_connection_fields } conn;                              \
433     struct { uv_stream_server_fields     } serv;                              \
434   } stream;
435 
436 #define uv_tcp_server_fields                                                  \
437   uv_tcp_accept_t* accept_reqs;                                               \
438   unsigned int processed_accepts;                                             \
439   uv_tcp_accept_t* pending_accepts;                                           \
440   LPFN_ACCEPTEX func_acceptex;
441 
442 #define uv_tcp_connection_fields                                              \
443   uv_buf_t read_buffer;                                                       \
444   LPFN_CONNECTEX func_connectex;
445 
446 #define UV_TCP_PRIVATE_FIELDS                                                 \
447   SOCKET socket;                                                              \
448   int delayed_error;                                                          \
449   union {                                                                     \
450     struct { uv_tcp_server_fields } serv;                                     \
451     struct { uv_tcp_connection_fields } conn;                                 \
452   } tcp;
453 
454 #define UV_UDP_PRIVATE_FIELDS                                                 \
455   SOCKET socket;                                                              \
456   unsigned int reqs_pending;                                                  \
457   int activecnt;                                                              \
458   uv_req_t recv_req;                                                          \
459   uv_buf_t recv_buffer;                                                       \
460   struct sockaddr_storage recv_from;                                          \
461   int recv_from_len;                                                          \
462   uv_udp_recv_cb recv_cb;                                                     \
463   uv_alloc_cb alloc_cb;                                                       \
464   LPFN_WSARECV func_wsarecv;                                                  \
465   LPFN_WSARECVFROM func_wsarecvfrom;
466 
467 #define uv_pipe_server_fields                                                 \
468   int pending_instances;                                                      \
469   uv_pipe_accept_t* accept_reqs;                                              \
470   uv_pipe_accept_t* pending_accepts;
471 
472 #define uv_pipe_connection_fields                                             \
473   uv_timer_t* eof_timer;                                                      \
474   uv_write_t dummy; /* TODO: retained for ABI compat; remove this in v2.x. */ \
475   DWORD ipc_remote_pid;                                                       \
476   union {                                                                     \
477     uint32_t payload_remaining;                                               \
478     uint64_t dummy; /* TODO: retained for ABI compat; remove this in v2.x. */ \
479   } ipc_data_frame;                                                           \
480   void* ipc_xfer_queue[2];                                                    \
481   int ipc_xfer_queue_length;                                                  \
482   uv_write_t* non_overlapped_writes_tail;                                     \
483   CRITICAL_SECTION readfile_thread_lock;                                      \
484   volatile HANDLE readfile_thread_handle;
485 
486 #define UV_PIPE_PRIVATE_FIELDS                                                \
487   HANDLE handle;                                                              \
488   WCHAR* name;                                                                \
489   union {                                                                     \
490     struct { uv_pipe_server_fields } serv;                                    \
491     struct { uv_pipe_connection_fields } conn;                                \
492   } pipe;
493 
494 /* TODO: put the parser states in an union - TTY handles are always half-duplex
495  * so read-state can safely overlap write-state. */
496 #define UV_TTY_PRIVATE_FIELDS                                                 \
497   HANDLE handle;                                                              \
498   union {                                                                     \
499     struct {                                                                  \
500       /* Used for readable TTY handles */                                     \
501       /* TODO: remove me in v2.x. */                                          \
502       HANDLE unused_;                                                         \
503       uv_buf_t read_line_buffer;                                              \
504       HANDLE read_raw_wait;                                                   \
505       /* Fields used for translating win keystrokes into vt100 characters */  \
506       char last_key[8];                                                       \
507       unsigned char last_key_offset;                                          \
508       unsigned char last_key_len;                                             \
509       WCHAR last_utf16_high_surrogate;                                        \
510       INPUT_RECORD last_input_record;                                         \
511     } rd;                                                                     \
512     struct {                                                                  \
513       /* Used for writable TTY handles */                                     \
514       /* utf8-to-utf16 conversion state */                                    \
515       unsigned int utf8_codepoint;                                            \
516       unsigned char utf8_bytes_left;                                          \
517       /* eol conversion state */                                              \
518       unsigned char previous_eol;                                             \
519       /* ansi parser state */                                                 \
520       unsigned char ansi_parser_state;                                        \
521       unsigned char ansi_csi_argc;                                            \
522       unsigned short ansi_csi_argv[4];                                        \
523       COORD saved_position;                                                   \
524       WORD saved_attributes;                                                  \
525     } wr;                                                                     \
526   } tty;
527 
528 #define UV_POLL_PRIVATE_FIELDS                                                \
529   SOCKET socket;                                                              \
530   /* Used in fast mode */                                                     \
531   SOCKET peer_socket;                                                         \
532   AFD_POLL_INFO afd_poll_info_1;                                              \
533   AFD_POLL_INFO afd_poll_info_2;                                              \
534   /* Used in fast and slow mode. */                                           \
535   uv_req_t poll_req_1;                                                        \
536   uv_req_t poll_req_2;                                                        \
537   unsigned char submitted_events_1;                                           \
538   unsigned char submitted_events_2;                                           \
539   unsigned char mask_events_1;                                                \
540   unsigned char mask_events_2;                                                \
541   unsigned char events;
542 
543 #define UV_TIMER_PRIVATE_FIELDS                                               \
544   void* heap_node[3];                                                         \
545   int unused;                                                                 \
546   uint64_t timeout;                                                           \
547   uint64_t repeat;                                                            \
548   uint64_t start_id;                                                          \
549   uv_timer_cb timer_cb;
550 
551 #define UV_ASYNC_PRIVATE_FIELDS                                               \
552   struct uv_req_s async_req;                                                  \
553   uv_async_cb async_cb;                                                       \
554   /* char to avoid alignment issues */                                        \
555   char volatile async_sent;
556 
557 #define UV_PREPARE_PRIVATE_FIELDS                                             \
558   uv_prepare_t* prepare_prev;                                                 \
559   uv_prepare_t* prepare_next;                                                 \
560   uv_prepare_cb prepare_cb;
561 
562 #define UV_CHECK_PRIVATE_FIELDS                                               \
563   uv_check_t* check_prev;                                                     \
564   uv_check_t* check_next;                                                     \
565   uv_check_cb check_cb;
566 
567 #define UV_IDLE_PRIVATE_FIELDS                                                \
568   uv_idle_t* idle_prev;                                                       \
569   uv_idle_t* idle_next;                                                       \
570   uv_idle_cb idle_cb;
571 
572 #define UV_HANDLE_PRIVATE_FIELDS                                              \
573   uv_handle_t* endgame_next;                                                  \
574   unsigned int flags;
575 
576 #define UV_GETADDRINFO_PRIVATE_FIELDS                                         \
577   struct uv__work work_req;                                                   \
578   uv_getaddrinfo_cb getaddrinfo_cb;                                           \
579   void* alloc;                                                                \
580   WCHAR* node;                                                                \
581   WCHAR* service;                                                             \
582   /* The addrinfoW field is used to store a pointer to the hints, and    */   \
583   /* later on to store the result of GetAddrInfoW. The final result will */   \
584   /* be converted to struct addrinfo* and stored in the addrinfo field.  */   \
585   struct addrinfoW* addrinfow;                                                \
586   struct addrinfo* addrinfo;                                                  \
587   int retcode;
588 
589 #define UV_GETNAMEINFO_PRIVATE_FIELDS                                         \
590   struct uv__work work_req;                                                   \
591   uv_getnameinfo_cb getnameinfo_cb;                                           \
592   struct sockaddr_storage storage;                                            \
593   int flags;                                                                  \
594   char host[NI_MAXHOST];                                                      \
595   char service[NI_MAXSERV];                                                   \
596   int retcode;
597 
598 #define UV_PROCESS_PRIVATE_FIELDS                                             \
599   struct uv_process_exit_s {                                                  \
600     UV_REQ_FIELDS                                                             \
601   } exit_req;                                                                 \
602   BYTE* child_stdio_buffer;                                                   \
603   int exit_signal;                                                            \
604   HANDLE wait_handle;                                                         \
605   HANDLE process_handle;                                                      \
606   volatile char exit_cb_pending;
607 
608 #define UV_FS_PRIVATE_FIELDS                                                  \
609   struct uv__work work_req;                                                   \
610   int flags;                                                                  \
611   DWORD sys_errno_;                                                           \
612   union {                                                                     \
613     /* TODO: remove me in 0.9. */                                             \
614     WCHAR* pathw;                                                             \
615     int fd;                                                                   \
616   } file;                                                                     \
617   union {                                                                     \
618     struct {                                                                  \
619       int mode;                                                               \
620       WCHAR* new_pathw;                                                       \
621       int file_flags;                                                         \
622       int fd_out;                                                             \
623       unsigned int nbufs;                                                     \
624       uv_buf_t* bufs;                                                         \
625       int64_t offset;                                                         \
626       uv_buf_t bufsml[4];                                                     \
627     } info;                                                                   \
628     struct {                                                                  \
629       double atime;                                                           \
630       double mtime;                                                           \
631     } time;                                                                   \
632   } fs;
633 
634 #define UV_WORK_PRIVATE_FIELDS                                                \
635   struct uv__work work_req;
636 
637 #define UV_FS_EVENT_PRIVATE_FIELDS                                            \
638   struct uv_fs_event_req_s {                                                  \
639     UV_REQ_FIELDS                                                             \
640   } req;                                                                      \
641   HANDLE dir_handle;                                                          \
642   int req_pending;                                                            \
643   uv_fs_event_cb cb;                                                          \
644   WCHAR* filew;                                                               \
645   WCHAR* short_filew;                                                         \
646   WCHAR* dirw;                                                                \
647   char* buffer;
648 
649 #define UV_SIGNAL_PRIVATE_FIELDS                                              \
650   RB_ENTRY(uv_signal_s) tree_entry;                                           \
651   struct uv_req_s signal_req;                                                 \
652   unsigned long pending_signum;
653 
654 #ifndef F_OK
655 #define F_OK 0
656 #endif
657 #ifndef R_OK
658 #define R_OK 4
659 #endif
660 #ifndef W_OK
661 #define W_OK 2
662 #endif
663 #ifndef X_OK
664 #define X_OK 1
665 #endif
666 
667 /* fs open() flags supported on this platform: */
668 #define UV_FS_O_APPEND       _O_APPEND
669 #define UV_FS_O_CREAT        _O_CREAT
670 #define UV_FS_O_EXCL         _O_EXCL
671 #define UV_FS_O_FILEMAP      0x20000000
672 #define UV_FS_O_RANDOM       _O_RANDOM
673 #define UV_FS_O_RDONLY       _O_RDONLY
674 #define UV_FS_O_RDWR         _O_RDWR
675 #define UV_FS_O_SEQUENTIAL   _O_SEQUENTIAL
676 #define UV_FS_O_SHORT_LIVED  _O_SHORT_LIVED
677 #define UV_FS_O_TEMPORARY    _O_TEMPORARY
678 #define UV_FS_O_TRUNC        _O_TRUNC
679 #define UV_FS_O_WRONLY       _O_WRONLY
680 
681 /* fs open() flags supported on other platforms (or mapped on this platform): */
682 #define UV_FS_O_DIRECT       0x02000000 /* FILE_FLAG_NO_BUFFERING */
683 #define UV_FS_O_DIRECTORY    0
684 #define UV_FS_O_DSYNC        0x04000000 /* FILE_FLAG_WRITE_THROUGH */
685 #define UV_FS_O_EXLOCK       0x10000000 /* EXCLUSIVE SHARING MODE */
686 #define UV_FS_O_NOATIME      0
687 #define UV_FS_O_NOCTTY       0
688 #define UV_FS_O_NOFOLLOW     0
689 #define UV_FS_O_NONBLOCK     0
690 #define UV_FS_O_SYMLINK      0
691 #define UV_FS_O_SYNC         0x08000000 /* FILE_FLAG_WRITE_THROUGH */
692