summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Bluhm <bluhm@cvs.openbsd.org>2014-10-03 15:41:19 +0000
committerAlexander Bluhm <bluhm@cvs.openbsd.org>2014-10-03 15:41:19 +0000
commitcbae1b58d7e4130a6feb32ce1d3605617907a6a6 (patch)
tree8c8be333caf0fa90610c452b4b78c1d000228ec6
parent6295da21278d0a1bd4a54f775cc25e3e79e06b33 (diff)
Strip trailing new-lines from syslog messages. This avoids empty
lines when printing to console or stderr. OK deraadt@
-rw-r--r--lib/libc/gen/syslog_r.c17
1 files changed, 11 insertions, 6 deletions
diff --git a/lib/libc/gen/syslog_r.c b/lib/libc/gen/syslog_r.c
index 3b3058a83b1..1f993f446bd 100644
--- a/lib/libc/gen/syslog_r.c
+++ b/lib/libc/gen/syslog_r.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: syslog_r.c,v 1.5 2014/07/14 03:52:04 deraadt Exp $ */
+/* $OpenBSD: syslog_r.c,v 1.6 2014/10/03 15:41:18 bluhm Exp $ */
/*
* Copyright (c) 1983, 1988, 1993
* The Regents of the University of California. All rights reserved.
@@ -90,7 +90,7 @@ __vsyslog_r(int pri, struct syslog_data *data,
int fd, saved_errno, error;
#define TBUF_LEN 2048
#define FMT_LEN 1024
- char *stdp, tbuf[TBUF_LEN], fmt_cpy[FMT_LEN];
+ char *conp = NULL, *stdp = NULL, tbuf[TBUF_LEN], fmt_cpy[FMT_LEN];
int tbuf_left, fmt_left, prlen;
#define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
@@ -127,6 +127,8 @@ __vsyslog_r(int pri, struct syslog_data *data,
prlen = snprintf(p, tbuf_left, "<%d>", pri);
DEC();
+ if (data->log_stat & LOG_CONS)
+ conp = p;
/*
* syslogd will expand time automagically for reentrant case, and
@@ -195,13 +197,17 @@ __vsyslog_r(int pri, struct syslog_data *data,
prlen = vsnprintf(p, tbuf_left, fmt_cpy, ap);
DEC();
cnt = p - tbuf;
+ while (cnt > 0 && p[-1] == '\n') {
+ *(--p) = '\0';
+ --cnt;
+ }
/* Output to stderr if requested. */
if (data->log_stat & LOG_PERROR) {
struct iovec iov[2];
iov[0].iov_base = stdp;
- iov[0].iov_len = cnt - (stdp - tbuf);
+ iov[0].iov_len = cnt > stdp - tbuf ? cnt - (stdp - tbuf) : 0;
iov[1].iov_base = "\n";
iov[1].iov_len = 1;
(void)writev(STDERR_FILENO, iov, 2);
@@ -222,9 +228,8 @@ __vsyslog_r(int pri, struct syslog_data *data,
(fd = open(_PATH_CONSOLE, O_WRONLY|O_NONBLOCK, 0)) >= 0) {
struct iovec iov[2];
- p = strchr(tbuf, '>') + 1;
- iov[0].iov_base = p;
- iov[0].iov_len = cnt - (p - tbuf);
+ iov[0].iov_base = conp;
+ iov[0].iov_len = cnt > conp - tbuf ? cnt - (conp - tbuf) : 0;
iov[1].iov_base = "\r\n";
iov[1].iov_len = 2;
(void)writev(fd, iov, 2);