Skip to content

Commit

Permalink
Use syslog service in dhclient(8).
Browse files Browse the repository at this point in the history
dhclient(8) is failing during boot to connect to the syslog service, because
syslog daemon is started after dhclient(8). This can be reproduced by stooping
syslog daemon and ktrace the dhclient or use kern.trap_enotcap sysctl and boot
the machine. Using the Casper syslog service fix the problem.

Reviewed by:	bapt@
Differential Revision:	https://reviews.freebsd.org/D12825
  • Loading branch information
oshogbo committed Nov 12, 2017
1 parent 321aa05 commit 90ae5f5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 17 deletions.
6 changes: 6 additions & 0 deletions sbin/dhclient/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ MAN= dhclient.8 dhclient.conf.5 dhclient.leases.5 dhcp-options.5 \
dhclient-script.8
LIBADD= util

.if ${MK_CASPER} != "no" && !defined(RESCUE)
LIBADD+= casper
LIBADD+= cap_syslog
CFLAGS+=-DWITH_CASPER
.endif

WARNS?= 2

HAS_TESTS=
Expand Down
25 changes: 22 additions & 3 deletions sbin/dhclient/dhclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ __FBSDID("$FreeBSD$");

#define CLIENT_PATH "PATH=/usr/bin:/usr/sbin:/bin:/sbin"

cap_channel_t *capsyslog;

time_t cur_time;
time_t default_lease_time = 43200; /* 12 hours... */

Expand Down Expand Up @@ -345,6 +347,21 @@ routehandler(struct protocol *p)
exit(1);
}

static void
init_casper(void)
{
cap_channel_t *casper;

casper = cap_init();
if (casper == NULL)
error("unable to start casper");

capsyslog = cap_service_open(casper, "system.syslog");
cap_close(casper);
if (capsyslog == NULL)
error("unable to open system.syslog service");
}

int
main(int argc, char *argv[])
{
Expand All @@ -356,9 +373,11 @@ main(int argc, char *argv[])
pid_t otherpid;
cap_rights_t rights;

init_casper();

/* Initially, log errors to stderr as well as to syslogd. */
openlog(__progname, LOG_PID | LOG_NDELAY, DHCPD_LOG_FACILITY);
setlogmask(LOG_UPTO(LOG_DEBUG));
cap_openlog(capsyslog, __progname, LOG_PID | LOG_NDELAY, DHCPD_LOG_FACILITY);
cap_setlogmask(capsyslog, LOG_UPTO(LOG_DEBUG));

while ((ch = getopt(argc, argv, "bc:dl:p:qu")) != -1)
switch (ch) {
Expand Down Expand Up @@ -518,7 +537,7 @@ main(int argc, char *argv[])

setproctitle("%s", ifi->name);

if (cap_enter() < 0 && errno != ENOSYS)
if (CASPER_SUPPORT && cap_enter() < 0 && errno != ENOSYS)
error("can't enter capability mode: %m");

if (immediate_daemon)
Expand Down
4 changes: 4 additions & 0 deletions sbin/dhclient/dhcpd.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
#include <time.h>
#include <unistd.h>

#include <libcasper.h>
#include <casper/cap_syslog.h>

#include "dhcp.h"
#include "tree.h"

Expand Down Expand Up @@ -352,6 +355,7 @@ int addr_eq(struct iaddr, struct iaddr);
char *piaddr(struct iaddr);

/* dhclient.c */
extern cap_channel_t *capsyslog;
extern char *path_dhclient_conf;
extern char *path_dhclient_db;
extern time_t cur_time;
Expand Down
12 changes: 6 additions & 6 deletions sbin/dhclient/dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ interface_status(struct interface_info *ifinfo)
memset(&ifr, 0, sizeof(ifr));
strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) < 0) {
syslog(LOG_ERR, "ioctl(SIOCGIFFLAGS) on %s: %m", ifname);
cap_syslog(capsyslog, LOG_ERR, "ioctl(SIOCGIFFLAGS) on %s: %m",
ifname);
goto inactive;
}

Expand All @@ -316,9 +317,8 @@ interface_status(struct interface_info *ifinfo)
strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
if (errno != EINVAL) {
syslog(LOG_DEBUG, "ioctl(SIOCGIFMEDIA) on %s: %m",
ifname);

cap_syslog(capsyslog, LOG_DEBUG,
"ioctl(SIOCGIFMEDIA) on %s: %m", ifname);
ifinfo->noifmedia = 1;
goto active;
}
Expand Down Expand Up @@ -479,8 +479,8 @@ interface_link_status(char *ifname)
if (ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
/* EINVAL -> link state unknown. treat as active */
if (errno != EINVAL)
syslog(LOG_DEBUG, "ioctl(SIOCGIFMEDIA) on %s: %m",
ifname);
cap_syslog(capsyslog, LOG_DEBUG,
"ioctl(SIOCGIFMEDIA) on %s: %m", ifname);
close(sock);
return (1);
}
Expand Down
16 changes: 8 additions & 8 deletions sbin/dhclient/errwarn.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ error(char *fmt, ...)
va_end(list);

#ifndef DEBUG
syslog(log_priority | LOG_ERR, "%s", mbuf);
cap_syslog(capsyslog, log_priority | LOG_ERR, "%s", mbuf);
#endif

/* Also log it to stderr? */
Expand All @@ -78,7 +78,7 @@ error(char *fmt, ...)
write(2, "\n", 1);
}

syslog(LOG_CRIT, "exiting.");
cap_syslog(capsyslog, LOG_CRIT, "exiting.");
if (log_perror) {
fprintf(stderr, "exiting.\n");
fflush(stderr);
Expand All @@ -103,7 +103,7 @@ warning(char *fmt, ...)
va_end(list);

#ifndef DEBUG
syslog(log_priority | LOG_ERR, "%s", mbuf);
cap_syslog(capsyslog, log_priority | LOG_ERR, "%s", mbuf);
#endif

if (log_perror) {
Expand All @@ -129,7 +129,7 @@ note(char *fmt, ...)
va_end(list);

#ifndef DEBUG
syslog(log_priority | LOG_INFO, "%s", mbuf);
cap_syslog(capsyslog, log_priority | LOG_INFO, "%s", mbuf);
#endif

if (log_perror) {
Expand All @@ -155,7 +155,7 @@ debug(char *fmt, ...)
va_end(list);

#ifndef DEBUG
syslog(log_priority | LOG_DEBUG, "%s", mbuf);
cap_syslog(capsyslog, log_priority | LOG_DEBUG, "%s", mbuf);
#endif

if (log_perror) {
Expand Down Expand Up @@ -217,10 +217,10 @@ parse_warn(char *fmt, ...)
va_end(list);

#ifndef DEBUG
syslog(log_priority | LOG_ERR, "%s", mbuf);
syslog(log_priority | LOG_ERR, "%s", token_line);
cap_syslog(capsyslog, log_priority | LOG_ERR, "%s", mbuf);
cap_syslog(capsyslog, log_priority | LOG_ERR, "%s", token_line);
if (lexline < 81)
syslog(log_priority | LOG_ERR,
cap_syslog(capsyslog, log_priority | LOG_ERR,
"%s^", &spaces[sizeof(spaces) - lexchar]);
#endif

Expand Down

0 comments on commit 90ae5f5

Please sign in to comment.