sysctl-rs icon indicating copy to clipboard operation
sysctl-rs copied to clipboard

Question about list of longs

Open rudolphfroger opened this issue 5 years ago • 3 comments

Hi,

First of all, thanks for this library. I'm very new to Rust so this might be a stupid question. I'm trying to read the "kern.cp_time" and "kern.cp_times" Ctl's on FreeBSD. This should be a list of long integers (user, nice, sys, intr, idle). sysctl-rs says it's a Long type value and the value I get is just one i64 value but sysctl kern.cp_time gives me the list of all 5 numbers. Could you point me the way on how to read all 5?

Thanks, Rudolph

rudolphfroger avatar Feb 25 '20 08:02 rudolphfroger

Hi That's a good question! I haven't actually tried to read this myself and this crate does not have built-in support for the array type (yet). No sure this would work but you could try value_as() which is described in the example here https://github.com/johalun/sysctl-rs/blob/master/examples/value_as.rs and with the struct

#[repr(C)]
struct Times {
    times: [libc::c_long;5],
}

johalun avatar Feb 25 '20 22:02 johalun

Thanks, I've already tried that but it seems only the first long is read by sysctl-rs, probably because its type is LU. See line 250 of src/unix/funcs.rs. Before this line we probably have the complete array in val (didn't test) but then we read this into a 64 bit integer and loose the remaining bytes.

I do not feel confident yet to write a patch, I'm currently writing my first lines of rust...

rudolphfroger avatar Feb 26 '20 07:02 rudolphfroger

I looked into the code of the sysctl utility and it seems to assume that all numeric types are potentialy arrays: https://github.com/freebsd/freebsd/blob/master/sbin/sysctl/sysctl.c#L1075-L1134 . Maybe sysctl-rs could do something similar.

rudolphfroger avatar Feb 26 '20 07:02 rudolphfroger