base: Handle errors during unix system_info calls
BUG=NONE TEST=./tools/presubmit Change-Id: Ia528b4dcd478be82db3946ee8ef92b9258d895f0 Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/7540107 Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org> Commit-Queue: Cassie Wang <cassiewang@google.com>
This commit is contained in:
parent
d27cad3b00
commit
ea32c72919
1 changed files with 22 additions and 3 deletions
|
|
@ -7,14 +7,23 @@ use libc::_SC_IOV_MAX;
|
|||
use libc::_SC_NPROCESSORS_CONF;
|
||||
use libc::_SC_PAGESIZE;
|
||||
|
||||
use crate::errno_result;
|
||||
use crate::Result;
|
||||
|
||||
// Comes from UIO_MAXIOV in "uio.h"
|
||||
const IOV_MAX_DEFAULT: usize = 1024;
|
||||
|
||||
/// Safe wrapper for `sysconf(_SC_IOV_MAX)`.
|
||||
#[inline(always)]
|
||||
pub fn iov_max() -> usize {
|
||||
// SAFETY:
|
||||
// Trivially safe
|
||||
unsafe { sysconf(_SC_IOV_MAX) as usize }
|
||||
let iov_max = unsafe { sysconf(_SC_IOV_MAX) };
|
||||
if iov_max < 0 {
|
||||
IOV_MAX_DEFAULT
|
||||
} else {
|
||||
iov_max as usize
|
||||
}
|
||||
}
|
||||
|
||||
/// Safe wrapper for `sysconf(_SC_PAGESIZE)`.
|
||||
|
|
@ -22,7 +31,12 @@ pub fn iov_max() -> usize {
|
|||
pub fn pagesize() -> usize {
|
||||
// SAFETY:
|
||||
// Trivially safe
|
||||
unsafe { sysconf(_SC_PAGESIZE) as usize }
|
||||
// man sysconf says return value "Must not be less than 1."
|
||||
let pagesize = unsafe { sysconf(_SC_PAGESIZE) };
|
||||
if pagesize <= 0 {
|
||||
panic!("Error reading system page size, got invalid value.");
|
||||
}
|
||||
pagesize as usize
|
||||
}
|
||||
|
||||
/// Returns the number of online logical cores on the system.
|
||||
|
|
@ -30,5 +44,10 @@ pub fn pagesize() -> usize {
|
|||
pub fn number_of_logical_cores() -> Result<usize> {
|
||||
// SAFETY:
|
||||
// Safe because we pass a flag for this call and the host supports this system call
|
||||
Ok(unsafe { sysconf(_SC_NPROCESSORS_CONF) } as usize)
|
||||
let nprocs_conf = unsafe { sysconf(_SC_NPROCESSORS_CONF) };
|
||||
if nprocs_conf < 0 {
|
||||
errno_result()
|
||||
} else {
|
||||
Ok(nprocs_conf as usize)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue