Skip to content

Commit b6ab191

Browse files
committed
catch sigint so tracer maps get fully cleaned
1 parent 4f0c644 commit b6ab191

1 file changed

Lines changed: 30 additions & 7 deletions

File tree

src/main.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clap::Parser;
33
use libbpf_rs::{MapCore, MapFlags, RingBufferBuilder};
44
use libc::{
55
c_int, kill, sigaction, sigaddset, sigemptyset, sighandler_t, sigprocmask, sigset_t, sigwait,
6-
waitpid, SA_NOCLDSTOP, SA_RESTART, SIGCHLD, SIGUSR1, SIG_BLOCK, SIG_UNBLOCK,
6+
waitpid, SA_NOCLDSTOP, SA_RESTART, SIGCHLD, SIGINT, SIGUSR1, SIG_BLOCK, SIG_UNBLOCK,
77
};
88
use nix::unistd::{setgroups, setresgid, setresuid, Gid, Uid};
99
use std::ffi::CStr;
@@ -22,7 +22,7 @@ mod dep_tracer;
2222
mod installer;
2323
mod utils;
2424

25-
use crate::cli::{Cli, Commands, Outputs, StreamOutputs, OutputMode};
25+
use crate::cli::{Cli, Commands, OutputMode, Outputs, StreamOutputs};
2626
use crate::dep_tracer::event_stream_handler;
2727
use crate::dep_tracer::SyscallEvent;
2828
use crate::dep_tracer::{CTXT, LOGS, SETS};
@@ -38,6 +38,10 @@ extern "C" fn sigchld_handler(_sig: i32) {
3838
RUNNING.store(false, Ordering::Relaxed);
3939
}
4040

41+
extern "C" fn sigint_handler(_sig: i32) {
42+
RUNNING.store(false, Ordering::Relaxed);
43+
}
44+
4145
fn monitor_pid(pid: i32) -> std::io::Result<RawFd> {
4246
unsafe {
4347
let fd = libc::syscall(libc::SYS_pidfd_open, pid, 0);
@@ -119,15 +123,23 @@ fn main() -> Result<()> {
119123
} else {
120124
attach_to_existing_proc = false;
121125
}
122-
126+
123127
let mut outputs = Outputs::from_cli(&cli)?;
124-
let stream_cfg= if matches!(cli.mode, OutputMode::Stream | OutputMode::Both) {
128+
let stream_cfg = if matches!(cli.mode, OutputMode::Stream | OutputMode::Both) {
125129
let so = StreamOutputs::from_cli(&cli)?;
126130
Some(dep_tracer::StreamCfg {
127131
read_out: if cli.stream_read { Some(so.read) } else { None },
128-
write_out: if cli.stream_write { Some(so.write) } else { None },
129-
trace_out: if cli.stream_trace { Some(so.deps) } else { None },
130-
})
132+
write_out: if cli.stream_write {
133+
Some(so.write)
134+
} else {
135+
None
136+
},
137+
trace_out: if cli.stream_trace {
138+
Some(so.deps)
139+
} else {
140+
None
141+
},
142+
})
131143
} else {
132144
None
133145
};
@@ -144,6 +156,17 @@ fn main() -> Result<()> {
144156
"couldn't register sigchld handler",
145157
))?;
146158
}
159+
160+
sa.sa_sigaction = sigint_handler as *const () as sighandler_t;
161+
sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
162+
sigemptyset(&mut sa.sa_mask);
163+
164+
if sigaction(SIGINT, &sa, ptr::null_mut()) == -1 {
165+
Err(Error::new(
166+
ErrorKind::Other,
167+
"couldn't register sigint handler",
168+
))?
169+
}
147170
}
148171

149172
// either trace the pid or fork the child

0 commit comments

Comments
 (0)