From: Aaron LI Date: Fri, 22 Jun 2018 13:23:25 +0000 (+0800) Subject: ifconfig(8): Use atexit() to ensure printing interface name X-Git-Tag: v5.5.0~314 X-Git-Url: https://gitweb.dragonflybsd.org/dragonfly.git/commitdiff_plain/e9e1626f77881d1971173b83792c178b084ff759 ifconfig(8): Use atexit() to ensure printing interface name The ifconfig program may not exit at the end of its main() function, so use atexit(printifnamemaybe) to ensure the interface name (e.g., newly cloned but with wildcard name) is printed on exit. (Taken from FreeBSD) Use "return (0)" instead of "exit(0)" in the main() function. Minor whitespace updates. --- diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c index 8ff2da92cc..4d2562d44b 100644 --- a/sbin/ifconfig/ifconfig.c +++ b/sbin/ifconfig/ifconfig.c @@ -93,6 +93,7 @@ static void usage(void) __dead2; static struct afswtch *af_getbyname(const char *name); static struct afswtch *af_getbyfamily(int af); static void af_other_status(int); +static void printifnamemaybe(void); static struct option *opts = NULL; @@ -127,6 +128,12 @@ usage(void) exit(1); } +static void printifnamemaybe(void) +{ + if (printifname) + printf("%s\n", name); +} + int main(int argc, char *argv[]) { @@ -143,10 +150,16 @@ main(int argc, char *argv[]) char options[1024]; const char *ifname; struct option *p; - size_t iflen; + size_t iflen; all = downonly = uponly = namesonly = verbose = 0; + /* + * Ensure we print interface name when expected to, + * even if we terminate early due to error. + */ + atexit(printifnamemaybe); + /* Parse leading line options */ strlcpy(options, "adklmuv", sizeof(options)); for (p = opts; p != NULL; p = p->next) @@ -240,7 +253,7 @@ main(int argc, char *argv[]) errx(1, "%s: cloning name too long", ifname); ifconfig(argc, argv, 1, NULL); - exit(0); + return (0); } errx(1, "interface %s does not exist", ifname); } @@ -303,7 +316,7 @@ retry: fprintf(stderr, "msglen = %d\n", ifm->ifm_msglen); fprintf(stderr, "buf:%p, next:%p, lim:%p\n", buf, next, lim); - exit (1); + exit(1); } next += ifm->ifm_msglen; @@ -358,10 +371,8 @@ retry: if (namesonly && need_nl > 0) putchar('\n'); - if (printifname) - printf("%s\n", name); - exit (0); + return (0); } static struct afswtch *afs = NULL; @@ -486,7 +497,8 @@ static const struct cmd setifdstaddr_cmd = DEF_CMD("ifdstaddr", 0, setifdstaddr); static int -ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp) +ifconfig(int argc, char *const *argv, int iscreate, + const struct afswtch *uafp) { const struct afswtch *afp, *nafp; struct callback *cb; @@ -623,7 +635,7 @@ top: } close(s); - return(0); + return (0); } /*ARGSUSED*/