11use std:: env;
22use std:: os:: unix:: process:: ExitStatusExt as _;
3+ use std:: path:: PathBuf ;
34
45use anyhow:: { Context , Result } ;
56use log:: error;
67use muvm:: server:: cli_options:: options;
78use muvm:: server:: worker:: { State , Worker } ;
9+ use nix:: unistd:: geteuid;
810use tokio:: net:: TcpListener ;
911use tokio:: process:: Command ;
1012use tokio:: sync:: watch;
@@ -23,19 +25,33 @@ fn main() -> Result<()> {
2325async fn tokio_main ( cookie : String ) -> Result < ( ) > {
2426 env_logger:: init ( ) ;
2527
26- let options = options ( ) . run ( ) ;
2728 let cookie = Uuid :: try_parse ( & cookie) . context ( "Couldn't parse cookie as UUID v7" ) ?;
29+ let uid: u32 = geteuid ( ) . into ( ) ;
2830
29- let listener = TcpListener :: bind ( format ! ( "0.0.0.0:{}" , options. server_port) ) . await ?;
31+ let ( server_port, command, command_args) = if uid == 0 {
32+ let server_port = if let Ok ( server_port) = env:: var ( "MUVM_ROOT_SERVER_PORT" ) {
33+ server_port. parse ( ) ?
34+ } else {
35+ 3335
36+ } ;
37+ (
38+ server_port,
39+ PathBuf :: from ( "/bin/sleep" ) ,
40+ vec ! [ "inf" . to_string( ) ] ,
41+ )
42+ } else {
43+ let options = options ( ) . run ( ) ;
44+ ( options. server_port , options. command , options. command_args )
45+ } ;
46+
47+ let listener = TcpListener :: bind ( format ! ( "0.0.0.0:{}" , server_port) ) . await ?;
3048 let ( state_tx, state_rx) = watch:: channel ( State :: new ( ) ) ;
3149
3250 let mut worker_handle = tokio:: spawn ( async move {
3351 let mut worker = Worker :: new ( cookie, listener, state_tx) ;
3452 worker. run ( ) . await ;
3553 } ) ;
36- let command_status = Command :: new ( & options. command )
37- . args ( options. command_args )
38- . status ( ) ;
54+ let command_status = Command :: new ( & command) . args ( command_args) . status ( ) ;
3955 tokio:: pin!( command_status) ;
4056 let mut state_rx = WatchStream :: new ( state_rx) ;
4157
@@ -63,12 +79,12 @@ async fn tokio_main(cookie: String) -> Result<()> {
6379 if let Some ( code) = status. code( ) {
6480 eprintln!(
6581 "{:?} process exited with status code: {code}" ,
66- options . command
82+ command
6783 ) ;
6884 } else {
6985 eprintln!(
7086 "{:?} process terminated by signal: {}" ,
71- options . command,
87+ command,
7288 status
7389 . signal( )
7490 . expect( "either one of status code or signal should be set" )
@@ -79,7 +95,7 @@ async fn tokio_main(cookie: String) -> Result<()> {
7995 Err ( err) => {
8096 eprintln!(
8197 "Failed to execute {:?} as child process: {err}" ,
82- options . command
98+ command
8399 ) ;
84100 } ,
85101 }
0 commit comments