From 64299960a521b58c79f4131f5fc9a4e3198db220 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Thu, 22 May 2025 15:51:55 +0200 Subject: [PATCH] kill orphaned pm3 on startup --- src/pm3.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/pm3.rs b/src/pm3.rs index a1a8b4f..fafa4bd 100644 --- a/src/pm3.rs +++ b/src/pm3.rs @@ -1,4 +1,4 @@ -use log::{info, trace}; +use log::{debug, info, trace, warn}; use std::env; use std::error::Error; use std::process::Stdio; @@ -10,6 +10,8 @@ use tokio::sync::broadcast; /// The pm3 binary is ether set in the env var PM3_BIN or found in the path /// The ouput is parsed and send via the `tx` channel pub async fn run_pm3(tx: broadcast::Sender) -> Result<(), Box> { + kill_orphans().await; + let pm3_path = match env::var("PM3_BIN") { Ok(path) => path, Err(_) => { @@ -47,3 +49,23 @@ pub async fn run_pm3(tx: broadcast::Sender) -> Result<(), Box Err("PM3 exited with a non zero exit code".into()) } } + +/// Kills any open pm3 instances +/// Also funny name. hehehe. +async fn kill_orphans() { + let kill_result = Command::new("pkill") + .arg("-KILL") + .arg("-x") + .arg("proxmark3") + .output() + .await; + + match kill_result { + Ok(_) => { + debug!("Successfully killed orphaned pm3 instances"); + } + Err(e) => { + warn!("Failed to kill pm3 orphans: {e} Continuing anyway"); + } + } +}