mirror of
https://github.com/NotAShelf/microfetch.git
synced 2026-04-12 12:57:41 +00:00
microfetch-lib: use home-made syscall wrappers over libc::uname
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I162c771dce2714fa73db3e2e5dc8dcb36a6a6964
This commit is contained in:
parent
f6f1b3003a
commit
99f7be5aac
1 changed files with 15 additions and 13 deletions
28
src/lib.rs
28
src/lib.rs
|
|
@ -5,42 +5,44 @@ pub mod syscall;
|
|||
pub mod system;
|
||||
pub mod uptime;
|
||||
|
||||
use std::mem::MaybeUninit;
|
||||
use std::{ffi::CStr, mem::MaybeUninit};
|
||||
|
||||
/// Wrapper for `libc::utsname` with safe accessor methods
|
||||
pub struct UtsName(libc::utsname);
|
||||
use crate::syscall::{UtsNameBuf, sys_uname};
|
||||
|
||||
/// Wrapper for `utsname` with safe accessor methods
|
||||
pub struct UtsName(UtsNameBuf);
|
||||
|
||||
impl UtsName {
|
||||
/// Calls `uname` syscall and returns a `UtsName` wrapper
|
||||
/// Calls `uname(2)` syscall and returns a `UtsName` wrapper
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns an error if the `uname` syscall fails
|
||||
pub fn uname() -> Result<Self, std::io::Error> {
|
||||
let mut uts = MaybeUninit::uninit();
|
||||
if unsafe { libc::uname(uts.as_mut_ptr()) } != 0 {
|
||||
if unsafe { sys_uname(uts.as_mut_ptr()) } != 0 {
|
||||
return Err(std::io::Error::last_os_error());
|
||||
}
|
||||
Ok(Self(unsafe { uts.assume_init() }))
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn nodename(&self) -> &std::ffi::CStr {
|
||||
unsafe { std::ffi::CStr::from_ptr(self.0.nodename.as_ptr()) }
|
||||
pub const fn nodename(&self) -> &CStr {
|
||||
unsafe { CStr::from_ptr(self.0.nodename.as_ptr()) }
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn sysname(&self) -> &std::ffi::CStr {
|
||||
unsafe { std::ffi::CStr::from_ptr(self.0.sysname.as_ptr()) }
|
||||
pub const fn sysname(&self) -> &CStr {
|
||||
unsafe { CStr::from_ptr(self.0.sysname.as_ptr()) }
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn release(&self) -> &std::ffi::CStr {
|
||||
unsafe { std::ffi::CStr::from_ptr(self.0.release.as_ptr()) }
|
||||
pub const fn release(&self) -> &CStr {
|
||||
unsafe { CStr::from_ptr(self.0.release.as_ptr()) }
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn machine(&self) -> &std::ffi::CStr {
|
||||
unsafe { std::ffi::CStr::from_ptr(self.0.machine.as_ptr()) }
|
||||
pub const fn machine(&self) -> &CStr {
|
||||
unsafe { CStr::from_ptr(self.0.machine.as_ptr()) }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue