From c44140f23054a2b38bbf3c0117f5cd1c8fa97977 Mon Sep 17 00:00:00 2001 From: Jeff Smith Date: Wed, 22 Mar 2017 09:31:25 -0500 Subject: [PATCH] peekfd: free and detach on termination with Ctrl-C Noticed when running tool in valgrind --- src/peekfd.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/peekfd.c b/src/peekfd.c index cba2130..d2e637e 100644 --- a/src/peekfd.c +++ b/src/peekfd.c @@ -87,11 +87,16 @@ #define MAX_ATTACHED_PIDS 1024 int num_attached_pids = 0; pid_t attached_pids[MAX_ATTACHED_PIDS]; +int *fds = NULL; -void detach(void) { +void detach(int signum) { int i; for (i = 0; i < num_attached_pids; i++) ptrace(PTRACE_DETACH, attached_pids[i], 0, 0); + if (fds) + free(fds); + signal(SIGINT, SIG_DFL); + raise(SIGINT); } void attach(pid_t pid) { @@ -147,7 +152,6 @@ int main(int argc, char **argv) int optc; int target_pid = 0; int numfds = 0; - int *fds = NULL; int i; struct option options[] = { @@ -212,7 +216,7 @@ int main(int argc, char **argv) if (num_attached_pids == 0) return 1; - atexit(detach); + signal(SIGINT, detach); ptrace(PTRACE_SYSCALL, attached_pids[0], 0, 0); -- 2.40.0