diff options
author | Jesse Luehrs <doy@tozt.net> | 2020-10-12 23:38:59 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2020-10-12 23:38:59 -0400 |
commit | 201c590badb75468adb11c88abe3149046a178b5 (patch) | |
tree | ddd6c33e7b9abee9fed56c469676cac13b984824 | |
parent | 4cb9ff362d59b1e51236d1deb2e741dc66538dbd (diff) | |
download | rbw-201c590badb75468adb11c88abe3149046a178b5.tar.gz rbw-201c590badb75468adb11c88abe3149046a178b5.zip |
ttyname was merged back into nix
-rw-r--r-- | src/bin/rbw/actions.rs | 30 |
1 files changed, 5 insertions, 25 deletions
diff --git a/src/bin/rbw/actions.rs b/src/bin/rbw/actions.rs index 2c7d699..ec6ba99 100644 --- a/src/bin/rbw/actions.rs +++ b/src/bin/rbw/actions.rs @@ -1,6 +1,5 @@ use anyhow::Context as _; use std::io::Read as _; -use std::os::unix::ffi::OsStringExt as _; pub fn login() -> anyhow::Result<()> { simple_action(rbw::protocol::Action::Login) @@ -30,7 +29,7 @@ pub fn quit() -> anyhow::Result<()> { std::fs::File::open(pidfile)?.read_to_string(&mut pid)?; let pid = nix::unistd::Pid::from_raw(pid.parse()?); sock.send(&rbw::protocol::Request { - tty: ttyname(0) + tty: nix::unistd::ttyname(0) .ok() .and_then(|p| p.to_str().map(|s| s.to_string())), action: rbw::protocol::Action::Quit, @@ -54,7 +53,7 @@ pub fn decrypt( ) -> anyhow::Result<String> { let mut sock = connect()?; sock.send(&rbw::protocol::Request { - tty: ttyname(0) + tty: nix::unistd::ttyname(0) .ok() .and_then(|p| p.to_str().map(|s| s.to_string())), action: rbw::protocol::Action::Decrypt { @@ -79,7 +78,7 @@ pub fn encrypt( ) -> anyhow::Result<String> { let mut sock = connect()?; sock.send(&rbw::protocol::Request { - tty: ttyname(0) + tty: nix::unistd::ttyname(0) .ok() .and_then(|p| p.to_str().map(|s| s.to_string())), action: rbw::protocol::Action::Encrypt { @@ -101,7 +100,7 @@ pub fn encrypt( pub fn version() -> anyhow::Result<u32> { let mut sock = connect()?; sock.send(&rbw::protocol::Request { - tty: ttyname(0) + tty: nix::unistd::ttyname(0) .ok() .and_then(|p| p.to_str().map(|s| s.to_string())), action: rbw::protocol::Action::Version, @@ -121,7 +120,7 @@ fn simple_action(action: rbw::protocol::Action) -> anyhow::Result<()> { let mut sock = connect()?; sock.send(&rbw::protocol::Request { - tty: ttyname(0) + tty: nix::unistd::ttyname(0) .ok() .and_then(|p| p.to_str().map(|s| s.to_string())), action, @@ -149,25 +148,6 @@ fn connect() -> anyhow::Result<crate::sock::Sock> { }) } -// XXX copied (with small modifications) from the as-yet-unreleased nix -// version, should use that directly once 0.18 is released -fn ttyname( - fd: std::os::unix::io::RawFd, -) -> anyhow::Result<std::path::PathBuf> { - const PATH_MAX: usize = libc::PATH_MAX as usize; - let mut buf = vec![0_u8; PATH_MAX]; - let c_buf = buf.as_mut_ptr() as *mut libc::c_char; - - let ret = unsafe { libc::ttyname_r(fd, c_buf, buf.len()) }; - if ret != 0 { - return Err(anyhow::anyhow!("ttyname_r failed: {}", ret)); - } - - let nul = buf.iter().position(|c| *c == b'\0').unwrap(); - buf.truncate(nul); - Ok(std::ffi::OsString::from_vec(buf).into()) -} - fn wait_for_exit(pid: nix::unistd::Pid) -> anyhow::Result<()> { loop { if nix::sys::signal::kill(pid, None).is_err() { |