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