summaryrefslogtreecommitdiff
path: root/usr.sbin
diff options
context:
space:
mode:
authorHenning Brauer <henning@cvs.openbsd.org>2003-12-24 13:28:03 +0000
committerHenning Brauer <henning@cvs.openbsd.org>2003-12-24 13:28:03 +0000
commitb9f919ad8e4bd53a01b04a664c619a8833fe4eea (patch)
treebaf977705fd3ead7ef8d3c29bd377d9e0dd5dad4 /usr.sbin
parentf924fc5687fcce16de3cbe026d2266242ff50a73 (diff)
handle write() returning 0 correctly, adjust the msgbuf API appropriately,
and make all callers cope.
Diffstat (limited to 'usr.sbin')
-rw-r--r--usr.sbin/bgpd/bgpd.c10
-rw-r--r--usr.sbin/bgpd/buffer.c20
-rw-r--r--usr.sbin/bgpd/imsg.c6
-rw-r--r--usr.sbin/bgpd/mrt.c6
-rw-r--r--usr.sbin/bgpd/rde.c7
-rw-r--r--usr.sbin/bgpd/session.c43
6 files changed, 54 insertions, 38 deletions
diff --git a/usr.sbin/bgpd/bgpd.c b/usr.sbin/bgpd/bgpd.c
index 346f94cc78a..c725ce9b971 100644
--- a/usr.sbin/bgpd/bgpd.c
+++ b/usr.sbin/bgpd/bgpd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: bgpd.c,v 1.21 2003/12/23 19:14:49 deraadt Exp $ */
+/* $OpenBSD: bgpd.c,v 1.22 2003/12/24 13:28:01 henning Exp $ */
/*
* Copyright (c) 2003 Henning Brauer <henning@openbsd.org>
@@ -221,11 +221,11 @@ main(int argc, char *argv[])
fatal("poll error", errno);
if (nfds > 0 && (pfd[PFD_PIPE_SESSION].revents & POLLOUT))
- if ((n = msgbuf_write(&ibuf_se.w)) == -1)
+ if ((n = msgbuf_write(&ibuf_se.w)) < 0)
fatal("pipe write error", errno);
if (nfds > 0 && (pfd[PFD_PIPE_ROUTE].revents & POLLOUT))
- if ((n = msgbuf_write(&ibuf_rde.w)) == -1)
+ if ((n = msgbuf_write(&ibuf_rde.w)) < 0)
fatal("pipe write error", errno);
if (nfds > 0 && pfd[PFD_PIPE_SESSION].revents & POLLIN) {
@@ -240,7 +240,7 @@ main(int argc, char *argv[])
for (j = PFD_MRT_START; j < i && nfds > 0 ; j++) {
if (pfd[j].revents & POLLOUT) {
- if ((n = msgbuf_write(&mrt[i]->msgbuf)) == -1)
+ if ((n = msgbuf_write(&mrt[i]->msgbuf)) < 0)
fatal("pipe write error", errno);
}
}
@@ -341,7 +341,7 @@ dispatch_imsg(struct imsgbuf *ibuf, int idx, struct mrt_config *conf)
fatal("buf_open error", 0);
if (buf_add(wbuf, imsg.data, len) == -1)
fatal("buf_add error", 0);
- if ((n = buf_close(&m->msgbuf, wbuf)) == -1)
+ if ((n = buf_close(&m->msgbuf, wbuf)) < 0)
fatal("buf_close error", 0);
break;
}
diff --git a/usr.sbin/bgpd/buffer.c b/usr.sbin/bgpd/buffer.c
index 71db8737494..3cb2df56b38 100644
--- a/usr.sbin/bgpd/buffer.c
+++ b/usr.sbin/bgpd/buffer.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: buffer.c,v 1.8 2003/12/23 19:14:49 deraadt Exp $ */
+/* $OpenBSD: buffer.c,v 1.9 2003/12/24 13:28:01 henning Exp $ */
/*
* Copyright (c) 2003 Henning Brauer <henning@openbsd.org>
@@ -78,8 +78,8 @@ buf_close(struct msgbuf *msgbuf, struct buf *buf)
/* first try to write out directly */
if (msgbuf->queued == 0) {
- if ((n = buf_write(msgbuf->sock, buf)) == -1)
- return (-1);
+ if ((n = buf_write(msgbuf->sock, buf)) < 0)
+ return (n);
if (n == 1) { /* all data written out */
buf_free(buf);
@@ -101,14 +101,13 @@ buf_write(int sock, struct buf *buf)
buf->size-buf->rpos)) == -1) {
if (errno == EAGAIN) /* cannot write immediately */
return (0);
- else {
- /* XXX better let caller log with info which sock etc */
- logit(LOG_CRIT, "buf_write: write error: %s",
- strerror(errno));
+ else
return (-1);
- }
}
+ if (n == 0) /* connection closed */
+ return (-2);
+
if (n < buf->size - buf->rpos) { /* not all data written yet */
buf->rpos += n;
return (0);
@@ -158,8 +157,9 @@ msgbuf_write(struct msgbuf *msgbuf)
for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL; buf = next) {
next = TAILQ_NEXT(buf, entries);
- if ((n = buf_write(msgbuf->sock, buf)) == -1)
- return (-1);
+ if ((n = buf_write(msgbuf->sock, buf)) < 0)
+ return (n);
+
if (n == 1) /* everything written out */
buf_dequeue(msgbuf, buf);
else
diff --git a/usr.sbin/bgpd/imsg.c b/usr.sbin/bgpd/imsg.c
index 0424062d18c..fbaa7c1a55e 100644
--- a/usr.sbin/bgpd/imsg.c
+++ b/usr.sbin/bgpd/imsg.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: imsg.c,v 1.8 2003/12/21 23:28:39 henning Exp $ */
+/* $OpenBSD: imsg.c,v 1.9 2003/12/24 13:28:02 henning Exp $ */
/*
* Copyright (c) 2003 Henning Brauer <henning@openbsd.org>
@@ -111,8 +111,8 @@ imsg_compose(struct imsgbuf *ibuf, int type, u_int32_t peerid, void *data,
if (datalen)
if (buf_add(wbuf, data, datalen) == -1)
fatal("imsg_compose: buf_add error", 0);
- if ((n = buf_close(&ibuf->w, wbuf)) == -1)
- fatal("imsg_compose: buf_close error", 0);
+ if ((n = buf_close(&ibuf->w, wbuf)) < 0)
+ fatal("imsg_compose: buf_close error", errno);
return (n);
}
diff --git a/usr.sbin/bgpd/mrt.c b/usr.sbin/bgpd/mrt.c
index 414dc9016a1..4c24c0f62a0 100644
--- a/usr.sbin/bgpd/mrt.c
+++ b/usr.sbin/bgpd/mrt.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: mrt.c,v 1.9 2003/12/23 19:14:49 deraadt Exp $ */
+/* $OpenBSD: mrt.c,v 1.10 2003/12/24 13:28:02 henning Exp $ */
/*
* Copyright (c) 2003 Claudio Jeker <claudio@openbsd.org>
@@ -113,7 +113,7 @@ mrt_dump_bgp_msg(struct mrt *mrt, void *pkg, u_int16_t pkglen, int type,
if (buf_add(buf, pkg, pkglen) == -1)
fatal("buf_add error", 0);
- if ((n = buf_close(mrt->msgbuf, buf)) == -1)
+ if ((n = buf_close(mrt->msgbuf, buf)) < 0)
fatal("buf_close error", 0);
return (n);
@@ -159,7 +159,7 @@ mrt_dump_entry(struct mrt *mrt, struct prefix *p, u_int16_t snum,
if (attr_dump(bptr, attr_len, &p->aspath->flags) == -1)
fatal("attr_dump error", 0);
- if ((n = buf_close(mrt->msgbuf, buf)) == -1)
+ if ((n = buf_close(mrt->msgbuf, buf)) < 0)
fatal("buf_close error", 0);
return (n);
diff --git a/usr.sbin/bgpd/rde.c b/usr.sbin/bgpd/rde.c
index e7f8352b879..16af16b34e0 100644
--- a/usr.sbin/bgpd/rde.c
+++ b/usr.sbin/bgpd/rde.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: rde.c,v 1.23 2003/12/23 19:14:49 deraadt Exp $ */
+/* $OpenBSD: rde.c,v 1.24 2003/12/24 13:28:02 henning Exp $ */
/*
* Copyright (c) 2003 Henning Brauer <henning@openbsd.org>
@@ -148,15 +148,14 @@ rde_main(struct bgpd_config *config, int pipe_m2r[2], int pipe_s2r[2])
if (nfds > 0 && (pfd[PFD_PIPE_MAIN].revents & POLLOUT) &&
ibuf_main.w.queued) {
nfds--;
- if ((n = msgbuf_write(&ibuf_main.w)) == -1)
+ if ((n = msgbuf_write(&ibuf_main.w)) < 0)
fatal("pipe write error", errno);
}
if (nfds > 0 && (pfd[PFD_PIPE_SESSION].revents & POLLOUT) &&
ibuf_se.w.queued) {
nfds--;
- if ((n = msgbuf_write(&ibuf_se.w)) ==
- -1)
+ if ((n = msgbuf_write(&ibuf_se.w)) < 0)
fatal("pipe write error", errno);
}
}
diff --git a/usr.sbin/bgpd/session.c b/usr.sbin/bgpd/session.c
index dc3dc5df3fb..111f7bbe2ae 100644
--- a/usr.sbin/bgpd/session.c
+++ b/usr.sbin/bgpd/session.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: session.c,v 1.33 2003/12/24 11:39:43 henning Exp $ */
+/* $OpenBSD: session.c,v 1.34 2003/12/24 13:28:02 henning Exp $ */
/*
* Copyright (c) 2003 Henning Brauer <henning@openbsd.org>
@@ -269,8 +269,8 @@ session_main(struct bgpd_config *config, int pipe_m2s[2], int pipe_s2r[2])
}
if (nfds > 0 && pfd[PFD_PIPE_ROUTE].revents & POLLOUT)
- if (msgbuf_write(&ibuf_rde.w) == -1)
- fatal("pipe write error", 0);
+ if (msgbuf_write(&ibuf_rde.w) < 0)
+ fatal("pipe write error", errno);
if (nfds > 0 && pfd[PFD_PIPE_ROUTE].revents & POLLIN) {
nfds--;
@@ -750,7 +750,7 @@ session_open(struct peer *peer)
struct msg_open msg;
struct buf *buf;
u_int16_t len;
- int errs = 0;
+ int errs = 0, n;
len = MSGSIZE_OPEN_MIN;
@@ -775,7 +775,11 @@ session_open(struct peer *peer)
errs += buf_add(buf, &msg.optparamlen, sizeof(msg.optparamlen));
if (errs == 0) {
- if (buf_close(&peer->wbuf, buf) == -1) {
+ if ((n = buf_close(&peer->wbuf, buf)) < 0) {
+ if (n == -2)
+ log_errx(peer, "Connection closed");
+ else
+ log_err(peer, "Write error");
buf_free(buf);
bgp_fsm(peer, EVNT_CON_FATAL);
}
@@ -791,7 +795,7 @@ session_keepalive(struct peer *peer)
struct msg_header msg;
struct buf *buf;
ssize_t len;
- int errs = 0;
+ int errs = 0, n;
len = MSGSIZE_KEEPALIVE;
@@ -811,11 +815,15 @@ session_keepalive(struct peer *peer)
return;
}
- if (buf_close(&peer->wbuf, buf) == -1) {
+ if ((n = buf_close(&peer->wbuf, buf)) < 0) {
+ if (n == -2)
+ log_errx(peer, "Connection closed");
+ else
+ log_err(peer, "Write error");
buf_free(buf);
bgp_fsm(peer, EVNT_CON_FATAL);
- return;
}
+
start_timer_keepalive(peer);
}
@@ -832,7 +840,7 @@ session_notification(struct peer *peer, u_int8_t errcode, u_int8_t subcode,
struct msg_header msg;
struct buf *buf;
ssize_t len;
- int errs = 0;
+ int errs = 0, n;
len = MSGSIZE_NOTIFICATION_MIN + datalen;
@@ -857,7 +865,11 @@ session_notification(struct peer *peer, u_int8_t errcode, u_int8_t subcode,
return;
}
- if (buf_close(&peer->wbuf, buf) == -1) {
+ if ((n = buf_close(&peer->wbuf, buf)) < 0) {
+ if (n == -2)
+ log_errx(peer, "Connection closed");
+ else
+ log_err(peer, "Write error");
buf_free(buf);
bgp_fsm(peer, EVNT_CON_FATAL);
}
@@ -908,8 +920,13 @@ session_dispatch_msg(struct pollfd *pfd, struct peer *peer)
}
if (pfd->revents & POLLOUT && peer->wbuf.queued) {
- if (msgbuf_write(&peer->wbuf))
+ if ((error = msgbuf_write(&peer->wbuf)) < 0) {
+ if (error == -2)
+ log_errx(peer, "Connection closed");
+ else
+ log_err(peer, "Write error");
bgp_fsm(peer, EVNT_CON_FATAL);
+ }
if (!(pfd->revents & POLLIN))
return (1);
}
@@ -966,8 +983,8 @@ session_dispatch_msg(struct pollfd *pfd, struct peer *peer)
}
n = 0; /* give others a chance... */
if (peer->rbuf != NULL) {
- bzero(peer->rbuf,
- sizeof(struct peer_buf_read));
+ bzero(peer->rbuf, sizeof(struct
+ peer_buf_read));
peer->rbuf->wptr =
peer->rbuf->buf;
peer->rbuf->pkt_len =