531551
From 1e0289af99737049de97b8cfe342b56d380560bf Mon Sep 17 00:00:00 2001
531551
From: Karel Zak <kzak@redhat.com>
531551
Date: Thu, 16 Mar 2017 12:20:58 +0100
531551
Subject: [PATCH 091/116] logger: backport --size
531551
531551
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1323916
531551
Signed-off-by: Karel Zak <kzak@redhat.com>
531551
---
531551
 misc-utils/logger.1 |  6 ++++++
531551
 misc-utils/logger.c | 34 ++++++++++++++++++++++++----------
531551
 2 files changed, 30 insertions(+), 10 deletions(-)
531551
531551
diff --git a/misc-utils/logger.1 b/misc-utils/logger.1
531551
index 8c4faca..57ca0d5 100644
531551
--- a/misc-utils/logger.1
531551
+++ b/misc-utils/logger.1
531551
@@ -98,6 +98,12 @@ logs the message as informational in the local3 facility.
531551
 The default is
531551
 .IR user.notice .
531551
 .TP
531551
+\fB\-S\fR, \fB\-\-size\fR \fIsize\fR
531551
+Sets the maximum permitted message size. The default is 1KiB, which is
531551
+the limit traditionally used and specified in RFC 3164. When selecting a
531551
+maximum message size, it is important to ensure that the receiver supports
531551
+the max size as well, otherwise messages may become truncated.
531551
+.TP
531551
 \fB\-s\fR, \fB\-\-stderr\fR
531551
 Output the message to standard error as well as to the system log.
531551
 .TP
531551
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
531551
index a331869..dfda018 100644
531551
--- a/misc-utils/logger.c
531551
+++ b/misc-utils/logger.c
531551
@@ -54,6 +54,8 @@
531551
 #include "closestream.h"
531551
 #include "nls.h"
531551
 #include "strutils.h"
531551
+#include "xalloc.h"
531551
+#include "all-io.h"
531551
 
531551
 #define	SYSLOG_NAMES
531551
 #include <syslog.h>
531551
@@ -183,7 +185,7 @@ inet_socket(const char *servername, const char *port, const int socket_type)
531551
 
531551
 static void
