Skip to content

Commit 610e787

Browse files
WhatAmISupposedToPutHereslp
authored andcommitted
Merge x11bridge into the server process.
Signed-off-by: Sasha Finkelstein <[email protected]>
1 parent 6f0d354 commit 610e787

5 files changed

Lines changed: 19 additions & 69 deletions

File tree

crates/muvm/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,3 @@ path = "src/guest/bin/muvm-guest.rs"
3939
[[bin]]
4040
name = "muvm-hidpipe"
4141
path = "src/hidpipe/bin/muvm-hidpipe.rs"
42-
43-
[[bin]]
44-
name = "muvm-x11bridge"
45-
path = "src/x11bridge/bin/muvm-x11bridge.rs"

crates/muvm/src/guest/bin/muvm-guest.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::cmp;
21
use std::os::fd::AsFd;
32
use std::process::Command;
3+
use std::{cmp, env};
44

55
use anyhow::{Context, Result};
66
use muvm::env::find_muvm_exec;
@@ -12,11 +12,17 @@ use muvm::guest::server::server_main;
1212
use muvm::guest::socket::setup_socket_proxy;
1313
use muvm::guest::user::setup_user;
1414
use muvm::guest::x11::setup_x11_forwarding;
15+
use muvm::guest::x11bridge::start_x11bridge;
1516
use rustix::process::{getrlimit, setrlimit, Resource};
1617

1718
fn main() -> Result<()> {
1819
env_logger::init();
1920

21+
if let Ok(val) = env::var("__X11BRIDGE_DEBUG") {
22+
start_x11bridge(val.parse()?);
23+
return Ok(());
24+
}
25+
2026
let options = options().run();
2127

2228
{

crates/muvm/src/guest/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ pub mod server_worker;
77
pub mod socket;
88
pub mod user;
99
pub mod x11;
10+
pub mod x11bridge;

crates/muvm/src/guest/x11.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
use std::env;
21
use std::fs::File;
32
use std::io::{Read, Write};
3+
use std::panic::catch_unwind;
44
use std::path::Path;
5-
use std::process::Command;
5+
use std::{env, thread};
66

77
use anyhow::{anyhow, Context, Result};
88
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
99

10-
use crate::env::find_muvm_exec;
10+
use crate::guest::x11bridge::start_x11bridge;
1111

1212
pub fn setup_x11_forwarding<P>(run_path: P) -> Result<bool>
1313
where
@@ -24,11 +24,6 @@ where
2424
}
2525
let host_display = &host_display[1..];
2626

27-
let mut cmd = Command::new(find_muvm_exec("muvm-x11bridge")?);
28-
cmd.args(["--listen-display", ":1"]);
29-
30-
cmd.spawn().context("Failed to spawn `muvm-x11bridge`")?;
31-
3227
// SAFETY: Safe if and only if `muvm-guest` program is not multithreaded.
3328
// See https://doc.rust-lang.org/std/env/fn.set_var.html#safety
3429
env::set_var("DISPLAY", ":1");
@@ -89,6 +84,11 @@ where
8984
// See https://doc.rust-lang.org/std/env/fn.set_var.html#safety
9085
env::set_var("XAUTHORITY", dst_path);
9186
}
87+
thread::spawn(|| {
88+
if catch_unwind(|| start_x11bridge(1)).is_err() {
89+
eprintln!("x11bridge thread crashed, x11 passthrough will no longer function");
90+
}
91+
});
9292

9393
Ok(true)
9494
}

crates/muvm/src/x11bridge/bin/muvm-x11bridge.rs renamed to crates/muvm/src/guest/x11bridge.rs

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use anyhow::Result;
2-
use bpaf::{construct, long, OptionParser, Parser};
32
use nix::errno::Errno;
43
use nix::fcntl::readlink;
54
use nix::libc::{
@@ -18,7 +17,7 @@ use nix::sys::socket::{
1817
use nix::sys::stat::fstat;
1918
use nix::sys::uio::{process_vm_writev, RemoteIoVec};
2019
use nix::sys::wait::{waitpid, WaitPidFlag, WaitStatus};
21-
use nix::unistd::{getresgid, getresuid, mkstemp, read, setegid, seteuid, Pid};
20+
use nix::unistd::{mkstemp, read, Pid};
2221
use nix::{cmsg_space, ioctl_read, ioctl_readwrite, ioctl_write_ptr, NixPath};
2322
use std::borrow::Cow;
2423
use std::cell::RefCell;
@@ -1445,36 +1444,8 @@ fn find_vdso(pid: Option<Pid>) -> Result<(usize, usize), Errno> {
14451444
Err(Errno::EINVAL)
14461445
}
14471446

1448-
#[derive(Clone, Debug)]
1449-
pub struct Options {
1450-
listen_display: String,
1451-
}
1452-
1453-
pub fn options() -> OptionParser<Options> {
1454-
let listen_display = long("listen-display")
1455-
.short('l')
1456-
.help("The X11 display number to listen on")
1457-
.argument("DISPLAY")
1458-
.fallback(":0".into());
1459-
1460-
construct!(Options { listen_display }).to_options()
1461-
}
1462-
1463-
fn main() {
1464-
let options = options().fallback_to_usage().run();
1465-
1466-
let display = if options.listen_display.starts_with(':') {
1467-
options.listen_display[1..].parse::<u32>().ok()
1468-
} else {
1469-
None
1470-
};
1471-
1472-
let sock_path = if let Some(display) = display {
1473-
format!("/tmp/.X11-unix/X{}", display)
1474-
} else {
1475-
eprintln!("Invalid --listen-display value");
1476-
exit(1)
1477-
};
1447+
pub fn start_x11bridge(display: u32) {
1448+
let sock_path = format!("/tmp/.X11-unix/X{}", display);
14781449

14791450
// Look for a syscall instruction in the vDSO. We assume all processes map
14801451
// the same vDSO (which should be true if they are running under the same
@@ -1495,21 +1466,7 @@ fn main() {
14951466

14961467
let epoll = Epoll::new(EpollCreateFlags::empty()).unwrap();
14971468
_ = fs::remove_file(&sock_path);
1498-
let resuid = getresuid().unwrap();
1499-
let resgid = getresgid().unwrap();
1500-
if resuid.real != resuid.effective {
1501-
seteuid(resuid.real).unwrap();
1502-
}
1503-
if resgid.real != resgid.effective {
1504-
setegid(resgid.real).unwrap()
1505-
}
15061469
let listen_sock = UnixListener::bind(sock_path).unwrap();
1507-
if resuid.real != resuid.effective {
1508-
seteuid(resuid.effective).unwrap();
1509-
}
1510-
if resgid.real != resgid.effective {
1511-
setegid(resgid.effective).unwrap()
1512-
}
15131470
epoll
15141471
.add(
15151472
&listen_sock,
@@ -1640,13 +1597,3 @@ fn main() {
16401597
}
16411598
}
16421599
}
1643-
1644-
#[cfg(test)]
1645-
mod tests {
1646-
use super::*;
1647-
1648-
#[test]
1649-
fn check_options() {
1650-
options().check_invariants(false)
1651-
}
1652-
}

0 commit comments

Comments
 (0)