Merge branch 'master' into applet-host

This commit is contained in:
Ashley Wulber 2022-07-20 13:10:49 -04:00
commit 12bd4302c9
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820
7 changed files with 57 additions and 33 deletions

1
Cargo.lock generated
View file

@ -251,6 +251,7 @@ dependencies = [
"tracing",
"tracing-journald",
"tracing-subscriber",
"zbus",
]
[[package]]

View file

@ -23,3 +23,4 @@ tokio-util = "0.7"
tracing = "0.1"
tracing-journald = "0.3.0"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
zbus = { version = "2.0.1" }

View file

@ -1,13 +1,10 @@
// SPDX-License-Identifier: GPL-3.0-only
use crate::process::{ProcessEvent, ProcessHandler};
use color_eyre::eyre::{ContextCompat, Result, WrapErr};
use nix::fcntl;
use color_eyre::eyre::{Result, WrapErr};
use nix::unistd;
use sendfd::SendWithFd;
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
os::unix::prelude::{AsRawFd, IntoRawFd},
};
use std::{collections::HashMap, os::unix::prelude::*};
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::{
@ -30,30 +27,14 @@ pub enum Message {
NewPrivilegedClient { count: usize },
}
fn mark_as_not_cloexec(stream: &UnixStream) -> Result<()> {
let raw_fd = stream.as_raw_fd();
let fd_flags = fcntl::FdFlag::from_bits(
fcntl::fcntl(raw_fd, fcntl::FcntlArg::F_GETFD)
.wrap_err("failed to get GETFD value of stream")?,
)
.wrap_err("failed to get fd flags from file")?;
fcntl::fcntl(
raw_fd,
fcntl::FcntlArg::F_SETFD(fd_flags.difference(fcntl::FdFlag::FD_CLOEXEC)),
)
.wrap_err("failed to set CLOEXEC on file")?;
Ok(())
}
pub fn create_privileged_socket(
sockets: &mut Vec<UnixStream>,
env_vars: &[(String, String)],
) -> Result<Vec<(String, String)>> {
) -> Result<(Vec<(String, String)>, RawFd)> {
let (comp_socket, client_socket) =
UnixStream::pair().wrap_err("failed to create socket pair")?;
sockets.push(comp_socket);
let client_fd = {
mark_as_not_cloexec(&client_socket).wrap_err("failed to mark client stream as CLOEXEC")?;
let std_stream = client_socket
.into_std()
.wrap_err("failed to convert client socket to std socket")?;
@ -64,7 +45,7 @@ pub fn create_privileged_socket(
};
let mut env_vars = env_vars.to_vec();
env_vars.push(("WAYLAND_SOCKET".into(), client_fd.to_string()));
Ok(env_vars)
Ok((env_vars, client_fd))
}
async fn receive_event(rx: &mut mpsc::UnboundedReceiver<ProcessEvent>) -> Option<()> {
@ -159,7 +140,6 @@ async fn send_fd(session_tx: &mut OwnedWriteHalf, stream: Vec<UnixStream>) -> Re
let fds = stream
.into_iter()
.map(|stream| {
mark_as_not_cloexec(&stream).wrap_err("failed to mark stream as CLOEXEC")?;
let std_stream = stream
.into_std()
.wrap_err("failed to convert stream to std stream")?;
@ -183,6 +163,9 @@ async fn send_fd(session_tx: &mut OwnedWriteHalf, stream: Vec<UnixStream>) -> Re
tokio::time::sleep(std::time::Duration::from_micros(100)).await;
let fd: &UnixStream = session_tx.as_ref();
fd.send_with_fd(&[0], &fds).wrap_err("failed to send fd")?;
for fd in &fds {
let _ = unistd::close(*fd);
}
info!("sent {} fds", fds.len());
Ok(())
}
@ -196,7 +179,6 @@ pub fn run_compositor(
let (session, comp) = UnixStream::pair().wrap_err("failed to create pair of unix sockets")?;
let (mut session_rx, mut session_tx) = session.into_split();
let comp = {
mark_as_not_cloexec(&comp).wrap_err("failed to mark compositor stream as CLOEXEC")?;
let std_stream = comp
.into_std()
.wrap_err("failed to convert compositor unix stream to a standard unix stream")?;
@ -213,6 +195,7 @@ pub fn run_compositor(
"cosmic-comp",
vec![],
vec![("COSMIC_SESSION_SOCK".into(), comp.to_string())],
vec![comp],
&span,
);
let mut ipc_state = IpcState {

View file

@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only
use crate::process::{ProcessEvent, ProcessHandler};
use std::os::unix::io::RawFd;
use tokio::sync::mpsc::unbounded_channel;
use tokio_util::sync::CancellationToken;
use tracing::{Instrument, Span};
@ -10,12 +11,13 @@ pub fn run_executable(
executable: &'static str,
args: Vec<String>,
env_vars: Vec<(String, String)>,
fds: Vec<RawFd>,
) {
let span_2 = span.clone();
let (tx, mut rx) = unbounded_channel::<ProcessEvent>();
tokio::spawn(
async move {
ProcessHandler::new(tx, &token).run(executable, args, env_vars, &span);
ProcessHandler::new(tx, &token).run(executable, args, env_vars, fds, &span);
while let Some(event) = rx.recv().await {
match event {
ProcessEvent::Started => {

View file

@ -55,21 +55,25 @@ async fn main() -> Result<()> {
let mut sockets = Vec::with_capacity(2);
let (env, fd) = comp::create_privileged_socket(&mut sockets, &env_vars)
.wrap_err("failed to create panel socket")?;
generic::run_executable(
token.child_token(),
info_span!(parent: None, "cosmic-panel"),
"cosmic-panel",
vec!["testing-panel".into()],
comp::create_privileged_socket(&mut sockets, &env_vars)
.wrap_err("failed to create panel socket")?,
env,
vec![fd],
);
let (env, fd) = comp::create_privileged_socket(&mut sockets, &env_vars)
.wrap_err("failed to create dock socket")?;
generic::run_executable(
token.child_token(),
info_span!(parent: None, "cosmic-panel dock"),
"cosmic-panel",
vec!["testing-dock".into()],
comp::create_privileged_socket(&mut sockets, &env_vars)
.wrap_err("failed to create dock socket")?,
env,
vec![fd],
);
generic::run_executable(
token.child_token(),

View file

@ -1,5 +1,10 @@
// SPDX-License-Identifier: GPL-3.0-only
use std::process::{ExitStatus, Stdio};
use color_eyre::eyre::{ContextCompat, Result, WrapErr};
use nix::{fcntl, unistd};
use std::{
os::unix::prelude::*,
process::{ExitStatus, Stdio},
};
use tokio::{
io::{AsyncBufReadExt, BufReader},
process::Command,
@ -28,16 +33,24 @@ impl ProcessHandler {
}
}
// TODO: Use `OwnedFd` when stable
pub fn run(
self,
executable: impl ToString,
args: Vec<String>,
vars: Vec<(String, String)>,
fds: Vec<RawFd>,
span: &Span,
) {
let executable = executable.to_string();
tokio::spawn(
async move {
for fd in &fds {
if let Err(err) = mark_as_not_cloexec(fd) {
error!("failed to launch '{}': {}", executable, err);
return;
}
}
let mut child = match Command::new(&executable)
.args(&args)
.stdin(Stdio::null())
@ -58,6 +71,9 @@ impl ProcessHandler {
return;
}
};
for fd in &fds {
let _ = unistd::close(*fd);
}
let mut stdout = BufReader::new(child.stdout.take().unwrap()).lines();
let mut stderr = BufReader::new(child.stderr.take().unwrap()).lines();
std::mem::drop(self.tx.send(ProcessEvent::Started));
@ -116,3 +132,18 @@ impl ProcessHandler {
);
}
}
fn mark_as_not_cloexec(file: &impl AsRawFd) -> Result<()> {
let raw_fd = file.as_raw_fd();
let fd_flags = fcntl::FdFlag::from_bits(
fcntl::fcntl(raw_fd, fcntl::FcntlArg::F_GETFD)
.wrap_err("failed to get GETFD value of stream")?,
)
.wrap_err("failed to get fd flags from file")?;
fcntl::fcntl(
raw_fd,
fcntl::FcntlArg::F_SETFD(fd_flags.difference(fcntl::FdFlag::FD_CLOEXEC)),
)
.wrap_err("failed to set CLOEXEC on file")?;
Ok(())
}

View file

@ -1,9 +1,11 @@
// SPDX-License-Identifier: GPL-3.0-only
use color_eyre::{eyre::WrapErr, Result};
use systemd_client::manager::SystemdManagerProxy;
pub async fn start_systemd_target() -> Result<()> {
let manager = systemd_client::manager::build_nonblock_proxy()
let connection = zbus::Connection::session().await?;
let manager = SystemdManagerProxy::new(&connection)
.await
.wrap_err("failed to connect to org.freedesktop.systemd1.Manager")?;
manager