diff options
author | cions <gh.cions@gmail.com> | 2024-01-10 22:04:14 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-10 06:04:14 -0700 |
commit | 881a62869db13199eff30231d4299d36faf143df (patch) | |
tree | e739decaa38309910485c0808b25df9a1e560534 /runtime/ops/os/sys_info.rs | |
parent | 1bafde9cd03f42f490cf0e6e504a5606ac6b634e (diff) |
fix: android support (#19437)
<!--
Before submitting a PR, please read https://deno.com/manual/contributing
1. Give the PR a descriptive title.
Examples of good title:
- fix(std/http): Fix race condition in server
- docs(console): Update docstrings
- feat(doc): Handle nested reexports
Examples of bad title:
- fix #7123
- update docs
- fix bugs
2. Ensure there is a related issue and it is referenced in the PR text.
3. Ensure there are tests that cover the changes.
4. Ensure `cargo test` passes.
5. Ensure `./tools/format.js` passes without changing files.
6. Ensure `./tools/lint.js` passes.
7. Open as a draft PR if your work is still in progress. The CI won't
run
all steps, but you can add '[ci]' to a commit message to force it to.
8. If you would like to run the benchmarks on the CI, add the 'ci-bench'
label.
-->
---------
Signed-off-by: Matt Mastracci <matthew@mastracci.com>
Co-authored-by: Matt Mastracci <matthew@mastracci.com>
Diffstat (limited to 'runtime/ops/os/sys_info.rs')
-rw-r--r-- | runtime/ops/os/sys_info.rs | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/runtime/ops/os/sys_info.rs b/runtime/ops/os/sys_info.rs index c735ab5ca..40cdc52fa 100644 --- a/runtime/ops/os/sys_info.rs +++ b/runtime/ops/os/sys_info.rs @@ -6,7 +6,7 @@ type LoadAvg = (f64, f64, f64); const DEFAULT_LOADAVG: LoadAvg = (0.0, 0.0, 0.0); pub fn loadavg() -> LoadAvg { - #[cfg(target_os = "linux")] + #[cfg(any(target_os = "android", target_os = "linux"))] { use libc::SI_LOAD_SHIFT; @@ -57,6 +57,22 @@ pub fn os_release() -> String { _ => String::from(""), } } + #[cfg(target_os = "android")] + { + let mut info = std::mem::MaybeUninit::uninit(); + // SAFETY: `info` is a valid pointer to a `libc::utsname` struct. + let res = unsafe { libc::uname(info.as_mut_ptr()) }; + if res != 0 { + return String::from(""); + } + // SAFETY: `uname` returns 0 on success, and `info` is initialized. + let mut info = unsafe { info.assume_init() }; + let len = info.release.len(); + info.release[len - 1] = 0; + // SAFETY: `info.release` is a valid pointer and NUL-terminated. + let c_str = unsafe { std::ffi::CStr::from_ptr(info.release.as_ptr()) }; + c_str.to_string_lossy().into_owned() + } #[cfg(any( target_vendor = "apple", target_os = "freebsd", @@ -198,7 +214,7 @@ pub fn mem_info() -> Option<MemInfo> { swap_total: 0, swap_free: 0, }; - #[cfg(target_os = "linux")] + #[cfg(any(target_os = "android", target_os = "linux"))] { let mut info = std::mem::MaybeUninit::uninit(); // SAFETY: `info` is a valid pointer to a `libc::sysinfo` struct. @@ -343,7 +359,7 @@ pub fn mem_info() -> Option<MemInfo> { pub fn os_uptime() -> u64 { let uptime: u64; - #[cfg(target_os = "linux")] + #[cfg(any(target_os = "android", target_os = "linux"))] { let mut info = std::mem::MaybeUninit::uninit(); // SAFETY: `info` is a valid pointer to a `libc::sysinfo` struct. |