#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <syslog.h>
#define ELOOP_QUEUE ELOOP_DHCP
#include "config.h"
}
static void
-log_dhcp(logfunc_t *logfunc, const char *msg,
+log_dhcp(int loglevel, const char *msg,
const struct interface *ifp, const struct bootp *bootp, size_t bootp_len,
const struct in_addr *from, int ad)
{
print_string(sname, sizeof(sname), OT_STRING | OT_DOMAIN,
bootp->sname, sizeof(bootp->sname));
if (a == NULL)
- logfunc("%s: %s %s %s `%s'",
+ logmessage(loglevel, "%s: %s %s %s `%s'",
ifp->name, msg, tfrom, inet_ntoa(addr), sname);
else
- logfunc("%s: %s %s %s %s `%s'",
+ logmessage(loglevel, "%s: %s %s %s %s `%s'",
ifp->name, msg, a, tfrom, inet_ntoa(addr), sname);
} else {
if (r != 0) {
addr = *from;
}
if (a == NULL)
- logfunc("%s: %s %s %s",
+ logmessage(loglevel, "%s: %s %s %s",
ifp->name, msg, tfrom, inet_ntoa(addr));
else
- logfunc("%s: %s %s %s %s",
+ logmessage(loglevel, "%s: %s %s %s %s",
ifp->name, msg, a, tfrom, inet_ntoa(addr));
}
free(a);
(uint8_t *)bootp, bootp_len, 4, type,
auth, auth_len) == NULL)
{
- LOGDHCP0(logerrx, "authentication failed");
+ LOGDHCP0(LOG_ERR, "authentication failed");
return;
}
if (state->auth.token)
loginfox("%s: accepted reconfigure key", ifp->name);
} else if (ifo->auth.options & DHCPCD_AUTH_SEND) {
if (ifo->auth.options & DHCPCD_AUTH_REQUIRE) {
- LOGDHCP0(logerrx, "no authentication");
+ LOGDHCP0(LOG_ERR, "no authentication");
return;
}
- LOGDHCP0(logwarnx, "no authentication");
+ LOGDHCP0(LOG_WARNING, "no authentication");
}
#endif
if (from->s_addr == INADDR_ANY ||
from->s_addr == INADDR_BROADCAST)
{
- LOGDHCP(logerrx, "discarding Force Renew");
+ LOGDHCP(LOG_ERR, "discarding Force Renew");
return;
}
#ifdef AUTH
if (auth == NULL) {
- LOGDHCP(logerrx, "unauthenticated Force Renew");
+ LOGDHCP(LOG_ERR, "unauthenticated Force Renew");
if (ifo->auth.options & DHCPCD_AUTH_REQUIRE)
return;
}
if (state->state != DHS_BOUND && state->state != DHS_INFORM) {
- LOGDHCP(logdebugx, "not bound, ignoring Force Renew");
+ LOGDHCP(LOG_DEBUG, "not bound, ignoring Force Renew");
return;
}
- LOGDHCP(loginfox, "Force Renew from");
+ LOGDHCP(LOG_INFO, "Force Renew from");
/* The rebind and expire timings are still the same, we just
* enter the renew state early */
if (state->state == DHS_BOUND)
dhcp_inform(ifp);
}
#else
- LOGDHCP(logerrx, "unauthenticated Force Renew");
+ LOGDHCP(LOG_ERR, "unauthenticated Force Renew");
#endif
return;
}
if (state->state == DHS_BOUND) {
- LOGDHCP(logdebugx, "bound, ignoring");
+ LOGDHCP(LOG_DEBUG, "bound, ignoring");
return;
}
if (state->state == DHS_PROBE) {
/* Ignore any DHCP messages whilst probing a lease to bind. */
- LOGDHCP(logdebugx, "probing, ignoring");
+ LOGDHCP(LOG_DEBUG, "probing, ignoring");
return;
}
get_option_uint8(ifp->ctx, &tmp,
bootp, bootp_len, (uint8_t)i) == 0)
{
- LOGDHCP(logwarnx, "reject DHCP");
+ LOGDHCP(LOG_WARNING, "reject DHCP");
return;
}
}
get_option_addr(ifp->ctx, &addr,
bootp, bootp_len, DHO_SERVERID) == -1)
{
- LOGDHCP(logwarnx, "reject NAK");
+ LOGDHCP(LOG_WARNING, "reject NAK");
return;
}
/* We should restart on a NAK */
- LOGDHCP(logwarnx, "NAK:");
+ LOGDHCP(LOG_WARNING, "NAK:");
if ((msg = get_option_string(ifp->ctx,
bootp, bootp_len, DHO_MESSAGE)))
{
* always true. */
if (type == 0 && i == DHO_SERVERID)
continue;
- LOGDHCP(logwarnx, "reject DHCP");
+ LOGDHCP(LOG_WARNING, "reject DHCP");
return;
}
}
/* DHCP Auto-Configure, RFC 2563 */
if (type == DHCP_OFFER && bootp->yiaddr == 0) {
- LOGDHCP(logwarnx, "no address given");
+ LOGDHCP(LOG_WARNING, "no address given");
if ((msg = get_option_string(ifp->ctx,
bootp, bootp_len, DHO_MESSAGE)))
{
{
switch (tmp) {
case 0:
- LOGDHCP(logwarnx, "IPv4LL disabled from");
+ LOGDHCP(LOG_WARNING, "IPv4LL disabled from");
ipv4ll_drop(ifp);
#ifdef ARP
arp_drop(ifp);
#endif
break;
case 1:
- LOGDHCP(logwarnx, "IPv4LL enabled from");
+ LOGDHCP(LOG_WARNING, "IPv4LL enabled from");
ipv4ll_start(ifp);
break;
default:
&&
(bootp->yiaddr == INADDR_ANY || bootp->yiaddr == INADDR_BROADCAST))
{
- LOGDHCP(logwarnx, "reject invalid address");
+ LOGDHCP(LOG_WARNING, "reject invalid address");
return;
}
#ifdef IN_IFF_DUPLICATED
ia = ipv4_iffindaddr(ifp, &lease->addr, NULL);
if (ia && ia->addr_flags & IN_IFF_DUPLICATED) {
- LOGDHCP(logwarnx, "declined duplicate address");
+ LOGDHCP(LOG_WARNING, "declined duplicate address");
if (type)
dhcp_decline(ifp);
ipv4_deladdr(ia, 0);
goto rapidcommit;
}
- LOGDHCP(loginfox, "offered");
+ LOGDHCP(LOG_INFO, "offered");
if (state->offer_len < bootp_len) {
free(state->offer);
if ((state->offer = malloc(bootp_len)) == NULL) {
if (type) {
if (type == DHCP_OFFER) {
- LOGDHCP(logwarnx, "ignoring offer of");
+ LOGDHCP(LOG_WARNING, "ignoring offer of");
return;
}
/* We should only be dealing with acks */
if (type != DHCP_ACK) {
- LOGDHCP(logerr, "not ACK or OFFER");
+ LOGDHCP(LOG_ERR, "not ACK or OFFER");
return;
}
DHO_RAPIDCOMMIT, NULL))
state->state = DHS_REQUEST;
else {
- LOGDHCP(logdebugx, "ignoring ack of");
+ LOGDHCP(LOG_DEBUG, "ignoring ack of");
return;
}
}
rapidcommit:
if (!(ifo->options & DHCPCD_INFORM))
- LOGDHCP(logdebugx, "acknowledged");
+ LOGDHCP(LOG_DEBUG, "acknowledged");
else
ifo->options &= ~DHCPCD_STATIC;
}
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
+#include <syslog.h>
#define ELOOP_QUEUE ELOOP_DHCP6
#include "config.h"
void * (*f)(void *, size_t, uint16_t, uint16_t *), *farg;
char buf[32], *sbuf;
const char *status;
- logfunc_t *logfunc;
+ int loglevel;
state = D6_STATE(ifp);
f = p ? dhcp6_findoption : dhcp6_findmoption;
}
if (state->lerror == code || state->state == DH6S_INIT)
- logfunc = logdebugx;
+ loglevel = LOG_DEBUG;
else
- logfunc = logerrx;
- logfunc("%s: DHCPv6 REPLY: %s", ifp->name, status);
+ loglevel = LOG_ERR;
+ logmessage(loglevel, "%s: DHCPv6 REPLY: %s", ifp->name, status);
free(sbuf);
state->lerror = code;
errno = 0;
if (!(ap->flags & IPV6_AF_DELEGATEDPFX))
continue;
if (!(ap->flags & IPV6_AF_DELEGATEDLOG)) {
- logfunc_t *logfunc;
+ int loglevel;
if (ap->flags & IPV6_AF_NEW)
- logfunc = loginfox;
+ loglevel = LOG_INFO;
else
- logfunc = logdebugx;
+ loglevel = LOG_DEBUG;
/* We only want to log this the once as we loop
* through many interfaces first. */
ap->flags |= IPV6_AF_DELEGATEDLOG;
- logfunc("%s: delegated prefix %s",
+ logmessage(loglevel, "%s: delegated prefix %s",
ifp->name, ap->saddr);
ap->flags &= ~IPV6_AF_NEW;
}
struct dhcp6_state *state = D6_STATE(ifp);
bool timedout = (op == NULL), has_new = false, confirmed;
struct ipv6_addr *ia;
- logfunc_t *lognewinfo;
+ int loglevel;
struct timespec now;
TAILQ_FOREACH(ia, &state->addrs, next) {
break;
}
}
- lognewinfo = has_new ? loginfox : logdebugx;
+ loglevel = has_new ? LOG_INFO : LOG_DEBUG;
if (!timedout) {
- lognewinfo("%s: %s received from %s", ifp->name, op, sfrom);
+ logmessage(loglevel, "%s: %s received from %s",
+ ifp->name, op, sfrom);
/* If we delegated from an unconfirmed lease we MUST drop
* them now. Hopefully we have new delegations. */
if (state->reason != NULL &&
dhcp6_deprecateaddrs(&state->addrs);
if (state->state == DH6S_INFORMED)
- lognewinfo("%s: refresh in %"PRIu32" seconds",
+ logmessage(loglevel, "%s: refresh in %"PRIu32" seconds",
ifp->name, state->renew);
else if (state->renew == ND6_INFINITE_LIFETIME)
- lognewinfo("%s: leased for infinity", ifp->name);
+ logmessage(loglevel, "%s: leased for infinity",
+ ifp->name);
else if (state->renew || state->rebind)
- lognewinfo("%s: renew in %"PRIu32", "
+ logmessage(loglevel, "%s: renew in %"PRIu32", "
"rebind in %"PRIu32", "
"expire in %"PRIu32" seconds",
ifp->name,
state->renew, state->rebind, state->expire);
else if (state->expire == 0)
- lognewinfo("%s: will expire", ifp->name);
+ logmessage(loglevel, "%s: will expire", ifp->name);
else
- lognewinfo("%s: expire in %"PRIu32" seconds",
+ logmessage(loglevel, "%s: expire in %"PRIu32" seconds",
ifp->name, state->expire);
rt_build(ifp->ctx, AF_INET6);
if (!confirmed && !timedout)
#include <string.h>
#include <unistd.h>
#include <time.h>
+#include <syslog.h>
#include "config.h"
#include "arp.h"
}
if (ifp == NULL) {
if (ctx.ifc == 0) {
- logfunc_t *logfunc;
+ int loglevel;
- logfunc = ctx.options & DHCPCD_INACTIVE ?
- logdebugx : logerrx;
- logfunc("no valid interfaces found");
+ loglevel = ctx.options & DHCPCD_INACTIVE ?
+ LOG_DEBUG : LOG_ERR;
+ logmessage(loglevel, "no valid interfaces found");
} else
goto exit_failure;
if (!(ctx.options & DHCPCD_LINK)) {
ctx.options & DHCPCD_LINK &&
!(ctx.options & DHCPCD_WAITIP))
{
- logfunc_t *logfunc;
+ int loglevel;
- logfunc = ctx.options & DHCPCD_INACTIVE ?
- logdebugx : logwarnx;
- logfunc("no interfaces have a carrier");
+ loglevel = ctx.options & DHCPCD_INACTIVE ?
+ LOG_DEBUG : LOG_WARNING;
+ logmessage(loglevel, "no interfaces have a carrier");
if (dhcpcd_daemonise(&ctx))
goto exit_success;
} else if (t > 0 &&
snprintf(path, sizeof(path), "%s/%s/accept_ra", p_conf, ifp->name);
ra = check_proc_int(path);
if (ra == -1) {
- logfunc_t *logfunc = errno == ENOENT? logdebug : logwarn;
-
/* The sysctl probably doesn't exist, but this isn't an
* error as such so just log it and continue */
- logfunc("%s", path);
+ if (errno != ENOENT)
+ logerr("%s: %s", __func__, path);
} else if (ra != 0) {
if (if_writepathuint(path, 0) == -1)
logerr("%s: %s", __func__, path);
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
+#include <syslog.h>
#include "config.h"
#endif
if (if_vimaster(ctx, spec.devname) == 1) {
- logfunc_t *logfunc = argc != 0 ? logerrx : logdebugx;
- logfunc("%s: is a Virtual Interface Master, skipping",
+ int loglevel = argc != 0 ? LOG_ERR : LOG_DEBUG;
+ logmessage(loglevel,
+ "%s: is a Virtual Interface Master, skipping",
spec.devname);
continue;
}
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
+#include <syslog.h>
#include "config.h"
{
struct interface *ifp;
uint32_t pltime, vltime;
- __printflike(1, 2) void (*logfunc)(const char *, ...);
+ int loglevel;
#ifdef ND6_ADVERTISE
bool vltime_was_zero = ia->prefix_vltime == 0;
#endif
}
}
- logfunc = ia->flags & IPV6_AF_NEW ? loginfox : logdebugx;
- logfunc("%s: adding %saddress %s", ifp->name,
+ loglevel = ia->flags & IPV6_AF_NEW ? LOG_INFO : LOG_DEBUG;
+ logmessage(loglevel, "%s: adding %saddress %s", ifp->name,
#ifdef IPV6_AF_TEMPORARY
ia->flags & IPV6_AF_TEMPORARY ? "temporary " : "",
#else
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <syslog.h>
#define ELOOP_QUEUE ELOOP_IPV6ND
#include "common.h"
bool new_rap, new_data, has_address;
uint32_t old_lifetime;
int ifmtu;
- __printflike(1, 2) void (*logfunc)(const char *, ...);
+ int loglevel;
#ifdef IPV6_MANAGETEMPADDR
uint8_t new_ap;
#endif
* much needless log spam. */
if (rap->willexpire)
new_data = true;
- logfunc = new_data || !rap->isreachable ? loginfox : logdebugx,
- logfunc("%s: Router Advertisement from %s", ifp->name, rap->sfrom);
+ loglevel = new_data || !rap->isreachable ? LOG_INFO : LOG_DEBUG,
+ logmessage(loglevel, "%s: Router Advertisement from %s",
+ ifp->name, rap->sfrom);
clock_gettime(CLOCK_MONOTONIC, &rap->acquired);
rap->flags = nd_ra->nd_ra_flags_reserved;
switch (ndo.nd_opt_type) {
case ND_OPT_PREFIX_INFORMATION:
- logfunc = new_data ? logerrx : logdebugx;
+ loglevel = new_data ? LOG_ERR : LOG_DEBUG;
if (ndo.nd_opt_len != 4) {
- logfunc("%s: invalid option len for prefix",
+ logmessage(loglevel, "%s: invalid option len for prefix",
ifp->name);
continue;
}
memcpy(&pi, p, sizeof(pi));
if (pi.nd_opt_pi_prefix_len > 128) {
- logfunc("%s: invalid prefix len", ifp->name);
+ logmessage(loglevel, "%s: invalid prefix len", ifp->name);
continue;
}
/* nd_opt_pi_prefix is not aligned. */
if (IN6_IS_ADDR_MULTICAST(&pi_prefix) ||
IN6_IS_ADDR_LINKLOCAL(&pi_prefix))
{
- logfunc("%s: invalid prefix in RA", ifp->name);
+ logmessage(loglevel, "%s: invalid prefix in RA", ifp->name);
continue;
}
if (ntohl(pi.nd_opt_pi_preferred_time) >
ntohl(pi.nd_opt_pi_valid_time))
{
- logfunc("%s: pltime > vltime", ifp->name);
+ logmessage(loglevel, "%s: pltime > vltime", ifp->name);
continue;
}
TAILQ_FOREACH(ap, &rap->addrs, next)
case ND_OPT_MTU:
if (len < sizeof(mtu)) {
- logfunc("%s: short MTU option", ifp->name);
+ logmessage(loglevel, "%s: short MTU option", ifp->name);
break;
}
memcpy(&mtu, p, sizeof(mtu));
mtu.nd_opt_mtu_mtu = ntohl(mtu.nd_opt_mtu_mtu);
if (mtu.nd_opt_mtu_mtu < IPV6_MMTU) {
- logfunc("%s: invalid MTU %d",
+ logmessage(loglevel, "%s: invalid MTU %d",
ifp->name, mtu.nd_opt_mtu_mtu);
break;
}
if (ifmtu == -1)
logerr("if_getmtu");
else if (mtu.nd_opt_mtu_mtu > (uint32_t)ifmtu) {
- logfunc("%s: advertised MTU %d"
+ logmessage(loglevel, "%s: advertised MTU %d"
" is greater than link MTU %d",
ifp->name, mtu.nd_opt_mtu_mtu, ifmtu);
rap->mtu = (uint32_t)ifmtu;
break;
case ND_OPT_RDNSS:
if (len < sizeof(rdnss)) {
- logfunc("%s: short RDNSS option", ifp->name);
+ logmessage(loglevel, "%s: short RDNSS option", ifp->name);
break;
}
memcpy(&rdnss, p, sizeof(rdnss));
#pragma GCC diagnostic pop
#endif
-__printflike(2, 3) static void
+__printflike(2, 3) void
logmessage(int pri, const char *fmt, ...)
{
va_list args;
logmessage(pri, "%s: %s", buf, strerror(_errno));
}
+__printflike(2, 3) void
+logerrmessage(int pri, const char *fmt, ...)
+{
+ va_list args;
+
+ va_start(args, fmt);
+ vlogerrmessage(pri, fmt, args);
+ va_end(args);
+}
+
void
-logdebug(const char *fmt, ...)
+log_debug(const char *fmt, ...)
{
va_list args;
}
void
-logdebugx(const char *fmt, ...)
+log_debugx(const char *fmt, ...)
{
va_list args;
}
void
-loginfo(const char *fmt, ...)
+log_info(const char *fmt, ...)
{
va_list args;
}
void
-loginfox(const char *fmt, ...)
+log_infox(const char *fmt, ...)
{
va_list args;
}
void
-logwarn(const char *fmt, ...)
+log_warn(const char *fmt, ...)
{
va_list args;
}
void
-logwarnx(const char *fmt, ...)
+log_warnx(const char *fmt, ...)
{
va_list args;
}
void
-logerr(const char *fmt, ...)
+log_err(const char *fmt, ...)
{
va_list args;
}
void
-logerrx(const char *fmt, ...)
+log_errx(const char *fmt, ...)
{
va_list args;
#endif
#endif /* !__printflike */
-__printflike(1, 2) typedef void logfunc_t(const char *, ...);
-
-__printflike(1, 2) void logdebug(const char *, ...);
-__printflike(1, 2) void logdebugx(const char *, ...);
-__printflike(1, 2) void loginfo(const char *, ...);
-__printflike(1, 2) void loginfox(const char *, ...);
-__printflike(1, 2) void logwarn(const char *, ...);
-__printflike(1, 2) void logwarnx(const char *, ...);
-__printflike(1, 2) void logerr(const char *, ...);
+/* Please do not call log_* functions directly, use macros below */
+__printflike(1, 2) void log_debug(const char *, ...);
+__printflike(1, 2) void log_debugx(const char *, ...);
+__printflike(1, 2) void log_info(const char *, ...);
+__printflike(1, 2) void log_infox(const char *, ...);
+__printflike(1, 2) void log_warn(const char *, ...);
+__printflike(1, 2) void log_warnx(const char *, ...);
+__printflike(1, 2) void log_err(const char *, ...);
+__printflike(1, 2) void log_errx(const char *, ...);
#define LOGERROR logerr("%s: %d", __FILE__, __LINE__)
-__printflike(1, 2) void logerrx(const char *, ...);
+
+__printflike(2, 3) void logmessage(int pri, const char *fmt, ...);
+__printflike(2, 3) void logerrmessage(int pri, const char *fmt, ...);
+
+/*
+ * These are macros to prevent taking address of them so
+ * __FILE__, __LINE__, etc can easily be added.
+ *
+ * We should be using
+ * #define loginfox(fmt, __VA_OPT__(,) __VA_ARGS__)
+ * but that requires gcc-8 or clang-6 and we still have a need to support
+ * old OS's without modern compilers.
+ *
+ * Likewise, ##__VA_ARGS__ can't be used as that's a gcc only extension.
+ *
+ * The solution is to put fmt into __VA_ARGS__.
+ * It's not pretty but it's 100% portable.
+ */
+#define logdebug(...) log_debug(__VA_ARGS__)
+#define logdebugx(...) log_debugx(__VA_ARGS__)
+#define loginfo(...) log_info(__VA_ARGS__)
+#define loginfox(...) log_infox(__VA_ARGS__)
+#define logwarn(...) log_warn(__VA_ARGS__)
+#define logwarnx(...) log_warnx(__VA_ARGS__)
+#define logerr(...) log_err(__VA_ARGS__)
+#define logerrx(...) log_errx(__VA_ARGS__)
void logsetopts(unsigned int);
#define LOGERR_DEBUG (1U << 6)