From: Jeff Smith Date: Wed, 22 Mar 2017 14:31:25 +0000 (-0500) Subject: peekfd: free and detach on termination with Ctrl-C X-Git-Tag: v23.0~15^2^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c44140f23054a2b38bbf3c0117f5cd1c8fa97977;p=psmisc peekfd: free and detach on termination with Ctrl-C Noticed when running tool in valgrind --- 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);