cdown / rpms / util-linux

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