public inbox for b.a.t.m.a.n@lists.open-mesh.org
 help / color / mirror / Atom feed
From: Sven Eckelmann <sven@narfation.org>
To: b.a.t.m.a.n@lists.open-mesh.org
Subject: [B.A.T.M.A.N.] [PATCH 2/4] batctl: tcpdump: Free resources on SIG(TERM|INT)
Date: Sun, 22 Jan 2017 13:07:47 +0100	[thread overview]
Message-ID: <20170122120749.27932-2-sven@narfation.org> (raw)
In-Reply-To: <2527763.cX7mnL5d4j@sven-edge>

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


  parent reply	other threads:[~2017-01-22 12:07 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-22 12:07 [B.A.T.M.A.N.] [PATCH 0/4] batctl: Minor cleanups related to tcpdump Sven Eckelmann
2017-01-22 12:07 ` [B.A.T.M.A.N.] [PATCH 1/4] batctl: tcpdump: Reset socket state on initialization error Sven Eckelmann
2017-01-22 12:07 ` Sven Eckelmann [this message]
2017-01-22 12:07 ` [B.A.T.M.A.N.] [PATCH 3/4] batctl: ping: Use sig_atomic_t variable to stop ping Sven Eckelmann
2017-01-22 12:07 ` [B.A.T.M.A.N.] [PATCH 4/4] batctl: Simplify standard error messages with perror Sven Eckelmann
2017-01-24 12:41 ` [B.A.T.M.A.N.] [PATCH 0/4] batctl: Minor cleanups related to tcpdump Simon Wunderlich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170122120749.27932-2-sven@narfation.org \
    --to=sven@narfation.org \
    --cc=b.a.t.m.a.n@lists.open-mesh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox