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