Skip to content

Commit

Permalink
Merge pull request #122 from kunai-project/fix-run-check
Browse files Browse the repository at this point in the history
fix(main): check if another kunai instance is running
  • Loading branch information
qjerome authored Oct 14, 2024
2 parents 4130bd6 + 6bdbf5d commit 30812f9
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions kunai/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use kunai_common::{inspect_err, kernel};

use kunai_macros::StrEnum;
use log::LevelFilter;
use procfs::ProcError;
use serde::{Deserialize, Serialize};

use tokio::sync::mpsc::error::SendError;
Expand Down Expand Up @@ -2227,6 +2228,10 @@ struct RunOpt {
#[arg(long)]
harden: bool,

/// Force Kunai to run
#[arg(long)]
force: bool,

/// Exclude events by name (comma separated).
#[arg(long)]
exclude: Option<String>,
Expand Down Expand Up @@ -2469,6 +2474,21 @@ impl Command {
));
}

let self_proc: PathBuf = PathBuf::from("/proc/self/exe").canonicalize()?;
let force = opt_ro.as_ref().map(|o| o.force).unwrap_or_default();

// we check in procfs if we find another instance of kunai running
if !force
&& procfs::process::all_processes()?
.flatten()
.flat_map(|p| Ok::<_, ProcError>((p.pid, p.exe()?)))
.flat_map(|(pid, exe)| Ok::<_, io::Error>((pid, exe.canonicalize()?)))
.any(|(pid, exe)| pid as u32 != process::id() && exe == self_proc)
{
warn!("An instance of Kunai is already running");
return Ok(());
}

let current_kernel = Utsname::kernel_version()?;
let conf: Config = match opt_ro {
Some(ro) => ro.try_into()?,
Expand Down

0 comments on commit 30812f9

Please sign in to comment.