]> git.lizzy.rs Git - rust.git/blob - src/librustuv/uvll.rs
auto merge of #10424 : alexcrichton/rust/optimize-buffered, r=brson
[rust.git] / src / librustuv / uvll.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 /*!
12  * Low-level bindings to the libuv library.
13  *
14  * This module contains a set of direct, 'bare-metal' wrappers around
15  * the libuv C-API.
16  *
17  * We're not bothering yet to redefine uv's structs as Rust structs
18  * because they are quite large and change often between versions.
19  * The maintenance burden is just too high. Instead we use the uv's
20  * `uv_handle_size` and `uv_req_size` to find the correct size of the
21  * structs and allocate them on the heap. This can be revisited later.
22  *
23  * There are also a collection of helper functions to ease interacting
24  * with the low-level API.
25  *
26  * As new functionality, existent in uv.h, is added to the rust stdlib,
27  * the mappings should be added in this module.
28  */
29
30 #[allow(non_camel_case_types)]; // C types
31
32 use std::libc::{size_t, c_int, c_uint, c_void, c_char, uintptr_t, c_double};
33 use std::libc::ssize_t;
34 use std::libc::{malloc, free};
35 use std::libc;
36 use std::vec;
37
38 pub use self::errors::*;
39
40 pub static OK: c_int = 0;
41 pub static EOF: c_int = -4095;
42 pub static UNKNOWN: c_int = -4094;
43
44 // uv-errno.h redefines error codes for windows, but not for unix...
45
46 #[cfg(windows)]
47 pub mod errors {
48     use std::libc::c_int;
49
50     pub static EACCES: c_int = -4092;
51     pub static ECONNREFUSED: c_int = -4078;
52     pub static ECONNRESET: c_int = -4077;
53     pub static ENOTCONN: c_int = -4053;
54     pub static EPIPE: c_int = -4047;
55     pub static ECONNABORTED: c_int = -4079;
56     pub static ECANCELED: c_int = -4081;
57     pub static EBADF: c_int = -4083;
58 }
59 #[cfg(not(windows))]
60 pub mod errors {
61     use std::libc;
62     use std::libc::c_int;
63
64     pub static EACCES: c_int = -libc::EACCES;
65     pub static ECONNREFUSED: c_int = -libc::ECONNREFUSED;
66     pub static ECONNRESET: c_int = -libc::ECONNRESET;
67     pub static ENOTCONN: c_int = -libc::ENOTCONN;
68     pub static EPIPE: c_int = -libc::EPIPE;
69     pub static ECONNABORTED: c_int = -libc::ECONNABORTED;
70     pub static ECANCELED : c_int = -libc::ECANCELED;
71     pub static EBADF : c_int = -libc::EBADF;
72 }
73
74 pub static PROCESS_SETUID: c_int = 1 << 0;
75 pub static PROCESS_SETGID: c_int = 1 << 1;
76 pub static PROCESS_WINDOWS_VERBATIM_ARGUMENTS: c_int = 1 << 2;
77 pub static PROCESS_DETACHED: c_int = 1 << 3;
78 pub static PROCESS_WINDOWS_HIDE: c_int = 1 << 4;
79
80 pub static STDIO_IGNORE: c_int = 0x00;
81 pub static STDIO_CREATE_PIPE: c_int = 0x01;
82 pub static STDIO_INHERIT_FD: c_int = 0x02;
83 pub static STDIO_INHERIT_STREAM: c_int = 0x04;
84 pub static STDIO_READABLE_PIPE: c_int = 0x10;
85 pub static STDIO_WRITABLE_PIPE: c_int = 0x20;
86
87 #[cfg(unix)]
88 pub type uv_buf_len_t = libc::size_t;
89 #[cfg(windows)]
90 pub type uv_buf_len_t = libc::c_ulong;
91
92 // see libuv/include/uv-unix.h
93 #[cfg(unix)]
94 pub struct uv_buf_t {
95     base: *u8,
96     len: uv_buf_len_t,
97 }
98
99 // see libuv/include/uv-win.h
100 #[cfg(windows)]
101 pub struct uv_buf_t {
102     len: uv_buf_len_t,
103     base: *u8,
104 }
105
106 #[repr(C)]
107 pub enum uv_run_mode {
108     RUN_DEFAULT = 0,
109     RUN_ONCE,
110     RUN_NOWAIT,
111 }
112
113 pub struct uv_process_options_t {
114     exit_cb: uv_exit_cb,
115     file: *libc::c_char,
116     args: **libc::c_char,
117     env: **libc::c_char,
118     cwd: *libc::c_char,
119     flags: libc::c_uint,
120     stdio_count: libc::c_int,
121     stdio: *uv_stdio_container_t,
122     uid: uv_uid_t,
123     gid: uv_gid_t,
124 }
125
126 // These fields are private because they must be interfaced with through the
127 // functions below.
128 pub struct uv_stdio_container_t {
129     priv flags: libc::c_int,
130     priv stream: *uv_stream_t,
131 }
132
133 pub type uv_handle_t = c_void;
134 pub type uv_req_t = c_void;
135 pub type uv_loop_t = c_void;
136 pub type uv_idle_t = c_void;
137 pub type uv_tcp_t = c_void;
138 pub type uv_udp_t = c_void;
139 pub type uv_connect_t = c_void;
140 pub type uv_connection_t = c_void;
141 pub type uv_write_t = c_void;
142 pub type uv_async_t = c_void;
143 pub type uv_timer_t = c_void;
144 pub type uv_stream_t = c_void;
145 pub type uv_fs_t = c_void;
146 pub type uv_udp_send_t = c_void;
147 pub type uv_getaddrinfo_t = c_void;
148 pub type uv_process_t = c_void;
149 pub type uv_pipe_t = c_void;
150 pub type uv_tty_t = c_void;
151 pub type uv_signal_t = c_void;
152
153 pub struct uv_timespec_t {
154     tv_sec: libc::c_long,
155     tv_nsec: libc::c_long
156 }
157
158 pub struct uv_stat_t {
159     st_dev: libc::uint64_t,
160     st_mode: libc::uint64_t,
161     st_nlink: libc::uint64_t,
162     st_uid: libc::uint64_t,
163     st_gid: libc::uint64_t,
164     st_rdev: libc::uint64_t,
165     st_ino: libc::uint64_t,
166     st_size: libc::uint64_t,
167     st_blksize: libc::uint64_t,
168     st_blocks: libc::uint64_t,
169     st_flags: libc::uint64_t,
170     st_gen: libc::uint64_t,
171     st_atim: uv_timespec_t,
172     st_mtim: uv_timespec_t,
173     st_ctim: uv_timespec_t,
174     st_birthtim: uv_timespec_t
175 }
176
177 impl uv_stat_t {
178     pub fn new() -> uv_stat_t {
179         uv_stat_t {
180             st_dev: 0,
181             st_mode: 0,
182             st_nlink: 0,
183             st_uid: 0,
184             st_gid: 0,
185             st_rdev: 0,
186             st_ino: 0,
187             st_size: 0,
188             st_blksize: 0,
189             st_blocks: 0,
190             st_flags: 0,
191             st_gen: 0,
192             st_atim: uv_timespec_t { tv_sec: 0, tv_nsec: 0 },
193             st_mtim: uv_timespec_t { tv_sec: 0, tv_nsec: 0 },
194             st_ctim: uv_timespec_t { tv_sec: 0, tv_nsec: 0 },
195             st_birthtim: uv_timespec_t { tv_sec: 0, tv_nsec: 0 }
196         }
197     }
198     pub fn is_file(&self) -> bool {
199         ((self.st_mode) & libc::S_IFMT as libc::uint64_t) == libc::S_IFREG as libc::uint64_t
200     }
201     pub fn is_dir(&self) -> bool {
202         ((self.st_mode) & libc::S_IFMT as libc::uint64_t) == libc::S_IFDIR as libc::uint64_t
203     }
204 }
205
206 pub type uv_idle_cb = extern "C" fn(handle: *uv_idle_t,
207                                     status: c_int);
208 pub type uv_alloc_cb = extern "C" fn(stream: *uv_stream_t,
209                                      suggested_size: size_t,
210                                      buf: *mut uv_buf_t);
211 pub type uv_read_cb = extern "C" fn(stream: *uv_stream_t,
212                                     nread: ssize_t,
213                                     buf: *uv_buf_t);
214 pub type uv_udp_send_cb = extern "C" fn(req: *uv_udp_send_t,
215                                         status: c_int);
216 pub type uv_udp_recv_cb = extern "C" fn(handle: *uv_udp_t,
217                                         nread: ssize_t,
218                                         buf: *uv_buf_t,
219                                         addr: *sockaddr,
220                                         flags: c_uint);
221 pub type uv_close_cb = extern "C" fn(handle: *uv_handle_t);
222 pub type uv_walk_cb = extern "C" fn(handle: *uv_handle_t,
223                                     arg: *c_void);
224 pub type uv_async_cb = extern "C" fn(handle: *uv_async_t,
225                                      status: c_int);
226 pub type uv_connect_cb = extern "C" fn(handle: *uv_connect_t,
227                                        status: c_int);
228 pub type uv_connection_cb = extern "C" fn(handle: *uv_connection_t,
229                                           status: c_int);
230 pub type uv_timer_cb = extern "C" fn(handle: *uv_timer_t,
231                                      status: c_int);
232 pub type uv_write_cb = extern "C" fn(handle: *uv_write_t,
233                                      status: c_int);
234 pub type uv_getaddrinfo_cb = extern "C" fn(req: *uv_getaddrinfo_t,
235                                            status: c_int,
236                                            res: *addrinfo);
237 pub type uv_exit_cb = extern "C" fn(handle: *uv_process_t,
238                                     exit_status: i64,
239                                     term_signal: c_int);
240 pub type uv_signal_cb = extern "C" fn(handle: *uv_signal_t,
241                                       signum: c_int);
242 pub type uv_fs_cb = extern "C" fn(req: *uv_fs_t);
243
244 pub type sockaddr = c_void;
245
246 #[cfg(unix)]
247 pub type socklen_t = c_int;
248
249 // XXX: This is a standard C type. Could probably be defined in libc
250 #[cfg(target_os = "android")]
251 #[cfg(target_os = "linux")]
252 pub struct addrinfo {
253     ai_flags: c_int,
254     ai_family: c_int,
255     ai_socktype: c_int,
256     ai_protocol: c_int,
257     ai_addrlen: socklen_t,
258     ai_addr: *sockaddr,
259     ai_canonname: *char,
260     ai_next: *addrinfo
261 }
262
263 #[cfg(target_os = "macos")]
264 #[cfg(target_os = "freebsd")]
265 pub struct addrinfo {
266     ai_flags: c_int,
267     ai_family: c_int,
268     ai_socktype: c_int,
269     ai_protocol: c_int,
270     ai_addrlen: socklen_t,
271     ai_canonname: *char,
272     ai_addr: *sockaddr,
273     ai_next: *addrinfo
274 }
275
276 #[cfg(windows)]
277 pub struct addrinfo {
278     ai_flags: c_int,
279     ai_family: c_int,
280     ai_socktype: c_int,
281     ai_protocol: c_int,
282     ai_addrlen: size_t,
283     ai_canonname: *char,
284     ai_addr: *sockaddr,
285     ai_next: *addrinfo
286 }
287
288 #[cfg(unix)] pub type uv_uid_t = libc::types::os::arch::posix88::uid_t;
289 #[cfg(unix)] pub type uv_gid_t = libc::types::os::arch::posix88::gid_t;
290 #[cfg(windows)] pub type uv_uid_t = libc::c_uchar;
291 #[cfg(windows)] pub type uv_gid_t = libc::c_uchar;
292
293 #[repr(C)]
294 #[deriving(Eq)]
295 pub enum uv_handle_type {
296     UV_UNKNOWN_HANDLE,
297     UV_ASYNC,
298     UV_CHECK,
299     UV_FS_EVENT,
300     UV_FS_POLL,
301     UV_HANDLE,
302     UV_IDLE,
303     UV_NAMED_PIPE,
304     UV_POLL,
305     UV_PREPARE,
306     UV_PROCESS,
307     UV_STREAM,
308     UV_TCP,
309     UV_TIMER,
310     UV_TTY,
311     UV_UDP,
312     UV_SIGNAL,
313     UV_FILE,
314     UV_HANDLE_TYPE_MAX
315 }
316
317 #[repr(C)]
318 #[cfg(unix)]
319 #[deriving(Eq)]
320 pub enum uv_req_type {
321     UV_UNKNOWN_REQ,
322     UV_REQ,
323     UV_CONNECT,
324     UV_WRITE,
325     UV_SHUTDOWN,
326     UV_UDP_SEND,
327     UV_FS,
328     UV_WORK,
329     UV_GETADDRINFO,
330     UV_REQ_TYPE_MAX
331 }
332
333 // uv_req_type may have additional fields defined by UV_REQ_TYPE_PRIVATE.
334 // See UV_REQ_TYPE_PRIVATE at libuv/include/uv-win.h
335 #[repr(C)]
336 #[cfg(windows)]
337 #[deriving(Eq)]
338 pub enum uv_req_type {
339     UV_UNKNOWN_REQ,
340     UV_REQ,
341     UV_CONNECT,
342     UV_WRITE,
343     UV_SHUTDOWN,
344     UV_UDP_SEND,
345     UV_FS,
346     UV_WORK,
347     UV_GETADDRINFO,
348     UV_ACCEPT,
349     UV_FS_EVENT_REQ,
350     UV_POLL_REQ,
351     UV_PROCESS_EXIT,
352     UV_READ,
353     UV_UDP_RECV,
354     UV_WAKEUP,
355     UV_SIGNAL_REQ,
356     UV_REQ_TYPE_MAX
357 }
358
359 #[repr(C)]
360 #[deriving(Eq)]
361 pub enum uv_membership {
362     UV_LEAVE_GROUP,
363     UV_JOIN_GROUP
364 }
365
366 pub unsafe fn malloc_handle(handle: uv_handle_type) -> *c_void {
367     assert!(handle != UV_UNKNOWN_HANDLE && handle != UV_HANDLE_TYPE_MAX);
368     let size = uv_handle_size(handle);
369     let p = malloc(size);
370     assert!(p.is_not_null());
371     return p;
372 }
373
374 pub unsafe fn free_handle(v: *c_void) {
375     free(v)
376 }
377
378 pub unsafe fn malloc_req(req: uv_req_type) -> *c_void {
379     assert!(req != UV_UNKNOWN_REQ && req != UV_REQ_TYPE_MAX);
380     let size = uv_req_size(req);
381     let p = malloc(size);
382     assert!(p.is_not_null());
383     return p;
384 }
385
386 pub unsafe fn free_req(v: *c_void) {
387     free(v)
388 }
389
390 #[test]
391 fn handle_sanity_check() {
392     unsafe {
393         assert_eq!(UV_HANDLE_TYPE_MAX as uint, rust_uv_handle_type_max());
394     }
395 }
396
397 #[test]
398 fn request_sanity_check() {
399     unsafe {
400         assert_eq!(UV_REQ_TYPE_MAX as uint, rust_uv_req_type_max());
401     }
402 }
403
404 // XXX Event loops ignore SIGPIPE by default.
405 pub unsafe fn loop_new() -> *c_void {
406     return rust_uv_loop_new();
407 }
408
409 pub unsafe fn uv_write(req: *uv_write_t,
410                        stream: *uv_stream_t,
411                        buf_in: &[uv_buf_t],
412                        cb: uv_write_cb) -> c_int {
413     extern {
414         fn uv_write(req: *uv_write_t, stream: *uv_stream_t,
415                     buf_in: *uv_buf_t, buf_cnt: c_int,
416                     cb: uv_write_cb) -> c_int;
417     }
418
419     let buf_ptr = vec::raw::to_ptr(buf_in);
420     let buf_cnt = buf_in.len() as i32;
421     return uv_write(req, stream, buf_ptr, buf_cnt, cb);
422 }
423
424 pub unsafe fn uv_udp_send(req: *uv_udp_send_t,
425                           handle: *uv_udp_t,
426                           buf_in: &[uv_buf_t],
427                           addr: *sockaddr,
428                           cb: uv_udp_send_cb) -> c_int {
429     extern {
430         fn uv_udp_send(req: *uv_write_t, stream: *uv_stream_t,
431                        buf_in: *uv_buf_t, buf_cnt: c_int, addr: *sockaddr,
432                        cb: uv_udp_send_cb) -> c_int;
433     }
434
435     let buf_ptr = vec::raw::to_ptr(buf_in);
436     let buf_cnt = buf_in.len() as i32;
437     return uv_udp_send(req, handle, buf_ptr, buf_cnt, addr, cb);
438 }
439
440 pub unsafe fn get_udp_handle_from_send_req(send_req: *uv_udp_send_t) -> *uv_udp_t {
441     return rust_uv_get_udp_handle_from_send_req(send_req);
442 }
443
444 pub unsafe fn process_pid(p: *uv_process_t) -> c_int {
445
446     return rust_uv_process_pid(p);
447 }
448
449 pub unsafe fn set_stdio_container_flags(c: *uv_stdio_container_t,
450                                         flags: libc::c_int) {
451
452     rust_set_stdio_container_flags(c, flags);
453 }
454
455 pub unsafe fn set_stdio_container_fd(c: *uv_stdio_container_t,
456                                      fd: libc::c_int) {
457
458     rust_set_stdio_container_fd(c, fd);
459 }
460
461 pub unsafe fn set_stdio_container_stream(c: *uv_stdio_container_t,
462                                          stream: *uv_stream_t) {
463     rust_set_stdio_container_stream(c, stream);
464 }
465
466 // data access helpers
467 pub unsafe fn get_result_from_fs_req(req: *uv_fs_t) -> c_int {
468     rust_uv_get_result_from_fs_req(req)
469 }
470 pub unsafe fn get_ptr_from_fs_req(req: *uv_fs_t) -> *libc::c_void {
471     rust_uv_get_ptr_from_fs_req(req)
472 }
473 pub unsafe fn get_path_from_fs_req(req: *uv_fs_t) -> *c_char {
474     rust_uv_get_path_from_fs_req(req)
475 }
476 pub unsafe fn get_loop_from_fs_req(req: *uv_fs_t) -> *uv_loop_t {
477     rust_uv_get_loop_from_fs_req(req)
478 }
479 pub unsafe fn get_loop_from_getaddrinfo_req(req: *uv_getaddrinfo_t) -> *uv_loop_t {
480     rust_uv_get_loop_from_getaddrinfo_req(req)
481 }
482 pub unsafe fn get_loop_for_uv_handle<T>(handle: *T) -> *c_void {
483     return rust_uv_get_loop_for_uv_handle(handle as *c_void);
484 }
485 pub unsafe fn get_stream_handle_from_connect_req(connect: *uv_connect_t) -> *uv_stream_t {
486     return rust_uv_get_stream_handle_from_connect_req(connect);
487 }
488 pub unsafe fn get_stream_handle_from_write_req(write_req: *uv_write_t) -> *uv_stream_t {
489     return rust_uv_get_stream_handle_from_write_req(write_req);
490 }
491 pub unsafe fn get_data_for_uv_loop(loop_ptr: *c_void) -> *c_void {
492     rust_uv_get_data_for_uv_loop(loop_ptr)
493 }
494 pub unsafe fn set_data_for_uv_loop(loop_ptr: *c_void, data: *c_void) {
495     rust_uv_set_data_for_uv_loop(loop_ptr, data);
496 }
497 pub unsafe fn get_data_for_uv_handle<T>(handle: *T) -> *c_void {
498     return rust_uv_get_data_for_uv_handle(handle as *c_void);
499 }
500 pub unsafe fn set_data_for_uv_handle<T, U>(handle: *T, data: *U) {
501     rust_uv_set_data_for_uv_handle(handle as *c_void, data as *c_void);
502 }
503 pub unsafe fn get_data_for_req<T>(req: *T) -> *c_void {
504     return rust_uv_get_data_for_req(req as *c_void);
505 }
506 pub unsafe fn set_data_for_req<T, U>(req: *T, data: *U) {
507     rust_uv_set_data_for_req(req as *c_void, data as *c_void);
508 }
509 pub unsafe fn populate_stat(req_in: *uv_fs_t, stat_out: *uv_stat_t) {
510     rust_uv_populate_uv_stat(req_in, stat_out)
511 }
512 pub unsafe fn guess_handle(handle: c_int) -> c_int {
513     rust_uv_guess_handle(handle)
514 }
515
516
517 // uv_support is the result of compiling rust_uv.cpp
518 //
519 // Note that this is in a cfg'd block so it doesn't get linked during testing.
520 // There's a bit of a conundrum when testing in that we're actually assuming
521 // that the tests are running in a uv loop, but they were created from the
522 // statically linked uv to the original rustuv crate. When we create the test
523 // executable, on some platforms if we re-link against uv, it actually creates
524 // second copies of everything. We obviously don't want this, so instead of
525 // dying horribly during testing, we allow all of the test rustuv's references
526 // to get resolved to the original rustuv crate.
527 #[link_args = "-luv_support -luv"]
528 #[cfg(not(test))]
529 extern {}
530
531 extern {
532     fn rust_uv_loop_new() -> *c_void;
533
534     // dealing with sockaddr things
535     pub fn rust_sockaddr_size() -> c_int;
536     pub fn rust_malloc_ip4_addr(s: *c_char, port: c_int) -> *sockaddr;
537     pub fn rust_malloc_ip6_addr(s: *c_char, port: c_int) -> *sockaddr;
538     pub fn rust_ip4_port(src: *sockaddr) -> c_uint;
539     pub fn rust_ip6_port(src: *sockaddr) -> c_uint;
540     pub fn rust_is_ipv4_sockaddr(addr: *sockaddr) -> c_int;
541     pub fn rust_is_ipv6_sockaddr(addr: *sockaddr) -> c_int;
542
543     fn rust_uv_handle_type_max() -> uintptr_t;
544     fn rust_uv_req_type_max() -> uintptr_t;
545     fn rust_uv_get_udp_handle_from_send_req(req: *uv_udp_send_t) -> *uv_udp_t;
546
547     fn rust_uv_populate_uv_stat(req_in: *uv_fs_t, stat_out: *uv_stat_t);
548     fn rust_uv_get_result_from_fs_req(req: *uv_fs_t) -> c_int;
549     fn rust_uv_get_ptr_from_fs_req(req: *uv_fs_t) -> *libc::c_void;
550     fn rust_uv_get_path_from_fs_req(req: *uv_fs_t) -> *c_char;
551     fn rust_uv_get_loop_from_fs_req(req: *uv_fs_t) -> *uv_loop_t;
552     fn rust_uv_get_loop_from_getaddrinfo_req(req: *uv_fs_t) -> *uv_loop_t;
553     fn rust_uv_get_stream_handle_from_connect_req(req: *uv_connect_t) -> *uv_stream_t;
554     fn rust_uv_get_stream_handle_from_write_req(req: *uv_write_t) -> *uv_stream_t;
555     fn rust_uv_get_loop_for_uv_handle(handle: *c_void) -> *c_void;
556     fn rust_uv_get_data_for_uv_loop(loop_ptr: *c_void) -> *c_void;
557     fn rust_uv_set_data_for_uv_loop(loop_ptr: *c_void, data: *c_void);
558     fn rust_uv_get_data_for_uv_handle(handle: *c_void) -> *c_void;
559     fn rust_uv_set_data_for_uv_handle(handle: *c_void, data: *c_void);
560     fn rust_uv_get_data_for_req(req: *c_void) -> *c_void;
561     fn rust_uv_set_data_for_req(req: *c_void, data: *c_void);
562     fn rust_set_stdio_container_flags(c: *uv_stdio_container_t, flags: c_int);
563     fn rust_set_stdio_container_fd(c: *uv_stdio_container_t, fd: c_int);
564     fn rust_set_stdio_container_stream(c: *uv_stdio_container_t,
565                                        stream: *uv_stream_t);
566     fn rust_uv_process_pid(p: *uv_process_t) -> c_int;
567     fn rust_uv_guess_handle(fd: c_int) -> c_int;
568
569     // generic uv functions
570     pub fn uv_loop_delete(l: *uv_loop_t);
571     pub fn uv_handle_size(ty: uv_handle_type) -> size_t;
572     pub fn uv_req_size(ty: uv_req_type) -> size_t;
573     pub fn uv_run(l: *uv_loop_t, mode: uv_run_mode) -> c_int;
574     pub fn uv_close(h: *uv_handle_t, cb: uv_close_cb);
575     pub fn uv_walk(l: *uv_loop_t, cb: uv_walk_cb, arg: *c_void);
576     pub fn uv_buf_init(base: *c_char, len: c_uint) -> uv_buf_t;
577     pub fn uv_strerror(err: c_int) -> *c_char;
578     pub fn uv_err_name(err: c_int) -> *c_char;
579     pub fn uv_listen(s: *uv_stream_t, backlog: c_int,
580                      cb: uv_connection_cb) -> c_int;
581     pub fn uv_accept(server: *uv_stream_t, client: *uv_stream_t) -> c_int;
582     pub fn uv_read_start(stream: *uv_stream_t,
583                          on_alloc: uv_alloc_cb,
584                          on_read: uv_read_cb) -> c_int;
585     pub fn uv_read_stop(stream: *uv_stream_t) -> c_int;
586
587     // idle bindings
588     pub fn uv_idle_init(l: *uv_loop_t, i: *uv_idle_t) -> c_int;
589     pub fn uv_idle_start(i: *uv_idle_t, cb: uv_idle_cb) -> c_int;
590     pub fn uv_idle_stop(i: *uv_idle_t) -> c_int;
591
592     // async bindings
593     pub fn uv_async_init(l: *uv_loop_t, a: *uv_async_t,
594                          cb: uv_async_cb) -> c_int;
595     pub fn uv_async_send(a: *uv_async_t);
596
597     // tcp bindings
598     pub fn uv_tcp_init(l: *uv_loop_t, h: *uv_tcp_t) -> c_int;
599     pub fn uv_tcp_connect(c: *uv_connect_t, h: *uv_tcp_t,
600                           addr: *sockaddr, cb: uv_connect_cb) -> c_int;
601     pub fn uv_tcp_bind(t: *uv_tcp_t, addr: *sockaddr) -> c_int;
602     pub fn uv_ip4_name(src: *sockaddr, dst: *c_char,
603                        size: size_t) -> c_int;
604     pub fn uv_ip6_name(src: *sockaddr, dst: *c_char,
605                        size: size_t) -> c_int;
606     pub fn uv_tcp_nodelay(h: *uv_tcp_t, enable: c_int) -> c_int;
607     pub fn uv_tcp_keepalive(h: *uv_tcp_t, enable: c_int,
608                             delay: c_uint) -> c_int;
609     pub fn uv_tcp_simultaneous_accepts(h: *uv_tcp_t, enable: c_int) -> c_int;
610     pub fn uv_tcp_getsockname(h: *uv_tcp_t, name: *sockaddr,
611                               len: *mut c_int) -> c_int;
612     pub fn uv_tcp_getpeername(h: *uv_tcp_t, name: *sockaddr,
613                               len: *mut c_int) -> c_int;
614     pub fn uv_ip4_addr(ip: *c_char, port: c_int, addr: *sockaddr) -> c_int;
615     pub fn uv_ip6_addr(ip: *c_char, port: c_int, addr: *sockaddr) -> c_int;
616
617     // udp bindings
618     pub fn uv_udp_init(l: *uv_loop_t, h: *uv_udp_t) -> c_int;
619     pub fn uv_udp_bind(h: *uv_udp_t, addr: *sockaddr, flags: c_uint) -> c_int;
620     pub fn uv_udp_recv_start(server: *uv_udp_t,
621                              on_alloc: uv_alloc_cb,
622                              on_recv: uv_udp_recv_cb) -> c_int;
623     pub fn uv_udp_set_membership(handle: *uv_udp_t, multicast_addr: *c_char,
624                                  interface_addr: *c_char,
625                                  membership: uv_membership) -> c_int;
626     pub fn uv_udp_recv_stop(server: *uv_udp_t) -> c_int;
627     pub fn uv_udp_set_multicast_loop(handle: *uv_udp_t, on: c_int) -> c_int;
628     pub fn uv_udp_set_multicast_ttl(handle: *uv_udp_t, ttl: c_int) -> c_int;
629     pub fn uv_udp_set_ttl(handle: *uv_udp_t, ttl: c_int) -> c_int;
630     pub fn uv_udp_set_broadcast(handle: *uv_udp_t, on: c_int) -> c_int;
631     pub fn uv_udp_getsockname(h: *uv_udp_t, name: *sockaddr,
632                               len: *mut c_int) -> c_int;
633
634     // timer bindings
635     pub fn uv_timer_init(l: *uv_loop_t, t: *uv_timer_t) -> c_int;
636     pub fn uv_timer_start(t: *uv_timer_t, cb: uv_timer_cb,
637                           timeout: libc::uint64_t,
638                           repeat: libc::uint64_t) -> c_int;
639     pub fn uv_timer_stop(handle: *uv_timer_t) -> c_int;
640
641     // fs operations
642     pub fn uv_fs_open(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char,
643                       flags: c_int, mode: c_int, cb: uv_fs_cb) -> c_int;
644     pub fn uv_fs_unlink(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char,
645                         cb: uv_fs_cb) -> c_int;
646     pub fn uv_fs_write(l: *uv_loop_t, req: *uv_fs_t, fd: c_int, buf: *c_void,
647                        len: size_t, offset: i64, cb: uv_fs_cb) -> c_int;
648     pub fn uv_fs_read(l: *uv_loop_t, req: *uv_fs_t, fd: c_int, buf: *c_void,
649                       len: size_t, offset: i64, cb: uv_fs_cb) -> c_int;
650     pub fn uv_fs_close(l: *uv_loop_t, req: *uv_fs_t, fd: c_int,
651                        cb: uv_fs_cb) -> c_int;
652     pub fn uv_fs_stat(l: *uv_loop_t, req: *uv_fs_t, path: *c_char,
653                       cb: uv_fs_cb) -> c_int;
654     pub fn uv_fs_fstat(l: *uv_loop_t, req: *uv_fs_t, fd: c_int,
655                        cb: uv_fs_cb) -> c_int;
656     pub fn uv_fs_mkdir(l: *uv_loop_t, req: *uv_fs_t, path: *c_char,
657                        mode: c_int, cb: uv_fs_cb) -> c_int;
658     pub fn uv_fs_rmdir(l: *uv_loop_t, req: *uv_fs_t, path: *c_char,
659                        cb: uv_fs_cb) -> c_int;
660     pub fn uv_fs_readdir(l: *uv_loop_t, req: *uv_fs_t, path: *c_char,
661                          flags: c_int, cb: uv_fs_cb) -> c_int;
662     pub fn uv_fs_req_cleanup(req: *uv_fs_t);
663     pub fn uv_fs_fsync(handle: *uv_loop_t, req: *uv_fs_t, file: c_int,
664                        cb: uv_fs_cb) -> c_int;
665     pub fn uv_fs_fdatasync(handle: *uv_loop_t, req: *uv_fs_t, file: c_int,
666                            cb: uv_fs_cb) -> c_int;
667     pub fn uv_fs_ftruncate(handle: *uv_loop_t, req: *uv_fs_t, file: c_int,
668                            offset: i64, cb: uv_fs_cb) -> c_int;
669     pub fn uv_fs_readlink(handle: *uv_loop_t, req: *uv_fs_t, file: *c_char,
670                           cb: uv_fs_cb) -> c_int;
671     pub fn uv_fs_symlink(handle: *uv_loop_t, req: *uv_fs_t, src: *c_char,
672                          dst: *c_char, flags: c_int, cb: uv_fs_cb) -> c_int;
673     pub fn uv_fs_rename(handle: *uv_loop_t, req: *uv_fs_t, src: *c_char,
674                         dst: *c_char, cb: uv_fs_cb) -> c_int;
675     pub fn uv_fs_utime(handle: *uv_loop_t, req: *uv_fs_t, path: *c_char,
676                        atime: c_double, mtime: c_double,
677                        cb: uv_fs_cb) -> c_int;
678     pub fn uv_fs_link(handle: *uv_loop_t, req: *uv_fs_t, src: *c_char,
679                       dst: *c_char, cb: uv_fs_cb) -> c_int;
680     pub fn uv_fs_chown(handle: *uv_loop_t, req: *uv_fs_t, src: *c_char,
681                        uid: uv_uid_t, gid: uv_gid_t, cb: uv_fs_cb) -> c_int;
682     pub fn uv_fs_chmod(handle: *uv_loop_t, req: *uv_fs_t, path: *c_char,
683                        mode: c_int, cb: uv_fs_cb) -> c_int;
684     pub fn uv_fs_lstat(handle: *uv_loop_t, req: *uv_fs_t, file: *c_char,
685                        cb: uv_fs_cb) -> c_int;
686
687     // getaddrinfo
688     pub fn uv_getaddrinfo(loop_: *uv_loop_t, req: *uv_getaddrinfo_t,
689                           getaddrinfo_cb: uv_getaddrinfo_cb,
690                           node: *c_char, service: *c_char,
691                           hints: *addrinfo) -> c_int;
692     pub fn uv_freeaddrinfo(ai: *addrinfo);
693
694     // process spawning
695     pub fn uv_spawn(loop_ptr: *uv_loop_t, outptr: *uv_process_t,
696                     options: *uv_process_options_t) -> c_int;
697     pub fn uv_process_kill(p: *uv_process_t, signum: c_int) -> c_int;
698
699     // pipes
700     pub fn uv_pipe_init(l: *uv_loop_t, p: *uv_pipe_t, ipc: c_int) -> c_int;
701     pub fn uv_pipe_open(pipe: *uv_pipe_t, file: c_int) -> c_int;
702     pub fn uv_pipe_bind(pipe: *uv_pipe_t, name: *c_char) -> c_int;
703     pub fn uv_pipe_connect(req: *uv_connect_t, handle: *uv_pipe_t,
704                            name: *c_char, cb: uv_connect_cb);
705
706     // tty
707     pub fn uv_tty_init(l: *uv_loop_t, tty: *uv_tty_t, fd: c_int,
708                        readable: c_int) -> c_int;
709     pub fn uv_tty_set_mode(tty: *uv_tty_t, mode: c_int) -> c_int;
710     pub fn uv_tty_get_winsize(tty: *uv_tty_t, width: *c_int,
711                               height: *c_int) -> c_int;
712
713     // signals
714     pub fn uv_signal_init(loop_: *uv_loop_t, handle: *uv_signal_t) -> c_int;
715     pub fn uv_signal_start(h: *uv_signal_t, cb: uv_signal_cb,
716                            signum: c_int) -> c_int;
717     pub fn uv_signal_stop(handle: *uv_signal_t) -> c_int;
718 }
719
720 // libuv requires various system libraries to successfully link on some
721 // platforms
722 #[cfg(target_os = "linux")]
723 #[link_args = "-lpthread"]
724 extern {}
725
726 #[cfg(target_os = "win32")]
727 #[link_args = "-lWs2_32 -lpsapi -liphlpapi"]
728 extern {}