|
| 1 | +use crate::socket::setup_socket_proxy; |
| 2 | + |
| 3 | +use anyhow::{anyhow, Context, Result}; |
| 4 | +use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; |
| 5 | +use std::env; |
| 6 | +use std::fs::File; |
| 7 | +use std::io::{Read, Write}; |
| 8 | +use std::path::Path; |
| 9 | + |
| 10 | +pub fn setup_x11_forwarding<P>(run_path: P) -> Result<()> |
| 11 | +where |
| 12 | + P: AsRef<Path>, |
| 13 | +{ |
| 14 | + // Set by krun if DISPLAY was provided from the host. |
| 15 | + let host_display = match env::var("HOST_DISPLAY") { |
| 16 | + Ok(d) => d, |
| 17 | + Err(_) => return Ok(()), |
| 18 | + }; |
| 19 | + |
| 20 | + if !host_display.starts_with(":") { |
| 21 | + return Err(anyhow!("Invalid host DISPLAY")); |
| 22 | + } |
| 23 | + let host_display = &host_display[1..]; |
| 24 | + |
| 25 | + setup_socket_proxy(Path::new("/tmp/.X11-unix/X1"), 6000)?; |
| 26 | + |
| 27 | + // Set HOST_DISPLAY to :1, which is the display number within the guest |
| 28 | + // at which the actual host display is accessible. |
| 29 | + // SAFETY: Safe if and only if `krun-guest` program is not multithreaded. |
| 30 | + // See https://doc.rust-lang.org/std/env/fn.set_var.html#safety |
| 31 | + env::set_var("HOST_DISPLAY", ":1"); |
| 32 | + |
| 33 | + if let Ok(xauthority) = std::env::var("XAUTHORITY") { |
| 34 | + let src_path = format!("/run/krun-host/{}", xauthority); |
| 35 | + let mut rdr = File::open(src_path)?; |
| 36 | + |
| 37 | + let dst_path = run_path.as_ref().join("xauth"); |
| 38 | + let mut wtr = File::options() |
| 39 | + .write(true) |
| 40 | + .create(true) |
| 41 | + .truncate(true) |
| 42 | + .open(&dst_path) |
| 43 | + .context("Failed to create `xauth`")?; |
| 44 | + |
| 45 | + while let Ok(family) = rdr.read_u16::<BigEndian>() { |
| 46 | + let mut addr = vec![0u8; rdr.read_u16::<BigEndian>()? as usize]; |
| 47 | + rdr.read_exact(&mut addr)?; |
| 48 | + |
| 49 | + let mut display = vec![0u8; rdr.read_u16::<BigEndian>()? as usize]; |
| 50 | + rdr.read_exact(&mut display)?; |
| 51 | + |
| 52 | + let mut name = vec![0u8; rdr.read_u16::<BigEndian>()? as usize]; |
| 53 | + rdr.read_exact(&mut name)?; |
| 54 | + |
| 55 | + let mut data = vec![0u8; rdr.read_u16::<BigEndian>()? as usize]; |
| 56 | + rdr.read_exact(&mut data)?; |
| 57 | + |
| 58 | + // Only copy the wildcard entry |
| 59 | + if family != 0xffff { |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + // Check for the correct host display |
| 64 | + if display != host_display.as_bytes() { |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + // Always use display number 1 |
| 69 | + let display = b"1"; |
| 70 | + |
| 71 | + wtr.write_u16::<BigEndian>(family)?; |
| 72 | + wtr.write_u16::<BigEndian>(addr.len().try_into()?)?; |
| 73 | + wtr.write(&addr)?; |
| 74 | + wtr.write_u16::<BigEndian>(display.len().try_into()?)?; |
| 75 | + wtr.write(display)?; |
| 76 | + wtr.write_u16::<BigEndian>(name.len().try_into()?)?; |
| 77 | + wtr.write(&name)?; |
| 78 | + wtr.write_u16::<BigEndian>(data.len().try_into()?)?; |
| 79 | + wtr.write(&data)?; |
| 80 | + |
| 81 | + break; |
| 82 | + } |
| 83 | + |
| 84 | + // SAFETY: Safe if and only if `krun-guest` program is not multithreaded. |
| 85 | + // See https://doc.rust-lang.org/std/env/fn.set_var.html#safety |
| 86 | + env::set_var("XAUTHORITY", dst_path); |
| 87 | + } |
| 88 | + |
| 89 | + Ok(()) |
| 90 | +} |
0 commit comments