Use new binder exception constructors moved from binder_common.
Bug: 234019127
Test: atest compos_key_tests MicrodroidHostTestCases MicrodroidTestApp
Change-Id: I938c9d0ebae90c933c9a7ee8c27e9ecb0cc3e5fa
diff --git a/authfs/fd_server/src/aidl.rs b/authfs/fd_server/src/aidl.rs
index 3a3cdb2..0e8952e 100644
--- a/authfs/fd_server/src/aidl.rs
+++ b/authfs/fd_server/src/aidl.rs
@@ -42,7 +42,6 @@
use authfs_fsverity_metadata::{
get_fsverity_metadata_path, parse_fsverity_metadata, FSVerityMetadata,
};
-use binder_common::{new_binder_exception, new_binder_service_specific_error};
/// Bitflags of forbidden file mode, e.g. setuid, setgid and sticky bit.
const FORBIDDEN_MODES: Mode = Mode::from_bits_truncate(!0o777);
@@ -107,9 +106,12 @@
entry.insert(new_fd_config);
Ok(new_fd)
} else {
- Err(new_binder_exception(
+ Err(Status::new_exception_str(
ExceptionCode::ILLEGAL_STATE,
- format!("The newly created FD {} is already in the pool unexpectedly", new_fd),
+ Some(format!(
+ "The newly created FD {} is already in the pool unexpectedly",
+ new_fd
+ )),
))
}
}
@@ -173,9 +175,9 @@
if let Some(signature) = &metadata.signature {
Ok(signature.clone())
} else {
- Err(new_binder_exception(
- ExceptionCode::SERVICE_SPECIFIC,
- "metadata doesn't contain a signature",
+ Err(Status::new_service_specific_error_str(
+ -1,
+ Some("metadata doesn't contain a signature"),
))
}
} else {
@@ -395,7 +397,7 @@
}
fn new_errno_error(errno: Errno) -> Status {
- new_binder_service_specific_error(errno as i32, errno.desc())
+ Status::new_service_specific_error_str(errno as i32, Some(errno.desc()))
}
fn open_readonly_at(dir_fd: RawFd, path: &Path) -> nix::Result<File> {
diff --git a/authfs/service/Android.bp b/authfs/service/Android.bp
index 6c32c67..943db35 100644
--- a/authfs/service/Android.bp
+++ b/authfs/service/Android.bp
@@ -12,7 +12,6 @@
"authfs_aidl_interface-rust",
"libandroid_logger",
"libanyhow",
- "libbinder_common",
"libbinder_rs",
"liblibc",
"liblog_rust",
diff --git a/authfs/service/src/authfs.rs b/authfs/service/src/authfs.rs
index c941360..547d15d 100644
--- a/authfs/service/src/authfs.rs
+++ b/authfs/service/src/authfs.rs
@@ -32,9 +32,8 @@
};
use authfs_aidl_interface::aidl::com::android::virt::fs::IAuthFs::{BnAuthFs, IAuthFs};
use authfs_aidl_interface::binder::{
- self, BinderFeatures, ExceptionCode, Interface, ParcelFileDescriptor, Strong,
+ self, BinderFeatures, Interface, ParcelFileDescriptor, Status, Strong,
};
-use binder_common::new_binder_exception;
const AUTHFS_BIN: &str = "/system/bin/authfs";
const AUTHFS_SETUP_POLL_INTERVAL_MS: Duration = Duration::from_millis(50);
@@ -60,9 +59,9 @@
let mut path = PathBuf::from(&self.mountpoint);
path.push(remote_fd_name.to_string());
let file = OpenOptions::new().read(true).write(writable).open(&path).map_err(|e| {
- new_binder_exception(
- ExceptionCode::SERVICE_SPECIFIC,
- format!("failed to open {:?} on authfs: {}", &path, e),
+ Status::new_service_specific_error_str(
+ -1,
+ Some(format!("failed to open {:?} on authfs: {}", &path, e)),
)
})?;
Ok(ParcelFileDescriptor::new(file))
@@ -72,7 +71,7 @@
if let Some(s) = self.mountpoint.to_str() {
Ok(s.to_string())
} else {
- Err(new_binder_exception(ExceptionCode::SERVICE_SPECIFIC, "Bad string encoding"))
+ Err(Status::new_service_specific_error_str(-1, Some("Bad string encoding")))
}
}
}
diff --git a/authfs/service/src/main.rs b/authfs/service/src/main.rs
index 890e108..fe8af61 100644
--- a/authfs/service/src/main.rs
+++ b/authfs/service/src/main.rs
@@ -34,9 +34,8 @@
BnAuthFsService, IAuthFsService,
};
use authfs_aidl_interface::binder::{
- self, add_service, BinderFeatures, ExceptionCode, Interface, ProcessState, Strong,
+ self, add_service, BinderFeatures, ExceptionCode, Interface, ProcessState, Status, Strong,
};
-use binder_common::new_binder_exception;
const SERVICE_NAME: &str = "authfs_service";
const SERVICE_ROOT: &str = "/data/misc/authfs";
@@ -57,16 +56,16 @@
// The directory is supposed to be deleted when `AuthFs` is dropped.
create_dir(&mountpoint).map_err(|e| {
- new_binder_exception(
- ExceptionCode::SERVICE_SPECIFIC,
- format!("Cannot create mount directory {:?}: {:?}", &mountpoint, e),
+ Status::new_service_specific_error_str(
+ -1,
+ Some(format!("Cannot create mount directory {:?}: {:?}", &mountpoint, e)),
)
})?;
authfs::AuthFs::mount_and_wait(mountpoint, config, self.debuggable).map_err(|e| {
- new_binder_exception(
- ExceptionCode::SERVICE_SPECIFIC,
- format!("mount_and_wait failed: {:?}", e),
+ Status::new_service_specific_error_str(
+ -1,
+ Some(format!("mount_and_wait failed: {:?}", e)),
)
})
}
@@ -80,9 +79,9 @@
fn validate(&self, config: &AuthFsConfig) -> binder::Result<()> {
if config.port < 0 {
- return Err(new_binder_exception(
+ return Err(Status::new_exception_str(
ExceptionCode::ILLEGAL_ARGUMENT,
- format!("Invalid port: {}", config.port),
+ Some(format!("Invalid port: {}", config.port)),
));
}
Ok(())
diff --git a/compos/common/Android.bp b/compos/common/Android.bp
index 3c3397d..0773652 100644
--- a/compos/common/Android.bp
+++ b/compos/common/Android.bp
@@ -11,7 +11,6 @@
"android.system.virtualizationservice-rust",
"compos_aidl_interface-rust",
"libanyhow",
- "libbinder_common",
"liblazy_static",
"liblog_rust",
"libnested_virt",
diff --git a/compos/common/binder.rs b/compos/common/binder.rs
index 45139f3..59726c0 100644
--- a/compos/common/binder.rs
+++ b/compos/common/binder.rs
@@ -16,8 +16,7 @@
//! Helper for converting Error types to what Binder expects
-use android_system_virtualizationservice::binder::{ExceptionCode, Result as BinderResult};
-use binder_common::new_binder_exception;
+use android_system_virtualizationservice::binder::{Result as BinderResult, Status};
use log::warn;
use std::fmt::Debug;
@@ -28,6 +27,6 @@
result.map_err(|e| {
let message = format!("{:?}", e);
warn!("Returning binder error: {}", &message);
- new_binder_exception(ExceptionCode::SERVICE_SPECIFIC, message)
+ Status::new_service_specific_error_str(-1, Some(message))
})
}
diff --git a/compos/src/compsvc.rs b/compos/src/compsvc.rs
index 5d58221..088d41a 100644
--- a/compos/src/compsvc.rs
+++ b/compos/src/compsvc.rs
@@ -19,7 +19,6 @@
//! actual compiler.
use anyhow::{bail, Context, Result};
-use binder_common::new_binder_exception;
use log::{error, info};
use rustutils::system_properties;
use std::default::Default;
@@ -35,7 +34,7 @@
BnCompOsService, CompilationMode::CompilationMode, ICompOsService,
};
use compos_aidl_interface::binder::{
- BinderFeatures, ExceptionCode, Interface, Result as BinderResult, Strong,
+ BinderFeatures, ExceptionCode, Interface, Result as BinderResult, Status, Strong,
};
use compos_common::binder::to_binder_result;
use compos_common::odrefresh::{is_system_property_interesting, ODREFRESH_PATH};
@@ -67,28 +66,28 @@
fn initializeSystemProperties(&self, names: &[String], values: &[String]) -> BinderResult<()> {
let mut initialized = self.initialized.write().unwrap();
if initialized.is_some() {
- return Err(new_binder_exception(
+ return Err(Status::new_exception_str(
ExceptionCode::ILLEGAL_STATE,
- format!("Already initialized: {:?}", initialized),
+ Some(format!("Already initialized: {:?}", initialized)),
));
}
*initialized = Some(false);
if names.len() != values.len() {
- return Err(new_binder_exception(
+ return Err(Status::new_exception_str(
ExceptionCode::ILLEGAL_ARGUMENT,
- format!(
+ Some(format!(
"Received inconsistent number of keys ({}) and values ({})",
names.len(),
values.len()
- ),
+ )),
));
}
for (name, value) in zip(names, values) {
if !is_system_property_interesting(name) {
- return Err(new_binder_exception(
+ return Err(Status::new_exception_str(
ExceptionCode::ILLEGAL_ARGUMENT,
- format!("Received invalid system property {}", &name),
+ Some(format!("Received invalid system property {}", &name)),
));
}
let result = system_properties::write(name, value);
@@ -113,9 +112,9 @@
) -> BinderResult<i8> {
let initialized = *self.initialized.read().unwrap();
if !initialized.unwrap_or(false) {
- return Err(new_binder_exception(
+ return Err(Status::new_exception_str(
ExceptionCode::ILLEGAL_STATE,
- "Service has not been initialized",
+ Some("Service has not been initialized"),
));
}
diff --git a/libs/binder_common/lib.rs b/libs/binder_common/lib.rs
index fd81da5..16812b4 100644
--- a/libs/binder_common/lib.rs
+++ b/libs/binder_common/lib.rs
@@ -19,24 +19,3 @@
pub mod lazy_service;
pub mod rpc_client;
pub mod rpc_server;
-
-use binder::{ExceptionCode, Status};
-use std::ffi::CString;
-
-/// Constructs a new Binder error `Status` with the given `ExceptionCode` and message.
-pub fn new_binder_exception<T: AsRef<str>>(exception: ExceptionCode, message: T) -> Status {
- match exception {
- ExceptionCode::SERVICE_SPECIFIC => new_binder_service_specific_error(-1, message),
- _ => Status::new_exception(exception, to_cstring(message).as_deref()),
- }
-}
-
-/// Constructs a Binder `Status` representing a service-specific exception with the given code and
-/// message.
-pub fn new_binder_service_specific_error<T: AsRef<str>>(code: i32, message: T) -> Status {
- Status::new_service_specific_error(code, to_cstring(message).as_deref())
-}
-
-fn to_cstring<T: AsRef<str>>(message: T) -> Option<CString> {
- CString::new(message.as_ref()).ok()
-}
diff --git a/virtualizationservice/src/aidl.rs b/virtualizationservice/src/aidl.rs
index cc8d8a3..b46d5f8 100644
--- a/virtualizationservice/src/aidl.rs
+++ b/virtualizationservice/src/aidl.rs
@@ -45,7 +45,7 @@
},
};
use anyhow::{anyhow, bail, Context, Result};
-use binder_common::{lazy_service::LazyServiceGuard, new_binder_exception, rpc_server::run_rpc_server_with_factory};
+use binder_common::{lazy_service::LazyServiceGuard, rpc_server::run_rpc_server_with_factory};
use disk::QcowFile;
use idsig::{HashAlgorithm, V4Signature};
use log::{debug, error, info, warn, trace};
@@ -160,23 +160,23 @@
) -> binder::Result<()> {
check_manage_access()?;
let size = size.try_into().map_err(|e| {
- new_binder_exception(
+ Status::new_exception_str(
ExceptionCode::ILLEGAL_ARGUMENT,
- format!("Invalid size {}: {}", size, e),
+ Some(format!("Invalid size {}: {}", size, e)),
)
})?;
let image = clone_file(image_fd)?;
// initialize the file. Any data in the file will be erased.
image.set_len(0).map_err(|e| {
- new_binder_exception(
- ExceptionCode::SERVICE_SPECIFIC,
- format!("Failed to reset a file: {}", e),
+ Status::new_service_specific_error_str(
+ -1,
+ Some(format!("Failed to reset a file: {}", e)),
)
})?;
let mut part = QcowFile::new(image, size).map_err(|e| {
- new_binder_exception(
- ExceptionCode::SERVICE_SPECIFIC,
- format!("Failed to create QCOW2 image: {}", e),
+ Status::new_service_specific_error_str(
+ -1,
+ Some(format!("Failed to create QCOW2 image: {}", e)),
)
})?;
@@ -189,9 +189,9 @@
)),
}
.map_err(|e| {
- new_binder_exception(
- ExceptionCode::SERVICE_SPECIFIC,
- format!("Failed to initialize partition as {:?}: {}", partition_type, e),
+ Status::new_service_specific_error_str(
+ -1,
+ Some(format!("Failed to initialize partition as {:?}: {}", partition_type, e)),
)
})?;
@@ -379,12 +379,12 @@
"Failed to create temporary directory {:?} for VM files: {:?}",
temporary_directory, e
);
- new_binder_exception(
- ExceptionCode::SERVICE_SPECIFIC,
- format!(
+ Status::new_service_specific_error_str(
+ -1,
+ Some(format!(
"Failed to create temporary directory {:?} for VM files: {}",
temporary_directory, e
- ),
+ )),
)
})?;
@@ -395,9 +395,12 @@
load_app_config(config, &temporary_directory).map_err(|e| {
error!("Failed to load app config from {}: {:?}", &config.configPath, e);
*is_protected = config.protectedVm;
- new_binder_exception(
- ExceptionCode::SERVICE_SPECIFIC,
- format!("Failed to load app config from {}: {}", &config.configPath, e),
+ Status::new_service_specific_error_str(
+ -1,
+ Some(format!(
+ "Failed to load app config from {}: {}",
+ &config.configPath, e
+ )),
)
})?,
),
@@ -423,14 +426,14 @@
}
})
.try_for_each(check_label_for_partition)
- .map_err(|e| new_binder_exception(ExceptionCode::SERVICE_SPECIFIC, e.to_string()))?;
+ .map_err(|e| Status::new_service_specific_error_str(-1, Some(e.to_string())))?;
let zero_filler_path = temporary_directory.join("zero.img");
write_zero_filler(&zero_filler_path).map_err(|e| {
error!("Failed to make composite image: {:?}", e);
- new_binder_exception(
- ExceptionCode::SERVICE_SPECIFIC,
- format!("Failed to make composite image: {}", e),
+ Status::new_service_specific_error_str(
+ -1,
+ Some(format!("Failed to make composite image: {}", e)),
)
})?;
@@ -456,9 +459,9 @@
let ramdump_path = temporary_directory.join("ramdump");
let ramdump = prepare_ramdump_file(&ramdump_path).map_err(|e| {
error!("Failed to prepare ramdump file: {}", e);
- new_binder_exception(
- ExceptionCode::SERVICE_SPECIFIC,
- format!("Failed to prepare ramdump file: {}", e),
+ Status::new_service_specific_error_str(
+ -1,
+ Some(format!("Failed to prepare ramdump file: {}", e)),
)
})?;
@@ -492,9 +495,9 @@
)
.map_err(|e| {
error!("Failed to create VM with config {:?}: {:?}", config, e);
- new_binder_exception(
- ExceptionCode::SERVICE_SPECIFIC,
- format!("Failed to create VM: {}", e),
+ Status::new_service_specific_error_str(
+ -1,
+ Some(format!("Failed to create VM: {}", e)),
)
})?,
);
@@ -575,9 +578,9 @@
let image = if !disk.partitions.is_empty() {
if disk.image.is_some() {
warn!("DiskImage {:?} contains both image and partitions.", disk);
- return Err(new_binder_exception(
+ return Err(Status::new_exception_str(
ExceptionCode::ILLEGAL_ARGUMENT,
- "DiskImage contains both image and partitions.",
+ Some("DiskImage contains both image and partitions."),
));
}
@@ -592,9 +595,9 @@
)
.map_err(|e| {
error!("Failed to make composite image with config {:?}: {:?}", disk, e);
- new_binder_exception(
- ExceptionCode::SERVICE_SPECIFIC,
- format!("Failed to make composite image: {}", e),
+ Status::new_service_specific_error_str(
+ -1,
+ Some(format!("Failed to make composite image: {}", e)),
)
})?;
@@ -607,9 +610,9 @@
clone_file(image)?
} else {
warn!("DiskImage {:?} didn't contain image or partitions.", disk);
- return Err(new_binder_exception(
+ return Err(Status::new_exception_str(
ExceptionCode::ILLEGAL_ARGUMENT,
- "DiskImage didn't contain image or partitions.",
+ Some("DiskImage didn't contain image or partitions."),
));
};
@@ -700,15 +703,15 @@
Ok(sid) => Ok(sid.to_owned()),
Err(e) => {
error!("SID was not valid UTF-8: {}", e);
- Err(new_binder_exception(
+ Err(Status::new_exception_str(
ExceptionCode::ILLEGAL_ARGUMENT,
- format!("SID was not valid UTF-8: {}", e),
+ Some(format!("SID was not valid UTF-8: {}", e)),
))
}
}
} else {
error!("Missing SID on createVm");
- Err(new_binder_exception(ExceptionCode::SECURITY, "Missing SID on createVm"))
+ Err(Status::new_exception_str(ExceptionCode::SECURITY, Some("Missing SID on createVm")))
}
})
}
@@ -726,9 +729,9 @@
if perm_svc.checkPermission(perm, calling_pid, calling_uid as i32)? {
Ok(())
} else {
- Err(new_binder_exception(
+ Err(Status::new_exception_str(
ExceptionCode::SECURITY,
- format!("does not have the {} permission", perm),
+ Some(format!("does not have the {} permission", perm)),
))
}
}
@@ -804,26 +807,26 @@
fn start(&self) -> binder::Result<()> {
self.instance.start().map_err(|e| {
error!("Error starting VM with CID {}: {:?}", self.instance.cid, e);
- new_binder_exception(ExceptionCode::SERVICE_SPECIFIC, e.to_string())
+ Status::new_service_specific_error_str(-1, Some(e.to_string()))
})
}
fn stop(&self) -> binder::Result<()> {
self.instance.kill().map_err(|e| {
error!("Error stopping VM with CID {}: {:?}", self.instance.cid, e);
- new_binder_exception(ExceptionCode::SERVICE_SPECIFIC, e.to_string())
+ Status::new_service_specific_error_str(-1, Some(e.to_string()))
})
}
fn connectVsock(&self, port: i32) -> binder::Result<ParcelFileDescriptor> {
if !matches!(&*self.instance.vm_state.lock().unwrap(), VmState::Running { .. }) {
- return Err(new_binder_exception(ExceptionCode::SERVICE_SPECIFIC, "VM is not running"));
+ return Err(Status::new_service_specific_error_str(-1, Some("VM is not running")));
}
let stream =
VsockStream::connect_with_cid_port(self.instance.cid, port as u32).map_err(|e| {
- new_binder_exception(
- ExceptionCode::SERVICE_SPECIFIC,
- format!("Failed to connect: {}", e),
+ Status::new_service_specific_error_str(
+ -1,
+ Some(format!("Failed to connect: {}", e)),
)
})?;
Ok(vsock_stream_to_pfd(stream))
@@ -1002,9 +1005,9 @@
/// Converts a `&ParcelFileDescriptor` to a `File` by cloning the file.
fn clone_file(file: &ParcelFileDescriptor) -> Result<File, Status> {
file.as_ref().try_clone().map_err(|e| {
- new_binder_exception(
+ Status::new_exception_str(
ExceptionCode::BAD_PARCELABLE,
- format!("Failed to clone File from ParcelFileDescriptor: {}", e),
+ Some(format!("Failed to clone File from ParcelFileDescriptor: {}", e)),
)
})
}
@@ -1024,9 +1027,9 @@
/// Parses the platform version requirement string.
fn parse_platform_version_req(s: &str) -> Result<VersionReq, Status> {
VersionReq::parse(s).map_err(|e| {
- new_binder_exception(
+ Status::new_exception_str(
ExceptionCode::BAD_PARCELABLE,
- format!("Invalid platform version requirement {}: {}", s, e),
+ Some(format!("Invalid platform version requirement {}: {}", s, e)),
)
})
}
@@ -1061,16 +1064,17 @@
let cid = self.cid;
if let Some(vm) = self.state.lock().unwrap().get_vm(cid) {
info!("VM having CID {} started payload", cid);
- vm.update_payload_state(PayloadState::Started)
- .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string()))?;
+ vm.update_payload_state(PayloadState::Started).map_err(|e| {
+ Status::new_exception_str(ExceptionCode::ILLEGAL_STATE, Some(e.to_string()))
+ })?;
let stream = vm.stream.lock().unwrap().take();
vm.callbacks.notify_payload_started(cid, stream);
Ok(())
} else {
error!("notifyPayloadStarted is called from an unknown CID {}", cid);
- Err(new_binder_exception(
- ExceptionCode::SERVICE_SPECIFIC,
- format!("cannot find a VM with CID {}", cid),
+ Err(Status::new_service_specific_error_str(
+ -1,
+ Some(format!("cannot find a VM with CID {}", cid)),
))
}
}
@@ -1079,15 +1083,16 @@
let cid = self.cid;
if let Some(vm) = self.state.lock().unwrap().get_vm(cid) {
info!("VM having CID {} payload is ready", cid);
- vm.update_payload_state(PayloadState::Ready)
- .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string()))?;
+ vm.update_payload_state(PayloadState::Ready).map_err(|e| {
+ Status::new_exception_str(ExceptionCode::ILLEGAL_STATE, Some(e.to_string()))
+ })?;
vm.callbacks.notify_payload_ready(cid);
Ok(())
} else {
error!("notifyPayloadReady is called from an unknown CID {}", cid);
- Err(new_binder_exception(
- ExceptionCode::SERVICE_SPECIFIC,
- format!("cannot find a VM with CID {}", cid),
+ Err(Status::new_service_specific_error_str(
+ -1,
+ Some(format!("cannot find a VM with CID {}", cid)),
))
}
}
@@ -1096,15 +1101,16 @@
let cid = self.cid;
if let Some(vm) = self.state.lock().unwrap().get_vm(cid) {
info!("VM having CID {} finished payload", cid);
- vm.update_payload_state(PayloadState::Finished)
- .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string()))?;
+ vm.update_payload_state(PayloadState::Finished).map_err(|e| {
+ Status::new_exception_str(ExceptionCode::ILLEGAL_STATE, Some(e.to_string()))
+ })?;
vm.callbacks.notify_payload_finished(cid, exit_code);
Ok(())
} else {
error!("notifyPayloadFinished is called from an unknown CID {}", cid);
- Err(new_binder_exception(
- ExceptionCode::SERVICE_SPECIFIC,
- format!("cannot find a VM with CID {}", cid),
+ Err(Status::new_service_specific_error_str(
+ -1,
+ Some(format!("cannot find a VM with CID {}", cid)),
))
}
}
@@ -1113,15 +1119,16 @@
let cid = self.cid;
if let Some(vm) = self.state.lock().unwrap().get_vm(cid) {
info!("VM having CID {} encountered an error", cid);
- vm.update_payload_state(PayloadState::Finished)
- .map_err(|e| new_binder_exception(ExceptionCode::ILLEGAL_STATE, e.to_string()))?;
+ vm.update_payload_state(PayloadState::Finished).map_err(|e| {
+ Status::new_exception_str(ExceptionCode::ILLEGAL_STATE, Some(e.to_string()))
+ })?;
vm.callbacks.notify_error(cid, error_code, message);
Ok(())
} else {
error!("notifyError is called from an unknown CID {}", cid);
- Err(new_binder_exception(
- ExceptionCode::SERVICE_SPECIFIC,
- format!("cannot find a VM with CID {}", cid),
+ Err(Status::new_service_specific_error_str(
+ -1,
+ Some(format!("cannot find a VM with CID {}", cid)),
))
}
}