Open japinli opened 4 months ago
Here is a POC patch to fix this bug:
diff --git a/src/ffi.rs b/src/ffi.rs
index ca22a25..0a060d1 100644
--- a/src/ffi.rs
+++ b/src/ffi.rs
@@ -74,8 +74,9 @@ impl kstat_named_t {
let cstr = unsafe { CStr::from_ptr(self.name.as_ptr()) };
cstr.to_string_lossy()
}
- pub fn value_as_char(&self) -> c_char {
- c_char::from_le(self.value[0] as i8)
+ pub fn value_as_str(&self) -> String {
+ let cstr = unsafe { CStr::from_ptr(self.value.as_ptr() as *const c_char) };
+ cstr.to_string_lossy().into_owned()
}
pub fn value_as_i32(&self) -> i32 {
diff --git a/src/kstat_named.rs b/src/kstat_named.rs
index 96bbebf..bfbdfbf 100644
--- a/src/kstat_named.rs
+++ b/src/kstat_named.rs
@@ -4,8 +4,6 @@ use std::borrow::Cow;
/// The types of data a kstat named/value pair can contain
#[derive(Debug)]
pub enum KstatNamedData {
- /// KSTAT_DATA_CHAR
- DataChar(i8),
/// KSTAT_DATA_INT32
DataInt32(i32),
/// KSTAT_DATA_UINT32
@@ -14,7 +12,7 @@ pub enum KstatNamedData {
DataInt64(i64),
/// KSTAT_DATA_UINT64 or KSTAT_DATA_ULONG
DataUInt64(u64),
- /// KSTAT_DATA_STRING
+ /// KSTAT_DATA_CHAR or KSTAT_DATA_STRING
DataString(String),
}
@@ -44,7 +42,9 @@ impl KstatNamed {
impl<'a> From<&'a KstatNamed> for KstatNamedData {
fn from(t: &'a KstatNamed) -> Self {
match t.get_data_type() {
- ffi::KSTAT_DATA_CHAR => KstatNamedData::DataChar(unsafe { (*t.inner).value_as_char() }),
+ ffi::KSTAT_DATA_CHAR => {
+ KstatNamedData::DataString(unsafe { (*t.inner).value_as_str() })
+ }
ffi::KSTAT_DATA_INT32 => {
KstatNamedData::DataInt32(unsafe { (*t.inner).value_as_i32() })
}
Hi,
When I try to use the following code to retrive CPU info, I find there is a bug about the
KstatNameData::DataChar(i8)
:Here is the output:
Note that instance 61 and 62, both of them have
state: DataChar(111)
, however, they are not in the same state (61 on-line, 62 off-line), see:The instance 64 in the spare state has a different
DataChar(115)
that begins withs
character.The bug caused by rust-kstat only fetches the first character for KSTAT_DATA_CHAR, see [1] and [2], however, illumos use 16-byte for KSTAT_DATA_CHAR, see [3].
[1] https://github.com/papertigers/rust-kstat/blob/master/src/kstat_named.rs#L6 [2] https://github.com/papertigers/rust-kstat/blob/master/src/ffi.rs#L77 [3] https://github.com/illumos/illumos-gate/blob/master/usr/src/cmd/stat/kstat/kstat.h#L49