diff --git a/src/lib.rs b/src/lib.rs index 20c32a29..11ffa1d6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -73,7 +73,7 @@ impl State { // potentially tell the session we are setup now if let Err(err) = - session::setup_socket(self.common.event_loop_handle.clone(), &self.common) + session::run_socket(self.common.event_loop_handle.clone(), &self.common) { warn!(?err, "Failed to setup cosmic-session communication"); } @@ -147,6 +147,11 @@ pub fn run(hooks: crate::hooks::Hooks) -> Result<(), Box> { tracy_client::Client::start(); utils::rlimit::increase_nofile_limit(); + // This needs to be done before any potential program launches + // (e.g. Xwayland) as it handles passed file descriptors. + if let Err(err) = session::setup_socket() { + warn!("Session error: {:?}", err); + }; // init hook globals hooks::HOOKS.set(hooks) diff --git a/src/session.rs b/src/session.rs index 4d6f5c1b..0b4025a6 100644 --- a/src/session.rs +++ b/src/session.rs @@ -72,18 +72,24 @@ pub fn get_env(common: &Common) -> Result> { Ok(env) } -pub fn setup_socket(handle: LoopHandle, common: &Common) -> Result<()> { +pub fn setup_socket() -> Result<()> { + if let Ok(fd_num) = std::env::var("COSMIC_SESSION_SOCK") + && let Ok(fd) = fd_num.parse::() + { + let res = unsafe { set_cloexec(fd) }.with_context(|| "Failed to setup session socket"); + if res.is_err() { + unsafe { rustix::io::close(fd) }; + } + res + } else { + Ok(()) + } +} + +pub fn run_socket(handle: LoopHandle, common: &Common) -> Result<()> { if let Ok(fd_num) = std::env::var("COSMIC_SESSION_SOCK") { if let Ok(fd) = fd_num.parse::() { - let mut session_socket = match unsafe { set_cloexec(fd) } { - // CLOEXEC worked and we can startup with session IPC - Ok(_) => unsafe { UnixStream::from_raw_fd(fd) }, - // CLOEXEC didn't work, something is wrong with the fd, just close it - Err(err) => { - unsafe { rustix::io::close(fd) }; - return Err(err).with_context(|| "Failed to setup session socket"); - } - }; + let mut session_socket = unsafe { UnixStream::from_raw_fd(fd) }; let env = get_env(common)?; let message = serde_json::to_string(&Message::SetEnv { variables: env })