Skip to content

Commit 208527a

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 f3e1c6c commit 208527a

1 file changed

Lines changed: 7 additions & 11 deletions

File tree

  • crates/muvm/src/guest/bridge

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};
@@ -714,7 +713,7 @@ impl RemoteCaller {
714713

715714
// Find the vDSO and the address of a syscall instruction within it
716715
let (vdso_start, _) = find_vdso(Some(pid))?;
717-
let syscall_addr = vdso_start + SYSCALL_OFFSET.get().unwrap();
716+
let syscall_addr = vdso_start + SYSCALL_OFFSET.get_or_init(find_syscall_offset);
718717

719718
let mut regs = old_regs;
720719
arch::set_syscall_addr(&mut regs, syscall_addr);
@@ -857,9 +856,7 @@ fn find_vdso(pid: Option<Pid>) -> Result<(usize, usize), Errno> {
857856
Err(Errno::EINVAL)
858857
}
859858

860-
pub fn start_x11bridge(display: u32) {
861-
let sock_path = format!("/tmp/.X11-unix/X{display}");
862-
859+
fn find_syscall_offset() -> usize {
863860
// Look for a syscall instruction in the vDSO. We assume all processes map
864861
// the same vDSO (which should be true if they are running under the same
865862
// kernel!)
@@ -868,14 +865,13 @@ pub fn start_x11bridge(display: u32) {
868865
let addr = vdso_start + off;
869866
let val = unsafe { std::ptr::read(addr as *const arch::SyscallInstr) };
870867
if val == arch::SYSCALL_INSTR {
871-
SYSCALL_OFFSET.set(off).unwrap();
872-
break;
868+
return off;
873869
}
874870
}
875-
if SYSCALL_OFFSET.get().is_none() {
876-
eprintln!("Failed to find syscall instruction in vDSO");
877-
exit(1);
878-
}
871+
panic!("Failed to find syscall instruction in vDSO");
872+
}
879873

874+
pub fn start_x11bridge(display: u32) {
875+
let sock_path = format!("/tmp/.X11-unix/X{display}");
880876
common::bridge_loop::<X11ProtocolHandler>(&sock_path)
881877
}

0 commit comments

Comments
 (0)