[B.A.T.M.A.N.] [PATCH 2/4] batctl: tcpdump: Free resources on SIG(TERM|INT)

Sven Eckelmann
2017-01-22T12:07:47Z

The cleanup code for dump interfaces and bathosts is only called on errors
during the initialization of the dump interfaces. But it should also be
used when the program exits normally.

Signed-off-by: Sven Eckelmann <sven@narfation.org>
---
tcpdump.c | 20 +++++++++++++++++++-
1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/tcpdump.c b/tcpdump.c
index 586a2ca..92171a3 100644
--- a/tcpdump.c
+++ b/tcpdump.c
@@ -39,6 +39,7 @@
#include <netinet/icmp6.h>
#include <netinet/if_ether.h>
#include <net/ethernet.h>
+#include <signal.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/select.h>
@@ -1189,6 +1190,20 @@ free_dumpif:
return NULL;
}

+static volatile sig_atomic_t is_aborted = 0;
+
+static void sig_handler(int sig)
+{
+ switch (sig) {
+ case SIGINT:
+ case SIGTERM:
+ is_aborted = 1;
+ break;
+ default:
+ break;
+ }
+}
+
int tcpdump(int argc, char **argv)
{
struct timeval tv;
@@ -1242,6 +1257,9 @@ int tcpdump(int argc, char **argv)

bat_hosts_init(read_opt);

+ signal(SIGINT, sig_handler);
+ signal(SIGTERM, sig_handler);
+
/* init interfaces list */
INIT_LIST_HEAD(&dump_if_list);
FD_ZERO(&wait_sockets);
@@ -1259,7 +1277,7 @@ int tcpdump(int argc, char **argv)
found_args++;
}

- while (1) {
+ while (!is_aborted) {

memcpy(&tmp_wait_sockets, &wait_sockets, sizeof(fd_set));

--
2.11.0