From 6d0e54bfa14b30bf7334bcb875d8e1f8d5d3ecd3 Mon Sep 17 00:00:00 2001 From: Chip Senkbeil Date: Fri, 15 Oct 2021 20:40:35 -0500 Subject: [PATCH] Remove --daemon in favor of opposite parameter --foreground --- Cargo.lock | 8 ++++---- distant-ssh2/src/lib.rs | 2 +- src/opt.rs | 13 ++++++------- src/subcommand/launch.rs | 4 ++-- src/subcommand/listen.rs | 17 ++++++++++++----- 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ad817be..7faaff0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -427,7 +427,7 @@ dependencies = [ [[package]] name = "distant" -version = "0.15.0-alpha.17" +version = "0.15.0-alpha.18" dependencies = [ "assert_cmd", "assert_fs", @@ -451,7 +451,7 @@ dependencies = [ [[package]] name = "distant-core" -version = "0.15.0-alpha.17" +version = "0.15.0-alpha.18" dependencies = [ "assert_fs", "bytes", @@ -476,7 +476,7 @@ dependencies = [ [[package]] name = "distant-lua" -version = "0.15.0-alpha.17" +version = "0.15.0-alpha.18" dependencies = [ "distant-core", "distant-ssh2", @@ -510,7 +510,7 @@ dependencies = [ [[package]] name = "distant-ssh2" -version = "0.15.0-alpha.17" +version = "0.15.0-alpha.18" dependencies = [ "assert_cmd", "assert_fs", diff --git a/distant-ssh2/src/lib.rs b/distant-ssh2/src/lib.rs index 01972fa..5179107 100644 --- a/distant-ssh2/src/lib.rs +++ b/distant-ssh2/src/lib.rs @@ -370,7 +370,7 @@ impl Ssh2Session { let mut session = self.into_ssh_client_session().await?; // Build arguments for distant - let mut args = vec![String::from("listen"), String::from("--daemon")]; + let mut args = vec![String::from("listen")]; args.extend( shell_words::split(&opts.args) .map_err(|x| io::Error::new(io::ErrorKind::InvalidInput, x))?, diff --git a/src/opt.rs b/src/opt.rs index 93e9184..5e27487 100644 --- a/src/opt.rs +++ b/src/opt.rs @@ -474,10 +474,9 @@ pub struct LaunchSubcommand { #[structopt(long)] pub shutdown_after: Option, - /// Runs in background via daemon-mode (does nothing on windows); only applies - /// when session is socket - #[structopt(short, long)] - pub daemon: bool, + /// When session is socket, runs in foreground instead of spawning a background process + #[structopt(long)] + pub foreground: bool, /// Represents the format that results should be returned when session is "keep", /// causing the launcher to enter an interactive loop to handle input and output @@ -553,9 +552,9 @@ impl LaunchSubcommand { /// Represents subcommand to operate in listen mode for incoming requests #[derive(Clone, Debug, StructOpt)] pub struct ListenSubcommand { - /// Runs in background via daemon-mode (does nothing on windows) - #[structopt(short, long)] - pub daemon: bool, + /// Runs in foreground instead of spawning a background process + #[structopt(long)] + pub foreground: bool, /// Control the IP address that the distant binds to /// diff --git a/src/subcommand/launch.rs b/src/subcommand/launch.rs index a99c22c..76b371b 100644 --- a/src/subcommand/launch.rs +++ b/src/subcommand/launch.rs @@ -38,7 +38,7 @@ pub fn run(cmd: LaunchSubcommand, opt: CommonOpt) -> Result<(), Error> { let rt = Runtime::new()?; let session_output = cmd.session; let format = cmd.format; - let is_daemon = cmd.daemon; + let is_daemon = !cmd.foreground; let session_file = cmd.session_data.session_file.clone(); let session_socket = cmd.session_data.session_socket.clone(); @@ -190,7 +190,7 @@ async fn socket_loop( /// Returns the session associated with the server async fn spawn_remote_server(cmd: LaunchSubcommand, _opt: CommonOpt) -> Result { let distant_command = format!( - "{} listen --daemon --host {} {}", + "{} listen --host {} {}", cmd.distant, cmd.bind_server, cmd.extra_server_args.unwrap_or_default(), diff --git a/src/subcommand/listen.rs b/src/subcommand/listen.rs index 2c4234c..21c789e 100644 --- a/src/subcommand/listen.rs +++ b/src/subcommand/listen.rs @@ -32,11 +32,11 @@ impl ExitCodeError for Error { } pub fn run(cmd: ListenSubcommand, opt: CommonOpt) -> Result<(), Error> { - if cmd.daemon { - run_daemon(cmd, opt)?; - } else { + if cmd.foreground { let rt = tokio::runtime::Runtime::new()?; rt.block_on(async { run_async(cmd, opt, false).await })?; + } else { + run_daemon(cmd, opt)?; } Ok(()) @@ -44,10 +44,17 @@ pub fn run(cmd: ListenSubcommand, opt: CommonOpt) -> Result<(), Error> { #[cfg(windows)] fn run_daemon(_cmd: ListenSubcommand, _opt: CommonOpt) -> Result<(), Error> { - use std::process::{Command, Stdio}; - let mut args = std::env::args_os().filter(|arg| arg != "--daemon"); + use std::{ + ffi::OsString, + iter, + process::{Command, Stdio}, + }; + let mut args = std::env::args_os(); let program = args.next().ok_or(Error::Fork)?; + // Ensure that forked server runs in foreground, otherwise we would fork bomb ourselves + let args = args.chain(iter::once(OsString::from("--foreground"))); + let child = Command::new(program) .args(args) .stdin(Stdio::null())