diff options
author | Pierre-Yves Ritschard <pyr@cvs.openbsd.org> | 2009-06-05 21:15:48 +0000 |
---|---|---|
committer | Pierre-Yves Ritschard <pyr@cvs.openbsd.org> | 2009-06-05 21:15:48 +0000 |
commit | 0bc023d5d841447af472a603455d0282221dc7f0 (patch) | |
tree | 8fe835fe2b1319dfd0914a0e95ce14e2ec19f9be | |
parent | 681b6213e7f4571464c1de214c40e3ed4aea4bf7 (diff) |
bring buf_write here as well.
-rw-r--r-- | usr.sbin/ospfd/buffer.c | 47 | ||||
-rw-r--r-- | usr.sbin/ospfd/imsg.h | 3 | ||||
-rw-r--r-- | usr.sbin/relayd/buffer.c | 47 | ||||
-rw-r--r-- | usr.sbin/relayd/imsg.h | 3 |
4 files changed, 96 insertions, 4 deletions
diff --git a/usr.sbin/ospfd/buffer.c b/usr.sbin/ospfd/buffer.c index a03a2470afa..66f7887dba9 100644 --- a/usr.sbin/ospfd/buffer.c +++ b/usr.sbin/ospfd/buffer.c @@ -1,4 +1,4 @@ -/* $OpenBSD: buffer.c,v 1.13 2009/06/05 19:33:58 pyr Exp $ */ +/* $OpenBSD: buffer.c,v 1.14 2009/06/05 21:15:47 pyr Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -140,6 +140,51 @@ buf_close(struct msgbuf *msgbuf, struct buf *buf) buf_enqueue(msgbuf, buf); } +int +buf_write(struct msgbuf *msgbuf) +{ + struct iovec iov[IOV_MAX]; + struct buf *buf, *next; + unsigned int i = 0; + ssize_t n; + + bzero(&iov, sizeof(iov)); + TAILQ_FOREACH(buf, &msgbuf->bufs, entry) { + if (i >= IOV_MAX) + break; + iov[i].iov_base = buf->buf + buf->rpos; + iov[i].iov_len = buf->size - buf->rpos; + i++; + } + + if ((n = writev(msgbuf->fd, iov, i)) == -1) { + if (errno == EAGAIN || errno == ENOBUFS || + errno == EINTR) /* try later */ + return (0); + else + return (-1); + } + + if (n == 0) { /* connection closed */ + errno = 0; + return (-2); + } + + for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0; + buf = next) { + next = TAILQ_NEXT(buf, entry); + if (buf->rpos + n >= buf->size) { + n -= buf->size - buf->rpos; + buf_dequeue(msgbuf, buf); + } else { + buf->rpos += n; + n = 0; + } + } + + return (0); +} + void buf_free(struct buf *buf) { diff --git a/usr.sbin/ospfd/imsg.h b/usr.sbin/ospfd/imsg.h index d700b98c01c..091d6aeb80d 100644 --- a/usr.sbin/ospfd/imsg.h +++ b/usr.sbin/ospfd/imsg.h @@ -1,4 +1,4 @@ -/* $OpenBSD: imsg.h,v 1.3 2009/06/05 19:42:33 pyr Exp $ */ +/* $OpenBSD: imsg.h,v 1.4 2009/06/05 21:15:47 pyr Exp $ */ /* * Copyright (c) 2006, 2007 Pierre-Yves Ritschard <pyr@openbsd.org> @@ -86,6 +86,7 @@ void *buf_seek(struct buf *, size_t, size_t); size_t buf_size(struct buf *); size_t buf_left(struct buf *); void buf_close(struct msgbuf *, struct buf *); +int buf_write(struct msgbuf *); void buf_free(struct buf *); void msgbuf_init(struct msgbuf *); void msgbuf_clear(struct msgbuf *); diff --git a/usr.sbin/relayd/buffer.c b/usr.sbin/relayd/buffer.c index ea81e108a09..cb459d14a96 100644 --- a/usr.sbin/relayd/buffer.c +++ b/usr.sbin/relayd/buffer.c @@ -1,4 +1,4 @@ -/* $OpenBSD: buffer.c,v 1.19 2009/06/05 07:14:31 pyr Exp $ */ +/* $OpenBSD: buffer.c,v 1.20 2009/06/05 21:15:47 pyr Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -140,6 +140,51 @@ buf_close(struct msgbuf *msgbuf, struct buf *buf) buf_enqueue(msgbuf, buf); } +int +buf_write(struct msgbuf *msgbuf) +{ + struct iovec iov[IOV_MAX]; + struct buf *buf, *next; + unsigned int i = 0; + ssize_t n; + + bzero(&iov, sizeof(iov)); + TAILQ_FOREACH(buf, &msgbuf->bufs, entry) { + if (i >= IOV_MAX) + break; + iov[i].iov_base = buf->buf + buf->rpos; + iov[i].iov_len = buf->size - buf->rpos; + i++; + } + + if ((n = writev(msgbuf->fd, iov, i)) == -1) { + if (errno == EAGAIN || errno == ENOBUFS || + errno == EINTR) /* try later */ + return (0); + else + return (-1); + } + + if (n == 0) { /* connection closed */ + errno = 0; + return (-2); + } + + for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0; + buf = next) { + next = TAILQ_NEXT(buf, entry); + if (buf->rpos + n >= buf->size) { + n -= buf->size - buf->rpos; + buf_dequeue(msgbuf, buf); + } else { + buf->rpos += n; + n = 0; + } + } + + return (0); +} + void buf_free(struct buf *buf) { diff --git a/usr.sbin/relayd/imsg.h b/usr.sbin/relayd/imsg.h index e39fcaf928e..bf15876b94f 100644 --- a/usr.sbin/relayd/imsg.h +++ b/usr.sbin/relayd/imsg.h @@ -1,4 +1,4 @@ -/* $OpenBSD: imsg.h,v 1.7 2009/06/05 19:42:33 pyr Exp $ */ +/* $OpenBSD: imsg.h,v 1.8 2009/06/05 21:15:47 pyr Exp $ */ /* * Copyright (c) 2006, 2007 Pierre-Yves Ritschard <pyr@openbsd.org> @@ -86,6 +86,7 @@ void *buf_seek(struct buf *, size_t, size_t); size_t buf_size(struct buf *); size_t buf_left(struct buf *); void buf_close(struct msgbuf *, struct buf *); +int buf_write(struct msgbuf *); void buf_free(struct buf *); void msgbuf_init(struct msgbuf *); void msgbuf_clear(struct msgbuf *); |