summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRalf Baechle <ralf@linux-mips.org>2010-05-30 13:54:31 +0000
committerRalf Baechle <ralf@linux-mips.org>2010-05-30 13:54:31 +0000
commite3a030cc1baf97809794406719f0cf434f9aed71 (patch)
treed1e687a92788bc144f5757b05b7ad4de97fcd334
parentdead05d568a865a6c2fe165731a27327075d8b40 (diff)
Don't abort on SIGWINCH. Cleanly terminate on SIGINT and SIGTERM.
Patch from Michael Stuermer <ms@mallorn.de> with a few touchups be me.
-rw-r--r--listen/listen.c38
1 files changed, 34 insertions, 4 deletions
diff --git a/listen/listen.c b/listen/listen.c
index 4643e9c..64a5d27 100644
--- a/listen/listen.c
+++ b/listen/listen.c
@@ -9,6 +9,8 @@
#include <string.h>
#include <time.h>
#include <curses.h>
+#include <signal.h>
+#include <errno.h>
#include <sys/socket.h>
#include <net/if.h>
@@ -24,6 +26,8 @@
#include "listen.h"
int timestamp;
+static int sigint;
+static int sock;
static void display_port(char *dev)
{
@@ -47,6 +51,12 @@ void display_timestamp(void)
timenow->tm_min, timenow->tm_sec);
}
+static void handle_sigint(int signal)
+{
+ sigint++;
+ close(sock); /* disturb blocking recvfrom */
+}
+
#define ASCII 0
#define HEX 1
#define READABLE 2
@@ -118,7 +128,7 @@ int main(int argc, char **argv)
}
}
- if ((s = socket(PF_PACKET, SOCK_PACKET, htons(proto))) == -1) {
+ if ((sock = socket(PF_PACKET, SOCK_PACKET, htons(proto))) == -1) {
perror("socket");
return 1;
}
@@ -131,13 +141,29 @@ int main(int argc, char **argv)
setservent(1);
- for (;;) {
+ signal(SIGINT, handle_sigint);
+ signal(SIGTERM, handle_sigint);
+
+ while (!sigint) {
asize = sizeof(sa);
if ((size =
- recvfrom(s, buffer, sizeof(buffer), 0, &sa,
+ recvfrom(sock, buffer, sizeof(buffer), 0, &sa,
&asize)) == -1) {
+ /*
+ * Signals are cared for by the handler, and we
+ * don't want to abort on SIGWINCH
+ */
+ if (errno == EINTR) {
+ refresh();
+ continue;
+ } else if (errno == EBADF && sigint)
+ break;
+
+ if (color)
+ endwin();
perror("recv");
+
return 1;
}
@@ -146,7 +172,7 @@ int main(int argc, char **argv)
if (proto == ETH_P_ALL) {
strcpy(ifr.ifr_name, sa.sa_data);
- if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0)
+ if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0)
perror("GIFADDR");
if (ifr.ifr_hwaddr.sa_family == AF_AX25) {
@@ -170,6 +196,10 @@ int main(int argc, char **argv)
if (color)
refresh();
}
+ if (color)
+ endwin();
+
+ return EXIT_SUCCESS;
}
static void ascii_dump(unsigned char *data, int length)