From d302c37a4b9fc85c360f71f70b74cf4ed86e5200 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 21 Apr 2024 16:22:04 -0400 Subject: switch from nix to rustix --- Cargo.lock | 26 +++++-------------------- Cargo.toml | 2 +- src/bin/rbw-agent/daemon.rs | 8 +++----- src/bin/rbw-agent/debugger.rs | 2 +- src/bin/rbw/actions.rs | 44 ++++++++++++++++++++++++++++--------------- src/dirs.rs | 2 +- 6 files changed, 40 insertions(+), 44 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1ce6100..87fe839 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -269,12 +269,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 = "cipher" version = "0.4.4" @@ -992,18 +986,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 2.5.0", - "cfg-if", - "cfg_aliases", - "libc", -] - [[package]] name = "num-bigint-dig" version = "0.8.4" @@ -1341,7 +1323,6 @@ dependencies = [ "is-terminal", "libc", "log", - "nix", "pbkdf2", "percent-encoding", "pkcs8", @@ -1351,6 +1332,7 @@ dependencies = [ "reqwest", "rmpv", "rsa", + "rustix", "serde", "serde_json", "serde_path_to_error", @@ -1537,14 +1519,16 @@ checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" [[package]] name = "rustix" -version = "0.38.32" +version = "0.38.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65e04861e65f21776e67888bfbea442b3642beaa0138fdb1dd7a84a52dffdb89" +checksum = "e3cc72858054fcff6d7dea32df2aeaee6a7c24227366d7ea429aada2f26b16ad" dependencies = [ "bitflags 2.5.0", "errno", + "itoa", "libc", "linux-raw-sys", + "once_cell", "windows-sys 0.52.0", ] diff --git a/Cargo.toml b/Cargo.toml index 9b6dbbe..697583f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,7 +37,6 @@ hmac = { version = "0.12.1", features = ["std"] } humantime = "2.1.0" libc = "0.2.153" log = "0.4.21" -nix = { version = "0.28", features = ["process", "signal", "term", "user"] } pbkdf2 = "0.12.2" percent-encoding = "2.3.1" pkcs8 = "0.10.2" @@ -66,6 +65,7 @@ rmpv = "1.0.2" tokio-tungstenite = { version = "0.21", features = ["rustls-tls-native-roots"] } is-terminal = "0.4.12" regex = "1.10.4" +rustix = { version = "0.38.33", features = ["termios", "procfs"] } [package.metadata.deb] depends = "pinentry" diff --git a/src/bin/rbw-agent/daemon.rs b/src/bin/rbw-agent/daemon.rs index 640d5e2..06db891 100644 --- a/src/bin/rbw-agent/daemon.rs +++ b/src/bin/rbw-agent/daemon.rs @@ -1,12 +1,10 @@ -use std::os::fd::AsRawFd as _; - pub struct StartupAck { writer: std::os::unix::io::OwnedFd, } impl StartupAck { pub fn ack(self) -> anyhow::Result<()> { - nix::unistd::write(&self.writer, &[0])?; + rustix::io::write(&self.writer, &[0])?; Ok(()) } } @@ -21,7 +19,7 @@ pub fn daemonize() -> anyhow::Result { .create(true) .open(rbw::dirs::agent_stderr_file())?; - let (r, w) = nix::unistd::pipe()?; + let (r, w) = rustix::pipe::pipe()?; let daemonize = daemonize::Daemonize::new() .pid_file(rbw::dirs::pid_file()) .stdout(stdout) @@ -32,7 +30,7 @@ pub fn daemonize() -> anyhow::Result { let mut buf = [0; 1]; // unwraps are necessary because not really a good way to handle // errors here otherwise - nix::unistd::read(r.as_raw_fd(), &mut buf).unwrap(); + rustix::io::read(&r, &mut buf).unwrap(); drop(r); std::process::exit(0); } diff --git a/src/bin/rbw-agent/debugger.rs b/src/bin/rbw-agent/debugger.rs index 59bbe50..be5260c 100644 --- a/src/bin/rbw-agent/debugger.rs +++ b/src/bin/rbw-agent/debugger.rs @@ -12,7 +12,7 @@ pub fn disable_tracing() -> anyhow::Result<()> { if ret == 0 { Ok(()) } else { - let e = nix::Error::last(); + let e = std::io::Error::last_os_error(); Err(anyhow::anyhow!("failed to disable PTRACE_ATTACH, agent memory may be dumpable by other processes: {}", e)) } } diff --git a/src/bin/rbw/actions.rs b/src/bin/rbw/actions.rs index a13c58b..c84ccd4 100644 --- a/src/bin/rbw/actions.rs +++ b/src/bin/rbw/actions.rs @@ -1,4 +1,4 @@ -use anyhow::Context as _; +use anyhow::{bail, Context as _}; use std::io::Read as _; pub fn register() -> anyhow::Result<()> { @@ -31,11 +31,17 @@ pub fn quit() -> anyhow::Result<()> { let pidfile = rbw::dirs::pid_file(); let mut pid = String::new(); std::fs::File::open(pidfile)?.read_to_string(&mut pid)?; - let pid = nix::unistd::Pid::from_raw(pid.trim_end().parse()?); + let Some(pid) = + rustix::process::Pid::from_raw(pid.trim_end().parse()?) + else { + bail!("failed to read pid from pidfile"); + }; sock.send(&rbw::protocol::Request { - tty: nix::unistd::ttyname(std::io::stdin()).ok().and_then( - |p| p.to_str().map(std::string::ToString::to_string), - ), + tty: rustix::termios::ttyname(std::io::stdin(), vec![]) + .ok() + .and_then(|p| { + p.to_str().map(std::string::ToString::to_string).ok() + }), action: rbw::protocol::Action::Quit, })?; wait_for_exit(pid); @@ -57,9 +63,11 @@ pub fn decrypt( ) -> anyhow::Result { let mut sock = connect()?; sock.send(&rbw::protocol::Request { - tty: nix::unistd::ttyname(std::io::stdin()) + tty: rustix::termios::ttyname(std::io::stdin(), vec![]) .ok() - .and_then(|p| p.to_str().map(std::string::ToString::to_string)), + .and_then(|p| { + p.to_str().map(std::string::ToString::to_string).ok() + }), action: rbw::protocol::Action::Decrypt { cipherstring: cipherstring.to_string(), org_id: org_id.map(std::string::ToString::to_string), @@ -82,9 +90,11 @@ pub fn encrypt( ) -> anyhow::Result { let mut sock = connect()?; sock.send(&rbw::protocol::Request { - tty: nix::unistd::ttyname(std::io::stdin()) + tty: rustix::termios::ttyname(std::io::stdin(), vec![]) .ok() - .and_then(|p| p.to_str().map(std::string::ToString::to_string)), + .and_then(|p| { + p.to_str().map(std::string::ToString::to_string).ok() + }), action: rbw::protocol::Action::Encrypt { plaintext: plaintext.to_string(), org_id: org_id.map(std::string::ToString::to_string), @@ -110,9 +120,11 @@ pub fn clipboard_store(text: &str) -> anyhow::Result<()> { pub fn version() -> anyhow::Result { let mut sock = connect()?; sock.send(&rbw::protocol::Request { - tty: nix::unistd::ttyname(std::io::stdin()) + tty: rustix::termios::ttyname(std::io::stdin(), vec![]) .ok() - .and_then(|p| p.to_str().map(std::string::ToString::to_string)), + .and_then(|p| { + p.to_str().map(std::string::ToString::to_string).ok() + }), action: rbw::protocol::Action::Version, })?; @@ -130,9 +142,11 @@ fn simple_action(action: rbw::protocol::Action) -> anyhow::Result<()> { let mut sock = connect()?; sock.send(&rbw::protocol::Request { - tty: nix::unistd::ttyname(std::io::stdin()) + tty: rustix::termios::ttyname(std::io::stdin(), vec![]) .ok() - .and_then(|p| p.to_str().map(std::string::ToString::to_string)), + .and_then(|p| { + p.to_str().map(std::string::ToString::to_string).ok() + }), action, })?; @@ -158,9 +172,9 @@ fn connect() -> anyhow::Result { }) } -fn wait_for_exit(pid: nix::unistd::Pid) { +fn wait_for_exit(pid: rustix::process::Pid) { loop { - if nix::sys::signal::kill(pid, None).is_err() { + if rustix::process::test_kill_process(pid).is_err() { break; } std::thread::sleep(std::time::Duration::from_millis(10)); diff --git a/src/dirs.rs b/src/dirs.rs index e838e12..2fa6e50 100644 --- a/src/dirs.rs +++ b/src/dirs.rs @@ -108,7 +108,7 @@ fn runtime_dir() -> std::path::PathBuf { "{}/{}-{}", std::env::temp_dir().to_string_lossy(), &profile(), - nix::unistd::getuid().as_raw() + rustix::process::getuid().as_raw() ) .into() }, -- cgit v1.2.3-54-g00ecf