From e3a030cc1baf97809794406719f0cf434f9aed71 Mon Sep 17 00:00:00 2001 From: Ralf Baechle Date: Sun, 30 May 2010 13:54:31 +0000 Subject: Don't abort on SIGWINCH. Cleanly terminate on SIGINT and SIGTERM. Patch from Michael Stuermer with a few touchups be me. --- listen/listen.c | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) (limited to 'listen/listen.c') 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 #include #include +#include +#include #include #include @@ -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) -- cgit v1.2.3