]> granicus.if.org Git - psmisc/commitdiff
peekfd: free and detach on termination with Ctrl-C
authorJeff Smith <whydoubt@gmail.com>
Wed, 22 Mar 2017 14:31:25 +0000 (09:31 -0500)
committerJeff Smith <whydoubt@gmail.com>
Thu, 23 Mar 2017 16:49:07 +0000 (11:49 -0500)
Noticed when running tool in valgrind

src/peekfd.c

index cba21309f07d1eb60059f3450f20dab2631a73d2..d2e637e8b6a15f2ee07450f47d93b1cd5c158b2b 100644 (file)
 #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);