-
-
Notifications
You must be signed in to change notification settings - Fork 755
/
main.rs
225 lines (193 loc) · 6.04 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use config::configuration;
use mux::activity::Activity;
use mux::domain::{Domain, LocalDomain};
use mux::Mux;
use portable_pty::cmdbuilder::CommandBuilder;
use std::ffi::OsString;
use std::process::Command;
use std::rc::Rc;
use std::sync::Arc;
use std::thread;
use structopt::*;
use wezterm_gui_subcommands::*;
mod daemonize;
#[derive(Debug, StructOpt)]
#[structopt(
about = "Wez's Terminal Emulator\nhttp://github.com/wez/wezterm",
global_setting = structopt::clap::AppSettings::ColoredHelp,
version = config::wezterm_version()
)]
struct Opt {
/// Skip loading wezterm.lua
#[structopt(name = "skip-config", short = "n")]
skip_config: bool,
/// Specify the configuration file to use, overrides the normal
/// configuration file resolution
#[structopt(
long = "config-file",
parse(from_os_str),
conflicts_with = "skip-config"
)]
config_file: Option<OsString>,
/// Override specific configuration values
#[structopt(
long = "config",
name = "name=value",
parse(try_from_str = name_equals_value),
number_of_values = 1)]
config_override: Vec<(String, String)>,
/// Detach from the foreground and become a background process
#[structopt(long = "daemonize")]
daemonize: bool,
/// Specify the current working directory for the initially
/// spawned program
#[structopt(long = "cwd", parse(from_os_str))]
cwd: Option<OsString>,
/// Instead of executing your shell, run PROG.
/// For example: `wezterm start -- bash -l` will spawn bash
/// as if it were a login shell.
#[structopt(parse(from_os_str))]
prog: Vec<OsString>,
}
fn main() {
if let Err(err) = run() {
log::error!("{:#}", err);
std::process::exit(1);
}
}
fn run() -> anyhow::Result<()> {
env_bootstrap::bootstrap();
//stats::Stats::init()?;
config::designate_this_as_the_main_thread();
let _saver = umask::UmaskSaver::new();
let opts = Opt::from_args();
config::common_init(
opts.config_file.as_ref(),
&opts.config_override,
opts.skip_config,
);
#[cfg(unix)]
{
let config = config::configuration();
if opts.daemonize {
daemonize::daemonize(&config)?;
// When we reach this line, we are in a forked child process,
// and the fork will have broken the async-io/reactor state
// of the smol runtime.
// To resolve this, we will re-exec ourselves in the block
// below that was originally Windows-specific
}
}
if opts.daemonize {
// On Windows we can't literally daemonize, but we can spawn another copy
// of ourselves in the background!
// On Unix, forking breaks the global state maintained by `smol`,
// so we need to re-exec ourselves to start things back up properly.
let mut cmd = Command::new(std::env::current_exe().unwrap());
if opts.skip_config {
cmd.arg("-n");
}
if let Some(cwd) = opts.cwd {
cmd.arg("--cwd");
cmd.arg(cwd);
}
if !opts.prog.is_empty() {
cmd.arg("--");
for a in &opts.prog {
cmd.arg(a);
}
}
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
cmd.creation_flags(winapi::um::winbase::DETACHED_PROCESS);
let child = cmd.spawn();
drop(child);
return Ok(());
}
#[cfg(unix)]
{
use std::os::unix::process::CommandExt;
return Err(anyhow::anyhow!("failed to re-exec: {:?}", cmd.exec()));
}
}
// Remove some environment variables that aren't super helpful or
// that are potentially misleading when we're starting up the
// server.
// We may potentially want to look into starting/registering
// a session of some kind here as well in the future.
for name in &[
"OLDPWD",
"PWD",
"SHLVL",
"SSH_AUTH_SOCK",
"SSH_CLIENT",
"SSH_CONNECTION",
"_",
] {
std::env::remove_var(name);
}
let need_builder = !opts.prog.is_empty() || opts.cwd.is_some();
let cmd = if need_builder {
let mut builder = if opts.prog.is_empty() {
CommandBuilder::new_default_prog()
} else {
CommandBuilder::from_argv(opts.prog)
};
if let Some(cwd) = opts.cwd {
builder.cwd(cwd);
}
Some(builder)
} else {
None
};
let domain: Arc<dyn Domain> = Arc::new(LocalDomain::new("local")?);
let mux = Rc::new(mux::Mux::new(Some(domain.clone())));
Mux::set_mux(&mux);
let executor = promise::spawn::SimpleExecutor::new();
spawn_listener().map_err(|e| {
log::error!("problem spawning listeners: {:?}", e);
e
})?;
let activity = Activity::new();
promise::spawn::spawn(async move {
if let Err(err) = async_run(cmd).await {
terminate_with_error(err);
}
drop(activity);
})
.detach();
loop {
executor.tick()?;
}
}
async fn async_run(cmd: Option<CommandBuilder>) -> anyhow::Result<()> {
let mux = Mux::get().unwrap();
let domain = mux.default_domain();
domain.attach().await?;
let config = config::configuration();
let window_id = mux.new_empty_window();
let _tab = mux
.default_domain()
.spawn(config.initial_size(), cmd, None, *window_id)
.await?;
Ok(())
}
fn terminate_with_error(err: anyhow::Error) -> ! {
log::error!("{:#}; terminating", err);
std::process::exit(1);
}
mod ossl;
pub fn spawn_listener() -> anyhow::Result<()> {
let config = configuration();
for unix_dom in &config.unix_domains {
let mut listener = wezterm_mux_server_impl::local::LocalListener::with_domain(unix_dom)?;
thread::spawn(move || {
listener.run();
});
}
for tls_server in &config.tls_servers {
ossl::spawn_tls_listener(tls_server)?;
}
Ok(())
}