Use safe nix API instead of libc (#12315)

# Description
Where possible, this PR replaces usages of raw `libc` bindings to
instead use safe interfaces from the `nix` crate. Where not possible,
the `libc` version reexported through `nix` was used instead of having a
separate `libc` dependency.
This commit is contained in:
Ian Manske
2024-03-30 13:49:54 +00:00
committed by GitHub
parent 714a0ccd24
commit 251599c507
8 changed files with 103 additions and 128 deletions

View File

@ -71,7 +71,7 @@ impl LazySystemInfoRecord {
) -> Result<Value, ShellError> {
let pid = Pid::from(std::process::id() as usize);
match column {
"thread_id" => Ok(Value::int(get_thread_id(), self.span)),
"thread_id" => Ok(Value::int(get_thread_id() as i64, self.span)),
"pid" => Ok(Value::int(pid.as_u32() as i64, self.span)),
"ppid" => {
// only get information requested
@ -261,13 +261,13 @@ impl<'a, F: Fn() -> RefreshKind> From<(Option<&'a System>, F)> for SystemOpt<'a>
}
}
fn get_thread_id() -> i64 {
#[cfg(target_family = "windows")]
fn get_thread_id() -> u64 {
#[cfg(windows)]
{
unsafe { windows::Win32::System::Threading::GetCurrentThreadId() as i64 }
unsafe { windows::Win32::System::Threading::GetCurrentThreadId().into() }
}
#[cfg(not(target_family = "windows"))]
#[cfg(unix)]
{
unsafe { libc::pthread_self() as i64 }
nix::sys::pthread::pthread_self() as u64
}
}