diff --git a/default-config/src/lib.rs b/default-config/src/lib.rs index 1230e500..8542ae1c 100644 --- a/default-config/src/lib.rs +++ b/default-config/src/lib.rs @@ -1,7 +1,9 @@ use i4config::keyboard::mods::{Modifiers, ALT, CTRL, SHIFT}; -use i4config::keyboard::syms::{SYM_Super_L, SYM_h, SYM_j, SYM_k, SYM_l, SYM_r, SYM_t, SYM_x, SYM_y}; +use i4config::keyboard::syms::{ + SYM_Super_L, SYM_h, SYM_j, SYM_k, SYM_l, SYM_r, SYM_t, SYM_x, SYM_y, +}; use i4config::Direction::{Down, Left, Right, Up}; -use i4config::{config, create_seat, input_devices, on_new_input_device, Seat, Command}; +use i4config::{config, create_seat, input_devices, on_new_input_device, Command, Seat}; const MOD: Modifiers = ALT; @@ -32,12 +34,8 @@ fn configure_seat(s: Seat) { s.bind(MOD | SHIFT | SYM_k, move || s.move_(Up)); s.bind(MOD | SHIFT | SYM_l, move || s.move_(Right)); - s.bind(SYM_x, || { - Command::new("alacritty").spawn() - }); - s.bind(SYM_y, || { - Command::new("sleep").arg("100").spawn() - }); + s.bind(SYM_x, || Command::new("alacritty").spawn()); + s.bind(SYM_y, || Command::new("sleep").arg("100").spawn()); } pub fn configure() { diff --git a/i4config/src/_private/client.rs b/i4config/src/_private/client.rs index 661bede0..5b964010 100644 --- a/i4config/src/_private/client.rs +++ b/i4config/src/_private/client.rs @@ -128,7 +128,11 @@ impl Client { } pub fn spawn(&self, command: &Command) { - let env = command.env.iter().map(|(a, b)| (a.to_string(), b.to_string())).collect(); + let env = command + .env + .iter() + .map(|(a, b)| (a.to_string(), b.to_string())) + .collect(); self.send(&ClientMessage::Run { prog: &command.prog, args: command.args.clone(), diff --git a/i4config/src/lib.rs b/i4config/src/lib.rs index 537f83bd..a7204291 100644 --- a/i4config/src/lib.rs +++ b/i4config/src/lib.rs @@ -1,9 +1,9 @@ #![feature(thread_local_const_init)] -use std::collections::HashMap; use crate::keyboard::keymap::Keymap; use crate::keyboard::ModifiedKeySym; use bincode::{Decode, Encode}; +use std::collections::HashMap; #[macro_use] mod macros; diff --git a/src/config/handler.rs b/src/config/handler.rs index dda0f345..5ee1df17 100644 --- a/src/config/handler.rs +++ b/src/config/handler.rs @@ -263,7 +263,12 @@ impl ConfigProxyHandler { Ok(()) } - fn handle_run(&self, prog: &str, args: Vec, env: Vec<(String, String)>) -> Result<(), RunError> { + fn handle_run( + &self, + prog: &str, + args: Vec, + env: Vec<(String, String)>, + ) -> Result<(), RunError> { let forker = match self.state.forker.get() { Some(f) => f, _ => return Err(RunError::NoForker), @@ -354,8 +359,6 @@ enum CphError { KeymapDoesNotExist(Keymap), #[error("Seat {0:?} does not exist")] SeatDoesNotExist(Seat), - #[error("Seat {0:?} does not exist")] - UnexpectedMessage(String), #[error("Could not parse the message")] ParsingFailed(#[source] DecodeError), } diff --git a/src/forker.rs b/src/forker.rs index 35404d93..4dff516d 100644 --- a/src/forker.rs +++ b/src/forker.rs @@ -10,13 +10,14 @@ use bincode::{Decode, Encode}; use i4config::_private::bincode_ops; use log::Level; use std::cell::Cell; +use std::env; use std::ffi::OsStr; use std::io::Read; use std::io::Write; use std::os::unix::ffi::OsStrExt; use std::rc::Rc; use thiserror::Error; -use uapi::{c, pipe2, IntoUstr, OwnedFd, UstrPtr, Fd}; +use uapi::{c, pipe2, Fd, IntoUstr, OwnedFd, UstrPtr}; pub struct ForkerProxy { pidfd: Rc, @@ -46,6 +47,7 @@ impl ForkerProxy { Ok(o) => o, Err(e) => return Err(ForkerError::Socketpair(e.into())), }; + let pid = uapi::getpid(); match fork_with_pidfd(false)? { Forked::Parent { pid, pidfd } => Ok(ForkerProxy { pidfd: Rc::new(pidfd), @@ -56,7 +58,7 @@ impl ForkerProxy { task_proc: Cell::new(None), outgoing: Default::default(), }), - Forked::Child { .. } => Forker::handle(child), + Forked::Child { .. } => Forker::handle(pid, child), } } @@ -83,11 +85,7 @@ impl ForkerProxy { } pub fn spawn(&self, prog: String, args: Vec, env: Vec<(String, String)>) { - self.outgoing.push(ServerMessage::Spawn { - prog, - args, - env, - }) + self.outgoing.push(ServerMessage::Spawn { prog, args, env }) } async fn incoming(self: Rc, socket: AsyncFd) { @@ -163,8 +161,15 @@ impl ForkerProxy { #[derive(Encode, Decode)] enum ServerMessage { - SetEnv { var: Vec, val: Vec }, - Spawn { prog: String, args: Vec, env: Vec<(String, String)> }, + SetEnv { + var: Vec, + val: Vec, + }, + Spawn { + prog: String, + args: Vec, + env: Vec<(String, String)>, + }, } #[derive(Encode, Decode)] @@ -180,11 +185,11 @@ struct Forker { } impl Forker { - fn handle(socket: OwnedFd) -> ! { - std::env::set_var("XDG_SESSION_TYPE", "wayland"); - std::env::remove_var("DISPLAY"); - std::env::remove_var("WAYLAND_DISPLAY"); - setup_deathsig(); + fn handle(ppid: c::pid_t, socket: OwnedFd) -> ! { + env::set_var("XDG_SESSION_TYPE", "wayland"); + env::remove_var("DISPLAY"); + env::remove_var("WAYLAND_DISPLAY"); + setup_deathsig(ppid); reset_signals(); let socket = Rc::new(setup_fds(socket)); std::panic::set_hook({ @@ -254,7 +259,7 @@ impl Forker { } fn handle_set_env(self: &Rc, var: &[u8], val: &[u8]) { - std::env::set_var(OsStr::from_bytes(var), OsStr::from_bytes(val)); + env::set_var(OsStr::from_bytes(var), OsStr::from_bytes(val)); } fn handle_spawn(self: &Rc, prog: String, args: Vec, env: Vec<(String, String)>) { @@ -293,7 +298,7 @@ impl Forker { c::signal(c::SIGCHLD, c::SIG_DFL); } for (key, val) in env { - std::env::set_var(&key, &val); + env::set_var(&key, &val); } let prog = prog.into_ustr(); let mut argsnt = UstrPtr::new(); @@ -335,9 +340,12 @@ fn reset_signals() { } } -fn setup_deathsig() { +fn setup_deathsig(ppid: c::pid_t) { unsafe { let res = c::prctl(c::PR_SET_PDEATHSIG, c::SIGKILL as c::c_ulong); uapi::map_err!(res).unwrap(); + if ppid != uapi::getppid() { + std::process::exit(0); + } } }