1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-23 15:04:36 +03:00

allow spawning the server automatically when connecting via unix domain

This commit is contained in:
Wez Furlong 2019-06-22 13:22:28 -07:00
parent 7751a85826
commit be345853cd
3 changed files with 43 additions and 3 deletions

View File

@ -407,6 +407,11 @@ pub struct UnixDomain {
#[serde(default)]
pub connect_automatically: bool,
/// If true, do not attempt to start this server if we try and fail to
/// connect to it.
#[serde(default)]
pub no_serve_automatically: bool,
/// If true, bypass checking for secure ownership of the
/// socket_path. This is not recommended on a multi-user
/// system, but is useful for example when running the

View File

@ -2,7 +2,6 @@
#![windows_subsystem = "windows"]
use failure::{Error, Fallible};
use log::error;
use std::ffi::OsString;
use structopt::StructOpt;
use tabout::{tabulate_output, Alignment, Column};
@ -243,7 +242,7 @@ fn main() -> Result<(), Error> {
.unwrap_or_else(|| SubCommand::Start(StartCommand::default()))
{
SubCommand::Start(start) => {
error!("Using configuration: {:#?}\nopts: {:#?}", config, opts);
log::info!("Using configuration: {:#?}\nopts: {:#?}", config, opts);
run_terminal_gui(config, &start)
}
SubCommand::Cli(cli) => {

View File

@ -163,6 +163,22 @@ fn client_thread(
}
}
fn unix_connect_with_retry(path: &Path) -> Result<UnixStream, std::io::Error> {
let mut error = std::io::Error::last_os_error();
for iter in 0..10 {
if iter > 0 {
std::thread::sleep(std::time::Duration::from_millis(iter * 10));
}
match UnixStream::connect(path) {
Ok(stream) => return Ok(stream),
Err(err) => error = err,
}
}
Err(error)
}
impl Client {
pub fn new(local_domain_id: DomainId, stream: Box<dyn ReadAndWrite>) -> Self {
let (sender, receiver) = pollable_channel().expect("failed to create pollable_channel");
@ -198,7 +214,27 @@ impl Client {
) -> Fallible<Self> {
let sock_path = unix_dom.socket_path();
info!("connect to {}", sock_path.display());
let stream = Box::new(UnixStream::connect(sock_path)?);
let stream = match unix_connect_with_retry(&sock_path) {
Ok(stream) => stream,
Err(e) => {
if unix_dom.no_serve_automatically {
bail!("failed to connect to {}: {}", sock_path.display(), e);
}
log::error!(
"While connecting to {}: {}. Will try spawning the server.",
sock_path.display(),
e
);
let mut child = std::process::Command::new(std::env::current_exe()?)
.args(&["start", "--daemonize", "--front-end", "MuxServer"])
.spawn()?;
child.wait()?;
unix_connect_with_retry(&sock_path)?
}
};
let stream: Box<dyn ReadAndWrite> = Box::new(stream);
Ok(Self::new(local_domain_id, stream))
}