Skip to content

Commit 6ad8dcd

Browse files
committed
x11: use get_or_init instead of the set/get dance for the offset
This is how OnceLock is really meant to be used.. This will be helpful for the custom init support, where various ways of starting the bridge loop will be used. Signed-off-by: Val Packett <[email protected]>
1 parent 1342075 commit 6ad8dcd

File tree

1 file changed

+7
-11
lines changed
  • crates/muvm/src/guest/bridge

1 file changed

+7
-11
lines changed

crates/muvm/src/guest/bridge/x11.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::ffi::{c_long, c_void, CString};
44
use std::fs::{read_to_string, remove_file, File};
55
use std::io::{IoSlice, Write};
66
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, OwnedFd};
7-
use std::process::exit;
87
use std::ptr::NonNull;
98
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
109
use std::sync::{Arc, OnceLock};
@@ -704,7 +703,7 @@ impl RemoteCaller {
704703

705704
// Find the vDSO and the address of a syscall instruction within it
706705
let (vdso_start, _) = find_vdso(Some(pid))?;
707-
let syscall_addr = vdso_start + SYSCALL_OFFSET.get().unwrap();
706+
let syscall_addr = vdso_start + SYSCALL_OFFSET.get_or_init(find_syscall_offset);
708707

709708
let mut regs = old_regs;
710709
arch::set_syscall_addr(&mut regs, syscall_addr);
@@ -847,9 +846,7 @@ fn find_vdso(pid: Option<Pid>) -> Result<(usize, usize), Errno> {
847846
Err(Errno::EINVAL)
848847
}
849848

850-
pub fn start_x11bridge(display: u32) {
851-
let sock_path = format!("/tmp/.X11-unix/X{display}");
852-
849+
fn find_syscall_offset() -> usize {
853850
// Look for a syscall instruction in the vDSO. We assume all processes map
854851
// the same vDSO (which should be true if they are running under the same
855852
// kernel!)
@@ -858,14 +855,13 @@ pub fn start_x11bridge(display: u32) {
858855
let addr = vdso_start + off;
859856
let val = unsafe { std::ptr::read(addr as *const arch::SyscallInstr) };
860857
if val == arch::SYSCALL_INSTR {
861-
SYSCALL_OFFSET.set(off).unwrap();
862-
break;
858+
return off;
863859
}
864860
}
865-
if SYSCALL_OFFSET.get().is_none() {
866-
eprintln!("Failed to find syscall instruction in vDSO");
867-
exit(1);
868-
}
861+
panic!("Failed to find syscall instruction in vDSO");
862+
}
869863

864+
pub fn start_x11bridge(display: u32) {
865+
let sock_path = format!("/tmp/.X11-unix/X{display}");
870866
common::bridge_loop::<X11ProtocolHandler>(&sock_path)
871867
}

0 commit comments

Comments
 (0)