531551
 mysyslog(int fd, int logflags, int pri, char *tag, char *msg) {
531551
-       char buf[1000], pid[30], *cp, *tp;
531551
+       char *buf, pid[30], *cp, *tp;
531551
        time_t now;
531551
 
531551
        if (fd > -1) {
531551
@@ -201,11 +203,11 @@ mysyslog(int fd, int logflags, int pri, char *tag, char *msg) {
531551
                (void)time(&now;;
531551
 	       tp = ctime(&now)+4;
531551
 
531551
-               snprintf(buf, sizeof(buf), "<%d>%.15s %.200s%s: %.400s",
531551
+               xasprintf(&buf, "<%d>%.15s %.200s%s: %s",
531551
 			pri, tp, cp, pid, msg);
531551
 
531551
-               if (write(fd, buf, strlen(buf)+1) < 0)
531551
-                       return; /* error */
531551
+	       write_all(fd, buf, strlen(buf)+1);
531551
+	       free(buf);
531551
        }
531551
 }
531551
 
531551
@@ -221,6 +223,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
531551
 		" -i, --id              log the process ID too\n"
531551
 		" -f, --file <file>     log the contents of this file\n"
531551
 		" -h, --help            display this help text and exit\n"), out);
531551
+	fputs(_(" -S, --size <num>      maximum size for a single message (default 1024)\n"), out);
531551
 	fputs(_(" -n, --server <name>   write to this remote syslog server\n"
531551
 		" -P, --port <port>     use this port for UDP or TCP connection\n"
531551
 		" -p, --priority <prio> mark given message with this priority\n"
531551
@@ -241,11 +244,12 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
531551
 int
531551
 main(int argc, char **argv) {
531551
 	int ch, logflags, pri;
531551
-	char *tag, buf[1024];
531551
+	char *tag, *buf;
531551
 	char *usock = NULL;
531551
 	char *server = NULL;
531551
 	char *port = NULL;
531551
 	int LogSock = -1, socket_type = ALL_TYPES;
531551
+	size_t max_message_size = 1024;
531551
 
531551
 	static const struct option longopts[] = {
531551
 		{ "id",		no_argument,	    0, 'i' },
531551
@@ -253,6 +257,7 @@ main(int argc, char **argv) {
531551
 		{ "file",	required_argument,  0, 'f' },
531551
 		{ "priority",	required_argument,  0, 'p' },
531551
 		{ "tag",	required_argument,  0, 't' },
531551
+		{ "size",       required_argument,  0, 'S' },
531551
 		{ "socket",	required_argument,  0, 'u' },
531551
 		{ "udp",	no_argument,	    0, 'd' },
531551
 		{ "tcp",	no_argument,	    0, 'T' },
531551
@@ -271,7 +276,7 @@ main(int argc, char **argv) {
531551
 	tag = NULL;
531551
 	pri = LOG_NOTICE;
531551
 	logflags = 0;
531551
-	while ((ch = getopt_long(argc, argv, "f:ip:st:u:dTn:P:Vh",
531551
+	while ((ch = getopt_long(argc, argv, "f:ip:st:u:dTn:P:S:Vh",
531551
 					    longopts, NULL)) != -1) {
531551
 		switch((char)ch) {
531551
 		case 'f':		/* file to log */
531551
@@ -297,6 +302,10 @@ main(int argc, char **argv) {
531551
 		case 'd':
531551
 			socket_type = TYPE_UDP;
531551
 			break;
531551
+		case 'S':
531551
+			max_message_size = strtosize_or_err(optarg,
531551
+                                _("failed to parse message size"));
531551
+			break;
531551
 		case 'T':
531551
 			socket_type = TYPE_TCP;
531551
 			break;
531551
@@ -327,21 +336,23 @@ main(int argc, char **argv) {
531551
 	else
531551
 		openlog(tag ? tag : getlogin(), logflags, 0);
531551
 
531551
+	buf = xcalloc(1, max_message_size);
531551
+
531551
 	/* log input line if appropriate */
531551
 	if (argc > 0) {
531551
 		register char *p, *endp;
531551
 		size_t len;
531551
 
531551
-		for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
531551
+		for (p = buf, endp = buf + max_message_size - 2; *argv;) {
531551
 			len = strlen(*argv);
531551
 			if (p + len > endp && p > buf) {
531551
 			    if (!usock && !server)
531551
 				syslog(pri, "%s", buf);
531551
 			    else
531551
 				mysyslog(LogSock, logflags, pri, tag, buf);
531551
-				p = buf;
531551
+			    p = buf;
531551
 			}
531551
-			if (len > sizeof(buf) - 1) {
531551
+			if (len > max_message_size - 1) {
531551
 			    if (!usock && !server)
531551
 				syslog(pri, "%s", *argv++);
531551
 			    else
531551
@@ -360,7 +371,7 @@ main(int argc, char **argv) {
531551
 			mysyslog(LogSock, logflags, pri, tag, buf);
531551
 		}
531551
 	} else {
531551
-		while (fgets(buf, sizeof(buf), stdin) != NULL) {
531551
+		while (fgets(buf, max_message_size, stdin) != NULL) {
531551
 		    /* glibc is buggy and adds an additional newline,
531551
 		       so we have to remove it here until glibc is fixed */
531551
 		    int len = strlen(buf);
531551
@@ -374,6 +385,9 @@ main(int argc, char **argv) {
531551
 			mysyslog(LogSock, logflags, pri, tag, buf);
531551
 		}
531551
 	}
531551
+
531551
+	free(buf);
531551
+
531551
 	if (!usock && !server)
531551
 		closelog();
531551
 	else
531551
-- 
531551
2.9.3
531551