MINOR: sink: now call the generic fd write function

Let's not mess up with fd-specific code, locking nor message formating
here, and use the new generic function instead. This substantially
simplifies the sink_write() code and makes it more agnostic to the
output representation and storage.
This commit is contained in:
Willy Tarreau 2019-08-27 14:21:02 +02:00
parent 931d8b79a8
commit a1426de5aa

View File

@ -18,7 +18,6 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#include <sys/uio.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <common/compat.h> #include <common/compat.h>
@ -108,48 +107,23 @@ struct sink *sink_new_fd(const char *name, const char *desc, enum sink_fmt fmt,
*/ */
void sink_write(struct sink *sink, const struct ist msg[], size_t nmsg) void sink_write(struct sink *sink, const struct ist msg[], size_t nmsg)
{ {
struct iovec iovec[10];
char short_hdr[4]; char short_hdr[4];
size_t maxlen = sink->maxlen ? sink->maxlen : ~0; struct ist pfx[4];
size_t npfx = 0;
size_t sent = 0; size_t sent = 0;
int vec = 0;
/* keep one char for a possible trailing '\n' in any case */
maxlen--;
if (sink->fmt == SINK_FMT_SHORT) { if (sink->fmt == SINK_FMT_SHORT) {
short_hdr[0] = '<'; short_hdr[0] = '<';
short_hdr[1] = '0' + sink->syslog_minlvl; short_hdr[1] = '0' + sink->syslog_minlvl;
short_hdr[2] = '>'; short_hdr[2] = '>';
iovec[vec].iov_base = short_hdr; pfx[npfx].ptr = short_hdr;
iovec[vec].iov_len = MIN(maxlen, 3); pfx[npfx].len = 3;
maxlen -= iovec[vec].iov_len; npfx++;
vec++;
}
/* copy the remaining entries from the original message. Skip empty fields and
* truncate the whole message to maxlen.
*/
while (nmsg && vec < (sizeof(iovec) / sizeof(iovec[0]) - 1)) {
iovec[vec].iov_base = msg->ptr;
iovec[vec].iov_len = MIN(maxlen, msg->len);
maxlen -= iovec[vec].iov_len;
if (iovec[vec].iov_len)
vec++;
msg++; nmsg--;
} }
if (sink->type == SINK_TYPE_FD) { if (sink->type == SINK_TYPE_FD) {
/* For the FD we always emit the trailing \n. It was already provisioned above. */ sent = fd_write_frag_line(sink->ctx.fd, sink->maxlen, pfx, npfx, msg, nmsg, 1);
iovec[vec].iov_base = "\n";
iovec[vec].iov_len = 1;
vec++;
HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &sink->ctx.lock);
sent = writev(sink->ctx.fd, iovec, vec);
HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &sink->ctx.lock);
/* sent > 0 if the message was delivered */
} }
/* account for errors now */ /* account for errors now */