Skip to content

Commit

Permalink
fix partial process get error
Browse files Browse the repository at this point in the history
Signed-off-by: Yang Keao <keao.yang@yahoo.com>
  • Loading branch information
YangKeao committed Jan 15, 2021
1 parent 6964800 commit f969542
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions src/ptrace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use nix::sys::wait;
use nix::unistd::Pid;

use log::{info, trace, warn};
use procfs::ProcError;

use std::cell::RefCell;
use std::collections::HashMap;
Expand Down Expand Up @@ -78,26 +79,35 @@ impl PtraceManager {
match counter_ref.get_mut(&pid) {
Some(count) => {
*count -= 1;
info!("decrease counter to {}", *count);
trace!("decrease counter to {}", *count);
if *count < 1 {
counter_ref.remove(&pid);

info!("detach process: {}", pid);
let process = procfs::process::Process::new(pid)?;
for task in process.tasks()? {
if let Ok(task) = task {
info!("detach task: {}", task.tid);
match ptrace::detach(Pid::from_raw(task.tid), None) {
Ok(()) => {}
Err(nix::Error::Sys(nix::errno::Errno::ESRCH)) => {
// ignore because the task could be newly created
match procfs::process::Process::new(pid) {
Ok(process) => {
for task in process.tasks()? {
if let Ok(task) = task {
info!("detach task: {}", task.tid);
match ptrace::detach(Pid::from_raw(task.tid), None) {
Ok(()) => {}
Err(nix::Error::Sys(nix::errno::Errno::ESRCH)) => {
// ignore because the task could be newly created
}
Err(err) => return Err(err.into()),
}
info!("detach task: {} successfully", task.tid);
}
Err(err) => return Err(err.into()),
}
info!("detach task: {} successfully", task.tid);
info!("detach process: {} successfully", pid);
}
Err(ProcError::NotFound(_)) => {
info!("process {} not found", pid)
}
Err(err) => {
return Err(err.into())
}
}
info!("detach process: {} successfully", pid);
}

Ok(())
Expand Down Expand Up @@ -303,7 +313,7 @@ impl TracedProcess {

impl Drop for TracedProcess {
fn drop(&mut self) {
info!("dropping traced process: {}", self.pid);
trace!("dropping traced process: {}", self.pid);

if let Err(err) = PTRACE_MANAGER.with(|pm| pm.detach(self.pid)) {
info!(
Expand Down

0 comments on commit f969542

Please sign in to comment.