Convert logging messages to suicide() where appropriate and clean up the

logging messages a bit.
This commit is contained in:
Nicholas J. Kain 2014-03-30 17:21:27 -04:00
parent 82d9682ed8
commit 1abf8462d3
7 changed files with 96 additions and 167 deletions

View File

@ -1,6 +1,6 @@
/* arp.c - arp ping checking /* arp.c - arp ping checking
* *
* Copyright (c) 2010-2013 Nicholas J. Kain <njkain at gmail dot com> * Copyright (c) 2010-2014 Nicholas J. Kain <njkain at gmail dot com>
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without

View File

@ -481,10 +481,8 @@ static void change_listen_mode(struct client_state_t *cs, int new_mode)
cs->listenFd = new_mode == LM_RAW ? cs->listenFd = new_mode == LM_RAW ?
create_raw_listen_socket(cs, client_config.ifindex) : create_raw_listen_socket(cs, client_config.ifindex) :
create_udp_listen_socket(client_config.interface); create_udp_listen_socket(client_config.interface);
if (cs->listenFd < 0) { if (cs->listenFd < 0)
log_error("FATAL: Couldn't listen on socket: %s.", strerror(errno)); suicide("FATAL: Couldn't listen on socket: %s.", strerror(errno));
exit(EXIT_FAILURE);
}
epoll_add(cs->epollFd, cs->listenFd); epoll_add(cs->epollFd, cs->listenFd);
} }

View File

@ -44,39 +44,29 @@
static void get_duid_path(char *duidfile, size_t dlen) static void get_duid_path(char *duidfile, size_t dlen)
{ {
int splen = snprintf(duidfile, dlen, "%s/DUID", state_dir); int splen = snprintf(duidfile, dlen, "%s/DUID", state_dir);
if (splen < 0) { if (splen < 0)
log_line("%s: snprintf failed; return=%d", __func__, splen); suicide("%s: snprintf failed; return=%d", __func__, splen);
exit(EXIT_FAILURE); if ((size_t)splen >= dlen)
} suicide("%s: snprintf dest buffer too small %d >= %u",
if ((size_t)splen >= dlen) {
log_line("%s: snprintf dest buffer too small %d >= %u",
__func__, splen, sizeof dlen); __func__, splen, sizeof dlen);
exit(EXIT_FAILURE);
}
} }
static void get_iaid_path(char *iaidfile, size_t ilen, uint8_t *hwaddr, static void get_iaid_path(char *iaidfile, size_t ilen, uint8_t *hwaddr,
size_t hwaddrlen) size_t hwaddrlen)
{ {
if (hwaddrlen != 6) { if (hwaddrlen != 6)
log_line("%s: Hardware address length=%u != 6 bytes", suicide("%s: Hardware address length=%u != 6 bytes",
__func__, hwaddrlen); __func__, hwaddrlen);
exit(EXIT_FAILURE);
}
int splen = snprintf int splen = snprintf
(iaidfile, ilen, (iaidfile, ilen,
"%s/IAID-%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x", "%s/IAID-%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
state_dir, hwaddr[0], hwaddr[1], hwaddr[2], state_dir, hwaddr[0], hwaddr[1], hwaddr[2],
hwaddr[3], hwaddr[4], hwaddr[5]); hwaddr[3], hwaddr[4], hwaddr[5]);
if (splen < 0) { if (splen < 0)
log_line("%s: snprintf failed; return=%d", __func__, splen); suicide("%s: snprintf failed; return=%d", __func__, splen);
exit(EXIT_FAILURE); if ((size_t)splen >= ilen)
} suicide("%s: snprintf dest buffer too small %d >= %u",
if ((size_t)splen >= ilen) {
log_line("%s: snprintf dest buffer too small %d >= %u",
__func__, splen, sizeof ilen); __func__, splen, sizeof ilen);
exit(EXIT_FAILURE);
}
} }
static int open_duidfile_read(void) static int open_duidfile_read(void)
@ -96,11 +86,9 @@ static int open_duidfile_write(void)
char duidfile[PATH_MAX]; char duidfile[PATH_MAX];
get_duid_path(duidfile, sizeof duidfile); get_duid_path(duidfile, sizeof duidfile);
int fd = open(duidfile, O_WRONLY|O_TRUNC|O_CREAT, 0644); int fd = open(duidfile, O_WRONLY|O_TRUNC|O_CREAT, 0644);
if (fd < 0) { if (fd < 0)
log_line("Failed to open duidfile '%s' for writing: %s", suicide("Failed to open duidfile '%s' for writing: %s",
duidfile, strerror(errno)); duidfile, strerror(errno));
exit(EXIT_FAILURE);
}
return fd; return fd;
} }
@ -121,11 +109,9 @@ static int open_iaidfile_write(uint8_t *hwaddr, size_t hwaddrlen)
char iaidfile[PATH_MAX]; char iaidfile[PATH_MAX];
get_iaid_path(iaidfile, sizeof iaidfile, hwaddr, hwaddrlen); get_iaid_path(iaidfile, sizeof iaidfile, hwaddr, hwaddrlen);
int fd = open(iaidfile, O_WRONLY|O_TRUNC|O_CREAT, 0644); int fd = open(iaidfile, O_WRONLY|O_TRUNC|O_CREAT, 0644);
if (fd < 0) { if (fd < 0)
log_line("Failed to open iaidfile '%s' for writing: %s", suicide("Failed to open iaidfile '%s' for writing: %s",
iaidfile, strerror(errno)); iaidfile, strerror(errno));
exit(EXIT_FAILURE);
}
return fd; return fd;
} }
@ -138,10 +124,8 @@ static size_t generate_duid(struct nk_random_state_u32 *s, char *dest,
size_t dlen) size_t dlen)
{ {
const size_t tlen = sizeof(uint16_t) + 4 * sizeof(uint32_t); const size_t tlen = sizeof(uint16_t) + 4 * sizeof(uint32_t);
if (dlen < tlen) { if (dlen < tlen)
log_error("%s: dlen < %u", __func__, tlen); suicide("%s: dlen < %u", __func__, tlen);
exit(EXIT_FAILURE);
}
size_t off = 0; size_t off = 0;
uint16_t typefield = htons(4); uint16_t typefield = htons(4);
@ -161,10 +145,8 @@ static size_t generate_duid(struct nk_random_state_u32 *s, char *dest,
static size_t generate_iaid(struct nk_random_state_u32 *s, char *dest, static size_t generate_iaid(struct nk_random_state_u32 *s, char *dest,
size_t dlen) size_t dlen)
{ {
if (dlen < sizeof(uint32_t)) { if (dlen < sizeof(uint32_t))
log_error("%s: dlen < %u", __func__, sizeof(uint32_t)); suicide("%s: dlen < %u", __func__, sizeof(uint32_t));
exit(EXIT_FAILURE);
}
size_t off = 0; size_t off = 0;
uint32_t r32 = nk_random_u32(s); uint32_t r32 = nk_random_u32(s);
@ -188,11 +170,9 @@ void get_clientid(struct client_state_t *cs, struct client_config_t *cc)
iaid_len = generate_iaid(&cs->rnd32_state, iaid, sizeof iaid); iaid_len = generate_iaid(&cs->rnd32_state, iaid, sizeof iaid);
fd = open_iaidfile_write(cc->arp, sizeof cc->arp); fd = open_iaidfile_write(cc->arp, sizeof cc->arp);
int r = safe_write(fd, iaid, iaid_len); int r = safe_write(fd, iaid, iaid_len);
if (r < 0 || (size_t)r != iaid_len) { if (r < 0 || (size_t)r != iaid_len)
log_error("%s: (%s) failed to write generated IAID.", suicide("%s: (%s) failed to write generated IAID.",
cc->interface, __func__); cc->interface, __func__);
exit(EXIT_FAILURE);
}
} else { } else {
iaid_len = safe_read(fd, iaid, sizeof iaid); iaid_len = safe_read(fd, iaid, sizeof iaid);
} }
@ -203,11 +183,9 @@ void get_clientid(struct client_state_t *cs, struct client_config_t *cc)
duid_len = generate_duid(&cs->rnd32_state, duid, sizeof duid); duid_len = generate_duid(&cs->rnd32_state, duid, sizeof duid);
fd = open_duidfile_write(); fd = open_duidfile_write();
int r = safe_write(fd, duid, duid_len); int r = safe_write(fd, duid, duid_len);
if (r < 0 || (size_t)r != duid_len) { if (r < 0 || (size_t)r != duid_len)
log_error("%s: (%s) failed to write generated DUID.", suicide("%s: (%s) failed to write generated DUID.",
cc->interface, __func__); cc->interface, __func__);
exit(EXIT_FAILURE);
}
} else { } else {
duid_len = safe_read(fd, duid, sizeof duid); duid_len = safe_read(fd, duid, sizeof duid);
} }
@ -215,11 +193,9 @@ void get_clientid(struct client_state_t *cs, struct client_config_t *cc)
const uint8_t cid_type = 255; const uint8_t cid_type = 255;
size_t cdl = sizeof cid_type + iaid_len + duid_len; size_t cdl = sizeof cid_type + iaid_len + duid_len;
if (cdl > sizeof cc->clientid) { if (cdl > sizeof cc->clientid)
log_error("%s: (%s) clientid length %u > %u", suicide("%s: (%s) clientid length %u > %u",
cc->interface, __func__, cdl, sizeof cc->clientid); cc->interface, __func__, cdl, sizeof cc->clientid);
exit(EXIT_FAILURE);
}
uint8_t cid_len = 0; uint8_t cid_len = 0;
memcpy(cc->clientid + cid_len, &cid_type, sizeof cid_type); memcpy(cc->clientid + cid_len, &cid_type, sizeof cid_type);

View File

@ -316,9 +316,8 @@ static void inform_execute(char c)
} else if (r < 0) { } else if (r < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) if (errno == EAGAIN || errno == EWOULDBLOCK)
goto retry; goto retry;
log_line("%s: (%s) error writing to ifch -> ndhc pipe: %s", suicide("%s: (%s) error writing to ifch -> ndhc pipe: %s",
client_config.interface, __func__, strerror(errno)); client_config.interface, __func__, strerror(errno));
exit(EXIT_FAILURE);
} }
} }
@ -334,16 +333,14 @@ static void process_client_pipe(void)
} else if (r < 0) { } else if (r < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) if (errno == EAGAIN || errno == EWOULDBLOCK)
return; return;
log_line("%s: (%s) error reading from ndhc -> ifch pipe: %s", suicide("%s: (%s) error reading from ndhc -> ifch pipe: %s",
client_config.interface, __func__, strerror(errno)); client_config.interface, __func__, strerror(errno));
exit(EXIT_FAILURE);
} }
if (execute_buffer(buf) == -1) { if (execute_buffer(buf) == -1) {
log_line("%s: (%s) execute_buffer was passed invalid commands: '%s'",
client_config.interface, __func__, buf);
inform_execute('-'); inform_execute('-');
exit(EXIT_FAILURE); suicide("%s: (%s) execute_buffer was passed invalid commands: '%s'",
client_config.interface, __func__, buf);
} else } else
inform_execute('+'); inform_execute('+');
} }
@ -375,14 +372,12 @@ void do_ifch_work(void)
} }
for (int i = 0; i < r; ++i) { for (int i = 0; i < r; ++i) {
int fd = events[i].data.fd; int fd = events[i].data.fd;
if (fd == pToIfchR) { if (fd == pToIfchR)
process_client_pipe(); process_client_pipe();
} else if (fd == signalFd) { else if (fd == signalFd)
signal_dispatch(); signal_dispatch();
} else { else
log_line("ifch: unexpected fd while performing epoll"); suicide("ifch: unexpected fd while performing epoll");
exit(EXIT_FAILURE);
}
} }
} }
} }
@ -390,11 +385,9 @@ void do_ifch_work(void)
void ifch_main(void) void ifch_main(void)
{ {
prctl(PR_SET_NAME, "ndhc: ifch"); prctl(PR_SET_NAME, "ndhc: ifch");
if (file_exists(pidfile_ifch, "w") == -1) { if (file_exists(pidfile_ifch, "w") == -1)
log_line("FATAL - can't open ifch-pidfile '%s' for write!", suicide("FATAL - can't open ifch-pidfile '%s' for write!",
pidfile_ifch); pidfile_ifch);
exit(EXIT_FAILURE);
}
write_pid(pidfile_ifch); write_pid(pidfile_ifch);
memset(pidfile_ifch, '\0', sizeof pidfile_ifch); memset(pidfile_ifch, '\0', sizeof pidfile_ifch);

View File

@ -48,16 +48,12 @@ static void get_leasefile_path(char *leasefile, size_t dlen, char *ifname)
{ {
int splen = snprintf(leasefile, dlen, "%s/LEASE-%s", int splen = snprintf(leasefile, dlen, "%s/LEASE-%s",
state_dir, ifname); state_dir, ifname);
if (splen < 0) { if (splen < 0)
log_line("%s: (%s) snprintf failed; return=%d", suicide("%s: (%s) snprintf failed; return=%d",
client_config.interface, __func__, splen); client_config.interface, __func__, splen);
exit(EXIT_FAILURE); if ((size_t)splen >= dlen)
} suicide("%s: (%s) snprintf dest buffer too small %d >= %u",
if ((size_t)splen >= dlen) {
log_line("%s: (%s) snprintf dest buffer too small %d >= %u",
client_config.interface, __func__, splen, sizeof dlen); client_config.interface, __func__, splen, sizeof dlen);
exit(EXIT_FAILURE);
}
} }
void open_leasefile(void) void open_leasefile(void)
@ -65,11 +61,9 @@ void open_leasefile(void)
char leasefile[PATH_MAX]; char leasefile[PATH_MAX];
get_leasefile_path(leasefile, sizeof leasefile, client_config.interface); get_leasefile_path(leasefile, sizeof leasefile, client_config.interface);
leasefilefd = open(leasefile, O_WRONLY|O_TRUNC|O_CREAT, 0644); leasefilefd = open(leasefile, O_WRONLY|O_TRUNC|O_CREAT, 0644);
if (leasefilefd < 0) { if (leasefilefd < 0)
log_line("%s: Failed to create lease file '%s': %s", suicide("%s: Failed to create lease file '%s': %s",
client_config.interface, leasefile, strerror(errno)); client_config.interface, leasefile, strerror(errno));
exit(EXIT_FAILURE);
}
} }
void write_leasefile(struct in_addr ipnum) void write_leasefile(struct in_addr ipnum)

View File

@ -180,8 +180,7 @@ static void signal_dispatch(void)
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
break; break;
case SIGCHLD: case SIGCHLD:
log_line("ndhc-master: Subprocess terminated unexpectedly. Exiting."); suicide("ndhc-master: Subprocess terminated unexpectedly. Exiting.");
exit(EXIT_FAILURE);
break; break;
case SIGTERM: case SIGTERM:
log_line("Received SIGTERM. Exiting gracefully."); log_line("Received SIGTERM. Exiting gracefully.");
@ -228,21 +227,14 @@ static int get_clientid_string(char *str, size_t slen)
static void fail_if_state_dir_dne(void) static void fail_if_state_dir_dne(void)
{ {
if (strlen(state_dir) == 0) { if (strlen(state_dir) == 0)
log_error("state_dir path is empty; it must be specified"); suicide("state_dir path is empty; it must be specified");
exit(EXIT_FAILURE);
}
struct stat st; struct stat st;
if (stat(state_dir, &st) < 0) { if (stat(state_dir, &st) < 0)
log_error("failed to stat state_dir path '%s': %s", suicide("failed to stat state_dir path '%s': %s",
state_dir, strerror(errno)); state_dir, strerror(errno));
exit(EXIT_FAILURE); if (!S_ISDIR(st.st_mode))
} suicide("state_dir path '%s' does not specify a directory", state_dir);
if (!S_ISDIR(st.st_mode)) {
log_error("state_dir path '%s' does not specify a directory",
state_dir);
exit(EXIT_FAILURE);
}
} }
static void handle_ifch_message(void) static void handle_ifch_message(void)
@ -255,9 +247,8 @@ static void handle_ifch_message(void)
} else if (r < 0) { } else if (r < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) if (errno == EAGAIN || errno == EWOULDBLOCK)
return; return;
log_line("%s: (%s) error reading from ifch -> ndhc pipe: %s", suicide("%s: (%s) error reading from ifch -> ndhc pipe: %s",
client_config.interface, __func__, strerror(errno)); client_config.interface, __func__, strerror(errno));
exit(EXIT_FAILURE);
} }
if (c == '+') if (c == '+')
@ -353,18 +344,14 @@ static void create_ipc_pipes(void) {
int niPipe[2]; int niPipe[2];
int inPipe[2]; int inPipe[2];
if (pipe2(niPipe, O_NONBLOCK)) { if (pipe2(niPipe, O_NONBLOCK))
log_line("FATAL - can't create ndhc -> ndhc-ifch pipe: %s", suicide("FATAL - can't create ndhc -> ndhc-ifch pipe: %s",
strerror(errno)); strerror(errno));
exit(EXIT_FAILURE);
}
pToNdhcR = niPipe[0]; pToNdhcR = niPipe[0];
pToNdhcW = niPipe[1]; pToNdhcW = niPipe[1];
if (pipe2(inPipe, O_NONBLOCK)) { if (pipe2(inPipe, O_NONBLOCK))
log_line("FATAL - can't create ndhc-ifch -> ndhc pipe: %s", suicide("FATAL - can't create ndhc-ifch -> ndhc pipe: %s",
strerror(errno)); strerror(errno));
exit(EXIT_FAILURE);
}
pToIfchR = inPipe[0]; pToIfchR = inPipe[0];
pToIfchW = inPipe[1]; pToIfchW = inPipe[1];
} }
@ -374,16 +361,13 @@ static void ndhc_main(void) {
log_line("ndhc client " NDHC_VERSION " started on interface [%s].", log_line("ndhc client " NDHC_VERSION " started on interface [%s].",
client_config.interface); client_config.interface);
if ((cs.nlFd = nl_open(NETLINK_ROUTE, RTMGRP_LINK, &cs.nlPortId)) < 0) { if ((cs.nlFd = nl_open(NETLINK_ROUTE, RTMGRP_LINK, &cs.nlPortId)) < 0)
log_line("FATAL - failed to open netlink socket"); suicide("%s: failed to open netlink socket", __func__);
exit(EXIT_FAILURE);
}
if (client_config.foreground && !client_config.background_if_no_lease) { if (client_config.foreground && !client_config.background_if_no_lease) {
if (file_exists(pidfile, "w") == -1) { if (file_exists(pidfile, "w") == -1)
log_line("FATAL - can't open pidfile '%s' for write!", pidfile); suicide("%s: can't open pidfile '%s' for write!",
exit(EXIT_FAILURE); __func__, pidfile);
}
write_pid(pidfile); write_pid(pidfile);
} }
@ -412,7 +396,7 @@ void background(void)
} }
} }
if (file_exists(pidfile, "w") == -1) { if (file_exists(pidfile, "w") == -1) {
log_line("Cannot open pidfile for write!"); log_warning("Cannot open pidfile for write!");
} else } else
write_pid(pidfile); write_pid(pidfile);
} }
@ -497,10 +481,8 @@ int main(int argc, char **argv)
if (pwd) { if (pwd) {
ndhc_uid = (int)pwd->pw_uid; ndhc_uid = (int)pwd->pw_uid;
ndhc_gid = (int)pwd->pw_gid; ndhc_gid = (int)pwd->pw_gid;
} else { } else
printf("Bad username provided to '-u'.\n"); suicide("Bad username provided to '-u'.");
exit(EXIT_FAILURE);
}
break; break;
} }
case 'U': { case 'U': {
@ -514,10 +496,8 @@ int main(int argc, char **argv)
if (pwd) { if (pwd) {
ifch_uid = (int)pwd->pw_uid; ifch_uid = (int)pwd->pw_uid;
ifch_gid = (int)pwd->pw_gid; ifch_gid = (int)pwd->pw_gid;
} else { } else
printf("Bad username provided to '-U'.\n"); suicide("Bad username provided to '-U'.");
exit(EXIT_FAILURE);
}
break; break;
} }
case 'C': case 'C':
@ -588,15 +568,10 @@ int main(int argc, char **argv)
case 't': { case 't': {
char *p; char *p;
long mt = strtol(optarg, &p, 10); long mt = strtol(optarg, &p, 10);
if (p == optarg) { if (p == optarg)
log_error("gw-metric arg '%s' isn't a valid number", suicide("gw-metric arg '%s' isn't a valid number", optarg);
optarg); if (mt > INT_MAX)
exit(EXIT_FAILURE); suicide("gw-metric arg '%s' is too large", optarg);
}
if (mt > INT_MAX) {
log_error("gw-metric arg '%s' is too large", optarg);
exit(EXIT_FAILURE);
}
if (mt < 0) if (mt < 0)
mt = 0; mt = 0;
client_config.metric = (int)mt; client_config.metric = (int)mt;
@ -617,15 +592,13 @@ int main(int argc, char **argv)
nk_random_u32_init(&cs.rnd32_state); nk_random_u32_init(&cs.rnd32_state);
if (getuid()) if (getuid())
suicide("FATAL - I need to be started as root."); suicide("I need to be started as root.");
if (!strncmp(chroot_dir, "", sizeof chroot_dir)) if (!strncmp(chroot_dir, "", sizeof chroot_dir))
suicide("FATAL - No chroot path specified. Refusing to run."); suicide("No chroot path is specified. Refusing to run.");
fail_if_state_dir_dne(); fail_if_state_dir_dne();
if (nl_getifdata() < 0) { if (nl_getifdata() < 0)
log_line("FATAL - failed to get interface MAC or index"); suicide("failed to get interface MAC or index");
exit(EXIT_FAILURE);
}
get_clientid(&cs, &client_config); get_clientid(&cs, &client_config);
@ -635,8 +608,7 @@ int main(int argc, char **argv)
case 0: case 0:
break; break;
default: default:
log_error("FATAL - failed to set the interface to up state"); suicide("failed to set the interface to up state");
exit(EXIT_FAILURE);
} }
create_ipc_pipes(); create_ipc_pipes();
@ -651,10 +623,8 @@ int main(int argc, char **argv)
close(pToIfchR); close(pToIfchR);
close(pToNdhcW); close(pToNdhcW);
ndhc_main(); ndhc_main();
} else { } else
log_line("FATAL - failed to fork ndhc-ifch: %s", strerror(errno)); suicide("failed to fork ndhc-ifch: %s", strerror(errno));
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }

View File

@ -303,10 +303,8 @@ static void selecting_timeout(struct client_state_t *cs, long long nowts)
log_line("No lease, going to background."); log_line("No lease, going to background.");
cs->init = 0; cs->init = 0;
background(); background();
} else if (client_config.abort_if_no_lease) { } else if (client_config.abort_if_no_lease)
log_line("No lease, failing."); suicide("No lease, failing.");
exit(EXIT_FAILURE);
}
} }
if (num_dhcp_requests == 0) if (num_dhcp_requests == 0)
cs->xid = nk_random_u32(&cs->rnd32_state); cs->xid = nk_random_u32(&cs->rnd32_state);