Skip to content

Commit

Permalink
Switched from nix to rustix
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Kim committed Jun 27, 2024
1 parent 41885f1 commit bb6747e
Show file tree
Hide file tree
Showing 16 changed files with 291 additions and 184 deletions.
80 changes: 45 additions & 35 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
4 changes: 0 additions & 4 deletions rsbinder-aidl/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,6 @@ pub fn name_to_const_expr(name: &str) -> Option<ConstExpr> {
lookup_name_from_decl(&lookup_decl.decl, &lookup_decl)
}

#[derive(Debug)]
pub struct Interface {
}

#[derive(Debug)]
pub struct Document {
pub package: Option<String>,
Expand Down
2 changes: 1 addition & 1 deletion rsbinder-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion rsbinder-tests/src/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion rsbinder-tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ lazy_static = { workspace = true }
rsbinder = { workspace = true }
log = { workspace = true }
env_logger = { workspace = true }
nix = { workspace = true }
anstyle = { workspace = true }
2 changes: 1 addition & 1 deletion rsbinder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
15 changes: 13 additions & 2 deletions rsbinder/src/binder_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*,
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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(())
Expand Down
15 changes: 5 additions & 10 deletions rsbinder/src/binderfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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))
}
Loading

0 comments on commit bb6747e

Please sign in to comment.