Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

look at /proc to see if daemon is really running #24

Merged
merged 1 commit into from
Aug 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 63 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::Parser;
use std::{os::unix::net::UnixStream, time::Duration};
use std::{os::unix::net::UnixStream, path::PathBuf, time::Duration};

mod cli;
mod communication;
Expand All @@ -10,13 +10,37 @@ use communication::Answer;
fn main() -> Result<(), String> {
let mut swww = Swww::parse();
if let Swww::Init { no_daemon } = &swww {
if connect_to_socket(1, 0).is_err() {
spawn_daemon(*no_daemon)?;
if *no_daemon {
return Ok(());
match is_daemon_running() {
Ok(false) => {
let socket_path = communication::get_socket_path();
if socket_path.exists() {
eprintln!(
"WARNING: socket file {} was not deleted when the previous daemon exited",
socket_path.to_string_lossy()
);
if let Err(e) = std::fs::remove_file(socket_path) {
return Err(format!("failed to delete previous socket: {}", e));
}
}
}
Ok(true) => {
return Err("There seems to already be another instance running...".to_string())
}
Err(e) => {
eprintln!("WARNING: failed to read '/proc' directory to determine whether the daemon is running: {}
Falling back to trying to checking if the socket file exists...", e);
let socket_path = communication::get_socket_path();
if socket_path.exists() {
return Err(format!(
"Found socket at {}. There seems to be an instance already running...",
socket_path.to_string_lossy()
));
}
}
} else {
return Err("There seems to already be another instance running...".to_string());
}
spawn_daemon(*no_daemon)?;
if *no_daemon {
return Ok(());
}
}

Expand Down Expand Up @@ -94,3 +118,35 @@ fn connect_to_socket(tries: u8, interval: u64) -> Result<UnixStream, String> {

Err(format!("Failed to connect to socket: {}", error))
}

fn is_daemon_running() -> Result<bool, String> {
let proc = PathBuf::from("/proc");

let entries = match proc.read_dir() {
Ok(e) => e,
Err(e) => return Err(e.to_string()),
};

for entry in entries.flatten() {
let dirname = entry.file_name();
if let Ok(pid) = dirname.to_string_lossy().parse::<u32>() {
if std::process::id() == pid {
continue;
}
let mut entry_path = entry.path();
entry_path.push("cmdline");
if let Ok(cmd) = std::fs::read_to_string(entry_path) {
let mut args = cmd.split(&[' ', '\0']);
if let Some(arg0) = args.next() {
if arg0.ends_with("swww") {
if let Some("init") = args.next() {
return Ok(true);
}
}
}
}
}
}

Ok(false)
}