11use std:: collections:: HashMap ;
2- use std:: env;
32use std:: ffi:: { c_char, CString } ;
43use std:: io:: Write ;
54use std:: os:: fd:: { IntoRawFd , OwnedFd } ;
65use std:: path:: Path ;
76use std:: process:: ExitCode ;
7+ use std:: { env, fs} ;
88
99use anyhow:: { anyhow, Context , Result } ;
1010use krun_sys:: {
11- krun_add_disk, krun_add_virtiofs2, krun_add_vsock_port, krun_create_ctx , krun_set_env ,
12- krun_set_gpu_options2, krun_set_log_level, krun_set_passt_fd, krun_set_root,
11+ krun_add_disk, krun_add_virtiofs2, krun_add_vsock_port, krun_add_vsock_port2 , krun_create_ctx ,
12+ krun_set_env , krun_set_gpu_options2, krun_set_log_level, krun_set_passt_fd, krun_set_root,
1313 krun_set_vm_config, krun_set_workdir, krun_start_enter, VIRGLRENDERER_DRM ,
1414 VIRGLRENDERER_THREAD_SYNC , VIRGLRENDERER_USE_ASYNC_FENCE_CB , VIRGLRENDERER_USE_EGL ,
1515} ;
@@ -22,7 +22,9 @@ use muvm::launch::{launch_or_lock, LaunchResult, DYNAMIC_PORT_RANGE};
2222use muvm:: monitor:: spawn_monitor;
2323use muvm:: net:: { connect_to_passt, start_passt} ;
2424use muvm:: types:: MiB ;
25- use muvm:: utils:: launch:: { GuestConfiguration , Launch } ;
25+ use muvm:: utils:: launch:: {
26+ GuestConfiguration , Launch , HIDPIPE_SOCKET , MUVM_GUEST_SOCKET , PULSE_SOCKET ,
27+ } ;
2628use nix:: sys:: sysinfo:: sysinfo;
2729use nix:: unistd:: User ;
2830use rustix:: io:: Errno ;
@@ -72,8 +74,7 @@ fn main() -> Result<ExitCode> {
7274
7375 let options = options ( ) . fallback_to_usage ( ) . run ( ) ;
7476
75- let ( cookie, _lock_file, command, command_args, env) = match launch_or_lock (
76- options. server_port ,
77+ let ( _lock_file, command, command_args, env) = match launch_or_lock (
7778 options. command ,
7879 options. command_args ,
7980 options. env ,
@@ -87,12 +88,11 @@ fn main() -> Result<ExitCode> {
8788 return Ok ( code) ;
8889 } ,
8990 LaunchResult :: LockAcquired {
90- cookie,
9191 lock_file,
9292 command,
9393 command_args,
9494 env,
95- } => ( cookie , lock_file, command, command_args, env) ,
95+ } => ( lock_file, command, command_args, env) ,
9696 } ;
9797
9898 let mut env = prepare_env_vars ( env) . context ( "Failed to prepare environment variables" ) ?;
@@ -264,9 +264,7 @@ fn main() -> Result<ExitCode> {
264264 . context ( "Failed to connect to `passt`" ) ?
265265 . into ( )
266266 } else {
267- start_passt ( options. server_port )
268- . context ( "Failed to start `passt`" ) ?
269- . into ( )
267+ start_passt ( ) . context ( "Failed to start `passt`" ) ?. into ( )
270268 } ;
271269 // SAFETY: `passt_fd` is an `OwnedFd` and consumed to prevent closing on drop.
272270 // See https://doc.rust-lang.org/std/io/index.html#io-safety
@@ -287,7 +285,7 @@ fn main() -> Result<ExitCode> {
287285 )
288286 . context ( "Failed to process `pulse/native` path as it contains NUL character" ) ?;
289287 // SAFETY: `pulse_path` is a pointer to a `CString` with long enough lifetime.
290- let err = unsafe { krun_add_vsock_port ( ctx_id, 3333 , pulse_path. as_ptr ( ) ) } ;
288+ let err = unsafe { krun_add_vsock_port ( ctx_id, PULSE_SOCKET , pulse_path. as_ptr ( ) ) } ;
291289 if err < 0 {
292290 let err = Errno :: from_raw_os_error ( -err) ;
293291 return Err ( err) . context ( "Failed to configure vsock for pulse socket" ) ;
@@ -304,7 +302,7 @@ fn main() -> Result<ExitCode> {
304302 . context ( "Failed to process `hidpipe` path as it contains NUL character" ) ?;
305303
306304 // SAFETY: `hidpipe_path` is a pointer to a `CString` with long enough lifetime.
307- let err = unsafe { krun_add_vsock_port ( ctx_id, 3334 , hidpipe_path. as_ptr ( ) ) } ;
305+ let err = unsafe { krun_add_vsock_port ( ctx_id, HIDPIPE_SOCKET , hidpipe_path. as_ptr ( ) ) } ;
308306 if err < 0 {
309307 let err = Errno :: from_raw_os_error ( -err) ;
310308 return Err ( err) . context ( "Failed to configure vsock for hidpipe socket" ) ;
@@ -328,6 +326,22 @@ fn main() -> Result<ExitCode> {
328326 return Err ( err) . context ( "Failed to configure vsock for dynamic socket" ) ;
329327 }
330328 }
329+
330+ let server_path = Path :: new ( & run_path) . join ( "krun/server" ) ;
331+ _ = fs:: remove_file ( & server_path) ;
332+ let server_path = CString :: new (
333+ server_path
334+ . to_str ( )
335+ . expect ( "server_path should not contain invalid UTF-8" ) ,
336+ )
337+ . context ( "Failed to process `muvm-guest` path as it contains NUL characters" ) ?;
338+ // SAFETY: `server_path` is a pointer to a `CString` with long enough lifetime.
339+ let err =
340+ unsafe { krun_add_vsock_port2 ( ctx_id, MUVM_GUEST_SOCKET , server_path. as_ptr ( ) , true ) } ;
341+ if err < 0 {
342+ let err = Errno :: from_raw_os_error ( -err) ;
343+ return Err ( err) . context ( "Failed to configure vsock for guest server socket" ) ;
344+ }
331345 }
332346
333347 let username = env:: var ( "USER" ) . context ( "Failed to get username from environment" ) ?;
@@ -366,20 +380,17 @@ fn main() -> Result<ExitCode> {
366380 let display = env:: var ( "DISPLAY" ) . ok ( ) ;
367381 let guest_config = GuestConfiguration {
368382 command : Launch {
369- cookie,
370383 command,
371384 command_args,
372385 env : HashMap :: new ( ) ,
373386 vsock_port : 0 ,
374387 tty : false ,
375388 privileged : false ,
376389 } ,
377- server_port : options. server_port ,
378390 username,
379391 uid : getuid ( ) . as_raw ( ) ,
380392 gid : getgid ( ) . as_raw ( ) ,
381393 host_display : display,
382- server_cookie : cookie,
383394 merged_rootfs : options. merged_rootfs ,
384395 } ;
385396 let mut muvm_config_file = NamedTempFile :: new ( )
@@ -449,7 +460,7 @@ fn main() -> Result<ExitCode> {
449460 }
450461 }
451462
452- spawn_monitor ( options . server_port , cookie ) ;
463+ spawn_monitor ( ) ;
453464
454465 {
455466 // Start and enter the microVM. Unless there is some error while creating the
0 commit comments