summaryrefslogtreecommitdiff
path: root/sbin/dhclient/privsep.c
diff options
context:
space:
mode:
authorKenneth R Westerback <krw@cvs.openbsd.org>2005-08-07 01:35:12 +0000
committerKenneth R Westerback <krw@cvs.openbsd.org>2005-08-07 01:35:12 +0000
commit63f76296bf7e1d14577f3377c4f349a828da94ea (patch)
tree04ceed52cbd5a3f236ad2f08373901239254683e /sbin/dhclient/privsep.c
parente307578c870485471cf5b4e3b01663435d5376af (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/privsep.c')
-rw-r--r--sbin/dhclient/privsep.c37
1 files changed, 15 insertions, 22 deletions
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);