]> git.lizzy.rs Git - rust.git/blob - src/librustuv/uvll.rs
Work around bugs in 32-bit enum FFI
[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 = -4093;
51     pub static ECONNREFUSED: c_int = -4079;
52     pub static ECONNRESET: c_int = -4078;
53     pub static ENOTCONN: c_int = -4054;
54     pub static EPIPE: c_int = -4048;
55     pub static ECONNABORTED: c_int = -4080;
56     pub static ECANCELED: c_int = -4082;
57 }
58 #[cfg(not(windows))]
59 pub mod errors {
60     use std::libc;
61     use std::libc::c_int;
62
63     pub static EACCES: c_int = -libc::EACCES;
64     pub static ECONNREFUSED: c_int = -libc::ECONNREFUSED;
65     pub static ECONNRESET: c_int = -libc::ECONNRESET;
66     pub static ENOTCONN: c_int = -libc::ENOTCONN;
67     pub static EPIPE: c_int = -libc::EPIPE;
68     pub static ECONNABORTED: c_int = -libc::ECONNABORTED;
69     pub static ECANCELED : c_int = -libc::ECANCELED;
70 }
71
72 pub static PROCESS_SETUID: c_int = 1 << 0;
73 pub static PROCESS_SETGID: c_int = 1 << 1;
74 pub static PROCESS_WINDOWS_VERBATIM_ARGUMENTS: c_int = 1 << 2;
75 pub static PROCESS_DETACHED: c_int = 1 << 3;
76 pub static PROCESS_WINDOWS_HIDE: c_int = 1 << 4;
77
78 pub static STDIO_IGNORE: c_int = 0x00;
79 pub static STDIO_CREATE_PIPE: c_int = 0x01;
80 pub static STDIO_INHERIT_FD: c_int = 0x02;
81 pub static STDIO_INHERIT_STREAM: c_int = 0x04;
82 pub static STDIO_READABLE_PIPE: c_int = 0x10;
83 pub static STDIO_WRITABLE_PIPE: c_int = 0x20;
84
85 #[cfg(unix)]
86 pub type uv_buf_len_t = libc::size_t;
87 #[cfg(windows)]
88 pub type uv_buf_len_t = u32;
89
90 // see libuv/include/uv-unix.h
91 #[cfg(unix)]
92 pub struct uv_buf_t {
93     base: *u8,
94     len: libc::size_t,
95 }
96
97 // see libuv/include/uv-win.h
98 #[cfg(windows)]
99 pub struct uv_buf_t {
100     len: u32,
101     base: *u8,
102 }
103
104 #[repr(C)]
105 pub enum uv_run_mode {
106     RUN_DEFAULT = 0,
107     RUN_ONCE,
108     RUN_NOWAIT,
109 }
110
111 pub struct uv_process_options_t {
112     exit_cb: uv_exit_cb,
113     file: *libc::c_char,
114     args: **libc::c_char,
115     env: **libc::c_char,
116     cwd: *libc::c_char,
117     flags: libc::c_uint,
118     stdio_count: libc::c_int,
119     stdio: *uv_stdio_container_t,
120     uid: uv_uid_t,
121     gid: uv_gid_t,
122 }
123
124 // These fields are private because they must be interfaced with through the
125 // functions below.
126 pub struct uv_stdio_container_t {
127     priv flags: libc::c_int,
128     priv stream: *uv_stream_t,
129 }
130
131 pub type uv_handle_t = c_void;
132 pub type uv_req_t = c_void;
133 pub type uv_loop_t = c_void;
134 pub type uv_idle_t = c_void;
135 pub type uv_tcp_t = c_void;
136 pub type uv_udp_t = c_void;
137 pub type uv_connect_t = c_void;
138 pub type uv_connection_t = c_void;
139 pub type uv_write_t = c_void;
140 pub type uv_async_t = c_void;
141 pub type uv_timer_t = c_void;
142 pub type uv_stream_t = c_void;
143 pub type uv_fs_t = c_void;
144 pub type uv_udp_send_t = c_void;
145 pub type uv_getaddrinfo_t = c_void;
146 pub type uv_process_t = c_void;
147 pub type uv_pipe_t = c_void;
148 pub type uv_tty_t = c_void;
149 pub type uv_signal_t = c_void;
150
151 pub struct uv_timespec_t {
152     tv_sec: libc::c_long,
153     tv_nsec: libc::c_long
154 }
155
156 pub struct uv_stat_t {
157     st_dev: libc::uint64_t,
158     st_mode: libc::uint64_t,
159     st_nlink: libc::uint64_t,
160     st_uid: libc::uint64_t,
161     st_gid: libc::uint64_t,
162     st_rdev: libc::uint64_t,
163     st_ino: libc::uint64_t,
164     st_size: libc::uint64_t,
165     st_blksize: libc::uint64_t,
166     st_blocks: libc::uint64_t,
167     st_flags: libc::uint64_t,
168     st_gen: libc::uint64_t,
169     st_atim: uv_timespec_t,
170     st_mtim: uv_timespec_t,
171     st_ctim: uv_timespec_t,
172     st_birthtim: uv_timespec_t
173 }
174
175 impl uv_stat_t {
176     pub fn new() -> uv_stat_t {
177         uv_stat_t {
178             st_dev: 0,
179             st_mode: 0,
180             st_nlink: 0,
181             st_uid: 0,
182             st_gid: 0,
183             st_rdev: 0,
184             st_ino: 0,
185             st_size: 0,
186             st_blksize: 0,
187             st_blocks: 0,
188             st_flags: 0,
189             st_gen: 0,
190             st_atim: uv_timespec_t { tv_sec: 0, tv_nsec: 0 },
191             st_mtim: uv_timespec_t { tv_sec: 0, tv_nsec: 0 },
192             st_ctim: uv_timespec_t { tv_sec: 0, tv_nsec: 0 },
193             st_birthtim: uv_timespec_t { tv_sec: 0, tv_nsec: 0 }
194         }
195     }
196     pub fn is_file(&self) -> bool {
197         ((self.st_mode) & libc::S_IFMT as libc::uint64_t) == libc::S_IFREG as libc::uint64_t
198     }
199     pub fn is_dir(&self) -> bool {
200         ((self.st_mode) & libc::S_IFMT as libc::uint64_t) == libc::S_IFDIR as libc::uint64_t
201     }
202 }
203
204 pub type uv_idle_cb = extern "C" fn(handle: *uv_idle_t,
205                                     status: c_int);
206 pub type uv_alloc_cb = extern "C" fn(stream: *uv_stream_t,
207                                      suggested_size: size_t) -> uv_buf_t;
208 pub type uv_read_cb = extern "C" fn(stream: *uv_stream_t,
209                                     nread: ssize_t,
210                                     buf: uv_buf_t);
211 pub type uv_udp_send_cb = extern "C" fn(req: *uv_udp_send_t,
212                                         status: c_int);
213 pub type uv_udp_recv_cb = extern "C" fn(handle: *uv_udp_t,
214                                         nread: ssize_t,
215                                         buf: uv_buf_t,
216                                         addr: *sockaddr,
217                                         flags: c_uint);
218 pub type uv_close_cb = extern "C" fn(handle: *uv_handle_t);
219 pub type uv_walk_cb = extern "C" fn(handle: *uv_handle_t,
220                                     arg: *c_void);
221 pub type uv_async_cb = extern "C" fn(handle: *uv_async_t,
222                                      status: c_int);
223 pub type uv_connect_cb = extern "C" fn(handle: *uv_connect_t,
224                                        status: c_int);
225 pub type uv_connection_cb = extern "C" fn(handle: *uv_connection_t,
226                                           status: c_int);
227 pub type uv_timer_cb = extern "C" fn(handle: *uv_timer_t,
228                                      status: c_int);
229 pub type uv_write_cb = extern "C" fn(handle: *uv_write_t,
230                                      status: c_int);
231 pub type uv_getaddrinfo_cb = extern "C" fn(req: *uv_getaddrinfo_t,
232                                            status: c_int,
233                                            res: *addrinfo);
234 pub type uv_exit_cb = extern "C" fn(handle: *uv_process_t,
235                                     exit_status: c_int,
236                                     term_signal: c_int);
237 pub type uv_signal_cb = extern "C" fn(handle: *uv_signal_t,
238                                       signum: c_int);
239 pub type uv_fs_cb = extern "C" fn(req: *uv_fs_t);
240
241 pub type sockaddr = c_void;
242 pub type sockaddr_in = c_void;
243 pub type sockaddr_in6 = c_void;
244 pub type sockaddr_storage = 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     #[fixed_stack_segment]; #[inline(never)];
368
369     assert!(handle != UV_UNKNOWN_HANDLE && handle != UV_HANDLE_TYPE_MAX);
370     let size = uv_handle_size(handle);
371     let p = malloc(size);
372     assert!(p.is_not_null());
373     return p;
374 }
375
376 pub unsafe fn free_handle(v: *c_void) {
377     #[fixed_stack_segment]; #[inline(never)];
378
379     free(v)
380 }
381
382 pub unsafe fn malloc_req(req: uv_req_type) -> *c_void {
383     #[fixed_stack_segment]; #[inline(never)];
384
385     assert!(req != UV_UNKNOWN_REQ && req != UV_REQ_TYPE_MAX);
386     let size = uv_req_size(req);
387     let p = malloc(size);
388     assert!(p.is_not_null());
389     return p;
390 }
391
392 pub unsafe fn free_req(v: *c_void) {
393     #[fixed_stack_segment]; #[inline(never)];
394
395     free(v)
396 }
397
398 #[test]
399 fn handle_sanity_check() {
400     #[fixed_stack_segment]; #[inline(never)];
401     unsafe {
402         assert_eq!(UV_HANDLE_TYPE_MAX as uint, rust_uv_handle_type_max());
403     }
404 }
405
406 #[test]
407 fn request_sanity_check() {
408     #[fixed_stack_segment]; #[inline(never)];
409     unsafe {
410         assert_eq!(UV_REQ_TYPE_MAX as uint, rust_uv_req_type_max());
411     }
412 }
413
414 // XXX Event loops ignore SIGPIPE by default.
415 pub unsafe fn loop_new() -> *c_void {
416     #[fixed_stack_segment]; #[inline(never)];
417
418     return rust_uv_loop_new();
419 }
420
421 pub unsafe fn udp_bind(server: *uv_udp_t, addr: *sockaddr_in, flags: c_uint) -> c_int {
422     #[fixed_stack_segment]; #[inline(never)];
423
424     return rust_uv_udp_bind(server, addr, flags);
425 }
426
427 pub unsafe fn udp_bind6(server: *uv_udp_t, addr: *sockaddr_in6, flags: c_uint) -> c_int {
428     #[fixed_stack_segment]; #[inline(never)];
429
430     return rust_uv_udp_bind6(server, addr, flags);
431 }
432
433 pub unsafe fn udp_send<T>(req: *uv_udp_send_t, handle: *T, buf_in: &[uv_buf_t],
434                           addr: *sockaddr_in, cb: uv_udp_send_cb) -> c_int {
435     #[fixed_stack_segment]; #[inline(never)];
436
437     let buf_ptr = vec::raw::to_ptr(buf_in);
438     let buf_cnt = buf_in.len() as i32;
439     return rust_uv_udp_send(req, handle as *c_void, buf_ptr, buf_cnt, addr, cb);
440 }
441
442 pub unsafe fn udp_send6<T>(req: *uv_udp_send_t, handle: *T, buf_in: &[uv_buf_t],
443                           addr: *sockaddr_in6, cb: uv_udp_send_cb) -> c_int {
444     #[fixed_stack_segment]; #[inline(never)];
445
446     let buf_ptr = vec::raw::to_ptr(buf_in);
447     let buf_cnt = buf_in.len() as i32;
448     return rust_uv_udp_send6(req, handle as *c_void, buf_ptr, buf_cnt, addr, cb);
449 }
450
451 pub unsafe fn get_udp_handle_from_send_req(send_req: *uv_udp_send_t) -> *uv_udp_t {
452     #[fixed_stack_segment]; #[inline(never)];
453
454     return rust_uv_get_udp_handle_from_send_req(send_req);
455 }
456
457 pub unsafe fn udp_getsockname(handle: *uv_udp_t, name: *sockaddr_storage) -> c_int {
458     #[fixed_stack_segment]; #[inline(never)];
459
460     return rust_uv_udp_getsockname(handle, name);
461 }
462
463 pub unsafe fn tcp_connect(connect_ptr: *uv_connect_t, tcp_handle_ptr: *uv_tcp_t,
464                           addr_ptr: *sockaddr_in, after_connect_cb: uv_connect_cb) -> c_int {
465     #[fixed_stack_segment]; #[inline(never)];
466
467     return rust_uv_tcp_connect(connect_ptr, tcp_handle_ptr, after_connect_cb, addr_ptr);
468 }
469
470 pub unsafe fn tcp_connect6(connect_ptr: *uv_connect_t, tcp_handle_ptr: *uv_tcp_t,
471                            addr_ptr: *sockaddr_in6, after_connect_cb: uv_connect_cb) -> c_int {
472     #[fixed_stack_segment]; #[inline(never)];
473
474     return rust_uv_tcp_connect6(connect_ptr, tcp_handle_ptr, after_connect_cb, addr_ptr);
475 }
476
477 pub unsafe fn tcp_bind(tcp_server_ptr: *uv_tcp_t, addr_ptr: *sockaddr_in) -> c_int {
478     #[fixed_stack_segment]; #[inline(never)];
479
480     return rust_uv_tcp_bind(tcp_server_ptr, addr_ptr);
481 }
482
483 pub unsafe fn tcp_bind6(tcp_server_ptr: *uv_tcp_t, addr_ptr: *sockaddr_in6) -> c_int {
484     #[fixed_stack_segment]; #[inline(never)];
485
486     return rust_uv_tcp_bind6(tcp_server_ptr, addr_ptr);
487 }
488
489 pub unsafe fn tcp_getpeername(tcp_handle_ptr: *uv_tcp_t, name: *sockaddr_storage) -> c_int {
490     #[fixed_stack_segment]; #[inline(never)];
491
492     return rust_uv_tcp_getpeername(tcp_handle_ptr, name);
493 }
494
495 pub unsafe fn tcp_getsockname(handle: *uv_tcp_t, name: *sockaddr_storage) -> c_int {
496     #[fixed_stack_segment]; #[inline(never)];
497
498     return rust_uv_tcp_getsockname(handle, name);
499 }
500
501 pub unsafe fn uv_write(req: *uv_write_t,
502                        stream: *uv_stream_t,
503                        buf_in: &[uv_buf_t],
504                        cb: uv_write_cb) -> c_int {
505     externfn!(fn uv_write(req: *uv_write_t, stream: *uv_stream_t,
506                           buf_in: *uv_buf_t, buf_cnt: c_int,
507                           cb: uv_write_cb) -> c_int)
508
509     let buf_ptr = vec::raw::to_ptr(buf_in);
510     let buf_cnt = buf_in.len() as i32;
511     return uv_write(req, stream, buf_ptr, buf_cnt, cb);
512 }
513
514 pub unsafe fn is_ip4_addr(addr: *sockaddr) -> bool {
515     #[fixed_stack_segment]; #[inline(never)];
516
517     match rust_uv_is_ipv4_sockaddr(addr) { 0 => false, _ => true }
518 }
519
520 pub unsafe fn is_ip6_addr(addr: *sockaddr) -> bool {
521     #[fixed_stack_segment]; #[inline(never)];
522
523     match rust_uv_is_ipv6_sockaddr(addr) { 0 => false, _ => true }
524 }
525
526 pub unsafe fn malloc_ip4_addr(ip: &str, port: int) -> *sockaddr_in {
527     #[fixed_stack_segment]; #[inline(never)];
528     do ip.with_c_str |ip_buf| {
529         rust_uv_ip4_addrp(ip_buf as *u8, port as libc::c_int)
530     }
531 }
532 pub unsafe fn malloc_ip6_addr(ip: &str, port: int) -> *sockaddr_in6 {
533     #[fixed_stack_segment]; #[inline(never)];
534     do ip.with_c_str |ip_buf| {
535         rust_uv_ip6_addrp(ip_buf as *u8, port as libc::c_int)
536     }
537 }
538
539 pub unsafe fn malloc_sockaddr_storage() -> *sockaddr_storage {
540     #[fixed_stack_segment]; #[inline(never)];
541
542     rust_uv_malloc_sockaddr_storage()
543 }
544
545 pub unsafe fn free_sockaddr_storage(ss: *sockaddr_storage) {
546     #[fixed_stack_segment]; #[inline(never)];
547
548     rust_uv_free_sockaddr_storage(ss);
549 }
550
551 pub unsafe fn free_ip4_addr(addr: *sockaddr_in) {
552     #[fixed_stack_segment]; #[inline(never)];
553
554     rust_uv_free_ip4_addr(addr);
555 }
556
557 pub unsafe fn free_ip6_addr(addr: *sockaddr_in6) {
558     #[fixed_stack_segment]; #[inline(never)];
559
560     rust_uv_free_ip6_addr(addr);
561 }
562
563 pub unsafe fn ip4_port(addr: *sockaddr_in) -> c_uint {
564     #[fixed_stack_segment]; #[inline(never)];
565
566    return rust_uv_ip4_port(addr);
567 }
568
569 pub unsafe fn ip6_port(addr: *sockaddr_in6) -> c_uint {
570     #[fixed_stack_segment]; #[inline(never)];
571
572     return rust_uv_ip6_port(addr);
573 }
574
575 pub unsafe fn process_pid(p: *uv_process_t) -> c_int {
576     #[fixed_stack_segment]; #[inline(never)];
577     return rust_uv_process_pid(p);
578 }
579
580 pub unsafe fn set_stdio_container_flags(c: *uv_stdio_container_t,
581                                         flags: libc::c_int) {
582     #[fixed_stack_segment]; #[inline(never)];
583     rust_set_stdio_container_flags(c, flags);
584 }
585
586 pub unsafe fn set_stdio_container_fd(c: *uv_stdio_container_t,
587                                      fd: libc::c_int) {
588     #[fixed_stack_segment]; #[inline(never)];
589     rust_set_stdio_container_fd(c, fd);
590 }
591
592 pub unsafe fn set_stdio_container_stream(c: *uv_stdio_container_t,
593                                          stream: *uv_stream_t) {
594     #[fixed_stack_segment]; #[inline(never)];
595     rust_set_stdio_container_stream(c, stream);
596 }
597
598 // data access helpers
599 pub unsafe fn get_result_from_fs_req(req: *uv_fs_t) -> c_int {
600     #[fixed_stack_segment]; #[inline(never)];
601
602     rust_uv_get_result_from_fs_req(req)
603 }
604 pub unsafe fn get_ptr_from_fs_req(req: *uv_fs_t) -> *libc::c_void {
605     #[fixed_stack_segment]; #[inline(never)];
606
607     rust_uv_get_ptr_from_fs_req(req)
608 }
609 pub unsafe fn get_path_from_fs_req(req: *uv_fs_t) -> *c_char {
610     #[fixed_stack_segment]; #[inline(never)];
611
612     rust_uv_get_path_from_fs_req(req)
613 }
614 pub unsafe fn get_loop_from_fs_req(req: *uv_fs_t) -> *uv_loop_t {
615     #[fixed_stack_segment]; #[inline(never)];
616
617     rust_uv_get_loop_from_fs_req(req)
618 }
619 pub unsafe fn get_loop_from_getaddrinfo_req(req: *uv_getaddrinfo_t) -> *uv_loop_t {
620     #[fixed_stack_segment]; #[inline(never)];
621
622     rust_uv_get_loop_from_getaddrinfo_req(req)
623 }
624 pub unsafe fn get_loop_for_uv_handle<T>(handle: *T) -> *c_void {
625     #[fixed_stack_segment]; #[inline(never)];
626
627     return rust_uv_get_loop_for_uv_handle(handle as *c_void);
628 }
629 pub unsafe fn get_stream_handle_from_connect_req(connect: *uv_connect_t) -> *uv_stream_t {
630     #[fixed_stack_segment]; #[inline(never)];
631
632     return rust_uv_get_stream_handle_from_connect_req(connect);
633 }
634 pub unsafe fn get_stream_handle_from_write_req(write_req: *uv_write_t) -> *uv_stream_t {
635     #[fixed_stack_segment]; #[inline(never)];
636
637     return rust_uv_get_stream_handle_from_write_req(write_req);
638 }
639 pub unsafe fn get_data_for_uv_loop(loop_ptr: *c_void) -> *c_void {
640     #[fixed_stack_segment]; #[inline(never)];
641
642     rust_uv_get_data_for_uv_loop(loop_ptr)
643 }
644 pub unsafe fn set_data_for_uv_loop(loop_ptr: *c_void, data: *c_void) {
645     #[fixed_stack_segment]; #[inline(never)];
646
647     rust_uv_set_data_for_uv_loop(loop_ptr, data);
648 }
649 pub unsafe fn get_data_for_uv_handle<T>(handle: *T) -> *c_void {
650     #[fixed_stack_segment]; #[inline(never)];
651
652     return rust_uv_get_data_for_uv_handle(handle as *c_void);
653 }
654 pub unsafe fn set_data_for_uv_handle<T, U>(handle: *T, data: *U) {
655     #[fixed_stack_segment]; #[inline(never)];
656
657     rust_uv_set_data_for_uv_handle(handle as *c_void, data as *c_void);
658 }
659 pub unsafe fn get_data_for_req<T>(req: *T) -> *c_void {
660     #[fixed_stack_segment]; #[inline(never)];
661
662     return rust_uv_get_data_for_req(req as *c_void);
663 }
664 pub unsafe fn set_data_for_req<T, U>(req: *T, data: *U) {
665     #[fixed_stack_segment]; #[inline(never)];
666
667     rust_uv_set_data_for_req(req as *c_void, data as *c_void);
668 }
669 pub unsafe fn populate_stat(req_in: *uv_fs_t, stat_out: *uv_stat_t) {
670     #[fixed_stack_segment]; #[inline(never)];
671
672     rust_uv_populate_uv_stat(req_in, stat_out)
673 }
674 pub unsafe fn guess_handle(handle: c_int) -> c_int {
675     #[fixed_stack_segment]; #[inline(never)];
676
677     rust_uv_guess_handle(handle)
678 }
679
680
681 // uv_support is the result of compiling rust_uv.cpp
682 #[link_args = "-luv_support -luv"]
683 extern {
684     fn rust_uv_loop_new() -> *c_void;
685
686     fn rust_uv_handle_type_max() -> uintptr_t;
687     fn rust_uv_req_type_max() -> uintptr_t;
688     fn rust_uv_ip4_addrp(ip: *u8, port: c_int) -> *sockaddr_in;
689     fn rust_uv_ip6_addrp(ip: *u8, port: c_int) -> *sockaddr_in6;
690     fn rust_uv_free_ip4_addr(addr: *sockaddr_in);
691     fn rust_uv_free_ip6_addr(addr: *sockaddr_in6);
692     fn rust_uv_ip4_port(src: *sockaddr_in) -> c_uint;
693     fn rust_uv_ip6_port(src: *sockaddr_in6) -> c_uint;
694     fn rust_uv_tcp_connect(req: *uv_connect_t, handle: *uv_tcp_t,
695                            cb: uv_connect_cb,
696                            addr: *sockaddr_in) -> c_int;
697     fn rust_uv_tcp_bind(tcp_server: *uv_tcp_t, addr: *sockaddr_in) -> c_int;
698     fn rust_uv_tcp_connect6(req: *uv_connect_t, handle: *uv_tcp_t,
699                             cb: uv_connect_cb,
700                             addr: *sockaddr_in6) -> c_int;
701     fn rust_uv_tcp_bind6(tcp_server: *uv_tcp_t, addr: *sockaddr_in6) -> c_int;
702     fn rust_uv_tcp_getpeername(tcp_handle_ptr: *uv_tcp_t, name: *sockaddr_storage) -> c_int;
703     fn rust_uv_tcp_getsockname(handle: *uv_tcp_t, name: *sockaddr_storage) -> c_int;
704     fn rust_uv_udp_bind(server: *uv_udp_t, addr: *sockaddr_in, flags: c_uint) -> c_int;
705     fn rust_uv_udp_bind6(server: *uv_udp_t, addr: *sockaddr_in6, flags: c_uint) -> c_int;
706     fn rust_uv_udp_send(req: *uv_udp_send_t, handle: *uv_udp_t, buf_in: *uv_buf_t,
707                         buf_cnt: c_int, addr: *sockaddr_in, cb: uv_udp_send_cb) -> c_int;
708     fn rust_uv_udp_send6(req: *uv_udp_send_t, handle: *uv_udp_t, buf_in: *uv_buf_t,
709                          buf_cnt: c_int, addr: *sockaddr_in6, cb: uv_udp_send_cb) -> c_int;
710     fn rust_uv_get_udp_handle_from_send_req(req: *uv_udp_send_t) -> *uv_udp_t;
711     fn rust_uv_udp_getsockname(handle: *uv_udp_t, name: *sockaddr_storage) -> c_int;
712     fn rust_uv_is_ipv4_sockaddr(addr: *sockaddr) -> c_int;
713     fn rust_uv_is_ipv6_sockaddr(addr: *sockaddr) -> c_int;
714     fn rust_uv_malloc_sockaddr_storage() -> *sockaddr_storage;
715     fn rust_uv_free_sockaddr_storage(ss: *sockaddr_storage);
716     fn rust_uv_populate_uv_stat(req_in: *uv_fs_t, stat_out: *uv_stat_t);
717     fn rust_uv_get_result_from_fs_req(req: *uv_fs_t) -> c_int;
718     fn rust_uv_get_ptr_from_fs_req(req: *uv_fs_t) -> *libc::c_void;
719     fn rust_uv_get_path_from_fs_req(req: *uv_fs_t) -> *c_char;
720     fn rust_uv_get_loop_from_fs_req(req: *uv_fs_t) -> *uv_loop_t;
721     fn rust_uv_get_loop_from_getaddrinfo_req(req: *uv_fs_t) -> *uv_loop_t;
722     fn rust_uv_get_stream_handle_from_connect_req(req: *uv_connect_t) -> *uv_stream_t;
723     fn rust_uv_get_stream_handle_from_write_req(req: *uv_write_t) -> *uv_stream_t;
724     fn rust_uv_get_loop_for_uv_handle(handle: *c_void) -> *c_void;
725     fn rust_uv_get_data_for_uv_loop(loop_ptr: *c_void) -> *c_void;
726     fn rust_uv_set_data_for_uv_loop(loop_ptr: *c_void, data: *c_void);
727     fn rust_uv_get_data_for_uv_handle(handle: *c_void) -> *c_void;
728     fn rust_uv_set_data_for_uv_handle(handle: *c_void, data: *c_void);
729     fn rust_uv_get_data_for_req(req: *c_void) -> *c_void;
730     fn rust_uv_set_data_for_req(req: *c_void, data: *c_void);
731     fn rust_set_stdio_container_flags(c: *uv_stdio_container_t, flags: c_int);
732     fn rust_set_stdio_container_fd(c: *uv_stdio_container_t, fd: c_int);
733     fn rust_set_stdio_container_stream(c: *uv_stdio_container_t,
734                                        stream: *uv_stream_t);
735     fn rust_uv_process_pid(p: *uv_process_t) -> c_int;
736     fn rust_uv_guess_handle(fd: c_int) -> c_int;
737 }
738
739 // generic uv functions
740 externfn!(fn uv_loop_delete(l: *uv_loop_t))
741 externfn!(fn uv_handle_size(ty: uv_handle_type) -> size_t)
742 externfn!(fn uv_req_size(ty: uv_req_type) -> size_t)
743 externfn!(fn uv_run(l: *uv_loop_t, mode: uv_run_mode) -> c_int)
744 externfn!(fn uv_close(h: *uv_handle_t, cb: uv_close_cb))
745 externfn!(fn uv_walk(l: *uv_loop_t, cb: uv_walk_cb, arg: *c_void))
746 externfn!(fn uv_buf_init(base: *c_char, len: c_uint) -> uv_buf_t)
747 externfn!(fn uv_strerror(err: c_int) -> *c_char)
748 externfn!(fn uv_err_name(err: c_int) -> *c_char)
749 externfn!(fn uv_listen(s: *uv_stream_t, backlog: c_int,
750                        cb: uv_connection_cb) -> c_int)
751 externfn!(fn uv_accept(server: *uv_stream_t, client: *uv_stream_t) -> c_int)
752 externfn!(fn uv_read_start(stream: *uv_stream_t,
753                            on_alloc: uv_alloc_cb,
754                            on_read: uv_read_cb) -> c_int)
755 externfn!(fn uv_read_stop(stream: *uv_stream_t) -> c_int)
756
757 // idle bindings
758 externfn!(fn uv_idle_init(l: *uv_loop_t, i: *uv_idle_t) -> c_int)
759 externfn!(fn uv_idle_start(i: *uv_idle_t, cb: uv_idle_cb) -> c_int)
760 externfn!(fn uv_idle_stop(i: *uv_idle_t) -> c_int)
761
762 // async bindings
763 externfn!(fn uv_async_init(l: *uv_loop_t, a: *uv_async_t,
764                            cb: uv_async_cb) -> c_int)
765 externfn!(fn uv_async_send(a: *uv_async_t))
766
767 // tcp bindings
768 externfn!(fn uv_tcp_init(l: *uv_loop_t, h: *uv_tcp_t) -> c_int)
769 externfn!(fn uv_ip4_name(src: *sockaddr_in, dst: *c_char,
770                          size: size_t) -> c_int)
771 externfn!(fn uv_ip6_name(src: *sockaddr_in6, dst: *c_char,
772                          size: size_t) -> c_int)
773 externfn!(fn uv_tcp_nodelay(h: *uv_tcp_t, enable: c_int) -> c_int)
774 externfn!(fn uv_tcp_keepalive(h: *uv_tcp_t, enable: c_int,
775                               delay: c_uint) -> c_int)
776 externfn!(fn uv_tcp_simultaneous_accepts(h: *uv_tcp_t, enable: c_int) -> c_int)
777
778 // udp bindings
779 externfn!(fn uv_udp_init(l: *uv_loop_t, h: *uv_udp_t) -> c_int)
780 externfn!(fn uv_udp_recv_start(server: *uv_udp_t,
781                                on_alloc: uv_alloc_cb,
782                                on_recv: uv_udp_recv_cb) -> c_int)
783 externfn!(fn uv_udp_set_membership(handle: *uv_udp_t, multicast_addr: *c_char,
784                                    interface_addr: *c_char,
785                                    membership: uv_membership) -> c_int)
786 externfn!(fn uv_udp_recv_stop(server: *uv_udp_t) -> c_int)
787 externfn!(fn uv_udp_set_multicast_loop(handle: *uv_udp_t, on: c_int) -> c_int)
788 externfn!(fn uv_udp_set_multicast_ttl(handle: *uv_udp_t, ttl: c_int) -> c_int)
789 externfn!(fn uv_udp_set_ttl(handle: *uv_udp_t, ttl: c_int) -> c_int)
790 externfn!(fn uv_udp_set_broadcast(handle: *uv_udp_t, on: c_int) -> c_int)
791
792 // timer bindings
793 externfn!(fn uv_timer_init(l: *uv_loop_t, t: *uv_timer_t) -> c_int)
794 externfn!(fn uv_timer_start(t: *uv_timer_t, cb: uv_timer_cb,
795                             timeout: libc::uint64_t,
796                             repeat: libc::uint64_t) -> c_int)
797 externfn!(fn uv_timer_stop(handle: *uv_timer_t) -> c_int)
798
799 // fs operations
800 externfn!(fn uv_fs_open(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char,
801                         flags: c_int, mode: c_int, cb: uv_fs_cb) -> c_int)
802 externfn!(fn uv_fs_unlink(loop_ptr: *uv_loop_t, req: *uv_fs_t, path: *c_char,
803                           cb: uv_fs_cb) -> c_int)
804 externfn!(fn uv_fs_write(l: *uv_loop_t, req: *uv_fs_t, fd: c_int, buf: *c_void,
805                          len: c_uint, offset: i64, cb: uv_fs_cb) -> c_int)
806 externfn!(fn uv_fs_read(l: *uv_loop_t, req: *uv_fs_t, fd: c_int, buf: *c_void,
807                         len: c_uint, offset: i64, cb: uv_fs_cb) -> c_int)
808 externfn!(fn uv_fs_close(l: *uv_loop_t, req: *uv_fs_t, fd: c_int,
809                          cb: uv_fs_cb) -> c_int)
810 externfn!(fn uv_fs_stat(l: *uv_loop_t, req: *uv_fs_t, path: *c_char,
811                         cb: uv_fs_cb) -> c_int)
812 externfn!(fn uv_fs_fstat(l: *uv_loop_t, req: *uv_fs_t, fd: c_int,
813                          cb: uv_fs_cb) -> c_int)
814 externfn!(fn uv_fs_mkdir(l: *uv_loop_t, req: *uv_fs_t, path: *c_char,
815                          mode: c_int, cb: uv_fs_cb) -> c_int)
816 externfn!(fn uv_fs_rmdir(l: *uv_loop_t, req: *uv_fs_t, path: *c_char,
817                          cb: uv_fs_cb) -> c_int)
818 externfn!(fn uv_fs_readdir(l: *uv_loop_t, req: *uv_fs_t, path: *c_char,
819                            flags: c_int, cb: uv_fs_cb) -> c_int)
820 externfn!(fn uv_fs_req_cleanup(req: *uv_fs_t))
821 externfn!(fn uv_fs_fsync(handle: *uv_loop_t, req: *uv_fs_t, file: c_int,
822                          cb: uv_fs_cb) -> c_int)
823 externfn!(fn uv_fs_fdatasync(handle: *uv_loop_t, req: *uv_fs_t, file: c_int,
824                              cb: uv_fs_cb) -> c_int)
825 externfn!(fn uv_fs_ftruncate(handle: *uv_loop_t, req: *uv_fs_t, file: c_int,
826                              offset: i64, cb: uv_fs_cb) -> c_int)
827 externfn!(fn uv_fs_readlink(handle: *uv_loop_t, req: *uv_fs_t, file: *c_char,
828                             cb: uv_fs_cb) -> c_int)
829 externfn!(fn uv_fs_symlink(handle: *uv_loop_t, req: *uv_fs_t, src: *c_char,
830                            dst: *c_char, flags: c_int, cb: uv_fs_cb) -> c_int)
831 externfn!(fn uv_fs_rename(handle: *uv_loop_t, req: *uv_fs_t, src: *c_char,
832                           dst: *c_char, cb: uv_fs_cb) -> c_int)
833 externfn!(fn uv_fs_utime(handle: *uv_loop_t, req: *uv_fs_t, path: *c_char,
834                          atime: c_double, mtime: c_double,
835                          cb: uv_fs_cb) -> c_int)
836 externfn!(fn uv_fs_link(handle: *uv_loop_t, req: *uv_fs_t, src: *c_char,
837                         dst: *c_char, cb: uv_fs_cb) -> c_int)
838 externfn!(fn uv_fs_chown(handle: *uv_loop_t, req: *uv_fs_t, src: *c_char,
839                          uid: uv_uid_t, gid: uv_gid_t, cb: uv_fs_cb) -> c_int)
840 externfn!(fn uv_fs_chmod(handle: *uv_loop_t, req: *uv_fs_t, path: *c_char,
841                          mode: c_int, cb: uv_fs_cb) -> c_int)
842 externfn!(fn uv_fs_lstat(handle: *uv_loop_t, req: *uv_fs_t, file: *c_char,
843                          cb: uv_fs_cb) -> c_int)
844
845 // getaddrinfo
846 externfn!(fn uv_getaddrinfo(loop_: *uv_loop_t, req: *uv_getaddrinfo_t,
847                             getaddrinfo_cb: uv_getaddrinfo_cb,
848                             node: *c_char, service: *c_char,
849                             hints: *addrinfo) -> c_int)
850 externfn!(fn uv_freeaddrinfo(ai: *addrinfo))
851
852 // process spawning
853 externfn!(fn uv_spawn(loop_ptr: *uv_loop_t, outptr: *uv_process_t,
854                       options: uv_process_options_t) -> c_int)
855 externfn!(fn uv_process_kill(p: *uv_process_t, signum: c_int) -> c_int)
856
857 // pipes
858 externfn!(fn uv_pipe_init(l: *uv_loop_t, p: *uv_pipe_t, ipc: c_int) -> c_int)
859 externfn!(fn uv_pipe_open(pipe: *uv_pipe_t, file: c_int) -> c_int)
860 externfn!(fn uv_pipe_bind(pipe: *uv_pipe_t, name: *c_char) -> c_int)
861 externfn!(fn uv_pipe_connect(req: *uv_connect_t, handle: *uv_pipe_t,
862                              name: *c_char, cb: uv_connect_cb))
863
864 // tty
865 externfn!(fn uv_tty_init(l: *uv_loop_t, tty: *uv_tty_t, fd: c_int,
866                          readable: c_int) -> c_int)
867 externfn!(fn uv_tty_set_mode(tty: *uv_tty_t, mode: c_int) -> c_int)
868 externfn!(fn uv_tty_get_winsize(tty: *uv_tty_t, width: *c_int,
869                                 height: *c_int) -> c_int)
870
871 // signals
872 externfn!(fn uv_signal_init(loop_: *uv_loop_t, handle: *uv_signal_t) -> c_int)
873 externfn!(fn uv_signal_start(h: *uv_signal_t, cb: uv_signal_cb,
874                              signum: c_int) -> c_int)
875 externfn!(fn uv_signal_stop(handle: *uv_signal_t) -> c_int)
876
877 // libuv requires various system libraries to successfully link on some
878 // platforms
879 #[cfg(target_os = "linux")]
880 #[link_args = "-lpthread"]
881 extern {}
882
883 #[cfg(target_os = "win32")]
884 #[link_args = "-lWs2_32 -lpsapi -liphlpapi"]
885 extern {}