Install SIGINT handler in nmrp_do

This commit is contained in:
Joseph C. Lehner 2016-01-31 18:42:58 +02:00
parent d712601254
commit 137aa84693

23
nmrp.c
View file

@ -24,6 +24,7 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <net/if.h> #include <net/if.h>
#include <signal.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
@ -244,6 +245,18 @@ static int mac_parse(const char *str, uint8_t *hwaddr)
return 0; return 0;
} }
static struct ethsock *gsock = NULL;
static void sigh(int sig)
{
printf("\n");
if (gsock) {
ethsock_close(gsock);
}
exit(1);
}
static const char *spinner = "\\|/-"; static const char *spinner = "\\|/-";
int nmrp_do(struct nmrpd_args *args) int nmrp_do(struct nmrpd_args *args)
@ -254,6 +267,7 @@ int nmrp_do(struct nmrpd_args *args)
time_t beg; time_t beg;
int i, err, ulreqs, expect; int i, err, ulreqs, expect;
struct ethsock *sock; struct ethsock *sock;
sig_t sigh_orig;
if (args->op != NMRP_UPLOAD_FW) { if (args->op != NMRP_UPLOAD_FW) {
fprintf(stderr, "Operation not implemented.\n"); fprintf(stderr, "Operation not implemented.\n");
@ -287,13 +301,16 @@ int nmrp_do(struct nmrpd_args *args)
return 1; return 1;
} }
gsock = sock;
sigh_orig = signal(SIGINT, sigh);
if (ethsock_set_timeout(sock, args->rx_timeout)) { if (ethsock_set_timeout(sock, args->rx_timeout)) {
return 1; goto out;
} }
src = ethsock_get_hwaddr(sock); src = ethsock_get_hwaddr(sock);
if (!src) { if (!src) {
return 1; goto out;
} }
memcpy(tx.eh.ether_shost, src, 6); memcpy(tx.eh.ether_shost, src, 6);
@ -470,6 +487,8 @@ int nmrp_do(struct nmrpd_args *args)
err = 0; err = 0;
out: out:
signal(SIGINT, sigh_orig);
gsock = NULL;
ethsock_close(sock); ethsock_close(sock);
return err; return err;
} }