diff options
-rw-r--r-- | lib/lib.c | 16 | ||||
-rw-r--r-- | lib/xwrap.c | 11 | ||||
-rw-r--r-- | toys/net/ping.c | 5 |
3 files changed, 20 insertions, 12 deletions
@@ -850,14 +850,20 @@ void exit_signal(int sig) // adds the handlers to a list, to be called in order. void sigatexit(void *handler) { - struct arg_list *al = xmalloc(sizeof(struct arg_list)); + struct arg_list *al; int i; for (i=0; signames[i].num != SIGCHLD; i++) - signal(signames[i].num, exit_signal); - al->next = toys.xexit; - al->arg = handler; - toys.xexit = al; + signal(signames[i].num, handler ? exit_signal : SIG_DFL); + if (handler) { + al = xmalloc(sizeof(struct arg_list)); + al->next = toys.xexit; + al->arg = handler; + toys.xexit = al; + } else { + llist_traverse(toys.xexit, free); + toys.xexit = 0; + } } // Convert name to signal number. If name == NULL print names. diff --git a/lib/xwrap.c b/lib/xwrap.c index 26383c92..d5ec9f64 100644 --- a/lib/xwrap.c +++ b/lib/xwrap.c @@ -46,12 +46,13 @@ void xexit(void) { // Call toys.xexit functions in reverse order added. while (toys.xexit) { - // This is typecasting xexit->arg to a function pointer,then calling it. - // Using the invalid signal number 0 lets the signal handlers distinguish - // an actual signal from a regular exit. - ((void (*)(int))(toys.xexit->arg))(0); + struct arg_list *al = llist_pop(&toys.xexit); - free(llist_pop(&toys.xexit)); + // typecast xexit->arg to a function pointer, then call it using invalid + // signal 0 to let signal handlers tell actual signal from regular exit. + ((void (*)(int))(al->arg))(0); + + free(al); } if (fflush(NULL) || ferror(stdout)) if (!toys.exitval) perror_msg("write"); diff --git a/toys/net/ping.c b/toys/net/ping.c index 86384794..36933939 100644 --- a/toys/net/ping.c +++ b/toys/net/ping.c @@ -76,9 +76,9 @@ static void summary(int sig) if (!(toys.optflags&FLAG_q) && TT.sent && TT.sa) { printf("\n--- %s ping statistics ---\n", ntop(TT.sa)); printf("%lu packets transmitted, %lu received, %ld%% packet loss\n", - TT.sent, TT.recv, ((TT.sent-TT.recv)*100)/TT.sent); + TT.sent, TT.recv, ((TT.sent-TT.recv)*100)/(TT.sent?TT.sent:1)); printf("round-trip min/avg/max = %lu/%lu/%lu ms\n", - TT.min, TT.max, TT.fugit/TT.recv); + TT.min, TT.max, TT.fugit/(TT.recv?TT.recv:1)); } TT.sa = 0; } @@ -278,6 +278,7 @@ void ping_main(void) toys.exitval = 0; } + sigatexit(0); summary(0); if (CFG_TOYBOX_FREE) { |