Skip to content

Commit

Permalink
feat: check process name before kill process by pid
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Oct 7, 2024
1 parent 640ae7a commit 911e6c5
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 59 deletions.
93 changes: 42 additions & 51 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions nyanpasu-utils/src/core/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ impl CoreType {
// CoreType::SingBox,
]
}

pub fn get_supported_cores_executables() -> Vec<&'static str> {
Self::get_supported_cores()
.iter()
.map(|core| core.get_executable_name())
.collect()
}
}

impl AsRef<str> for CoreType {
Expand Down
6 changes: 5 additions & 1 deletion nyanpasu-utils/src/core/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ impl CoreInstance {
#[instrument(skip(self))]
async fn kill_instance_by_pid_file(&self) -> Result<(), std::io::Error> {
tracing::debug!("kill instance by pid file: {:?}", self.pid_path);
crate::os::kill_by_pid_file(&self.pid_path).await
crate::os::kill_by_pid_file(
&self.pid_path,
Some(&CoreType::get_supported_cores_executables()),
)
.await
}

#[instrument(skip(self))]
Expand Down
31 changes: 24 additions & 7 deletions nyanpasu-utils/src/os/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
mod os_impl;

pub use os_impl::*;
use std::fmt::Debug;
use sysinfo::{Pid, ProcessRefreshKind, RefreshKind, System};
use tracing_attributes::instrument;

use std::{fmt::Display, io::Error as IoError, path::Path};
use std::{ffi::OsStr, fmt::Display, io::Error as IoError, path::Path};
use tokio::{fs::OpenOptions, io::AsyncWriteExt};

pub use os_utils::*;
Expand Down Expand Up @@ -40,17 +41,30 @@ where
}

#[instrument]
pub fn pid_exists(pid: u32) -> bool {
pub fn pid_exists<Name: AsRef<str> + Debug>(pid: u32, validator: Option<&[Name]>) -> bool {
let kind = RefreshKind::new().with_processes(ProcessRefreshKind::new());
let mut system = System::new_with_specifics(kind);
system.refresh_specifics(kind);
system.process(Pid::from_u32(pid)).is_some()
system
.process(Pid::from_u32(pid))
.is_some_and(|p| match validator {
Some(validator) => validator
.iter()
.map(|v| v.as_ref())
// FIXME: this validator is designed for core name, it use the ascii name of the process. So it always correct.
// If in future, our tool introduce the non-ascii name, we need to change this validator, use the encoding-rs crate to decode the name.
.any(|v| p.name().to_string_lossy().contains(v)),
None => true,
})
}

#[instrument]
pub async fn kill_pid(pid: u32) -> Result<(), std::io::Error> {
pub async fn kill_pid<Name: AsRef<str> + Debug>(
pid: u32,
validator: Option<&[Name]>,
) -> Result<(), std::io::Error> {
tracing::debug!("kill pid: {}", pid);
if pid_exists(pid) {
if pid_exists(pid, validator) {
tracing::debug!("pid exists, kill it");
let list = kill_tree::tokio::kill_tree(pid)
.await
Expand All @@ -65,7 +79,10 @@ pub async fn kill_pid(pid: u32) -> Result<(), std::io::Error> {
}

#[instrument]
pub async fn kill_by_pid_file<T>(path: T) -> Result<(), std::io::Error>
pub async fn kill_by_pid_file<T, Name: AsRef<str> + Debug>(
path: T,
validator: Option<&[Name]>,
) -> Result<(), std::io::Error>
where
T: AsRef<Path> + std::fmt::Debug,
{
Expand All @@ -76,6 +93,6 @@ where
return Ok(());
}
};
kill_pid(pid).await?;
kill_pid(pid, validator).await?;
tokio::fs::remove_file(path).await
}

0 comments on commit 911e6c5

Please sign in to comment.