diff --git a/Cargo.lock b/Cargo.lock index 37c0d0d..904d7ea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -124,9 +124,9 @@ dependencies = [ [[package]] name = "bitflags" -version = "2.5.0" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" [[package]] name = "block-buffer" @@ -161,9 +161,9 @@ checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" [[package]] name = "cc" -version = "1.0.99" +version = "1.0.101" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96c51067fd44124faa7f870b4b1c969379ad32b2ba805aa959430ceaa384f695" +checksum = "ac367972e516d45567c7eafc73d24e1c193dcf200a8d94e9db7b3d38b349572d" [[package]] name = "cfg-if" @@ -171,12 +171,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "cfg_aliases" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" - [[package]] name = "chrono" version = "0.4.38" @@ -321,6 +315,16 @@ dependencies = [ "log", ] +[[package]] +name = "errno" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "example-hello" version = "0.2.4" @@ -466,9 +470,9 @@ dependencies = [ [[package]] name = "lazy_static" -version = "1.4.0" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" @@ -482,6 +486,12 @@ version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" +[[package]] +name = "linux-raw-sys" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" + [[package]] name = "lock_api" version = "0.4.12" @@ -506,9 +516,9 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "miniz_oxide" -version = "0.7.3" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87dfd01fe195c66b572b37921ad8803d010623c0aca821bea2302239d155cdae" +checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08" dependencies = [ "adler", ] @@ -524,18 +534,6 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "nix" -version = "0.28.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab2156c4fce2f8df6c499cc1c763e4394b7482525bf2a9701c9d79d215f519e4" -dependencies = [ - "bitflags", - "cfg-if", - "cfg_aliases", - "libc", -] - [[package]] name = "num-traits" version = "0.2.19" @@ -711,9 +709,9 @@ checksum = "bbc83ee4a840062f368f9096d80077a9841ec117e17e7f700df81958f1451254" [[package]] name = "proc-macro2" -version = "1.0.85" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22244ce15aa966053a896d1accb3a6e68469b97c7f33f284b99f0d576879fc23" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" dependencies = [ "unicode-ident", ] @@ -804,9 +802,9 @@ dependencies = [ "env_logger", "lazy_static", "log", - "nix", "pretty-hex", "rsbinder-aidl", + "rustix", "tokio", ] @@ -830,9 +828,9 @@ dependencies = [ "async-trait", "env_logger", "lazy_static", - "nix", "rsbinder", "rsbinder-aidl", + "rustix", "tokio", ] @@ -844,7 +842,6 @@ dependencies = [ "env_logger", "lazy_static", "log", - "nix", "rsbinder", ] @@ -854,6 +851,19 @@ version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" +[[package]] +name = "rustix" +version = "0.38.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.52.0", +] + [[package]] name = "ryu" version = "1.0.18" @@ -897,9 +907,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.117" +version = "1.0.118" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "455182ea6142b14f93f4bc5320a2b31c1f266b66a4a5c858b013302a5d8cbfc3" +checksum = "d947f6b3163d8857ea16c4fa0dd4840d52f3041039a85decd46867eb1abef2e4" dependencies = [ "itoa", "ryu", @@ -966,9 +976,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.66" +version = "2.0.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5" +checksum = "901fa70d88b9d6c98022e23b4136f9f3e54e4662c3bc1bd1d84a42a9a0f0c1e9" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 436a54e..51c8da6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,6 @@ lazy_static = "1.4" rsbinder = { version = "0.2.4", path = "rsbinder", default-features = false } log = "0.4" env_logger = "0.11" -nix = "0.28" anstyle = "1.0" tokio = { version = "1.37", default-features = false } async-trait = "0.1" @@ -35,3 +34,4 @@ tera = "1.19" similar = "2.4" pretty_hex = { version = "0.4", package = "pretty-hex" } downcast-rs = "1.2" +rustix = "0.38" \ No newline at end of file diff --git a/rsbinder-aidl/src/parser.rs b/rsbinder-aidl/src/parser.rs index 90edf13..4bfd212 100644 --- a/rsbinder-aidl/src/parser.rs +++ b/rsbinder-aidl/src/parser.rs @@ -206,10 +206,6 @@ pub fn name_to_const_expr(name: &str) -> Option { lookup_name_from_decl(&lookup_decl.decl, &lookup_decl) } -#[derive(Debug)] -pub struct Interface { -} - #[derive(Debug)] pub struct Document { pub package: Option, diff --git a/rsbinder-tests/Cargo.toml b/rsbinder-tests/Cargo.toml index aec1753..69ade4e 100644 --- a/rsbinder-tests/Cargo.toml +++ b/rsbinder-tests/Cargo.toml @@ -11,7 +11,7 @@ lazy_static = { workspace = true } rsbinder = { workspace = true } tokio = { workspace = true } env_logger = { workspace = true } -nix = { workspace = true } +rustix = { workspace = true, features = ["pipe"] } async-trait = { workspace = true } [build-dependencies] diff --git a/rsbinder-tests/src/test_client.rs b/rsbinder-tests/src/test_client.rs index 4786a05..1438965 100644 --- a/rsbinder-tests/src/test_client.rs +++ b/rsbinder-tests/src/test_client.rs @@ -40,6 +40,7 @@ use android::aidl::versioned::tests::{ use android::aidl::tests::vintf::{ VintfExtendableParcelable::VintfExtendableParcelable, VintfParcelable::VintfParcelable, }; +use rustix::fd::OwnedFd; use std::{fs::File, os::fd::IntoRawFd}; use std::io::{Read, Write}; use std::os::unix::io::FromRawFd; @@ -321,7 +322,7 @@ fn test_interface_list_exchange() { } fn build_pipe() -> (File, File) { - let fds = nix::unistd::pipe().expect("error creating pipe"); + let fds = rustix::pipe::pipe().expect("error creating pipe"); // Safety: we get two file descriptors from pipe() // and pass them after checking if the function returned // without an error, so the descriptors should be valid diff --git a/rsbinder-tools/Cargo.toml b/rsbinder-tools/Cargo.toml index c7d2c66..ff157d3 100644 --- a/rsbinder-tools/Cargo.toml +++ b/rsbinder-tools/Cargo.toml @@ -15,5 +15,4 @@ lazy_static = { workspace = true } rsbinder = { workspace = true } log = { workspace = true } env_logger = { workspace = true } -nix = { workspace = true } anstyle = { workspace = true } diff --git a/rsbinder/Cargo.toml b/rsbinder/Cargo.toml index 9b4e9ad..a0b4e5c 100644 --- a/rsbinder/Cargo.toml +++ b/rsbinder/Cargo.toml @@ -18,7 +18,7 @@ tokio = ["async", "tokio/full"] async = ["rsbinder-aidl/async", "async-trait"] [dependencies] -nix = { workspace = true, features = ["ioctl", "mount", "fs", "feature", "mman", "process"] } +rustix = { workspace = true, features = ["process", "param", "mm"] } log = { workspace = true } pretty_hex = { workspace = true } downcast-rs = { workspace = true } diff --git a/rsbinder/src/binder_object.rs b/rsbinder/src/binder_object.rs index 6f3fc44..aba9b5a 100644 --- a/rsbinder/src/binder_object.rs +++ b/rsbinder/src/binder_object.rs @@ -3,6 +3,8 @@ use std::mem::ManuallyDrop; +use rustix::fd::{BorrowedFd, FromRawFd, OwnedFd}; + pub(crate) use crate::sys::binder::flat_binder_object; use crate::{ binder::*, @@ -48,6 +50,14 @@ impl flat_binder_object { unsafe { self.__bindgen_anon_1.handle } } + pub(crate) fn borrowed_fd(&self) -> BorrowedFd { + unsafe { BorrowedFd::borrow_raw(self.handle() as _) } + } + + pub(crate) fn owned_fd(&self) -> OwnedFd { + unsafe { OwnedFd::from_raw_fd(self.handle() as _) } + } + pub(crate) fn set_handle(&mut self, handle: u32) { self.__bindgen_anon_1.handle = handle } @@ -101,8 +111,9 @@ impl flat_binder_object { process_state::ProcessState::as_self().strong_proxy_for_handle(self.handle())?.decrease() } BINDER_TYPE_FD => { - if self.cookie != 0 { // owned - nix::unistd::close(self.handle() as _)?; + if self.cookie != 0 { + // Get owned fd and close it. + self.owned_fd(); } Ok(()) diff --git a/rsbinder/src/binderfs.rs b/rsbinder/src/binderfs.rs index 43c5141..f5fa06f 100644 --- a/rsbinder/src/binderfs.rs +++ b/rsbinder/src/binderfs.rs @@ -3,7 +3,6 @@ use std::path::Path; use std::fs::File; -use std::os::unix::io::{AsRawFd}; use log; use crate::sys::binder; use std::ffi::CString; @@ -27,15 +26,11 @@ pub fn add_device(driver: &Path, name: &str) -> std::io::Result<(u32, u32)> { *a = *c as std::os::raw::c_char; } - unsafe { - binder::binder_ctl_add(fd.as_raw_fd(), &mut device) - .map_err(|e| { - log::error!("Binder ioctl to add binder failed: {}", e.to_string()); - e - })?; - } - - drop(fd); + binder::binder_ctl_add(fd, &mut device) + .map_err(|e| { + log::error!("Binder ioctl to add binder failed: {}", e.to_string()); + e + })?; Ok((device.major, device.minor)) } diff --git a/rsbinder/src/error.rs b/rsbinder/src/error.rs index c8d2529..bf91900 100644 --- a/rsbinder/src/error.rs +++ b/rsbinder/src/error.rs @@ -72,24 +72,24 @@ impl From for i32 { match code { StatusCode::Ok => 0, StatusCode::Unknown => UNKNOWN_ERROR as _, - StatusCode::NoMemory => -(nix::errno::Errno::ENOMEM as i32), - StatusCode::InvalidOperation => -(nix::errno::Errno::ENOSYS as i32), - StatusCode::BadValue => -(nix::errno::Errno::EINVAL as i32), + StatusCode::NoMemory => -(rustix::io::Errno::NOMEM.raw_os_error()), + StatusCode::InvalidOperation => -(rustix::io::Errno::NOSYS.raw_os_error()), + StatusCode::BadValue => -(rustix::io::Errno::INVAL.raw_os_error()), StatusCode::BadType => UNKNOWN_ERROR + 1, - StatusCode::NameNotFound => -(nix::errno::Errno::ENOENT as i32), - StatusCode::PermissionDenied => -(nix::errno::Errno::EPERM as i32), - StatusCode::NoInit => -(nix::errno::Errno::ENODEV as i32), - StatusCode::AlreadyExists => -(nix::errno::Errno::EEXIST as i32), - StatusCode::DeadObject => -(nix::errno::Errno::EPIPE as i32), + StatusCode::NameNotFound => -(rustix::io::Errno::NOENT.raw_os_error()), + StatusCode::PermissionDenied => -(rustix::io::Errno::PERM.raw_os_error()), + StatusCode::NoInit => -(rustix::io::Errno::NODEV.raw_os_error()), + StatusCode::AlreadyExists => -(rustix::io::Errno::EXIST.raw_os_error()), + StatusCode::DeadObject => -(rustix::io::Errno::PIPE.raw_os_error()), StatusCode::FailedTransaction => UNKNOWN_ERROR + 2, - StatusCode::UnknownTransaction => -(nix::errno::Errno::EBADMSG as i32), - StatusCode::BadIndex => -(nix::errno::Errno::EOVERFLOW as i32), + StatusCode::UnknownTransaction => -(rustix::io::Errno::BADMSG.raw_os_error()), + StatusCode::BadIndex => -(rustix::io::Errno::OVERFLOW.raw_os_error()), StatusCode::FdsNotAllowed => UNKNOWN_ERROR + 7, StatusCode::UnexpectedNull => UNKNOWN_ERROR + 8, - StatusCode::NotEnoughData => -(nix::errno::Errno::ENODATA as i32), - StatusCode::WouldBlock => -(nix::errno::Errno::EWOULDBLOCK as i32), - StatusCode::TimedOut => -(nix::errno::Errno::ETIMEDOUT as i32), - StatusCode::BadFd => -(nix::errno::Errno::EBADF as i32), + StatusCode::NotEnoughData => -(rustix::io::Errno::NODATA.raw_os_error()), + StatusCode::WouldBlock => -(rustix::io::Errno::WOULDBLOCK.raw_os_error()), + StatusCode::TimedOut => -(rustix::io::Errno::TIMEDOUT.raw_os_error()), + StatusCode::BadFd => -(rustix::io::Errno::BADF.raw_os_error()), StatusCode::ServiceSpecific(v) => v, StatusCode::Errno(errno) => errno, } @@ -138,24 +138,24 @@ impl From for StatusCode { } } -impl From for StatusCode { - fn from(errno: nix::errno::Errno) -> Self { +impl From for StatusCode { + fn from(errno: rustix::io::Errno) -> Self { match errno { - nix::errno::Errno::ENOMEM => StatusCode::NoMemory, - nix::errno::Errno::ENOSYS => StatusCode::InvalidOperation, - nix::errno::Errno::EINVAL => StatusCode::BadValue, - nix::errno::Errno::ENOENT => StatusCode::NameNotFound, - nix::errno::Errno::EPERM => StatusCode::PermissionDenied, - nix::errno::Errno::ENODEV => StatusCode::NoInit, - nix::errno::Errno::EEXIST => StatusCode::AlreadyExists, - nix::errno::Errno::EPIPE => StatusCode::DeadObject, - nix::errno::Errno::EBADMSG => StatusCode::UnknownTransaction, - nix::errno::Errno::EOVERFLOW => StatusCode::BadIndex, - nix::errno::Errno::ENODATA => StatusCode::NotEnoughData, - nix::errno::Errno::EWOULDBLOCK => StatusCode::WouldBlock, - nix::errno::Errno::ETIMEDOUT => StatusCode::TimedOut, - nix::errno::Errno::EBADF => StatusCode::BadFd, - _ => StatusCode::Errno(errno as i32), + rustix::io::Errno::NOMEM => StatusCode::NoMemory, + rustix::io::Errno::NOSYS => StatusCode::InvalidOperation, + rustix::io::Errno::INVAL => StatusCode::BadValue, + rustix::io::Errno::NOENT => StatusCode::NameNotFound, + rustix::io::Errno::PERM => StatusCode::PermissionDenied, + rustix::io::Errno::NODEV => StatusCode::NoInit, + rustix::io::Errno::EXIST => StatusCode::AlreadyExists, + rustix::io::Errno::PIPE => StatusCode::DeadObject, + rustix::io::Errno::BADMSG => StatusCode::UnknownTransaction, + rustix::io::Errno::OVERFLOW => StatusCode::BadIndex, + rustix::io::Errno::NODATA => StatusCode::NotEnoughData, + rustix::io::Errno::WOULDBLOCK => StatusCode::WouldBlock, + rustix::io::Errno::TIMEDOUT => StatusCode::TimedOut, + rustix::io::Errno::BADF => StatusCode::BadFd, + _ => StatusCode::Errno(errno.raw_os_error()), } } } diff --git a/rsbinder/src/file_descriptor.rs b/rsbinder/src/file_descriptor.rs index e2f3885..4219e92 100644 --- a/rsbinder/src/file_descriptor.rs +++ b/rsbinder/src/file_descriptor.rs @@ -24,7 +24,7 @@ use crate::{ }; use crate::error::{Result, StatusCode}; -use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd, FromRawFd, OwnedFd}; +use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd, OwnedFd}; /// Rust version of the Java class android.os.ParcelFileDescriptor #[derive(Debug)] @@ -73,27 +73,19 @@ impl Eq for ParcelFileDescriptor {} impl Serialize for ParcelFileDescriptor { fn serialize(&self, parcel: &mut Parcel) -> Result<()> { - let fd = self.0.as_raw_fd(); - // Not null parcel.write::(&1)?; - let dup_fd = nix::fcntl::fcntl(fd, nix::fcntl::FcntlArg::F_DUPFD_CLOEXEC(0))?; - - let result = || -> Result<()> { - parcel.write::(&0)?; - let obj = flat_binder_object::new_with_fd(dup_fd, true); - parcel.write_object(&obj, true)?; - Ok(()) - }(); - - match result { - Ok(_) => Ok(()), - Err(e) => { - // Close the duplicated fd - nix::unistd::close(dup_fd)?; - Err(e) - } - } + let dup_fd = rustix::io::fcntl_dupfd_cloexec(&self.0, 0)?; + + parcel.write::(&0)?; + let obj = flat_binder_object::new_with_fd(dup_fd.as_raw_fd(), true); + parcel.write_object(&obj, true)?; + + // The dup_fd has been sent, so the file descriptor is now owned by the Parcel. + // So, we need to forget the OwnedFd to avoid double-closing the file descriptor. + dup_fd.into_raw_fd(); + + Ok(()) } } @@ -123,16 +115,9 @@ impl DeserializeOption for ParcelFileDescriptor { let obj = parcel.read_object(true)?; - let fd = nix::fcntl::fcntl(obj.handle() as _, nix::fcntl::FcntlArg::F_DUPFD_CLOEXEC(0))?; - - let file = unsafe { - // Safety: At this point, we know that the file descriptor was - // not -1, so must be a valid, owned file descriptor which we - // can safely turn into a `File`. - OwnedFd::from_raw_fd(fd) - }; + let fd = rustix::io::fcntl_dupfd_cloexec(&obj.borrowed_fd(), 0)?; - Ok(Some(ParcelFileDescriptor::new(file))) + Ok(Some(ParcelFileDescriptor::new(fd))) } } diff --git a/rsbinder/src/native.rs b/rsbinder/src/native.rs index 481dc2f..903c08d 100644 --- a/rsbinder/src/native.rs +++ b/rsbinder/src/native.rs @@ -172,7 +172,7 @@ impl Transactable for Inner { } DEBUG_PID_TRANSACTION => { - reply.write::(&nix::unistd::getpid().as_raw()) + reply.write::(&rustix::process::getpid().as_raw_nonzero().get()) } _ => { diff --git a/rsbinder/src/parcel.rs b/rsbinder/src/parcel.rs index 1b5bee9..e0c93eb 100644 --- a/rsbinder/src/parcel.rs +++ b/rsbinder/src/parcel.rs @@ -21,6 +21,7 @@ use std::vec::Vec; use std::default::Default; use pretty_hex::*; +use rustix::fd::IntoRawFd; use crate::{ error::{Result, StatusCode}, @@ -248,7 +249,8 @@ impl Parcel { for offset in self.objects.as_slice() { let obj: &flat_binder_object = (self.data.as_ptr(), *offset as usize).into(); if obj.header_type() == BINDER_TYPE_FD { - nix::unistd::close(obj.handle() as _).unwrap(); + // Close the file descriptor + obj.owned_fd(); } } } @@ -721,7 +723,8 @@ impl Parcel { let flat: &mut flat_binder_object = (self.data.as_mut_ptr(), off).into(); flat.acquire()?; if flat.header_type() == BINDER_TYPE_FD { - flat.set_handle(nix::fcntl::fcntl(flat.handle() as _, nix::fcntl::FcntlArg::F_DUPFD_CLOEXEC(0))? as _); +// flat.set_handle(nix::fcntl::fcntl(flat.handle() as _, nix::fcntl::FcntlArg::F_DUPFD_CLOEXEC(0))? as _); + flat.set_handle(rustix::io::fcntl_dupfd_cloexec(flat.borrowed_fd(), 0)?.into_raw_fd() as _); flat.set_cookie(1); } } diff --git a/rsbinder/src/process_state.rs b/rsbinder/src/process_state.rs index 3f4bff2..9a5ba3c 100644 --- a/rsbinder/src/process_state.rs +++ b/rsbinder/src/process_state.rs @@ -2,13 +2,11 @@ // SPDX-License-Identifier: Apache-2.0 use std::os::raw::c_void; -use std::ptr::NonNull; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::collections::HashMap; use std::sync::{Arc, RwLock, OnceLock}; use std::path::{Path, PathBuf}; use std::fs::File; -use std::os::unix::io::AsRawFd; use std::thread; use crate::{ @@ -33,7 +31,7 @@ const DEFAULT_MAX_BINDER_THREADS: u32 = 15; const DEFAULT_ENABLE_ONEWAY_SPAM_DETECTION: u32 = 1; struct MemoryMap { - ptr: NonNull, + ptr: *mut c_void, size: usize, } unsafe impl Sync for MemoryMap {} @@ -87,16 +85,22 @@ impl ProcessState { let driver = open_driver(&driver_name, max_threads)?; - let vm_size = ((1024 * 1024) - nix::unistd::sysconf(nix::unistd::SysconfVar::PAGE_SIZE)?.unwrap() * 2) as usize; - let vm_size = std::num::NonZeroUsize::new(vm_size).ok_or("vm_size is zero!")?; + let vm_size = ((1024 * 1024) - rustix::param::page_size() * 2) as usize; + // let vm_size = std::num::NonZeroUsize::new(vm_size).ok_or("vm_size is zero!")?; let mmap = unsafe { - let vm_start = nix::sys::mman::mmap(None, + // let vm_start = nix::sys::mman::mmap(None, + // vm_size, + // nix::sys::mman::ProtFlags::PROT_READ, + // nix::sys::mman::MapFlags::MAP_PRIVATE | nix::sys::mman::MapFlags::MAP_NORESERVE, + // &driver, + // 0)?; + + let vm_start = rustix::mm::mmap(std::ptr::null_mut(), vm_size, - nix::sys::mman::ProtFlags::PROT_READ, - nix::sys::mman::MapFlags::MAP_PRIVATE | nix::sys::mman::MapFlags::MAP_NORESERVE, - &driver, - 0)?; + rustix::mm::ProtFlags::READ, + rustix::mm::MapFlags::PRIVATE | rustix::mm::MapFlags::NORESERVE, + &driver, 0)?; (vm_start, vm_size) }; @@ -105,7 +109,7 @@ impl ProcessState { max_threads, driver_name, driver: driver.into(), - mmap: RwLock::new(MemoryMap { ptr: mmap.0, size: mmap.1.get() }), + mmap: RwLock::new(MemoryMap { ptr: mmap.0, size: mmap.1 }), context_manager: RwLock::new(None), handle_to_proxy: RwLock::new(HashMap::new()), disable_background_scheduling: AtomicBool::new(false), @@ -145,18 +149,14 @@ impl ProcessState { let mut context_manager = self.context_manager.write().unwrap(); if context_manager.is_none() { - let obj = std::mem::MaybeUninit::::zeroed(); - let mut obj = unsafe { obj.assume_init() }; + let mut obj: binder::flat_binder_object = unsafe { std::mem::zeroed() }; obj.flags = binder::FLAT_BINDER_FLAG_ACCEPTS_FDS; - unsafe { - let driver_fd = self.driver.as_raw_fd(); - if binder::set_context_mgr_ext(driver_fd, &obj).is_err() { - // android_errorWriteLog(0x534e4554, "121035042"); - let unused: i32 = 0; - if let Err(e) = binder::set_context_mgr(driver_fd, &unused) { - return Err(format!("Binder ioctl to become context manager failed: {}", e).into()); - } + if binder::set_context_mgr_ext(&self.driver, obj).is_err() { + // android_errorWriteLog(0x534e4554, "121035042"); + // let unused: i32 = 0; + if let Err(e) = binder::set_context_mgr(&self.driver, 0) { + return Err(format!("Binder ioctl to become context manager failed: {}", e).into()); } } *context_manager = Some(binder); @@ -282,25 +282,22 @@ fn open_driver(driver: &Path, max_threads: u32) -> std::result::Result std::result::Result(fd: Fd, write_read: &mut binder_write_read) -> std::result::Result<(), io::Errno> { + unsafe { + let ctl = ioctl::Updater::, binder_write_read>::new(write_read); + ioctl::ioctl(fd, ctl) + } + } - nix::ioctl_write_ptr!(thread_exit, b'b', 8, __s32); - nix::ioctl_readwrite!(version, b'b', 9, binder_version); + // nix::ioctl_write_ptr!(set_max_threads, b'b', 5, __u32); + pub(crate) fn set_max_threads(fd: Fd, max_threads: u32) -> std::result::Result<(), io::Errno> { + unsafe { + let ctl = ioctl::Setter::, _>::new(max_threads); + ioctl::ioctl(fd, ctl) + } + } - nix::ioctl_readwrite!(get_node_debug_info, b'b', 11, binder_node_debug_info); - nix::ioctl_readwrite!(get_node_info_for_ref, b'b', 12, binder_node_info_for_ref); - nix::ioctl_write_ptr!(set_context_mgr_ext, b'b', 13, flat_binder_object); - nix::ioctl_write_ptr!(freeze, b'b', 14, binder_freeze_info); - nix::ioctl_readwrite!(get_frozen_info, b'b', 15, binder_frozen_status_info); - nix::ioctl_write_ptr!(enable_oneway_spam_detection, b'b', 16, __u32); + // nix::ioctl_write_ptr!(set_context_mgr, b'b', 7, __s32); + pub(crate) fn set_context_mgr(fd: Fd, pid: i32) -> std::result::Result<(), io::Errno> { + unsafe { + let ctl = ioctl::Setter::, _>::new(pid); + ioctl::ioctl(fd, ctl) + } + } + + // nix::ioctl_readwrite!(version, b'b', 9, binder_version); + pub(crate) fn version(fd: Fd, ver: &mut binder_version) -> std::result::Result<(), io::Errno> { + unsafe { + let ctl = ioctl::Updater::, binder_version>::new(ver); + ioctl::ioctl(fd, ctl) + } + } + + // nix::ioctl_write_ptr!(set_context_mgr_ext, b'b', 13, flat_binder_object); + pub(crate) fn set_context_mgr_ext(fd: Fd, obj: flat_binder_object) -> std::result::Result<(), io::Errno> { + unsafe { + let ctl = ioctl::Setter::, _>::new(obj); + ioctl::ioctl(fd, ctl) + } + } + + // nix::ioctl_write_ptr!(enable_oneway_spam_detection, b'b', 16, __u32); + pub(crate) fn enable_oneway_spam_detection(fd: Fd, enable: __u32) -> std::result::Result<(), io::Errno> { + unsafe { + let ctl = ioctl::Setter::, _>::new(enable); + ioctl::ioctl(fd, ctl) + } + } + + // nix::ioctl_readwrite!(binder_ctl_add, b'b', 1, binderfs_device); + pub(crate) fn binder_ctl_add(fd: Fd, device: &mut binderfs_device) -> std::result::Result<(), io::Errno> { + unsafe { + let ctl = ioctl::Updater::, _>::new(device); + ioctl::ioctl(fd, ctl) + } + } + + // nix::ioctl_write_ptr!(set_idle_timeout, b'b', 3, __s64); + pub(crate) fn set_idle_timeout(fd: Fd, timeout: i64) -> std::result::Result<(), io::Errno> { + unsafe { + let ctl = ioctl::Setter::, _>::new(timeout); + ioctl::ioctl(fd, ctl) + } + } + + // nix::ioctl_write_ptr!(set_idle_priority, b'b', 6, __s32); + pub(crate) fn set_idle_priority(fd: Fd, priority: i32) -> std::result::Result<(), io::Errno> { + unsafe { + let ctl = ioctl::Setter::, _>::new(priority); + ioctl::ioctl(fd, ctl) + } + } + + // nix::ioctl_write_ptr!(thread_exit, b'b', 8, __s32); + pub(crate) fn thread_exit(fd: Fd, pid: i32) -> std::result::Result<(), io::Errno> { + unsafe { + let ctl = ioctl::Setter::, _>::new(pid); + ioctl::ioctl(fd, ctl) + } + } + + // nix::ioctl_readwrite!(get_node_debug_info, b'b', 11, binder_node_debug_info); + pub(crate) fn get_node_debug_info(fd: Fd, node_debug_info: &mut binder_node_debug_info) -> std::result::Result<(), rustix::io::Errno> { + unsafe { + let ctl = ioctl::Updater::, _>::new(node_debug_info); + ioctl::ioctl(fd, ctl) + } + } + + // nix::ioctl_readwrite!(get_node_info_for_ref, b'b', 12, binder_node_info_for_ref); + pub(crate) fn get_node_info_for_ref(fd: Fd, node_info: &mut binder_node_info_for_ref) -> std::result::Result<(), rustix::io::Errno> { + unsafe { + let ctl = ioctl::Updater::, _>::new(node_info); + ioctl::ioctl(fd, ctl) + } + } + + // nix::ioctl_write_ptr!(freeze, b'b', 14, binder_freeze_info); + pub(crate) fn freeze(fd: Fd, info: binder_freeze_info) -> std::result::Result<(), io::Errno> { + unsafe { + let ctl = ioctl::Setter::, _>::new(info); + ioctl::ioctl(fd, ctl) + } + } + + // nix::ioctl_readwrite!(get_frozen_info, b'b', 15, binder_frozen_status_info); + pub(crate) fn get_frozen_info(fd: Fd, frozen_info: &mut binder_frozen_status_info) -> std::result::Result<(), io::Errno> { + unsafe { + let ctl = ioctl::Updater::, _>::new(frozen_info); + ioctl::ioctl(fd, ctl) + } + } - nix::ioctl_readwrite!(binder_ctl_add, b'b', 1, binderfs_device); } \ No newline at end of file diff --git a/rsbinder/src/thread_state.rs b/rsbinder/src/thread_state.rs index 30b5cdd..7cd4a29 100644 --- a/rsbinder/src/thread_state.rs +++ b/rsbinder/src/thread_state.rs @@ -18,7 +18,6 @@ */ use std::sync::{atomic::Ordering, Arc}; -use std::os::unix::io::AsRawFd; use std::cell::RefCell; use log::error; use std::backtrace::Backtrace; @@ -678,18 +677,29 @@ fn talk_with_driver(do_receive: bool) -> Result<()> { log::trace!("Size of receive buffer: {}, need_read: {}, do_receive: {}", bwr.read_size, thread_state.borrow().in_parcel.is_empty(), do_receive); } + // unsafe { + // loop { + // let res = binder::write_read(thread_state.borrow().driver.as_raw_fd(), &mut bwr); + // match res { + // Ok(_) => break, + // Err(errno) if errno != nix::errno::Errno::EINTR => { + // log::error!("binder::write_read() error : {}", errno); + // return Err(StatusCode::Errno(errno as _)); + // }, + // _ => {} + // } + // } + // } - unsafe { - loop { - let res = binder::write_read(thread_state.borrow().driver.as_raw_fd(), &mut bwr); - match res { - Ok(_) => break, - Err(errno) if errno != nix::errno::Errno::EINTR => { - log::error!("binder::write_read() error : {}", errno); - return Err(StatusCode::Errno(errno as i32)); - }, - _ => {} - } + loop { + let res = binder::write_read(&thread_state.borrow().driver, &mut bwr); + match res { + Ok(_) => break, + Err(errno) if errno != rustix::io::Errno::INTR => { + log::error!("binder::write_read() error : {}", errno); + return Err(StatusCode::Errno(errno.raw_os_error())); + }, + _ => {} } } @@ -997,7 +1007,7 @@ pub(crate) fn join_thread_pool(is_main: bool) -> Result<()> { result = e; break } - StatusCode::Errno(errno) if errno == (nix::errno::Errno::ECONNREFUSED as i32) => { + StatusCode::Errno(errno) if errno == (rustix::io::Errno::CONNREFUSED.raw_os_error()) => { result = e; break; }