From: Joseph C. Lehner Date: Sun, 31 Jan 2016 16:42:58 +0000 (+0200) Subject: Install SIGINT handler in nmrp_do X-Git-Tag: v0.9~73 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=137aa84693b3ff99d8429bdbf0528c357583d6be;p=oweals%2Fnmrpflash.git Install SIGINT handler in nmrp_do --- diff --git a/nmrp.c b/nmrp.c index 43519fc..0330c63 100644 --- a/nmrp.c +++ b/nmrp.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -244,6 +245,18 @@ static int mac_parse(const char *str, uint8_t *hwaddr) 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 = "\\|/-"; int nmrp_do(struct nmrpd_args *args) @@ -254,6 +267,7 @@ int nmrp_do(struct nmrpd_args *args) time_t beg; int i, err, ulreqs, expect; struct ethsock *sock; + sig_t sigh_orig; if (args->op != NMRP_UPLOAD_FW) { fprintf(stderr, "Operation not implemented.\n"); @@ -287,13 +301,16 @@ int nmrp_do(struct nmrpd_args *args) return 1; } + gsock = sock; + sigh_orig = signal(SIGINT, sigh); + if (ethsock_set_timeout(sock, args->rx_timeout)) { - return 1; + goto out; } src = ethsock_get_hwaddr(sock); if (!src) { - return 1; + goto out; } memcpy(tx.eh.ether_shost, src, 6); @@ -470,6 +487,8 @@ int nmrp_do(struct nmrpd_args *args) err = 0; out: + signal(SIGINT, sigh_orig); + gsock = NULL; ethsock_close(sock); return err; }