Use OwnedFd, now that it's stable

This commit is contained in:
Ian Douglas Scott 2022-08-12 09:14:26 -07:00
parent e822a87072
commit 46047f4a58
3 changed files with 18 additions and 21 deletions

View file

@ -1,7 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only // SPDX-License-Identifier: GPL-3.0-only
use crate::process::{ProcessEvent, ProcessHandler}; use crate::process::{ProcessEvent, ProcessHandler};
use color_eyre::eyre::{Result, WrapErr}; use color_eyre::eyre::{Result, WrapErr};
use nix::unistd;
use sendfd::SendWithFd; use sendfd::SendWithFd;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{collections::HashMap, os::unix::prelude::*}; use std::{collections::HashMap, os::unix::prelude::*};
@ -30,7 +29,7 @@ pub enum Message {
pub fn create_privileged_socket( pub fn create_privileged_socket(
sockets: &mut Vec<UnixStream>, sockets: &mut Vec<UnixStream>,
env_vars: &[(String, String)], env_vars: &[(String, String)],
) -> Result<(Vec<(String, String)>, RawFd)> { ) -> Result<(Vec<(String, String)>, OwnedFd)> {
// Create a new pair of unnamed Unix sockets // Create a new pair of unnamed Unix sockets
let (comp_socket, client_socket) = let (comp_socket, client_socket) =
UnixStream::pair().wrap_err("failed to create socket pair")?; UnixStream::pair().wrap_err("failed to create socket pair")?;
@ -45,10 +44,10 @@ pub fn create_privileged_socket(
std_stream std_stream
.set_nonblocking(false) .set_nonblocking(false)
.wrap_err("failed to mark client socket as blocking")?; .wrap_err("failed to mark client socket as blocking")?;
std_stream.into_raw_fd() OwnedFd::from(std_stream)
}; };
let mut env_vars = env_vars.to_vec(); let mut env_vars = env_vars.to_vec();
env_vars.push(("WAYLAND_SOCKET".into(), client_fd.to_string())); env_vars.push(("WAYLAND_SOCKET".into(), client_fd.as_raw_fd().to_string()));
Ok((env_vars, client_fd)) Ok((env_vars, client_fd))
} }
@ -162,7 +161,7 @@ async fn send_fd(session_tx: &mut OwnedWriteHalf, stream: Vec<UnixStream>) -> Re
std_stream std_stream
.set_nonblocking(false) .set_nonblocking(false)
.wrap_err("failed to set stream as blocking")?; .wrap_err("failed to set stream as blocking")?;
Ok(std_stream.into_raw_fd()) Ok(OwnedFd::from(std_stream))
}) })
.collect::<Result<Vec<_>>>() .collect::<Result<Vec<_>>>()
.wrap_err("failed to convert streams to file descriptors")?; .wrap_err("failed to convert streams to file descriptors")?;
@ -184,11 +183,11 @@ async fn send_fd(session_tx: &mut OwnedWriteHalf, stream: Vec<UnixStream>) -> Re
tokio::time::sleep(std::time::Duration::from_micros(100)).await; tokio::time::sleep(std::time::Duration::from_micros(100)).await;
// Send our file descriptors. // Send our file descriptors.
let fd: &UnixStream = session_tx.as_ref(); let fd: &UnixStream = session_tx.as_ref();
fd.send_with_fd(&[0], &fds).wrap_err("failed to send fd")?; fd.send_with_fd(
// Close our copy of each file descriptor. &[0],
for fd in &fds { &fds.iter().map(|fd| fd.as_raw_fd()).collect::<Vec<_>>(),
let _ = unistd::close(*fd); )
} .wrap_err("failed to send fd")?;
info!("sent {} fds", fds.len()); info!("sent {} fds", fds.len());
Ok(()) Ok(())
} }
@ -211,7 +210,7 @@ pub fn run_compositor(
std_stream std_stream
.set_nonblocking(false) .set_nonblocking(false)
.wrap_err("failed to mark compositor unix stream as blocking")?; .wrap_err("failed to mark compositor unix stream as blocking")?;
std_stream.into_raw_fd() OwnedFd::from(std_stream)
}; };
// Create a new span, marking the upcoming task as `cosmic-comp` with tracing. // Create a new span, marking the upcoming task as `cosmic-comp` with tracing.
let span = info_span!(parent: None, "cosmic-comp"); let span = info_span!(parent: None, "cosmic-comp");
@ -223,7 +222,7 @@ pub fn run_compositor(
ProcessHandler::new(tx, &token).run( ProcessHandler::new(tx, &token).run(
"cosmic-comp", "cosmic-comp",
vec![], vec![],
vec![("COSMIC_SESSION_SOCK".into(), comp.to_string())], vec![("COSMIC_SESSION_SOCK".into(), comp.as_raw_fd().to_string())],
vec![comp], vec![comp],
&span, &span,
); );

View file

@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only // SPDX-License-Identifier: GPL-3.0-only
use crate::process::{ProcessEvent, ProcessHandler}; use crate::process::{ProcessEvent, ProcessHandler};
use std::os::unix::io::RawFd; use std::os::unix::io::OwnedFd;
use tokio::sync::mpsc::unbounded_channel; use tokio::sync::mpsc::unbounded_channel;
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
use tracing::{Instrument, Span}; use tracing::{Instrument, Span};
@ -11,7 +11,7 @@ pub fn run_executable(
executable: &'static str, executable: &'static str,
args: Vec<String>, args: Vec<String>,
env_vars: Vec<(String, String)>, env_vars: Vec<(String, String)>,
fds: Vec<RawFd>, fds: Vec<OwnedFd>,
) { ) {
let span_2 = span.clone(); let span_2 = span.clone();
let (tx, mut rx) = unbounded_channel::<ProcessEvent>(); let (tx, mut rx) = unbounded_channel::<ProcessEvent>();

View file

@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only // SPDX-License-Identifier: GPL-3.0-only
use color_eyre::eyre::{ContextCompat, Result, WrapErr}; use color_eyre::eyre::{ContextCompat, Result, WrapErr};
use nix::{fcntl, unistd}; use nix::fcntl;
use std::{ use std::{
os::unix::prelude::*, os::unix::prelude::*,
process::{ExitStatus, Stdio}, process::{ExitStatus, Stdio},
@ -39,7 +39,7 @@ impl ProcessHandler {
executable: impl ToString, executable: impl ToString,
args: Vec<String>, args: Vec<String>,
vars: Vec<(String, String)>, vars: Vec<(String, String)>,
fds: Vec<RawFd>, fds: Vec<OwnedFd>,
span: &Span, span: &Span,
) { ) {
let executable = executable.to_string(); let executable = executable.to_string();
@ -71,9 +71,7 @@ impl ProcessHandler {
return; return;
} }
}; };
for fd in &fds { drop(fds);
let _ = unistd::close(*fd);
}
let mut stdout = BufReader::new(child.stdout.take().unwrap()).lines(); let mut stdout = BufReader::new(child.stdout.take().unwrap()).lines();
let mut stderr = BufReader::new(child.stderr.take().unwrap()).lines(); let mut stderr = BufReader::new(child.stderr.take().unwrap()).lines();
std::mem::drop(self.tx.send(ProcessEvent::Started)); std::mem::drop(self.tx.send(ProcessEvent::Started));
@ -133,8 +131,8 @@ impl ProcessHandler {
} }
} }
fn mark_as_not_cloexec(file: &impl AsRawFd) -> Result<()> { fn mark_as_not_cloexec(file: &impl AsFd) -> Result<()> {
let raw_fd = file.as_raw_fd(); let raw_fd = file.as_fd().as_raw_fd();
let fd_flags = fcntl::FdFlag::from_bits( let fd_flags = fcntl::FdFlag::from_bits(
fcntl::fcntl(raw_fd, fcntl::FcntlArg::F_GETFD) fcntl::fcntl(raw_fd, fcntl::FcntlArg::F_GETFD)
.wrap_err("failed to get GETFD value of stream")?, .wrap_err("failed to get GETFD value of stream")?,