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