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
*
* 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.
*
* 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 ?
create_raw_listen_socket(cs, client_config.ifindex) :
create_udp_listen_socket(client_config.interface);
if (cs->listenFd < 0) {
log_error("FATAL: Couldn't listen on socket: %s.", strerror(errno));
exit(EXIT_FAILURE);
}
if (cs->listenFd < 0)
suicide("FATAL: Couldn't listen on socket: %s.", strerror(errno));
epoll_add(cs->epollFd, cs->listenFd);
}

View File

@ -44,39 +44,29 @@
static void get_duid_path(char *duidfile, size_t dlen)
{
int splen = snprintf(duidfile, dlen, "%s/DUID", state_dir);
if (splen < 0) {
log_line("%s: snprintf failed; return=%d", __func__, splen);
exit(EXIT_FAILURE);
}
if ((size_t)splen >= dlen) {
log_line("%s: snprintf dest buffer too small %d >= %u",
__func__, splen, sizeof dlen);
exit(EXIT_FAILURE);
}
if (splen < 0)
suicide("%s: snprintf failed; return=%d", __func__, splen);
if ((size_t)splen >= dlen)
suicide("%s: snprintf dest buffer too small %d >= %u",
__func__, splen, sizeof dlen);
}
static void get_iaid_path(char *iaidfile, size_t ilen, uint8_t *hwaddr,
size_t hwaddrlen)
{
if (hwaddrlen != 6) {
log_line("%s: Hardware address length=%u != 6 bytes",
__func__, hwaddrlen);
exit(EXIT_FAILURE);
}
if (hwaddrlen != 6)
suicide("%s: Hardware address length=%u != 6 bytes",
__func__, hwaddrlen);
int splen = snprintf
(iaidfile, ilen,
"%s/IAID-%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
state_dir, hwaddr[0], hwaddr[1], hwaddr[2],
hwaddr[3], hwaddr[4], hwaddr[5]);
if (splen < 0) {
log_line("%s: snprintf failed; return=%d", __func__, splen);
exit(EXIT_FAILURE);
}
if ((size_t)splen >= ilen) {
log_line("%s: snprintf dest buffer too small %d >= %u",
__func__, splen, sizeof ilen);
exit(EXIT_FAILURE);
}
if (splen < 0)
suicide("%s: snprintf failed; return=%d", __func__, splen);
if ((size_t)splen >= ilen)
suicide("%s: snprintf dest buffer too small %d >= %u",
__func__, splen, sizeof ilen);
}
static int open_duidfile_read(void)
@ -96,11 +86,9 @@ static int open_duidfile_write(void)
char duidfile[PATH_MAX];
get_duid_path(duidfile, sizeof duidfile);
int fd = open(duidfile, O_WRONLY|O_TRUNC|O_CREAT, 0644);
if (fd < 0) {
log_line("Failed to open duidfile '%s' for writing: %s",
duidfile, strerror(errno));
exit(EXIT_FAILURE);
}
if (fd < 0)
suicide("Failed to open duidfile '%s' for writing: %s",
duidfile, strerror(errno));
return fd;
}
@ -121,11 +109,9 @@ static int open_iaidfile_write(uint8_t *hwaddr, size_t hwaddrlen)
char iaidfile[PATH_MAX];
get_iaid_path(iaidfile, sizeof iaidfile, hwaddr, hwaddrlen);
int fd = open(iaidfile, O_WRONLY|O_TRUNC|O_CREAT, 0644);
if (fd < 0) {
log_line("Failed to open iaidfile '%s' for writing: %s",
iaidfile, strerror(errno));
exit(EXIT_FAILURE);
}
if (fd < 0)
suicide("Failed to open iaidfile '%s' for writing: %s",
iaidfile, strerror(errno));
return fd;
}
@ -138,10 +124,8 @@ static size_t generate_duid(struct nk_random_state_u32 *s, char *dest,
size_t dlen)
{
const size_t tlen = sizeof(uint16_t) + 4 * sizeof(uint32_t);
if (dlen < tlen) {
log_error("%s: dlen < %u", __func__, tlen);
exit(EXIT_FAILURE);
}
if (dlen < tlen)
suicide("%s: dlen < %u", __func__, tlen);
size_t off = 0;
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,
size_t dlen)
{
if (dlen < sizeof(uint32_t)) {
log_error("%s: dlen < %u", __func__, sizeof(uint32_t));
exit(EXIT_FAILURE);
}
if (dlen < sizeof(uint32_t))
suicide("%s: dlen < %u", __func__, sizeof(uint32_t));
size_t off = 0;
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);
fd = open_iaidfile_write(cc->arp, sizeof cc->arp);
int r = safe_write(fd, iaid, iaid_len);
if (r < 0 || (size_t)r != iaid_len) {
log_error("%s: (%s) failed to write generated IAID.",
cc->interface, __func__);
exit(EXIT_FAILURE);
}
if (r < 0 || (size_t)r != iaid_len)
suicide("%s: (%s) failed to write generated IAID.",
cc->interface, __func__);
} else {
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);
fd = open_duidfile_write();
int r = safe_write(fd, duid, duid_len);
if (r < 0 || (size_t)r != duid_len) {
log_error("%s: (%s) failed to write generated DUID.",
cc->interface, __func__);
exit(EXIT_FAILURE);
}
if (r < 0 || (size_t)r != duid_len)
suicide("%s: (%s) failed to write generated DUID.",
cc->interface, __func__);
} else {
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;
size_t cdl = sizeof cid_type + iaid_len + duid_len;
if (cdl > sizeof cc->clientid) {
log_error("%s: (%s) clientid length %u > %u",
cc->interface, __func__, cdl, sizeof cc->clientid);
exit(EXIT_FAILURE);
}
if (cdl > sizeof cc->clientid)
suicide("%s: (%s) clientid length %u > %u",
cc->interface, __func__, cdl, sizeof cc->clientid);
uint8_t cid_len = 0;
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) {
if (errno == EAGAIN || errno == EWOULDBLOCK)
goto retry;
log_line("%s: (%s) error writing to ifch -> ndhc pipe: %s",
client_config.interface, __func__, strerror(errno));
exit(EXIT_FAILURE);
suicide("%s: (%s) error writing to ifch -> ndhc pipe: %s",
client_config.interface, __func__, strerror(errno));
}
}
@ -334,16 +333,14 @@ static void process_client_pipe(void)
} else if (r < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK)
return;
log_line("%s: (%s) error reading from ndhc -> ifch pipe: %s",
client_config.interface, __func__, strerror(errno));
exit(EXIT_FAILURE);
suicide("%s: (%s) error reading from ndhc -> ifch pipe: %s",
client_config.interface, __func__, strerror(errno));
}
if (execute_buffer(buf) == -1) {
log_line("%s: (%s) execute_buffer was passed invalid commands: '%s'",
client_config.interface, __func__, buf);
inform_execute('-');
exit(EXIT_FAILURE);
suicide("%s: (%s) execute_buffer was passed invalid commands: '%s'",
client_config.interface, __func__, buf);
} else
inform_execute('+');
}
@ -375,14 +372,12 @@ void do_ifch_work(void)
}
for (int i = 0; i < r; ++i) {
int fd = events[i].data.fd;
if (fd == pToIfchR) {
if (fd == pToIfchR)
process_client_pipe();
} else if (fd == signalFd) {
else if (fd == signalFd)
signal_dispatch();
} else {
log_line("ifch: unexpected fd while performing epoll");
exit(EXIT_FAILURE);
}
else
suicide("ifch: unexpected fd while performing epoll");
}
}
}
@ -390,11 +385,9 @@ void do_ifch_work(void)
void ifch_main(void)
{
prctl(PR_SET_NAME, "ndhc: ifch");
if (file_exists(pidfile_ifch, "w") == -1) {
log_line("FATAL - can't open ifch-pidfile '%s' for write!",
pidfile_ifch);
exit(EXIT_FAILURE);
}
if (file_exists(pidfile_ifch, "w") == -1)
suicide("FATAL - can't open ifch-pidfile '%s' for write!",
pidfile_ifch);
write_pid(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",
state_dir, ifname);
if (splen < 0) {
log_line("%s: (%s) snprintf failed; return=%d",
client_config.interface, __func__, splen);
exit(EXIT_FAILURE);
}
if ((size_t)splen >= dlen) {
log_line("%s: (%s) snprintf dest buffer too small %d >= %u",
client_config.interface, __func__, splen, sizeof dlen);
exit(EXIT_FAILURE);
}
if (splen < 0)
suicide("%s: (%s) snprintf failed; return=%d",
client_config.interface, __func__, splen);
if ((size_t)splen >= dlen)
suicide("%s: (%s) snprintf dest buffer too small %d >= %u",
client_config.interface, __func__, splen, sizeof dlen);
}
void open_leasefile(void)
@ -65,11 +61,9 @@ void open_leasefile(void)
char leasefile[PATH_MAX];
get_leasefile_path(leasefile, sizeof leasefile, client_config.interface);
leasefilefd = open(leasefile, O_WRONLY|O_TRUNC|O_CREAT, 0644);
if (leasefilefd < 0) {
log_line("%s: Failed to create lease file '%s': %s",
client_config.interface, leasefile, strerror(errno));
exit(EXIT_FAILURE);
}
if (leasefilefd < 0)
suicide("%s: Failed to create lease file '%s': %s",
client_config.interface, leasefile, strerror(errno));
}
void write_leasefile(struct in_addr ipnum)

View File

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