/rust/registry/src/index.crates.io-1949cf8c6b5b557f/sysinfo-0.37.2/src/unix/utils.rs
Line | Count | Source |
1 | | // Take a look at the license at the top of the repository in the LICENSE file. |
2 | | |
3 | | #[cfg(feature = "user")] |
4 | | pub(crate) fn cstr_to_rust(c: *const libc::c_char) -> Option<String> { |
5 | | cstr_to_rust_with_size(c, None) |
6 | | } |
7 | | |
8 | | #[cfg(any(feature = "disk", feature = "system", feature = "user"))] |
9 | | #[allow(dead_code)] |
10 | 0 | pub(crate) fn cstr_to_rust_with_size( |
11 | 0 | c: *const libc::c_char, |
12 | 0 | size: Option<usize>, |
13 | 0 | ) -> Option<String> { |
14 | 0 | if c.is_null() { |
15 | 0 | return None; |
16 | 0 | } |
17 | 0 | let (mut s, max) = match size { |
18 | 0 | Some(len) => (Vec::with_capacity(len), len as isize), |
19 | 0 | None => (Vec::new(), isize::MAX), |
20 | | }; |
21 | 0 | let mut i = 0; |
22 | | unsafe { |
23 | | loop { |
24 | 0 | let value = *c.offset(i) as u8; |
25 | 0 | if value == 0 { |
26 | 0 | break; |
27 | 0 | } |
28 | 0 | s.push(value); |
29 | 0 | i += 1; |
30 | 0 | if i >= max { |
31 | 0 | break; |
32 | 0 | } |
33 | | } |
34 | 0 | String::from_utf8(s).ok() |
35 | | } |
36 | 0 | } |
37 | | |
38 | | #[cfg(all( |
39 | | feature = "system", |
40 | | not(any( |
41 | | target_os = "ios", |
42 | | all(target_os = "macos", feature = "apple-sandbox",) |
43 | | )) |
44 | | ))] |
45 | 0 | pub(crate) fn wait_process(pid: crate::Pid) -> Option<std::process::ExitStatus> { |
46 | | use std::os::unix::process::ExitStatusExt; |
47 | | |
48 | 0 | let mut status = 0; |
49 | | // attempt waiting |
50 | | unsafe { |
51 | 0 | if retry_eintr!(libc::waitpid(pid.0, &mut status, 0)) < 0 { |
52 | | // attempt failed (non-child process) so loop until process ends |
53 | 0 | let duration = std::time::Duration::from_millis(10); |
54 | 0 | while libc::kill(pid.0, 0) == 0 { |
55 | 0 | std::thread::sleep(duration); |
56 | 0 | } |
57 | 0 | } |
58 | 0 | Some(std::process::ExitStatus::from_raw(status)) |
59 | | } |
60 | 0 | } |