diff options
Diffstat (limited to '')
-rw-r--r-- | third_party/rust/whatsys/.cargo-checksum.json | 1 | ||||
-rw-r--r-- | third_party/rust/whatsys/Cargo.lock | 30 | ||||
-rw-r--r-- | third_party/rust/whatsys/Cargo.toml | 37 | ||||
-rw-r--r-- | third_party/rust/whatsys/LICENSE | 21 | ||||
-rw-r--r-- | third_party/rust/whatsys/README.md | 35 | ||||
-rw-r--r-- | third_party/rust/whatsys/build.rs | 17 | ||||
-rw-r--r-- | third_party/rust/whatsys/c/windows.c | 86 | ||||
-rw-r--r-- | third_party/rust/whatsys/examples/version.rs | 5 | ||||
-rw-r--r-- | third_party/rust/whatsys/src/apple.rs | 61 | ||||
-rw-r--r-- | third_party/rust/whatsys/src/fallback.rs | 6 | ||||
-rw-r--r-- | third_party/rust/whatsys/src/lib.rs | 70 | ||||
-rw-r--r-- | third_party/rust/whatsys/src/linux.rs | 33 | ||||
-rw-r--r-- | third_party/rust/whatsys/src/windows.rs | 53 |
13 files changed, 455 insertions, 0 deletions
diff --git a/third_party/rust/whatsys/.cargo-checksum.json b/third_party/rust/whatsys/.cargo-checksum.json new file mode 100644 index 0000000000..61b3a7ac60 --- /dev/null +++ b/third_party/rust/whatsys/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.lock":"b14278d1089b157167570210f22c3234d12c49be5dabe7c0684a880a754810ad","Cargo.toml":"d887571e615d1b18d8f8e305189121aef1974dc65c5072fa93d9078236facd1e","LICENSE":"031c799b7549525cc605e8ea4c7f27af841ed86a2c1e88bee8039f8ba08b3bbd","README.md":"966ab1cb3749543d222f932b0b023e38f91bbd7396df84eef44d8b9e75692689","build.rs":"46387085fd9838eb9a36ed4f225668ed3055f2150682c1a9a66031dc43061ad1","c/windows.c":"38905b343f98b57890fdead3355219eed22b3f23bab5c0edeab77ff82cc9ab6f","examples/version.rs":"f7f0d42fc10b72d180cf38b45b722a67604ffb1a42d02db572dbaa6ab00c9c1a","src/apple.rs":"60ef2e17c925991b7578278bea8098c7e1b3680e73ac6c729148608f499031f9","src/fallback.rs":"98f08ebb87778421a86e568f526b053818e07b5701216deca743ded6b0e8a1bb","src/lib.rs":"45998dbf0962635794ad16dbb6fde6322c7b0b1d1e54db77d5a1fc8a471efbf9","src/linux.rs":"4fdb46384c0a81a4d0d82effecf8938e69f096255aa7eee1c2a4ab9b462178b5","src/windows.rs":"a2c71952b0bf99dcdc896f9b8d82adacaf1f036b906229c3695197bf626ce4c8"},"package":"bb632c0076024630111a08ca9fcbd34736c80d10b9ae517077487b0c82f46a36"}
\ No newline at end of file diff --git a/third_party/rust/whatsys/Cargo.lock b/third_party/rust/whatsys/Cargo.lock new file mode 100644 index 0000000000..aa0a34c17b --- /dev/null +++ b/third_party/rust/whatsys/Cargo.lock @@ -0,0 +1,30 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cc" +version = "1.0.59" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66120af515773fb005778dc07c261bd201ec8ce50bd6e7144c927753fe013381" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "libc" +version = "0.2.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2f02823cf78b754822df5f7f268fb59822e7296276d3e069d8e8cb26a14bd10" + +[[package]] +name = "whatsys" +version = "0.3.1" +dependencies = [ + "cc", + "cfg-if", + "libc", +] diff --git a/third_party/rust/whatsys/Cargo.toml b/third_party/rust/whatsys/Cargo.toml new file mode 100644 index 0000000000..00963fc356 --- /dev/null +++ b/third_party/rust/whatsys/Cargo.toml @@ -0,0 +1,37 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2018" +name = "whatsys" +version = "0.3.1" +authors = ["Jan-Erik Rediger <janerik@fnordig.de>"] +description = "Determine the kernel version" +readme = "README.md" +license = "MIT" +repository = "https://github.com/badboy/whatsys" + +[package.metadata.docs.rs] +default-target = "x86_64-unknown-linux-gnu" +targets = [ + "x86_64-unknown-linux-gnu", + "x86_64-apple-darwin", + "x86_64-pc-windows-msvc", +] + +[dependencies.cfg-if] +version = "1.0" + +[dependencies.libc] +version = "0.2" + +[build-dependencies.cc] +version = "1.0" diff --git a/third_party/rust/whatsys/LICENSE b/third_party/rust/whatsys/LICENSE new file mode 100644 index 0000000000..d11818a195 --- /dev/null +++ b/third_party/rust/whatsys/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2021-2022 Jan-Erik Rediger + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/third_party/rust/whatsys/README.md b/third_party/rust/whatsys/README.md new file mode 100644 index 0000000000..7c6f308deb --- /dev/null +++ b/third_party/rust/whatsys/README.md @@ -0,0 +1,35 @@ +# `whatsys` -- What's this? + +[![Crates.io version](https://img.shields.io/crates/v/whatsys.svg?style=flat-square)](https://crates.io/crates/whatsys) +[![docs.rs docs](https://img.shields.io/badge/docs-latest-blue.svg?style=flat-square)](https://docs.rs/whatsys) +[![License: MIT](https://img.shields.io/github/license/badboy/whatsys?style=flat-square)](LICENSE) +[![Build Status](https://img.shields.io/github/actions/workflow/status/badboy/whatsys/ci.yml?branch=main&style=flat-square)](https://github.com/badboy/whatsys/actions/workflows/ci.yml?query=workflow%3ACI) + +What kernel version is running? + +# Example + +```rust +let kernel = whatsys::kernel_version(); // E.g. Some("20.3.0") +``` + +# Supported operating systems + +We support the following operating systems: + +* Windows +* macOS +* Linux +* Android + +# License + +MIT. See [LICENSE](LICENSE). + +Based on: + +* [sys-info](https://crates.io/crates/sys-info), [Repository](https://github.com/FillZpp/sys-info-rs), [MIT LICENSE][sys-info-mit] +* [sysinfo](https://crates.io/crates/sysinfo), [Repository](https://github.com/GuillaumeGomez/sysinfo), [MIT LICENSE][sysinfo-mit] + +[sys-info-mit]: https://github.com/FillZpp/sys-info-rs/blob/master/LICENSE +[sysinfo-mit]: https://github.com/GuillaumeGomez/sysinfo/blob/master/LICENSE diff --git a/third_party/rust/whatsys/build.rs b/third_party/rust/whatsys/build.rs new file mode 100644 index 0000000000..75070a4b0d --- /dev/null +++ b/third_party/rust/whatsys/build.rs @@ -0,0 +1,17 @@ +use std::env; + +fn rmain() -> Option<()> { + // Missing? No idea what's going on. + let target_os = env::var("CARGO_CFG_TARGET_OS").ok()?; + if target_os == "windows" { + let mut builder = cc::Build::new(); + builder.file("c/windows.c"); + builder.compile("info"); + } + + Some(()) +} + +fn main() { + let _ = rmain(); +} diff --git a/third_party/rust/whatsys/c/windows.c b/third_party/rust/whatsys/c/windows.c new file mode 100644 index 0000000000..47ce0e63dc --- /dev/null +++ b/third_party/rust/whatsys/c/windows.c @@ -0,0 +1,86 @@ +#include <assert.h> +#include <stdio.h> +#include <string.h> +#include <windows.h> + +/** + * Based on code from sys-info: https://crates.io/crates/sys-info + * Original licenses: MIT + * Original author: Siyu Wang + * License file: https://github.com/FillZpp/sys-info-rs/blob/master/LICENSE + */ + +/** + * Get the OS release version. + * + * This uses [`GetVersionEx`] internally. + * Depending on how the application is compiled + * it might return different values for the same operating system. + * + * On Windows 10 the example will return the Windows 8 OS version value `6.2`. + * If compiled into an application which explicitly targets Windows 10, it will + * return `10.0`. + * + * [`GetVersionEx`]: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getversionexa + * + * ## Arguments + * + * * `outbuf` - Pointer to an allocated buffer of size `outlen`. + * The OS version will be written here, null-terminated. + * If the version is longer than the buffer it will be truncated. + * * `outlen` - Size of the buffer. + * + * ## Return value + * + * Returns the number of bytes written. + */ +int get_os_release(char *outbuf, size_t outlen) { + assert(outlen > 1); + + OSVERSIONINFO osvi; + + ZeroMemory(&osvi, sizeof(osvi)); + osvi.dwOSVersionInfoSize = sizeof(osvi); + + int written = 0; + if (GetVersionEx(&osvi)) { + written = snprintf(outbuf, outlen, "%ld.%ld", osvi.dwMajorVersion, + osvi.dwMinorVersion); + } else { + int res = strncpy_s(outbuf, outlen, "unknown", strlen("unknown")); + if (res != 0) { + return 0; + } + written = strlen("unknown"); + } + + // If the output buffer is smaller than the version (or "unknown"), + // we only wrote until the buffer was full. + return min(written, (int)outlen - 1); +} + +/** + * Get the windows build version. + * + * Works similarly to `get_os_release`, but returns the `dwBuildNumber` field from the + * struct returned by `GetVersionEx`. + * + * [`GetVersionEx`]: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getversionexa + * + * ## Return value + * + * Returns the build number as an int. + */ +int get_build_number() { + OSVERSIONINFO osvi; + + ZeroMemory(&osvi, sizeof(osvi)); + osvi.dwOSVersionInfoSize = sizeof(osvi); + + if (GetVersionEx(&osvi)) { + return osvi.dwBuildNumber; + } + + return 0; +} + diff --git a/third_party/rust/whatsys/examples/version.rs b/third_party/rust/whatsys/examples/version.rs new file mode 100644 index 0000000000..0bc9a16ede --- /dev/null +++ b/third_party/rust/whatsys/examples/version.rs @@ -0,0 +1,5 @@ +fn main() { + println!("{:?}", whatsys::kernel_version()); + #[cfg(target_os = "windows")] + println!("{:?}", whatsys::windows_build_number()); +} diff --git a/third_party/rust/whatsys/src/apple.rs b/third_party/rust/whatsys/src/apple.rs new file mode 100644 index 0000000000..000fbe36cb --- /dev/null +++ b/third_party/rust/whatsys/src/apple.rs @@ -0,0 +1,61 @@ +/* Based on code from sysinfo: https://crates.io/crates/sysinfo + * Original licenses: MIT + * Original author: Guillaume Gomez + * License file: https://github.com/GuillaumeGomez/sysinfo/blob/master/LICENSE + */ + +use libc::c_int; + +fn get_system_info(value: c_int) -> Option<String> { + let mut mib: [c_int; 2] = [libc::CTL_KERN, value]; + let mut size = 0; + + // Call first to get size + unsafe { + libc::sysctl( + mib.as_mut_ptr(), + 2, + std::ptr::null_mut(), + &mut size, + std::ptr::null_mut(), + 0, + ) + }; + + // exit early if we did not update the size + if size == 0 { + return None; + } + + // set the buffer to the correct size + let mut buf = vec![0_u8; size as usize]; + + if unsafe { + libc::sysctl( + mib.as_mut_ptr(), + 2, + buf.as_mut_ptr() as _, + &mut size, + std::ptr::null_mut(), + 0, + ) + } == -1 + { + // If command fails return default + None + } else { + if let Some(pos) = buf.iter().position(|x| *x == 0) { + // Shrink buffer to terminate the null bytes + buf.resize(pos, 0); + } + + String::from_utf8(buf).ok() + } +} + +/// Get the version of the currently running kernel. +/// +/// Returns `None` if an error occured. +pub fn kernel_version() -> Option<String> { + get_system_info(libc::KERN_OSRELEASE) +} diff --git a/third_party/rust/whatsys/src/fallback.rs b/third_party/rust/whatsys/src/fallback.rs new file mode 100644 index 0000000000..c9c690cb2b --- /dev/null +++ b/third_party/rust/whatsys/src/fallback.rs @@ -0,0 +1,6 @@ +/// **Unsupported** on this operating system. +/// +/// Returns `None`. +pub fn kernel_version() -> Option<String> { + None +} diff --git a/third_party/rust/whatsys/src/lib.rs b/third_party/rust/whatsys/src/lib.rs new file mode 100644 index 0000000000..1e071343b7 --- /dev/null +++ b/third_party/rust/whatsys/src/lib.rs @@ -0,0 +1,70 @@ +//! What kernel version is running? +//! +//! # Example +//! +//! ``` +//! let kernel = whatsys::kernel_version(); // E.g. Some("20.3.0") +//! ``` +//! +//! # Supported operating systems +//! +//! We support the following operating systems: +//! +//! * Windows +//! * macOS +//! * Linux +//! * Android +//! +//! # License +//! +//! MIT. Copyright (c) 2021-2022 Jan-Erik Rediger +//! +//! Based on: +//! +//! * [sys-info](https://crates.io/crates/sys-info), [Repository](https://github.com/FillZpp/sys-info-rs), [MIT LICENSE][sys-info-mit] +//! * [sysinfo](https://crates.io/crates/sysinfo), [Repository](https://github.com/GuillaumeGomez/sysinfo), [MIT LICENSE][sysinfo-mit] +//! +//! [sys-info-mit]: https://github.com/FillZpp/sys-info-rs/blob/master/LICENSE +//! [sysinfo-mit]: https://github.com/GuillaumeGomez/sysinfo/blob/master/LICENSE + +#![deny(missing_docs)] +#![deny(rustdoc::broken_intra_doc_links)] + +cfg_if::cfg_if! { + if #[cfg(target_os = "macos")] { + mod apple; + use apple as system; + + } else if #[cfg(any(target_os = "linux", target_os = "android"))] { + mod linux; + use linux as system; + } else if #[cfg(windows)] { + mod windows; + use windows as system; + } else { + mod fallback; + use fallback as system; + } +} + +pub use system::kernel_version; + +#[cfg(target_os = "windows")] +pub use system::windows_build_number; + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn gets_a_version() { + assert!(kernel_version().is_some()); + } + + #[cfg(target_os = "windows")] + #[test] + fn test_windows_build_number() { + let build_number = windows::windows_build_number(); + assert!(build_number.is_some()); + } +} diff --git a/third_party/rust/whatsys/src/linux.rs b/third_party/rust/whatsys/src/linux.rs new file mode 100644 index 0000000000..cea6838749 --- /dev/null +++ b/third_party/rust/whatsys/src/linux.rs @@ -0,0 +1,33 @@ +/* Based on code from sysinfo: https://crates.io/crates/sysinfo + * Original licenses: MIT + * Original author: Guillaume Gomez + * License file: https://github.com/GuillaumeGomez/sysinfo/blob/master/LICENSE + */ + +/// Get the version of the currently running kernel. +/// +/// **Note**: The kernel version might include arbitrary alphanumeric suffixes. +/// +/// Returns `None` if an error occured. +pub fn kernel_version() -> Option<String> { + let mut raw = std::mem::MaybeUninit::<libc::utsname>::zeroed(); + + unsafe { + // SAFETY: We created the pointer from a value on the stack. + if libc::uname(raw.as_mut_ptr()) == 0 { + // SAFETY: If `uname` succesfully returns it filled in the data. + let info = raw.assume_init(); + + let release = info + .release + .iter() + .filter(|c| **c != 0) + .map(|c| *c as u8 as char) + .collect::<String>(); + + Some(release) + } else { + None + } + } +} diff --git a/third_party/rust/whatsys/src/windows.rs b/third_party/rust/whatsys/src/windows.rs new file mode 100644 index 0000000000..7e5a22c048 --- /dev/null +++ b/third_party/rust/whatsys/src/windows.rs @@ -0,0 +1,53 @@ +use libc::{c_char, c_int}; + +extern "C" { + fn get_os_release(outbuf: *const c_char, outlen: usize) -> c_int; + fn get_build_number() -> c_int; +} + +/// Get the version of the currently running kernel. +/// +/// **Note**: On Windows 8 and later this will report the Windows 8 OS version value `6.2`, +/// unless the final application is build to explicitly target Windows 10. +/// See [`GetVersionEx`] for details. +/// +/// [`GetVersionEx`]: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getversionexa +/// +/// +/// Returns `None` if an error occured. +pub fn kernel_version() -> Option<String> { + unsafe { + // Windows 10 should report "10.0", which is 4 bytes, + // "unknown" is 7 bytes. + // We need to account for the null byte. + let size = 8; + let mut buf = vec![0; size]; + let written = get_os_release(buf.as_mut_ptr() as _, size) as usize; + match written { + 0 => None, + _ => Some(String::from_utf8_lossy(&buf[0..written]).into_owned()), + } + } +} + +/// Get the build number from Windows. +/// +/// **Note**: On Windows 8 and later this may report a Windows 8 build number, +/// for example 9200, unless the final application is build to explicitly +/// target Windows 10. +/// See [`GetVersionEx`] for details. +/// +/// [`GetVersionEx`]: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getversionexa +/// +/// +/// Returns `None` if an error occured. +pub fn windows_build_number() -> Option<i32> { + unsafe { + // Get windows build number + let build_number = get_build_number(); + match build_number { + 0 => None, + _ => Some(build_number), + } + } +} |