aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKarel Zak <kzak@redhat.com>2015-03-06 12:31:48 +0100
committerKarel Zak <kzak@redhat.com>2015-03-06 12:31:48 +0100
commit0d1f3a707d3d916ecd52669bae29d02c04bdeb6c (patch)
treebc74bd1610d1941c537edb3e0190f1f081edcb59
parent17d66ccc7516a5e2cf681abc73406559da41eaae (diff)
parent940a14a3515a0d8cddb338605e788315565fd6cc (diff)
downloadutil-linux-playground-0d1f3a707d3d916ecd52669bae29d02c04bdeb6c.tar.gz
Merge branch 'logger-fix-tcp-framing' of https://github.com/rgerhards/util-linux
* 'logger-fix-tcp-framing' of https://github.com/rgerhards/util-linux: logger: bugfix: tcp syslog framing is broken, -T unusable logger: refactor the way output is written
-rw-r--r--misc-utils/logger.c40
1 files changed, 27 insertions, 13 deletions
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 308db8b34d..24cbb3b0b4 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -332,6 +332,30 @@ rfc3164_current_time(void)
return time;
}
+/* writes generated buffer to desired destination. For TCP syslog,
+ * we use RFC6587 octet-stuffing. This is not great, but doing
+ * full blown RFC5425 (TLS) looks like it is too much for the
+ * logger utility.
+ */
+static void write_output(const struct logger_ctl *ctl, const char *const buf,
+ const size_t len)
+{
+ if (write_all(ctl->fd, buf, len) < 0)
+ warn(_("write failed"));
+ else
+ if (ctl->socket_type == TYPE_TCP)
+ /* using an additional write seems like the best compromise:
+ * - writev() is not yet supported by framework
+ * - adding the \n to the buffer in formatters violates layers
+ * - adding \n after the fact requires memory copy
+ * - logger is not a high-performance app
+ */
+ if (write_all(ctl->fd, "\n", 1) < 0)
+ warn(_("write failed"));
+ if (ctl->stderr_printout)
+ fprintf(stderr, "%s\n", buf);
+}
+
static void syslog_rfc3164(const struct logger_ctl *ctl, const char *msg)
{
char *buf, pid[30], *cp, *hostname, *dot;
@@ -353,10 +377,7 @@ static void syslog_rfc3164(const struct logger_ctl *ctl, const char *msg)
len = xasprintf(&buf, "<%d>%.15s %s %.200s%s: %.400s",
ctl->pri, rfc3164_current_time(), hostname, cp, pid, msg);
- if (write_all(ctl->fd, buf, len) < 0)
- warn(_("write failed"));
- if (ctl->stderr_printout)
- fprintf(stderr, "%s\n", buf);
+ write_output(ctl, buf, len);
free(hostname);
free(buf);
@@ -427,11 +448,7 @@ static void syslog_rfc5424(const struct logger_ctl *ctl, const char *msg)
hostname ? hostname : "",
tag, pid, timeq, msg);
- if (write_all(ctl->fd, buf, len) < 0)
- warn(_("write failed"));
-
- if (ctl->stderr_printout)
- fprintf(stderr, "%s\n", buf);
+ write_output(ctl, buf, len);
free(hostname);
free(buf);
@@ -483,10 +500,7 @@ static void syslog_local(const struct logger_ctl *ctl, const char *msg)
len = xasprintf(&buf, "<%d>%s %s%s: %s", ctl->pri, rfc3164_current_time(),
tag, pid, msg);
- if (write_all(ctl->fd, buf, len) < 0)
- warn(_("write failed"));
- if (ctl->stderr_printout)
- fprintf(stderr, "%s\n", buf);
+ write_output(ctl, buf, len);
free(buf);
}