d6485e52b09e1e0ad7a0787224295139ce3cc235
[platform/upstream/nodejs.git] / deps / uv / include / uv.h
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 /* See http://nikhilm.github.com/uvbook/ for an introduction. */
23
24 #ifndef UV_H
25 #define UV_H
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 #ifdef _WIN32
31   /* Windows - set up dll import/export decorators. */
32 # if defined(BUILDING_UV_SHARED)
33     /* Building shared library. */
34 #   define UV_EXTERN __declspec(dllexport)
35 # elif defined(USING_UV_SHARED)
36     /* Using shared library. */
37 #   define UV_EXTERN __declspec(dllimport)
38 # else
39     /* Building static library. */
40 #   define UV_EXTERN /* nothing */
41 # endif
42 #elif __GNUC__ >= 4
43 # define UV_EXTERN __attribute__((visibility("default")))
44 #else
45 # define UV_EXTERN /* nothing */
46 #endif
47
48 #include "uv-errno.h"
49 #include <stddef.h>
50
51 #if defined(_MSC_VER) && _MSC_VER < 1600
52 # include "stdint-msvc2008.h"
53 #else
54 # include <stdint.h>
55 #endif
56
57 #if defined(_WIN32)
58 # include "uv-win.h"
59 #else
60 # include "uv-unix.h"
61 #endif
62
63 /* Expand this list if necessary. */
64 #define UV_ERRNO_MAP(XX)                                                      \
65   XX(E2BIG, "argument list too long")                                         \
66   XX(EACCES, "permission denied")                                             \
67   XX(EADDRINUSE, "address already in use")                                    \
68   XX(EADDRNOTAVAIL, "address not available")                                  \
69   XX(EAFNOSUPPORT, "address family not supported")                            \
70   XX(EAGAIN, "resource temporarily unavailable")                              \
71   XX(EAI_ADDRFAMILY, "address family not supported")                          \
72   XX(EAI_AGAIN, "temporary failure")                                          \
73   XX(EAI_BADFLAGS, "bad ai_flags value")                                      \
74   XX(EAI_BADHINTS, "invalid value for hints")                                 \
75   XX(EAI_CANCELED, "request canceled")                                        \
76   XX(EAI_FAIL, "permanent failure")                                           \
77   XX(EAI_FAMILY, "ai_family not supported")                                   \
78   XX(EAI_MEMORY, "out of memory")                                             \
79   XX(EAI_NODATA, "no address")                                                \
80   XX(EAI_NONAME, "unknown node or service")                                   \
81   XX(EAI_OVERFLOW, "argument buffer overflow")                                \
82   XX(EAI_PROTOCOL, "resolved protocol is unknown")                            \
83   XX(EAI_SERVICE, "service not available for socket type")                    \
84   XX(EAI_SOCKTYPE, "socket type not supported")                               \
85   XX(EAI_SYSTEM, "system error")                                              \
86   XX(EALREADY, "connection already in progress")                              \
87   XX(EBADF, "bad file descriptor")                                            \
88   XX(EBUSY, "resource busy or locked")                                        \
89   XX(ECANCELED, "operation canceled")                                         \
90   XX(ECHARSET, "invalid Unicode character")                                   \
91   XX(ECONNABORTED, "software caused connection abort")                        \
92   XX(ECONNREFUSED, "connection refused")                                      \
93   XX(ECONNRESET, "connection reset by peer")                                  \
94   XX(EDESTADDRREQ, "destination address required")                            \
95   XX(EEXIST, "file already exists")                                           \
96   XX(EFAULT, "bad address in system call argument")                           \
97   XX(EHOSTUNREACH, "host is unreachable")                                     \
98   XX(EINTR, "interrupted system call")                                        \
99   XX(EINVAL, "invalid argument")                                              \
100   XX(EIO, "i/o error")                                                        \
101   XX(EISCONN, "socket is already connected")                                  \
102   XX(EISDIR, "illegal operation on a directory")                              \
103   XX(ELOOP, "too many symbolic links encountered")                            \
104   XX(EMFILE, "too many open files")                                           \
105   XX(EMSGSIZE, "message too long")                                            \
106   XX(ENAMETOOLONG, "name too long")                                           \
107   XX(ENETDOWN, "network is down")                                             \
108   XX(ENETUNREACH, "network is unreachable")                                   \
109   XX(ENFILE, "file table overflow")                                           \
110   XX(ENOBUFS, "no buffer space available")                                    \
111   XX(ENODEV, "no such device")                                                \
112   XX(ENOENT, "no such file or directory")                                     \
113   XX(ENOMEM, "not enough memory")                                             \
114   XX(ENONET, "machine is not on the network")                                 \
115   XX(ENOSPC, "no space left on device")                                       \
116   XX(ENOSYS, "function not implemented")                                      \
117   XX(ENOTCONN, "socket is not connected")                                     \
118   XX(ENOTDIR, "not a directory")                                              \
119   XX(ENOTEMPTY, "directory not empty")                                        \
120   XX(ENOTSOCK, "socket operation on non-socket")                              \
121   XX(ENOTSUP, "operation not supported on socket")                            \
122   XX(EPERM, "operation not permitted")                                        \
123   XX(EPIPE, "broken pipe")                                                    \
124   XX(EPROTO, "protocol error")                                                \
125   XX(EPROTONOSUPPORT, "protocol not supported")                               \
126   XX(EPROTOTYPE, "protocol wrong type for socket")                            \
127   XX(EROFS, "read-only file system")                                          \
128   XX(ESHUTDOWN, "cannot send after transport endpoint shutdown")              \
129   XX(ESPIPE, "invalid seek")                                                  \
130   XX(ESRCH, "no such process")                                                \
131   XX(ETIMEDOUT, "connection timed out")                                       \
132   XX(EXDEV, "cross-device link not permitted")                                \
133   XX(UNKNOWN, "unknown error")                                                \
134   XX(EOF, "end of file")                                                      \
135
136 #define UV_HANDLE_TYPE_MAP(XX)                                                \
137   XX(ASYNC, async)                                                            \
138   XX(CHECK, check)                                                            \
139   XX(FS_EVENT, fs_event)                                                      \
140   XX(FS_POLL, fs_poll)                                                        \
141   XX(HANDLE, handle)                                                          \
142   XX(IDLE, idle)                                                              \
143   XX(NAMED_PIPE, pipe)                                                        \
144   XX(POLL, poll)                                                              \
145   XX(PREPARE, prepare)                                                        \
146   XX(PROCESS, process)                                                        \
147   XX(STREAM, stream)                                                          \
148   XX(TCP, tcp)                                                                \
149   XX(TIMER, timer)                                                            \
150   XX(TTY, tty)                                                                \
151   XX(UDP, udp)                                                                \
152   XX(SIGNAL, signal)                                                          \
153
154 #define UV_REQ_TYPE_MAP(XX)                                                   \
155   XX(REQ, req)                                                                \
156   XX(CONNECT, connect)                                                        \
157   XX(WRITE, write)                                                            \
158   XX(SHUTDOWN, shutdown)                                                      \
159   XX(UDP_SEND, udp_send)                                                      \
160   XX(FS, fs)                                                                  \
161   XX(WORK, work)                                                              \
162   XX(GETADDRINFO, getaddrinfo)                                                \
163
164 typedef enum {
165 #define XX(code, _) UV_ ## code = UV__ ## code,
166   UV_ERRNO_MAP(XX)
167 #undef XX
168   UV_ERRNO_MAX = UV__EOF - 1
169 } uv_errno_t;
170
171 typedef enum {
172   UV_UNKNOWN_HANDLE = 0,
173 #define XX(uc, lc) UV_##uc,
174   UV_HANDLE_TYPE_MAP(XX)
175 #undef XX
176   UV_FILE,
177   UV_HANDLE_TYPE_MAX
178 } uv_handle_type;
179
180 typedef enum {
181   UV_UNKNOWN_REQ = 0,
182 #define XX(uc, lc) UV_##uc,
183   UV_REQ_TYPE_MAP(XX)
184 #undef XX
185   UV_REQ_TYPE_PRIVATE
186   UV_REQ_TYPE_MAX
187 } uv_req_type;
188
189
190 /* Handle types. */
191 typedef struct uv_loop_s uv_loop_t;
192 typedef struct uv_handle_s uv_handle_t;
193 typedef struct uv_stream_s uv_stream_t;
194 typedef struct uv_tcp_s uv_tcp_t;
195 typedef struct uv_udp_s uv_udp_t;
196 typedef struct uv_pipe_s uv_pipe_t;
197 typedef struct uv_tty_s uv_tty_t;
198 typedef struct uv_poll_s uv_poll_t;
199 typedef struct uv_timer_s uv_timer_t;
200 typedef struct uv_prepare_s uv_prepare_t;
201 typedef struct uv_check_s uv_check_t;
202 typedef struct uv_idle_s uv_idle_t;
203 typedef struct uv_async_s uv_async_t;
204 typedef struct uv_process_s uv_process_t;
205 typedef struct uv_fs_event_s uv_fs_event_t;
206 typedef struct uv_fs_poll_s uv_fs_poll_t;
207 typedef struct uv_signal_s uv_signal_t;
208
209 /* Request types. */
210 typedef struct uv_req_s uv_req_t;
211 typedef struct uv_getaddrinfo_s uv_getaddrinfo_t;
212 typedef struct uv_shutdown_s uv_shutdown_t;
213 typedef struct uv_write_s uv_write_t;
214 typedef struct uv_connect_s uv_connect_t;
215 typedef struct uv_udp_send_s uv_udp_send_t;
216 typedef struct uv_fs_s uv_fs_t;
217 typedef struct uv_work_s uv_work_t;
218
219 /* None of the above. */
220 typedef struct uv_cpu_info_s uv_cpu_info_t;
221 typedef struct uv_interface_address_s uv_interface_address_t;
222
223
224 typedef enum {
225   UV_RUN_DEFAULT = 0,
226   UV_RUN_ONCE,
227   UV_RUN_NOWAIT
228 } uv_run_mode;
229
230
231 /*
232  * Returns the libuv version packed into a single integer. 8 bits are used for
233  * each component, with the patch number stored in the 8 least significant
234  * bits. E.g. for libuv 1.2.3 this would return 0x010203.
235  */
236 UV_EXTERN unsigned int uv_version(void);
237
238 /*
239  * Returns the libuv version number as a string. For non-release versions
240  * "-pre" is appended, so the version number could be "1.2.3-pre".
241  */
242 UV_EXTERN const char* uv_version_string(void);
243
244
245 /*
246  * This function must be called before any other functions in libuv.
247  *
248  * All functions besides uv_run() are non-blocking.
249  *
250  * All callbacks in libuv are made asynchronously. That is they are never
251  * made by the function that takes them as a parameter.
252  */
253 UV_EXTERN uv_loop_t* uv_loop_new(void);
254 UV_EXTERN void uv_loop_delete(uv_loop_t*);
255
256 /*
257  * Returns the default loop.
258  */
259 UV_EXTERN uv_loop_t* uv_default_loop(void);
260
261 /*
262  * This function runs the event loop. It will act differently depending on the
263  * specified mode:
264  *  - UV_RUN_DEFAULT: Runs the event loop until the reference count drops to
265  *    zero. Always returns zero.
266  *  - UV_RUN_ONCE: Poll for new events once. Note that this function blocks if
267  *    there are no pending events. Returns zero when done (no active handles
268  *    or requests left), or non-zero if more events are expected (meaning you
269  *    should run the event loop again sometime in the future).
270  *  - UV_RUN_NOWAIT: Poll for new events once but don't block if there are no
271  *    pending events.
272  */
273 UV_EXTERN int uv_run(uv_loop_t*, uv_run_mode mode);
274
275 /*
276  * This function will stop the event loop by forcing uv_run to end
277  * as soon as possible, but not sooner than the next loop iteration.
278  * If this function was called before blocking for i/o, the loop won't
279  * block for i/o on this iteration.
280  */
281 UV_EXTERN void uv_stop(uv_loop_t*);
282
283 /*
284  * Manually modify the event loop's reference count. Useful if the user wants
285  * to have a handle or timeout that doesn't keep the loop alive.
286  */
287 UV_EXTERN void uv_ref(uv_handle_t*);
288 UV_EXTERN void uv_unref(uv_handle_t*);
289 UV_EXTERN int uv_has_ref(const uv_handle_t*);
290
291 /*
292  * Update the event loop's concept of "now". Libuv caches the current time
293  * at the start of the event loop tick in order to reduce the number of
294  * time-related system calls.
295  *
296  * You won't normally need to call this function unless you have callbacks
297  * that block the event loop for longer periods of time, where "longer" is
298  * somewhat subjective but probably on the order of a millisecond or more.
299  */
300 UV_EXTERN void uv_update_time(uv_loop_t*);
301
302 /*
303  * Return the current timestamp in milliseconds. The timestamp is cached at
304  * the start of the event loop tick, see |uv_update_time()| for details and
305  * rationale.
306  *
307  * The timestamp increases monotonically from some arbitrary point in time.
308  * Don't make assumptions about the starting point, you will only get
309  * disappointed.
310  *
311  * Use uv_hrtime() if you need sub-millisecond granularity.
312  */
313 UV_EXTERN uint64_t uv_now(uv_loop_t*);
314
315 /*
316  * Get backend file descriptor. Only kqueue, epoll and event ports are
317  * supported.
318  *
319  * This can be used in conjunction with `uv_run(loop, UV_RUN_NOWAIT)` to
320  * poll in one thread and run the event loop's event callbacks in another.
321  *
322  * Useful for embedding libuv's event loop in another event loop.
323  * See test/test-embed.c for an example.
324  *
325  * Note that embedding a kqueue fd in another kqueue pollset doesn't work on
326  * all platforms. It's not an error to add the fd but it never generates
327  * events.
328  */
329 UV_EXTERN int uv_backend_fd(const uv_loop_t*);
330
331 /*
332  * Get the poll timeout. The return value is in milliseconds, or -1 for no
333  * timeout.
334  */
335 UV_EXTERN int uv_backend_timeout(const uv_loop_t*);
336
337
338 /*
339  * Should prepare a buffer that libuv can use to read data into.
340  *
341  * `suggested_size` is a hint. Returning a buffer that is smaller is perfectly
342  * okay as long as `buf.len > 0`.
343  *
344  * If you return a buffer with `buf.len == 0`, libuv skips the read and calls
345  * your read or recv callback with nread=UV_ENOBUFS.
346  *
347  * Note that returning a zero-length buffer does not stop the handle, call
348  * uv_read_stop() or uv_udp_recv_stop() for that.
349  */
350 typedef void (*uv_alloc_cb)(uv_handle_t* handle,
351                             size_t suggested_size,
352                             uv_buf_t* buf);
353
354 /*
355  * `nread` is > 0 if there is data available, 0 if libuv is done reading for
356  * now, or < 0 on error.
357  *
358  * The callee is responsible for closing the stream when an error happens.
359  * Trying to read from the stream again is undefined.
360  *
361  * The callee is responsible for freeing the buffer, libuv does not reuse it.
362  * The buffer may be a null buffer (where buf->base=NULL and buf->len=0) on
363  * EOF or error.
364  */
365 typedef void (*uv_read_cb)(uv_stream_t* stream,
366                            ssize_t nread,
367                            const uv_buf_t* buf);
368
369 /*
370  * Just like the uv_read_cb except that if the pending parameter is true
371  * then you can use uv_accept() to pull the new handle into the process.
372  * If no handle is pending then pending will be UV_UNKNOWN_HANDLE.
373  */
374 typedef void (*uv_read2_cb)(uv_pipe_t* pipe,
375                             ssize_t nread,
376                             const uv_buf_t* buf,
377                             uv_handle_type pending);
378
379 typedef void (*uv_write_cb)(uv_write_t* req, int status);
380 typedef void (*uv_connect_cb)(uv_connect_t* req, int status);
381 typedef void (*uv_shutdown_cb)(uv_shutdown_t* req, int status);
382 typedef void (*uv_connection_cb)(uv_stream_t* server, int status);
383 typedef void (*uv_close_cb)(uv_handle_t* handle);
384 typedef void (*uv_poll_cb)(uv_poll_t* handle, int status, int events);
385 typedef void (*uv_timer_cb)(uv_timer_t* handle, int status);
386 /* TODO: do these really need a status argument? */
387 typedef void (*uv_async_cb)(uv_async_t* handle, int status);
388 typedef void (*uv_prepare_cb)(uv_prepare_t* handle, int status);
389 typedef void (*uv_check_cb)(uv_check_t* handle, int status);
390 typedef void (*uv_idle_cb)(uv_idle_t* handle, int status);
391 typedef void (*uv_exit_cb)(uv_process_t*, int64_t exit_status, int term_signal);
392 typedef void (*uv_walk_cb)(uv_handle_t* handle, void* arg);
393 typedef void (*uv_fs_cb)(uv_fs_t* req);
394 typedef void (*uv_work_cb)(uv_work_t* req);
395 typedef void (*uv_after_work_cb)(uv_work_t* req, int status);
396 typedef void (*uv_getaddrinfo_cb)(uv_getaddrinfo_t* req,
397                                   int status,
398                                   struct addrinfo* res);
399
400 typedef struct {
401   long tv_sec;
402   long tv_nsec;
403 } uv_timespec_t;
404
405
406 typedef struct {
407   uint64_t st_dev;
408   uint64_t st_mode;
409   uint64_t st_nlink;
410   uint64_t st_uid;
411   uint64_t st_gid;
412   uint64_t st_rdev;
413   uint64_t st_ino;
414   uint64_t st_size;
415   uint64_t st_blksize;
416   uint64_t st_blocks;
417   uint64_t st_flags;
418   uint64_t st_gen;
419   uv_timespec_t st_atim;
420   uv_timespec_t st_mtim;
421   uv_timespec_t st_ctim;
422   uv_timespec_t st_birthtim;
423 } uv_stat_t;
424
425
426 /*
427 * This will be called repeatedly after the uv_fs_event_t is initialized.
428 * If uv_fs_event_t was initialized with a directory the filename parameter
429 * will be a relative path to a file contained in the directory.
430 * The events parameter is an ORed mask of enum uv_fs_event elements.
431 */
432 typedef void (*uv_fs_event_cb)(uv_fs_event_t* handle, const char* filename,
433     int events, int status);
434
435 typedef void (*uv_fs_poll_cb)(uv_fs_poll_t* handle,
436                               int status,
437                               const uv_stat_t* prev,
438                               const uv_stat_t* curr);
439
440 typedef void (*uv_signal_cb)(uv_signal_t* handle, int signum);
441
442
443 typedef enum {
444   UV_LEAVE_GROUP = 0,
445   UV_JOIN_GROUP
446 } uv_membership;
447
448
449 /*
450  * Most functions return 0 on success or an error code < 0 on failure.
451  */
452 UV_EXTERN const char* uv_strerror(int err);
453 UV_EXTERN const char* uv_err_name(int err);
454
455
456 #define UV_REQ_FIELDS                                                         \
457   /* public */                                                                \
458   void* data;                                                                 \
459   /* read-only */                                                             \
460   uv_req_type type;                                                           \
461   /* private */                                                               \
462   void* active_queue[2];                                                      \
463   UV_REQ_PRIVATE_FIELDS                                                       \
464
465 /* Abstract base class of all requests. */
466 struct uv_req_s {
467   UV_REQ_FIELDS
468 };
469
470
471 /* Platform-specific request types */
472 UV_PRIVATE_REQ_TYPES
473
474
475 /*
476  * uv_shutdown_t is a subclass of uv_req_t
477  *
478  * Shutdown the outgoing (write) side of a duplex stream. It waits for
479  * pending write requests to complete. The handle should refer to a
480  * initialized stream. req should be an uninitialized shutdown request
481  * struct. The cb is called after shutdown is complete.
482  */
483 UV_EXTERN int uv_shutdown(uv_shutdown_t* req, uv_stream_t* handle,
484     uv_shutdown_cb cb);
485
486 struct uv_shutdown_s {
487   UV_REQ_FIELDS
488   uv_stream_t* handle;
489   uv_shutdown_cb cb;
490   UV_SHUTDOWN_PRIVATE_FIELDS
491 };
492
493
494 #define UV_HANDLE_FIELDS                                                      \
495   /* public */                                                                \
496   uv_close_cb close_cb;                                                       \
497   void* data;                                                                 \
498   /* read-only */                                                             \
499   uv_loop_t* loop;                                                            \
500   uv_handle_type type;                                                        \
501   /* private */                                                               \
502   void* handle_queue[2];                                                      \
503   UV_HANDLE_PRIVATE_FIELDS                                                    \
504
505 /* The abstract base class of all handles.  */
506 struct uv_handle_s {
507   UV_HANDLE_FIELDS
508 };
509
510 /*
511  * Returns size of various handle types, useful for FFI
512  * bindings to allocate correct memory without copying struct
513  * definitions
514  */
515 UV_EXTERN size_t uv_handle_size(uv_handle_type type);
516
517 /*
518  * Returns size of request types, useful for dynamic lookup with FFI
519  */
520 UV_EXTERN size_t uv_req_size(uv_req_type type);
521
522 /*
523  * Returns non-zero if the handle is active, zero if it's inactive.
524  *
525  * What "active" means depends on the type of handle:
526  *
527  *  - A uv_async_t handle is always active and cannot be deactivated, except
528  *    by closing it with uv_close().
529  *
530  *  - A uv_pipe_t, uv_tcp_t, uv_udp_t, etc. handle - basically any handle that
531  *    deals with I/O - is active when it is doing something that involves I/O,
532  *    like reading, writing, connecting, accepting new connections, etc.
533  *
534  *  - A uv_check_t, uv_idle_t, uv_timer_t, etc. handle is active when it has
535  *    been started with a call to uv_check_start(), uv_idle_start(), etc.
536  *
537  *      Rule of thumb: if a handle of type uv_foo_t has a uv_foo_start()
538  *      function, then it's active from the moment that function is called.
539  *      Likewise, uv_foo_stop() deactivates the handle again.
540  *
541  */
542 UV_EXTERN int uv_is_active(const uv_handle_t* handle);
543
544 /*
545  * Walk the list of open handles.
546  */
547 UV_EXTERN void uv_walk(uv_loop_t* loop, uv_walk_cb walk_cb, void* arg);
548
549
550 /*
551  * Request handle to be closed. close_cb will be called asynchronously after
552  * this call. This MUST be called on each handle before memory is released.
553  *
554  * Note that handles that wrap file descriptors are closed immediately but
555  * close_cb will still be deferred to the next iteration of the event loop.
556  * It gives you a chance to free up any resources associated with the handle.
557  *
558  * In-progress requests, like uv_connect_t or uv_write_t, are cancelled and
559  * have their callbacks called asynchronously with status=UV_ECANCELED.
560  */
561 UV_EXTERN void uv_close(uv_handle_t* handle, uv_close_cb close_cb);
562
563
564 /*
565  * Constructor for uv_buf_t.
566  * Due to platform differences the user cannot rely on the ordering of the
567  * base and len members of the uv_buf_t struct. The user is responsible for
568  * freeing base after the uv_buf_t is done. Return struct passed by value.
569  */
570 UV_EXTERN uv_buf_t uv_buf_init(char* base, unsigned int len);
571
572
573 #define UV_STREAM_FIELDS                                                      \
574   /* number of bytes queued for writing */                                    \
575   size_t write_queue_size;                                                    \
576   uv_alloc_cb alloc_cb;                                                       \
577   uv_read_cb read_cb;                                                         \
578   uv_read2_cb read2_cb;                                                       \
579   /* private */                                                               \
580   UV_STREAM_PRIVATE_FIELDS
581
582 /*
583  * uv_stream_t is a subclass of uv_handle_t
584  *
585  * uv_stream is an abstract class.
586  *
587  * uv_stream_t is the parent class of uv_tcp_t, uv_pipe_t, uv_tty_t, and
588  * soon uv_file_t.
589  */
590 struct uv_stream_s {
591   UV_HANDLE_FIELDS
592   UV_STREAM_FIELDS
593 };
594
595 UV_EXTERN int uv_listen(uv_stream_t* stream, int backlog, uv_connection_cb cb);
596
597 /*
598  * This call is used in conjunction with uv_listen() to accept incoming
599  * connections. Call uv_accept after receiving a uv_connection_cb to accept
600  * the connection. Before calling uv_accept use uv_*_init() must be
601  * called on the client. Non-zero return value indicates an error.
602  *
603  * When the uv_connection_cb is called it is guaranteed that uv_accept will
604  * complete successfully the first time. If you attempt to use it more than
605  * once, it may fail. It is suggested to only call uv_accept once per
606  * uv_connection_cb call.
607  */
608 UV_EXTERN int uv_accept(uv_stream_t* server, uv_stream_t* client);
609
610 /*
611  * Read data from an incoming stream. The callback will be made several
612  * times until there is no more data to read or uv_read_stop is called.
613  * When we've reached EOF nread will be set to UV_EOF.
614  *
615  * When nread < 0, the buf parameter might not point to a valid buffer;
616  * in that case buf.len and buf.base are both set to 0.
617  *
618  * Note that nread might also be 0, which does *not* indicate an error or
619  * eof; it happens when libuv requested a buffer through the alloc callback
620  * but then decided that it didn't need that buffer.
621  */
622 UV_EXTERN int uv_read_start(uv_stream_t*, uv_alloc_cb alloc_cb,
623     uv_read_cb read_cb);
624
625 UV_EXTERN int uv_read_stop(uv_stream_t*);
626
627 /*
628  * Extended read methods for receiving handles over a pipe. The pipe must be
629  * initialized with ipc == 1.
630  */
631 UV_EXTERN int uv_read2_start(uv_stream_t*, uv_alloc_cb alloc_cb,
632     uv_read2_cb read_cb);
633
634
635 /*
636  * Write data to stream. Buffers are written in order. Example:
637  *
638  *   uv_buf_t a[] = {
639  *     { .base = "1", .len = 1 },
640  *     { .base = "2", .len = 1 }
641  *   };
642  *
643  *   uv_buf_t b[] = {
644  *     { .base = "3", .len = 1 },
645  *     { .base = "4", .len = 1 }
646  *   };
647  *
648  *   uv_write_t req1;
649  *   uv_write_t req2;
650  *
651  *   // writes "1234"
652  *   uv_write(&req1, stream, a, 2);
653  *   uv_write(&req2, stream, b, 2);
654  *
655  */
656 UV_EXTERN int uv_write(uv_write_t* req,
657                        uv_stream_t* handle,
658                        const uv_buf_t bufs[],
659                        unsigned int nbufs,
660                        uv_write_cb cb);
661
662 /*
663  * Extended write function for sending handles over a pipe. The pipe must be
664  * initialized with ipc == 1.
665  * send_handle must be a TCP socket or pipe, which is a server or a connection
666  * (listening or connected state).  Bound sockets or pipes will be assumed to
667  * be servers.
668  */
669 UV_EXTERN int uv_write2(uv_write_t* req,
670                         uv_stream_t* handle,
671                         const uv_buf_t bufs[],
672                         unsigned int nbufs,
673                         uv_stream_t* send_handle,
674                         uv_write_cb cb);
675
676 /*
677  * Same as `uv_write()`, but won't queue write request if it can't be completed
678  * immediately.
679  * Will return either:
680  * - positive number of bytes written
681  * - zero - if queued write is needed
682  * - negative error code
683  */
684 UV_EXTERN int uv_try_write(uv_stream_t* handle, const char* buf, size_t length);
685
686 /* uv_write_t is a subclass of uv_req_t */
687 struct uv_write_s {
688   UV_REQ_FIELDS
689   uv_write_cb cb;
690   uv_stream_t* send_handle;
691   uv_stream_t* handle;
692   UV_WRITE_PRIVATE_FIELDS
693 };
694
695
696 /*
697  * Used to determine whether a stream is readable or writable.
698  */
699 UV_EXTERN int uv_is_readable(const uv_stream_t* handle);
700 UV_EXTERN int uv_is_writable(const uv_stream_t* handle);
701
702
703 /*
704  * Enable or disable blocking mode for a stream.
705  *
706  * When blocking mode is enabled all writes complete synchronously. The
707  * interface remains unchanged otherwise, e.g. completion or failure of the
708  * operation will still be reported through a callback which is made
709  * asychronously.
710  *
711  * Relying too much on this API is not recommended. It is likely to change
712  * significantly in the future.
713  *
714  * On windows this currently works only for uv_pipe_t instances. On unix it
715  * works for tcp, pipe and tty instances. Be aware that changing the blocking
716  * mode on unix sets or clears the O_NONBLOCK bit. If you are sharing a handle
717  * with another process, the other process is affected by the change too,
718  * which can lead to unexpected results.
719  *
720  * Also libuv currently makes no ordering guarantee when the blocking mode
721  * is changed after write requests have already been submitted. Therefore it is
722  * recommended to set the blocking mode immediately after opening or creating
723  * the stream.
724  */
725 UV_EXTERN int uv_stream_set_blocking(uv_stream_t* handle, int blocking);
726
727
728 /*
729  * Used to determine whether a stream is closing or closed.
730  *
731  * N.B. is only valid between the initialization of the handle
732  *      and the arrival of the close callback, and cannot be used
733  *      to validate the handle.
734  */
735 UV_EXTERN int uv_is_closing(const uv_handle_t* handle);
736
737
738 /*
739  * uv_tcp_t is a subclass of uv_stream_t
740  *
741  * Represents a TCP stream or TCP server.
742  */
743 struct uv_tcp_s {
744   UV_HANDLE_FIELDS
745   UV_STREAM_FIELDS
746   UV_TCP_PRIVATE_FIELDS
747 };
748
749 UV_EXTERN int uv_tcp_init(uv_loop_t*, uv_tcp_t* handle);
750
751 /*
752  * Opens an existing file descriptor or SOCKET as a tcp handle.
753  */
754 UV_EXTERN int uv_tcp_open(uv_tcp_t* handle, uv_os_sock_t sock);
755
756 /* Enable/disable Nagle's algorithm. */
757 UV_EXTERN int uv_tcp_nodelay(uv_tcp_t* handle, int enable);
758
759 /*
760  * Enable/disable TCP keep-alive.
761  *
762  * `delay` is the initial delay in seconds, ignored when `enable` is zero.
763  */
764 UV_EXTERN int uv_tcp_keepalive(uv_tcp_t* handle,
765                                int enable,
766                                unsigned int delay);
767
768 /*
769  * Enable/disable simultaneous asynchronous accept requests that are
770  * queued by the operating system when listening for new tcp connections.
771  * This setting is used to tune a tcp server for the desired performance.
772  * Having simultaneous accepts can significantly improve the rate of
773  * accepting connections (which is why it is enabled by default) but
774  * may lead to uneven load distribution in multi-process setups.
775  */
776 UV_EXTERN int uv_tcp_simultaneous_accepts(uv_tcp_t* handle, int enable);
777
778 /*
779  * Bind the handle to an address and port.  `addr` should point to an
780  * initialized struct sockaddr_in or struct sockaddr_in6.
781  *
782  * When the port is already taken, you can expect to see an UV_EADDRINUSE
783  * error from either uv_tcp_bind(), uv_listen() or uv_tcp_connect().
784  *
785  * That is, a successful call to uv_tcp_bind() does not guarantee that
786  * the call to uv_listen() or uv_tcp_connect() will succeed as well.
787  */
788 UV_EXTERN int uv_tcp_bind(uv_tcp_t* handle, const struct sockaddr* addr);
789
790 UV_EXTERN int uv_tcp_getsockname(uv_tcp_t* handle, struct sockaddr* name,
791     int* namelen);
792 UV_EXTERN int uv_tcp_getpeername(uv_tcp_t* handle, struct sockaddr* name,
793     int* namelen);
794
795 /*
796  * Establish an IPv4 or IPv6 TCP connection.  Provide an initialized TCP handle
797  * and an uninitialized uv_connect_t*.  `addr` should point to an initialized
798  * struct sockaddr_in or struct sockaddr_in6.
799  *
800  * The callback is made when the connection has been established or when a
801  * connection error happened.
802  */
803 UV_EXTERN int uv_tcp_connect(uv_connect_t* req,
804                              uv_tcp_t* handle,
805                              const struct sockaddr* addr,
806                              uv_connect_cb cb);
807
808 /* uv_connect_t is a subclass of uv_req_t */
809 struct uv_connect_s {
810   UV_REQ_FIELDS
811   uv_connect_cb cb;
812   uv_stream_t* handle;
813   UV_CONNECT_PRIVATE_FIELDS
814 };
815
816
817 /*
818  * UDP support.
819  */
820
821 enum uv_udp_flags {
822   /* Disables dual stack mode. */
823   UV_UDP_IPV6ONLY = 1,
824   /*
825    * Indicates message was truncated because read buffer was too small. The
826    * remainder was discarded by the OS. Used in uv_udp_recv_cb.
827    */
828   UV_UDP_PARTIAL = 2
829 };
830
831 /*
832  * Called after a uv_udp_send() or uv_udp_send6(). status 0 indicates
833  * success otherwise error.
834  */
835 typedef void (*uv_udp_send_cb)(uv_udp_send_t* req, int status);
836
837 /*
838  * Callback that is invoked when a new UDP datagram is received.
839  *
840  *  handle  UDP handle.
841  *  nread   Number of bytes that have been received.
842  *          0 if there is no more data to read. You may
843  *          discard or repurpose the read buffer.
844  *          < 0 if a transmission error was detected.
845  *  buf     uv_buf_t with the received data.
846  *  addr    struct sockaddr_in or struct sockaddr_in6.
847  *          Valid for the duration of the callback only.
848  *  flags   One or more OR'ed UV_UDP_* constants.
849  *          Right now only UV_UDP_PARTIAL is used.
850  */
851 typedef void (*uv_udp_recv_cb)(uv_udp_t* handle,
852                                ssize_t nread,
853                                const uv_buf_t* buf,
854                                const struct sockaddr* addr,
855                                unsigned flags);
856
857 /* uv_udp_t is a subclass of uv_handle_t */
858 struct uv_udp_s {
859   UV_HANDLE_FIELDS
860   UV_UDP_PRIVATE_FIELDS
861 };
862
863 /* uv_udp_send_t is a subclass of uv_req_t */
864 struct uv_udp_send_s {
865   UV_REQ_FIELDS
866   uv_udp_t* handle;
867   uv_udp_send_cb cb;
868   UV_UDP_SEND_PRIVATE_FIELDS
869 };
870
871 /*
872  * Initialize a new UDP handle. The actual socket is created lazily.
873  * Returns 0 on success.
874  */
875 UV_EXTERN int uv_udp_init(uv_loop_t*, uv_udp_t* handle);
876
877 /*
878  * Opens an existing file descriptor or SOCKET as a udp handle.
879  *
880  * Unix only:
881  *  The only requirement of the sock argument is that it follows the
882  *  datagram contract (works in unconnected mode, supports sendmsg()/recvmsg(),
883  *  etc.). In other words, other datagram-type sockets like raw sockets or
884  *  netlink sockets can also be passed to this function.
885  *
886  * This sets the SO_REUSEPORT socket flag on the BSDs and OS X. On other
887  * UNIX platforms, it sets the SO_REUSEADDR flag.  What that means is that
888  * multiple threads or processes can bind to the same address without error
889  * (provided they all set the flag) but only the last one to bind will receive
890  * any traffic, in effect "stealing" the port from the previous listener.
891  * This behavior is something of an anomaly and may be replaced by an explicit
892  * opt-in mechanism in future versions of libuv.
893  */
894 UV_EXTERN int uv_udp_open(uv_udp_t* handle, uv_os_sock_t sock);
895
896 /*
897  * Bind to a IPv4 address and port.
898  *
899  * Arguments:
900  *  handle    UDP handle. Should have been initialized with `uv_udp_init`.
901  *  addr      struct sockaddr_in or struct sockaddr_in6 with the address and
902  *            port to bind to.
903  *  flags     Unused.
904  *
905  * Returns:
906  *  0 on success, or an error code < 0 on failure.
907  *
908  * This sets the SO_REUSEPORT socket flag on the BSDs and OS X. On other
909  * UNIX platforms, it sets the SO_REUSEADDR flag.  What that means is that
910  * multiple threads or processes can bind to the same address without error
911  * (provided they all set the flag) but only the last one to bind will receive
912  * any traffic, in effect "stealing" the port from the previous listener.
913  * This behavior is something of an anomaly and may be replaced by an explicit
914  * opt-in mechanism in future versions of libuv.
915  */
916 UV_EXTERN int uv_udp_bind(uv_udp_t* handle,
917                           const struct sockaddr* addr,
918                           unsigned int flags);
919
920 UV_EXTERN int uv_udp_getsockname(uv_udp_t* handle, struct sockaddr* name,
921     int* namelen);
922
923 /*
924  * Set membership for a multicast address
925  *
926  * Arguments:
927  *  handle              UDP handle. Should have been initialized with
928  *                      `uv_udp_init`.
929  *  multicast_addr      multicast address to set membership for
930  *  interface_addr      interface address
931  *  membership          Should be UV_JOIN_GROUP or UV_LEAVE_GROUP
932  *
933  * Returns:
934  *  0 on success, or an error code < 0 on failure.
935  */
936 UV_EXTERN int uv_udp_set_membership(uv_udp_t* handle,
937     const char* multicast_addr, const char* interface_addr,
938     uv_membership membership);
939
940 /*
941  * Set IP multicast loop flag. Makes multicast packets loop back to
942  * local sockets.
943  *
944  * Arguments:
945  *  handle              UDP handle. Should have been initialized with
946  *                      `uv_udp_init`.
947  *  on                  1 for on, 0 for off
948  *
949  * Returns:
950  *  0 on success, or an error code < 0 on failure.
951  */
952 UV_EXTERN int uv_udp_set_multicast_loop(uv_udp_t* handle, int on);
953
954 /*
955  * Set the multicast ttl
956  *
957  * Arguments:
958  *  handle              UDP handle. Should have been initialized with
959  *                      `uv_udp_init`.
960  *  ttl                 1 through 255
961  *
962  * Returns:
963  *  0 on success, or an error code < 0 on failure.
964  */
965 UV_EXTERN int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl);
966
967 /*
968  * Set broadcast on or off
969  *
970  * Arguments:
971  *  handle              UDP handle. Should have been initialized with
972  *                      `uv_udp_init`.
973  *  on                  1 for on, 0 for off
974  *
975  * Returns:
976  *  0 on success, or an error code < 0 on failure.
977  */
978 UV_EXTERN int uv_udp_set_broadcast(uv_udp_t* handle, int on);
979
980 /*
981  * Set the time to live
982  *
983  * Arguments:
984  *  handle              UDP handle. Should have been initialized with
985  *                      `uv_udp_init`.
986  *  ttl                 1 through 255
987  *
988  * Returns:
989  *  0 on success, or an error code < 0 on failure.
990  */
991 UV_EXTERN int uv_udp_set_ttl(uv_udp_t* handle, int ttl);
992
993 /*
994  * Send data. If the socket has not previously been bound with `uv_udp_bind`
995  * or `uv_udp_bind6`, it is bound to 0.0.0.0 (the "all interfaces" address)
996  * and a random port number.
997  *
998  * Arguments:
999  *  req       UDP request handle. Need not be initialized.
1000  *  handle    UDP handle. Should have been initialized with `uv_udp_init`.
1001  *  bufs      List of buffers to send.
1002  *  nbufs     Number of buffers in `bufs`.
1003  *  addr      Address of the remote peer. See `uv_ip4_addr`.
1004  *  send_cb   Callback to invoke when the data has been sent out.
1005  *
1006  * Returns:
1007  *  0 on success, or an error code < 0 on failure.
1008  */
1009 UV_EXTERN int uv_udp_send(uv_udp_send_t* req,
1010                           uv_udp_t* handle,
1011                           const uv_buf_t bufs[],
1012                           unsigned int nbufs,
1013                           const struct sockaddr* addr,
1014                           uv_udp_send_cb send_cb);
1015
1016 /*
1017  * Receive data. If the socket has not previously been bound with `uv_udp_bind`
1018  * or `uv_udp_bind6`, it is bound to 0.0.0.0 (the "all interfaces" address)
1019  * and a random port number.
1020  *
1021  * Arguments:
1022  *  handle    UDP handle. Should have been initialized with `uv_udp_init`.
1023  *  alloc_cb  Callback to invoke when temporary storage is needed.
1024  *  recv_cb   Callback to invoke with received data.
1025  *
1026  * Returns:
1027  *  0 on success, or an error code < 0 on failure.
1028  */
1029 UV_EXTERN int uv_udp_recv_start(uv_udp_t* handle, uv_alloc_cb alloc_cb,
1030     uv_udp_recv_cb recv_cb);
1031
1032 /*
1033  * Stop listening for incoming datagrams.
1034  *
1035  * Arguments:
1036  *  handle    UDP handle. Should have been initialized with `uv_udp_init`.
1037  *
1038  * Returns:
1039  *  0 on success, or an error code < 0 on failure.
1040  */
1041 UV_EXTERN int uv_udp_recv_stop(uv_udp_t* handle);
1042
1043
1044 /*
1045  * uv_tty_t is a subclass of uv_stream_t
1046  *
1047  * Representing a stream for the console.
1048  */
1049 struct uv_tty_s {
1050   UV_HANDLE_FIELDS
1051   UV_STREAM_FIELDS
1052   UV_TTY_PRIVATE_FIELDS
1053 };
1054
1055 /*
1056  * Initialize a new TTY stream with the given file descriptor. Usually the
1057  * file descriptor will be
1058  *   0 = stdin
1059  *   1 = stdout
1060  *   2 = stderr
1061  * The last argument, readable, specifies if you plan on calling
1062  * uv_read_start with this stream. stdin is readable, stdout is not.
1063  *
1064  * TTY streams which are not readable have blocking writes.
1065  */
1066 UV_EXTERN int uv_tty_init(uv_loop_t*, uv_tty_t*, uv_file fd, int readable);
1067
1068 /*
1069  * Set mode. 0 for normal, 1 for raw.
1070  */
1071 UV_EXTERN int uv_tty_set_mode(uv_tty_t*, int mode);
1072
1073 /*
1074  * To be called when the program exits. Resets TTY settings to default
1075  * values for the next process to take over.
1076  *
1077  * This function is async signal-safe on UNIX platforms but can fail with error
1078  * code UV_EBUSY if you call it when execution is inside uv_tty_set_mode().
1079  */
1080 UV_EXTERN int uv_tty_reset_mode(void);
1081
1082 /*
1083  * Gets the current Window size. On success zero is returned.
1084  */
1085 UV_EXTERN int uv_tty_get_winsize(uv_tty_t*, int* width, int* height);
1086
1087 /*
1088  * Used to detect what type of stream should be used with a given file
1089  * descriptor. Usually this will be used during initialization to guess the
1090  * type of the stdio streams.
1091  * For isatty() functionality use this function and test for UV_TTY.
1092  */
1093 UV_EXTERN uv_handle_type uv_guess_handle(uv_file file);
1094
1095 /*
1096  * uv_pipe_t is a subclass of uv_stream_t
1097  *
1098  * Representing a pipe stream or pipe server. On Windows this is a Named
1099  * Pipe. On Unix this is a UNIX domain socket.
1100  */
1101 struct uv_pipe_s {
1102   UV_HANDLE_FIELDS
1103   UV_STREAM_FIELDS
1104   int ipc; /* non-zero if this pipe is used for passing handles */
1105   UV_PIPE_PRIVATE_FIELDS
1106 };
1107
1108 /*
1109  * Initialize a pipe. The last argument is a boolean to indicate if
1110  * this pipe will be used for handle passing between processes.
1111  */
1112 UV_EXTERN int uv_pipe_init(uv_loop_t*, uv_pipe_t* handle, int ipc);
1113
1114 /*
1115  * Opens an existing file descriptor or HANDLE as a pipe.
1116  */
1117 UV_EXTERN int uv_pipe_open(uv_pipe_t*, uv_file file);
1118
1119 /*
1120  * Bind the pipe to a file path (UNIX) or a name (Windows.)
1121  *
1122  * Paths on UNIX get truncated to `sizeof(sockaddr_un.sun_path)` bytes,
1123  * typically between 92 and 108 bytes.
1124  */
1125 UV_EXTERN int uv_pipe_bind(uv_pipe_t* handle, const char* name);
1126
1127 /*
1128  * Connect to the UNIX domain socket or the named pipe.
1129  *
1130  * Paths on UNIX get truncated to `sizeof(sockaddr_un.sun_path)` bytes,
1131  * typically between 92 and 108 bytes.
1132  */
1133 UV_EXTERN void uv_pipe_connect(uv_connect_t* req, uv_pipe_t* handle,
1134     const char* name, uv_connect_cb cb);
1135
1136 /*
1137  * This setting applies to Windows only.
1138  * Set the number of pending pipe instance handles when the pipe server
1139  * is waiting for connections.
1140  */
1141 UV_EXTERN void uv_pipe_pending_instances(uv_pipe_t* handle, int count);
1142
1143
1144 /*
1145  * uv_poll_t is a subclass of uv_handle_t.
1146  *
1147  * The uv_poll watcher is used to watch file descriptors for readability and
1148  * writability, similar to the purpose of poll(2).
1149  *
1150  * The purpose of uv_poll is to enable integrating external libraries that
1151  * rely on the event loop to signal it about the socket status changes, like
1152  * c-ares or libssh2. Using uv_poll_t for any other other purpose is not
1153  * recommended; uv_tcp_t, uv_udp_t, etc. provide an implementation that is
1154  * much faster and more scalable than what can be achieved with uv_poll_t,
1155  * especially on Windows.
1156  *
1157  * It is possible that uv_poll occasionally signals that a file descriptor is
1158  * readable or writable even when it isn't. The user should therefore always
1159  * be prepared to handle EAGAIN or equivalent when it attempts to read from or
1160  * write to the fd.
1161  *
1162  * It is not okay to have multiple active uv_poll watchers for the same socket.
1163  * This can cause libuv to busyloop or otherwise malfunction.
1164  *
1165  * The user should not close a file descriptor while it is being polled by an
1166  * active uv_poll watcher. This can cause the poll watcher to report an error,
1167  * but it might also start polling another socket. However the fd can be safely
1168  * closed immediately after a call to uv_poll_stop() or uv_close().
1169  *
1170  * On windows only sockets can be polled with uv_poll. On unix any file
1171  * descriptor that would be accepted by poll(2) can be used with uv_poll.
1172  */
1173 struct uv_poll_s {
1174   UV_HANDLE_FIELDS
1175   uv_poll_cb poll_cb;
1176   UV_POLL_PRIVATE_FIELDS
1177 };
1178
1179 enum uv_poll_event {
1180   UV_READABLE = 1,
1181   UV_WRITABLE = 2
1182 };
1183
1184 /* Initialize the poll watcher using a file descriptor. */
1185 UV_EXTERN int uv_poll_init(uv_loop_t* loop, uv_poll_t* handle, int fd);
1186
1187 /* Initialize the poll watcher using a socket descriptor. On unix this is */
1188 /* identical to uv_poll_init. On windows it takes a SOCKET handle. */
1189 UV_EXTERN int uv_poll_init_socket(uv_loop_t* loop, uv_poll_t* handle,
1190     uv_os_sock_t socket);
1191
1192 /*
1193  * Starts polling the file descriptor. `events` is a bitmask consisting made up
1194  * of UV_READABLE and UV_WRITABLE. As soon as an event is detected the callback
1195  * will be called with `status` set to 0, and the detected events set en the
1196  * `events` field.
1197  *
1198  * If an error happens while polling status, `status` < 0 and corresponds
1199  * with one of the UV_E* error codes. The user should not close the socket
1200  * while uv_poll is active. If the user does that anyway, the callback *may*
1201  * be called reporting an error status, but this is not guaranteed.
1202  *
1203  * Calling uv_poll_start on an uv_poll watcher that is already active is fine.
1204  * Doing so will update the events mask that is being watched for.
1205  */
1206 UV_EXTERN int uv_poll_start(uv_poll_t* handle, int events, uv_poll_cb cb);
1207
1208 /* Stops polling the file descriptor. */
1209 UV_EXTERN int uv_poll_stop(uv_poll_t* handle);
1210
1211
1212 /*
1213  * uv_prepare_t is a subclass of uv_handle_t.
1214  *
1215  * Every active prepare handle gets its callback called exactly once per loop
1216  * iteration, just before the system blocks to wait for completed i/o.
1217  */
1218 struct uv_prepare_s {
1219   UV_HANDLE_FIELDS
1220   UV_PREPARE_PRIVATE_FIELDS
1221 };
1222
1223 UV_EXTERN int uv_prepare_init(uv_loop_t*, uv_prepare_t* prepare);
1224
1225 UV_EXTERN int uv_prepare_start(uv_prepare_t* prepare, uv_prepare_cb cb);
1226
1227 UV_EXTERN int uv_prepare_stop(uv_prepare_t* prepare);
1228
1229
1230 /*
1231  * uv_check_t is a subclass of uv_handle_t.
1232  *
1233  * Every active check handle gets its callback called exactly once per loop
1234  * iteration, just after the system returns from blocking.
1235  */
1236 struct uv_check_s {
1237   UV_HANDLE_FIELDS
1238   UV_CHECK_PRIVATE_FIELDS
1239 };
1240
1241 UV_EXTERN int uv_check_init(uv_loop_t*, uv_check_t* check);
1242
1243 UV_EXTERN int uv_check_start(uv_check_t* check, uv_check_cb cb);
1244
1245 UV_EXTERN int uv_check_stop(uv_check_t* check);
1246
1247
1248 /*
1249  * uv_idle_t is a subclass of uv_handle_t.
1250  *
1251  * Every active idle handle gets its callback called repeatedly until it is
1252  * stopped. This happens after all other types of callbacks are processed.
1253  * When there are multiple "idle" handles active, their callbacks are called
1254  * in turn.
1255  */
1256 struct uv_idle_s {
1257   UV_HANDLE_FIELDS
1258   UV_IDLE_PRIVATE_FIELDS
1259 };
1260
1261 UV_EXTERN int uv_idle_init(uv_loop_t*, uv_idle_t* idle);
1262
1263 UV_EXTERN int uv_idle_start(uv_idle_t* idle, uv_idle_cb cb);
1264
1265 UV_EXTERN int uv_idle_stop(uv_idle_t* idle);
1266
1267
1268 /*
1269  * uv_async_t is a subclass of uv_handle_t.
1270  *
1271  * uv_async_send wakes up the event loop and calls the async handle's callback.
1272  * There is no guarantee that every uv_async_send call leads to exactly one
1273  * invocation of the callback; the only guarantee is that the callback function
1274  * is called at least once after the call to async_send. Unlike all other
1275  * libuv functions, uv_async_send can be called from another thread.
1276  */
1277 struct uv_async_s {
1278   UV_HANDLE_FIELDS
1279   UV_ASYNC_PRIVATE_FIELDS
1280 };
1281
1282 /*
1283  * Initialize the uv_async_t handle. A NULL callback is allowed.
1284  *
1285  * Note that uv_async_init(), unlike other libuv functions, immediately
1286  * starts the handle. To stop the handle again, close it with uv_close().
1287  */
1288 UV_EXTERN int uv_async_init(uv_loop_t*, uv_async_t* async,
1289     uv_async_cb async_cb);
1290
1291 /*
1292  * This can be called from other threads to wake up a libuv thread.
1293  *
1294  * libuv is single threaded at the moment.
1295  */
1296 UV_EXTERN int uv_async_send(uv_async_t* async);
1297
1298
1299 /*
1300  * uv_timer_t is a subclass of uv_handle_t.
1301  *
1302  * Used to get woken up at a specified time in the future.
1303  */
1304 struct uv_timer_s {
1305   UV_HANDLE_FIELDS
1306   UV_TIMER_PRIVATE_FIELDS
1307 };
1308
1309 UV_EXTERN int uv_timer_init(uv_loop_t*, uv_timer_t* handle);
1310
1311 /*
1312  * Start the timer. `timeout` and `repeat` are in milliseconds.
1313  *
1314  * If timeout is zero, the callback fires on the next tick of the event loop.
1315  *
1316  * If repeat is non-zero, the callback fires first after timeout milliseconds
1317  * and then repeatedly after repeat milliseconds.
1318  */
1319 UV_EXTERN int uv_timer_start(uv_timer_t* handle,
1320                              uv_timer_cb cb,
1321                              uint64_t timeout,
1322                              uint64_t repeat);
1323
1324 UV_EXTERN int uv_timer_stop(uv_timer_t* handle);
1325
1326 /*
1327  * Stop the timer, and if it is repeating restart it using the repeat value
1328  * as the timeout. If the timer has never been started before it returns
1329  * UV_EINVAL.
1330  */
1331 UV_EXTERN int uv_timer_again(uv_timer_t* handle);
1332
1333 /*
1334  * Set the repeat value in milliseconds. Note that if the repeat value is set
1335  * from a timer callback it does not immediately take effect. If the timer was
1336  * non-repeating before, it will have been stopped. If it was repeating, then
1337  * the old repeat value will have been used to schedule the next timeout.
1338  */
1339 UV_EXTERN void uv_timer_set_repeat(uv_timer_t* handle, uint64_t repeat);
1340
1341 UV_EXTERN uint64_t uv_timer_get_repeat(const uv_timer_t* handle);
1342
1343
1344 /*
1345  * uv_getaddrinfo_t is a subclass of uv_req_t
1346  *
1347  * Request object for uv_getaddrinfo.
1348  */
1349 struct uv_getaddrinfo_s {
1350   UV_REQ_FIELDS
1351   /* read-only */
1352   uv_loop_t* loop;
1353   UV_GETADDRINFO_PRIVATE_FIELDS
1354 };
1355
1356
1357 /*
1358  * Asynchronous getaddrinfo(3).
1359  *
1360  * Either node or service may be NULL but not both.
1361  *
1362  * hints is a pointer to a struct addrinfo with additional address type
1363  * constraints, or NULL. Consult `man -s 3 getaddrinfo` for details.
1364  *
1365  * Returns 0 on success or an error code < 0 on failure.
1366  *
1367  * If successful, your callback gets called sometime in the future with the
1368  * lookup result, which is either:
1369  *
1370  *  a) err == 0, the res argument points to a valid struct addrinfo, or
1371  *  b) err < 0, the res argument is NULL. See the UV_EAI_* constants.
1372  *
1373  * Call uv_freeaddrinfo() to free the addrinfo structure.
1374  */
1375 UV_EXTERN int uv_getaddrinfo(uv_loop_t* loop,
1376                              uv_getaddrinfo_t* req,
1377                              uv_getaddrinfo_cb getaddrinfo_cb,
1378                              const char* node,
1379                              const char* service,
1380                              const struct addrinfo* hints);
1381
1382 /*
1383  * Free the struct addrinfo. Passing NULL is allowed and is a no-op.
1384  */
1385 UV_EXTERN void uv_freeaddrinfo(struct addrinfo* ai);
1386
1387
1388 /* uv_spawn() options */
1389 typedef enum {
1390   UV_IGNORE         = 0x00,
1391   UV_CREATE_PIPE    = 0x01,
1392   UV_INHERIT_FD     = 0x02,
1393   UV_INHERIT_STREAM = 0x04,
1394
1395   /* When UV_CREATE_PIPE is specified, UV_READABLE_PIPE and UV_WRITABLE_PIPE
1396    * determine the direction of flow, from the child process' perspective. Both
1397    * flags may be specified to create a duplex data stream.
1398    */
1399   UV_READABLE_PIPE  = 0x10,
1400   UV_WRITABLE_PIPE  = 0x20
1401 } uv_stdio_flags;
1402
1403 typedef struct uv_stdio_container_s {
1404   uv_stdio_flags flags;
1405
1406   union {
1407     uv_stream_t* stream;
1408     int fd;
1409   } data;
1410 } uv_stdio_container_t;
1411
1412 typedef struct uv_process_options_s {
1413   uv_exit_cb exit_cb; /* Called after the process exits. */
1414   const char* file; /* Path to program to execute. */
1415   /*
1416    * Command line arguments. args[0] should be the path to the program. On
1417    * Windows this uses CreateProcess which concatenates the arguments into a
1418    * string this can cause some strange errors. See the note at
1419    * windows_verbatim_arguments.
1420    */
1421   char** args;
1422   /*
1423    * This will be set as the environ variable in the subprocess. If this is
1424    * NULL then the parents environ will be used.
1425    */
1426   char** env;
1427   /*
1428    * If non-null this represents a directory the subprocess should execute
1429    * in. Stands for current working directory.
1430    */
1431   const char* cwd;
1432   /*
1433    * Various flags that control how uv_spawn() behaves. See the definition of
1434    * `enum uv_process_flags` below.
1435    */
1436   unsigned int flags;
1437   /*
1438    * The `stdio` field points to an array of uv_stdio_container_t structs that
1439    * describe the file descriptors that will be made available to the child
1440    * process. The convention is that stdio[0] points to stdin, fd 1 is used for
1441    * stdout, and fd 2 is stderr.
1442    *
1443    * Note that on windows file descriptors greater than 2 are available to the
1444    * child process only if the child processes uses the MSVCRT runtime.
1445    */
1446   int stdio_count;
1447   uv_stdio_container_t* stdio;
1448   /*
1449    * Libuv can change the child process' user/group id. This happens only when
1450    * the appropriate bits are set in the flags fields. This is not supported on
1451    * windows; uv_spawn() will fail and set the error to UV_ENOTSUP.
1452    */
1453   uv_uid_t uid;
1454   uv_gid_t gid;
1455 } uv_process_options_t;
1456
1457 /*
1458  * These are the flags that can be used for the uv_process_options.flags field.
1459  */
1460 enum uv_process_flags {
1461   /*
1462    * Set the child process' user id. The user id is supplied in the `uid` field
1463    * of the options struct. This does not work on windows; setting this flag
1464    * will cause uv_spawn() to fail.
1465    */
1466   UV_PROCESS_SETUID = (1 << 0),
1467   /*
1468    * Set the child process' group id. The user id is supplied in the `gid`
1469    * field of the options struct. This does not work on windows; setting this
1470    * flag will cause uv_spawn() to fail.
1471    */
1472   UV_PROCESS_SETGID = (1 << 1),
1473   /*
1474    * Do not wrap any arguments in quotes, or perform any other escaping, when
1475    * converting the argument list into a command line string. This option is
1476    * only meaningful on Windows systems. On unix it is silently ignored.
1477    */
1478   UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS = (1 << 2),
1479   /*
1480    * Spawn the child process in a detached state - this will make it a process
1481    * group leader, and will effectively enable the child to keep running after
1482    * the parent exits.  Note that the child process will still keep the
1483    * parent's event loop alive unless the parent process calls uv_unref() on
1484    * the child's process handle.
1485    */
1486   UV_PROCESS_DETACHED = (1 << 3),
1487   /*
1488    * Hide the subprocess console window that would normally be created. This
1489    * option is only meaningful on Windows systems. On unix it is silently
1490    * ignored.
1491    */
1492   UV_PROCESS_WINDOWS_HIDE = (1 << 4)
1493 };
1494
1495 /*
1496  * uv_process_t is a subclass of uv_handle_t
1497  */
1498 struct uv_process_s {
1499   UV_HANDLE_FIELDS
1500   uv_exit_cb exit_cb;
1501   int pid;
1502   UV_PROCESS_PRIVATE_FIELDS
1503 };
1504
1505 /*
1506  * Initializes the uv_process_t and starts the process. If the process is
1507  * successfully spawned, then this function will return 0. Otherwise, the
1508  * negative error code corresponding to the reason it couldn't spawn is
1509  * returned.
1510  *
1511  * Possible reasons for failing to spawn would include (but not be limited to)
1512  * the file to execute not existing, not having permissions to use the setuid or
1513  * setgid specified, or not having enough memory to allocate for the new
1514  * process.
1515  */
1516 UV_EXTERN int uv_spawn(uv_loop_t* loop,
1517                        uv_process_t* handle,
1518                        const uv_process_options_t* options);
1519
1520
1521 /*
1522  * Kills the process with the specified signal. The user must still
1523  * call uv_close on the process.
1524  */
1525 UV_EXTERN int uv_process_kill(uv_process_t*, int signum);
1526
1527
1528 /* Kills the process with the specified signal. */
1529 UV_EXTERN int uv_kill(int pid, int signum);
1530
1531
1532 /*
1533  * uv_work_t is a subclass of uv_req_t
1534  */
1535 struct uv_work_s {
1536   UV_REQ_FIELDS
1537   uv_loop_t* loop;
1538   uv_work_cb work_cb;
1539   uv_after_work_cb after_work_cb;
1540   UV_WORK_PRIVATE_FIELDS
1541 };
1542
1543 /* Queues a work request to execute asynchronously on the thread pool. */
1544 UV_EXTERN int uv_queue_work(uv_loop_t* loop, uv_work_t* req,
1545     uv_work_cb work_cb, uv_after_work_cb after_work_cb);
1546
1547 /* Cancel a pending request. Fails if the request is executing or has finished
1548  * executing.
1549  *
1550  * Returns 0 on success, or an error code < 0 on failure.
1551  *
1552  * Only cancellation of uv_fs_t, uv_getaddrinfo_t and uv_work_t requests is
1553  * currently supported.
1554  *
1555  * Cancelled requests have their callbacks invoked some time in the future.
1556  * It's _not_ safe to free the memory associated with the request until your
1557  * callback is called.
1558  *
1559  * Here is how cancellation is reported to your callback:
1560  *
1561  * - A uv_fs_t request has its req->result field set to UV_ECANCELED.
1562  *
1563  * - A uv_work_t or uv_getaddrinfo_t request has its callback invoked with
1564  *   status == UV_ECANCELED.
1565  *
1566  * This function is currently only implemented on UNIX platforms. On Windows,
1567  * it always returns UV_ENOSYS.
1568  */
1569 UV_EXTERN int uv_cancel(uv_req_t* req);
1570
1571
1572 struct uv_cpu_info_s {
1573   char* model;
1574   int speed;
1575   struct uv_cpu_times_s {
1576     uint64_t user;
1577     uint64_t nice;
1578     uint64_t sys;
1579     uint64_t idle;
1580     uint64_t irq;
1581   } cpu_times;
1582 };
1583
1584 struct uv_interface_address_s {
1585   char* name;
1586   char phys_addr[6];
1587   int is_internal;
1588   union {
1589     struct sockaddr_in address4;
1590     struct sockaddr_in6 address6;
1591   } address;
1592   union {
1593     struct sockaddr_in netmask4;
1594     struct sockaddr_in6 netmask6;
1595   } netmask;
1596 };
1597
1598 UV_EXTERN char** uv_setup_args(int argc, char** argv);
1599 UV_EXTERN int uv_get_process_title(char* buffer, size_t size);
1600 UV_EXTERN int uv_set_process_title(const char* title);
1601 UV_EXTERN int uv_resident_set_memory(size_t* rss);
1602 UV_EXTERN int uv_uptime(double* uptime);
1603
1604 /*
1605  * This allocates cpu_infos array, and sets count.  The array
1606  * is freed using uv_free_cpu_info().
1607  */
1608 UV_EXTERN int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count);
1609 UV_EXTERN void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count);
1610
1611 /*
1612  * This allocates addresses array, and sets count.  The array
1613  * is freed using uv_free_interface_addresses().
1614  */
1615 UV_EXTERN int uv_interface_addresses(uv_interface_address_t** addresses,
1616   int* count);
1617 UV_EXTERN void uv_free_interface_addresses(uv_interface_address_t* addresses,
1618   int count);
1619
1620 /*
1621  * File System Methods.
1622  *
1623  * The uv_fs_* functions execute a blocking system call asynchronously (in a
1624  * thread pool) and call the specified callback in the specified loop after
1625  * completion. If the user gives NULL as the callback the blocking system
1626  * call will be called synchronously. req should be a pointer to an
1627  * uninitialized uv_fs_t object.
1628  *
1629  * uv_fs_req_cleanup() must be called after completion of the uv_fs_
1630  * function to free any internal memory allocations associated with the
1631  * request.
1632  */
1633
1634 typedef enum {
1635   UV_FS_UNKNOWN = -1,
1636   UV_FS_CUSTOM,
1637   UV_FS_OPEN,
1638   UV_FS_CLOSE,
1639   UV_FS_READ,
1640   UV_FS_WRITE,
1641   UV_FS_SENDFILE,
1642   UV_FS_STAT,
1643   UV_FS_LSTAT,
1644   UV_FS_FSTAT,
1645   UV_FS_FTRUNCATE,
1646   UV_FS_UTIME,
1647   UV_FS_FUTIME,
1648   UV_FS_CHMOD,
1649   UV_FS_FCHMOD,
1650   UV_FS_FSYNC,
1651   UV_FS_FDATASYNC,
1652   UV_FS_UNLINK,
1653   UV_FS_RMDIR,
1654   UV_FS_MKDIR,
1655   UV_FS_RENAME,
1656   UV_FS_READDIR,
1657   UV_FS_LINK,
1658   UV_FS_SYMLINK,
1659   UV_FS_READLINK,
1660   UV_FS_CHOWN,
1661   UV_FS_FCHOWN
1662 } uv_fs_type;
1663
1664 /* uv_fs_t is a subclass of uv_req_t */
1665 struct uv_fs_s {
1666   UV_REQ_FIELDS
1667   uv_fs_type fs_type;
1668   uv_loop_t* loop;
1669   uv_fs_cb cb;
1670   ssize_t result;
1671   void* ptr;
1672   const char* path;
1673   uv_stat_t statbuf;  /* Stores the result of uv_fs_stat and uv_fs_fstat. */
1674   UV_FS_PRIVATE_FIELDS
1675 };
1676
1677 UV_EXTERN void uv_fs_req_cleanup(uv_fs_t* req);
1678
1679 UV_EXTERN int uv_fs_close(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1680     uv_fs_cb cb);
1681
1682 UV_EXTERN int uv_fs_open(uv_loop_t* loop, uv_fs_t* req, const char* path,
1683     int flags, int mode, uv_fs_cb cb);
1684
1685 UV_EXTERN int uv_fs_read(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1686     void* buf, size_t length, int64_t offset, uv_fs_cb cb);
1687
1688 UV_EXTERN int uv_fs_unlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
1689     uv_fs_cb cb);
1690
1691 UV_EXTERN int uv_fs_write(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1692     const void* buf, size_t length, int64_t offset, uv_fs_cb cb);
1693
1694 UV_EXTERN int uv_fs_mkdir(uv_loop_t* loop, uv_fs_t* req, const char* path,
1695     int mode, uv_fs_cb cb);
1696
1697 UV_EXTERN int uv_fs_rmdir(uv_loop_t* loop, uv_fs_t* req, const char* path,
1698     uv_fs_cb cb);
1699
1700 UV_EXTERN int uv_fs_readdir(uv_loop_t* loop, uv_fs_t* req,
1701     const char* path, int flags, uv_fs_cb cb);
1702
1703 UV_EXTERN int uv_fs_stat(uv_loop_t* loop, uv_fs_t* req, const char* path,
1704     uv_fs_cb cb);
1705
1706 UV_EXTERN int uv_fs_fstat(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1707     uv_fs_cb cb);
1708
1709 UV_EXTERN int uv_fs_rename(uv_loop_t* loop, uv_fs_t* req, const char* path,
1710     const char* new_path, uv_fs_cb cb);
1711
1712 UV_EXTERN int uv_fs_fsync(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1713     uv_fs_cb cb);
1714
1715 UV_EXTERN int uv_fs_fdatasync(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1716     uv_fs_cb cb);
1717
1718 UV_EXTERN int uv_fs_ftruncate(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1719     int64_t offset, uv_fs_cb cb);
1720
1721 UV_EXTERN int uv_fs_sendfile(uv_loop_t* loop, uv_fs_t* req, uv_file out_fd,
1722     uv_file in_fd, int64_t in_offset, size_t length, uv_fs_cb cb);
1723
1724 UV_EXTERN int uv_fs_chmod(uv_loop_t* loop, uv_fs_t* req, const char* path,
1725     int mode, uv_fs_cb cb);
1726
1727 UV_EXTERN int uv_fs_utime(uv_loop_t* loop, uv_fs_t* req, const char* path,
1728     double atime, double mtime, uv_fs_cb cb);
1729
1730 UV_EXTERN int uv_fs_futime(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1731     double atime, double mtime, uv_fs_cb cb);
1732
1733 UV_EXTERN int uv_fs_lstat(uv_loop_t* loop, uv_fs_t* req, const char* path,
1734     uv_fs_cb cb);
1735
1736 UV_EXTERN int uv_fs_link(uv_loop_t* loop, uv_fs_t* req, const char* path,
1737     const char* new_path, uv_fs_cb cb);
1738
1739 /*
1740  * This flag can be used with uv_fs_symlink on Windows
1741  * to specify whether path argument points to a directory.
1742  */
1743 #define UV_FS_SYMLINK_DIR          0x0001
1744
1745 /*
1746  * This flag can be used with uv_fs_symlink on Windows
1747  * to specify whether the symlink is to be created using junction points.
1748  */
1749 #define UV_FS_SYMLINK_JUNCTION     0x0002
1750
1751 UV_EXTERN int uv_fs_symlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
1752     const char* new_path, int flags, uv_fs_cb cb);
1753
1754 UV_EXTERN int uv_fs_readlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
1755     uv_fs_cb cb);
1756
1757 UV_EXTERN int uv_fs_fchmod(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1758     int mode, uv_fs_cb cb);
1759
1760 UV_EXTERN int uv_fs_chown(uv_loop_t* loop, uv_fs_t* req, const char* path,
1761     uv_uid_t uid, uv_gid_t gid, uv_fs_cb cb);
1762
1763 UV_EXTERN int uv_fs_fchown(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1764     uv_uid_t uid, uv_gid_t gid, uv_fs_cb cb);
1765
1766
1767 enum uv_fs_event {
1768   UV_RENAME = 1,
1769   UV_CHANGE = 2
1770 };
1771
1772
1773 struct uv_fs_event_s {
1774   UV_HANDLE_FIELDS
1775   char* filename;
1776   UV_FS_EVENT_PRIVATE_FIELDS
1777 };
1778
1779
1780 /*
1781  * uv_fs_stat() based polling file watcher.
1782  */
1783 struct uv_fs_poll_s {
1784   UV_HANDLE_FIELDS
1785   /* Private, don't touch. */
1786   void* poll_ctx;
1787 };
1788
1789 UV_EXTERN int uv_fs_poll_init(uv_loop_t* loop, uv_fs_poll_t* handle);
1790
1791 /*
1792  * Check the file at `path` for changes every `interval` milliseconds.
1793  *
1794  * Your callback is invoked with `status < 0` if `path` does not exist
1795  * or is inaccessible. The watcher is *not* stopped but your callback is
1796  * not called again until something changes (e.g. when the file is created
1797  * or the error reason changes).
1798  *
1799  * When `status == 0`, your callback receives pointers to the old and new
1800  * `uv_stat_t` structs. They are valid for the duration of the callback
1801  * only!
1802  *
1803  * For maximum portability, use multi-second intervals. Sub-second intervals
1804  * will not detect all changes on many file systems.
1805  */
1806 UV_EXTERN int uv_fs_poll_start(uv_fs_poll_t* handle,
1807                                uv_fs_poll_cb poll_cb,
1808                                const char* path,
1809                                unsigned int interval);
1810
1811 UV_EXTERN int uv_fs_poll_stop(uv_fs_poll_t* handle);
1812
1813
1814 /*
1815  * UNIX signal handling on a per-event loop basis. The implementation is not
1816  * ultra efficient so don't go creating a million event loops with a million
1817  * signal watchers.
1818  *
1819  * Note to Linux users: SIGRT0 and SIGRT1 (signals 32 and 33) are used by the
1820  * NPTL pthreads library to manage threads. Installing watchers for those
1821  * signals will lead to unpredictable behavior and is strongly discouraged.
1822  * Future versions of libuv may simply reject them.
1823  *
1824  * Some signal support is available on Windows:
1825  *
1826  *   SIGINT is normally delivered when the user presses CTRL+C. However, like
1827  *   on Unix, it is not generated when terminal raw mode is enabled.
1828  *
1829  *   SIGBREAK is delivered when the user pressed CTRL+BREAK.
1830  *
1831  *   SIGHUP is generated when the user closes the console window. On SIGHUP the
1832  *   program is given approximately 10 seconds to perform cleanup. After that
1833  *   Windows will unconditionally terminate it.
1834  *
1835  *   SIGWINCH is raised whenever libuv detects that the console has been
1836  *   resized. SIGWINCH is emulated by libuv when the program uses an uv_tty_t
1837  *   handle to write to the console. SIGWINCH may not always be delivered in a
1838  *   timely manner; libuv will only detect size changes when the cursor is
1839  *   being moved. When a readable uv_tty_handle is used in raw mode, resizing
1840  *   the console buffer will also trigger a SIGWINCH signal.
1841  *
1842  * Watchers for other signals can be successfully created, but these signals
1843  * are never generated. These signals are: SIGILL, SIGABRT, SIGFPE, SIGSEGV,
1844  * SIGTERM and SIGKILL.
1845  *
1846  * Note that calls to raise() or abort() to programmatically raise a signal are
1847  * not detected by libuv; these will not trigger a signal watcher.
1848  */
1849 struct uv_signal_s {
1850   UV_HANDLE_FIELDS
1851   uv_signal_cb signal_cb;
1852   int signum;
1853   UV_SIGNAL_PRIVATE_FIELDS
1854 };
1855
1856 UV_EXTERN int uv_signal_init(uv_loop_t* loop, uv_signal_t* handle);
1857
1858 UV_EXTERN int uv_signal_start(uv_signal_t* handle,
1859                               uv_signal_cb signal_cb,
1860                               int signum);
1861
1862 UV_EXTERN int uv_signal_stop(uv_signal_t* handle);
1863
1864
1865 /*
1866  * Gets load average.
1867  * See: http://en.wikipedia.org/wiki/Load_(computing)
1868  * Returns [0,0,0] on Windows.
1869  */
1870 UV_EXTERN void uv_loadavg(double avg[3]);
1871
1872
1873 /*
1874  * Flags to be passed to uv_fs_event_start.
1875  */
1876 enum uv_fs_event_flags {
1877   /*
1878    * By default, if the fs event watcher is given a directory name, we will
1879    * watch for all events in that directory. This flags overrides this behavior
1880    * and makes fs_event report only changes to the directory entry itself. This
1881    * flag does not affect individual files watched.
1882    * This flag is currently not implemented yet on any backend.
1883    */
1884   UV_FS_EVENT_WATCH_ENTRY = 1,
1885
1886   /*
1887    * By default uv_fs_event will try to use a kernel interface such as inotify
1888    * or kqueue to detect events. This may not work on remote filesystems such
1889    * as NFS mounts. This flag makes fs_event fall back to calling stat() on a
1890    * regular interval.
1891    * This flag is currently not implemented yet on any backend.
1892    */
1893   UV_FS_EVENT_STAT = 2,
1894
1895   /*
1896    * By default, event watcher, when watching directory, is not registering
1897    * (is ignoring) changes in it's subdirectories.
1898    * This flag will override this behaviour on platforms that support it.
1899    */
1900   UV_FS_EVENT_RECURSIVE = 4
1901 };
1902
1903
1904 UV_EXTERN int uv_fs_event_init(uv_loop_t* loop, uv_fs_event_t* handle);
1905
1906 UV_EXTERN int uv_fs_event_start(uv_fs_event_t* handle,
1907                                 uv_fs_event_cb cb,
1908                                 const char* filename,
1909                                 unsigned int flags);
1910
1911 UV_EXTERN int uv_fs_event_stop(uv_fs_event_t* handle);
1912
1913
1914 /* Utility */
1915
1916 /* Convert string ip addresses to binary structures */
1917 UV_EXTERN int uv_ip4_addr(const char* ip, int port, struct sockaddr_in* addr);
1918 UV_EXTERN int uv_ip6_addr(const char* ip, int port, struct sockaddr_in6* addr);
1919
1920 /* Convert binary addresses to strings */
1921 UV_EXTERN int uv_ip4_name(struct sockaddr_in* src, char* dst, size_t size);
1922 UV_EXTERN int uv_ip6_name(struct sockaddr_in6* src, char* dst, size_t size);
1923
1924 /* Cross-platform IPv6-capable implementation of the 'standard' inet_ntop */
1925 /* and inet_pton functions. On success they return 0. If an error */
1926 /* the target of the `dst` pointer is unmodified. */
1927 UV_EXTERN int uv_inet_ntop(int af, const void* src, char* dst, size_t size);
1928 UV_EXTERN int uv_inet_pton(int af, const char* src, void* dst);
1929
1930 /* Gets the executable path */
1931 UV_EXTERN int uv_exepath(char* buffer, size_t* size);
1932
1933 /* Gets the current working directory */
1934 UV_EXTERN int uv_cwd(char* buffer, size_t size);
1935
1936 /* Changes the current working directory */
1937 UV_EXTERN int uv_chdir(const char* dir);
1938
1939 /* Gets memory info in bytes */
1940 UV_EXTERN uint64_t uv_get_free_memory(void);
1941 UV_EXTERN uint64_t uv_get_total_memory(void);
1942
1943 /*
1944  * Returns the current high-resolution real time. This is expressed in
1945  * nanoseconds. It is relative to an arbitrary time in the past. It is not
1946  * related to the time of day and therefore not subject to clock drift. The
1947  * primary use is for measuring performance between intervals.
1948  *
1949  * Note not every platform can support nanosecond resolution; however, this
1950  * value will always be in nanoseconds.
1951  */
1952 UV_EXTERN extern uint64_t uv_hrtime(void);
1953
1954
1955 /*
1956  * Disables inheritance for file descriptors / handles that this process
1957  * inherited from its parent. The effect is that child processes spawned by
1958  * this process don't accidentally inherit these handles.
1959  *
1960  * It is recommended to call this function as early in your program as possible,
1961  * before the inherited file descriptors can be closed or duplicated.
1962  *
1963  * Note that this function works on a best-effort basis: there is no guarantee
1964  * that libuv can discover all file descriptors that were inherited. In general
1965  * it does a better job on Windows than it does on unix.
1966  */
1967 UV_EXTERN void uv_disable_stdio_inheritance(void);
1968
1969 /*
1970  * Opens a shared library. The filename is in utf-8. Returns 0 on success and
1971  * -1 on error. Call `uv_dlerror(uv_lib_t*)` to get the error message.
1972  */
1973 UV_EXTERN int uv_dlopen(const char* filename, uv_lib_t* lib);
1974
1975 /*
1976  * Close the shared library.
1977  */
1978 UV_EXTERN void uv_dlclose(uv_lib_t* lib);
1979
1980 /*
1981  * Retrieves a data pointer from a dynamic library. It is legal for a symbol to
1982  * map to NULL. Returns 0 on success and -1 if the symbol was not found.
1983  */
1984 UV_EXTERN int uv_dlsym(uv_lib_t* lib, const char* name, void** ptr);
1985
1986 /*
1987  * Returns the last uv_dlopen() or uv_dlsym() error message.
1988  */
1989 UV_EXTERN const char* uv_dlerror(uv_lib_t* lib);
1990
1991 /*
1992  * The mutex functions return 0 on success or an error code < 0
1993  * (unless the return type is void, of course).
1994  */
1995 UV_EXTERN int uv_mutex_init(uv_mutex_t* handle);
1996 UV_EXTERN void uv_mutex_destroy(uv_mutex_t* handle);
1997 UV_EXTERN void uv_mutex_lock(uv_mutex_t* handle);
1998 UV_EXTERN int uv_mutex_trylock(uv_mutex_t* handle);
1999 UV_EXTERN void uv_mutex_unlock(uv_mutex_t* handle);
2000
2001 /*
2002  * Same goes for the read/write lock functions.
2003  */
2004 UV_EXTERN int uv_rwlock_init(uv_rwlock_t* rwlock);
2005 UV_EXTERN void uv_rwlock_destroy(uv_rwlock_t* rwlock);
2006 UV_EXTERN void uv_rwlock_rdlock(uv_rwlock_t* rwlock);
2007 UV_EXTERN int uv_rwlock_tryrdlock(uv_rwlock_t* rwlock);
2008 UV_EXTERN void uv_rwlock_rdunlock(uv_rwlock_t* rwlock);
2009 UV_EXTERN void uv_rwlock_wrlock(uv_rwlock_t* rwlock);
2010 UV_EXTERN int uv_rwlock_trywrlock(uv_rwlock_t* rwlock);
2011 UV_EXTERN void uv_rwlock_wrunlock(uv_rwlock_t* rwlock);
2012
2013 /*
2014  * Same goes for the semaphore functions.
2015  */
2016 UV_EXTERN int uv_sem_init(uv_sem_t* sem, unsigned int value);
2017 UV_EXTERN void uv_sem_destroy(uv_sem_t* sem);
2018 UV_EXTERN void uv_sem_post(uv_sem_t* sem);
2019 UV_EXTERN void uv_sem_wait(uv_sem_t* sem);
2020 UV_EXTERN int uv_sem_trywait(uv_sem_t* sem);
2021
2022 /*
2023  * Same goes for the condition variable functions.
2024  */
2025 UV_EXTERN int uv_cond_init(uv_cond_t* cond);
2026 UV_EXTERN void uv_cond_destroy(uv_cond_t* cond);
2027 UV_EXTERN void uv_cond_signal(uv_cond_t* cond);
2028 UV_EXTERN void uv_cond_broadcast(uv_cond_t* cond);
2029 /* Waits on a condition variable without a timeout.
2030  *
2031  * Note:
2032  * 1. callers should be prepared to deal with spurious wakeups.
2033  */
2034 UV_EXTERN void uv_cond_wait(uv_cond_t* cond, uv_mutex_t* mutex);
2035 /* Waits on a condition variable with a timeout in nano seconds.
2036  * Returns 0 for success or UV_ETIMEDOUT on timeout, It aborts when other
2037  * errors happen.
2038  *
2039  * Note:
2040  * 1. callers should be prepared to deal with spurious wakeups.
2041  * 2. the granularity of timeout on Windows is never less than one millisecond.
2042  * 3. uv_cond_timedwait takes a relative timeout, not an absolute time.
2043  */
2044 UV_EXTERN int uv_cond_timedwait(uv_cond_t* cond, uv_mutex_t* mutex,
2045     uint64_t timeout);
2046
2047 UV_EXTERN int uv_barrier_init(uv_barrier_t* barrier, unsigned int count);
2048 UV_EXTERN void uv_barrier_destroy(uv_barrier_t* barrier);
2049 UV_EXTERN void uv_barrier_wait(uv_barrier_t* barrier);
2050
2051 /* Runs a function once and only once. Concurrent calls to uv_once() with the
2052  * same guard will block all callers except one (it's unspecified which one).
2053  * The guard should be initialized statically with the UV_ONCE_INIT macro.
2054  */
2055 UV_EXTERN void uv_once(uv_once_t* guard, void (*callback)(void));
2056
2057 /* Thread-local storage.  These functions largely follow the semantics of
2058  * pthread_key_create(), pthread_key_delete(), pthread_getspecific() and
2059  * pthread_setspecific().
2060  *
2061  * Note that the total thread-local storage size may be limited.
2062  * That is, it may not be possible to create many TLS keys.
2063  */
2064 UV_EXTERN int uv_key_create(uv_key_t* key);
2065 UV_EXTERN void uv_key_delete(uv_key_t* key);
2066 UV_EXTERN void* uv_key_get(uv_key_t* key);
2067 UV_EXTERN void uv_key_set(uv_key_t* key, void* value);
2068
2069 UV_EXTERN int uv_thread_create(uv_thread_t *tid,
2070     void (*entry)(void *arg), void *arg);
2071 UV_EXTERN unsigned long uv_thread_self(void);
2072 UV_EXTERN int uv_thread_join(uv_thread_t *tid);
2073
2074 /* The presence of these unions force similar struct layout. */
2075 #define XX(_, name) uv_ ## name ## _t name;
2076 union uv_any_handle {
2077   UV_HANDLE_TYPE_MAP(XX)
2078 };
2079
2080 union uv_any_req {
2081   UV_REQ_TYPE_MAP(XX)
2082 };
2083 #undef XX
2084
2085
2086 struct uv_loop_s {
2087   /* User data - use this for whatever. */
2088   void* data;
2089   /* Loop reference counting */
2090   unsigned int active_handles;
2091   void* handle_queue[2];
2092   void* active_reqs[2];
2093   /* Internal flag to signal loop stop */
2094   unsigned int stop_flag;
2095   UV_LOOP_PRIVATE_FIELDS
2096 };
2097
2098
2099 /* Don't export the private CPP symbols. */
2100 #undef UV_HANDLE_TYPE_PRIVATE
2101 #undef UV_REQ_TYPE_PRIVATE
2102 #undef UV_REQ_PRIVATE_FIELDS
2103 #undef UV_STREAM_PRIVATE_FIELDS
2104 #undef UV_TCP_PRIVATE_FIELDS
2105 #undef UV_PREPARE_PRIVATE_FIELDS
2106 #undef UV_CHECK_PRIVATE_FIELDS
2107 #undef UV_IDLE_PRIVATE_FIELDS
2108 #undef UV_ASYNC_PRIVATE_FIELDS
2109 #undef UV_TIMER_PRIVATE_FIELDS
2110 #undef UV_GETADDRINFO_PRIVATE_FIELDS
2111 #undef UV_FS_REQ_PRIVATE_FIELDS
2112 #undef UV_WORK_PRIVATE_FIELDS
2113 #undef UV_FS_EVENT_PRIVATE_FIELDS
2114 #undef UV_SIGNAL_PRIVATE_FIELDS
2115 #undef UV_LOOP_PRIVATE_FIELDS
2116 #undef UV_LOOP_PRIVATE_PLATFORM_FIELDS
2117
2118 #ifdef __cplusplus
2119 }
2120 #endif
2121 #endif /* UV_H */