From cf518c9186adfb767d1f63bea12bbf7defff0762 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 31 Jan 2022 07:29:15 -0700 Subject: [PATCH] fun with sticky bits and RUNTIME_DIR This commit does two related things, from opposite ends of the spectrum: * Sets the sticky bit on pid files and unix sockets to avoid tmpwatch deleting them in cleanup scenarios * Falls back to looking at the changed time if the filesystem doesn't support reporting creation time when wezterm does its own liveness and cleanup checks for unix domain sockets in the runtime dir * Allow any wezterm instance to perform that cleanup refs: #1601 --- config/src/daemon.rs | 17 +++++++++++++++++ wezterm-client/src/discovery.rs | 22 +++++++++++++++++----- wezterm-gui/src/update.rs | 16 ++++++++-------- wezterm-mux-server-impl/src/local.rs | 8 ++++++-- wezterm-mux-server/src/daemonize.rs | 1 + 5 files changed, 49 insertions(+), 15 deletions(-) diff --git a/config/src/daemon.rs b/config/src/daemon.rs index b49d754ed..a24acf793 100644 --- a/config/src/daemon.rs +++ b/config/src/daemon.rs @@ -10,6 +10,23 @@ pub struct DaemonOptions { } impl_lua_conversion!(DaemonOptions); +/// Set the sticky bit on path. +/// This is used in a couple of situations where we want files that +/// we create in the RUNTIME_DIR to not be removed by a potential +/// tmpwatch daemon. +pub fn set_sticky_bit(path: &Path) { + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + if let Ok(metadata) = path.metadata() { + let mut perms = metadata.permissions(); + let mode = perms.mode(); + perms.set_mode(mode | libc::S_ISVTX); + let _ = std::fs::set_permissions(&path, perms); + } + } +} + fn open_log(path: PathBuf) -> anyhow::Result { create_user_owned_dirs( path.parent() diff --git a/wezterm-client/src/discovery.rs b/wezterm-client/src/discovery.rs index 672802274..fd38cc842 100644 --- a/wezterm-client/src/discovery.rs +++ b/wezterm-client/src/discovery.rs @@ -350,10 +350,22 @@ pub fn discover_gui_socks() -> Vec { age: Duration, } - fn meta_age(t: std::io::Result) -> Duration { - t.ok() - .and_then(|t| SystemTime::now().duration_since(t).ok()) - .unwrap_or(Duration::from_millis(300)) + /// Get an idea of the age of the entry. + /// Some filesystems don't support reporting `created`, + /// so fall back on `modified`. + fn meta_age(meta: &std::fs::Metadata) -> Duration { + let t = if let Ok(created) = meta.created() { + created + } else if let Ok(changed) = meta.modified() { + changed + } else { + return Duration::from_millis(300); + }; + if let Ok(d) = SystemTime::now().duration_since(t) { + d + } else { + Duration::from_millis(300) + } } if let Ok(dir) = std::fs::read_dir(&*config::RUNTIME_DIR) { @@ -363,7 +375,7 @@ pub fn discover_gui_socks() -> Vec { if name.starts_with("gui-sock-") { let path = entry.path(); if let Ok(meta) = entry.metadata() { - let age = meta_age(meta.created()); + let age = meta_age(&meta); if is_sock_dead(&path) && age > Duration::from_secs(1) { let _ = std::fs::remove_file(&path); } else { diff --git a/wezterm-gui/src/update.rs b/wezterm-gui/src/update.rs index ec2213945..1eac519f9 100644 --- a/wezterm-gui/src/update.rs +++ b/wezterm-gui/src/update.rs @@ -363,6 +363,14 @@ fn update_checker() { let my_sock = config::RUNTIME_DIR.join(format!("gui-sock-{}", unsafe { libc::getpid() })); loop { + // Figure out which other wezterm-guis are running. + // We have a little "consensus protocol" to decide which + // of us will show the toast notification or show the update + // window: the one of us that sorts first in the list will + // own doing that, so that if there are a dozen gui processes + // running, we don't spam the user with a lot of notifications. + let socks = wezterm_client::discovery::discover_gui_socks(); + if configuration().check_for_updates { if let Ok(latest) = get_latest_release_info() { schedule_set_banner_from_release_info(&latest); @@ -379,14 +387,6 @@ fn update_checker() { latest.tag_name ); - // Figure out which other wezterm-guis are running. - // We have a little "consensus protocol" to decide which - // of us will show the toast notification or show the update - // window: the one of us that sorts first in the list will - // own doing that, so that if there are a dozen gui processes - // running, we don't spam the user with a lot of notifications. - let socks = wezterm_client::discovery::discover_gui_socks(); - if force_ui || socks.is_empty() || socks[0] == my_sock { persistent_toast_notification_with_click_to_open_url( "WezTerm Update Available", diff --git a/wezterm-mux-server-impl/src/local.rs b/wezterm-mux-server-impl/src/local.rs index ae7641104..ba566dfc4 100644 --- a/wezterm-mux-server-impl/src/local.rs +++ b/wezterm-mux-server-impl/src/local.rs @@ -85,6 +85,10 @@ fn safely_create_sock_path(unix_dom: &UnixDomain) -> anyhow::Result anyhow::Result .write(true) .open(&pid_file) .with_context(|| format!("opening pid file {}", pid_file.display()))?; + config::set_sticky_bit(&pid_file); let res = unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) }; if res != 0 { let err = std::io::Error::last_os_error();