diff options
author | Pierre-Yves Ritschard <pyr@cvs.openbsd.org> | 2009-06-05 21:15:08 +0000 |
---|---|---|
committer | Pierre-Yves Ritschard <pyr@cvs.openbsd.org> | 2009-06-05 21:15:08 +0000 |
commit | 681b6213e7f4571464c1de214c40e3ed4aea4bf7 (patch) | |
tree | 5bcfc5039a5f5e5391ae8ae2c842673ad20cd3df /usr.sbin/smtpd | |
parent | 39ed73224f88a6e467c05fd53a47a2006b00b862 (diff) |
bring buf_write which is going to be needed for bgpd.
Diffstat (limited to 'usr.sbin/smtpd')
-rw-r--r-- | usr.sbin/smtpd/buffer.c | 47 | ||||
-rw-r--r-- | usr.sbin/smtpd/imsg.h | 3 |
2 files changed, 48 insertions, 2 deletions
diff --git a/usr.sbin/smtpd/buffer.c b/usr.sbin/smtpd/buffer.c index 46e0e9d1013..49fe68f668a 100644 --- a/usr.sbin/smtpd/buffer.c +++ b/usr.sbin/smtpd/buffer.c @@ -1,4 +1,4 @@ -/* $OpenBSD: buffer.c,v 1.2 2009/06/05 20:43:57 pyr Exp $ */ +/* $OpenBSD: buffer.c,v 1.3 2009/06/05 21:15:07 pyr Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> @@ -134,6 +134,51 @@ buf_left(struct buf *buf) return (buf->max - buf->wpos); } +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_close(struct msgbuf *msgbuf, struct buf *buf) { diff --git a/usr.sbin/smtpd/imsg.h b/usr.sbin/smtpd/imsg.h index 41e41bd0919..ba11fbcf0ac 100644 --- a/usr.sbin/smtpd/imsg.h +++ b/usr.sbin/smtpd/imsg.h @@ -1,4 +1,4 @@ -/* $OpenBSD: imsg.h,v 1.1 2009/06/05 20:43:57 pyr Exp $ */ +/* $OpenBSD: imsg.h,v 1.2 2009/06/05 21:15:07 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 *); |