xref: /libuv/include/uv/win.h (revision abc97670)
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 unused;
294   INIT_ONCE init_once;
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     /* The loop's I/O completion port */                                      \
329   HANDLE iocp;                                                                \
330   /* The current time according to the event loop. in msecs. */               \
331   uint64_t time;                                                              \
332   /* Tail of a single-linked circular queue of pending reqs. If the queue */  \
333   /* is empty, tail_ is NULL. If there is only one item, */                   \
334   /* tail_->next_req == tail_ */                                              \
335   uv_req_t* pending_reqs_tail;                                                \
336   /* Head of a single-linked list of closed handles */                        \
337   uv_handle_t* endgame_handles;                                               \
338   /* TODO(bnoordhuis) Stop heap-allocating |timer_heap| in libuv v2.x. */     \
339   void* timer_heap;                                                           \
340     /* Lists of active loop (prepare / check / idle) watchers */              \
341   uv_prepare_t* prepare_handles;                                              \
342   uv_check_t* check_handles;                                                  \
343   uv_idle_t* idle_handles;                                                    \
344   /* This pointer will refer to the prepare/check/idle handle whose */        \
345   /* callback is scheduled to be called next. This is needed to allow */      \
346   /* safe removal from one of the lists above while that list being */        \
347   /* iterated over. */                                                        \
348   uv_prepare_t* next_prepare_handle;                                          \
349   uv_check_t* next_check_handle;                                              \
350   uv_idle_t* next_idle_handle;                                                \
351   /* This handle holds the peer sockets for the fast variant of uv_poll_t */  \
352   SOCKET poll_peer_sockets[UV_MSAFD_PROVIDER_COUNT];                          \
353   /* No longer used. */                                                       \
354   unsigned int active_tcp_streams;                                            \
355   /* No longer used. */                                                       \
356   unsigned int active_udp_streams;                                            \
357   /* Counter to started timer */                                              \
358   uint64_t timer_counter;                                                     \
359   /* Threadpool */                                                            \
360   struct uv__queue wq;                                                        \
361   uv_mutex_t wq_mutex;                                                        \
362   uv_async_t wq_async;
363 
364 #define UV_REQ_TYPE_PRIVATE                                                   \
365   /* TODO: remove the req suffix */                                           \
366   UV_ACCEPT,                                                                  \
367   UV_FS_EVENT_REQ,                                                            \
368   UV_POLL_REQ,                                                                \
369   UV_PROCESS_EXIT,                                                            \
370   UV_READ,                                                                    \
371   UV_UDP_RECV,                                                                \
372   UV_WAKEUP,                                                                  \
373   UV_SIGNAL_REQ,
374 
375 #define UV_REQ_PRIVATE_FIELDS                                                 \
376   union {                                                                     \
377     /* Used by I/O operations */                                              \
378     struct {                                                                  \
379       OVERLAPPED overlapped;                                                  \
380       size_t queued_bytes;                                                    \
381     } io;                                                                     \
382     /* in v2, we can move these to the UV_CONNECT_PRIVATE_FIELDS */           \
383     struct {                                                                  \
384       ULONG_PTR result; /* overlapped.Internal is reused to hold the result */\
385       HANDLE pipeHandle;                                                      \
386       DWORD duplex_flags;                                                     \
387       WCHAR* name;                                                             \
388     } connect;                                                                \
389   } u;                                                                        \
390   struct uv_req_s* next_req;
391 
392 #define UV_WRITE_PRIVATE_FIELDS \
393   int coalesced;                \
394   uv_buf_t write_buffer;        \
395   HANDLE event_handle;          \
396   HANDLE wait_handle;
397 
398 #define UV_CONNECT_PRIVATE_FIELDS                                             \
399   /* empty */
400 
401 #define UV_SHUTDOWN_PRIVATE_FIELDS                                            \
402   /* empty */
403 
404 #define UV_UDP_SEND_PRIVATE_FIELDS                                            \
405   /* empty */
406 
407 #define UV_PRIVATE_REQ_TYPES                                                  \
408   typedef struct uv_pipe_accept_s {                                           \
409     UV_REQ_FIELDS                                                             \
410     HANDLE pipeHandle;                                                        \
411     struct uv_pipe_accept_s* next_pending;                                    \
412   } uv_pipe_accept_t;                                                         \
413                                                                               \
414   typedef struct uv_tcp_accept_s {                                            \
415     UV_REQ_FIELDS                                                             \
416     SOCKET accept_socket;                                                     \
417     char accept_buffer[sizeof(struct sockaddr_storage) * 2 + 32];             \
418     HANDLE event_handle;                                                      \
419     HANDLE wait_handle;                                                       \
420     struct uv_tcp_accept_s* next_pending;                                     \
421   } uv_tcp_accept_t;                                                          \
422                                                                               \
423   typedef struct uv_read_s {                                                  \
424     UV_REQ_FIELDS                                                             \
425     HANDLE event_handle;                                                      \
426     HANDLE wait_handle;                                                       \
427   } uv_read_t;
428 
429 #define uv_stream_connection_fields                                           \
430   unsigned int write_reqs_pending;                                            \
431   uv_shutdown_t* shutdown_req;
432 
433 #define uv_stream_server_fields                                               \
434   uv_connection_cb connection_cb;
435 
436 #define UV_STREAM_PRIVATE_FIELDS                                              \
437   unsigned int reqs_pending;                                                  \
438   int activecnt;                                                              \
439   uv_read_t read_req;                                                         \
440   union {                                                                     \
441     struct { uv_stream_connection_fields } conn;                              \
442     struct { uv_stream_server_fields     } serv;                              \
443   } stream;
444 
445 #define uv_tcp_server_fields                                                  \
446   uv_tcp_accept_t* accept_reqs;                                               \
447   unsigned int processed_accepts;                                             \
448   uv_tcp_accept_t* pending_accepts;                                           \
449   LPFN_ACCEPTEX func_acceptex;
450 
451 #define uv_tcp_connection_fields                                              \
452   uv_buf_t read_buffer;                                                       \
453   LPFN_CONNECTEX func_connectex;
454 
455 #define UV_TCP_PRIVATE_FIELDS                                                 \
456   SOCKET socket;                                                              \
457   int delayed_error;                                                          \
458   union {                                                                     \
459     struct { uv_tcp_server_fields } serv;                                     \
460     struct { uv_tcp_connection_fields } conn;                                 \
461   } tcp;
462 
463 #define UV_UDP_PRIVATE_FIELDS                                                 \
464   SOCKET socket;                                                              \
465   unsigned int reqs_pending;                                                  \
466   int activecnt;                                                              \
467   uv_req_t recv_req;                                                          \
468   uv_buf_t recv_buffer;                                                       \
469   struct sockaddr_storage recv_from;                                          \
470   int recv_from_len;                                                          \
471   uv_udp_recv_cb recv_cb;                                                     \
472   uv_alloc_cb alloc_cb;                                                       \
473   LPFN_WSARECV func_wsarecv;                                                  \
474   LPFN_WSARECVFROM func_wsarecvfrom;
475 
476 #define uv_pipe_server_fields                                                 \
477   int pending_instances;                                                      \
478   uv_pipe_accept_t* accept_reqs;                                              \
479   uv_pipe_accept_t* pending_accepts;
480 
481 #define uv_pipe_connection_fields                                             \
482   uv_timer_t* eof_timer;                                                      \
483   uv_write_t dummy; /* TODO: retained for ABI compat; remove this in v2.x. */ \
484   DWORD ipc_remote_pid;                                                       \
485   union {                                                                     \
486     uint32_t payload_remaining;                                               \
487     uint64_t dummy; /* TODO: retained for ABI compat; remove this in v2.x. */ \
488   } ipc_data_frame;                                                           \
489   struct uv__queue ipc_xfer_queue;                                            \
490   int ipc_xfer_queue_length;                                                  \
491   uv_write_t* non_overlapped_writes_tail;                                     \
492   CRITICAL_SECTION readfile_thread_lock;                                      \
493   volatile HANDLE readfile_thread_handle;
494 
495 #define UV_PIPE_PRIVATE_FIELDS                                                \
496   HANDLE handle;                                                              \
497   WCHAR* name;                                                                \
498   union {                                                                     \
499     struct { uv_pipe_server_fields } serv;                                    \
500     struct { uv_pipe_connection_fields } conn;                                \
501   } pipe;
502 
503 /* TODO: put the parser states in a union - TTY handles are always half-duplex
504  * so read-state can safely overlap write-state. */
505 #define UV_TTY_PRIVATE_FIELDS                                                 \
506   HANDLE handle;                                                              \
507   union {                                                                     \
508     struct {                                                                  \
509       /* Used for readable TTY handles */                                     \
510       /* TODO: remove me in v2.x. */                                          \
511       HANDLE unused_;                                                         \
512       uv_buf_t read_line_buffer;                                              \
513       HANDLE read_raw_wait;                                                   \
514       /* Fields used for translating win keystrokes into vt100 characters */  \
515       char last_key[8];                                                       \
516       unsigned char last_key_offset;                                          \
517       unsigned char last_key_len;                                             \
518       WCHAR last_utf16_high_surrogate;                                        \
519       INPUT_RECORD last_input_record;                                         \
520     } rd;                                                                     \
521     struct {                                                                  \
522       /* Used for writable TTY handles */                                     \
523       /* utf8-to-utf16 conversion state */                                    \
524       unsigned int utf8_codepoint;                                            \
525       unsigned char utf8_bytes_left;                                          \
526       /* eol conversion state */                                              \
527       unsigned char previous_eol;                                             \
528       /* ansi parser state */                                                 \
529       unsigned short ansi_parser_state;                                       \
530       unsigned char ansi_csi_argc;                                            \
531       unsigned short ansi_csi_argv[4];                                        \
532       COORD saved_position;                                                   \
533       WORD saved_attributes;                                                  \
534     } wr;                                                                     \
535   } tty;
536 
537 #define UV_POLL_PRIVATE_FIELDS                                                \
538   SOCKET socket;                                                              \
539   /* Used in fast mode */                                                     \
540   SOCKET peer_socket;                                                         \
541   AFD_POLL_INFO afd_poll_info_1;                                              \
542   AFD_POLL_INFO afd_poll_info_2;                                              \
543   /* Used in fast and slow mode. */                                           \
544   uv_req_t poll_req_1;                                                        \
545   uv_req_t poll_req_2;                                                        \
546   unsigned char submitted_events_1;                                           \
547   unsigned char submitted_events_2;                                           \
548   unsigned char mask_events_1;                                                \
549   unsigned char mask_events_2;                                                \
550   unsigned char events;
551 
552 #define UV_TIMER_PRIVATE_FIELDS                                               \
553   union {                                                                     \
554     void* heap[3];                                                            \
555     struct uv__queue queue;                                                   \
556   } node;                                                                     \
557   int unused;                                                                 \
558   uint64_t timeout;                                                           \
559   uint64_t repeat;                                                            \
560   uint64_t start_id;                                                          \
561   uv_timer_cb timer_cb;
562 
563 #define UV_ASYNC_PRIVATE_FIELDS                                               \
564   struct uv_req_s async_req;                                                  \
565   uv_async_cb async_cb;                                                       \
566   /* char to avoid alignment issues */                                        \
567   char volatile async_sent;
568 
569 #define UV_PREPARE_PRIVATE_FIELDS                                             \
570   uv_prepare_t* prepare_prev;                                                 \
571   uv_prepare_t* prepare_next;                                                 \
572   uv_prepare_cb prepare_cb;
573 
574 #define UV_CHECK_PRIVATE_FIELDS                                               \
575   uv_check_t* check_prev;                                                     \
576   uv_check_t* check_next;                                                     \
577   uv_check_cb check_cb;
578 
579 #define UV_IDLE_PRIVATE_FIELDS                                                \
580   uv_idle_t* idle_prev;                                                       \
581   uv_idle_t* idle_next;                                                       \
582   uv_idle_cb idle_cb;
583 
584 #define UV_HANDLE_PRIVATE_FIELDS                                              \
585   uv_handle_t* endgame_next;                                                  \
586   unsigned int flags;
587 
588 #define UV_GETADDRINFO_PRIVATE_FIELDS                                         \
589   struct uv__work work_req;                                                   \
590   uv_getaddrinfo_cb getaddrinfo_cb;                                           \
591   void* alloc;                                                                \
592   WCHAR* node;                                                                \
593   WCHAR* service;                                                             \
594   /* The addrinfoW field is used to store a pointer to the hints, and    */   \
595   /* later on to store the result of GetAddrInfoW. The final result will */   \
596   /* be converted to struct addrinfo* and stored in the addrinfo field.  */   \
597   struct addrinfoW* addrinfow;                                                \
598   struct addrinfo* addrinfo;                                                  \
599   int retcode;
600 
601 #define UV_GETNAMEINFO_PRIVATE_FIELDS                                         \
602   struct uv__work work_req;                                                   \
603   uv_getnameinfo_cb getnameinfo_cb;                                           \
604   struct sockaddr_storage storage;                                            \
605   int flags;                                                                  \
606   char host[NI_MAXHOST];                                                      \
607   char service[NI_MAXSERV];                                                   \
608   int retcode;
609 
610 #define UV_PROCESS_PRIVATE_FIELDS                                             \
611   struct uv_process_exit_s {                                                  \
612     UV_REQ_FIELDS                                                             \
613   } exit_req;                                                                 \
614   void* unused; /* TODO: retained for ABI compat; remove this in v2.x. */     \
615   int exit_signal;                                                            \
616   HANDLE wait_handle;                                                         \
617   HANDLE process_handle;                                                      \
618   volatile char exit_cb_pending;
619 
620 #define UV_FS_PRIVATE_FIELDS                                                  \
621   struct uv__work work_req;                                                   \
622   int flags;                                                                  \
623   DWORD sys_errno_;                                                           \
624   union {                                                                     \
625     /* TODO: remove me in 0.9. */                                             \
626     WCHAR* pathw;                                                             \
627     int fd;                                                                   \
628   } file;                                                                     \
629   union {                                                                     \
630     struct {                                                                  \
631       int mode;                                                               \
632       WCHAR* new_pathw;                                                       \
633       int file_flags;                                                         \
634       int fd_out;                                                             \
635       unsigned int nbufs;                                                     \
636       uv_buf_t* bufs;                                                         \
637       int64_t offset;                                                         \
638       uv_buf_t bufsml[4];                                                     \
639     } info;                                                                   \
640     struct {                                                                  \
641       double atime;                                                           \
642       double mtime;                                                           \
643     } time;                                                                   \
644   } fs;
645 
646 #define UV_WORK_PRIVATE_FIELDS                                                \
647   struct uv__work work_req;
648 
649 #define UV_FS_EVENT_PRIVATE_FIELDS                                            \
650   struct uv_fs_event_req_s {                                                  \
651     UV_REQ_FIELDS                                                             \
652   } req;                                                                      \
653   HANDLE dir_handle;                                                          \
654   int req_pending;                                                            \
655   uv_fs_event_cb cb;                                                          \
656   WCHAR* filew;                                                               \
657   WCHAR* short_filew;                                                         \
658   WCHAR* dirw;                                                                \
659   char* buffer;
660 
661 #define UV_SIGNAL_PRIVATE_FIELDS                                              \
662   RB_ENTRY(uv_signal_s) tree_entry;                                           \
663   struct uv_req_s signal_req;                                                 \
664   unsigned long pending_signum;
665 
666 #ifndef F_OK
667 #define F_OK 0
668 #endif
669 #ifndef R_OK
670 #define R_OK 4
671 #endif
672 #ifndef W_OK
673 #define W_OK 2
674 #endif
675 #ifndef X_OK
676 #define X_OK 1
677 #endif
678 
679 /* fs open() flags supported on this platform: */
680 #define UV_FS_O_APPEND       _O_APPEND
681 #define UV_FS_O_CREAT        _O_CREAT
682 #define UV_FS_O_EXCL         _O_EXCL
683 #define UV_FS_O_FILEMAP      0x20000000
684 #define UV_FS_O_RANDOM       _O_RANDOM
685 #define UV_FS_O_RDONLY       _O_RDONLY
686 #define UV_FS_O_RDWR         _O_RDWR
687 #define UV_FS_O_SEQUENTIAL   _O_SEQUENTIAL
688 #define UV_FS_O_SHORT_LIVED  _O_SHORT_LIVED
689 #define UV_FS_O_TEMPORARY    _O_TEMPORARY
690 #define UV_FS_O_TRUNC        _O_TRUNC
691 #define UV_FS_O_WRONLY       _O_WRONLY
692 
693 /* fs open() flags supported on other platforms (or mapped on this platform): */
694 #define UV_FS_O_DIRECT       0x02000000 /* FILE_FLAG_NO_BUFFERING */
695 #define UV_FS_O_DIRECTORY    0
696 #define UV_FS_O_DSYNC        0x04000000 /* FILE_FLAG_WRITE_THROUGH */
697 #define UV_FS_O_EXLOCK       0x10000000 /* EXCLUSIVE SHARING MODE */
698 #define UV_FS_O_NOATIME      0
699 #define UV_FS_O_NOCTTY       0
700 #define UV_FS_O_NOFOLLOW     0
701 #define UV_FS_O_NONBLOCK     0
702 #define UV_FS_O_SYMLINK      0
703 #define UV_FS_O_SYNC         0x08000000 /* FILE_FLAG_WRITE_THROUGH */
704