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 */
51 #if defined(_MSC_VER) && _MSC_VER < 1600
52 # include "stdint-msvc2008.h"
63 /* Expand this list if necessary. */
64 #define UV_ERRNO_MAP(XX) \
65 XX(E2BIG, "argument list too long") \
66 XX(EACCES, "permission denied") \
67 XX(EADDRINUSE, "address already in use") \
68 XX(EADDRNOTAVAIL, "address not available") \
69 XX(EAFNOSUPPORT, "address family not supported") \
70 XX(EAGAIN, "resource temporarily unavailable") \
71 XX(EAI_ADDRFAMILY, "address family not supported") \
72 XX(EAI_AGAIN, "temporary failure") \
73 XX(EAI_BADFLAGS, "bad ai_flags value") \
74 XX(EAI_BADHINTS, "invalid value for hints") \
75 XX(EAI_CANCELED, "request canceled") \
76 XX(EAI_FAIL, "permanent failure") \
77 XX(EAI_FAMILY, "ai_family not supported") \
78 XX(EAI_MEMORY, "out of memory") \
79 XX(EAI_NODATA, "no address") \
80 XX(EAI_NONAME, "unknown node or service") \
81 XX(EAI_OVERFLOW, "argument buffer overflow") \
82 XX(EAI_PROTOCOL, "resolved protocol is unknown") \
83 XX(EAI_SERVICE, "service not available for socket type") \
84 XX(EAI_SOCKTYPE, "socket type not supported") \
85 XX(EAI_SYSTEM, "system error") \
86 XX(EALREADY, "connection already in progress") \
87 XX(EBADF, "bad file descriptor") \
88 XX(EBUSY, "resource busy or locked") \
89 XX(ECANCELED, "operation canceled") \
90 XX(ECHARSET, "invalid Unicode character") \
91 XX(ECONNABORTED, "software caused connection abort") \
92 XX(ECONNREFUSED, "connection refused") \
93 XX(ECONNRESET, "connection reset by peer") \
94 XX(EDESTADDRREQ, "destination address required") \
95 XX(EEXIST, "file already exists") \
96 XX(EFAULT, "bad address in system call argument") \
97 XX(EHOSTUNREACH, "host is unreachable") \
98 XX(EINTR, "interrupted system call") \
99 XX(EINVAL, "invalid argument") \
100 XX(EIO, "i/o error") \
101 XX(EISCONN, "socket is already connected") \
102 XX(EISDIR, "illegal operation on a directory") \
103 XX(ELOOP, "too many symbolic links encountered") \
104 XX(EMFILE, "too many open files") \
105 XX(EMSGSIZE, "message too long") \
106 XX(ENAMETOOLONG, "name too long") \
107 XX(ENETDOWN, "network is down") \
108 XX(ENETUNREACH, "network is unreachable") \
109 XX(ENFILE, "file table overflow") \
110 XX(ENOBUFS, "no buffer space available") \
111 XX(ENODEV, "no such device") \
112 XX(ENOENT, "no such file or directory") \
113 XX(ENOMEM, "not enough memory") \
114 XX(ENONET, "machine is not on the network") \
115 XX(ENOSPC, "no space left on device") \
116 XX(ENOSYS, "function not implemented") \
117 XX(ENOTCONN, "socket is not connected") \
118 XX(ENOTDIR, "not a directory") \
119 XX(ENOTEMPTY, "directory not empty") \
120 XX(ENOTSOCK, "socket operation on non-socket") \
121 XX(ENOTSUP, "operation not supported on socket") \
122 XX(EPERM, "operation not permitted") \
123 XX(EPIPE, "broken pipe") \
124 XX(EPROTO, "protocol error") \
125 XX(EPROTONOSUPPORT, "protocol not supported") \
126 XX(EPROTOTYPE, "protocol wrong type for socket") \
127 XX(EROFS, "read-only file system") \
128 XX(ESHUTDOWN, "cannot send after transport endpoint shutdown") \
129 XX(ESPIPE, "invalid seek") \
130 XX(ESRCH, "no such process") \
131 XX(ETIMEDOUT, "connection timed out") \
132 XX(EXDEV, "cross-device link not permitted") \
133 XX(UNKNOWN, "unknown error") \
134 XX(EOF, "end of file") \
136 #define UV_HANDLE_TYPE_MAP(XX) \
139 XX(FS_EVENT, fs_event) \
140 XX(FS_POLL, fs_poll) \
143 XX(NAMED_PIPE, pipe) \
145 XX(PREPARE, prepare) \
146 XX(PROCESS, process) \
154 #define UV_REQ_TYPE_MAP(XX) \
156 XX(CONNECT, connect) \
158 XX(SHUTDOWN, shutdown) \
159 XX(UDP_SEND, udp_send) \
162 XX(GETADDRINFO, getaddrinfo) \
165 #define XX(code, _) UV_ ## code = UV__ ## code,
168 UV_ERRNO_MAX = UV__EOF - 1
172 UV_UNKNOWN_HANDLE = 0,
173 #define XX(uc, lc) UV_##uc,
174 UV_HANDLE_TYPE_MAP(XX)
182 #define XX(uc, lc) UV_##uc,
191 typedef struct uv_loop_s uv_loop_t;
192 typedef struct uv_handle_s uv_handle_t;
193 typedef struct uv_stream_s uv_stream_t;
194 typedef struct uv_tcp_s uv_tcp_t;
195 typedef struct uv_udp_s uv_udp_t;
196 typedef struct uv_pipe_s uv_pipe_t;
197 typedef struct uv_tty_s uv_tty_t;
198 typedef struct uv_poll_s uv_poll_t;
199 typedef struct uv_timer_s uv_timer_t;
200 typedef struct uv_prepare_s uv_prepare_t;
201 typedef struct uv_check_s uv_check_t;
202 typedef struct uv_idle_s uv_idle_t;
203 typedef struct uv_async_s uv_async_t;
204 typedef struct uv_process_s uv_process_t;
205 typedef struct uv_fs_event_s uv_fs_event_t;
206 typedef struct uv_fs_poll_s uv_fs_poll_t;
207 typedef struct uv_signal_s uv_signal_t;
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 checks whether the reference count, the number of active
277 * handles or requests left in the event loop, is non-zero.
279 UV_EXTERN int uv_loop_alive(const uv_loop_t* loop);
282 * This function will stop the event loop by forcing uv_run to end
283 * as soon as possible, but not sooner than the next loop iteration.
284 * If this function was called before blocking for i/o, the loop won't
285 * block for i/o on this iteration.
287 UV_EXTERN void uv_stop(uv_loop_t*);
290 * Manually modify the event loop's reference count. Useful if the user wants
291 * to have a handle or timeout that doesn't keep the loop alive.
293 UV_EXTERN void uv_ref(uv_handle_t*);
294 UV_EXTERN void uv_unref(uv_handle_t*);
295 UV_EXTERN int uv_has_ref(const uv_handle_t*);
298 * Update the event loop's concept of "now". Libuv caches the current time
299 * at the start of the event loop tick in order to reduce the number of
300 * time-related system calls.
302 * You won't normally need to call this function unless you have callbacks
303 * that block the event loop for longer periods of time, where "longer" is
304 * somewhat subjective but probably on the order of a millisecond or more.
306 UV_EXTERN void uv_update_time(uv_loop_t*);
309 * Return the current timestamp in milliseconds. The timestamp is cached at
310 * the start of the event loop tick, see |uv_update_time()| for details and
313 * The timestamp increases monotonically from some arbitrary point in time.
314 * Don't make assumptions about the starting point, you will only get
317 * Use uv_hrtime() if you need sub-millisecond granularity.
319 UV_EXTERN uint64_t uv_now(uv_loop_t*);
322 * Get backend file descriptor. Only kqueue, epoll and event ports are
325 * This can be used in conjunction with `uv_run(loop, UV_RUN_NOWAIT)` to
326 * poll in one thread and run the event loop's event callbacks in another.
328 * Useful for embedding libuv's event loop in another event loop.
329 * See test/test-embed.c for an example.
331 * Note that embedding a kqueue fd in another kqueue pollset doesn't work on
332 * all platforms. It's not an error to add the fd but it never generates
335 UV_EXTERN int uv_backend_fd(const uv_loop_t*);
338 * Get the poll timeout. The return value is in milliseconds, or -1 for no
341 UV_EXTERN int uv_backend_timeout(const uv_loop_t*);
345 * Should prepare a buffer that libuv can use to read data into.
347 * `suggested_size` is a hint. Returning a buffer that is smaller is perfectly
348 * okay as long as `buf.len > 0`.
350 * If you return a buffer with `buf.len == 0`, libuv skips the read and calls
351 * your read or recv callback with nread=UV_ENOBUFS.
353 * Note that returning a zero-length buffer does not stop the handle, call
354 * uv_read_stop() or uv_udp_recv_stop() for that.
356 typedef void (*uv_alloc_cb)(uv_handle_t* handle,
357 size_t suggested_size,
361 * `nread` is > 0 if there is data available, 0 if libuv is done reading for
362 * now, or < 0 on error.
364 * The callee is responsible for closing the stream when an error happens.
365 * Trying to read from the stream again is undefined.
367 * The callee is responsible for freeing the buffer, libuv does not reuse it.
368 * The buffer may be a null buffer (where buf->base=NULL and buf->len=0) on
371 typedef void (*uv_read_cb)(uv_stream_t* stream,
373 const uv_buf_t* buf);
376 * Just like the uv_read_cb except that if the pending parameter is true
377 * then you can use uv_accept() to pull the new handle into the process.
378 * If no handle is pending then pending will be UV_UNKNOWN_HANDLE.
380 typedef void (*uv_read2_cb)(uv_pipe_t* pipe,
383 uv_handle_type pending);
385 typedef void (*uv_write_cb)(uv_write_t* req, int status);
386 typedef void (*uv_connect_cb)(uv_connect_t* req, int status);
387 typedef void (*uv_shutdown_cb)(uv_shutdown_t* req, int status);
388 typedef void (*uv_connection_cb)(uv_stream_t* server, int status);
389 typedef void (*uv_close_cb)(uv_handle_t* handle);
390 typedef void (*uv_poll_cb)(uv_poll_t* handle, int status, int events);
391 typedef void (*uv_timer_cb)(uv_timer_t* handle, int status);
392 /* TODO: do these really need a status argument? */
393 typedef void (*uv_async_cb)(uv_async_t* handle, int status);
394 typedef void (*uv_prepare_cb)(uv_prepare_t* handle, int status);
395 typedef void (*uv_check_cb)(uv_check_t* handle, int status);
396 typedef void (*uv_idle_cb)(uv_idle_t* handle, int status);
397 typedef void (*uv_exit_cb)(uv_process_t*, int64_t exit_status, int term_signal);
398 typedef void (*uv_walk_cb)(uv_handle_t* handle, void* arg);
399 typedef void (*uv_fs_cb)(uv_fs_t* req);
400 typedef void (*uv_work_cb)(uv_work_t* req);
401 typedef void (*uv_after_work_cb)(uv_work_t* req, int status);
402 typedef void (*uv_getaddrinfo_cb)(uv_getaddrinfo_t* req,
404 struct addrinfo* res);
425 uv_timespec_t st_atim;
426 uv_timespec_t st_mtim;
427 uv_timespec_t st_ctim;
428 uv_timespec_t st_birthtim;
433 * This will be called repeatedly after the uv_fs_event_t is initialized.
434 * If uv_fs_event_t was initialized with a directory the filename parameter
435 * will be a relative path to a file contained in the directory.
436 * The events parameter is an ORed mask of enum uv_fs_event elements.
438 typedef void (*uv_fs_event_cb)(uv_fs_event_t* handle, const char* filename,
439 int events, int status);
441 typedef void (*uv_fs_poll_cb)(uv_fs_poll_t* handle,
443 const uv_stat_t* prev,
444 const uv_stat_t* curr);
446 typedef void (*uv_signal_cb)(uv_signal_t* handle, int signum);
456 * Most functions return 0 on success or an error code < 0 on failure.
458 UV_EXTERN const char* uv_strerror(int err);
459 UV_EXTERN const char* uv_err_name(int err);
462 #define UV_REQ_FIELDS \
468 void* active_queue[2]; \
469 UV_REQ_PRIVATE_FIELDS \
471 /* Abstract base class of all requests. */
477 /* Platform-specific request types */
482 * uv_shutdown_t is a subclass of uv_req_t
484 * Shutdown the outgoing (write) side of a duplex stream. It waits for
485 * pending write requests to complete. The handle should refer to a
486 * initialized stream. req should be an uninitialized shutdown request
487 * struct. The cb is called after shutdown is complete.
489 UV_EXTERN int uv_shutdown(uv_shutdown_t* req, uv_stream_t* handle,
492 struct uv_shutdown_s {
496 UV_SHUTDOWN_PRIVATE_FIELDS
500 #define UV_HANDLE_FIELDS \
502 uv_close_cb close_cb; \
506 uv_handle_type type; \
508 void* handle_queue[2]; \
509 UV_HANDLE_PRIVATE_FIELDS \
511 /* The abstract base class of all handles. */
517 * Returns size of various handle types, useful for FFI
518 * bindings to allocate correct memory without copying struct
521 UV_EXTERN size_t uv_handle_size(uv_handle_type type);
524 * Returns size of request types, useful for dynamic lookup with FFI
526 UV_EXTERN size_t uv_req_size(uv_req_type type);
529 * Returns non-zero if the handle is active, zero if it's inactive.
531 * What "active" means depends on the type of handle:
533 * - A uv_async_t handle is always active and cannot be deactivated, except
534 * by closing it with uv_close().
536 * - A uv_pipe_t, uv_tcp_t, uv_udp_t, etc. handle - basically any handle that
537 * deals with I/O - is active when it is doing something that involves I/O,
538 * like reading, writing, connecting, accepting new connections, etc.
540 * - A uv_check_t, uv_idle_t, uv_timer_t, etc. handle is active when it has
541 * been started with a call to uv_check_start(), uv_idle_start(), etc.
543 * Rule of thumb: if a handle of type uv_foo_t has a uv_foo_start()
544 * function, then it's active from the moment that function is called.
545 * Likewise, uv_foo_stop() deactivates the handle again.
548 UV_EXTERN int uv_is_active(const uv_handle_t* handle);
551 * Walk the list of open handles.
553 UV_EXTERN void uv_walk(uv_loop_t* loop, uv_walk_cb walk_cb, void* arg);
557 * Request handle to be closed. close_cb will be called asynchronously after
558 * this call. This MUST be called on each handle before memory is released.
560 * Note that handles that wrap file descriptors are closed immediately but
561 * close_cb will still be deferred to the next iteration of the event loop.
562 * It gives you a chance to free up any resources associated with the handle.
564 * In-progress requests, like uv_connect_t or uv_write_t, are cancelled and
565 * have their callbacks called asynchronously with status=UV_ECANCELED.
567 UV_EXTERN void uv_close(uv_handle_t* handle, uv_close_cb close_cb);
571 * Constructor for uv_buf_t.
572 * Due to platform differences the user cannot rely on the ordering of the
573 * base and len members of the uv_buf_t struct. The user is responsible for
574 * freeing base after the uv_buf_t is done. Return struct passed by value.
576 UV_EXTERN uv_buf_t uv_buf_init(char* base, unsigned int len);
579 #define UV_STREAM_FIELDS \
580 /* number of bytes queued for writing */ \
581 size_t write_queue_size; \
582 uv_alloc_cb alloc_cb; \
583 uv_read_cb read_cb; \
584 uv_read2_cb read2_cb; \
586 UV_STREAM_PRIVATE_FIELDS
589 * uv_stream_t is a subclass of uv_handle_t
591 * uv_stream is an abstract class.
593 * uv_stream_t is the parent class of uv_tcp_t, uv_pipe_t, uv_tty_t, and
601 UV_EXTERN int uv_listen(uv_stream_t* stream, int backlog, uv_connection_cb cb);
604 * This call is used in conjunction with uv_listen() to accept incoming
605 * connections. Call uv_accept after receiving a uv_connection_cb to accept
606 * the connection. Before calling uv_accept use uv_*_init() must be
607 * called on the client. Non-zero return value indicates an error.
609 * When the uv_connection_cb is called it is guaranteed that uv_accept will
610 * complete successfully the first time. If you attempt to use it more than
611 * once, it may fail. It is suggested to only call uv_accept once per
612 * uv_connection_cb call.
614 UV_EXTERN int uv_accept(uv_stream_t* server, uv_stream_t* client);
617 * Read data from an incoming stream. The callback will be made several
618 * times until there is no more data to read or uv_read_stop is called.
619 * When we've reached EOF nread will be set to UV_EOF.
621 * When nread < 0, the buf parameter might not point to a valid buffer;
622 * in that case buf.len and buf.base are both set to 0.
624 * Note that nread might also be 0, which does *not* indicate an error or
625 * eof; it happens when libuv requested a buffer through the alloc callback
626 * but then decided that it didn't need that buffer.
628 UV_EXTERN int uv_read_start(uv_stream_t*, uv_alloc_cb alloc_cb,
631 UV_EXTERN int uv_read_stop(uv_stream_t*);
634 * Extended read methods for receiving handles over a pipe. The pipe must be
635 * initialized with ipc == 1.
637 UV_EXTERN int uv_read2_start(uv_stream_t*, uv_alloc_cb alloc_cb,
638 uv_read2_cb read_cb);
642 * Write data to stream. Buffers are written in order. Example:
645 * { .base = "1", .len = 1 },
646 * { .base = "2", .len = 1 }
650 * { .base = "3", .len = 1 },
651 * { .base = "4", .len = 1 }
658 * uv_write(&req1, stream, a, 2);
659 * uv_write(&req2, stream, b, 2);
662 UV_EXTERN int uv_write(uv_write_t* req,
664 const uv_buf_t bufs[],
669 * Extended write function for sending handles over a pipe. The pipe must be
670 * initialized with ipc == 1.
671 * send_handle must be a TCP socket or pipe, which is a server or a connection
672 * (listening or connected state). Bound sockets or pipes will be assumed to
675 UV_EXTERN int uv_write2(uv_write_t* req,
677 const uv_buf_t bufs[],
679 uv_stream_t* send_handle,
683 * Same as `uv_write()`, but won't queue write request if it can't be completed
685 * Will return either:
686 * - positive number of bytes written
687 * - zero - if queued write is needed
688 * - negative error code
690 UV_EXTERN int uv_try_write(uv_stream_t* handle,
691 const uv_buf_t bufs[],
694 /* uv_write_t is a subclass of uv_req_t */
698 uv_stream_t* send_handle;
700 UV_WRITE_PRIVATE_FIELDS
705 * Used to determine whether a stream is readable or writable.
707 UV_EXTERN int uv_is_readable(const uv_stream_t* handle);
708 UV_EXTERN int uv_is_writable(const uv_stream_t* handle);
712 * Enable or disable blocking mode for a stream.
714 * When blocking mode is enabled all writes complete synchronously. The
715 * interface remains unchanged otherwise, e.g. completion or failure of the
716 * operation will still be reported through a callback which is made
719 * Relying too much on this API is not recommended. It is likely to change
720 * significantly in the future.
722 * On windows this currently works only for uv_pipe_t instances. On unix it
723 * works for tcp, pipe and tty instances. Be aware that changing the blocking
724 * mode on unix sets or clears the O_NONBLOCK bit. If you are sharing a handle
725 * with another process, the other process is affected by the change too,
726 * which can lead to unexpected results.
728 * Also libuv currently makes no ordering guarantee when the blocking mode
729 * is changed after write requests have already been submitted. Therefore it is
730 * recommended to set the blocking mode immediately after opening or creating
733 UV_EXTERN int uv_stream_set_blocking(uv_stream_t* handle, int blocking);
737 * Used to determine whether a stream is closing or closed.
739 * N.B. is only valid between the initialization of the handle
740 * and the arrival of the close callback, and cannot be used
741 * to validate the handle.
743 UV_EXTERN int uv_is_closing(const uv_handle_t* handle);
747 * uv_tcp_t is a subclass of uv_stream_t
749 * Represents a TCP stream or TCP server.
754 UV_TCP_PRIVATE_FIELDS
757 UV_EXTERN int uv_tcp_init(uv_loop_t*, uv_tcp_t* handle);
760 * Opens an existing file descriptor or SOCKET as a tcp handle.
762 UV_EXTERN int uv_tcp_open(uv_tcp_t* handle, uv_os_sock_t sock);
764 /* Enable/disable Nagle's algorithm. */
765 UV_EXTERN int uv_tcp_nodelay(uv_tcp_t* handle, int enable);
768 * Enable/disable TCP keep-alive.
770 * `delay` is the initial delay in seconds, ignored when `enable` is zero.
772 UV_EXTERN int uv_tcp_keepalive(uv_tcp_t* handle,
777 * Enable/disable simultaneous asynchronous accept requests that are
778 * queued by the operating system when listening for new tcp connections.
779 * This setting is used to tune a tcp server for the desired performance.
780 * Having simultaneous accepts can significantly improve the rate of
781 * accepting connections (which is why it is enabled by default) but
782 * may lead to uneven load distribution in multi-process setups.
784 UV_EXTERN int uv_tcp_simultaneous_accepts(uv_tcp_t* handle, int enable);
787 * Bind the handle to an address and port. `addr` should point to an
788 * initialized struct sockaddr_in or struct sockaddr_in6.
790 * When the port is already taken, you can expect to see an UV_EADDRINUSE
791 * error from either uv_tcp_bind(), uv_listen() or uv_tcp_connect().
793 * That is, a successful call to uv_tcp_bind() does not guarantee that
794 * the call to uv_listen() or uv_tcp_connect() will succeed as well.
796 UV_EXTERN int uv_tcp_bind(uv_tcp_t* handle, const struct sockaddr* addr);
798 UV_EXTERN int uv_tcp_getsockname(uv_tcp_t* handle, struct sockaddr* name,
800 UV_EXTERN int uv_tcp_getpeername(uv_tcp_t* handle, struct sockaddr* name,
804 * Establish an IPv4 or IPv6 TCP connection. Provide an initialized TCP handle
805 * and an uninitialized uv_connect_t*. `addr` should point to an initialized
806 * struct sockaddr_in or struct sockaddr_in6.
808 * The callback is made when the connection has been established or when a
809 * connection error happened.
811 UV_EXTERN int uv_tcp_connect(uv_connect_t* req,
813 const struct sockaddr* addr,
816 /* uv_connect_t is a subclass of uv_req_t */
817 struct uv_connect_s {
821 UV_CONNECT_PRIVATE_FIELDS
830 /* Disables dual stack mode. */
833 * Indicates message was truncated because read buffer was too small. The
834 * remainder was discarded by the OS. Used in uv_udp_recv_cb.
840 * Called after a uv_udp_send() or uv_udp_send6(). status 0 indicates
841 * success otherwise error.
843 typedef void (*uv_udp_send_cb)(uv_udp_send_t* req, int status);
846 * Callback that is invoked when a new UDP datagram is received.
849 * nread Number of bytes that have been received.
850 * 0 if there is no more data to read. You may
851 * discard or repurpose the read buffer.
852 * < 0 if a transmission error was detected.
853 * buf uv_buf_t with the received data.
854 * addr struct sockaddr_in or struct sockaddr_in6.
855 * Valid for the duration of the callback only.
856 * flags One or more OR'ed UV_UDP_* constants.
857 * Right now only UV_UDP_PARTIAL is used.
859 typedef void (*uv_udp_recv_cb)(uv_udp_t* handle,
862 const struct sockaddr* addr,
865 /* uv_udp_t is a subclass of uv_handle_t */
868 UV_UDP_PRIVATE_FIELDS
871 /* uv_udp_send_t is a subclass of uv_req_t */
872 struct uv_udp_send_s {
876 UV_UDP_SEND_PRIVATE_FIELDS
880 * Initialize a new UDP handle. The actual socket is created lazily.
881 * Returns 0 on success.
883 UV_EXTERN int uv_udp_init(uv_loop_t*, uv_udp_t* handle);
886 * Opens an existing file descriptor or SOCKET as a udp handle.
889 * The only requirement of the sock argument is that it follows the
890 * datagram contract (works in unconnected mode, supports sendmsg()/recvmsg(),
891 * etc.). In other words, other datagram-type sockets like raw sockets or
892 * netlink sockets can also be passed to this function.
894 * This sets the SO_REUSEPORT socket flag on the BSDs and OS X. On other
895 * UNIX platforms, it sets the SO_REUSEADDR flag. What that means is that
896 * multiple threads or processes can bind to the same address without error
897 * (provided they all set the flag) but only the last one to bind will receive
898 * any traffic, in effect "stealing" the port from the previous listener.
899 * This behavior is something of an anomaly and may be replaced by an explicit
900 * opt-in mechanism in future versions of libuv.
902 UV_EXTERN int uv_udp_open(uv_udp_t* handle, uv_os_sock_t sock);
905 * Bind to a IPv4 address and port.
908 * handle UDP handle. Should have been initialized with `uv_udp_init`.
909 * addr struct sockaddr_in or struct sockaddr_in6 with the address and
914 * 0 on success, or an error code < 0 on failure.
916 * This sets the SO_REUSEPORT socket flag on the BSDs and OS X. On other
917 * UNIX platforms, it sets the SO_REUSEADDR flag. What that means is that
918 * multiple threads or processes can bind to the same address without error
919 * (provided they all set the flag) but only the last one to bind will receive
920 * any traffic, in effect "stealing" the port from the previous listener.
921 * This behavior is something of an anomaly and may be replaced by an explicit
922 * opt-in mechanism in future versions of libuv.
924 UV_EXTERN int uv_udp_bind(uv_udp_t* handle,
925 const struct sockaddr* addr,
928 UV_EXTERN int uv_udp_getsockname(uv_udp_t* handle, struct sockaddr* name,
932 * Set membership for a multicast address
935 * handle UDP handle. Should have been initialized with
937 * multicast_addr multicast address to set membership for
938 * interface_addr interface address
939 * membership Should be UV_JOIN_GROUP or UV_LEAVE_GROUP
942 * 0 on success, or an error code < 0 on failure.
944 UV_EXTERN int uv_udp_set_membership(uv_udp_t* handle,
945 const char* multicast_addr, const char* interface_addr,
946 uv_membership membership);
949 * Set IP multicast loop flag. Makes multicast packets loop back to
953 * handle UDP handle. Should have been initialized with
955 * on 1 for on, 0 for off
958 * 0 on success, or an error code < 0 on failure.
960 UV_EXTERN int uv_udp_set_multicast_loop(uv_udp_t* handle, int on);
963 * Set the multicast ttl
966 * handle UDP handle. Should have been initialized with
971 * 0 on success, or an error code < 0 on failure.
973 UV_EXTERN int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl);
976 * Set broadcast on or off
979 * handle UDP handle. Should have been initialized with
981 * on 1 for on, 0 for off
984 * 0 on success, or an error code < 0 on failure.
986 UV_EXTERN int uv_udp_set_broadcast(uv_udp_t* handle, int on);
989 * Set the time to live
992 * handle UDP handle. Should have been initialized with
997 * 0 on success, or an error code < 0 on failure.
999 UV_EXTERN int uv_udp_set_ttl(uv_udp_t* handle, int ttl);
1002 * Send data. If the socket has not previously been bound with `uv_udp_bind`
1003 * or `uv_udp_bind6`, it is bound to 0.0.0.0 (the "all interfaces" address)
1004 * and a random port number.
1007 * req UDP request handle. Need not be initialized.
1008 * handle UDP handle. Should have been initialized with `uv_udp_init`.
1009 * bufs List of buffers to send.
1010 * nbufs Number of buffers in `bufs`.
1011 * addr Address of the remote peer. See `uv_ip4_addr`.
1012 * send_cb Callback to invoke when the data has been sent out.
1015 * 0 on success, or an error code < 0 on failure.
1017 UV_EXTERN int uv_udp_send(uv_udp_send_t* req,
1019 const uv_buf_t bufs[],
1021 const struct sockaddr* addr,
1022 uv_udp_send_cb send_cb);
1025 * Receive data. If the socket has not previously been bound with `uv_udp_bind`
1026 * or `uv_udp_bind6`, it is bound to 0.0.0.0 (the "all interfaces" address)
1027 * and a random port number.
1030 * handle UDP handle. Should have been initialized with `uv_udp_init`.
1031 * alloc_cb Callback to invoke when temporary storage is needed.
1032 * recv_cb Callback to invoke with received data.
1035 * 0 on success, or an error code < 0 on failure.
1037 UV_EXTERN int uv_udp_recv_start(uv_udp_t* handle, uv_alloc_cb alloc_cb,
1038 uv_udp_recv_cb recv_cb);
1041 * Stop listening for incoming datagrams.
1044 * handle UDP handle. Should have been initialized with `uv_udp_init`.
1047 * 0 on success, or an error code < 0 on failure.
1049 UV_EXTERN int uv_udp_recv_stop(uv_udp_t* handle);
1053 * uv_tty_t is a subclass of uv_stream_t
1055 * Representing a stream for the console.
1060 UV_TTY_PRIVATE_FIELDS
1064 * Initialize a new TTY stream with the given file descriptor. Usually the
1065 * file descriptor will be
1069 * The last argument, readable, specifies if you plan on calling
1070 * uv_read_start with this stream. stdin is readable, stdout is not.
1072 * TTY streams which are not readable have blocking writes.
1074 UV_EXTERN int uv_tty_init(uv_loop_t*, uv_tty_t*, uv_file fd, int readable);
1077 * Set mode. 0 for normal, 1 for raw.
1079 UV_EXTERN int uv_tty_set_mode(uv_tty_t*, int mode);
1082 * To be called when the program exits. Resets TTY settings to default
1083 * values for the next process to take over.
1085 * This function is async signal-safe on UNIX platforms but can fail with error
1086 * code UV_EBUSY if you call it when execution is inside uv_tty_set_mode().
1088 UV_EXTERN int uv_tty_reset_mode(void);
1091 * Gets the current Window size. On success zero is returned.
1093 UV_EXTERN int uv_tty_get_winsize(uv_tty_t*, int* width, int* height);
1096 * Used to detect what type of stream should be used with a given file
1097 * descriptor. Usually this will be used during initialization to guess the
1098 * type of the stdio streams.
1099 * For isatty() functionality use this function and test for UV_TTY.
1101 UV_EXTERN uv_handle_type uv_guess_handle(uv_file file);
1104 * uv_pipe_t is a subclass of uv_stream_t
1106 * Representing a pipe stream or pipe server. On Windows this is a Named
1107 * Pipe. On Unix this is a UNIX domain socket.
1112 int ipc; /* non-zero if this pipe is used for passing handles */
1113 UV_PIPE_PRIVATE_FIELDS
1117 * Initialize a pipe. The last argument is a boolean to indicate if
1118 * this pipe will be used for handle passing between processes.
1120 UV_EXTERN int uv_pipe_init(uv_loop_t*, uv_pipe_t* handle, int ipc);
1123 * Opens an existing file descriptor or HANDLE as a pipe.
1125 UV_EXTERN int uv_pipe_open(uv_pipe_t*, uv_file file);
1128 * Bind the pipe to a file path (UNIX) or a name (Windows.)
1130 * Paths on UNIX get truncated to `sizeof(sockaddr_un.sun_path)` bytes,
1131 * typically between 92 and 108 bytes.
1133 UV_EXTERN int uv_pipe_bind(uv_pipe_t* handle, const char* name);
1136 * Connect to the UNIX domain socket or the named pipe.
1138 * Paths on UNIX get truncated to `sizeof(sockaddr_un.sun_path)` bytes,
1139 * typically between 92 and 108 bytes.
1141 UV_EXTERN void uv_pipe_connect(uv_connect_t* req, uv_pipe_t* handle,
1142 const char* name, uv_connect_cb cb);
1145 * This setting applies to Windows only.
1146 * Set the number of pending pipe instance handles when the pipe server
1147 * is waiting for connections.
1149 UV_EXTERN void uv_pipe_pending_instances(uv_pipe_t* handle, int count);
1153 * uv_poll_t is a subclass of uv_handle_t.
1155 * The uv_poll watcher is used to watch file descriptors for readability and
1156 * writability, similar to the purpose of poll(2).
1158 * The purpose of uv_poll is to enable integrating external libraries that
1159 * rely on the event loop to signal it about the socket status changes, like
1160 * c-ares or libssh2. Using uv_poll_t for any other other purpose is not
1161 * recommended; uv_tcp_t, uv_udp_t, etc. provide an implementation that is
1162 * much faster and more scalable than what can be achieved with uv_poll_t,
1163 * especially on Windows.
1165 * It is possible that uv_poll occasionally signals that a file descriptor is
1166 * readable or writable even when it isn't. The user should therefore always
1167 * be prepared to handle EAGAIN or equivalent when it attempts to read from or
1170 * It is not okay to have multiple active uv_poll watchers for the same socket.
1171 * This can cause libuv to busyloop or otherwise malfunction.
1173 * The user should not close a file descriptor while it is being polled by an
1174 * active uv_poll watcher. This can cause the poll watcher to report an error,
1175 * but it might also start polling another socket. However the fd can be safely
1176 * closed immediately after a call to uv_poll_stop() or uv_close().
1178 * On windows only sockets can be polled with uv_poll. On unix any file
1179 * descriptor that would be accepted by poll(2) can be used with uv_poll.
1184 UV_POLL_PRIVATE_FIELDS
1187 enum uv_poll_event {
1192 /* Initialize the poll watcher using a file descriptor. */
1193 UV_EXTERN int uv_poll_init(uv_loop_t* loop, uv_poll_t* handle, int fd);
1195 /* Initialize the poll watcher using a socket descriptor. On unix this is */
1196 /* identical to uv_poll_init. On windows it takes a SOCKET handle. */
1197 UV_EXTERN int uv_poll_init_socket(uv_loop_t* loop, uv_poll_t* handle,
1198 uv_os_sock_t socket);
1201 * Starts polling the file descriptor. `events` is a bitmask consisting made up
1202 * of UV_READABLE and UV_WRITABLE. As soon as an event is detected the callback
1203 * will be called with `status` set to 0, and the detected events set en the
1206 * If an error happens while polling status, `status` < 0 and corresponds
1207 * with one of the UV_E* error codes. The user should not close the socket
1208 * while uv_poll is active. If the user does that anyway, the callback *may*
1209 * be called reporting an error status, but this is not guaranteed.
1211 * Calling uv_poll_start on an uv_poll watcher that is already active is fine.
1212 * Doing so will update the events mask that is being watched for.
1214 UV_EXTERN int uv_poll_start(uv_poll_t* handle, int events, uv_poll_cb cb);
1216 /* Stops polling the file descriptor. */
1217 UV_EXTERN int uv_poll_stop(uv_poll_t* handle);
1221 * uv_prepare_t is a subclass of uv_handle_t.
1223 * Every active prepare handle gets its callback called exactly once per loop
1224 * iteration, just before the system blocks to wait for completed i/o.
1226 struct uv_prepare_s {
1228 UV_PREPARE_PRIVATE_FIELDS
1231 UV_EXTERN int uv_prepare_init(uv_loop_t*, uv_prepare_t* prepare);
1233 UV_EXTERN int uv_prepare_start(uv_prepare_t* prepare, uv_prepare_cb cb);
1235 UV_EXTERN int uv_prepare_stop(uv_prepare_t* prepare);
1239 * uv_check_t is a subclass of uv_handle_t.
1241 * Every active check handle gets its callback called exactly once per loop
1242 * iteration, just after the system returns from blocking.
1246 UV_CHECK_PRIVATE_FIELDS
1249 UV_EXTERN int uv_check_init(uv_loop_t*, uv_check_t* check);
1251 UV_EXTERN int uv_check_start(uv_check_t* check, uv_check_cb cb);
1253 UV_EXTERN int uv_check_stop(uv_check_t* check);
1257 * uv_idle_t is a subclass of uv_handle_t.
1259 * Every active idle handle gets its callback called repeatedly until it is
1260 * stopped. This happens after all other types of callbacks are processed.
1261 * When there are multiple "idle" handles active, their callbacks are called
1266 UV_IDLE_PRIVATE_FIELDS
1269 UV_EXTERN int uv_idle_init(uv_loop_t*, uv_idle_t* idle);
1271 UV_EXTERN int uv_idle_start(uv_idle_t* idle, uv_idle_cb cb);
1273 UV_EXTERN int uv_idle_stop(uv_idle_t* idle);
1277 * uv_async_t is a subclass of uv_handle_t.
1279 * uv_async_send wakes up the event loop and calls the async handle's callback.
1280 * There is no guarantee that every uv_async_send call leads to exactly one
1281 * invocation of the callback; the only guarantee is that the callback function
1282 * is called at least once after the call to async_send. Unlike all other
1283 * libuv functions, uv_async_send can be called from another thread.
1287 UV_ASYNC_PRIVATE_FIELDS
1291 * Initialize the uv_async_t handle. A NULL callback is allowed.
1293 * Note that uv_async_init(), unlike other libuv functions, immediately
1294 * starts the handle. To stop the handle again, close it with uv_close().
1296 UV_EXTERN int uv_async_init(uv_loop_t*, uv_async_t* async,
1297 uv_async_cb async_cb);
1300 * This can be called from other threads to wake up a libuv thread.
1302 * libuv is single threaded at the moment.
1304 UV_EXTERN int uv_async_send(uv_async_t* async);
1308 * uv_timer_t is a subclass of uv_handle_t.
1310 * Used to get woken up at a specified time in the future.
1314 UV_TIMER_PRIVATE_FIELDS
1317 UV_EXTERN int uv_timer_init(uv_loop_t*, uv_timer_t* handle);
1320 * Start the timer. `timeout` and `repeat` are in milliseconds.
1322 * If timeout is zero, the callback fires on the next tick of the event loop.
1324 * If repeat is non-zero, the callback fires first after timeout milliseconds
1325 * and then repeatedly after repeat milliseconds.
1327 UV_EXTERN int uv_timer_start(uv_timer_t* handle,
1332 UV_EXTERN int uv_timer_stop(uv_timer_t* handle);
1335 * Stop the timer, and if it is repeating restart it using the repeat value
1336 * as the timeout. If the timer has never been started before it returns
1339 UV_EXTERN int uv_timer_again(uv_timer_t* handle);
1342 * Set the repeat value in milliseconds. Note that if the repeat value is set
1343 * from a timer callback it does not immediately take effect. If the timer was
1344 * non-repeating before, it will have been stopped. If it was repeating, then
1345 * the old repeat value will have been used to schedule the next timeout.
1347 UV_EXTERN void uv_timer_set_repeat(uv_timer_t* handle, uint64_t repeat);
1349 UV_EXTERN uint64_t uv_timer_get_repeat(const uv_timer_t* handle);
1353 * uv_getaddrinfo_t is a subclass of uv_req_t
1355 * Request object for uv_getaddrinfo.
1357 struct uv_getaddrinfo_s {
1361 UV_GETADDRINFO_PRIVATE_FIELDS
1366 * Asynchronous getaddrinfo(3).
1368 * Either node or service may be NULL but not both.
1370 * hints is a pointer to a struct addrinfo with additional address type
1371 * constraints, or NULL. Consult `man -s 3 getaddrinfo` for details.
1373 * Returns 0 on success or an error code < 0 on failure.
1375 * If successful, your callback gets called sometime in the future with the
1376 * lookup result, which is either:
1378 * a) err == 0, the res argument points to a valid struct addrinfo, or
1379 * b) err < 0, the res argument is NULL. See the UV_EAI_* constants.
1381 * Call uv_freeaddrinfo() to free the addrinfo structure.
1383 UV_EXTERN int uv_getaddrinfo(uv_loop_t* loop,
1384 uv_getaddrinfo_t* req,
1385 uv_getaddrinfo_cb getaddrinfo_cb,
1387 const char* service,
1388 const struct addrinfo* hints);
1391 * Free the struct addrinfo. Passing NULL is allowed and is a no-op.
1393 UV_EXTERN void uv_freeaddrinfo(struct addrinfo* ai);
1396 /* uv_spawn() options */
1399 UV_CREATE_PIPE = 0x01,
1400 UV_INHERIT_FD = 0x02,
1401 UV_INHERIT_STREAM = 0x04,
1403 /* When UV_CREATE_PIPE is specified, UV_READABLE_PIPE and UV_WRITABLE_PIPE
1404 * determine the direction of flow, from the child process' perspective. Both
1405 * flags may be specified to create a duplex data stream.
1407 UV_READABLE_PIPE = 0x10,
1408 UV_WRITABLE_PIPE = 0x20
1411 typedef struct uv_stdio_container_s {
1412 uv_stdio_flags flags;
1415 uv_stream_t* stream;
1418 } uv_stdio_container_t;
1420 typedef struct uv_process_options_s {
1421 uv_exit_cb exit_cb; /* Called after the process exits. */
1422 const char* file; /* Path to program to execute. */
1424 * Command line arguments. args[0] should be the path to the program. On
1425 * Windows this uses CreateProcess which concatenates the arguments into a
1426 * string this can cause some strange errors. See the note at
1427 * windows_verbatim_arguments.
1431 * This will be set as the environ variable in the subprocess. If this is
1432 * NULL then the parents environ will be used.
1436 * If non-null this represents a directory the subprocess should execute
1437 * in. Stands for current working directory.
1441 * Various flags that control how uv_spawn() behaves. See the definition of
1442 * `enum uv_process_flags` below.
1446 * The `stdio` field points to an array of uv_stdio_container_t structs that
1447 * describe the file descriptors that will be made available to the child
1448 * process. The convention is that stdio[0] points to stdin, fd 1 is used for
1449 * stdout, and fd 2 is stderr.
1451 * Note that on windows file descriptors greater than 2 are available to the
1452 * child process only if the child processes uses the MSVCRT runtime.
1455 uv_stdio_container_t* stdio;
1457 * Libuv can change the child process' user/group id. This happens only when
1458 * the appropriate bits are set in the flags fields. This is not supported on
1459 * windows; uv_spawn() will fail and set the error to UV_ENOTSUP.
1463 } uv_process_options_t;
1466 * These are the flags that can be used for the uv_process_options.flags field.
1468 enum uv_process_flags {
1470 * Set the child process' user id. The user id is supplied in the `uid` field
1471 * of the options struct. This does not work on windows; setting this flag
1472 * will cause uv_spawn() to fail.
1474 UV_PROCESS_SETUID = (1 << 0),
1476 * Set the child process' group id. The user id is supplied in the `gid`
1477 * field of the options struct. This does not work on windows; setting this
1478 * flag will cause uv_spawn() to fail.
1480 UV_PROCESS_SETGID = (1 << 1),
1482 * Do not wrap any arguments in quotes, or perform any other escaping, when
1483 * converting the argument list into a command line string. This option is
1484 * only meaningful on Windows systems. On unix it is silently ignored.
1486 UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS = (1 << 2),
1488 * Spawn the child process in a detached state - this will make it a process
1489 * group leader, and will effectively enable the child to keep running after
1490 * the parent exits. Note that the child process will still keep the
1491 * parent's event loop alive unless the parent process calls uv_unref() on
1492 * the child's process handle.
1494 UV_PROCESS_DETACHED = (1 << 3),
1496 * Hide the subprocess console window that would normally be created. This
1497 * option is only meaningful on Windows systems. On unix it is silently
1500 UV_PROCESS_WINDOWS_HIDE = (1 << 4)
1504 * uv_process_t is a subclass of uv_handle_t
1506 struct uv_process_s {
1510 UV_PROCESS_PRIVATE_FIELDS
1514 * Initializes the uv_process_t and starts the process. If the process is
1515 * successfully spawned, then this function will return 0. Otherwise, the
1516 * negative error code corresponding to the reason it couldn't spawn is
1519 * Possible reasons for failing to spawn would include (but not be limited to)
1520 * the file to execute not existing, not having permissions to use the setuid or
1521 * setgid specified, or not having enough memory to allocate for the new
1524 UV_EXTERN int uv_spawn(uv_loop_t* loop,
1525 uv_process_t* handle,
1526 const uv_process_options_t* options);
1530 * Kills the process with the specified signal. The user must still
1531 * call uv_close on the process.
1533 UV_EXTERN int uv_process_kill(uv_process_t*, int signum);
1536 /* Kills the process with the specified signal. */
1537 UV_EXTERN int uv_kill(int pid, int signum);
1541 * uv_work_t is a subclass of uv_req_t
1547 uv_after_work_cb after_work_cb;
1548 UV_WORK_PRIVATE_FIELDS
1551 /* Queues a work request to execute asynchronously on the thread pool. */
1552 UV_EXTERN int uv_queue_work(uv_loop_t* loop, uv_work_t* req,
1553 uv_work_cb work_cb, uv_after_work_cb after_work_cb);
1555 /* Cancel a pending request. Fails if the request is executing or has finished
1558 * Returns 0 on success, or an error code < 0 on failure.
1560 * Only cancellation of uv_fs_t, uv_getaddrinfo_t and uv_work_t requests is
1561 * currently supported.
1563 * Cancelled requests have their callbacks invoked some time in the future.
1564 * It's _not_ safe to free the memory associated with the request until your
1565 * callback is called.
1567 * Here is how cancellation is reported to your callback:
1569 * - A uv_fs_t request has its req->result field set to UV_ECANCELED.
1571 * - A uv_work_t or uv_getaddrinfo_t request has its callback invoked with
1572 * status == UV_ECANCELED.
1574 * This function is currently only implemented on UNIX platforms. On Windows,
1575 * it always returns UV_ENOSYS.
1577 UV_EXTERN int uv_cancel(uv_req_t* req);
1580 struct uv_cpu_info_s {
1583 struct uv_cpu_times_s {
1592 struct uv_interface_address_s {
1597 struct sockaddr_in address4;
1598 struct sockaddr_in6 address6;
1601 struct sockaddr_in netmask4;
1602 struct sockaddr_in6 netmask6;
1606 UV_EXTERN char** uv_setup_args(int argc, char** argv);
1607 UV_EXTERN int uv_get_process_title(char* buffer, size_t size);
1608 UV_EXTERN int uv_set_process_title(const char* title);
1609 UV_EXTERN int uv_resident_set_memory(size_t* rss);
1610 UV_EXTERN int uv_uptime(double* uptime);
1613 * This allocates cpu_infos array, and sets count. The array
1614 * is freed using uv_free_cpu_info().
1616 UV_EXTERN int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count);
1617 UV_EXTERN void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count);
1620 * This allocates addresses array, and sets count. The array
1621 * is freed using uv_free_interface_addresses().
1623 UV_EXTERN int uv_interface_addresses(uv_interface_address_t** addresses,
1625 UV_EXTERN void uv_free_interface_addresses(uv_interface_address_t* addresses,
1629 * File System Methods.
1631 * The uv_fs_* functions execute a blocking system call asynchronously (in a
1632 * thread pool) and call the specified callback in the specified loop after
1633 * completion. If the user gives NULL as the callback the blocking system
1634 * call will be called synchronously. req should be a pointer to an
1635 * uninitialized uv_fs_t object.
1637 * uv_fs_req_cleanup() must be called after completion of the uv_fs_
1638 * function to free any internal memory allocations associated with the
1672 /* uv_fs_t is a subclass of uv_req_t */
1681 uv_stat_t statbuf; /* Stores the result of uv_fs_stat and uv_fs_fstat. */
1682 UV_FS_PRIVATE_FIELDS
1685 UV_EXTERN void uv_fs_req_cleanup(uv_fs_t* req);
1687 UV_EXTERN int uv_fs_close(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1690 UV_EXTERN int uv_fs_open(uv_loop_t* loop, uv_fs_t* req, const char* path,
1691 int flags, int mode, uv_fs_cb cb);
1693 UV_EXTERN int uv_fs_read(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1694 void* buf, size_t length, int64_t offset, uv_fs_cb cb);
1696 UV_EXTERN int uv_fs_unlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
1699 UV_EXTERN int uv_fs_write(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1700 const void* buf, size_t length, int64_t offset, uv_fs_cb cb);
1702 UV_EXTERN int uv_fs_mkdir(uv_loop_t* loop, uv_fs_t* req, const char* path,
1703 int mode, uv_fs_cb cb);
1705 UV_EXTERN int uv_fs_rmdir(uv_loop_t* loop, uv_fs_t* req, const char* path,
1708 UV_EXTERN int uv_fs_readdir(uv_loop_t* loop, uv_fs_t* req,
1709 const char* path, int flags, uv_fs_cb cb);
1711 UV_EXTERN int uv_fs_stat(uv_loop_t* loop, uv_fs_t* req, const char* path,
1714 UV_EXTERN int uv_fs_fstat(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1717 UV_EXTERN int uv_fs_rename(uv_loop_t* loop, uv_fs_t* req, const char* path,
1718 const char* new_path, uv_fs_cb cb);
1720 UV_EXTERN int uv_fs_fsync(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1723 UV_EXTERN int uv_fs_fdatasync(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1726 UV_EXTERN int uv_fs_ftruncate(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1727 int64_t offset, uv_fs_cb cb);
1729 UV_EXTERN int uv_fs_sendfile(uv_loop_t* loop, uv_fs_t* req, uv_file out_fd,
1730 uv_file in_fd, int64_t in_offset, size_t length, uv_fs_cb cb);
1732 UV_EXTERN int uv_fs_chmod(uv_loop_t* loop, uv_fs_t* req, const char* path,
1733 int mode, uv_fs_cb cb);
1735 UV_EXTERN int uv_fs_utime(uv_loop_t* loop, uv_fs_t* req, const char* path,
1736 double atime, double mtime, uv_fs_cb cb);
1738 UV_EXTERN int uv_fs_futime(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1739 double atime, double mtime, uv_fs_cb cb);
1741 UV_EXTERN int uv_fs_lstat(uv_loop_t* loop, uv_fs_t* req, const char* path,
1744 UV_EXTERN int uv_fs_link(uv_loop_t* loop, uv_fs_t* req, const char* path,
1745 const char* new_path, uv_fs_cb cb);
1748 * This flag can be used with uv_fs_symlink on Windows
1749 * to specify whether path argument points to a directory.
1751 #define UV_FS_SYMLINK_DIR 0x0001
1754 * This flag can be used with uv_fs_symlink on Windows
1755 * to specify whether the symlink is to be created using junction points.
1757 #define UV_FS_SYMLINK_JUNCTION 0x0002
1759 UV_EXTERN int uv_fs_symlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
1760 const char* new_path, int flags, uv_fs_cb cb);
1762 UV_EXTERN int uv_fs_readlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
1765 UV_EXTERN int uv_fs_fchmod(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1766 int mode, uv_fs_cb cb);
1768 UV_EXTERN int uv_fs_chown(uv_loop_t* loop, uv_fs_t* req, const char* path,
1769 uv_uid_t uid, uv_gid_t gid, uv_fs_cb cb);
1771 UV_EXTERN int uv_fs_fchown(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1772 uv_uid_t uid, uv_gid_t gid, uv_fs_cb cb);
1781 struct uv_fs_event_s {
1784 UV_FS_EVENT_PRIVATE_FIELDS
1789 * uv_fs_stat() based polling file watcher.
1791 struct uv_fs_poll_s {
1793 /* Private, don't touch. */
1797 UV_EXTERN int uv_fs_poll_init(uv_loop_t* loop, uv_fs_poll_t* handle);
1800 * Check the file at `path` for changes every `interval` milliseconds.
1802 * Your callback is invoked with `status < 0` if `path` does not exist
1803 * or is inaccessible. The watcher is *not* stopped but your callback is
1804 * not called again until something changes (e.g. when the file is created
1805 * or the error reason changes).
1807 * When `status == 0`, your callback receives pointers to the old and new
1808 * `uv_stat_t` structs. They are valid for the duration of the callback
1811 * For maximum portability, use multi-second intervals. Sub-second intervals
1812 * will not detect all changes on many file systems.
1814 UV_EXTERN int uv_fs_poll_start(uv_fs_poll_t* handle,
1815 uv_fs_poll_cb poll_cb,
1817 unsigned int interval);
1819 UV_EXTERN int uv_fs_poll_stop(uv_fs_poll_t* handle);
1823 * UNIX signal handling on a per-event loop basis. The implementation is not
1824 * ultra efficient so don't go creating a million event loops with a million
1827 * Note to Linux users: SIGRT0 and SIGRT1 (signals 32 and 33) are used by the
1828 * NPTL pthreads library to manage threads. Installing watchers for those
1829 * signals will lead to unpredictable behavior and is strongly discouraged.
1830 * Future versions of libuv may simply reject them.
1832 * Some signal support is available on Windows:
1834 * SIGINT is normally delivered when the user presses CTRL+C. However, like
1835 * on Unix, it is not generated when terminal raw mode is enabled.
1837 * SIGBREAK is delivered when the user pressed CTRL+BREAK.
1839 * SIGHUP is generated when the user closes the console window. On SIGHUP the
1840 * program is given approximately 10 seconds to perform cleanup. After that
1841 * Windows will unconditionally terminate it.
1843 * SIGWINCH is raised whenever libuv detects that the console has been
1844 * resized. SIGWINCH is emulated by libuv when the program uses an uv_tty_t
1845 * handle to write to the console. SIGWINCH may not always be delivered in a
1846 * timely manner; libuv will only detect size changes when the cursor is
1847 * being moved. When a readable uv_tty_handle is used in raw mode, resizing
1848 * the console buffer will also trigger a SIGWINCH signal.
1850 * Watchers for other signals can be successfully created, but these signals
1851 * are never generated. These signals are: SIGILL, SIGABRT, SIGFPE, SIGSEGV,
1852 * SIGTERM and SIGKILL.
1854 * Note that calls to raise() or abort() to programmatically raise a signal are
1855 * not detected by libuv; these will not trigger a signal watcher.
1857 struct uv_signal_s {
1859 uv_signal_cb signal_cb;
1861 UV_SIGNAL_PRIVATE_FIELDS
1864 UV_EXTERN int uv_signal_init(uv_loop_t* loop, uv_signal_t* handle);
1866 UV_EXTERN int uv_signal_start(uv_signal_t* handle,
1867 uv_signal_cb signal_cb,
1870 UV_EXTERN int uv_signal_stop(uv_signal_t* handle);
1874 * Gets load average.
1875 * See: http://en.wikipedia.org/wiki/Load_(computing)
1876 * Returns [0,0,0] on Windows.
1878 UV_EXTERN void uv_loadavg(double avg[3]);
1882 * Flags to be passed to uv_fs_event_start.
1884 enum uv_fs_event_flags {
1886 * By default, if the fs event watcher is given a directory name, we will
1887 * watch for all events in that directory. This flags overrides this behavior
1888 * and makes fs_event report only changes to the directory entry itself. This
1889 * flag does not affect individual files watched.
1890 * This flag is currently not implemented yet on any backend.
1892 UV_FS_EVENT_WATCH_ENTRY = 1,
1895 * By default uv_fs_event will try to use a kernel interface such as inotify
1896 * or kqueue to detect events. This may not work on remote filesystems such
1897 * as NFS mounts. This flag makes fs_event fall back to calling stat() on a
1899 * This flag is currently not implemented yet on any backend.
1901 UV_FS_EVENT_STAT = 2,
1904 * By default, event watcher, when watching directory, is not registering
1905 * (is ignoring) changes in it's subdirectories.
1906 * This flag will override this behaviour on platforms that support it.
1908 UV_FS_EVENT_RECURSIVE = 4
1912 UV_EXTERN int uv_fs_event_init(uv_loop_t* loop, uv_fs_event_t* handle);
1914 UV_EXTERN int uv_fs_event_start(uv_fs_event_t* handle,
1916 const char* filename,
1917 unsigned int flags);
1919 UV_EXTERN int uv_fs_event_stop(uv_fs_event_t* handle);
1924 /* Convert string ip addresses to binary structures */
1925 UV_EXTERN int uv_ip4_addr(const char* ip, int port, struct sockaddr_in* addr);
1926 UV_EXTERN int uv_ip6_addr(const char* ip, int port, struct sockaddr_in6* addr);
1928 /* Convert binary addresses to strings */
1929 UV_EXTERN int uv_ip4_name(struct sockaddr_in* src, char* dst, size_t size);
1930 UV_EXTERN int uv_ip6_name(struct sockaddr_in6* src, char* dst, size_t size);
1932 /* Cross-platform IPv6-capable implementation of the 'standard' inet_ntop */
1933 /* and inet_pton functions. On success they return 0. If an error */
1934 /* the target of the `dst` pointer is unmodified. */
1935 UV_EXTERN int uv_inet_ntop(int af, const void* src, char* dst, size_t size);
1936 UV_EXTERN int uv_inet_pton(int af, const char* src, void* dst);
1938 /* Gets the executable path */
1939 UV_EXTERN int uv_exepath(char* buffer, size_t* size);
1941 /* Gets the current working directory */
1942 UV_EXTERN int uv_cwd(char* buffer, size_t size);
1944 /* Changes the current working directory */
1945 UV_EXTERN int uv_chdir(const char* dir);
1947 /* Gets memory info in bytes */
1948 UV_EXTERN uint64_t uv_get_free_memory(void);
1949 UV_EXTERN uint64_t uv_get_total_memory(void);
1952 * Returns the current high-resolution real time. This is expressed in
1953 * nanoseconds. It is relative to an arbitrary time in the past. It is not
1954 * related to the time of day and therefore not subject to clock drift. The
1955 * primary use is for measuring performance between intervals.
1957 * Note not every platform can support nanosecond resolution; however, this
1958 * value will always be in nanoseconds.
1960 UV_EXTERN extern uint64_t uv_hrtime(void);
1964 * Disables inheritance for file descriptors / handles that this process
1965 * inherited from its parent. The effect is that child processes spawned by
1966 * this process don't accidentally inherit these handles.
1968 * It is recommended to call this function as early in your program as possible,
1969 * before the inherited file descriptors can be closed or duplicated.
1971 * Note that this function works on a best-effort basis: there is no guarantee
1972 * that libuv can discover all file descriptors that were inherited. In general
1973 * it does a better job on Windows than it does on unix.
1975 UV_EXTERN void uv_disable_stdio_inheritance(void);
1978 * Opens a shared library. The filename is in utf-8. Returns 0 on success and
1979 * -1 on error. Call `uv_dlerror(uv_lib_t*)` to get the error message.
1981 UV_EXTERN int uv_dlopen(const char* filename, uv_lib_t* lib);
1984 * Close the shared library.
1986 UV_EXTERN void uv_dlclose(uv_lib_t* lib);
1989 * Retrieves a data pointer from a dynamic library. It is legal for a symbol to
1990 * map to NULL. Returns 0 on success and -1 if the symbol was not found.
1992 UV_EXTERN int uv_dlsym(uv_lib_t* lib, const char* name, void** ptr);
1995 * Returns the last uv_dlopen() or uv_dlsym() error message.
1997 UV_EXTERN const char* uv_dlerror(uv_lib_t* lib);
2000 * The mutex functions return 0 on success or an error code < 0
2001 * (unless the return type is void, of course).
2003 UV_EXTERN int uv_mutex_init(uv_mutex_t* handle);
2004 UV_EXTERN void uv_mutex_destroy(uv_mutex_t* handle);
2005 UV_EXTERN void uv_mutex_lock(uv_mutex_t* handle);
2006 UV_EXTERN int uv_mutex_trylock(uv_mutex_t* handle);
2007 UV_EXTERN void uv_mutex_unlock(uv_mutex_t* handle);
2010 * Same goes for the read/write lock functions.
2012 UV_EXTERN int uv_rwlock_init(uv_rwlock_t* rwlock);
2013 UV_EXTERN void uv_rwlock_destroy(uv_rwlock_t* rwlock);
2014 UV_EXTERN void uv_rwlock_rdlock(uv_rwlock_t* rwlock);
2015 UV_EXTERN int uv_rwlock_tryrdlock(uv_rwlock_t* rwlock);
2016 UV_EXTERN void uv_rwlock_rdunlock(uv_rwlock_t* rwlock);
2017 UV_EXTERN void uv_rwlock_wrlock(uv_rwlock_t* rwlock);
2018 UV_EXTERN int uv_rwlock_trywrlock(uv_rwlock_t* rwlock);
2019 UV_EXTERN void uv_rwlock_wrunlock(uv_rwlock_t* rwlock);
2022 * Same goes for the semaphore functions.
2024 UV_EXTERN int uv_sem_init(uv_sem_t* sem, unsigned int value);
2025 UV_EXTERN void uv_sem_destroy(uv_sem_t* sem);
2026 UV_EXTERN void uv_sem_post(uv_sem_t* sem);
2027 UV_EXTERN void uv_sem_wait(uv_sem_t* sem);
2028 UV_EXTERN int uv_sem_trywait(uv_sem_t* sem);
2031 * Same goes for the condition variable functions.
2033 UV_EXTERN int uv_cond_init(uv_cond_t* cond);
2034 UV_EXTERN void uv_cond_destroy(uv_cond_t* cond);
2035 UV_EXTERN void uv_cond_signal(uv_cond_t* cond);
2036 UV_EXTERN void uv_cond_broadcast(uv_cond_t* cond);
2037 /* Waits on a condition variable without a timeout.
2040 * 1. callers should be prepared to deal with spurious wakeups.
2042 UV_EXTERN void uv_cond_wait(uv_cond_t* cond, uv_mutex_t* mutex);
2043 /* Waits on a condition variable with a timeout in nano seconds.
2044 * Returns 0 for success or UV_ETIMEDOUT on timeout, It aborts when other
2048 * 1. callers should be prepared to deal with spurious wakeups.
2049 * 2. the granularity of timeout on Windows is never less than one millisecond.
2050 * 3. uv_cond_timedwait takes a relative timeout, not an absolute time.
2052 UV_EXTERN int uv_cond_timedwait(uv_cond_t* cond, uv_mutex_t* mutex,
2055 UV_EXTERN int uv_barrier_init(uv_barrier_t* barrier, unsigned int count);
2056 UV_EXTERN void uv_barrier_destroy(uv_barrier_t* barrier);
2057 UV_EXTERN void uv_barrier_wait(uv_barrier_t* barrier);
2059 /* Runs a function once and only once. Concurrent calls to uv_once() with the
2060 * same guard will block all callers except one (it's unspecified which one).
2061 * The guard should be initialized statically with the UV_ONCE_INIT macro.
2063 UV_EXTERN void uv_once(uv_once_t* guard, void (*callback)(void));
2065 /* Thread-local storage. These functions largely follow the semantics of
2066 * pthread_key_create(), pthread_key_delete(), pthread_getspecific() and
2067 * pthread_setspecific().
2069 * Note that the total thread-local storage size may be limited.
2070 * That is, it may not be possible to create many TLS keys.
2072 UV_EXTERN int uv_key_create(uv_key_t* key);
2073 UV_EXTERN void uv_key_delete(uv_key_t* key);
2074 UV_EXTERN void* uv_key_get(uv_key_t* key);
2075 UV_EXTERN void uv_key_set(uv_key_t* key, void* value);
2077 UV_EXTERN int uv_thread_create(uv_thread_t *tid,
2078 void (*entry)(void *arg), void *arg);
2079 UV_EXTERN unsigned long uv_thread_self(void);
2080 UV_EXTERN int uv_thread_join(uv_thread_t *tid);
2082 /* The presence of these unions force similar struct layout. */
2083 #define XX(_, name) uv_ ## name ## _t name;
2084 union uv_any_handle {
2085 UV_HANDLE_TYPE_MAP(XX)
2095 /* User data - use this for whatever. */
2097 /* Loop reference counting */
2098 unsigned int active_handles;
2099 void* handle_queue[2];
2100 void* active_reqs[2];
2101 /* Internal flag to signal loop stop */
2102 unsigned int stop_flag;
2103 UV_LOOP_PRIVATE_FIELDS
2107 /* Don't export the private CPP symbols. */
2108 #undef UV_HANDLE_TYPE_PRIVATE
2109 #undef UV_REQ_TYPE_PRIVATE
2110 #undef UV_REQ_PRIVATE_FIELDS
2111 #undef UV_STREAM_PRIVATE_FIELDS
2112 #undef UV_TCP_PRIVATE_FIELDS
2113 #undef UV_PREPARE_PRIVATE_FIELDS
2114 #undef UV_CHECK_PRIVATE_FIELDS
2115 #undef UV_IDLE_PRIVATE_FIELDS
2116 #undef UV_ASYNC_PRIVATE_FIELDS
2117 #undef UV_TIMER_PRIVATE_FIELDS
2118 #undef UV_GETADDRINFO_PRIVATE_FIELDS
2119 #undef UV_FS_REQ_PRIVATE_FIELDS
2120 #undef UV_WORK_PRIVATE_FIELDS
2121 #undef UV_FS_EVENT_PRIVATE_FIELDS
2122 #undef UV_SIGNAL_PRIVATE_FIELDS
2123 #undef UV_LOOP_PRIVATE_FIELDS
2124 #undef UV_LOOP_PRIVATE_PLATFORM_FIELDS