From d4d7dbce0e5b0347ab10f3ccc095b4e503e36201 Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Thu, 9 Nov 2023 14:34:29 -0500 Subject: [PATCH] refactor: pass OwnedFd to launch-pad --- Cargo.lock | 2 +- Cargo.toml | 3 ++- src/main.rs | 11 +++-------- src/notifications.rs | 40 +++++++++++++++++++++++++++------------- 4 files changed, 33 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f7ccd10..1ae1005 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -572,7 +572,7 @@ checksum = "62b02a5381cc465bd3041d84623d0fa3b66738b52b8e2fc3bab8ad63ab032f4a" [[package]] name = "launch-pad" version = "0.1.0" -source = "git+https://github.com/pop-os/launch-pad#4707088eb1111946fc5ea91e9b02ff2737552462" +source = "git+https://github.com/pop-os/launch-pad?branch=fix-close-fds-after-start#b6b37d47e503d9b1c06441ca89eb1f62935dc2b3" dependencies = [ "log", "nix 0.26.2", diff --git a/Cargo.toml b/Cargo.toml index ff16a1b..3fc0be2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,8 @@ publish = false async-signals = "0.4" color-eyre = "0.6" futures-util = "0.3" -launch-pad = { git = "https://github.com/pop-os/launch-pad" } +launch-pad = { git = "https://github.com/pop-os/launch-pad", branch = "fix-close-fds-after-start" } +# launch-pad = { path = "../launch-pad" } libc = "0.2" log-panics = { version = "2", features = ["with-backtrace"] } rustix = "0.38" diff --git a/src/main.rs b/src/main.rs index 8a199dc..7afd9ed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ mod service; mod systemd; use std::{ - os::fd::{AsRawFd, IntoRawFd}, + os::fd::AsRawFd, sync::{Arc, Mutex}, }; @@ -156,12 +156,11 @@ async fn start( "cosmic-notifications", notif_key.clone(), daemon_env_vars.clone(), - daemon_notifications_fd.as_raw_fd(), + daemon_notifications_fd, panel_span.clone(), "cosmic-panel", panel_key.clone(), panel_env_vars.clone(), - panel_notifications_fd.as_raw_fd(), socket_tx.clone(), )) .await @@ -177,12 +176,11 @@ async fn start( "cosmic-panel", panel_key.clone(), panel_env_vars, - panel_notifications_fd.as_raw_fd(), + panel_notifications_fd, notifications_span, "cosmic-notifications", notif_key, daemon_env_vars, - daemon_notifications_fd.as_raw_fd(), socket_tx.clone(), )) .await @@ -288,7 +286,6 @@ async fn start_component( ) { let mut sockets = Vec::with_capacity(1); let (env_vars, fd) = create_privileged_socket(&mut sockets, env_vars).unwrap(); - let fd = fd.into_raw_fd(); if let Err(why) = socket_tx.send(sockets) { error!(?why, "Failed to send the privileged socket"); } @@ -330,8 +327,6 @@ async fn start_component( let (env_vars, new_fd) = create_privileged_socket(&mut sockets, &env_vars).unwrap(); - let new_fd = new_fd.into_raw_fd(); - if let Err(why) = socket_tx_clone.send(sockets) { error!(?why, "Failed to send the privileged socket"); } diff --git a/src/notifications.rs b/src/notifications.rs index 6c8fad1..d6b5bd0 100644 --- a/src/notifications.rs +++ b/src/notifications.rs @@ -1,8 +1,10 @@ use color_eyre::eyre::Context; use color_eyre::Result; +use cosmic_notifications_util::{DAEMON_NOTIFICATIONS_FD, PANEL_NOTIFICATIONS_FD}; use launch_pad::process::Process; use launch_pad::ProcessKey; -use std::os::fd::{IntoRawFd, OwnedFd, RawFd}; +use rustix::fd::AsRawFd; +use std::os::fd::OwnedFd; use std::os::unix::net::UnixStream; use std::sync::{Arc, Mutex}; use tokio::sync::mpsc; @@ -32,12 +34,11 @@ pub fn notifications_process( cmd: &'static str, key: Arc>>, mut env_vars: Vec<(String, String)>, - fd: RawFd, + fd: OwnedFd, restart_span: tracing::Span, restart_cmd: &'static str, restart_key: Arc>>, restart_env_vars: Vec<(String, String)>, - restart_fd: RawFd, socket_tx: mpsc::UnboundedSender>, ) -> Process { env_vars.retain(|v| &v.0 != "WAYLAND_SOCKET"); @@ -49,7 +50,6 @@ pub fn notifications_process( _ = socket_tx.send(sockets); let env_clone = env_vars.clone(); let socket_tx_clone = socket_tx.clone(); - let privileged_fd = privileged_fd.into_raw_fd(); Process::new() .with_executable(cmd) .with_fds(move || vec![privileged_fd, fd]) @@ -69,31 +69,45 @@ pub fn notifications_process( }) .with_on_exit(move |pman, my_key, _, will_restart| { // force restart of notifications / panel when the other exits + // also update the environment variables to use the new socket + let (my_fd, their_fd) = create_socket().expect("Failed to create notification socket"); + let mut my_env_vars = env_clone.clone(); + if let Some((_k, v)) = my_env_vars + .iter_mut() + .find(|(k, _v)| k == PANEL_NOTIFICATIONS_FD || k == DAEMON_NOTIFICATIONS_FD) + { + *v = format!("{}", my_fd.as_raw_fd().to_string()); + } + + let mut their_env_vars = restart_env_vars.clone(); + if let Some((_k, v)) = their_env_vars + .iter_mut() + .find(|(k, _v)| k == PANEL_NOTIFICATIONS_FD || k == DAEMON_NOTIFICATIONS_FD) + { + *v = format!("{}", their_fd.as_raw_fd().to_string()); + } + let new_process = notifications_process( restart_span.clone(), restart_cmd, restart_key.clone(), - restart_env_vars.clone(), - restart_fd, + their_env_vars.clone(), + their_fd, span.clone(), cmd, key.clone(), - env_clone.clone(), - fd, + my_env_vars.clone(), socket_tx_clone.clone(), ); let restart_key = restart_key.clone(); let socket_tx_clone = socket_tx_clone.clone(); - let env_clone = env_clone.clone(); let mut pman_clone = pman.clone(); async move { if will_restart { let mut sockets = Vec::with_capacity(1); let (env_vars, new_fd) = - create_privileged_socket(&mut sockets, &env_clone).unwrap(); - - let new_fd = new_fd.into_raw_fd(); + create_privileged_socket(&mut sockets, &my_env_vars).unwrap(); if let Err(why) = socket_tx_clone.send(sockets) { error!(?why, "Failed to send the privileged socket"); @@ -102,7 +116,7 @@ pub fn notifications_process( error!(?why, "Failed to update environment variables"); } if let Err(why) = pman_clone - .update_process_fds(&my_key, move || vec![new_fd, fd]) + .update_process_fds(&my_key, move || vec![new_fd, my_fd]) .await { error!(?why, "Failed to update fds");