1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright 2022 Jeff Kim <hiking90@gmail.com>
// SPDX-License-Identifier: Apache-2.0
//! A pure Rust implementation of Android Binder IPC mechanism.
//!
//! This library provides a complete implementation of the Android Binder protocol
//! for inter-process communication (IPC) on Linux and Android systems. It enables
//! services to communicate across process boundaries with type safety and efficiency.
//!
//! # Core Components
//!
//! - **Binder**: Core binder object and transaction handling
//! - **Parcel**: Serialization/deserialization for IPC data
//! - **Proxy**: Client-side interface for remote services
//! - **Native**: Server-side service implementation utilities
//! - **ProcessState**: Process-level binder state management
//! - **ServiceManager**: Service discovery and registration (the [`hub`] module)
//! - **RPC transport**: binder-over-socket (`rpc` module, behind the `rpc`
//! feature) — a separate stack from the kernel binder path
//! - **Service facade**: the [`service`] module — register/look up services
//! once, choosing kernel binder or RPC by construction
//!
//! # Feature flags
//!
//! - `tokio` *(default)* — full async/await support on the Tokio runtime
//! (implies `async`).
//! - `async` — generic async-trait support, without committing to a
//! specific runtime.
//! - `rpc` — master switch for the RPC transport (binder-over-socket), a
//! separate stack from the kernel binder path. Off by default; default
//! and `--no-default-features` builds carry zero RPC/networking cost.
//! - `rpc-tcp-debug` — plaintext TCP backend (implies `rpc`). Debug and
//! interop bring-up **only**, never production — use `rpc-tls` for real
//! networks.
//! - `rpc-vsock` — vsock backend for host↔VM channels (implies `rpc`;
//! Linux and Android targets only).
//! - `rpc-tls` — TLS backend over rustls (implies `rpc`). rsbinder never
//! invents crypto; the caller supplies the `rustls` configuration.
//! - `android_10` … `android_16`, plus the `android_*_plus` ranges (e.g.
//! `android_11_plus`) — select which Android service-manager protocol
//! versions to support. Android 10 uses the legacy C service-manager
//! protocol. Android 15 and 17 need no dedicated flag: their
//! service-manager wire format is identical to Android 14 and
//! Android 16 respectively, so they are served by `android_14` /
//! `android_16`.
//!
//! # Basic Usage
//!
//! This library works with AIDL (Android Interface Definition Language) files to generate
//! type-safe Rust bindings for IPC services.
//!
//! ## Setting up an AIDL-based Service
//!
//! First, create an AIDL interface file (`aidl/hello/IHello.aidl`):
//!
//! ```aidl
//! package hello;
//!
//! interface IHello {
//! String echo(in String message);
//! }
//! ```
//!
//! Add a `build.rs` file to generate Rust bindings:
//!
//! ```rust,ignore
//! # use std::path::PathBuf;
//! rsbinder_aidl::Builder::new()
//! .source(PathBuf::from("aidl/hello/IHello.aidl"))
//! .output(PathBuf::from("hello.rs"))
//! .generate()
//! .unwrap();
//! ```
//!
//! In your `Cargo.toml`, add the build dependency:
//!
//! ```toml
//! [build-dependencies]
//! rsbinder-aidl = "*" # Check crates.io for the latest version
//! ```
//!
//! ## Implementing the Service
//!
//! ```rust,ignore
//! use rsbinder::*;
//!
//! // Include the generated code
//! rsbinder::include_aidl!("hello", crate::hello::IHello::*);
//!
//! // Implement the service
//! struct HelloService;
//!
//! impl Interface for HelloService {}
//!
//! impl IHello for HelloService {
//! fn echo(&self, message: &str) -> rsbinder::BinderResult<String> {
//! Ok(format!("Echo: {}", message))
//! }
//! }
//!
//! # fn main() -> Result<()> {
//! // Initialize the process state
//! ProcessState::init_default()?;
//!
//! // Start the thread pool
//! ProcessState::start_thread_pool();
//!
//! // Register your service
//! let service = BnHello::new_binder(HelloService);
//! hub::add_service("hello_service", service.as_binder())?;
//!
//! println!("Hello service started");
//!
//! // Join the thread pool to handle requests
//! ProcessState::join_thread_pool();
//! # Ok(())
//! # }
//! ```
//!
//! ## Creating a Client
//!
//! ```rust,ignore
//! use rsbinder::*;
//!
//! // Include the same generated code
//! rsbinder::include_aidl!("hello", crate::hello::IHello::*);
//!
//! # fn main() -> Result<()> {
//! // Initialize the process state
//! ProcessState::init_default()?;
//!
//! // Wait for the service to be registered, then cast it to the interface.
//! let hello_service: rsbinder::Strong<dyn IHello> =
//! hub::wait_for_interface("hello_service")?;
//!
//! // Call remote method
//! let result = hello_service.echo("Hello, World!")?;
//! println!("Service response: {}", result);
//! # Ok(())
//! # }
//! ```
//!
//! ## Opting into kernel-level features
//!
//! Native binders can opt into per-binder kernel features (for example,
//! receiving the caller's SELinux security context) via [`BinderFeatures`]
//! and the `*_with_features` constructor variants. See [`BinderFeatures`]
//! for the available opt-ins.
//!
//! # License
//!
//! Licensed under Apache License, Version 2.0.
//!
//! # References
//!
//! * [AIDL](https://source.android.com/docs/core/architecture/aidl)
//! * [Binder](https://source.android.com/docs/core/architecture/hidl/binder-ipc)
//!
// Core binder functionality
/// Async binder runtime support
/// BinderFS filesystem utilities
/// Error types and result handling
/// File descriptor wrapper for IPC
/// `LazyServiceRegistrar` skeleton (state machine +
/// `IClientCallback::onClients` dispatch). AOSP
/// `frameworks/native/libs/binder/LazyServiceRegistrar.cpp`. Hub
/// integration (actual `registerClientCallback` / `tryUnregisterService`
/// IPC) is owed by the caller until the hub plumbing lands — see the
/// module docs.
/// Native service implementation helpers
/// Data serialization for IPC
/// Parcelable trait for serializable types
/// Holder for parcelable objects
/// Client proxy for remote services
/// Process-global proxy count + watermark callbacks
/// with opt-in per-uid tracking. AOSP `BpBinder` proxy-count surface
/// (`getBinderProxyCount` / `setBinderProxyCountWatermarks` /
/// `setBinderProxyCountEventCallback` / `enableCountByUid`).
/// Shared-memory IPC trait skeleton
/// (`IMemoryHeap` / `IMemory` / `MemoryHeapBase`). AOSP
/// `frameworks/native/libs/binder/include/binder/IMemory.h`. A future
/// `memfd_create(2)`-backed `MemoryHeapBase` impl on Linux/Android and
/// a `MemoryDealer` chunk allocator are not yet implemented. macOS
/// host build compiles the trait surface but `MemoryHeapBase::new`
/// permanently returns `Err(StatusCode::InvalidOperation)` — see the
/// module docs.
/// Status and exception handling
/// Thread-local binder state
/// RPC transport (binder-over-socket) — a separate stack from the
/// kernel binder path. Present only with the `rpc` feature.
/// Service hub and manager implementations
/// Client stub for Android's `PermissionManagerService`
/// (`android.os.IPermissionController`). See module doc for the
/// AOSP-faithful surface and fail-closed `check_permission` helper.
/// Async runtime implementations
// Cross-transport service facade — see the module's own docs. Kept as a
// plain (non-doc) comment: an outer doc here would merge with the
// module's inner `//!` docs and re-resolve their intra-doc links at the
// crate root, breaking them.
// Explicit re-exports: glob re-exports would silently leak every
// newly-added `pub` item in these modules, defeating semver review.
// Items kept out of the crate root must be reached via
// `rsbinder::<module>::<item>`.
// From `binder` — core binder identity, transaction codes, traits.
pub use ;
// `declare_binder_interface!` expands to `$crate::__rpc_stamp_descriptor(...)`
// in consumer crates, so this helper must stay reachable at the crate root.
pub use __rpc_stamp_descriptor;
pub use ;
pub use ;
pub use ParcelFileDescriptor;
// From `native` — server-side binder construction.
pub use ;
// From `thread_state` — calling identity (AOSP
// `IPCThreadState::getCalling*`). Kernel-path only; the RPC stack has
// its own `PeerIdentity` model under `rpc::PeerIdentity`.
pub use ;
pub use Parcel;
// From `parcelable` — (de)serialization trait stack.
pub use ;
pub use ParcelableHolder;
pub use ProcessState;
// From `proxy` — client-side handle types.
pub use ;
pub use *;
pub use ;
/// Default path to the binder control device
pub const DEFAULT_BINDER_CONTROL_PATH: &str = "/dev/binderfs/binder-control";
/// Default path to the binder device (binderfs, used on Android 11+).
pub const DEFAULT_BINDER_PATH: &str = "/dev/binderfs/binder";
/// Default path to the binderfs mount point
pub const DEFAULT_BINDERFS_PATH: &str = "/dev/binderfs";
/// Legacy binder device path used on Android 10 and earlier.
pub const LEGACY_BINDER_PATH: &str = "/dev/binder";
/// Returns `true` when the runtime Android SDK version is at least `version`.
/// On non-Android platforms this always returns `true`.
static ANDROID_SDK_VERSION: OnceLock = new;
/// Get the Android SDK version from system properties.
///
/// Returns the Android SDK version number, or 0 if not available.