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 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*);
289 UV_EXTERN int uv_has_ref(const uv_handle_t*);
292 * Update the event loop's concept of "now". Libuv caches the current time
293 * at the start of the event loop tick in order to reduce the number of
294 * time-related system calls.
296 * You won't normally need to call this function unless you have callbacks
297 * that block the event loop for longer periods of time, where "longer" is
298 * somewhat subjective but probably on the order of a millisecond or more.
300 UV_EXTERN void uv_update_time(uv_loop_t*);
303 * Return the current timestamp in milliseconds. The timestamp is cached at
304 * the start of the event loop tick, see |uv_update_time()| for details and
307 * The timestamp increases monotonically from some arbitrary point in time.
308 * Don't make assumptions about the starting point, you will only get
311 * Use uv_hrtime() if you need sub-millisecond granularity.
313 UV_EXTERN uint64_t uv_now(uv_loop_t*);
316 * Get backend file descriptor. Only kqueue, epoll and event ports are
319 * This can be used in conjunction with `uv_run(loop, UV_RUN_NOWAIT)` to
320 * poll in one thread and run the event loop's event callbacks in another.
322 * Useful for embedding libuv's event loop in another event loop.
323 * See test/test-embed.c for an example.
325 * Note that embedding a kqueue fd in another kqueue pollset doesn't work on
326 * all platforms. It's not an error to add the fd but it never generates
329 UV_EXTERN int uv_backend_fd(const uv_loop_t*);
332 * Get the poll timeout. The return value is in milliseconds, or -1 for no
335 UV_EXTERN int uv_backend_timeout(const uv_loop_t*);
339 * Should prepare a buffer that libuv can use to read data into.
341 * `suggested_size` is a hint. Returning a buffer that is smaller is perfectly
342 * okay as long as `buf.len > 0`.
344 * If you return a buffer with `buf.len == 0`, libuv skips the read and calls
345 * your read or recv callback with nread=UV_ENOBUFS.
347 * Note that returning a zero-length buffer does not stop the handle, call
348 * uv_read_stop() or uv_udp_recv_stop() for that.
350 typedef void (*uv_alloc_cb)(uv_handle_t* handle,
351 size_t suggested_size,
355 * `nread` is > 0 if there is data available, 0 if libuv is done reading for
356 * now, or < 0 on error.
358 * The callee is responsible for closing the stream when an error happens.
359 * Trying to read from the stream again is undefined.
361 * The callee is responsible for freeing the buffer, libuv does not reuse it.
362 * The buffer may be a null buffer (where buf->base=NULL and buf->len=0) on
365 typedef void (*uv_read_cb)(uv_stream_t* stream,
367 const uv_buf_t* buf);
370 * Just like the uv_read_cb except that if the pending parameter is true
371 * then you can use uv_accept() to pull the new handle into the process.
372 * If no handle is pending then pending will be UV_UNKNOWN_HANDLE.
374 typedef void (*uv_read2_cb)(uv_pipe_t* pipe,
377 uv_handle_type pending);
379 typedef void (*uv_write_cb)(uv_write_t* req, int status);
380 typedef void (*uv_connect_cb)(uv_connect_t* req, int status);
381 typedef void (*uv_shutdown_cb)(uv_shutdown_t* req, int status);
382 typedef void (*uv_connection_cb)(uv_stream_t* server, int status);
383 typedef void (*uv_close_cb)(uv_handle_t* handle);
384 typedef void (*uv_poll_cb)(uv_poll_t* handle, int status, int events);
385 typedef void (*uv_timer_cb)(uv_timer_t* handle, int status);
386 /* TODO: do these really need a status argument? */
387 typedef void (*uv_async_cb)(uv_async_t* handle, int status);
388 typedef void (*uv_prepare_cb)(uv_prepare_t* handle, int status);
389 typedef void (*uv_check_cb)(uv_check_t* handle, int status);
390 typedef void (*uv_idle_cb)(uv_idle_t* handle, int status);
391 typedef void (*uv_exit_cb)(uv_process_t*, int64_t exit_status, int term_signal);
392 typedef void (*uv_walk_cb)(uv_handle_t* handle, void* arg);
393 typedef void (*uv_fs_cb)(uv_fs_t* req);
394 typedef void (*uv_work_cb)(uv_work_t* req);
395 typedef void (*uv_after_work_cb)(uv_work_t* req, int status);
396 typedef void (*uv_getaddrinfo_cb)(uv_getaddrinfo_t* req,
398 struct addrinfo* res);
419 uv_timespec_t st_atim;
420 uv_timespec_t st_mtim;
421 uv_timespec_t st_ctim;
422 uv_timespec_t st_birthtim;
427 * This will be called repeatedly after the uv_fs_event_t is initialized.
428 * If uv_fs_event_t was initialized with a directory the filename parameter
429 * will be a relative path to a file contained in the directory.
430 * The events parameter is an ORed mask of enum uv_fs_event elements.
432 typedef void (*uv_fs_event_cb)(uv_fs_event_t* handle, const char* filename,
433 int events, int status);
435 typedef void (*uv_fs_poll_cb)(uv_fs_poll_t* handle,
437 const uv_stat_t* prev,
438 const uv_stat_t* curr);
440 typedef void (*uv_signal_cb)(uv_signal_t* handle, int signum);
450 * Most functions return 0 on success or an error code < 0 on failure.
452 UV_EXTERN const char* uv_strerror(int err);
453 UV_EXTERN const char* uv_err_name(int err);
456 #define UV_REQ_FIELDS \
462 void* active_queue[2]; \
463 UV_REQ_PRIVATE_FIELDS \
465 /* Abstract base class of all requests. */
471 /* Platform-specific request types */
476 * uv_shutdown_t is a subclass of uv_req_t
478 * Shutdown the outgoing (write) side of a duplex stream. It waits for
479 * pending write requests to complete. The handle should refer to a
480 * initialized stream. req should be an uninitialized shutdown request
481 * struct. The cb is called after shutdown is complete.
483 UV_EXTERN int uv_shutdown(uv_shutdown_t* req, uv_stream_t* handle,
486 struct uv_shutdown_s {
490 UV_SHUTDOWN_PRIVATE_FIELDS
494 #define UV_HANDLE_FIELDS \
496 uv_close_cb close_cb; \
500 uv_handle_type type; \
502 void* handle_queue[2]; \
503 UV_HANDLE_PRIVATE_FIELDS \
505 /* The abstract base class of all handles. */
511 * Returns size of various handle types, useful for FFI
512 * bindings to allocate correct memory without copying struct
515 UV_EXTERN size_t uv_handle_size(uv_handle_type type);
518 * Returns size of request types, useful for dynamic lookup with FFI
520 UV_EXTERN size_t uv_req_size(uv_req_type type);
523 * Returns non-zero if the handle is active, zero if it's inactive.
525 * What "active" means depends on the type of handle:
527 * - A uv_async_t handle is always active and cannot be deactivated, except
528 * by closing it with uv_close().
530 * - A uv_pipe_t, uv_tcp_t, uv_udp_t, etc. handle - basically any handle that
531 * deals with I/O - is active when it is doing something that involves I/O,
532 * like reading, writing, connecting, accepting new connections, etc.
534 * - A uv_check_t, uv_idle_t, uv_timer_t, etc. handle is active when it has
535 * been started with a call to uv_check_start(), uv_idle_start(), etc.
537 * Rule of thumb: if a handle of type uv_foo_t has a uv_foo_start()
538 * function, then it's active from the moment that function is called.
539 * Likewise, uv_foo_stop() deactivates the handle again.
542 UV_EXTERN int uv_is_active(const uv_handle_t* handle);
545 * Walk the list of open handles.
547 UV_EXTERN void uv_walk(uv_loop_t* loop, uv_walk_cb walk_cb, void* arg);
551 * Request handle to be closed. close_cb will be called asynchronously after
552 * this call. This MUST be called on each handle before memory is released.
554 * Note that handles that wrap file descriptors are closed immediately but
555 * close_cb will still be deferred to the next iteration of the event loop.
556 * It gives you a chance to free up any resources associated with the handle.
558 * In-progress requests, like uv_connect_t or uv_write_t, are cancelled and
559 * have their callbacks called asynchronously with status=UV_ECANCELED.
561 UV_EXTERN void uv_close(uv_handle_t* handle, uv_close_cb close_cb);
565 * Constructor for uv_buf_t.
566 * Due to platform differences the user cannot rely on the ordering of the
567 * base and len members of the uv_buf_t struct. The user is responsible for
568 * freeing base after the uv_buf_t is done. Return struct passed by value.
570 UV_EXTERN uv_buf_t uv_buf_init(char* base, unsigned int len);
573 #define UV_STREAM_FIELDS \
574 /* number of bytes queued for writing */ \
575 size_t write_queue_size; \
576 uv_alloc_cb alloc_cb; \
577 uv_read_cb read_cb; \
578 uv_read2_cb read2_cb; \
580 UV_STREAM_PRIVATE_FIELDS
583 * uv_stream_t is a subclass of uv_handle_t
585 * uv_stream is an abstract class.
587 * uv_stream_t is the parent class of uv_tcp_t, uv_pipe_t, uv_tty_t, and
595 UV_EXTERN int uv_listen(uv_stream_t* stream, int backlog, uv_connection_cb cb);
598 * This call is used in conjunction with uv_listen() to accept incoming
599 * connections. Call uv_accept after receiving a uv_connection_cb to accept
600 * the connection. Before calling uv_accept use uv_*_init() must be
601 * called on the client. Non-zero return value indicates an error.
603 * When the uv_connection_cb is called it is guaranteed that uv_accept will
604 * complete successfully the first time. If you attempt to use it more than
605 * once, it may fail. It is suggested to only call uv_accept once per
606 * uv_connection_cb call.
608 UV_EXTERN int uv_accept(uv_stream_t* server, uv_stream_t* client);
611 * Read data from an incoming stream. The callback will be made several
612 * times until there is no more data to read or uv_read_stop is called.
613 * When we've reached EOF nread will be set to UV_EOF.
615 * When nread < 0, the buf parameter might not point to a valid buffer;
616 * in that case buf.len and buf.base are both set to 0.
618 * Note that nread might also be 0, which does *not* indicate an error or
619 * eof; it happens when libuv requested a buffer through the alloc callback
620 * but then decided that it didn't need that buffer.
622 UV_EXTERN int uv_read_start(uv_stream_t*, uv_alloc_cb alloc_cb,
625 UV_EXTERN int uv_read_stop(uv_stream_t*);
628 * Extended read methods for receiving handles over a pipe. The pipe must be
629 * initialized with ipc == 1.
631 UV_EXTERN int uv_read2_start(uv_stream_t*, uv_alloc_cb alloc_cb,
632 uv_read2_cb read_cb);
636 * Write data to stream. Buffers are written in order. Example:
639 * { .base = "1", .len = 1 },
640 * { .base = "2", .len = 1 }
644 * { .base = "3", .len = 1 },
645 * { .base = "4", .len = 1 }
652 * uv_write(&req1, stream, a, 2);
653 * uv_write(&req2, stream, b, 2);
656 UV_EXTERN int uv_write(uv_write_t* req,
658 const uv_buf_t bufs[],
663 * Extended write function for sending handles over a pipe. The pipe must be
664 * initialized with ipc == 1.
665 * send_handle must be a TCP socket or pipe, which is a server or a connection
666 * (listening or connected state). Bound sockets or pipes will be assumed to
669 UV_EXTERN int uv_write2(uv_write_t* req,
671 const uv_buf_t bufs[],
673 uv_stream_t* send_handle,
677 * Same as `uv_write()`, but won't queue write request if it can't be completed
679 * Will return either:
680 * - positive number of bytes written
681 * - zero - if queued write is needed
682 * - negative error code
684 UV_EXTERN int uv_try_write(uv_stream_t* handle, const char* buf, size_t length);
686 /* uv_write_t is a subclass of uv_req_t */
690 uv_stream_t* send_handle;
692 UV_WRITE_PRIVATE_FIELDS
697 * Used to determine whether a stream is readable or writable.
699 UV_EXTERN int uv_is_readable(const uv_stream_t* handle);
700 UV_EXTERN int uv_is_writable(const uv_stream_t* handle);
704 * Enable or disable blocking mode for a stream.
706 * When blocking mode is enabled all writes complete synchronously. The
707 * interface remains unchanged otherwise, e.g. completion or failure of the
708 * operation will still be reported through a callback which is made
711 * Relying too much on this API is not recommended. It is likely to change
712 * significantly in the future.
714 * On windows this currently works only for uv_pipe_t instances. On unix it
715 * works for tcp, pipe and tty instances. Be aware that changing the blocking
716 * mode on unix sets or clears the O_NONBLOCK bit. If you are sharing a handle
717 * with another process, the other process is affected by the change too,
718 * which can lead to unexpected results.
720 * Also libuv currently makes no ordering guarantee when the blocking mode
721 * is changed after write requests have already been submitted. Therefore it is
722 * recommended to set the blocking mode immediately after opening or creating
725 UV_EXTERN int uv_stream_set_blocking(uv_stream_t* handle, int blocking);
729 * Used to determine whether a stream is closing or closed.
731 * N.B. is only valid between the initialization of the handle
732 * and the arrival of the close callback, and cannot be used
733 * to validate the handle.
735 UV_EXTERN int uv_is_closing(const uv_handle_t* handle);
739 * uv_tcp_t is a subclass of uv_stream_t
741 * Represents a TCP stream or TCP server.
746 UV_TCP_PRIVATE_FIELDS
749 UV_EXTERN int uv_tcp_init(uv_loop_t*, uv_tcp_t* handle);
752 * Opens an existing file descriptor or SOCKET as a tcp handle.
754 UV_EXTERN int uv_tcp_open(uv_tcp_t* handle, uv_os_sock_t sock);
756 /* Enable/disable Nagle's algorithm. */
757 UV_EXTERN int uv_tcp_nodelay(uv_tcp_t* handle, int enable);
760 * Enable/disable TCP keep-alive.
762 * `delay` is the initial delay in seconds, ignored when `enable` is zero.
764 UV_EXTERN int uv_tcp_keepalive(uv_tcp_t* handle,
769 * Enable/disable simultaneous asynchronous accept requests that are
770 * queued by the operating system when listening for new tcp connections.
771 * This setting is used to tune a tcp server for the desired performance.
772 * Having simultaneous accepts can significantly improve the rate of
773 * accepting connections (which is why it is enabled by default) but
774 * may lead to uneven load distribution in multi-process setups.
776 UV_EXTERN int uv_tcp_simultaneous_accepts(uv_tcp_t* handle, int enable);
779 * Bind the handle to an address and port. `addr` should point to an
780 * initialized struct sockaddr_in or struct sockaddr_in6.
782 * When the port is already taken, you can expect to see an UV_EADDRINUSE
783 * error from either uv_tcp_bind(), uv_listen() or uv_tcp_connect().
785 * That is, a successful call to uv_tcp_bind() does not guarantee that
786 * the call to uv_listen() or uv_tcp_connect() will succeed as well.
788 UV_EXTERN int uv_tcp_bind(uv_tcp_t* handle, const struct sockaddr* addr);
790 UV_EXTERN int uv_tcp_getsockname(uv_tcp_t* handle, struct sockaddr* name,
792 UV_EXTERN int uv_tcp_getpeername(uv_tcp_t* handle, struct sockaddr* name,
796 * Establish an IPv4 or IPv6 TCP connection. Provide an initialized TCP handle
797 * and an uninitialized uv_connect_t*. `addr` should point to an initialized
798 * struct sockaddr_in or struct sockaddr_in6.
800 * The callback is made when the connection has been established or when a
801 * connection error happened.
803 UV_EXTERN int uv_tcp_connect(uv_connect_t* req,
805 const struct sockaddr* addr,
808 /* uv_connect_t is a subclass of uv_req_t */
809 struct uv_connect_s {
813 UV_CONNECT_PRIVATE_FIELDS
822 /* Disables dual stack mode. */
825 * Indicates message was truncated because read buffer was too small. The
826 * remainder was discarded by the OS. Used in uv_udp_recv_cb.
832 * Called after a uv_udp_send() or uv_udp_send6(). status 0 indicates
833 * success otherwise error.
835 typedef void (*uv_udp_send_cb)(uv_udp_send_t* req, int status);
838 * Callback that is invoked when a new UDP datagram is received.
841 * nread Number of bytes that have been received.
842 * 0 if there is no more data to read. You may
843 * discard or repurpose the read buffer.
844 * < 0 if a transmission error was detected.
845 * buf uv_buf_t with the received data.
846 * addr struct sockaddr_in or struct sockaddr_in6.
847 * Valid for the duration of the callback only.
848 * flags One or more OR'ed UV_UDP_* constants.
849 * Right now only UV_UDP_PARTIAL is used.
851 typedef void (*uv_udp_recv_cb)(uv_udp_t* handle,
854 const struct sockaddr* addr,
857 /* uv_udp_t is a subclass of uv_handle_t */
860 UV_UDP_PRIVATE_FIELDS
863 /* uv_udp_send_t is a subclass of uv_req_t */
864 struct uv_udp_send_s {
868 UV_UDP_SEND_PRIVATE_FIELDS
872 * Initialize a new UDP handle. The actual socket is created lazily.
873 * Returns 0 on success.
875 UV_EXTERN int uv_udp_init(uv_loop_t*, uv_udp_t* handle);
878 * Opens an existing file descriptor or SOCKET as a udp handle.
881 * The only requirement of the sock argument is that it follows the
882 * datagram contract (works in unconnected mode, supports sendmsg()/recvmsg(),
883 * etc.). In other words, other datagram-type sockets like raw sockets or
884 * netlink sockets can also be passed to this function.
886 * This sets the SO_REUSEPORT socket flag on the BSDs and OS X. On other
887 * UNIX platforms, it sets the SO_REUSEADDR flag. What that means is that
888 * multiple threads or processes can bind to the same address without error
889 * (provided they all set the flag) but only the last one to bind will receive
890 * any traffic, in effect "stealing" the port from the previous listener.
891 * This behavior is something of an anomaly and may be replaced by an explicit
892 * opt-in mechanism in future versions of libuv.
894 UV_EXTERN int uv_udp_open(uv_udp_t* handle, uv_os_sock_t sock);
897 * Bind to a IPv4 address and port.
900 * handle UDP handle. Should have been initialized with `uv_udp_init`.
901 * addr struct sockaddr_in or struct sockaddr_in6 with the address and
906 * 0 on success, or an error code < 0 on failure.
908 * This sets the SO_REUSEPORT socket flag on the BSDs and OS X. On other
909 * UNIX platforms, it sets the SO_REUSEADDR flag. What that means is that
910 * multiple threads or processes can bind to the same address without error
911 * (provided they all set the flag) but only the last one to bind will receive
912 * any traffic, in effect "stealing" the port from the previous listener.
913 * This behavior is something of an anomaly and may be replaced by an explicit
914 * opt-in mechanism in future versions of libuv.
916 UV_EXTERN int uv_udp_bind(uv_udp_t* handle,
917 const struct sockaddr* addr,
920 UV_EXTERN int uv_udp_getsockname(uv_udp_t* handle, struct sockaddr* name,
924 * Set membership for a multicast address
927 * handle UDP handle. Should have been initialized with
929 * multicast_addr multicast address to set membership for
930 * interface_addr interface address
931 * membership Should be UV_JOIN_GROUP or UV_LEAVE_GROUP
934 * 0 on success, or an error code < 0 on failure.
936 UV_EXTERN int uv_udp_set_membership(uv_udp_t* handle,
937 const char* multicast_addr, const char* interface_addr,
938 uv_membership membership);
941 * Set IP multicast loop flag. Makes multicast packets loop back to
945 * handle UDP handle. Should have been initialized with
947 * on 1 for on, 0 for off
950 * 0 on success, or an error code < 0 on failure.
952 UV_EXTERN int uv_udp_set_multicast_loop(uv_udp_t* handle, int on);
955 * Set the multicast ttl
958 * handle UDP handle. Should have been initialized with
963 * 0 on success, or an error code < 0 on failure.
965 UV_EXTERN int uv_udp_set_multicast_ttl(uv_udp_t* handle, int ttl);
968 * Set broadcast on or off
971 * handle UDP handle. Should have been initialized with
973 * on 1 for on, 0 for off
976 * 0 on success, or an error code < 0 on failure.
978 UV_EXTERN int uv_udp_set_broadcast(uv_udp_t* handle, int on);
981 * Set the time to live
984 * handle UDP handle. Should have been initialized with
989 * 0 on success, or an error code < 0 on failure.
991 UV_EXTERN int uv_udp_set_ttl(uv_udp_t* handle, int ttl);
994 * Send data. If the socket has not previously been bound with `uv_udp_bind`
995 * or `uv_udp_bind6`, it is bound to 0.0.0.0 (the "all interfaces" address)
996 * and a random port number.
999 * req UDP request handle. Need not be initialized.
1000 * handle UDP handle. Should have been initialized with `uv_udp_init`.
1001 * bufs List of buffers to send.
1002 * nbufs Number of buffers in `bufs`.
1003 * addr Address of the remote peer. See `uv_ip4_addr`.
1004 * send_cb Callback to invoke when the data has been sent out.
1007 * 0 on success, or an error code < 0 on failure.
1009 UV_EXTERN int uv_udp_send(uv_udp_send_t* req,
1011 const uv_buf_t bufs[],
1013 const struct sockaddr* addr,
1014 uv_udp_send_cb send_cb);
1017 * Receive data. If the socket has not previously been bound with `uv_udp_bind`
1018 * or `uv_udp_bind6`, it is bound to 0.0.0.0 (the "all interfaces" address)
1019 * and a random port number.
1022 * handle UDP handle. Should have been initialized with `uv_udp_init`.
1023 * alloc_cb Callback to invoke when temporary storage is needed.
1024 * recv_cb Callback to invoke with received data.
1027 * 0 on success, or an error code < 0 on failure.
1029 UV_EXTERN int uv_udp_recv_start(uv_udp_t* handle, uv_alloc_cb alloc_cb,
1030 uv_udp_recv_cb recv_cb);
1033 * Stop listening for incoming datagrams.
1036 * handle UDP handle. Should have been initialized with `uv_udp_init`.
1039 * 0 on success, or an error code < 0 on failure.
1041 UV_EXTERN int uv_udp_recv_stop(uv_udp_t* handle);
1045 * uv_tty_t is a subclass of uv_stream_t
1047 * Representing a stream for the console.
1052 UV_TTY_PRIVATE_FIELDS
1056 * Initialize a new TTY stream with the given file descriptor. Usually the
1057 * file descriptor will be
1061 * The last argument, readable, specifies if you plan on calling
1062 * uv_read_start with this stream. stdin is readable, stdout is not.
1064 * TTY streams which are not readable have blocking writes.
1066 UV_EXTERN int uv_tty_init(uv_loop_t*, uv_tty_t*, uv_file fd, int readable);
1069 * Set mode. 0 for normal, 1 for raw.
1071 UV_EXTERN int uv_tty_set_mode(uv_tty_t*, int mode);
1074 * To be called when the program exits. Resets TTY settings to default
1075 * values for the next process to take over.
1077 * This function is async signal-safe on UNIX platforms but can fail with error
1078 * code UV_EBUSY if you call it when execution is inside uv_tty_set_mode().
1080 UV_EXTERN int uv_tty_reset_mode(void);
1083 * Gets the current Window size. On success zero is returned.
1085 UV_EXTERN int uv_tty_get_winsize(uv_tty_t*, int* width, int* height);
1088 * Used to detect what type of stream should be used with a given file
1089 * descriptor. Usually this will be used during initialization to guess the
1090 * type of the stdio streams.
1091 * For isatty() functionality use this function and test for UV_TTY.
1093 UV_EXTERN uv_handle_type uv_guess_handle(uv_file file);
1096 * uv_pipe_t is a subclass of uv_stream_t
1098 * Representing a pipe stream or pipe server. On Windows this is a Named
1099 * Pipe. On Unix this is a UNIX domain socket.
1104 int ipc; /* non-zero if this pipe is used for passing handles */
1105 UV_PIPE_PRIVATE_FIELDS
1109 * Initialize a pipe. The last argument is a boolean to indicate if
1110 * this pipe will be used for handle passing between processes.
1112 UV_EXTERN int uv_pipe_init(uv_loop_t*, uv_pipe_t* handle, int ipc);
1115 * Opens an existing file descriptor or HANDLE as a pipe.
1117 UV_EXTERN int uv_pipe_open(uv_pipe_t*, uv_file file);
1120 * Bind the pipe to a file path (UNIX) or a name (Windows.)
1122 * Paths on UNIX get truncated to `sizeof(sockaddr_un.sun_path)` bytes,
1123 * typically between 92 and 108 bytes.
1125 UV_EXTERN int uv_pipe_bind(uv_pipe_t* handle, const char* name);
1128 * Connect to the UNIX domain socket or the named pipe.
1130 * Paths on UNIX get truncated to `sizeof(sockaddr_un.sun_path)` bytes,
1131 * typically between 92 and 108 bytes.
1133 UV_EXTERN void uv_pipe_connect(uv_connect_t* req, uv_pipe_t* handle,
1134 const char* name, uv_connect_cb cb);
1137 * This setting applies to Windows only.
1138 * Set the number of pending pipe instance handles when the pipe server
1139 * is waiting for connections.
1141 UV_EXTERN void uv_pipe_pending_instances(uv_pipe_t* handle, int count);
1145 * uv_poll_t is a subclass of uv_handle_t.
1147 * The uv_poll watcher is used to watch file descriptors for readability and
1148 * writability, similar to the purpose of poll(2).
1150 * The purpose of uv_poll is to enable integrating external libraries that
1151 * rely on the event loop to signal it about the socket status changes, like
1152 * c-ares or libssh2. Using uv_poll_t for any other other purpose is not
1153 * recommended; uv_tcp_t, uv_udp_t, etc. provide an implementation that is
1154 * much faster and more scalable than what can be achieved with uv_poll_t,
1155 * especially on Windows.
1157 * It is possible that uv_poll occasionally signals that a file descriptor is
1158 * readable or writable even when it isn't. The user should therefore always
1159 * be prepared to handle EAGAIN or equivalent when it attempts to read from or
1162 * It is not okay to have multiple active uv_poll watchers for the same socket.
1163 * This can cause libuv to busyloop or otherwise malfunction.
1165 * The user should not close a file descriptor while it is being polled by an
1166 * active uv_poll watcher. This can cause the poll watcher to report an error,
1167 * but it might also start polling another socket. However the fd can be safely
1168 * closed immediately after a call to uv_poll_stop() or uv_close().
1170 * On windows only sockets can be polled with uv_poll. On unix any file
1171 * descriptor that would be accepted by poll(2) can be used with uv_poll.
1176 UV_POLL_PRIVATE_FIELDS
1179 enum uv_poll_event {
1184 /* Initialize the poll watcher using a file descriptor. */
1185 UV_EXTERN int uv_poll_init(uv_loop_t* loop, uv_poll_t* handle, int fd);
1187 /* Initialize the poll watcher using a socket descriptor. On unix this is */
1188 /* identical to uv_poll_init. On windows it takes a SOCKET handle. */
1189 UV_EXTERN int uv_poll_init_socket(uv_loop_t* loop, uv_poll_t* handle,
1190 uv_os_sock_t socket);
1193 * Starts polling the file descriptor. `events` is a bitmask consisting made up
1194 * of UV_READABLE and UV_WRITABLE. As soon as an event is detected the callback
1195 * will be called with `status` set to 0, and the detected events set en the
1198 * If an error happens while polling status, `status` < 0 and corresponds
1199 * with one of the UV_E* error codes. The user should not close the socket
1200 * while uv_poll is active. If the user does that anyway, the callback *may*
1201 * be called reporting an error status, but this is not guaranteed.
1203 * Calling uv_poll_start on an uv_poll watcher that is already active is fine.
1204 * Doing so will update the events mask that is being watched for.
1206 UV_EXTERN int uv_poll_start(uv_poll_t* handle, int events, uv_poll_cb cb);
1208 /* Stops polling the file descriptor. */
1209 UV_EXTERN int uv_poll_stop(uv_poll_t* handle);
1213 * uv_prepare_t is a subclass of uv_handle_t.
1215 * Every active prepare handle gets its callback called exactly once per loop
1216 * iteration, just before the system blocks to wait for completed i/o.
1218 struct uv_prepare_s {
1220 UV_PREPARE_PRIVATE_FIELDS
1223 UV_EXTERN int uv_prepare_init(uv_loop_t*, uv_prepare_t* prepare);
1225 UV_EXTERN int uv_prepare_start(uv_prepare_t* prepare, uv_prepare_cb cb);
1227 UV_EXTERN int uv_prepare_stop(uv_prepare_t* prepare);
1231 * uv_check_t is a subclass of uv_handle_t.
1233 * Every active check handle gets its callback called exactly once per loop
1234 * iteration, just after the system returns from blocking.
1238 UV_CHECK_PRIVATE_FIELDS
1241 UV_EXTERN int uv_check_init(uv_loop_t*, uv_check_t* check);
1243 UV_EXTERN int uv_check_start(uv_check_t* check, uv_check_cb cb);
1245 UV_EXTERN int uv_check_stop(uv_check_t* check);
1249 * uv_idle_t is a subclass of uv_handle_t.
1251 * Every active idle handle gets its callback called repeatedly until it is
1252 * stopped. This happens after all other types of callbacks are processed.
1253 * When there are multiple "idle" handles active, their callbacks are called
1258 UV_IDLE_PRIVATE_FIELDS
1261 UV_EXTERN int uv_idle_init(uv_loop_t*, uv_idle_t* idle);
1263 UV_EXTERN int uv_idle_start(uv_idle_t* idle, uv_idle_cb cb);
1265 UV_EXTERN int uv_idle_stop(uv_idle_t* idle);
1269 * uv_async_t is a subclass of uv_handle_t.
1271 * uv_async_send wakes up the event loop and calls the async handle's callback.
1272 * There is no guarantee that every uv_async_send call leads to exactly one
1273 * invocation of the callback; the only guarantee is that the callback function
1274 * is called at least once after the call to async_send. Unlike all other
1275 * libuv functions, uv_async_send can be called from another thread.
1279 UV_ASYNC_PRIVATE_FIELDS
1283 * Initialize the uv_async_t handle. A NULL callback is allowed.
1285 * Note that uv_async_init(), unlike other libuv functions, immediately
1286 * starts the handle. To stop the handle again, close it with uv_close().
1288 UV_EXTERN int uv_async_init(uv_loop_t*, uv_async_t* async,
1289 uv_async_cb async_cb);
1292 * This can be called from other threads to wake up a libuv thread.
1294 * libuv is single threaded at the moment.
1296 UV_EXTERN int uv_async_send(uv_async_t* async);
1300 * uv_timer_t is a subclass of uv_handle_t.
1302 * Used to get woken up at a specified time in the future.
1306 UV_TIMER_PRIVATE_FIELDS
1309 UV_EXTERN int uv_timer_init(uv_loop_t*, uv_timer_t* handle);
1312 * Start the timer. `timeout` and `repeat` are in milliseconds.
1314 * If timeout is zero, the callback fires on the next tick of the event loop.
1316 * If repeat is non-zero, the callback fires first after timeout milliseconds
1317 * and then repeatedly after repeat milliseconds.
1319 UV_EXTERN int uv_timer_start(uv_timer_t* handle,
1324 UV_EXTERN int uv_timer_stop(uv_timer_t* handle);
1327 * Stop the timer, and if it is repeating restart it using the repeat value
1328 * as the timeout. If the timer has never been started before it returns
1331 UV_EXTERN int uv_timer_again(uv_timer_t* handle);
1334 * Set the repeat value in milliseconds. Note that if the repeat value is set
1335 * from a timer callback it does not immediately take effect. If the timer was
1336 * non-repeating before, it will have been stopped. If it was repeating, then
1337 * the old repeat value will have been used to schedule the next timeout.
1339 UV_EXTERN void uv_timer_set_repeat(uv_timer_t* handle, uint64_t repeat);
1341 UV_EXTERN uint64_t uv_timer_get_repeat(const uv_timer_t* handle);
1345 * uv_getaddrinfo_t is a subclass of uv_req_t
1347 * Request object for uv_getaddrinfo.
1349 struct uv_getaddrinfo_s {
1353 UV_GETADDRINFO_PRIVATE_FIELDS
1358 * Asynchronous getaddrinfo(3).
1360 * Either node or service may be NULL but not both.
1362 * hints is a pointer to a struct addrinfo with additional address type
1363 * constraints, or NULL. Consult `man -s 3 getaddrinfo` for details.
1365 * Returns 0 on success or an error code < 0 on failure.
1367 * If successful, your callback gets called sometime in the future with the
1368 * lookup result, which is either:
1370 * a) err == 0, the res argument points to a valid struct addrinfo, or
1371 * b) err < 0, the res argument is NULL. See the UV_EAI_* constants.
1373 * Call uv_freeaddrinfo() to free the addrinfo structure.
1375 UV_EXTERN int uv_getaddrinfo(uv_loop_t* loop,
1376 uv_getaddrinfo_t* req,
1377 uv_getaddrinfo_cb getaddrinfo_cb,
1379 const char* service,
1380 const struct addrinfo* hints);
1383 * Free the struct addrinfo. Passing NULL is allowed and is a no-op.
1385 UV_EXTERN void uv_freeaddrinfo(struct addrinfo* ai);
1388 /* uv_spawn() options */
1391 UV_CREATE_PIPE = 0x01,
1392 UV_INHERIT_FD = 0x02,
1393 UV_INHERIT_STREAM = 0x04,
1395 /* When UV_CREATE_PIPE is specified, UV_READABLE_PIPE and UV_WRITABLE_PIPE
1396 * determine the direction of flow, from the child process' perspective. Both
1397 * flags may be specified to create a duplex data stream.
1399 UV_READABLE_PIPE = 0x10,
1400 UV_WRITABLE_PIPE = 0x20
1403 typedef struct uv_stdio_container_s {
1404 uv_stdio_flags flags;
1407 uv_stream_t* stream;
1410 } uv_stdio_container_t;
1412 typedef struct uv_process_options_s {
1413 uv_exit_cb exit_cb; /* Called after the process exits. */
1414 const char* file; /* Path to program to execute. */
1416 * Command line arguments. args[0] should be the path to the program. On
1417 * Windows this uses CreateProcess which concatenates the arguments into a
1418 * string this can cause some strange errors. See the note at
1419 * windows_verbatim_arguments.
1423 * This will be set as the environ variable in the subprocess. If this is
1424 * NULL then the parents environ will be used.
1428 * If non-null this represents a directory the subprocess should execute
1429 * in. Stands for current working directory.
1433 * Various flags that control how uv_spawn() behaves. See the definition of
1434 * `enum uv_process_flags` below.
1438 * The `stdio` field points to an array of uv_stdio_container_t structs that
1439 * describe the file descriptors that will be made available to the child
1440 * process. The convention is that stdio[0] points to stdin, fd 1 is used for
1441 * stdout, and fd 2 is stderr.
1443 * Note that on windows file descriptors greater than 2 are available to the
1444 * child process only if the child processes uses the MSVCRT runtime.
1447 uv_stdio_container_t* stdio;
1449 * Libuv can change the child process' user/group id. This happens only when
1450 * the appropriate bits are set in the flags fields. This is not supported on
1451 * windows; uv_spawn() will fail and set the error to UV_ENOTSUP.
1455 } uv_process_options_t;
1458 * These are the flags that can be used for the uv_process_options.flags field.
1460 enum uv_process_flags {
1462 * Set the child process' user id. The user id is supplied in the `uid` field
1463 * of the options struct. This does not work on windows; setting this flag
1464 * will cause uv_spawn() to fail.
1466 UV_PROCESS_SETUID = (1 << 0),
1468 * Set the child process' group id. The user id is supplied in the `gid`
1469 * field of the options struct. This does not work on windows; setting this
1470 * flag will cause uv_spawn() to fail.
1472 UV_PROCESS_SETGID = (1 << 1),
1474 * Do not wrap any arguments in quotes, or perform any other escaping, when
1475 * converting the argument list into a command line string. This option is
1476 * only meaningful on Windows systems. On unix it is silently ignored.
1478 UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS = (1 << 2),
1480 * Spawn the child process in a detached state - this will make it a process
1481 * group leader, and will effectively enable the child to keep running after
1482 * the parent exits. Note that the child process will still keep the
1483 * parent's event loop alive unless the parent process calls uv_unref() on
1484 * the child's process handle.
1486 UV_PROCESS_DETACHED = (1 << 3),
1488 * Hide the subprocess console window that would normally be created. This
1489 * option is only meaningful on Windows systems. On unix it is silently
1492 UV_PROCESS_WINDOWS_HIDE = (1 << 4)
1496 * uv_process_t is a subclass of uv_handle_t
1498 struct uv_process_s {
1502 UV_PROCESS_PRIVATE_FIELDS
1506 * Initializes the uv_process_t and starts the process. If the process is
1507 * successfully spawned, then this function will return 0. Otherwise, the
1508 * negative error code corresponding to the reason it couldn't spawn is
1511 * Possible reasons for failing to spawn would include (but not be limited to)
1512 * the file to execute not existing, not having permissions to use the setuid or
1513 * setgid specified, or not having enough memory to allocate for the new
1516 UV_EXTERN int uv_spawn(uv_loop_t* loop,
1517 uv_process_t* handle,
1518 const uv_process_options_t* options);
1522 * Kills the process with the specified signal. The user must still
1523 * call uv_close on the process.
1525 UV_EXTERN int uv_process_kill(uv_process_t*, int signum);
1528 /* Kills the process with the specified signal. */
1529 UV_EXTERN int uv_kill(int pid, int signum);
1533 * uv_work_t is a subclass of uv_req_t
1539 uv_after_work_cb after_work_cb;
1540 UV_WORK_PRIVATE_FIELDS
1543 /* Queues a work request to execute asynchronously on the thread pool. */
1544 UV_EXTERN int uv_queue_work(uv_loop_t* loop, uv_work_t* req,
1545 uv_work_cb work_cb, uv_after_work_cb after_work_cb);
1547 /* Cancel a pending request. Fails if the request is executing or has finished
1550 * Returns 0 on success, or an error code < 0 on failure.
1552 * Only cancellation of uv_fs_t, uv_getaddrinfo_t and uv_work_t requests is
1553 * currently supported.
1555 * Cancelled requests have their callbacks invoked some time in the future.
1556 * It's _not_ safe to free the memory associated with the request until your
1557 * callback is called.
1559 * Here is how cancellation is reported to your callback:
1561 * - A uv_fs_t request has its req->result field set to UV_ECANCELED.
1563 * - A uv_work_t or uv_getaddrinfo_t request has its callback invoked with
1564 * status == UV_ECANCELED.
1566 * This function is currently only implemented on UNIX platforms. On Windows,
1567 * it always returns UV_ENOSYS.
1569 UV_EXTERN int uv_cancel(uv_req_t* req);
1572 struct uv_cpu_info_s {
1575 struct uv_cpu_times_s {
1584 struct uv_interface_address_s {
1589 struct sockaddr_in address4;
1590 struct sockaddr_in6 address6;
1593 struct sockaddr_in netmask4;
1594 struct sockaddr_in6 netmask6;
1598 UV_EXTERN char** uv_setup_args(int argc, char** argv);
1599 UV_EXTERN int uv_get_process_title(char* buffer, size_t size);
1600 UV_EXTERN int uv_set_process_title(const char* title);
1601 UV_EXTERN int uv_resident_set_memory(size_t* rss);
1602 UV_EXTERN int uv_uptime(double* uptime);
1605 * This allocates cpu_infos array, and sets count. The array
1606 * is freed using uv_free_cpu_info().
1608 UV_EXTERN int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count);
1609 UV_EXTERN void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count);
1612 * This allocates addresses array, and sets count. The array
1613 * is freed using uv_free_interface_addresses().
1615 UV_EXTERN int uv_interface_addresses(uv_interface_address_t** addresses,
1617 UV_EXTERN void uv_free_interface_addresses(uv_interface_address_t* addresses,
1621 * File System Methods.
1623 * The uv_fs_* functions execute a blocking system call asynchronously (in a
1624 * thread pool) and call the specified callback in the specified loop after
1625 * completion. If the user gives NULL as the callback the blocking system
1626 * call will be called synchronously. req should be a pointer to an
1627 * uninitialized uv_fs_t object.
1629 * uv_fs_req_cleanup() must be called after completion of the uv_fs_
1630 * function to free any internal memory allocations associated with the
1664 /* uv_fs_t is a subclass of uv_req_t */
1673 uv_stat_t statbuf; /* Stores the result of uv_fs_stat and uv_fs_fstat. */
1674 UV_FS_PRIVATE_FIELDS
1677 UV_EXTERN void uv_fs_req_cleanup(uv_fs_t* req);
1679 UV_EXTERN int uv_fs_close(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1682 UV_EXTERN int uv_fs_open(uv_loop_t* loop, uv_fs_t* req, const char* path,
1683 int flags, int mode, uv_fs_cb cb);
1685 UV_EXTERN int uv_fs_read(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1686 void* buf, size_t length, int64_t offset, uv_fs_cb cb);
1688 UV_EXTERN int uv_fs_unlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
1691 UV_EXTERN int uv_fs_write(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1692 const void* buf, size_t length, int64_t offset, uv_fs_cb cb);
1694 UV_EXTERN int uv_fs_mkdir(uv_loop_t* loop, uv_fs_t* req, const char* path,
1695 int mode, uv_fs_cb cb);
1697 UV_EXTERN int uv_fs_rmdir(uv_loop_t* loop, uv_fs_t* req, const char* path,
1700 UV_EXTERN int uv_fs_readdir(uv_loop_t* loop, uv_fs_t* req,
1701 const char* path, int flags, uv_fs_cb cb);
1703 UV_EXTERN int uv_fs_stat(uv_loop_t* loop, uv_fs_t* req, const char* path,
1706 UV_EXTERN int uv_fs_fstat(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1709 UV_EXTERN int uv_fs_rename(uv_loop_t* loop, uv_fs_t* req, const char* path,
1710 const char* new_path, uv_fs_cb cb);
1712 UV_EXTERN int uv_fs_fsync(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1715 UV_EXTERN int uv_fs_fdatasync(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1718 UV_EXTERN int uv_fs_ftruncate(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1719 int64_t offset, uv_fs_cb cb);
1721 UV_EXTERN int uv_fs_sendfile(uv_loop_t* loop, uv_fs_t* req, uv_file out_fd,
1722 uv_file in_fd, int64_t in_offset, size_t length, uv_fs_cb cb);
1724 UV_EXTERN int uv_fs_chmod(uv_loop_t* loop, uv_fs_t* req, const char* path,
1725 int mode, uv_fs_cb cb);
1727 UV_EXTERN int uv_fs_utime(uv_loop_t* loop, uv_fs_t* req, const char* path,
1728 double atime, double mtime, uv_fs_cb cb);
1730 UV_EXTERN int uv_fs_futime(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1731 double atime, double mtime, uv_fs_cb cb);
1733 UV_EXTERN int uv_fs_lstat(uv_loop_t* loop, uv_fs_t* req, const char* path,
1736 UV_EXTERN int uv_fs_link(uv_loop_t* loop, uv_fs_t* req, const char* path,
1737 const char* new_path, uv_fs_cb cb);
1740 * This flag can be used with uv_fs_symlink on Windows
1741 * to specify whether path argument points to a directory.
1743 #define UV_FS_SYMLINK_DIR 0x0001
1746 * This flag can be used with uv_fs_symlink on Windows
1747 * to specify whether the symlink is to be created using junction points.
1749 #define UV_FS_SYMLINK_JUNCTION 0x0002
1751 UV_EXTERN int uv_fs_symlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
1752 const char* new_path, int flags, uv_fs_cb cb);
1754 UV_EXTERN int uv_fs_readlink(uv_loop_t* loop, uv_fs_t* req, const char* path,
1757 UV_EXTERN int uv_fs_fchmod(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1758 int mode, uv_fs_cb cb);
1760 UV_EXTERN int uv_fs_chown(uv_loop_t* loop, uv_fs_t* req, const char* path,
1761 uv_uid_t uid, uv_gid_t gid, uv_fs_cb cb);
1763 UV_EXTERN int uv_fs_fchown(uv_loop_t* loop, uv_fs_t* req, uv_file file,
1764 uv_uid_t uid, uv_gid_t gid, uv_fs_cb cb);
1773 struct uv_fs_event_s {
1776 UV_FS_EVENT_PRIVATE_FIELDS
1781 * uv_fs_stat() based polling file watcher.
1783 struct uv_fs_poll_s {
1785 /* Private, don't touch. */
1789 UV_EXTERN int uv_fs_poll_init(uv_loop_t* loop, uv_fs_poll_t* handle);
1792 * Check the file at `path` for changes every `interval` milliseconds.
1794 * Your callback is invoked with `status < 0` if `path` does not exist
1795 * or is inaccessible. The watcher is *not* stopped but your callback is
1796 * not called again until something changes (e.g. when the file is created
1797 * or the error reason changes).
1799 * When `status == 0`, your callback receives pointers to the old and new
1800 * `uv_stat_t` structs. They are valid for the duration of the callback
1803 * For maximum portability, use multi-second intervals. Sub-second intervals
1804 * will not detect all changes on many file systems.
1806 UV_EXTERN int uv_fs_poll_start(uv_fs_poll_t* handle,
1807 uv_fs_poll_cb poll_cb,
1809 unsigned int interval);
1811 UV_EXTERN int uv_fs_poll_stop(uv_fs_poll_t* handle);
1815 * UNIX signal handling on a per-event loop basis. The implementation is not
1816 * ultra efficient so don't go creating a million event loops with a million
1819 * Note to Linux users: SIGRT0 and SIGRT1 (signals 32 and 33) are used by the
1820 * NPTL pthreads library to manage threads. Installing watchers for those
1821 * signals will lead to unpredictable behavior and is strongly discouraged.
1822 * Future versions of libuv may simply reject them.
1824 * Some signal support is available on Windows:
1826 * SIGINT is normally delivered when the user presses CTRL+C. However, like
1827 * on Unix, it is not generated when terminal raw mode is enabled.
1829 * SIGBREAK is delivered when the user pressed CTRL+BREAK.
1831 * SIGHUP is generated when the user closes the console window. On SIGHUP the
1832 * program is given approximately 10 seconds to perform cleanup. After that
1833 * Windows will unconditionally terminate it.
1835 * SIGWINCH is raised whenever libuv detects that the console has been
1836 * resized. SIGWINCH is emulated by libuv when the program uses an uv_tty_t
1837 * handle to write to the console. SIGWINCH may not always be delivered in a
1838 * timely manner; libuv will only detect size changes when the cursor is
1839 * being moved. When a readable uv_tty_handle is used in raw mode, resizing
1840 * the console buffer will also trigger a SIGWINCH signal.
1842 * Watchers for other signals can be successfully created, but these signals
1843 * are never generated. These signals are: SIGILL, SIGABRT, SIGFPE, SIGSEGV,
1844 * SIGTERM and SIGKILL.
1846 * Note that calls to raise() or abort() to programmatically raise a signal are
1847 * not detected by libuv; these will not trigger a signal watcher.
1849 struct uv_signal_s {
1851 uv_signal_cb signal_cb;
1853 UV_SIGNAL_PRIVATE_FIELDS
1856 UV_EXTERN int uv_signal_init(uv_loop_t* loop, uv_signal_t* handle);
1858 UV_EXTERN int uv_signal_start(uv_signal_t* handle,
1859 uv_signal_cb signal_cb,
1862 UV_EXTERN int uv_signal_stop(uv_signal_t* handle);
1866 * Gets load average.
1867 * See: http://en.wikipedia.org/wiki/Load_(computing)
1868 * Returns [0,0,0] on Windows.
1870 UV_EXTERN void uv_loadavg(double avg[3]);
1874 * Flags to be passed to uv_fs_event_start.
1876 enum uv_fs_event_flags {
1878 * By default, if the fs event watcher is given a directory name, we will
1879 * watch for all events in that directory. This flags overrides this behavior
1880 * and makes fs_event report only changes to the directory entry itself. This
1881 * flag does not affect individual files watched.
1882 * This flag is currently not implemented yet on any backend.
1884 UV_FS_EVENT_WATCH_ENTRY = 1,
1887 * By default uv_fs_event will try to use a kernel interface such as inotify
1888 * or kqueue to detect events. This may not work on remote filesystems such
1889 * as NFS mounts. This flag makes fs_event fall back to calling stat() on a
1891 * This flag is currently not implemented yet on any backend.
1893 UV_FS_EVENT_STAT = 2,
1896 * By default, event watcher, when watching directory, is not registering
1897 * (is ignoring) changes in it's subdirectories.
1898 * This flag will override this behaviour on platforms that support it.
1900 UV_FS_EVENT_RECURSIVE = 4
1904 UV_EXTERN int uv_fs_event_init(uv_loop_t* loop, uv_fs_event_t* handle);
1906 UV_EXTERN int uv_fs_event_start(uv_fs_event_t* handle,
1908 const char* filename,
1909 unsigned int flags);
1911 UV_EXTERN int uv_fs_event_stop(uv_fs_event_t* handle);
1916 /* Convert string ip addresses to binary structures */
1917 UV_EXTERN int uv_ip4_addr(const char* ip, int port, struct sockaddr_in* addr);
1918 UV_EXTERN int uv_ip6_addr(const char* ip, int port, struct sockaddr_in6* addr);
1920 /* Convert binary addresses to strings */
1921 UV_EXTERN int uv_ip4_name(struct sockaddr_in* src, char* dst, size_t size);
1922 UV_EXTERN int uv_ip6_name(struct sockaddr_in6* src, char* dst, size_t size);
1924 /* Cross-platform IPv6-capable implementation of the 'standard' inet_ntop */
1925 /* and inet_pton functions. On success they return 0. If an error */
1926 /* the target of the `dst` pointer is unmodified. */
1927 UV_EXTERN int uv_inet_ntop(int af, const void* src, char* dst, size_t size);
1928 UV_EXTERN int uv_inet_pton(int af, const char* src, void* dst);
1930 /* Gets the executable path */
1931 UV_EXTERN int uv_exepath(char* buffer, size_t* size);
1933 /* Gets the current working directory */
1934 UV_EXTERN int uv_cwd(char* buffer, size_t size);
1936 /* Changes the current working directory */
1937 UV_EXTERN int uv_chdir(const char* dir);
1939 /* Gets memory info in bytes */
1940 UV_EXTERN uint64_t uv_get_free_memory(void);
1941 UV_EXTERN uint64_t uv_get_total_memory(void);
1944 * Returns the current high-resolution real time. This is expressed in
1945 * nanoseconds. It is relative to an arbitrary time in the past. It is not
1946 * related to the time of day and therefore not subject to clock drift. The
1947 * primary use is for measuring performance between intervals.
1949 * Note not every platform can support nanosecond resolution; however, this
1950 * value will always be in nanoseconds.
1952 UV_EXTERN extern uint64_t uv_hrtime(void);
1956 * Disables inheritance for file descriptors / handles that this process
1957 * inherited from its parent. The effect is that child processes spawned by
1958 * this process don't accidentally inherit these handles.
1960 * It is recommended to call this function as early in your program as possible,
1961 * before the inherited file descriptors can be closed or duplicated.
1963 * Note that this function works on a best-effort basis: there is no guarantee
1964 * that libuv can discover all file descriptors that were inherited. In general
1965 * it does a better job on Windows than it does on unix.
1967 UV_EXTERN void uv_disable_stdio_inheritance(void);
1970 * Opens a shared library. The filename is in utf-8. Returns 0 on success and
1971 * -1 on error. Call `uv_dlerror(uv_lib_t*)` to get the error message.
1973 UV_EXTERN int uv_dlopen(const char* filename, uv_lib_t* lib);
1976 * Close the shared library.
1978 UV_EXTERN void uv_dlclose(uv_lib_t* lib);
1981 * Retrieves a data pointer from a dynamic library. It is legal for a symbol to
1982 * map to NULL. Returns 0 on success and -1 if the symbol was not found.
1984 UV_EXTERN int uv_dlsym(uv_lib_t* lib, const char* name, void** ptr);
1987 * Returns the last uv_dlopen() or uv_dlsym() error message.
1989 UV_EXTERN const char* uv_dlerror(uv_lib_t* lib);
1992 * The mutex functions return 0 on success or an error code < 0
1993 * (unless the return type is void, of course).
1995 UV_EXTERN int uv_mutex_init(uv_mutex_t* handle);
1996 UV_EXTERN void uv_mutex_destroy(uv_mutex_t* handle);
1997 UV_EXTERN void uv_mutex_lock(uv_mutex_t* handle);
1998 UV_EXTERN int uv_mutex_trylock(uv_mutex_t* handle);
1999 UV_EXTERN void uv_mutex_unlock(uv_mutex_t* handle);
2002 * Same goes for the read/write lock functions.
2004 UV_EXTERN int uv_rwlock_init(uv_rwlock_t* rwlock);
2005 UV_EXTERN void uv_rwlock_destroy(uv_rwlock_t* rwlock);
2006 UV_EXTERN void uv_rwlock_rdlock(uv_rwlock_t* rwlock);
2007 UV_EXTERN int uv_rwlock_tryrdlock(uv_rwlock_t* rwlock);
2008 UV_EXTERN void uv_rwlock_rdunlock(uv_rwlock_t* rwlock);
2009 UV_EXTERN void uv_rwlock_wrlock(uv_rwlock_t* rwlock);
2010 UV_EXTERN int uv_rwlock_trywrlock(uv_rwlock_t* rwlock);
2011 UV_EXTERN void uv_rwlock_wrunlock(uv_rwlock_t* rwlock);
2014 * Same goes for the semaphore functions.
2016 UV_EXTERN int uv_sem_init(uv_sem_t* sem, unsigned int value);
2017 UV_EXTERN void uv_sem_destroy(uv_sem_t* sem);
2018 UV_EXTERN void uv_sem_post(uv_sem_t* sem);
2019 UV_EXTERN void uv_sem_wait(uv_sem_t* sem);
2020 UV_EXTERN int uv_sem_trywait(uv_sem_t* sem);
2023 * Same goes for the condition variable functions.
2025 UV_EXTERN int uv_cond_init(uv_cond_t* cond);
2026 UV_EXTERN void uv_cond_destroy(uv_cond_t* cond);
2027 UV_EXTERN void uv_cond_signal(uv_cond_t* cond);
2028 UV_EXTERN void uv_cond_broadcast(uv_cond_t* cond);
2029 /* Waits on a condition variable without a timeout.
2032 * 1. callers should be prepared to deal with spurious wakeups.
2034 UV_EXTERN void uv_cond_wait(uv_cond_t* cond, uv_mutex_t* mutex);
2035 /* Waits on a condition variable with a timeout in nano seconds.
2036 * Returns 0 for success or UV_ETIMEDOUT on timeout, It aborts when other
2040 * 1. callers should be prepared to deal with spurious wakeups.
2041 * 2. the granularity of timeout on Windows is never less than one millisecond.
2042 * 3. uv_cond_timedwait takes a relative timeout, not an absolute time.
2044 UV_EXTERN int uv_cond_timedwait(uv_cond_t* cond, uv_mutex_t* mutex,
2047 UV_EXTERN int uv_barrier_init(uv_barrier_t* barrier, unsigned int count);
2048 UV_EXTERN void uv_barrier_destroy(uv_barrier_t* barrier);
2049 UV_EXTERN void uv_barrier_wait(uv_barrier_t* barrier);
2051 /* Runs a function once and only once. Concurrent calls to uv_once() with the
2052 * same guard will block all callers except one (it's unspecified which one).
2053 * The guard should be initialized statically with the UV_ONCE_INIT macro.
2055 UV_EXTERN void uv_once(uv_once_t* guard, void (*callback)(void));
2057 /* Thread-local storage. These functions largely follow the semantics of
2058 * pthread_key_create(), pthread_key_delete(), pthread_getspecific() and
2059 * pthread_setspecific().
2061 * Note that the total thread-local storage size may be limited.
2062 * That is, it may not be possible to create many TLS keys.
2064 UV_EXTERN int uv_key_create(uv_key_t* key);
2065 UV_EXTERN void uv_key_delete(uv_key_t* key);
2066 UV_EXTERN void* uv_key_get(uv_key_t* key);
2067 UV_EXTERN void uv_key_set(uv_key_t* key, void* value);
2069 UV_EXTERN int uv_thread_create(uv_thread_t *tid,
2070 void (*entry)(void *arg), void *arg);
2071 UV_EXTERN unsigned long uv_thread_self(void);
2072 UV_EXTERN int uv_thread_join(uv_thread_t *tid);
2074 /* The presence of these unions force similar struct layout. */
2075 #define XX(_, name) uv_ ## name ## _t name;
2076 union uv_any_handle {
2077 UV_HANDLE_TYPE_MAP(XX)
2087 /* User data - use this for whatever. */
2089 /* Loop reference counting */
2090 unsigned int active_handles;
2091 void* handle_queue[2];
2092 void* active_reqs[2];
2093 /* Internal flag to signal loop stop */
2094 unsigned int stop_flag;
2095 UV_LOOP_PRIVATE_FIELDS
2099 /* Don't export the private CPP symbols. */
2100 #undef UV_HANDLE_TYPE_PRIVATE
2101 #undef UV_REQ_TYPE_PRIVATE
2102 #undef UV_REQ_PRIVATE_FIELDS
2103 #undef UV_STREAM_PRIVATE_FIELDS
2104 #undef UV_TCP_PRIVATE_FIELDS
2105 #undef UV_PREPARE_PRIVATE_FIELDS
2106 #undef UV_CHECK_PRIVATE_FIELDS
2107 #undef UV_IDLE_PRIVATE_FIELDS
2108 #undef UV_ASYNC_PRIVATE_FIELDS
2109 #undef UV_TIMER_PRIVATE_FIELDS
2110 #undef UV_GETADDRINFO_PRIVATE_FIELDS
2111 #undef UV_FS_REQ_PRIVATE_FIELDS
2112 #undef UV_WORK_PRIVATE_FIELDS
2113 #undef UV_FS_EVENT_PRIVATE_FIELDS
2114 #undef UV_SIGNAL_PRIVATE_FIELDS
2115 #undef UV_LOOP_PRIVATE_FIELDS
2116 #undef UV_LOOP_PRIVATE_PLATFORM_FIELDS