Skip to content

Commit e227614

Browse files
hoshinolinaslp
authored andcommitted
Add dynamic UNIX socket forwarding
Unconditionally forward a range of ports to sockets named `$XDG_RUNTIME_DIR/krun/socket/port-$PORT`. This allows applications to dynamically bind to sockets at those paths and have them forwarded to the guest as vsock ports, after the krun VM is already running. The port range is currently hardcoded as 50000..50200. Signed-off-by: Asahi Lina <[email protected]> Signed-off-by: Sasha Finkelstein <[email protected]>
1 parent 9e67d19 commit e227614

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

crates/muvm/src/bin/muvm.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ fn main() -> Result<()> {
297297
return Err(err).context("Failed to configure vsock for pulse socket");
298298
}
299299
}
300+
300301
let hidpipe_path = Path::new(&run_path).join("hidpipe");
301302
spawn_hidpipe_server(hidpipe_path.clone()).context("Failed to spawn hidpipe thread")?;
302303
let hidpipe_path = CString::new(
@@ -312,6 +313,25 @@ fn main() -> Result<()> {
312313
let err = Errno::from_raw_os_error(-err);
313314
return Err(err).context("Failed to configure vsock for hidpipe socket");
314315
}
316+
317+
let socket_dir = Path::new(&run_path).join("krun/socket");
318+
std::fs::create_dir_all(&socket_dir)?;
319+
// Dynamic ports: Applications may listen on these sockets as neeeded.
320+
for port in 50000..50200 {
321+
let socket_path = socket_dir.join(format!("port-{}", port));
322+
let socket_path = CString::new(
323+
socket_path
324+
.to_str()
325+
.expect("socket_path should not contain invalid UTF-8"),
326+
)
327+
.context("Failed to process dynamic socket path as it contains NUL character")?;
328+
// SAFETY: `socket_path` is a pointer to a `CString` with long enough lifetime.
329+
let err = unsafe { krun_add_vsock_port(ctx_id, port, socket_path.as_ptr()) };
330+
if err < 0 {
331+
let err = Errno::from_raw_os_error(-err);
332+
return Err(err).context("Failed to configure vsock for dynamic socket");
333+
}
334+
}
315335
}
316336

317337
// Forward the native X11 display into the guest as a socket

0 commit comments

Comments
 (0)