diff --git a/Cargo.toml b/Cargo.toml index 5b182f9d..fefd43d0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,5 +3,6 @@ members = [ "proxmox-tools", "proxmox-api", "proxmox-api-macro", + "proxmox-sys", "proxmox", ] diff --git a/proxmox-sys/Cargo.toml b/proxmox-sys/Cargo.toml new file mode 100644 index 00000000..2f575e0c --- /dev/null +++ b/proxmox-sys/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "proxmox-sys" +edition = "2018" +version = "0.1.0" +authors = [ + "Dietmar Maurer ", + "Wolfgang Bumiller ", +] + +[dependencies] +failure = "0.1" +libc = "0.2" +nix = "0.13" +proxmox-tools = { path = "../proxmox-tools" } + +# Docs should be able to reference the proxmox crate. +[dev-dependencies] +proxmox = { path = "../proxmox" } diff --git a/proxmox-sys/src/lib.rs b/proxmox-sys/src/lib.rs new file mode 100644 index 00000000..08b9341e --- /dev/null +++ b/proxmox-sys/src/lib.rs @@ -0,0 +1,4 @@ +//! This is a system utility crate used by all our rust projects. + + +pub mod linux; diff --git a/proxmox-sys/src/linux.rs b/proxmox-sys/src/linux.rs new file mode 100644 index 00000000..e1ca7416 --- /dev/null +++ b/proxmox-sys/src/linux.rs @@ -0,0 +1,38 @@ +//! Linux specific helpers and syscall wrapper + +use failure::*; +use proxmox_tools as tools; + +/// Get pseudo random data (/dev/urandom) +pub fn random_data(size: usize) -> Result, Error> { + + let mut buffer = tools::vec::undefined(size); + fill_with_random_data(&mut buffer)?; + + Ok(buffer) +} + +/// Fill buffer with pseudo random data (/dev/urandom) +/// +/// This code uses the Linux syscall getrandom() - see "man 2 getrandom". +pub fn fill_with_random_data(buffer: &mut [u8]) -> Result<(), Error> { + + let res = unsafe { + libc::syscall( + libc::SYS_getrandom, + buffer.as_mut_ptr(), + buffer.len() as libc::size_t, + 0 as libc::c_uint, + ) + }; + + if res == -1 { + return Err(std::io::Error::last_os_error().into()); + } + + if res as usize != buffer.len() { // should not happen + bail!("short getrandom read"); + } + + Ok(()) +} diff --git a/proxmox/Cargo.toml b/proxmox/Cargo.toml index 441f7e34..0906b6c6 100644 --- a/proxmox/Cargo.toml +++ b/proxmox/Cargo.toml @@ -11,3 +11,4 @@ authors = [ proxmox-api = { path = "../proxmox-api" } proxmox-api-macro = { path = "../proxmox-api-macro" } proxmox-tools = { path = "../proxmox-tools" } +proxmox-sys = { path = "../proxmox-sys" } diff --git a/proxmox/src/lib.rs b/proxmox/src/lib.rs index c2124297..91244374 100644 --- a/proxmox/src/lib.rs +++ b/proxmox/src/lib.rs @@ -1,4 +1,5 @@ pub use proxmox_tools as tools; +pub use proxmox_sys as sys; // Both `proxmox_api` and the 2 macros from `proxmox_api_macro` should be // exposed via `proxmox::api`.