|
| 1 | +fn main() { |
| 2 | + println!("cargo:rerun-if-changed=include/liburing.h"); |
| 3 | + |
| 4 | + let bindings = bindgen::Builder::default() |
| 5 | + // From `liburing/src/include/liburing.h`. |
| 6 | + .header("include/liburing.h") |
| 7 | + .parse_callbacks(Box::new(bindgen::CargoCallbacks)) |
| 8 | + .impl_debug(false) |
| 9 | + .impl_partialeq(false) |
| 10 | + .derive_copy(true) |
| 11 | + .derive_debug(false) |
| 12 | + .derive_default(false) |
| 13 | + .derive_hash(false) |
| 14 | + .derive_partialord(false) |
| 15 | + .derive_ord(false) |
| 16 | + .derive_partialeq(false) |
| 17 | + .derive_eq(false) |
| 18 | + .merge_extern_blocks(true) |
| 19 | + .default_non_copy_union_style(bindgen::NonCopyUnionStyle::ManuallyDrop) |
| 20 | + .prepend_enum_name(false) |
| 21 | + .rustfmt_bindings(true) |
| 22 | + .sort_semantically(true) |
| 23 | + // Limit to io_uring types and constants. |
| 24 | + .allowlist_type("io_uring.*") |
| 25 | + .allowlist_var("IORING.*") |
| 26 | + // We define the function ourselves, since no definitions exist in libc |
| 27 | + // yet (otherwise this wasn't needed at all!). |
| 28 | + .ignore_functions() |
| 29 | + // We'll use the libc definition. |
| 30 | + .blocklist_item("sigset_t") |
| 31 | + // Add our header with the `syscall!` macro and module docs. |
| 32 | + .raw_line(HEADER.trim()) |
| 33 | + .disable_header_comment() |
| 34 | + .generate() |
| 35 | + .expect("failed to generate bindings"); |
| 36 | + |
| 37 | + bindings |
| 38 | + .write_to_file("src/sys.rs") |
| 39 | + .expect("failed to write generated bindings"); |
| 40 | +} |
| 41 | + |
| 42 | +/// Code added at the top of the generated file. |
| 43 | +const HEADER: &str = " |
| 44 | +//! Code that should be moved to libc once C libraries have a wrapper. |
| 45 | +
|
| 46 | +#![allow(dead_code, non_camel_case_types)] |
| 47 | +#![allow(clippy::unreadable_literal, clippy::missing_safety_doc)] |
| 48 | +
|
| 49 | +/// Helper macro to execute a system call that returns an `io::Result`. |
| 50 | +macro_rules! syscall { |
| 51 | + ($fn: ident ( $($arg: expr),* $(,)? ) ) => {{ |
| 52 | + let res = unsafe { libc::$fn($( $arg, )*) }; |
| 53 | + if res == -1 { |
| 54 | + Err(std::io::Error::last_os_error()) |
| 55 | + } else { |
| 56 | + Ok(res) |
| 57 | + } |
| 58 | + }}; |
| 59 | +} |
| 60 | +
|
| 61 | +pub use syscall; |
| 62 | +pub use libc::*; |
| 63 | +
|
| 64 | +pub unsafe fn io_uring_setup(entries: c_uint, p: *mut io_uring_params) -> c_int { |
| 65 | + syscall(SYS_io_uring_setup, entries as c_long, p as c_long) as _ |
| 66 | +} |
| 67 | +
|
| 68 | +pub unsafe fn io_uring_register( |
| 69 | + fd: c_int, |
| 70 | + opcode: c_uint, |
| 71 | + arg: *const c_void, |
| 72 | + nr_args: c_uint, |
| 73 | +) -> c_int { |
| 74 | + syscall( |
| 75 | + SYS_io_uring_register, |
| 76 | + fd as c_long, |
| 77 | + opcode as c_long, |
| 78 | + arg as c_long, |
| 79 | + nr_args as c_long, |
| 80 | + ) as _ |
| 81 | +} |
| 82 | +
|
| 83 | +pub unsafe fn io_uring_enter2( |
| 84 | + fd: c_int, |
| 85 | + to_submit: c_uint, |
| 86 | + min_complete: c_uint, |
| 87 | + flags: c_uint, |
| 88 | + arg: *const libc::c_void, |
| 89 | + size: usize, |
| 90 | +) -> c_int { |
| 91 | + syscall( |
| 92 | + SYS_io_uring_enter, |
| 93 | + fd as c_long, |
| 94 | + to_submit as c_long, |
| 95 | + min_complete as c_long, |
| 96 | + flags as c_long, |
| 97 | + arg as c_long, |
| 98 | + size as c_long, |
| 99 | + ) as _ |
| 100 | +} |
| 101 | +"; |
0 commit comments