summaryrefslogtreecommitdiffstats
path: root/dhcpcd.c
diff options
context:
space:
mode:
authorRoy Marples <roy@marples.name>2008-09-05 18:24:34 +0000
committerRoy Marples <roy@marples.name>2008-09-05 18:24:34 +0000
commit765fbf7db0669762f6fe4f829f37c6b10de77fe4 (patch)
tree56290e5630958b75f89dea657836284eaeb25ec5 /dhcpcd.c
parentf25f3fb214d3106bb12fdaa0b67eeabd3dadbb0a (diff)
downloaddhcpcd-765fbf7db0669762f6fe4f829f37c6b10de77fe4.tar.xz
Remove logger and exclusively uses syslog instead. This saves almost 2K on NetBSD/amd64 from before changing strerror to %m.
Diffstat (limited to 'dhcpcd.c')
-rw-r--r--dhcpcd.c129
1 files changed, 64 insertions, 65 deletions
diff --git a/dhcpcd.c b/dhcpcd.c
index 1b3719e8..de01713a 100644
--- a/dhcpcd.c
+++ b/dhcpcd.c
@@ -42,6 +42,7 @@ const char copyright[] = "Copyright (c) 2006-2008 Roy Marples";
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <syslog.h>
#include <unistd.h>
#include <time.h>
@@ -57,7 +58,6 @@ const char copyright[] = "Copyright (c) 2006-2008 Roy Marples";
#include "eloop.h"
#include "if-options.h"
#include "ipv4ll.h"
-#include "logger.h"
#include "net.h"
#include "signals.h"
@@ -145,7 +145,7 @@ cleanup(void)
if (pidfd > -1) {
if (options & DHCPCD_MASTER) {
if (stop_control() == -1)
- logger(LOG_ERR, "stop_control: %m");
+ syslog(LOG_ERR, "stop_control: %m");
}
close(pidfd);
unlink(pidfile);
@@ -158,7 +158,7 @@ cleanup(void)
_noreturn void
handle_exit_timeout(_unused void *arg)
{
- logger(LOG_ERR, "timed out");
+ syslog(LOG_ERR, "timed out");
exit(EXIT_FAILURE);
}
@@ -199,7 +199,7 @@ stop_interface(struct interface *iface)
{
struct interface *ifp, *ifl = NULL;
- logger(LOG_INFO, "%s: removing interface", iface->name);
+ syslog(LOG_INFO, "%s: removing interface", iface->name);
drop_config(iface, "STOP");
close_sockets(iface);
delete_timeout(NULL, iface);
@@ -230,7 +230,7 @@ send_message(struct interface *iface, int type,
struct timeval tv;
if (!callback)
- logger(LOG_DEBUG, "%s: sending %s with xid 0x%x",
+ syslog(LOG_DEBUG, "%s: sending %s with xid 0x%x",
iface->name, get_dhcp_op(type), state->xid);
else {
if (state->interval == 0)
@@ -242,7 +242,7 @@ send_message(struct interface *iface, int type,
}
tv.tv_sec = state->interval + DHCP_RAND_MIN;
tv.tv_usec = arc4random() % (DHCP_RAND_MAX_U - DHCP_RAND_MIN_U);
- logger(LOG_DEBUG,
+ syslog(LOG_DEBUG,
"%s: sending %s with xid 0x%x, next in %0.2f seconds",
iface->name, get_dhcp_op(type), state->xid,
timeval_to_double(&tv));
@@ -265,13 +265,13 @@ send_message(struct interface *iface, int type,
if (to.s_addr && to.s_addr != INADDR_BROADCAST) {
r = send_packet(iface, to, (uint8_t *)dhcp, len);
if (r == -1)
- logger(LOG_ERR, "%s: send_packet: %m", iface->name);
+ syslog(LOG_ERR, "%s: send_packet: %m", iface->name);
} else {
len = make_udp_packet(&udp, (uint8_t *)dhcp, len, from, to);
r = send_raw_packet(iface, ETHERTYPE_IP, udp, len);
free(udp);
if (r == -1)
- logger(LOG_ERR, "%s: send_raw_packet: %m", iface->name);
+ syslog(LOG_ERR, "%s: send_raw_packet: %m", iface->name);
}
free(dhcp);
if (r == -1) {
@@ -305,8 +305,8 @@ start_renew(void *arg)
{
struct interface *iface = arg;
- logger(LOG_INFO, "%s: renewing lease of %s",
- iface->name, inet_ntoa(iface->state->lease.addr));
+ syslog(LOG_INFO, "%s: renewing lease of %s",
+ iface->name, inet_ntoa(iface->state->lease.addr));
iface->state->state = DHS_RENEWING;
iface->state->xid = arc4random();
send_renew(iface);
@@ -323,8 +323,8 @@ start_rebind(void *arg)
{
struct interface *iface = arg;
- logger(LOG_ERR, "%s: failed to renew, attmepting to rebind",
- iface->name);
+ syslog(LOG_ERR, "%s: failed to renew, attmepting to rebind",
+ iface->name);
iface->state->state = DHS_REBINDING;
delete_timeout(send_renew, iface);
iface->state->lease.server.s_addr = 0;
@@ -337,7 +337,7 @@ start_expire(void *arg)
struct interface *iface = arg;
int ll = IN_LINKLOCAL(htonl(iface->state->lease.addr.s_addr));
- logger(LOG_ERR, "%s: lease expired", iface->name);
+ syslog(LOG_ERR, "%s: lease expired", iface->name);
delete_timeout(NULL, iface);
drop_config(iface, "EXPIRE");
iface->state->interval = 0;
@@ -371,13 +371,13 @@ log_dhcp(int lvl, const char *msg,
}
r = get_option_addr(&addr.s_addr, dhcp, DHO_SERVERID);
if (dhcp->servername[0] && r == 0)
- logger(lvl, "%s: %s %s from %s `%s'", iface->name, msg, a,
+ syslog(lvl, "%s: %s %s from %s `%s'", iface->name, msg, a,
inet_ntoa(addr), dhcp->servername);
else if (r == 0)
- logger(lvl, "%s: %s %s from %s",
+ syslog(lvl, "%s: %s %s from %s",
iface->name, msg, a, inet_ntoa(addr));
else
- logger(lvl, "%s: %s %s", iface->name, msg, a);
+ syslog(lvl, "%s: %s %s", iface->name, msg, a);
free(a);
}
@@ -411,12 +411,12 @@ handle_dhcp(struct interface *iface, struct dhcp_message **dhcpp)
if (ifo->blacklist[i] != addr.s_addr)
continue;
if (dhcp->servername[0])
- logger(LOG_WARNING,
+ syslog(LOG_WARNING,
"%s: ignoring blacklisted server %s `%s'",
- iface->name,
- inet_ntoa(addr), dhcp->servername);
+ iface->name,
+ inet_ntoa(addr), dhcp->servername);
else
- logger(LOG_WARNING,
+ syslog(LOG_WARNING,
"%s: ignoring blacklisted server %s",
iface->name, inet_ntoa(addr));
return;
@@ -534,7 +534,7 @@ handle_dhcp_packet(void *arg)
continue;
bytes = get_udp_data(&pp, packet);
if ((size_t)bytes > sizeof(*dhcp)) {
- logger(LOG_ERR, "%s: packet greater than DHCP size",
+ syslog(LOG_ERR, "%s: packet greater than DHCP size",
iface->name);
continue;
}
@@ -542,13 +542,13 @@ handle_dhcp_packet(void *arg)
dhcp = xmalloc(sizeof(*dhcp));
memcpy(dhcp, pp, bytes);
if (dhcp->cookie != htonl(MAGIC_COOKIE)) {
- logger(LOG_DEBUG, "%s: bogus cookie, ignoring",
+ syslog(LOG_DEBUG, "%s: bogus cookie, ignoring",
iface->name);
continue;
}
/* Ensure it's the right transaction */
if (iface->state->xid != dhcp->xid) {
- logger(LOG_DEBUG,
+ syslog(LOG_DEBUG,
"%s: ignoring packet with xid 0x%x as"
" it's not ours (0x%x)",
iface->name, dhcp->xid, iface->state->xid);
@@ -558,7 +558,7 @@ handle_dhcp_packet(void *arg)
if (iface->hwlen <= sizeof(dhcp->chaddr) &&
memcmp(dhcp->chaddr, iface->hwaddr, iface->hwlen))
{
- logger(LOG_DEBUG, "%s: xid 0x%x is not for our hwaddr %s",
+ syslog(LOG_DEBUG, "%s: xid 0x%x is not for our hwaddr %s",
iface->name, dhcp->xid,
hwaddr_ntoa(dhcp->chaddr, sizeof(dhcp->chaddr)));
continue;
@@ -587,11 +587,11 @@ open_sockets(struct interface *iface)
close(iface->udp_fd);
if (open_udp_socket(iface) == -1 &&
(errno != EADDRINUSE || iface->addr.s_addr != 0))
- logger(LOG_ERR, "%s: open_udp_socket: %m", iface->name);
+ syslog(LOG_ERR, "%s: open_udp_socket: %m", iface->name);
if (iface->raw_fd != -1)
delete_event(iface->raw_fd);
if (open_socket(iface, ETHERTYPE_IP) == -1)
- logger(LOG_ERR, "%s: open_socket: %m", iface->name);
+ syslog(LOG_ERR, "%s: open_socket: %m", iface->name);
if (iface->raw_fd != -1)
add_event(iface->raw_fd, handle_dhcp_packet, iface);
}
@@ -608,12 +608,12 @@ handle_carrier(const char *ifname)
return;
switch (carrier_status(iface->name)) {
case -1:
- logger(LOG_ERR, "carrier_status: %m");
+ syslog(LOG_ERR, "carrier_status: %m");
break;
case 0:
if (iface->state->carrier != LINK_DOWN) {
iface->state->carrier = LINK_DOWN;
- logger(LOG_INFO, "%s: carrier lost", iface->name);
+ syslog(LOG_INFO, "%s: carrier lost", iface->name);
close_sockets(iface);
delete_timeouts(iface, start_expire, NULL);
}
@@ -621,7 +621,7 @@ handle_carrier(const char *ifname)
default:
if (iface->state->carrier != LINK_UP) {
iface->state->carrier = LINK_UP;
- logger(LOG_INFO, "%s: carrier acquired", iface->name);
+ syslog(LOG_INFO, "%s: carrier acquired", iface->name);
start_interface(iface);
}
break;
@@ -646,7 +646,7 @@ start_discover(void *arg)
else
add_timeout_sec(ifo->timeout, start_ipv4ll, iface);
}
- logger(LOG_INFO, "%s: broadcasting for a lease", iface->name);
+ syslog(LOG_INFO, "%s: broadcasting for a lease", iface->name);
send_discover(iface);
}
@@ -656,8 +656,8 @@ start_reboot(struct interface *iface)
{
struct if_options *ifo = iface->state->options;
- logger(LOG_INFO, "%s: rebinding lease of %s",
- iface->name, inet_ntoa(iface->state->lease.addr));
+ syslog(LOG_INFO, "%s: rebinding lease of %s",
+ iface->name, inet_ntoa(iface->state->lease.addr));
iface->state->state = DHS_REBINDING;
iface->state->xid = arc4random();
iface->state->lease.server.s_addr = 0;
@@ -673,8 +673,8 @@ send_release(struct interface *iface)
if (iface->state->lease.addr.s_addr &&
!IN_LINKLOCAL(htonl(iface->state->lease.addr.s_addr)))
{
- logger(LOG_INFO, "%s: releasing lease of %s",
- iface->name, inet_ntoa(iface->state->lease.addr));
+ syslog(LOG_INFO, "%s: releasing lease of %s",
+ iface->name, inet_ntoa(iface->state->lease.addr));
open_sockets(iface);
send_message(iface, DHCP_RELEASE, NULL);
}
@@ -717,7 +717,7 @@ configure_interface(struct interface *iface, int argc, char **argv)
if (ifo->options & DHCPCD_DUID) {
duid = xmalloc(DUID_LEN);
if ((len = get_duid(duid, iface)) == 0)
- logger(LOG_ERR, "get_duid: %m");
+ syslog(LOG_ERR, "get_duid: %m");
}
if (len > 0) {
iface->clientid = xmalloc(len + 6);
@@ -775,7 +775,7 @@ init_state(struct interface *iface, int argc, char **argv)
}
if (ifs->carrier == LINK_DOWN)
- logger(LOG_INFO, "%s: waiting for carrier", iface->name);
+ syslog(LOG_INFO, "%s: waiting for carrier", iface->name);
else
start_interface(iface);
}
@@ -834,7 +834,7 @@ handle_link(_unused void *arg)
handle_carrier,
handle_new_interface,
handle_remove_interface) == -1)
- logger(LOG_ERR, "manage_link: %m");
+ syslog(LOG_ERR, "manage_link: %m");
}
static void
@@ -846,21 +846,21 @@ handle_signal(_unused void *arg)
switch (sig) {
case SIGINT:
- logger(LOG_INFO, "received SIGINT, stopping");
+ syslog(LOG_INFO, "received SIGINT, stopping");
break;
case SIGTERM:
- logger(LOG_INFO, "received SIGTERM, stopping");
+ syslog(LOG_INFO, "received SIGTERM, stopping");
break;
case SIGALRM:
- logger(LOG_INFO, "received SIGALRM, rebinding lease");
+ syslog(LOG_INFO, "received SIGALRM, rebinding lease");
do_reboot = 1;
break;
case SIGHUP:
- logger(LOG_INFO, "received SIGHUP, releasing lease");
+ syslog(LOG_INFO, "received SIGHUP, releasing lease");
do_release = 1;
break;
default:
- logger (LOG_ERR,
+ syslog (LOG_ERR,
"received signal %d, but don't know what to do with it",
sig);
return;
@@ -905,7 +905,7 @@ handle_args(int argc, char **argv)
/* We only deal with one interface here */
if (optind == argc) {
- logger(LOG_ERR, "handle_args: no interface");
+ syslog(LOG_ERR, "handle_args: no interface");
return -1;
}
@@ -921,7 +921,7 @@ handle_args(int argc, char **argv)
if (do_release)
send_release(ifp);
if (do_exit || do_release) {
- logger(LOG_INFO, "%s: removing interface", ifp->name);
+ syslog(LOG_INFO, "%s: removing interface", ifp->name);
drop_config(ifp, do_release ? "RELEASE" : "STOP");
close_sockets(ifp);
delete_timeout(NULL, ifp);
@@ -970,9 +970,8 @@ main(int argc, char **argv)
struct timespec ts;
closefrom(3);
- /* Saves calling fflush(stream) in the logger */
- setlinebuf(stdout);
- openlog(PACKAGE, LOG_PID, LOG_LOCAL0);
+ openlog(PACKAGE, LOG_PERROR, LOG_LOCAL0);
+ setlogmask(LOG_UPTO(LOG_INFO));
options = DHCPCD_DAEMONISE;
/* Test for --help and --version */
@@ -993,7 +992,7 @@ main(int argc, char **argv)
options |= DHCPCD_BACKGROUND;
break;
case 'd':
- setloglevel(LOG_DEBUG);
+ setlogmask(LOG_UPTO(LOG_DEBUG));
break;
case 'f':
cffile = optarg;
@@ -1005,7 +1004,7 @@ main(int argc, char **argv)
sig = SIGALRM;
break;
case 'q':
- setloglevel(LOG_WARNING);
+ setlogmask(LOG_UPTO(LOG_WARNING));
options |= DHCPCD_QUIET;
break;
case 'x':
@@ -1056,40 +1055,40 @@ main(int argc, char **argv)
if (!(options & DHCPCD_MASTER)) {
control_fd = open_control();
if (control_fd != -1) {
- logger(LOG_INFO, "sending commands to master dhcpcd process");
+ syslog(LOG_INFO, "sending commands to master dhcpcd process");
i = send_control(argc, argv);
if (i > 0) {
- logger(LOG_DEBUG, "send OK");
+ syslog(LOG_DEBUG, "send OK");
exit(EXIT_SUCCESS);
} else {
- logger(LOG_ERR, "failed to send commands");
+ syslog(LOG_ERR, "failed to send commands");
exit(EXIT_FAILURE);
}
} else {
if (errno != ENOENT)
- logger(LOG_ERR, "open_control: %m");
+ syslog(LOG_ERR, "open_control: %m");
}
}
if (geteuid())
- logger(LOG_WARNING, PACKAGE " will not work correctly unless"
+ syslog(LOG_WARNING, PACKAGE " will not work correctly unless"
" run as root");
if (sig != 0) {
i = -1;
pid = read_pid();
if (pid != 0)
- logger(LOG_INFO, "sending signal %d to pid %d",
+ syslog(LOG_INFO, "sending signal %d to pid %d",
sig, pid);
if (!pid || (i = kill(pid, sig))) {
if (sig != SIGALRM)
- logger(LOG_ERR, ""PACKAGE" not running");
+ syslog(LOG_ERR, ""PACKAGE" not running");
unlink(pidfile);
exit(EXIT_FAILURE);
}
/* Spin until it exits */
- logger(LOG_INFO, "waiting for pid %d to exit", pid);
+ syslog(LOG_INFO, "waiting for pid %d to exit", pid);
ts.tv_sec = 0;
ts.tv_nsec = 100000000; /* 10th of a second */
for(i = 0; i < 100; i++) {
@@ -1097,7 +1096,7 @@ main(int argc, char **argv)
if (read_pid() == 0)
exit(EXIT_SUCCESS);
}
- logger(LOG_ERR, "pid %d failed to exit", pid);
+ syslog(LOG_ERR, "pid %d failed to exit", pid);
exit(EXIT_FAILURE);
}
@@ -1105,7 +1104,7 @@ main(int argc, char **argv)
if ((pid = read_pid()) > 0 &&
kill(pid, 0) == 0)
{
- logger(LOG_ERR, ""PACKAGE
+ syslog(LOG_ERR, ""PACKAGE
" already running on pid %d (%s)",
pid, pidfile);
exit(EXIT_FAILURE);
@@ -1113,13 +1112,13 @@ main(int argc, char **argv)
pidfd = open(pidfile, O_WRONLY | O_CREAT | O_NONBLOCK, 0664);
if (pidfd == -1) {
- logger(LOG_ERR, "open `%s': %m", pidfile);
+ syslog(LOG_ERR, "open `%s': %m", pidfile);
exit(EXIT_FAILURE);
}
/* Lock the file so that only one instance of dhcpcd runs
* on an interface */
if (flock(pidfd, LOCK_EX | LOCK_NB) == -1) {
- logger(LOG_ERR, "flock `%s': %m", pidfile);
+ syslog(LOG_ERR, "flock `%s': %m", pidfile);
exit(EXIT_FAILURE);
}
if (set_cloexec(pidfd) == -1)
@@ -1127,7 +1126,7 @@ main(int argc, char **argv)
writepid(pidfd, getpid());
}
- logger(LOG_INFO, PACKAGE " " VERSION " starting");
+ syslog(LOG_INFO, "version " VERSION " starting");
if ((signal_fd =signal_init()) == -1)
exit(EXIT_FAILURE);
@@ -1137,7 +1136,7 @@ main(int argc, char **argv)
if (options & DHCPCD_MASTER) {
if (start_control() == -1) {
- logger(LOG_ERR, "start_control: %m");
+ syslog(LOG_ERR, "start_control: %m");
exit(EXIT_FAILURE);
}
}
@@ -1145,7 +1144,7 @@ main(int argc, char **argv)
if (ifo->options & DHCPCD_LINK) {
linkfd = open_link_socket();
if (linkfd == -1)
- logger(LOG_ERR, "open_link_socket: %m");
+ syslog(LOG_ERR, "open_link_socket: %m");
else
add_event(linkfd, handle_link, NULL);
}
@@ -1164,7 +1163,7 @@ main(int argc, char **argv)
for (iface = ifaces; iface; iface = iface->next)
init_state(iface, argc, argv);
if (!ifaces && ifc == 1) {
- logger(LOG_ERR, "interface `%s' does not exist", ifv[0]);
+ syslog(LOG_ERR, "interface `%s' does not exist", ifv[0]);
exit(EXIT_FAILURE);
}
if (options & DHCPCD_BACKGROUND)