diff options
author | Kenneth R Westerback <krw@cvs.openbsd.org> | 2005-08-07 01:35:12 +0000 |
---|---|---|
committer | Kenneth R Westerback <krw@cvs.openbsd.org> | 2005-08-07 01:35:12 +0000 |
commit | 63f76296bf7e1d14577f3377c4f349a828da94ea (patch) | |
tree | 04ceed52cbd5a3f236ad2f08373901239254683e /sbin/dhclient | |
parent | e307578c870485471cf5b4e3b01663435d5376af (diff) |
Simplify code by calling error() from inside buf_* functions rather
than always testing the return code and calling error() from the
caller.
From a diff by moritz@, ok henning@.
Diffstat (limited to 'sbin/dhclient')
-rw-r--r-- | sbin/dhclient/dhclient.c | 65 | ||||
-rw-r--r-- | sbin/dhclient/privsep.c | 37 | ||||
-rw-r--r-- | sbin/dhclient/privsep.h | 8 |
3 files changed, 43 insertions, 67 deletions
diff --git a/sbin/dhclient/dhclient.c b/sbin/dhclient/dhclient.c index 61688787cc2..df41c45c07c 100644 --- a/sbin/dhclient/dhclient.c +++ b/sbin/dhclient/dhclient.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dhclient.c,v 1.77 2005/08/02 02:34:03 krw Exp $ */ +/* $OpenBSD: dhclient.c,v 1.78 2005/08/07 01:35:11 krw Exp $ */ /* * Copyright 2004 Henning Brauer <henning@openbsd.org> @@ -1685,7 +1685,6 @@ script_init(char *reason, struct string_list *medium) size_t len, mediumlen = 0; struct imsg_hdr hdr; struct buf *buf; - int errs; if (medium != NULL && medium->string != NULL) mediumlen = strlen(medium->string); @@ -1695,23 +1694,17 @@ script_init(char *reason, struct string_list *medium) sizeof(size_t) + mediumlen + sizeof(size_t) + strlen(reason); - if ((buf = buf_open(hdr.len)) == NULL) - error("buf_open: %m"); + buf = buf_open(hdr.len); - errs = 0; - errs += buf_add(buf, &hdr, sizeof(hdr)); - errs += buf_add(buf, &mediumlen, sizeof(mediumlen)); + buf_add(buf, &hdr, sizeof(hdr)); + buf_add(buf, &mediumlen, sizeof(mediumlen)); if (mediumlen > 0) - errs += buf_add(buf, medium->string, mediumlen); + buf_add(buf, medium->string, mediumlen); len = strlen(reason); - errs += buf_add(buf, &len, sizeof(len)); - errs += buf_add(buf, reason, len); + buf_add(buf, &len, sizeof(len)); + buf_add(buf, reason, len); - if (errs) - error("buf_add: %m"); - - if (buf_close(privfd, buf) == -1) - error("buf_close: %m"); + buf_close(privfd, buf); } void @@ -1859,7 +1852,7 @@ script_write_params(char *prefix, struct client_lease *lease) size_t fn_len = 0, sn_len = 0, pr_len = 0; struct imsg_hdr hdr; struct buf *buf; - int errs, i; + int i; if (lease->filename != NULL) fn_len = strlen(lease->filename); @@ -1878,31 +1871,25 @@ script_write_params(char *prefix, struct client_lease *lease) scripttime = time(NULL); - if ((buf = buf_open(hdr.len)) == NULL) - error("buf_open: %m"); + buf = buf_open(hdr.len); - errs = 0; - errs += buf_add(buf, &hdr, sizeof(hdr)); - errs += buf_add(buf, lease, sizeof(struct client_lease)); - errs += buf_add(buf, &fn_len, sizeof(fn_len)); - errs += buf_add(buf, lease->filename, fn_len); - errs += buf_add(buf, &sn_len, sizeof(sn_len)); - errs += buf_add(buf, lease->server_name, sn_len); - errs += buf_add(buf, &pr_len, sizeof(pr_len)); - errs += buf_add(buf, prefix, pr_len); + buf_add(buf, &hdr, sizeof(hdr)); + buf_add(buf, lease, sizeof(struct client_lease)); + buf_add(buf, &fn_len, sizeof(fn_len)); + buf_add(buf, lease->filename, fn_len); + buf_add(buf, &sn_len, sizeof(sn_len)); + buf_add(buf, lease->server_name, sn_len); + buf_add(buf, &pr_len, sizeof(pr_len)); + buf_add(buf, prefix, pr_len); for (i = 0; i < 256; i++) { - errs += buf_add(buf, &lease->options[i].len, + buf_add(buf, &lease->options[i].len, sizeof(lease->options[i].len)); - errs += buf_add(buf, lease->options[i].data, + buf_add(buf, lease->options[i].data, lease->options[i].len); } - if (errs) - error("buf_add: %m"); - - if (buf_close(privfd, buf) == -1) - error("buf_close: %m"); + buf_close(privfd, buf); } int @@ -1917,14 +1904,10 @@ script_go(void) hdr.code = IMSG_SCRIPT_GO; hdr.len = sizeof(struct imsg_hdr); - if ((buf = buf_open(hdr.len)) == NULL) - error("buf_open: %m"); - - if (buf_add(buf, &hdr, sizeof(hdr))) - error("buf_add: %m"); + buf = buf_open(hdr.len); - if (buf_close(privfd, buf) == -1) - error("buf_close: %m"); + buf_add(buf, &hdr, sizeof(hdr)); + buf_close(privfd, buf); bzero(&hdr, sizeof(hdr)); buf_read(privfd, &hdr, sizeof(hdr)); diff --git a/sbin/dhclient/privsep.c b/sbin/dhclient/privsep.c index 6606b3b2784..ab018063c7d 100644 --- a/sbin/dhclient/privsep.c +++ b/sbin/dhclient/privsep.c @@ -1,4 +1,4 @@ -/* $OpenBSD: privsep.c,v 1.9 2005/08/04 14:21:04 henning Exp $ */ +/* $OpenBSD: privsep.c,v 1.10 2005/08/07 01:35:11 krw Exp $ */ /* * Copyright (c) 2004 Henning Brauer <henning@openbsd.org> @@ -25,31 +25,30 @@ buf_open(size_t len) struct buf *buf; if ((buf = calloc(1, sizeof(struct buf))) == NULL) - return (NULL); + error("buf_open: %m"); if ((buf->buf = malloc(len)) == NULL) { free(buf); - return (NULL); + error("buf_open: %m"); } buf->size = len; return (buf); } -int +void buf_add(struct buf *buf, void *data, size_t len) { if (len == 0) - return (0); + return; if (buf->wpos + len > buf->size) - return (-1); + error("buf_add: %m"); memcpy(buf->buf + buf->wpos, data, len); buf->wpos += len; - return (0); } -int +void buf_close(int sock, struct buf *buf) { ssize_t n; @@ -58,19 +57,18 @@ buf_close(int sock, struct buf *buf) n = write(sock, buf->buf + buf->rpos, buf->size - buf->rpos); if (n == 0) { /* connection closed */ errno = 0; - return (-1); + error("buf_close (connection closed): %m"); } if (n != -1 && n < buf->size - buf->rpos) - error("short write"); + error("buf_close (short write): %m"); } while (n == -1 && (errno == EAGAIN || errno == EINTR)); free(buf->buf); free(buf); - return (n); } -ssize_t +void buf_read(int sock, void *buf, size_t nbytes) { ssize_t n; @@ -78,15 +76,13 @@ buf_read(int sock, void *buf, size_t nbytes) do { n = read(sock, buf, nbytes); if (n == 0) - error("connection closed"); + error("buf_read (connection closed): %m"); if (n != -1 && n < nbytes) - error("short read"); + error("buf_read (short read): %m"); } while (n == -1 && (errno == EINTR || errno == EAGAIN)); if (n == -1) error("buf_read: %m"); - - return (n); } void @@ -214,12 +210,9 @@ dispatch_imsg(int fd) hdr.len = sizeof(struct imsg_hdr) + sizeof(int); if ((buf = buf_open(hdr.len)) == NULL) error("buf_open: %m"); - if (buf_add(buf, &hdr, sizeof(hdr))) - error("buf_add: %m"); - if (buf_add(buf, &ret, sizeof(ret))) - error("buf_add: %m"); - if (buf_close(fd, buf) == -1) - error("buf_close: %m"); + buf_add(buf, &hdr, sizeof(hdr)); + buf_add(buf, &ret, sizeof(ret)); + buf_close(fd, buf); break; default: error("received unknown message, code %d", hdr.code); diff --git a/sbin/dhclient/privsep.h b/sbin/dhclient/privsep.h index 6828ea21ba6..85fb71ba22e 100644 --- a/sbin/dhclient/privsep.h +++ b/sbin/dhclient/privsep.h @@ -1,4 +1,4 @@ -/* $OpenBSD: privsep.h,v 1.3 2005/08/05 16:23:30 moritz Exp $ */ +/* $OpenBSD: privsep.h,v 1.4 2005/08/07 01:35:11 krw Exp $ */ /* * Copyright (c) 2004 Henning Brauer <henning@openbsd.org> @@ -42,7 +42,7 @@ struct imsg_hdr { }; struct buf *buf_open(size_t); -int buf_add(struct buf *, void *, size_t); -int buf_close(int, struct buf *); -ssize_t buf_read(int, void *, size_t); +void buf_add(struct buf *, void *, size_t); +void buf_close(int, struct buf *); +void buf_read(int, void *, size_t); void dispatch_imsg(int); |