MCPcopy Create free account
hub / github.com/RustCrypto/utils / sysctlbyname

Function sysctlbyname

cpufeatures/src/aarch64.rs:149–181  ·  view source on GitHub ↗
(name: &[u8])

Source from the content-addressed store, hash-verified

147#[cfg(target_vendor = "apple")]
148#[must_use]
149pub unsafe fn sysctlbyname(name: &[u8]) -> bool {
150 assert_eq!(
151 name.last().cloned(),
152 Some(0),
153 "name is not NUL terminated: {:?}",
154 name
155 );
156
157 let mut value: u32 = 0;
158 let mut size = size_of::<u32>();
159
160 // SAFETY:
161 // - `name` is being cast from a valid byte slice we asserted was NUL terminated above.
162 // - `value` is a properly-aligned, writable integer.
163 // - `size` is initialized to the size of `value` (4-bytes).
164 // - The last two arguments,`newp` and `newlen`, are for setting system parameters, which we
165 // aren't doing here (and requires root privileges). The docs say the following:
166 // - `newp`: "Specify NULL if you don’t want to set the attribute’s value"
167 // - `newlen`: "Specify 0 if you don’t want to set the attribute’s value"
168 let rc = unsafe {
169 libc::sysctlbyname(
170 name.as_ptr().cast::<i8>(),
171 (&raw mut value).cast::<libc::c_void>(),
172 &raw mut size,
173 core::ptr::null_mut(),
174 0,
175 )
176 };
177
178 assert_eq!(size, 4, "unexpected sysctlbyname(3) result size");
179 assert_eq!(rc, 0, "sysctlbyname returned error code: {}", rc);
180 value != 0
181}
182
183// On other targets, runtime CPU feature detection is unavailable
184#[cfg(not(any(target_vendor = "apple", target_os = "linux", target_os = "android",)))]

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected