hmgle / graftcp

A flexible tool for redirecting a given program's TCP traffic to SOCKS5 or HTTP proxy.
GNU General Public License v3.0
2.07k stars 174 forks source link

graftcp absorbs child signal and exits cleanly #44

Closed guihkx closed 2 years ago

guihkx commented 2 years ago

When a program running through graftcp receives SIGKILL/SIGABRT, graftcp still exits cleanly, when in my opinion it should forward the signal of the child to itself (not sure if this would break some functionality, though).

For example, leave ping running on a terminal window:

$ ping www.google.com

Now on a second terminal window, kill ping:

$ kill -KILL $(pidof ping)

Go back to the first window, and if you use bash, right at the end it should say Killed, which is expected.

However, that is not what happens when you kill ping running through graftcp.

Thanks.

guihkx commented 2 years ago

I forgot to mention that the reason I want this feature, is because I have a systemd service in which I use graftcp to run a buggy proprietary program that keeps getting aborted because it leaks too many file descriptors. So, when this program gets aborted, graftcp absorbs the SIGABRT signal and exits cleanly, which makes the very useful Restart=on-failure option of systemd, useless.

Anyway, I'm not an expert on the subject, but I've been using the following patch for an hour and everything seems to be working like used to, so I'm gonna close this (but feel free to re-open if necessary):

diff --git a/graftcp.c b/graftcp.c
index 820d34f..5751952 100644
--- a/graftcp.c
+++ b/graftcp.c
@@ -323,8 +323,10 @@ int do_trace()
            sig = 0;
            goto end;
        }
-       if (WIFSIGNALED(status) || WIFEXITED(status)
-           || !WIFSTOPPED(status)) {
+       if (WIFSIGNALED(status)) {
+           raise(WTERMSIG(status));
+       }
+       if (WIFEXITED(status) || !WIFSTOPPED(status)) {
            exit_code = WEXITSTATUS(status);
            /* TODO free pinfp */
            continue;

The above is for master, but since I'm still using v0.4.0:

diff --git a/graftcp.c b/graftcp.c
index ab0acd5..78cf62e 100644
--- a/graftcp.c
+++ b/graftcp.c
@@ -314,8 +314,10 @@ int do_trace()
            sig = 0;
            goto end;
        }
-       if (WIFSIGNALED(status) || WIFEXITED(status)
-           || !WIFSTOPPED(status)) {
+       if (WIFSIGNALED(status)) {
+           raise(WTERMSIG(status));
+       }
+       if (WIFEXITED(status) || !WIFSTOPPED(status)) {
            /* TODO free pinfp */
            continue;
        }