Skip to content

Commit 6f0d354

Browse files
WhatAmISupposedToPutHereslp
authored andcommitted
Merge muvm-guest and muvm-server
We no longer need the server to be a separate binary. Signed-off-by: Sasha Finkelstein <[email protected]>
1 parent 1acbfa7 commit 6f0d354

10 files changed

Lines changed: 30 additions & 92 deletions

File tree

crates/muvm/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ path = "src/guest/bin/muvm-guest.rs"
4040
name = "muvm-hidpipe"
4141
path = "src/hidpipe/bin/muvm-hidpipe.rs"
4242

43-
[[bin]]
44-
name = "muvm-server"
45-
path = "src/server/bin/muvm-server.rs"
46-
4743
[[bin]]
4844
name = "muvm-x11bridge"
4945
path = "src/x11bridge/bin/muvm-x11bridge.rs"

crates/muvm/src/bin/muvm.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,6 @@ fn main() -> Result<ExitCode> {
384384
}
385385

386386
let muvm_guest_path = find_muvm_exec("muvm-guest")?;
387-
let muvm_server_path = find_muvm_exec("muvm-server")?;
388387

389388
let mut muvm_guest_args: Vec<String> = vec![
390389
muvm_guest_path
@@ -394,10 +393,6 @@ fn main() -> Result<ExitCode> {
394393
username,
395394
format!("{uid}", uid = getuid().as_raw()),
396395
format!("{gid}", gid = getgid().as_raw()),
397-
muvm_server_path
398-
.to_str()
399-
.context("Failed to process `muvm-server` path as it contains invalid UTF-8")?
400-
.to_owned(),
401396
command
402397
.to_str()
403398
.context("Failed to process command as it contains invalid UTF-8")?

crates/muvm/src/guest/bin/muvm-guest.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
use std::cmp;
22
use std::os::fd::AsFd;
3-
use std::os::unix::process::CommandExt as _;
43
use std::process::Command;
54

65
use anyhow::{Context, Result};
7-
use log::debug;
86
use muvm::env::find_muvm_exec;
97
use muvm::guest::cli_options::options;
108
use muvm::guest::fex::setup_fex;
119
use muvm::guest::mount::mount_filesystems;
1210
use muvm::guest::net::configure_network;
11+
use muvm::guest::server::server_main;
1312
use muvm::guest::socket::setup_socket_proxy;
1413
use muvm::guest::user::setup_user;
1514
use muvm::guest::x11::setup_x11_forwarding;
16-
use nix::unistd::{setresgid, setresuid, Gid, Uid};
1715
use rustix::process::{getrlimit, setrlimit, Resource};
1816

1917
fn main() -> Result<()> {
@@ -75,11 +73,8 @@ fn main() -> Result<()> {
7573

7674
setup_x11_forwarding(run_path)?;
7775

78-
setresuid(options.uid, Uid::from(0), Uid::from(0))?;
79-
setresgid(options.gid, Gid::from(0), Gid::from(0))?;
80-
debug!(command:? = options.command, command_args:? = options.command_args; "exec");
81-
let err = Command::new(&options.command)
82-
.args(options.command_args)
83-
.exec();
84-
Err(err).with_context(|| format!("Failed to exec {:?}", options.command))?
76+
let rt = tokio::runtime::Runtime::new().unwrap();
77+
rt.block_on(async {
78+
server_main(options.server_port, options.command, options.command_args).await
79+
})
8580
}

crates/muvm/src/guest/cli_options.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::path::PathBuf;
22

33
use anyhow::Context;
4-
use bpaf::{any, construct, positional, OptionParser, Parser};
4+
use bpaf::{any, construct, env, positional, OptionParser, Parser};
55
use nix::libc::{gid_t, uid_t};
66
use nix::unistd::{Gid, Uid};
77

@@ -10,11 +10,18 @@ pub struct Options {
1010
pub username: String,
1111
pub uid: Uid,
1212
pub gid: Gid,
13+
pub server_port: u32,
1314
pub command: PathBuf,
1415
pub command_args: Vec<String>,
1516
}
1617

1718
pub fn options() -> OptionParser<Options> {
19+
let server_port = env("MUVM_SERVER_PORT")
20+
.short('p')
21+
.help("TCP port to listen for command launch requests")
22+
.argument("SERVER_PORT")
23+
.fallback(3334)
24+
.display_fallback();
1825
let username = positional("USER");
1926
let uid = positional::<String>("UID").parse(|s| {
2027
s.parse::<uid_t>()
@@ -33,6 +40,7 @@ pub fn options() -> OptionParser<Options> {
3340
.many();
3441

3542
construct!(Options {
43+
server_port,
3644
// positionals
3745
username,
3846
uid,

crates/muvm/src/guest/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ pub mod cli_options;
22
pub mod fex;
33
pub mod mount;
44
pub mod net;
5+
pub mod server;
6+
pub mod server_worker;
57
pub mod socket;
68
pub mod user;
79
pub mod x11;
Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,34 @@
1-
use std::env;
2-
use std::os::unix::process::ExitStatusExt as _;
3-
1+
use crate::guest::server_worker::{State, Worker};
42
use anyhow::{Context, Result};
53
use log::error;
6-
use muvm::server::cli_options::options;
7-
use muvm::server::worker::{State, Worker};
8-
use nix::unistd::{getgid, getuid, setresgid, setresuid, Gid, Uid};
4+
use std::env;
5+
use std::os::unix::process::ExitStatusExt as _;
6+
use std::path::PathBuf;
97
use tokio::net::TcpListener;
108
use tokio::process::Command;
119
use tokio::sync::watch;
1210
use tokio_stream::wrappers::WatchStream;
1311
use tokio_stream::StreamExt as _;
1412
use uuid::Uuid;
1513

16-
fn main() -> Result<()> {
14+
pub async fn server_main(
15+
server_port: u32,
16+
command: PathBuf,
17+
command_args: Vec<String>,
18+
) -> Result<()> {
1719
let cookie = env::var("MUVM_SERVER_COOKIE")
1820
.with_context(|| "Could find server cookie as an environment variable")?;
1921

20-
let rt = tokio::runtime::Runtime::new().unwrap();
21-
rt.block_on(async { tokio_main(cookie).await })
22-
}
23-
24-
async fn tokio_main(cookie: String) -> Result<()> {
25-
env_logger::init();
26-
2722
let cookie = Uuid::try_parse(&cookie).context("Couldn't parse cookie as UUID v7")?;
28-
let uid = getuid();
29-
let gid = getgid();
30-
setresuid(uid, uid, Uid::from(0))?;
31-
setresgid(gid, gid, Gid::from(0))?;
32-
33-
let options = options().run();
3423

35-
let listener = TcpListener::bind(format!("0.0.0.0:{}", options.server_port)).await?;
24+
let listener = TcpListener::bind(format!("0.0.0.0:{}", server_port)).await?;
3625
let (state_tx, state_rx) = watch::channel(State::new());
3726

3827
let mut worker_handle = tokio::spawn(async move {
3928
let mut worker = Worker::new(cookie, listener, state_tx);
4029
worker.run().await;
4130
});
42-
let command_status = Command::new(&options.command)
43-
.args(options.command_args)
44-
.status();
31+
let command_status = Command::new(&command).args(command_args).status();
4532
tokio::pin!(command_status);
4633
let mut state_rx = WatchStream::new(state_rx);
4734

@@ -69,12 +56,12 @@ async fn tokio_main(cookie: String) -> Result<()> {
6956
if let Some(code) = status.code() {
7057
eprintln!(
7158
"{:?} process exited with status code: {code}",
72-
options.command
59+
command
7360
);
7461
} else {
7562
eprintln!(
7663
"{:?} process terminated by signal: {}",
77-
options.command,
64+
command,
7865
status
7966
.signal()
8067
.expect("either one of status code or signal should be set")
@@ -85,7 +72,7 @@ async fn tokio_main(cookie: String) -> Result<()> {
8572
Err(err) => {
8673
eprintln!(
8774
"Failed to execute {:?} as child process: {err}",
88-
options.command
75+
command
8976
);
9077
},
9178
}

crates/muvm/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ pub mod net;
99
pub mod types;
1010

1111
pub mod guest;
12-
pub mod server;
1312
pub mod tty;
1413
pub mod utils;

crates/muvm/src/server/cli_options.rs

Lines changed: 0 additions & 42 deletions
This file was deleted.

crates/muvm/src/server/mod.rs

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)