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