1
0
Fork 0
forked from wry/wry

autocommit 2022-02-15 23:50:26 CET

This commit is contained in:
Julian Orth 2022-02-15 23:50:26 +01:00
parent 3591f6f4f9
commit 086f2f73f4
5 changed files with 43 additions and 30 deletions

View file

@ -1,7 +1,9 @@
use i4config::keyboard::mods::{Modifiers, ALT, CTRL, SHIFT}; 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::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; 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_k, move || s.move_(Up));
s.bind(MOD | SHIFT | SYM_l, move || s.move_(Right)); s.bind(MOD | SHIFT | SYM_l, move || s.move_(Right));
s.bind(SYM_x, || { s.bind(SYM_x, || Command::new("alacritty").spawn());
Command::new("alacritty").spawn() s.bind(SYM_y, || Command::new("sleep").arg("100").spawn());
});
s.bind(SYM_y, || {
Command::new("sleep").arg("100").spawn()
});
} }
pub fn configure() { pub fn configure() {

View file

@ -128,7 +128,11 @@ impl Client {
} }
pub fn spawn(&self, command: &Command) { 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 { self.send(&ClientMessage::Run {
prog: &command.prog, prog: &command.prog,
args: command.args.clone(), args: command.args.clone(),

View file

@ -1,9 +1,9 @@
#![feature(thread_local_const_init)] #![feature(thread_local_const_init)]
use std::collections::HashMap;
use crate::keyboard::keymap::Keymap; use crate::keyboard::keymap::Keymap;
use crate::keyboard::ModifiedKeySym; use crate::keyboard::ModifiedKeySym;
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use std::collections::HashMap;
#[macro_use] #[macro_use]
mod macros; mod macros;

View file

@ -263,7 +263,12 @@ impl ConfigProxyHandler {
Ok(()) Ok(())
} }
fn handle_run(&self, prog: &str, args: Vec<String>, env: Vec<(String, String)>) -> Result<(), RunError> { fn handle_run(
&self,
prog: &str,
args: Vec<String>,
env: Vec<(String, String)>,
) -> Result<(), RunError> {
let forker = match self.state.forker.get() { let forker = match self.state.forker.get() {
Some(f) => f, Some(f) => f,
_ => return Err(RunError::NoForker), _ => return Err(RunError::NoForker),
@ -354,8 +359,6 @@ enum CphError {
KeymapDoesNotExist(Keymap), KeymapDoesNotExist(Keymap),
#[error("Seat {0:?} does not exist")] #[error("Seat {0:?} does not exist")]
SeatDoesNotExist(Seat), SeatDoesNotExist(Seat),
#[error("Seat {0:?} does not exist")]
UnexpectedMessage(String),
#[error("Could not parse the message")] #[error("Could not parse the message")]
ParsingFailed(#[source] DecodeError), ParsingFailed(#[source] DecodeError),
} }

View file

@ -10,13 +10,14 @@ use bincode::{Decode, Encode};
use i4config::_private::bincode_ops; use i4config::_private::bincode_ops;
use log::Level; use log::Level;
use std::cell::Cell; use std::cell::Cell;
use std::env;
use std::ffi::OsStr; use std::ffi::OsStr;
use std::io::Read; use std::io::Read;
use std::io::Write; use std::io::Write;
use std::os::unix::ffi::OsStrExt; use std::os::unix::ffi::OsStrExt;
use std::rc::Rc; use std::rc::Rc;
use thiserror::Error; use thiserror::Error;
use uapi::{c, pipe2, IntoUstr, OwnedFd, UstrPtr, Fd}; use uapi::{c, pipe2, Fd, IntoUstr, OwnedFd, UstrPtr};
pub struct ForkerProxy { pub struct ForkerProxy {
pidfd: Rc<OwnedFd>, pidfd: Rc<OwnedFd>,
@ -46,6 +47,7 @@ impl ForkerProxy {
Ok(o) => o, Ok(o) => o,
Err(e) => return Err(ForkerError::Socketpair(e.into())), Err(e) => return Err(ForkerError::Socketpair(e.into())),
}; };
let pid = uapi::getpid();
match fork_with_pidfd(false)? { match fork_with_pidfd(false)? {
Forked::Parent { pid, pidfd } => Ok(ForkerProxy { Forked::Parent { pid, pidfd } => Ok(ForkerProxy {
pidfd: Rc::new(pidfd), pidfd: Rc::new(pidfd),
@ -56,7 +58,7 @@ impl ForkerProxy {
task_proc: Cell::new(None), task_proc: Cell::new(None),
outgoing: Default::default(), 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<String>, env: Vec<(String, String)>) { pub fn spawn(&self, prog: String, args: Vec<String>, env: Vec<(String, String)>) {
self.outgoing.push(ServerMessage::Spawn { self.outgoing.push(ServerMessage::Spawn { prog, args, env })
prog,
args,
env,
})
} }
async fn incoming(self: Rc<Self>, socket: AsyncFd) { async fn incoming(self: Rc<Self>, socket: AsyncFd) {
@ -163,8 +161,15 @@ impl ForkerProxy {
#[derive(Encode, Decode)] #[derive(Encode, Decode)]
enum ServerMessage { enum ServerMessage {
SetEnv { var: Vec<u8>, val: Vec<u8> }, SetEnv {
Spawn { prog: String, args: Vec<String>, env: Vec<(String, String)> }, var: Vec<u8>,
val: Vec<u8>,
},
Spawn {
prog: String,
args: Vec<String>,
env: Vec<(String, String)>,
},
} }
#[derive(Encode, Decode)] #[derive(Encode, Decode)]
@ -180,11 +185,11 @@ struct Forker {
} }
impl Forker { impl Forker {
fn handle(socket: OwnedFd) -> ! { fn handle(ppid: c::pid_t, socket: OwnedFd) -> ! {
std::env::set_var("XDG_SESSION_TYPE", "wayland"); env::set_var("XDG_SESSION_TYPE", "wayland");
std::env::remove_var("DISPLAY"); env::remove_var("DISPLAY");
std::env::remove_var("WAYLAND_DISPLAY"); env::remove_var("WAYLAND_DISPLAY");
setup_deathsig(); setup_deathsig(ppid);
reset_signals(); reset_signals();
let socket = Rc::new(setup_fds(socket)); let socket = Rc::new(setup_fds(socket));
std::panic::set_hook({ std::panic::set_hook({
@ -254,7 +259,7 @@ impl Forker {
} }
fn handle_set_env(self: &Rc<Self>, var: &[u8], val: &[u8]) { fn handle_set_env(self: &Rc<Self>, 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<Self>, prog: String, args: Vec<String>, env: Vec<(String, String)>) { fn handle_spawn(self: &Rc<Self>, prog: String, args: Vec<String>, env: Vec<(String, String)>) {
@ -293,7 +298,7 @@ impl Forker {
c::signal(c::SIGCHLD, c::SIG_DFL); c::signal(c::SIGCHLD, c::SIG_DFL);
} }
for (key, val) in env { for (key, val) in env {
std::env::set_var(&key, &val); env::set_var(&key, &val);
} }
let prog = prog.into_ustr(); let prog = prog.into_ustr();
let mut argsnt = UstrPtr::new(); let mut argsnt = UstrPtr::new();
@ -335,9 +340,12 @@ fn reset_signals() {
} }
} }
fn setup_deathsig() { fn setup_deathsig(ppid: c::pid_t) {
unsafe { unsafe {
let res = c::prctl(c::PR_SET_PDEATHSIG, c::SIGKILL as c::c_ulong); let res = c::prctl(c::PR_SET_PDEATHSIG, c::SIGKILL as c::c_ulong);
uapi::map_err!(res).unwrap(); uapi::map_err!(res).unwrap();
if ppid != uapi::getppid() {
std::process::exit(0);
}
} }
} }