Merge changes Ibe1a854b,I8c281ad2,I9e290cd0,I0035be2d
* changes:
libbinder: RPC limit on oneway transactions
libbinder: RPC state termination shutdown session
libbinder: server sessions shut down independently
libbinder: remove unused FdTrigger::readFd
diff --git a/libs/binder/Android.bp b/libs/binder/Android.bp
index 9d58d87..b9c8d20 100644
--- a/libs/binder/Android.bp
+++ b/libs/binder/Android.bp
@@ -309,3 +309,17 @@
export_aidl_headers: true,
},
}
+
+cc_binary {
+ name: "servicedispatcher",
+ host_supported: false,
+ srcs: [
+ "servicedispatcher.cpp",
+ ],
+ shared_libs: [
+ "libbase",
+ "libbinder",
+ "liblog",
+ "libutils",
+ ],
+}
diff --git a/libs/binder/rust/src/lib.rs b/libs/binder/rust/src/lib.rs
index 2694cba..7c0584b 100644
--- a/libs/binder/rust/src/lib.rs
+++ b/libs/binder/rust/src/lib.rs
@@ -115,14 +115,14 @@
pub use native::add_service;
pub use native::Binder;
pub use parcel::Parcel;
-pub use proxy::{get_interface, get_service};
+pub use proxy::{get_interface, get_service, wait_for_interface, wait_for_service};
pub use proxy::{AssociateClass, DeathRecipient, Proxy, SpIBinder, WpIBinder};
pub use state::{ProcessState, ThreadState};
/// The public API usable outside AIDL-generated interface crates.
pub mod public_api {
pub use super::parcel::ParcelFileDescriptor;
- pub use super::{add_service, get_interface};
+ pub use super::{add_service, get_interface, wait_for_interface};
pub use super::{
BinderFeatures, DeathRecipient, ExceptionCode, IBinder, Interface, ProcessState, SpIBinder,
Status, StatusCode, Strong, ThreadState, Weak, WpIBinder,
diff --git a/libs/binder/rust/src/proxy.rs b/libs/binder/rust/src/proxy.rs
index 52036f5..4a6d118 100644
--- a/libs/binder/rust/src/proxy.rs
+++ b/libs/binder/rust/src/proxy.rs
@@ -653,6 +653,18 @@
}
}
+/// Retrieve an existing service, or start it if it is configured as a dynamic
+/// service and isn't yet started.
+pub fn wait_for_service(name: &str) -> Option<SpIBinder> {
+ let name = CString::new(name).ok()?;
+ unsafe {
+ // Safety: `AServiceManager_waitforService` returns either a null
+ // pointer or a valid pointer to an owned `AIBinder`. Either of these
+ // values is safe to pass to `SpIBinder::from_raw`.
+ SpIBinder::from_raw(sys::AServiceManager_waitForService(name.as_ptr()))
+ }
+}
+
/// Retrieve an existing service for a particular interface, blocking for a few
/// seconds if it doesn't yet exist.
pub fn get_interface<T: FromIBinder + ?Sized>(name: &str) -> Result<Strong<T>> {
@@ -663,6 +675,16 @@
}
}
+/// Retrieve an existing service for a particular interface, or start it if it
+/// is configured as a dynamic service and isn't yet started.
+pub fn wait_for_interface<T: FromIBinder + ?Sized>(name: &str) -> Result<Strong<T>> {
+ let service = wait_for_service(name);
+ match service {
+ Some(service) => FromIBinder::try_from(service),
+ None => Err(StatusCode::NAME_NOT_FOUND),
+ }
+}
+
/// # Safety
///
/// `SpIBinder` guarantees that `binder` always contains a valid pointer to an
diff --git a/libs/binder/rust/tests/integration.rs b/libs/binder/rust/tests/integration.rs
index 0332007..10b77f4 100644
--- a/libs/binder/rust/tests/integration.rs
+++ b/libs/binder/rust/tests/integration.rs
@@ -274,6 +274,20 @@
}
#[test]
+ fn check_wait_for_service() {
+ let mut sm =
+ binder::wait_for_service("manager").expect("Did not get manager binder service");
+ assert!(sm.is_binder_alive());
+ assert!(sm.ping_binder().is_ok());
+
+ // The service manager service isn't an ITest, so this must fail.
+ assert_eq!(
+ binder::wait_for_interface::<dyn ITest>("manager").err(),
+ Some(StatusCode::BAD_TYPE)
+ );
+ }
+
+ #[test]
fn trivial_client() {
let service_name = "trivial_client_test";
let _process = ScopedServiceProcess::new(service_name);
@@ -283,6 +297,15 @@
}
#[test]
+ fn wait_for_trivial_client() {
+ let service_name = "wait_for_trivial_client_test";
+ let _process = ScopedServiceProcess::new(service_name);
+ let test_client: Strong<dyn ITest> =
+ binder::wait_for_interface(service_name).expect("Did not get manager binder service");
+ assert_eq!(test_client.test().unwrap(), "wait_for_trivial_client_test");
+ }
+
+ #[test]
fn get_selinux_context() {
let service_name = "get_selinux_context";
let _process = ScopedServiceProcess::new(service_name);
diff --git a/libs/binder/servicedispatcher.cpp b/libs/binder/servicedispatcher.cpp
new file mode 100644
index 0000000..f61df08
--- /dev/null
+++ b/libs/binder/servicedispatcher.cpp
@@ -0,0 +1,138 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * 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.
+ */
+
+#include <stdint.h>
+#include <sysexits.h>
+#include <unistd.h>
+
+#include <iostream>
+
+#include <android-base/file.h>
+#include <android-base/logging.h>
+#include <android-base/parseint.h>
+#include <android-base/properties.h>
+#include <android-base/stringprintf.h>
+#include <binder/IServiceManager.h>
+#include <binder/RpcServer.h>
+
+using android::defaultServiceManager;
+using android::OK;
+using android::RpcServer;
+using android::statusToString;
+using android::String16;
+using android::base::Basename;
+using android::base::GetBoolProperty;
+using android::base::InitLogging;
+using android::base::LogdLogger;
+using android::base::LogId;
+using android::base::LogSeverity;
+using android::base::ParseUint;
+using android::base::StdioLogger;
+using android::base::StringPrintf;
+
+namespace {
+int Usage(const char* program) {
+ auto format = R"(dispatch calls to RPC service.
+Usage:
+ %s [-n <num_threads>] <service_name>
+ -n <num_threads>: number of RPC threads added to the service (default 1).
+ <service_name>: the service to connect to.
+)";
+ LOG(ERROR) << StringPrintf(format, Basename(program).c_str());
+ return EX_USAGE;
+}
+
+int Dispatch(const char* name, uint32_t numThreads) {
+ auto sm = defaultServiceManager();
+ if (nullptr == sm) {
+ LOG(ERROR) << "No servicemanager";
+ return EX_SOFTWARE;
+ }
+ auto binder = sm->checkService(String16(name));
+ if (nullptr == binder) {
+ LOG(ERROR) << "No service \"" << name << "\"";
+ return EX_SOFTWARE;
+ }
+ auto rpcServer = RpcServer::make();
+ if (nullptr == rpcServer) {
+ LOG(ERROR) << "Cannot create RpcServer";
+ return EX_SOFTWARE;
+ }
+ rpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction();
+ unsigned int port;
+ if (!rpcServer->setupInetServer(0, &port)) {
+ LOG(ERROR) << "setupInetServer failed";
+ return EX_SOFTWARE;
+ }
+ auto socket = rpcServer->releaseServer();
+ auto status = binder->setRpcClientDebug(std::move(socket), numThreads);
+ if (status != OK) {
+ LOG(ERROR) << "setRpcClientDebug failed with " << statusToString(status);
+ return EX_SOFTWARE;
+ }
+ LOG(INFO) << "Finish setting up RPC on service " << name << " with " << numThreads
+ << " threads on port" << port;
+
+ std::cout << port << std::endl;
+ return EX_OK;
+}
+
+// Log to logd. For warning and more severe messages, also log to stderr.
+class ServiceDispatcherLogger {
+public:
+ void operator()(LogId id, LogSeverity severity, const char* tag, const char* file,
+ unsigned int line, const char* message) {
+ mLogdLogger(id, severity, tag, file, line, message);
+ if (severity >= LogSeverity::WARNING) {
+ std::cout << std::flush;
+ std::cerr << Basename(getprogname()) << ": " << message << std::endl;
+ }
+ }
+
+private:
+ LogdLogger mLogdLogger{};
+};
+
+} // namespace
+
+int main(int argc, char* argv[]) {
+ InitLogging(argv, ServiceDispatcherLogger());
+
+ if (!GetBoolProperty("ro.debuggable", false)) {
+ LOG(ERROR) << "servicedispatcher is only allowed on debuggable builds.";
+ return EX_NOPERM;
+ }
+ LOG(WARNING) << "WARNING: servicedispatcher is debug only. Use with caution.";
+
+ uint32_t numThreads = 1;
+ int opt;
+ while (-1 != (opt = getopt(argc, argv, "n:"))) {
+ switch (opt) {
+ case 'n': {
+ if (!ParseUint(optarg, &numThreads)) {
+ return Usage(argv[0]);
+ }
+ } break;
+ default: {
+ return Usage(argv[0]);
+ }
+ }
+ }
+ if (optind + 1 != argc) return Usage(argv[0]);
+ auto name = argv[optind];
+
+ return Dispatch(name, numThreads);
+}