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
This commit is contained in:
Wez Furlong committed 2022-01-31 07:29:15 -07:00
1 parent a8aafaebf8
commit cf518c9186
5 files changed
+49 -15

No files matched your search

+17
View File
@@ -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<File> {
create_user_owned_dirs(
path.parent()
+17 -5
View File
@@ -350,10 +350,22 @@ pub fn discover_gui_socks() -> Vec<PathBuf> {
age: Duration,
}
fn meta_age(t: std::io::Result<SystemTime>) -> 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<PathBuf> {
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 {
+8 -8
View File
@@ -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",
+6 -2
View File
@@ -85,6 +85,10 @@ fn safely_create_sock_path(unix_dom: &UnixDomain) -> anyhow::Result<UnixListener
},
}
UnixListener::bind(sock_path)
.with_context(|| format!("Failed to bind to {}", sock_path.display()))
let listener = UnixListener::bind(sock_path)
.with_context(|| format!("Failed to bind to {}", sock_path.display()))?;
config::set_sticky_bit(&sock_path);
Ok(listener)
}
+1
View File
@@ -51,6 +51,7 @@ fn lock_pid_file(config: &config::ConfigHandle) -> anyhow::Result<std::fs::File>
.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();