From patchwork Wed Apr 1 16:33:26 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Etienne Champetier X-Patchwork-Id: 457332 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from arrakis.dune.hu (arrakis.dune.hu [78.24.191.176]) (using TLSv1.1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id A836214007F for ; Thu, 2 Apr 2015 03:36:14 +1100 (AEDT) Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id DA823280893; Wed, 1 Apr 2015 18:35:18 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on arrakis.dune.hu X-Spam-Level: X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,FREEMAIL_FROM, T_RP_MATCHES_RCVD autolearn=unavailable version=3.3.2 Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id AE29D280174 for ; Wed, 1 Apr 2015 18:35:12 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 CL_IP_EQ_FROM_IP=-2 (check from: .champetier. - helo: .ns3354555.champetier. - helo-domain: .champetier.) FROM/MX_MATCHES_HELO(DOMAIN)=-2; rate: -8.5 Received: from ns3354555.champetier.me (ns3354555.champetier.me [37.187.20.53]) by arrakis.dune.hu (Postfix) with ESMTP for ; Wed, 1 Apr 2015 18:35:12 +0200 (CEST) Received: by ns3354555.champetier.me (Postfix, from userid 1000) id E414836A453C; Wed, 1 Apr 2015 18:35:50 +0200 (CEST) From: Etienne CHAMPETIER To: John Crispin , openwrt-devel@lists.openwrt.org Date: Wed, 1 Apr 2015 18:33:26 +0200 Message-Id: <1427906006-25548-1-git-send-email-champetier.etienne@gmail.com> X-Mailer: git-send-email 2.1.0 Subject: [OpenWrt-Devel] [PATCH] logread: add support for non-transparent-framing for Syslog over TCP X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: openwrt-devel-bounces@lists.openwrt.org Sender: "openwrt-devel" https://tools.ietf.org/html/rfc6587#section-3.4.2 the idea is to add \n or \0 as message trailer so it's easier to find message end Signed-off-by: Etienne CHAMPETIER --- log/logread.c | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/log/logread.c b/log/logread.c index 06dda62..c791a85 100644 --- a/log/logread.c +++ b/log/logread.c @@ -59,7 +59,7 @@ static struct uloop_timeout retry; static struct uloop_fd sender; static const char *log_file, *log_ip, *log_port, *log_prefix, *pid_file, *hostname; static int log_type = LOG_STDOUT; -static int log_size, log_udp, log_follow = 0; +static int log_size, log_udp, log_follow, log_trailer_null, log_trailer_lf = 0; static const char* getcodetext(int value, CODE *codetable) { CODE *i; @@ -149,10 +149,26 @@ static int log_notify(struct blob_attr *msg) if (blobmsg_get_u32(tb[LOG_SOURCE]) == SOURCE_KLOG) strncat(buf, "kernel: ", sizeof(buf) - strlen(buf) - 1); strncat(buf, m, sizeof(buf) - strlen(buf) - 1); - if (log_udp) + if (log_udp) { err = write(sender.fd, buf, strlen(buf)); - else - err = send(sender.fd, buf, strlen(buf), 0); + } else { + size_t buflen = strlen(buf); + if (log_trailer_lf) { + if (buflen == sizeof(buf)-1) { + //buf is full, overwrite last char + buf[buflen-1] = '\n'; + } else { + buf[buflen] = '\n'; + buf[buflen+1] = '\0'; + buflen++; + } + } + //buf is already null terminated + if (log_trailer_null) + buflen++; + + err = send(sender.fd, buf, buflen, 0); + } if (err < 0) { syslog(LOG_INFO, "failed to send log data to %s:%s via %s\n", @@ -182,14 +198,16 @@ static int usage(const char *prog) "Options:\n" " -s Path to ubus socket\n" " -l Got only the last 'count' messages\n" - " -r Stream message to a server\n" " -F Log file\n" " -S Log size\n" " -p PID file\n" + " -f Follow log messages\n" + " -r Stream message to a server\n" " -h Add hostname to the message\n" " -P Prefix custom text to streamed messages\n" - " -f Follow log messages\n" " -u Use UDP as the protocol\n" + " -t Add null byte at the end of message when using TCP protocol\n" + " -T Add LF at the end of message when using TCP protocol\n" "\n", prog); return 1; } @@ -234,11 +252,17 @@ int main(int argc, char **argv) signal(SIGPIPE, SIG_IGN); - while ((ch = getopt(argc, argv, "ufcs:l:r:F:p:S:P:h:")) != -1) { + while ((ch = getopt(argc, argv, "utTfcs:l:r:F:p:S:P:h:")) != -1) { switch (ch) { case 'u': log_udp = 1; break; + case 't': + log_trailer_null = 1; + break; + case 'T': + log_trailer_lf = 1; + break; case 's': ubus_socket = optarg; break;