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