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