Skip to content

Commit

Permalink
Fix nix usage
Browse files Browse the repository at this point in the history
Signed-off-by: Sascha Grunert <[email protected]>
  • Loading branch information
saschagrunert committed Aug 30, 2023
1 parent 595e8b0 commit 2dd2b37
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[workspace]
resolver = "2"
members = [
"conmon-rs/common",
"conmon-rs/client",
Expand Down
2 changes: 1 addition & 1 deletion conmon-rs/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ libc = "0.2.147"
libsystemd = "0.6.0"
memchr = "2.6.0"
multimap = "0.9.0"
nix = "0.27.1"
nix = { version = "0.27.1", features = ["fs", "hostname", "mount", "sched", "signal", "socket", "term", "user"] }
notify = "6.1.1"
once_cell = "1.18.0"
opentelemetry = { version = "0.20.0", features = ["rt-tokio"] }
Expand Down
16 changes: 7 additions & 9 deletions conmon-rs/server/src/attach.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ use nix::{
};
use std::{
convert::From,
os::unix::{
fs::PermissionsExt,
io::{FromRawFd, RawFd},
net,
os::{
fd::{AsRawFd, OwnedFd},
unix::fs::PermissionsExt,
},
path::{Path, PathBuf},
};
Expand Down Expand Up @@ -154,13 +153,13 @@ impl Attach {
let (shortened_path, _parent_dir) =
Listener::<DefaultListener>::default().shorten_socket_path(path)?;
let addr = UnixAddr::new(&shortened_path).context("create socket addr")?;
bind(fd, &addr).context("bind socket fd")?;
bind(fd.as_raw_fd(), &addr).context("bind socket fd")?;

let metadata = path.metadata()?;
let mut permissions = metadata.permissions();
permissions.set_mode(0o700);

listen(fd, 10).context("listen on socket fd")?;
listen(&fd, 10).context("listen on socket fd")?;

task::spawn(
async move {
Expand All @@ -177,15 +176,14 @@ impl Attach {
}

async fn start(
fd: RawFd,
fd: OwnedFd,
read_half_tx: Sender<Vec<u8>>,
write_half_tx: Sender<Message>,
token: CancellationToken,
stop_after_stdin_eof: bool,
) -> Result<()> {
debug!("Start listening on attach socket");
let listener = UnixListener::from_std(unsafe { net::UnixListener::from_raw_fd(fd) })
.context("create unix listener")?;
let listener = UnixListener::from_std(fd.into()).context("create unix listener")?;
loop {
match listener.accept().await {
Ok((stream, _)) => {
Expand Down
16 changes: 11 additions & 5 deletions conmon-rs/server/src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use sendfd::RecvWithFd;
use std::{
io::{self, ErrorKind, Read, Write},
os::{
fd::{AsRawFd, FromRawFd, OwnedFd},
fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, OwnedFd},
unix::{fs::PermissionsExt, io::RawFd},
},
path::PathBuf,
Expand Down Expand Up @@ -112,9 +112,9 @@ impl Terminal {
self.set_tty(Arc::downgrade(&fd).into());

debug!("Changing terminal settings");
let mut term = termios::tcgetattr(fd.as_raw_fd())?;
let mut term = termios::tcgetattr(&fd)?;
term.output_flags |= OutputFlags::ONLCR;
termios::tcsetattr(fd.as_raw_fd(), SetArg::TCSANOW, &term)?;
termios::tcsetattr(&fd, SetArg::TCSANOW, &term)?;

let attach_clone = self.attach.clone();
let logger_clone = self.logger.clone();
Expand Down Expand Up @@ -285,7 +285,7 @@ mod tests {
loop {
let ready = stream.ready(Interest::WRITABLE).await?;
if ready.is_writable() {
match stream.send_with_fd(b"test", &[res.master]) {
match stream.send_with_fd(b"test", &[res.master.as_raw_fd()]) {
Ok(_) => break,
Err(ref e) if e.kind() == ErrorKind::WouldBlock => continue,
Err(e) => anyhow::bail!(e),
Expand All @@ -297,7 +297,7 @@ mod tests {
assert!(!sut.path().exists());

// Write to the slave
let mut file = unsafe { fs::File::from_raw_fd(res.slave) };
let mut file = unsafe { fs::File::from_raw_fd(res.slave.as_raw_fd()) };
file.write_all(b"test").await?;

Ok(())
Expand All @@ -322,6 +322,12 @@ impl AsRawFd for TerminalFd {
}
}

impl AsFd for TerminalFd {
fn as_fd(&self) -> BorrowedFd<'_> {
self.0.as_fd()
}
}

impl AsyncRead for &TerminalFd {
fn poll_read(
self: Pin<&mut Self>,
Expand Down

0 comments on commit 2dd2b37

Please sign in to comment.