1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
22 /* See http://nikhilm.github.com/uvbook/ for an introduction. */
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)
39 /* Building static library. */
40 # define UV_EXTERN /* nothing */
43 # define UV_EXTERN __attribute__((visibility("default")))
45 # define UV_EXTERN /* nothing */
49 #define UV_VERSION_MAJOR 0
50 #define UV_VERSION_MINOR 10
53 #if defined(_MSC_VER) && _MSC_VER < 1600
54 # include "uv-private/stdint-msvc2008.h"
59 #include <sys/types.h> /* size_t */
61 #if defined(__SVR4) && !defined(__unix__)
65 #if defined(__unix__) || defined(__POSIX__) || \
66 defined(__APPLE__) || defined(_AIX)
67 # include "uv-private/uv-unix.h"
69 # include "uv-private/uv-win.h"
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") \
135 #define UV_ERRNO_GEN(val, name, s) UV_##name = val,
137 UV_ERRNO_MAP(UV_ERRNO_GEN)
142 #define UV_HANDLE_TYPE_MAP(XX) \
145 XX(FS_EVENT, fs_event) \
146 XX(FS_POLL, fs_poll) \
149 XX(NAMED_PIPE, pipe) \
151 XX(PREPARE, prepare) \
152 XX(PROCESS, process) \
160 #define UV_REQ_TYPE_MAP(XX) \
162 XX(CONNECT, connect) \
164 XX(SHUTDOWN, shutdown) \
165 XX(UDP_SEND, udp_send) \
168 XX(GETADDRINFO, getaddrinfo) \
171 UV_UNKNOWN_HANDLE = 0,
172 #define XX(uc, lc) UV_##uc,
173 UV_HANDLE_TYPE_MAP(XX)
181 #define XX(uc, lc) UV_##uc,
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;
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;
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;
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.
236 UV_EXTERN unsigned int uv_version(void);
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".
242 UV_EXTERN const char* uv_version_string(void);
246 * This function must be called before any other functions in libuv.
248 * All functions besides uv_run() are non-blocking.
250 * All callbacks in libuv are made asynchronously. That is they are never
251 * made by the function that takes them as a parameter.
253 UV_EXTERN uv_loop_t* uv_loop_new(void);
254 UV_EXTERN void uv_loop_delete(uv_loop_t*);
257 * Returns the default loop.
259 UV_EXTERN uv_loop_t* uv_default_loop(void);
262 * This function runs the event loop. It will act differently depending on the
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
273 UV_EXTERN int uv_run(uv_loop_t*, uv_run_mode mode);
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.
281 UV_EXTERN void uv_stop(uv_loop_t*);
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.
287 UV_EXTERN void uv_ref(uv_handle_t*);
288 UV_EXTERN void uv_unref(uv_handle_t*);
290 UV_EXTERN void uv_update_time(uv_loop_t*);
291 UV_EXTERN uint64_t uv_now(uv_loop_t*);
294 * Get backend file descriptor. Only kqueue, epoll and event ports are
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.
300 * Useful for embedding libuv's event loop in another event loop.
301 * See test/test-embed.c for an example.
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
307 UV_EXTERN int uv_backend_fd(const uv_loop_t*);
310 * Get the poll timeout. The return value is in milliseconds, or -1 for no
313 UV_EXTERN int uv_backend_timeout(const uv_loop_t*);
317 * Should return a buffer that libuv can use to read data into.
319 * `suggested_size` is a hint. Returning a buffer that is smaller is perfectly
320 * okay as long as `buf.len > 0`.
322 typedef uv_buf_t (*uv_alloc_cb)(uv_handle_t* handle, size_t suggested_size);
325 * `nread` is > 0 if there is data available, 0 if libuv is done reading for now
328 * Error details can be obtained by calling uv_last_error(). UV_EOF indicates
329 * that the stream has been closed.
331 * The callee is responsible for closing the stream when an error happens.
332 * Trying to read from the stream again is undefined.
334 * The callee is responsible for freeing the buffer, libuv does not reuse it.
336 typedef void (*uv_read_cb)(uv_stream_t* stream, ssize_t nread, uv_buf_t buf);
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.
343 typedef void (*uv_read2_cb)(uv_pipe_t* pipe, ssize_t nread, uv_buf_t buf,
344 uv_handle_type pending);
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,
365 struct addrinfo* res);
384 uv_timespec_t st_atim;
385 uv_timespec_t st_mtim;
386 uv_timespec_t st_ctim;
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.
396 typedef void (*uv_fs_event_cb)(uv_fs_event_t* handle, const char* filename,
397 int events, int status);
399 typedef void (*uv_fs_poll_cb)(uv_fs_poll_t* handle,
401 const uv_stat_t* prev,
402 const uv_stat_t* curr);
404 typedef void (*uv_signal_cb)(uv_signal_t* handle, int signum);
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
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);
431 #define UV_REQ_FIELDS \
437 ngx_queue_t active_queue; \
438 UV_REQ_PRIVATE_FIELDS \
440 /* Abstract base class of all requests. */
446 /* Platform-specific request types */
451 * uv_shutdown_t is a subclass of uv_req_t
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.
458 UV_EXTERN int uv_shutdown(uv_shutdown_t* req, uv_stream_t* handle,
461 struct uv_shutdown_s {
465 UV_SHUTDOWN_PRIVATE_FIELDS
469 #define UV_HANDLE_FIELDS \
471 uv_close_cb close_cb; \
475 uv_handle_type type; \
477 ngx_queue_t handle_queue; \
478 UV_HANDLE_PRIVATE_FIELDS \
480 /* The abstract base class of all handles. */
486 * Returns size of various handle types, useful for FFI
487 * bindings to allocate correct memory without copying struct
490 UV_EXTERN size_t uv_handle_size(uv_handle_type type);
493 * Returns size of request types, useful for dynamic lookup with FFI
495 UV_EXTERN size_t uv_req_size(uv_req_type type);
498 * Returns 1 if the prepare/check/idle/timer handle has been started, 0
499 * otherwise. For other handle types this always returns 1.
501 UV_EXTERN int uv_is_active(const uv_handle_t* handle);
504 * Walk the list of open handles.
506 UV_EXTERN void uv_walk(uv_loop_t* loop, uv_walk_cb walk_cb, void* arg);
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.
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.
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.
521 UV_EXTERN void uv_close(uv_handle_t* handle, uv_close_cb close_cb);
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.
530 UV_EXTERN uv_buf_t uv_buf_init(char* base, unsigned int len);
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.
537 UV_EXTERN size_t uv_strlcpy(char* dst, const char* src, size_t size);
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`.
545 UV_EXTERN size_t uv_strlcat(char* dst, const char* src, size_t size);
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; \
555 UV_STREAM_PRIVATE_FIELDS
558 * uv_stream_t is a subclass of uv_handle_t
560 * uv_stream is an abstract class.
562 * uv_stream_t is the parent class of uv_tcp_t, uv_pipe_t, uv_tty_t, and
570 UV_EXTERN int uv_listen(uv_stream_t* stream, int backlog, uv_connection_cb cb);
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.
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.
583 UV_EXTERN int uv_accept(uv_stream_t* server, uv_stream_t* client);
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.
595 UV_EXTERN int uv_read_start(uv_stream_t*, uv_alloc_cb alloc_cb,
598 UV_EXTERN int uv_read_stop(uv_stream_t*);
601 * Extended read methods for receiving handles over a pipe. The pipe must be
602 * initialized with ipc == 1.
604 UV_EXTERN int uv_read2_start(uv_stream_t*, uv_alloc_cb alloc_cb,
605 uv_read2_cb read_cb);
609 * Write data to stream. Buffers are written in order. Example:
612 * { .base = "1", .len = 1 },
613 * { .base = "2", .len = 1 }
617 * { .base = "3", .len = 1 },
618 * { .base = "4", .len = 1 }
625 * uv_write(&req1, stream, a, 2);
626 * uv_write(&req2, stream, b, 2);
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);
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
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);
642 /* uv_write_t is a subclass of uv_req_t */
646 uv_stream_t* send_handle;
648 UV_WRITE_PRIVATE_FIELDS
653 * Used to determine whether a stream is readable or writable.
655 UV_EXTERN int uv_is_readable(const uv_stream_t* handle);
656 UV_EXTERN int uv_is_writable(const uv_stream_t* handle);
660 * Used to determine whether a stream is closing or closed.
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.
666 UV_EXTERN int uv_is_closing(const uv_handle_t* handle);
670 * uv_tcp_t is a subclass of uv_stream_t
672 * Represents a TCP stream or TCP server.
677 UV_TCP_PRIVATE_FIELDS
680 UV_EXTERN int uv_tcp_init(uv_loop_t*, uv_tcp_t* handle);
683 * Opens an existing file descriptor or SOCKET as a tcp handle.
685 UV_EXTERN int uv_tcp_open(uv_tcp_t* handle, uv_os_sock_t sock);
687 /* Enable/disable Nagle's algorithm. */
688 UV_EXTERN int uv_tcp_nodelay(uv_tcp_t* handle, int enable);
691 * Enable/disable TCP keep-alive.
693 * `delay` is the initial delay in seconds, ignored when `enable` is zero.
695 UV_EXTERN int uv_tcp_keepalive(uv_tcp_t* handle,
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.
707 UV_EXTERN int uv_tcp_simultaneous_accepts(uv_tcp_t* handle, int enable);
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,
713 UV_EXTERN int uv_tcp_getpeername(uv_tcp_t* handle, struct sockaddr* name,
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.
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);
727 /* uv_connect_t is a subclass of uv_req_t */
728 struct uv_connect_s {
732 UV_CONNECT_PRIVATE_FIELDS
741 /* Disables dual stack mode. Used with uv_udp_bind6(). */
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.
751 * Called after a uv_udp_send() or uv_udp_send6(). status 0 indicates
752 * success otherwise error.
754 typedef void (*uv_udp_send_cb)(uv_udp_send_t* req, int status);
757 * Callback that is invoked when a new UDP datagram is received.
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.
770 typedef void (*uv_udp_recv_cb)(uv_udp_t* handle, ssize_t nread, uv_buf_t buf,
771 struct sockaddr* addr, unsigned flags);
773 /* uv_udp_t is a subclass of uv_handle_t */
776 UV_UDP_PRIVATE_FIELDS
779 /* uv_udp_send_t is a subclass of uv_req_t */
780 struct uv_udp_send_s {
784 UV_UDP_SEND_PRIVATE_FIELDS
788 * Initialize a new UDP handle. The actual socket is created lazily.
789 * Returns 0 on success.
791 UV_EXTERN int uv_udp_init(uv_loop_t*, uv_udp_t* handle);
794 * Opens an existing file descriptor or SOCKET as a udp handle.
796 UV_EXTERN int uv_udp_open(uv_udp_t* handle, uv_os_sock_t sock);
799 * Bind to a IPv4 address and port.
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.
807 * 0 on success, -1 on error.
809 UV_EXTERN int uv_udp_bind(uv_udp_t* handle, struct sockaddr_in addr,
813 * Bind to a IPv6 address and port.
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.
821 * 0 on success, -1 on error.
823 UV_EXTERN int uv_udp_bind6(uv_udp_t* handle, struct sockaddr_in6 addr,
825 UV_EXTERN int uv_udp_getsockname(uv_udp_t* handle, struct sockaddr* name,
829 * Set membership for a multicast address
832 * handle UDP handle. Should have been initialized with
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
839 * 0 on success, -1 on error.
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);
846 * Set IP multicast loop flag. Makes multicast packets loop back to
850 * handle UDP handle. Should have been initialized with
852 * on 1 for on, 0 for off
855 * 0 on success, -1 on error.
857 UV_EXTERN int uv_udp_set_multicast_loop(uv_udp_t* handle, int on);
860 * Set the multicast ttl
863 * handle UDP handle. Should have been initialized with
868 * 0 on success, -1 on error.
870 UV_EXTERN int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl);
873 * Set broadcast on or off
876 * handle UDP handle. Should have been initialized with
878 * on 1 for on, 0 for off
881 * 0 on success, -1 on error.
883 UV_EXTERN int uv_udp_set_broadcast(uv_udp_t* handle, int on);
886 * Set the time to live
889 * handle UDP handle. Should have been initialized with
894 * 0 on success, -1 on error.
896 UV_EXTERN int uv_udp_set_ttl(uv_udp_t* handle, int ttl);
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.
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.
912 * 0 on success, -1 on error.
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);
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.
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.
931 * 0 on success, -1 on error.
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);
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.
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.
948 * 0 on success, -1 on error.
950 UV_EXTERN int uv_udp_recv_start(uv_udp_t* handle, uv_alloc_cb alloc_cb,
951 uv_udp_recv_cb recv_cb);
954 * Stop listening for incoming datagrams.
957 * handle UDP handle. Should have been initialized with `uv_udp_init`.
960 * 0 on success, -1 on error.
962 UV_EXTERN int uv_udp_recv_stop(uv_udp_t* handle);
966 * uv_tty_t is a subclass of uv_stream_t
968 * Representing a stream for the console.
973 UV_TTY_PRIVATE_FIELDS
977 * Initialize a new TTY stream with the given file descriptor. Usually the
978 * file descriptor will be
982 * The last argument, readable, specifies if you plan on calling
983 * uv_read_start with this stream. stdin is readable, stdout is not.
985 * TTY streams which are not readable have blocking writes.
987 UV_EXTERN int uv_tty_init(uv_loop_t*, uv_tty_t*, uv_file fd, int readable);
990 * Set mode. 0 for normal, 1 for raw.
992 UV_EXTERN int uv_tty_set_mode(uv_tty_t*, int mode);
995 * To be called when the program exits. Resets TTY settings to default
996 * values for the next process to take over.
998 UV_EXTERN void uv_tty_reset_mode(void);
1001 * Gets the current Window size. On success zero is returned.
1003 UV_EXTERN int uv_tty_get_winsize(uv_tty_t*, int* width, int* height);
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.
1011 UV_EXTERN uv_handle_type uv_guess_handle(uv_file file);
1014 * uv_pipe_t is a subclass of uv_stream_t
1016 * Representing a pipe stream or pipe server. On Windows this is a Named
1017 * Pipe. On Unix this is a UNIX domain socket.
1022 int ipc; /* non-zero if this pipe is used for passing handles */
1023 UV_PIPE_PRIVATE_FIELDS
1027 * Initialize a pipe. The last argument is a boolean to indicate if
1028 * this pipe will be used for handle passing between processes.
1030 UV_EXTERN int uv_pipe_init(uv_loop_t*, uv_pipe_t* handle, int ipc);
1033 * Opens an existing file descriptor or HANDLE as a pipe.
1035 UV_EXTERN int uv_pipe_open(uv_pipe_t*, uv_file file);
1037 UV_EXTERN int uv_pipe_bind(uv_pipe_t* handle, const char* name);
1039 UV_EXTERN void uv_pipe_connect(uv_connect_t* req, uv_pipe_t* handle,
1040 const char* name, uv_connect_cb cb);
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.
1047 UV_EXTERN void uv_pipe_pending_instances(uv_pipe_t* handle, int count);
1051 * uv_poll_t is a subclass of uv_handle_t.
1053 * The uv_poll watcher is used to watch file descriptors for readability and
1054 * writability, similar to the purpose of poll(2).
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.
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
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.
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().
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.
1082 UV_POLL_PRIVATE_FIELDS
1085 enum uv_poll_event {
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);
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);
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
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.
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.
1112 UV_EXTERN int uv_poll_start(uv_poll_t* handle, int events, uv_poll_cb cb);
1114 /* Stops polling the file descriptor. */
1115 UV_EXTERN int uv_poll_stop(uv_poll_t* handle);
1119 * uv_prepare_t is a subclass of uv_handle_t.
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.
1124 struct uv_prepare_s {
1126 UV_PREPARE_PRIVATE_FIELDS
1129 UV_EXTERN int uv_prepare_init(uv_loop_t*, uv_prepare_t* prepare);
1131 UV_EXTERN int uv_prepare_start(uv_prepare_t* prepare, uv_prepare_cb cb);
1133 UV_EXTERN int uv_prepare_stop(uv_prepare_t* prepare);
1137 * uv_check_t is a subclass of uv_handle_t.
1139 * Every active check handle gets its callback called exactly once per loop
1140 * iteration, just after the system returns from blocking.
1144 UV_CHECK_PRIVATE_FIELDS
1147 UV_EXTERN int uv_check_init(uv_loop_t*, uv_check_t* check);
1149 UV_EXTERN int uv_check_start(uv_check_t* check, uv_check_cb cb);
1151 UV_EXTERN int uv_check_stop(uv_check_t* check);
1155 * uv_idle_t is a subclass of uv_handle_t.
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
1164 UV_IDLE_PRIVATE_FIELDS
1167 UV_EXTERN int uv_idle_init(uv_loop_t*, uv_idle_t* idle);
1169 UV_EXTERN int uv_idle_start(uv_idle_t* idle, uv_idle_cb cb);
1171 UV_EXTERN int uv_idle_stop(uv_idle_t* idle);
1175 * uv_async_t is a subclass of uv_handle_t.
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.
1185 UV_ASYNC_PRIVATE_FIELDS
1188 UV_EXTERN int uv_async_init(uv_loop_t*, uv_async_t* async,
1189 uv_async_cb async_cb);
1192 * This can be called from other threads to wake up a libuv thread.
1194 * libuv is single threaded at the moment.
1196 UV_EXTERN int uv_async_send(uv_async_t* async);
1200 * uv_timer_t is a subclass of uv_handle_t.
1202 * Used to get woken up at a specified time in the future.
1206 UV_TIMER_PRIVATE_FIELDS
1209 UV_EXTERN int uv_timer_init(uv_loop_t*, uv_timer_t* handle);
1212 * Start the timer. `timeout` and `repeat` are in milliseconds.
1214 * If timeout is zero, the callback fires on the next tick of the event loop.
1216 * If repeat is non-zero, the callback fires first after timeout milliseconds
1217 * and then repeatedly after repeat milliseconds.
1219 UV_EXTERN int uv_timer_start(uv_timer_t* handle,
1224 UV_EXTERN int uv_timer_stop(uv_timer_t* handle);
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.
1231 UV_EXTERN int uv_timer_again(uv_timer_t* handle);
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.
1239 UV_EXTERN void uv_timer_set_repeat(uv_timer_t* handle, uint64_t repeat);
1241 UV_EXTERN uint64_t uv_timer_get_repeat(const uv_timer_t* handle);
1245 * uv_getaddrinfo_t is a subclass of uv_req_t
1247 * Request object for uv_getaddrinfo.
1249 struct uv_getaddrinfo_s {
1253 UV_GETADDRINFO_PRIVATE_FIELDS
1258 * Asynchronous getaddrinfo(3).
1260 * Either node or service may be NULL but not both.
1262 * hints is a pointer to a struct addrinfo with additional address type
1263 * constraints, or NULL. Consult `man -s 3 getaddrinfo` for details.
1265 * Returns 0 on success, -1 on error. Call uv_last_error() to get the error.
1267 * If successful, your callback gets called sometime in the future with the
1268 * lookup result, which is either:
1270 * a) status == 0, the res argument points to a valid struct addrinfo, or
1271 * b) status == -1, the res argument is NULL.
1273 * On NXDOMAIN, the status code is -1 and uv_last_error() returns UV_ENOENT.
1275 * Call uv_freeaddrinfo() to free the addrinfo structure.
1277 UV_EXTERN int uv_getaddrinfo(uv_loop_t* loop,
1278 uv_getaddrinfo_t* req,
1279 uv_getaddrinfo_cb getaddrinfo_cb,
1281 const char* service,
1282 const struct addrinfo* hints);
1285 * Free the struct addrinfo. Passing NULL is allowed and is a no-op.
1287 UV_EXTERN void uv_freeaddrinfo(struct addrinfo* ai);
1289 /* uv_spawn() options */
1292 UV_CREATE_PIPE = 0x01,
1293 UV_INHERIT_FD = 0x02,
1294 UV_INHERIT_STREAM = 0x04,
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.
1300 UV_READABLE_PIPE = 0x10,
1301 UV_WRITABLE_PIPE = 0x20
1304 typedef struct uv_stdio_container_s {
1305 uv_stdio_flags flags;
1308 uv_stream_t* stream;
1311 } uv_stdio_container_t;
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. */
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.
1324 * This will be set as the environ variable in the subprocess. If this is
1325 * NULL then the parents environ will be used.
1329 * If non-null this represents a directory the subprocess should execute
1330 * in. Stands for current working directory.
1334 * Various flags that control how uv_spawn() behaves. See the definition of
1335 * `enum uv_process_flags` below.
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.
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.
1348 uv_stdio_container_t* stdio;
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.
1356 } uv_process_options_t;
1359 * These are the flags that can be used for the uv_process_options.flags field.
1361 enum uv_process_flags {
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.
1367 UV_PROCESS_SETUID = (1 << 0),
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.
1373 UV_PROCESS_SETGID = (1 << 1),
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.
1379 UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS = (1 << 2),
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.
1387 UV_PROCESS_DETACHED = (1 << 3),
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
1393 UV_PROCESS_WINDOWS_HIDE = (1 << 4)
1397 * uv_process_t is a subclass of uv_handle_t
1399 struct uv_process_s {
1403 UV_PROCESS_PRIVATE_FIELDS
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);
1412 * Kills the process with the specified signal. The user must still
1413 * call uv_close on the process.
1415 UV_EXTERN int uv_process_kill(uv_process_t*, int signum);
1418 /* Kills the process with the specified signal. */
1419 UV_EXTERN uv_err_t uv_kill(int pid, int signum);
1423 * uv_work_t is a subclass of uv_req_t
1429 uv_after_work_cb after_work_cb;
1430 UV_WORK_PRIVATE_FIELDS
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);
1437 /* Cancel a pending request. Fails if the request is executing or has finished
1440 * Returns 0 on success, -1 on error. The loop error code is not touched.
1442 * Only cancellation of uv_fs_t, uv_getaddrinfo_t and uv_work_t requests is
1443 * currently supported.
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.
1449 * Here is how cancellation is reported to your callback:
1451 * - A uv_fs_t request has its req->errorno field set to UV_ECANCELED.
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.
1456 * This function is currently only implemented on UNIX platforms. On Windows,
1457 * it always returns -1.
1459 UV_EXTERN int uv_cancel(uv_req_t* req);
1462 struct uv_cpu_info_s {
1465 struct uv_cpu_times_s {
1474 struct uv_interface_address_s {
1478 struct sockaddr_in address4;
1479 struct sockaddr_in6 address6;
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);
1490 * This allocates cpu_infos array, and sets count. The array
1491 * is freed using uv_free_cpu_info().
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);
1497 * This allocates addresses array, and sets count. The array
1498 * is freed using uv_free_interface_addresses().
1500 UV_EXTERN uv_err_t uv_interface_addresses(uv_interface_address_t** addresses,
1502 UV_EXTERN void uv_free_interface_addresses(uv_interface_address_t* addresses,
1506 * File System Methods.
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.
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
1549 /* uv_fs_t is a subclass of uv_req_t */
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
1563 UV_EXTERN void uv_fs_req_cleanup(uv_fs_t* req);
1565 UV_EXTERN int uv_fs_close(uv_loop_t* loop, uv_fs_t* req, uv_file file,
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);
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);
1574 UV_EXTERN int uv_fs_unlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
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);
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);
1583 UV_EXTERN int uv_fs_rmdir(uv_loop_t* loop, uv_fs_t* req, const char* path,
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);
1589 UV_EXTERN int uv_fs_stat(uv_loop_t* loop, uv_fs_t* req, const char* path,
1592 UV_EXTERN int uv_fs_fstat(uv_loop_t* loop, uv_fs_t* req, uv_file file,
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);
1598 UV_EXTERN int uv_fs_fsync(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1601 UV_EXTERN int uv_fs_fdatasync(uv_loop_t* loop, uv_fs_t* req, uv_file file,
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);
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);
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);
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);
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);
1619 UV_EXTERN int uv_fs_lstat(uv_loop_t* loop, uv_fs_t* req, const char* path,
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);
1626 * This flag can be used with uv_fs_symlink on Windows
1627 * to specify whether path argument points to a directory.
1629 #define UV_FS_SYMLINK_DIR 0x0001
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.
1635 #define UV_FS_SYMLINK_JUNCTION 0x0002
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);
1640 UV_EXTERN int uv_fs_readlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
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);
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);
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);
1659 struct uv_fs_event_s {
1662 UV_FS_EVENT_PRIVATE_FIELDS
1667 * uv_fs_stat() based polling file watcher.
1669 struct uv_fs_poll_s {
1671 /* Private, don't touch. */
1675 UV_EXTERN int uv_fs_poll_init(uv_loop_t* loop, uv_fs_poll_t* handle);
1678 * Check the file at `path` for changes every `interval` milliseconds.
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).
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
1689 * For maximum portability, use multi-second intervals. Sub-second intervals
1690 * will not detect all changes on many file systems.
1692 UV_EXTERN int uv_fs_poll_start(uv_fs_poll_t* handle,
1693 uv_fs_poll_cb poll_cb,
1695 unsigned int interval);
1697 UV_EXTERN int uv_fs_poll_stop(uv_fs_poll_t* handle);
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
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.
1710 * Some signal support is available on Windows:
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.
1715 * SIGBREAK is delivered when the user pressed CTRL+BREAK.
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.
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.
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.
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.
1735 struct uv_signal_s {
1737 uv_signal_cb signal_cb;
1739 UV_SIGNAL_PRIVATE_FIELDS
1742 UV_EXTERN int uv_signal_init(uv_loop_t* loop, uv_signal_t* handle);
1744 UV_EXTERN int uv_signal_start(uv_signal_t* handle,
1745 uv_signal_cb signal_cb,
1748 UV_EXTERN int uv_signal_stop(uv_signal_t* handle);
1753 * See: http://en.wikipedia.org/wiki/Load_(computing)
1754 * (Returns [0,0,0] for windows and cygwin)
1756 UV_EXTERN void uv_loadavg(double avg[3]);
1760 * Flags to be passed to uv_fs_event_init.
1762 enum uv_fs_event_flags {
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.
1770 UV_FS_EVENT_WATCH_ENTRY = 1,
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
1777 * This flag is currently not implemented yet on any backend.
1779 UV_FS_EVENT_STAT = 2,
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.
1786 UV_FS_EVENT_RECURSIVE = 3
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);
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);
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);
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);
1809 /* Gets the executable path */
1810 UV_EXTERN int uv_exepath(char* buffer, size_t* size);
1812 /* Gets the current working directory */
1813 UV_EXTERN uv_err_t uv_cwd(char* buffer, size_t size);
1815 /* Changes the current working directory */
1816 UV_EXTERN uv_err_t uv_chdir(const char* dir);
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);
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.
1828 * Note not every platform can support nanosecond resolution; however, this
1829 * value will always be in nanoseconds.
1831 UV_EXTERN extern uint64_t uv_hrtime(void);
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.
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.
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.
1846 * TODO(bb): insert snarky remark to annoy bnoordhuis and the folks at joyent.
1848 UV_EXTERN void uv_disable_stdio_inheritance(void);
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.
1854 UV_EXTERN int uv_dlopen(const char* filename, uv_lib_t* lib);
1857 * Close the shared library.
1859 UV_EXTERN void uv_dlclose(uv_lib_t* lib);
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.
1865 UV_EXTERN int uv_dlsym(uv_lib_t* lib, const char* name, void** ptr);
1868 * Returns the last uv_dlopen() or uv_dlsym() error message.
1870 UV_EXTERN const char* uv_dlerror(uv_lib_t* lib);
1873 * The mutex functions return 0 on success, -1 on error
1874 * (unless the return type is void, of course).
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);
1883 * Same goes for the read/write lock functions.
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);
1895 * Same goes for the semaphore functions.
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);
1904 * Same goes for the condition variable functions.
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.
1913 * 1. callers should be prepared to deal with spurious wakeups.
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.
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.
1924 UV_EXTERN int uv_cond_timedwait(uv_cond_t* cond, uv_mutex_t* mutex,
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);
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.
1935 UV_EXTERN void uv_once(uv_once_t* guard, void (*callback)(void));
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);
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)
1955 /* User data - use this for whatever. */
1957 /* The last error */
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
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