diff options
| author | Roy Marples <roy@marples.name> | 2017-04-09 08:44:16 +0100 |
|---|---|---|
| committer | Roy Marples <roy@marples.name> | 2017-04-09 08:44:16 +0100 |
| commit | a7544e344dedcd8a97a0252663de8f789404e8fa (patch) | |
| tree | fd80dab09183fc0bced47d7ab26b7fcd6ef13824 /src/ipv6.c | |
| parent | ce3ea7a52b7246b9ab1131b4030e8b843339c44e (diff) | |
| download | dhcpcd-a7544e344dedcd8a97a0252663de8f789404e8fa.tar.xz | |
logerr: errx with logging
Had a few complaints that the lastlog option was dropped.
It's now been restored, but with a new way of logging which is
the same as errx(3) as found on the BSD platforms but the output
also goes to the logging option of choice.
This allows the function to define the log priority for syslog
and there is no more need to specify %m.
In turn, this makes the code needed to write error messages
a lot shorter than before.
loginfo and logdebug have no optional x suffix because they are not
meant to record errors, unlike logerr and logwarn.
Diffstat (limited to 'src/ipv6.c')
| -rw-r--r-- | src/ipv6.c | 80 |
1 files changed, 37 insertions, 43 deletions
@@ -55,7 +55,6 @@ #include <inttypes.h> #include <stdlib.h> #include <string.h> -#include <syslog.h> #include <unistd.h> #define ELOOP_QUEUE 7 @@ -66,6 +65,7 @@ #include "eloop.h" #include "ipv6.h" #include "ipv6nd.h" +#include "logerr.h" #include "sa.h" #include "script.h" @@ -170,7 +170,7 @@ ipv6_readsecret(struct dhcpcd_ctx *ctx) return (ssize_t)ctx->secret_len; if (errno != ENOENT) - syslog(LOG_ERR, "error reading secret: %s: %m", SECRET); + logerr("%s: %s", __func__, SECRET); /* Chaining arc4random should be good enough. * RFC7217 section 5.1 states the key SHOULD be at least 128 bits. @@ -178,7 +178,7 @@ ipv6_readsecret(struct dhcpcd_ctx *ctx) * 512 bits (64 bytes). */ if (ctx->secret_len < 64) { if ((ctx->secret = malloc(64)) == NULL) { - syslog(LOG_ERR, "%s: malloc: %m", __func__); + logerr(__func__); return -1; } ctx->secret_len = 64; @@ -205,7 +205,7 @@ ipv6_readsecret(struct dhcpcd_ctx *ctx) return (ssize_t)ctx->secret_len; eexit: - syslog(LOG_ERR, "error writing secret: %s: %m", SECRET); + logerr("%s: %s", __func__, SECRET); if (fp != NULL) fclose(fp); unlink(SECRET); @@ -558,7 +558,7 @@ ipv6_checkaddrflags(void *arg) alias = NULL; #endif if ((flags = if_addrflags6(ia->iface, &ia->addr, alias)) == -1) { - syslog(LOG_ERR, "%s: if_addrflags6: %m", ia->iface->name); + logerr("%s: if_addrflags6", ia->iface->name); return; } @@ -584,11 +584,11 @@ ipv6_deleteaddr(struct ipv6_addr *ia) struct ipv6_state *state; struct ipv6_addr *ap; - syslog(LOG_INFO, "%s: deleting address %s", ia->iface->name, ia->saddr); + loginfo("%s: deleting address %s", ia->iface->name, ia->saddr); if (if_address6(RTM_DELADDR, ia) == -1 && errno != EADDRNOTAVAIL && errno != ESRCH && errno != ENXIO && errno != ENODEV) - syslog(LOG_ERR, "if_address6: %m"); + logerr(__func__); /* NOREJECT is set if we delegated exactly the prefix to another * address. @@ -614,6 +614,7 @@ ipv6_addaddr1(struct ipv6_addr *ap, const struct timespec *now) struct ipv6_state *state; struct ipv6_addr *nap; uint32_t pltime, vltime; + __printflike(1, 2) void (*logfunc)(const char *, ...); /* Ensure no other interface has this address */ TAILQ_FOREACH(ifp, ap->iface->ctx->ifaces, next) { @@ -634,8 +635,8 @@ ipv6_addaddr1(struct ipv6_addr *ap, const struct timespec *now) ipv6_iffindaddr(ap->iface, &ap->addr, IN6_IFF_NOTUSEABLE)) ap->flags |= IPV6_AF_DADCOMPLETED; - syslog(ap->flags & IPV6_AF_NEW ? LOG_INFO : LOG_DEBUG, - "%s: adding %saddress %s", ap->iface->name, + logfunc = ap->flags & IPV6_AF_NEW ? loginfo : logdebug; + logfunc("%s: adding %saddress %s", ap->iface->name, #ifdef IPV6_AF_TEMPORARY ap->flags & IPV6_AF_TEMPORARY ? "temporary " : "", #else @@ -644,19 +645,17 @@ ipv6_addaddr1(struct ipv6_addr *ap, const struct timespec *now) ap->saddr); if (ap->prefix_pltime == ND6_INFINITE_LIFETIME && ap->prefix_vltime == ND6_INFINITE_LIFETIME) - syslog(LOG_DEBUG, "%s: pltime infinity, vltime infinity", + logdebug("%s: pltime infinity, vltime infinity", ap->iface->name); else if (ap->prefix_pltime == ND6_INFINITE_LIFETIME) - syslog(LOG_DEBUG, - "%s: pltime infinity, vltime %"PRIu32" seconds", + logdebug("%s: pltime infinity, vltime %"PRIu32" seconds", ap->iface->name, ap->prefix_vltime); else if (ap->prefix_vltime == ND6_INFINITE_LIFETIME) - syslog(LOG_DEBUG, - "%s: pltime %"PRIu32"seconds, vltime infinity", + logdebug("%s: pltime %"PRIu32"seconds, vltime infinity", ap->iface->name, ap->prefix_pltime); else - syslog(LOG_DEBUG, - "%s: pltime %"PRIu32" seconds, vltime %"PRIu32" seconds", + logdebug("%s: pltime %"PRIu32" seconds, vltime %"PRIu32 + " seconds", ap->iface->name, ap->prefix_pltime, ap->prefix_vltime); /* Adjust plftime and vltime based on acquired time */ @@ -684,21 +683,19 @@ ipv6_addaddr1(struct ipv6_addr *ap, const struct timespec *now) ap->prefix_vltime -= (uint32_t)n.tv_sec; #if 0 - syslog(LOG_DEBUG, - "%s: acquired %lld.%.9ld, now %lld.%.9ld, diff %lld.%.9ld", + logdebug("%s: acquired %lld.%.9ld, now %lld.%.9ld, diff %lld.%.9ld", ap->iface->name, (long long)ap->acquired.tv_sec, ap->acquired.tv_nsec, (long long)now->tv_sec, now->tv_nsec, (long long)n.tv_sec, n.tv_nsec); - syslog(LOG_DEBUG, - "%s: adj pltime %"PRIu32" seconds, " + logdebug("%s: adj pltime %"PRIu32" seconds, " "vltime %"PRIu32" seconds", ap->iface->name, ap->prefix_pltime, ap->prefix_vltime); #endif } if (if_address6(RTM_NEWADDR, ap) == -1) { - syslog(LOG_ERR, "if_addaddress6: %m"); + logerr(__func__); /* Restore real pltime and vltime */ ap->prefix_pltime = pltime; ap->prefix_vltime = vltime; @@ -750,7 +747,7 @@ ipv6_addaddr1(struct ipv6_addr *ap, const struct timespec *now) } if (nap == NULL) { if ((nap = malloc(sizeof(*nap))) == NULL) { - syslog(LOG_ERR, "%s: malloc: %m", __func__); + logerr(__func__); return 0; /* Well, we did add the address */ } memcpy(nap, ap, sizeof(*nap)); @@ -833,7 +830,7 @@ ipv6_addaddr(struct ipv6_addr *ia, const struct timespec *now) if ((replaced = ipv6_aliasaddr(ia, &replaced_ia)) == -1) return -1; if (blank) - syslog(LOG_DEBUG, "%s: aliased %s", ia->alias, ia->saddr); + logdebug("%s: aliased %s", ia->alias, ia->saddr); #endif if ((r = ipv6_addaddr1(ia, now)) == 0) { @@ -916,21 +913,19 @@ ipv6_addaddrs(struct ipv6_addrhead *addrs) &ap->addr, IPV6_AF_ADDED); if (apf && apf->iface != ap->iface) { if (apf->iface->metric <= ap->iface->metric) { - syslog(LOG_INFO, - "%s: preferring %s on %s", + loginfo("%s: preferring %s on %s", ap->iface->name, ap->saddr, apf->iface->name); continue; } - syslog(LOG_INFO, - "%s: preferring %s on %s", + loginfo("%s: preferring %s on %s", apf->iface->name, ap->saddr, ap->iface->name); if (if_address6(RTM_DELADDR, apf) == -1 && errno != EADDRNOTAVAIL && errno != ENXIO) - syslog(LOG_ERR, "if_address6: %m"); + logerr(__func__); apf->flags &= ~(IPV6_AF_ADDED | IPV6_AF_DADCOMPLETED); } else if (apf) @@ -1023,7 +1018,7 @@ ipv6_getstate(struct interface *ifp) ifp->if_data[IF_DATA_IPV6] = calloc(1, sizeof(*state)); state = IPV6_STATE(ifp); if (state == NULL) { - syslog(LOG_ERR, "%s: %m", __func__); + logerr(__func__); return NULL; } TAILQ_INIT(&state->addrs); @@ -1053,7 +1048,7 @@ ipv6_handleifa(struct dhcpcd_ctx *ctx, dbp = inet_ntop(AF_INET6, &addr->s6_addr, dbuf, INET6_ADDRSTRLEN); - syslog(LOG_INFO, "%s: cmd %d addr %s", + loginfo("%s: cmd %d addr %s", ifname, cmd, dbp); #endif @@ -1084,7 +1079,7 @@ ipv6_handleifa(struct dhcpcd_ctx *ctx, const char *cbp; if ((ia = calloc(1, sizeof(*ia))) == NULL) { - syslog(LOG_ERR, "%s: calloc: %m", __func__); + logerr(__func__); break; } #ifdef ALIAS_ADDR @@ -1261,7 +1256,7 @@ ipv6_addlinklocalcallback(struct interface *ifp, if (cb == NULL) { cb = malloc(sizeof(*cb)); if (cb == NULL) { - syslog(LOG_ERR, "%s: %m", __func__); + logerr(__func__); return -1; } cb->callback = callback; @@ -1389,7 +1384,7 @@ nextslaacprivate: return -1; } - syslog(LOG_WARNING, "%s: waiting for %s to complete", + logwarnx("%s: waiting for %s to complete", ap2->iface->name, ap2->saddr); free(ap); errno = EEXIST; @@ -1469,10 +1464,10 @@ ipv6_staticdadcallback(void *arg) wascompleted = (ia->flags & IPV6_AF_DADCOMPLETED); ia->flags |= IPV6_AF_DADCOMPLETED; if (ia->flags & IPV6_AF_DUPLICATED) - syslog(LOG_WARNING, "%s: DAD detected %s", ia->iface->name, + logwarnx("%s: DAD detected %s", ia->iface->name, ia->saddr); else if (!wascompleted) { - syslog(LOG_DEBUG, "%s: IPv6 static DAD completed", + logdebug("%s: IPv6 static DAD completed", ia->iface->name); } @@ -1677,7 +1672,7 @@ ipv6_handleifa_addrs(int cmd, switch (cmd) { case RTM_DELADDR: if (ia->flags & IPV6_AF_ADDED) { - syslog(LOG_INFO, "%s: deleted address %s", + loginfo("%s: deleted address %s", ia->iface->name, ia->saddr); ia->flags &= ~IPV6_AF_ADDED; } @@ -1821,14 +1816,13 @@ ipv6_tempdadcallback(void *arg) struct timespec tv; if (++ia->dadcounter == TEMP_IDGEN_RETRIES) { - syslog(LOG_ERR, - "%s: too many duplicate temporary addresses", + logerrx("%s: too many duplicate temporary addresses", ia->iface->name); return; } clock_gettime(CLOCK_MONOTONIC, &tv); if ((ia1 = ipv6_createtempaddr(ia, &tv)) == NULL) - syslog(LOG_ERR, "ipv6_createtempaddr: %m"); + logerr(__func__); else ia1->dadcounter = ia->dadcounter; ipv6_deleteaddr(ia); @@ -2047,13 +2041,13 @@ ipv6_regentempaddr(void *arg) struct ipv6_addr *ia = arg, *ia1; struct timespec tv; - syslog(LOG_DEBUG, "%s: regen temp addr %s", ia->iface->name, ia->saddr); + logdebug("%s: regen temp addr %s", ia->iface->name, ia->saddr); clock_gettime(CLOCK_MONOTONIC, &tv); ia1 = ipv6_createtempaddr(ia, &tv); if (ia1) ipv6_addaddr(ia1, &tv); else - syslog(LOG_ERR, "ipv6_createtempaddr: %m"); + logerr(__func__); } static void @@ -2120,8 +2114,8 @@ inet6_makeprefix(struct interface *ifp, const struct ra *rap, break; } if (lo0 == NULL) - syslog(LOG_WARNING, - "cannot find a loopback interface to reject via"); + logwarnx("cannot find a loopback interface " + "to reject via"); else ifp = lo0; } |
