From e1c60ef5bd49024f9d9c350394a7d1204a19a749 Mon Sep 17 00:00:00 2001 From: Matthew Maurer Date: Thu, 26 May 2022 10:44:53 -0700 Subject: Update TEST_MAPPING Test: None Bug: 233924440 Change-Id: I678a7e37c2aa90732a4a8977eb5ed35c9c30afef --- TEST_MAPPING | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/TEST_MAPPING b/TEST_MAPPING index 061ff86..6495c8a 100644 --- a/TEST_MAPPING +++ b/TEST_MAPPING @@ -1,5 +1,10 @@ // Generated by update_crate_tests.py for tests that depend on this crate. { + "postsubmit": [ + { + "name": "virtualizationservice_device_test" + } + ], "presubmit": [ { "name": "virtualizationservice_device_test" -- cgit v1.2.3 From a4cf2ef40bdfb3e87c1295ec9390c4afd5b05b6d Mon Sep 17 00:00:00 2001 From: Matthew Maurer Date: Tue, 14 Jun 2022 15:48:28 -0700 Subject: Update TEST_MAPPING Test: None Bug: 236006683 Change-Id: I2670e189eb562609ba89a0973d162fd86f9da6b9 --- TEST_MAPPING | 5 ----- 1 file changed, 5 deletions(-) diff --git a/TEST_MAPPING b/TEST_MAPPING index 6495c8a..061ff86 100644 --- a/TEST_MAPPING +++ b/TEST_MAPPING @@ -1,10 +1,5 @@ // Generated by update_crate_tests.py for tests that depend on this crate. { - "postsubmit": [ - { - "name": "virtualizationservice_device_test" - } - ], "presubmit": [ { "name": "virtualizationservice_device_test" -- cgit v1.2.3 From 0d0ce64348a3488d2a657288cbe53deb25ab28cc Mon Sep 17 00:00:00 2001 From: David LeGare Date: Tue, 28 Jun 2022 17:46:54 +0000 Subject: Upgrade rust/crates/command-fds to 0.2.2 Test: make Change-Id: Iea446d189fa281a352bf4544752f7d004f38c1a2 --- .cargo_vcs_info.json | 7 ++++--- Android.bp | 3 ++- Cargo.toml | 22 ++++++++++++++++------ Cargo.toml.orig | 13 ++++++++++++- METADATA | 10 +++++----- src/lib.rs | 22 +++++++++++++++------- src/tokio.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 96 insertions(+), 23 deletions(-) create mode 100644 src/tokio.rs diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index eb2050d..0562cca 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,5 +1,6 @@ { "git": { - "sha1": "cb5819f26b7e1b2eab75994bdd73309ef2859df7" - } -} + "sha1": "425bb5dc189f3a5130bdc80ca47d6da5001157b4" + }, + "path_in_vcs": "" +} \ No newline at end of file diff --git a/Android.bp b/Android.bp index f2faab7..28f7c62 100644 --- a/Android.bp +++ b/Android.bp @@ -23,9 +23,10 @@ rust_library { host_supported: true, crate_name: "command_fds", cargo_env_compat: true, - cargo_pkg_version: "0.2.1", + cargo_pkg_version: "0.2.2", srcs: ["src/lib.rs"], edition: "2018", + features: ["default"], rustlibs: [ "libnix", "libthiserror", diff --git a/Cargo.toml b/Cargo.toml index 1d72a8c..bb015ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,17 +3,16 @@ # 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 +# to registry (e.g., crates.io) dependencies. # -# If you believe there's an error in this file please file an -# issue against the rust-lang/cargo repository. If you're -# editing this file be aware that the upstream Cargo.toml -# will likely look very different (and much more reasonable) +# 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 = "command-fds" -version = "0.2.1" +version = "0.2.2" authors = ["Andrew Walbran "] description = "A library for passing arbitrary file descriptors when spawning child processes." keywords = ["command", "process", "child", "subprocess", "fd"] @@ -25,3 +24,14 @@ version = "0.22.0" [dependencies.thiserror] version = "1.0.24" + +[dependencies.tokio-crate] +version = "^1.0" +features = ["process"] +optional = true +default-features = false +package = "tokio" + +[features] +default = [] +tokio = ["tokio-crate"] diff --git a/Cargo.toml.orig b/Cargo.toml.orig index 15a6874..ef218ac 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,6 +1,6 @@ [package] name = "command-fds" -version = "0.2.1" +version = "0.2.2" edition = "2018" authors = ["Andrew Walbran "] license = "Apache-2.0" @@ -12,3 +12,14 @@ categories = ["os::unix-apis"] [dependencies] nix = "0.22.0" thiserror = "1.0.24" + +[dependencies.tokio-crate] +package = "tokio" +version = "^1.0" +optional = true +default-features = false +features = ["process"] + +[features] +default = [] +tokio = ["tokio-crate"] diff --git a/METADATA b/METADATA index 9ed08b1..7b98618 100644 --- a/METADATA +++ b/METADATA @@ -7,13 +7,13 @@ third_party { } url { type: ARCHIVE - value: "https://static.crates.io/crates/command-fds/command-fds-0.2.1.crate" + value: "https://static.crates.io/crates/command-fds/command-fds-0.2.2.crate" } - version: "0.2.1" + version: "0.2.2" license_type: NOTICE last_upgrade_date { - year: 2021 - month: 8 - day: 16 + year: 2022 + month: 6 + day: 28 } } diff --git a/src/lib.rs b/src/lib.rs index 2dfb63f..fde680a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -48,6 +48,9 @@ //! child.wait().unwrap(); //! ``` +#[cfg(feature = "tokio")] +pub mod tokio; + use nix::fcntl::{fcntl, FcntlArg, FdFlag}; use nix::unistd::dup2; use std::cmp::max; @@ -90,13 +93,7 @@ impl CommandFdExt for Command { &mut self, mut mappings: Vec, ) -> Result<&mut Self, FdMappingCollision> { - // Validate that there are no conflicting mappings to the same child FD. - let mut child_fds: Vec = mappings.iter().map(|mapping| mapping.child_fd).collect(); - child_fds.sort_unstable(); - child_fds.dedup(); - if child_fds.len() != mappings.len() { - return Err(FdMappingCollision); - } + let child_fds = validate_child_fds(&mappings)?; // Register the callback to apply the mappings after forking but before execing. // Safety: `map_fds` will not allocate, so it is safe to call from this hook. @@ -120,6 +117,17 @@ impl CommandFdExt for Command { } } +/// Validates that there are no conflicting mappings to the same child FD. +fn validate_child_fds(mappings: &[FdMapping]) -> Result, FdMappingCollision> { + let mut child_fds: Vec = mappings.iter().map(|mapping| mapping.child_fd).collect(); + child_fds.sort_unstable(); + child_fds.dedup(); + if child_fds.len() != mappings.len() { + return Err(FdMappingCollision); + } + Ok(child_fds) +} + // This function must not do any allocation, as it is called from the pre_exec hook. fn map_fds(mappings: &mut [FdMapping], child_fds: &[RawFd]) -> io::Result<()> { if mappings.is_empty() { diff --git a/src/tokio.rs b/src/tokio.rs new file mode 100644 index 0000000..78979bf --- /dev/null +++ b/src/tokio.rs @@ -0,0 +1,42 @@ +use std::os::unix::prelude::RawFd; + +use tokio::process::Command; +use tokio_crate as tokio; + +use crate::{map_fds, preserve_fds, validate_child_fds, FdMapping, FdMappingCollision}; + +/// Extension to add file descriptor mappings to a [`Command`]. +pub trait CommandFdAsyncExt { + /// Adds the given set of file descriptors to the command. + /// + /// Warning: Calling this more than once on the same command, or attempting to run the same + /// command more than once after calling this, may result in unexpected behaviour. + fn fd_mappings(&mut self, mappings: Vec) -> Result<&mut Self, FdMappingCollision>; + + /// Adds the given set of file descriptors to be passed on to the child process when the command + /// is run. + fn preserved_fds(&mut self, fds: Vec) -> &mut Self; +} + +impl CommandFdAsyncExt for Command { + fn fd_mappings( + &mut self, + mut mappings: Vec, + ) -> Result<&mut Self, FdMappingCollision> { + let child_fds = validate_child_fds(&mappings)?; + + unsafe { + self.pre_exec(move || map_fds(&mut mappings, &child_fds)); + } + + Ok(self) + } + + fn preserved_fds(&mut self, fds: Vec) -> &mut Self { + unsafe { + self.pre_exec(move || preserve_fds(&fds)); + } + + self + } +} -- cgit v1.2.3 From a175302b54c131a24415118b626f0ae31e41010c Mon Sep 17 00:00:00 2001 From: David Brazdil Date: Sat, 8 Oct 2022 14:30:25 +0100 Subject: Apply new 'apex_available' default Test: builds Change-Id: I35cd6da2a566c66875663fb6eed7a16f12499360 --- Android.bp | 2 +- cargo2android.json | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/Android.bp b/Android.bp index 28f7c62..6c86e2d 100644 --- a/Android.bp +++ b/Android.bp @@ -33,6 +33,6 @@ rust_library { ], apex_available: [ "//apex_available:platform", - "com.android.virt", + "//apex_available:anyapex", ], } diff --git a/cargo2android.json b/cargo2android.json index 42b7833..1b6cca2 100644 --- a/cargo2android.json +++ b/cargo2android.json @@ -1,8 +1,4 @@ { - "apex-available": [ - "//apex_available:platform", - "com.android.virt" - ], "dependencies": true, "device": true, "run": true -- cgit v1.2.3 From f63734e47b3b87775781bb8c12b4549b374441a6 Mon Sep 17 00:00:00 2001 From: David Brazdil Date: Mon, 23 Jan 2023 13:24:18 +0000 Subject: Update TEST_MAPPING Test: None Change-Id: Iac720c3d6c4bbb71d9da88ddda32f71625b17a44 --- TEST_MAPPING | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/TEST_MAPPING b/TEST_MAPPING index 061ff86..697da71 100644 --- a/TEST_MAPPING +++ b/TEST_MAPPING @@ -2,12 +2,48 @@ { "presubmit": [ { - "name": "virtualizationservice_device_test" + "name": "compos_verify.test" + }, + { + "name": "composd_cmd.test" + }, + { + "name": "open_then_run.test" + }, + { + "name": "rialto_test" + }, + { + "name": "virtualizationmanager_device_test" + }, + { + "name": "vm.test" + }, + { + "name": "vmbase_example.integration_test" } ], "presubmit-rust": [ { - "name": "virtualizationservice_device_test" + "name": "compos_verify.test" + }, + { + "name": "composd_cmd.test" + }, + { + "name": "open_then_run.test" + }, + { + "name": "rialto_test" + }, + { + "name": "virtualizationmanager_device_test" + }, + { + "name": "vm.test" + }, + { + "name": "vmbase_example.integration_test" } ] } -- cgit v1.2.3 From b0df4f9fb891f9319a64163b352f3586a26e6c6e Mon Sep 17 00:00:00 2001 From: Jeff Vander Stoep Date: Mon, 30 Jan 2023 11:07:54 +0100 Subject: Update TEST_MAPPING Test: atest Change-Id: Iae4c5131bb45ffe05ea6e98e11a3439b352db8cf --- TEST_MAPPING | 44 +++----------------------------------------- 1 file changed, 3 insertions(+), 41 deletions(-) diff --git a/TEST_MAPPING b/TEST_MAPPING index 697da71..5366bfb 100644 --- a/TEST_MAPPING +++ b/TEST_MAPPING @@ -1,49 +1,11 @@ // Generated by update_crate_tests.py for tests that depend on this crate. { - "presubmit": [ + "imports": [ { - "name": "compos_verify.test" + "path": "packages/modules/Virtualization/virtualizationmanager" }, { - "name": "composd_cmd.test" - }, - { - "name": "open_then_run.test" - }, - { - "name": "rialto_test" - }, - { - "name": "virtualizationmanager_device_test" - }, - { - "name": "vm.test" - }, - { - "name": "vmbase_example.integration_test" - } - ], - "presubmit-rust": [ - { - "name": "compos_verify.test" - }, - { - "name": "composd_cmd.test" - }, - { - "name": "open_then_run.test" - }, - { - "name": "rialto_test" - }, - { - "name": "virtualizationmanager_device_test" - }, - { - "name": "vm.test" - }, - { - "name": "vmbase_example.integration_test" + "path": "packages/modules/Virtualization/vm" } ] } -- cgit v1.2.3 From 79b81d52e8928f69e5e73ee777f50870f8fd3e2a Mon Sep 17 00:00:00 2001 From: Matthew Maurer Date: Tue, 7 Mar 2023 17:23:36 -0800 Subject: Make command-fds available to product and vendor Bug: 270690570 Test: mma in external/rust/crates Change-Id: I620011a0191af462fa11675ccc9132637d410357 --- Android.bp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Android.bp b/Android.bp index 6c86e2d..dc3d4e7 100644 --- a/Android.bp +++ b/Android.bp @@ -35,4 +35,6 @@ rust_library { "//apex_available:platform", "//apex_available:anyapex", ], + product_available: true, + vendor_available: true, } -- cgit v1.2.3