diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:47:29 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:47:29 +0000 |
commit | 0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d (patch) | |
tree | a31f07c9bcca9d56ce61e9a1ffd30ef350d513aa /third_party/rust/android_system_properties | |
parent | Initial commit. (diff) | |
download | firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.tar.xz firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.zip |
Adding upstream version 115.8.0esr.upstream/115.8.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/android_system_properties')
9 files changed, 392 insertions, 0 deletions
diff --git a/third_party/rust/android_system_properties/.cargo-checksum.json b/third_party/rust/android_system_properties/.cargo-checksum.json new file mode 100644 index 0000000000..1588ce5b52 --- /dev/null +++ b/third_party/rust/android_system_properties/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"CONTRIBUTING.md":"0834cb3b5e092977688d73d219a05bed23ae0ecb54b6d6e5d866ce07f6583b5e","Cargo.lock":"37ffc00dbbbec58fd27b4f4cb597e5402d6cf615ce0458f62a73a7f0d987e5bd","Cargo.toml":"e9e8c037cdef7adc9794b17c13e5a014421524d67ea5048bc09cf70ef13c782c","LICENSE-APACHE":"216486f29671a4262efe32af6d84a75bef398127f8c5f369b5c8305983887a06","LICENSE-MIT":"80f275e90d799911ed3830a7f242a2ef5a4ade2092fe0aa07bfb2d2cf2f2b95e","README.md":"aba8ff5dbd0712326d97d32bc6a3b66b24d1980a446c238f7e14b96784766cd1","examples/time_zone.rs":"8edb32a946ef2680146ba9ac16c233dd94391ac9f98464e9fb6f87d3954b72a9","src/lib.rs":"0004133d6c3805bf449e7183d2931e9640167511bea6cd12b400805073c4305d"},"package":"819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"}
\ No newline at end of file diff --git a/third_party/rust/android_system_properties/CONTRIBUTING.md b/third_party/rust/android_system_properties/CONTRIBUTING.md new file mode 100644 index 0000000000..62436f59bb --- /dev/null +++ b/third_party/rust/android_system_properties/CONTRIBUTING.md @@ -0,0 +1,40 @@ +# Contributing + +Contributions are very much welcome. Here are the guidelines if you are thinking of helping us: + +## Contributions + +Contributions should be made in the form of GitHub pull requests. +Each pull request will be reviewed by a core contributor (someone with +permission to land patches) and either landed in the main tree or +given feedback for changes that would be required. + +Should you wish to work on an issue, please claim it first by commenting on +the GitHub issue that you want to work on it. This is to prevent duplicated +efforts from contributors on the same issue. + +## Pull Request Checklist + +- Branch from the master branch and, if needed, rebase to the current master + branch before submitting your pull request. If it doesn't merge cleanly with + master you may be asked to rebase your changes. + +- Commits should be as small as possible, while ensuring that each commit is + correct independently (i.e., each commit should compile and pass tests). + +- If your patch is not getting reviewed or you need a specific person to review + it, you can @-reply a reviewer asking for a review in the pull request or a + comment. + +- Whenever applicable, add tests relevant to the fixed bug or new feature. + +For specific git instructions, see [GitHub workflow 101](https://github.com/servo/servo/wiki/Github-workflow). + +## Conduct + +We follow the [Rust Code of Conduct](http://www.rust-lang.org/conduct.html). +For escalation or moderation issues, please contact Nical (nical@fastmail.com) instead of the Rust moderation team. + +## License + +Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be licensed dual MIT/Apache 2, without any additional terms or conditions. diff --git a/third_party/rust/android_system_properties/Cargo.lock b/third_party/rust/android_system_properties/Cargo.lock new file mode 100644 index 0000000000..9ee6e3d292 --- /dev/null +++ b/third_party/rust/android_system_properties/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "android_system_properties" +version = "0.1.5" +dependencies = [ + "libc", +] + +[[package]] +name = "libc" +version = "0.2.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" diff --git a/third_party/rust/android_system_properties/Cargo.toml b/third_party/rust/android_system_properties/Cargo.toml new file mode 100644 index 0000000000..79e4ea0d27 --- /dev/null +++ b/third_party/rust/android_system_properties/Cargo.toml @@ -0,0 +1,36 @@ +# 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 = "android_system_properties" +version = "0.1.5" +authors = ["Nicolas Silva <nical@fastmail.com>"] +description = "Minimal Android system properties wrapper" +homepage = "https://github.com/nical/android_system_properties" +documentation = "https://docs.rs/android_system_properties" +readme = "README.md" +keywords = ["android"] +license = "MIT/Apache-2.0" +repository = "https://github.com/nical/android_system_properties" + +[package.metadata.docs.rs] +targets = [ + "arm-linux-androideabi", + "armv7-linux-androideabi", + "aarch64-linux-android", + "i686-linux-android", + "x86_64-linux-android", + "x86_64-unknown-linux-gnu", +] + +[dependencies.libc] +version = "0.2.126" diff --git a/third_party/rust/android_system_properties/LICENSE-APACHE b/third_party/rust/android_system_properties/LICENSE-APACHE new file mode 100644 index 0000000000..47f4fd0e89 --- /dev/null +++ b/third_party/rust/android_system_properties/LICENSE-APACHE @@ -0,0 +1,13 @@ +Copyright 2016 Nicolas Silva + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/third_party/rust/android_system_properties/LICENSE-MIT b/third_party/rust/android_system_properties/LICENSE-MIT new file mode 100644 index 0000000000..e6d4b17994 --- /dev/null +++ b/third_party/rust/android_system_properties/LICENSE-MIT @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2013 Nicolas Silva + +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/android_system_properties/README.md b/third_party/rust/android_system_properties/README.md new file mode 100644 index 0000000000..46f6500b59 --- /dev/null +++ b/third_party/rust/android_system_properties/README.md @@ -0,0 +1,36 @@ +# android_system_properties + +A thin rust wrapper for Android system properties. + +This crate is similar to the `android-properties` crate with the exception that +the necessary Android libc symbols are loaded dynamically instead of linked +statically. In practice this means that the same binary will work with old and +new versions of Android, even though the API for reading system properties changed +around Android L. + +## Example + +```rust +use android_system_properties::AndroidSystemProperties; + +let properties = AndroidSystemProperties::new(); + +if let Some(value) = properties.get("persist.sys.timezone") { + println!("{}", value); +} +``` + +## Listing and setting properties + +For the sake of simplicity this crate currently only contains what's needed by wgpu. +The implementations for listing and setting properties can be added back if anyone needs +them (let me know by filing an issue). + +## License + +Licensed under either of + + * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) + * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) + +at your option. diff --git a/third_party/rust/android_system_properties/examples/time_zone.rs b/third_party/rust/android_system_properties/examples/time_zone.rs new file mode 100644 index 0000000000..f3a6a3c7b7 --- /dev/null +++ b/third_party/rust/android_system_properties/examples/time_zone.rs @@ -0,0 +1,9 @@ +/// Prints the current time zone, e.g. "Europe/Paris". + +use android_system_properties::AndroidSystemProperties; + +fn main() { + let android_system_properties = AndroidSystemProperties::new(); + let tz = android_system_properties.get("persist.sys.timezone"); + println!("Your time zone is: {}", tz.as_deref().unwrap_or("<unknown>")); +} diff --git a/third_party/rust/android_system_properties/src/lib.rs b/third_party/rust/android_system_properties/src/lib.rs new file mode 100644 index 0000000000..9cd9d49161 --- /dev/null +++ b/third_party/rust/android_system_properties/src/lib.rs @@ -0,0 +1,221 @@ +//! A thin rust wrapper for Android system properties. +//! +//! This crate is similar to the `android-properties` crate with the exception that +//! the necessary Android libc symbols are loaded dynamically instead of linked +//! statically. In practice this means that the same binary will work with old and +//! new versions of Android, even though the API for reading system properties changed +//! around Android L. +//! +//! ## Example +//! +//! ```rust +//! use android_system_properties::AndroidSystemProperties; +//! +//! let properties = AndroidSystemProperties::new(); +//! +//! if let Some(value) = properties.get("persist.sys.timezone") { +//! println!("{}", value); +//! } +//! ``` +//! +//! ## Listing and setting properties +//! +//! For the sake of simplicity this crate currently only contains what's needed by wgpu. +//! The implementations for listing and setting properties can be added back if anyone needs +//! them (let me know by filing an issue). +//! +//! ## License +//! +//! Licensed under either of +//! +//! * Apache License, Version 2.0 ([LICENSE-APACHE] or <http://www.apache.org/licenses/LICENSE-2.0>) +//! * MIT license ([LICENSE-MIT] or <http://opensource.org/licenses/MIT>) +//! +//! at your option. +//! +//! [LICENSE-APACHE]: https://github.com/nical/android_system_properties/blob/804681c5c1c93d4fab29c1a2f47b7d808dc70fd3/LICENSE-APACHE +//! [LICENSE-MIT]: https://github.com/nical/android_system_properties/blob/804681c5c1c93d4fab29c1a2f47b7d808dc70fd3/LICENSE-MIT + +use std::{ + ffi::{CStr, CString}, + os::raw::{c_char, c_int, c_void}, +}; + +#[cfg(target_os = "android")] +use std::mem; + +unsafe fn property_callback(payload: *mut String, _name: *const c_char, value: *const c_char, _serial: u32) { + let cvalue = CStr::from_ptr(value); + (*payload) = cvalue.to_str().unwrap().to_string(); +} + +type Callback = unsafe fn(*mut String, *const c_char, *const c_char, u32); + +type SystemPropertyGetFn = unsafe extern "C" fn(*const c_char, *mut c_char) -> c_int; +type SystemPropertyFindFn = unsafe extern "C" fn(*const c_char) -> *const c_void; +type SystemPropertyReadCallbackFn = unsafe extern "C" fn(*const c_void, Callback, *mut String) -> *const c_void; + +#[derive(Debug)] +/// An object that can retrieve android system properties. +/// +/// ## Example +/// +/// ``` +/// use android_system_properties::AndroidSystemProperties; +/// +/// let properties = AndroidSystemProperties::new(); +/// +/// if let Some(value) = properties.get("persist.sys.timezone") { +/// println!("{}", value); +/// } +/// ``` +pub struct AndroidSystemProperties { + libc_so: *mut c_void, + get_fn: Option<SystemPropertyGetFn>, + find_fn: Option<SystemPropertyFindFn>, + read_callback_fn: Option<SystemPropertyReadCallbackFn>, +} + +unsafe impl Send for AndroidSystemProperties {} +unsafe impl Sync for AndroidSystemProperties {} + +impl AndroidSystemProperties { + #[cfg(not(target_os = "android"))] + /// Create an entry point for accessing Android properties. + pub fn new() -> Self { + AndroidSystemProperties { + libc_so: std::ptr::null_mut(), + find_fn: None, + read_callback_fn: None, + get_fn: None, + } + } + + #[cfg(target_os = "android")] + /// Create an entry point for accessing Android properties. + pub fn new() -> Self { + let libc_so = unsafe { libc::dlopen(b"libc.so\0".as_ptr().cast(), libc::RTLD_NOLOAD) }; + + let mut properties = AndroidSystemProperties { + libc_so, + find_fn: None, + read_callback_fn: None, + get_fn: None, + }; + + if libc_so.is_null() { + return properties; + } + + + unsafe fn load_fn(libc_so: *mut c_void, name: &[u8]) -> Option<*const c_void> { + let fn_ptr = libc::dlsym(libc_so, name.as_ptr().cast()); + + if fn_ptr.is_null() { + return None; + } + + Some(fn_ptr) + } + + unsafe { + properties.read_callback_fn = load_fn(libc_so, b"__system_property_read_callback\0") + .map(|raw| mem::transmute::<*const c_void, SystemPropertyReadCallbackFn>(raw)); + + properties.find_fn = load_fn(libc_so, b"__system_property_find\0") + .map(|raw| mem::transmute::<*const c_void, SystemPropertyFindFn>(raw)); + + // Fallback for old versions of Android. + if properties.read_callback_fn.is_none() || properties.find_fn.is_none() { + properties.get_fn = load_fn(libc_so, b"__system_property_get\0") + .map(|raw| mem::transmute::<*const c_void, SystemPropertyGetFn>(raw)); + } + } + + properties + } + + /// Retrieve a system property. + /// + /// Returns None if the operation fails. + /// + /// # Example + /// + /// ``` + /// # use android_system_properties::AndroidSystemProperties; + /// let properties = AndroidSystemProperties::new(); + /// + /// if let Some(value) = properties.get("persist.sys.timezone") { + /// println!("{}", value); + /// } + /// ``` + pub fn get(&self, name: &str) -> Option<String> { + let cname = CString::new(name).ok()?; + self.get_from_cstr(&cname) + } + + /// Retrieve a system property using a [`CStr`] key. + /// + /// Returns None if the operation fails. + /// + /// # Example + /// + /// ``` + /// # use android_system_properties::AndroidSystemProperties; + /// # use std::ffi::CStr; + /// let properties = AndroidSystemProperties::new(); + /// + /// let key = unsafe { CStr::from_bytes_with_nul_unchecked(b"persist.sys.timezone\0") }; + /// if let Some(value) = properties.get_from_cstr(key) { + /// println!("{}", value); + /// } + /// ``` + pub fn get_from_cstr(&self, cname: &std::ffi::CStr) -> Option<String> { + // If available, use the recommended approach to accessing properties (Android L and onward). + if let (Some(find_fn), Some(read_callback_fn)) = (self.find_fn, self.read_callback_fn) { + let info = unsafe { (find_fn)(cname.as_ptr()) }; + + if info.is_null() { + return None; + } + + let mut result = String::new(); + + unsafe { + (read_callback_fn)(info, property_callback, &mut result); + } + + return Some(result); + } + + // Fall back to the older approach. + if let Some(get_fn) = self.get_fn { + // The constant is PROP_VALUE_MAX in Android's libc/include/sys/system_properties.h + const PROPERTY_VALUE_MAX: usize = 92; + let mut buffer: Vec<u8> = Vec::with_capacity(PROPERTY_VALUE_MAX); + let raw = buffer.as_mut_ptr() as *mut c_char; + + let len = unsafe { (get_fn)(cname.as_ptr(), raw) }; + + if len > 0 { + assert!(len as usize <= buffer.capacity()); + unsafe { buffer.set_len(len as usize); } + String::from_utf8(buffer).ok() + } else { + None + } + } else { + None + } + } +} + +impl Drop for AndroidSystemProperties { + fn drop(&mut self) { + if !self.libc_so.is_null() { + unsafe { + libc::dlclose(self.libc_so); + } + } + } +} |