Skip to content

Instantly share code, notes, and snippets.

@brandt
Created October 24, 2016 19:21
Show Gist options
  • Save brandt/d652913a148bc428c621dc1047b4cc44 to your computer and use it in GitHub Desktop.
Save brandt/d652913a148bc428c621dc1047b4cc44 to your computer and use it in GitHub Desktop.
Changes of the logger CLI tool from util-linux-ng v2.17.2 (version included in RHEL/CentOS 6) to v2.29-rc2
From 912d6b98925a34b05fb4791a2f52225b06c420af Mon Sep 17 00:00:00 2001
From: "WUEBBELS, Josef \\(Extern\\)" <Josef.WUEBBELS@mtu.de>
Date: Fri, 28 Jan 2011 14:15:20 +0100
Subject: [PATCH 001/115] logger: support for logging to UDP socket / remote
syslog server
It adds the ability to logger to log a message to a udp socket. The -n option
followed by the hostname of the remote host is mandatory to do this. The
optional -P option can be used to change the UDP destination port (default
514). The function udpopenlog is used to open the udp socket. After that
everything works in almost the same way like it does when logging to a UNIX
socket.
Signed-off-by: Josef Wuebbels <josef.wuebbels@mtu.de>
---
misc-utils/logger.c | 47 +++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 43 insertions(+), 4 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 95050a1..9873399 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -47,6 +47,8 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
+#include <arpa/inet.h>
+#include <netdb.h>
#include "nls.h"
#define SYSLOG_NAMES
@@ -57,6 +59,7 @@ int pencode __P((char *));
void usage __P((void));
static int optd = 0;
+static int udpport = 514;
static int
myopenlog(const char *sock) {
@@ -83,6 +86,32 @@ myopenlog(const char *sock) {
return fd;
}
+static int
+udpopenlog(const char *servername,int port) {
+ int fd;
+ struct sockaddr_in s_addr;
+ struct hostent *serverhost;
+
+ if ((serverhost = gethostbyname(servername)) == NULL ){
+ printf (_("unable to resolve '%s'\n"), servername);
+ exit(1);
+ }
+
+ if ((fd = socket(AF_INET, SOCK_DGRAM , 0)) == -1) {
+ printf (_("socket: %s.\n"), strerror(errno));
+ exit (1);
+ }
+
+ bcopy(serverhost->h_addr,&s_addr.sin_addr,serverhost->h_length);
+ s_addr.sin_family=AF_INET;
+ s_addr.sin_port=htons(port);
+
+ if (connect(fd, (struct sockaddr *) &s_addr, sizeof(s_addr)) == -1) {
+ printf (_("connect: %s.\n"), strerror(errno));
+ exit(1);
+ }
+ return fd;
+}
static void
mysyslog(int fd, int logflags, int pri, char *tag, char *msg) {
char buf[1000], pid[30], *cp, *tp;
@@ -122,6 +151,7 @@ main(int argc, char **argv) {
int ch, logflags, pri;
char *tag, buf[1024];
char *usock = NULL;
+ char *udpserver = NULL;
int LogSock = -1;
setlocale(LC_ALL, "");
@@ -131,7 +161,7 @@ main(int argc, char **argv) {
tag = NULL;
pri = LOG_NOTICE;
logflags = 0;
- while ((ch = getopt(argc, argv, "f:ip:st:u:d")) != -1)
+ while ((ch = getopt(argc, argv, "f:ip:st:u:dn:P:")) != -1)
switch((char)ch) {
case 'f': /* file to log */
if (freopen(optarg, "r", stdin) == NULL) {
@@ -159,6 +189,13 @@ main(int argc, char **argv) {
case 'd':
optd = 1; /* use datagrams */
break;
+ case 'n': /* udp socket */
+ optd = 1; /* use datagrams because udp */
+ udpserver = optarg;
+ break;
+ case 'P': /* change udp port */
+ udpport = atoi(optarg);
+ break;
case '?':
default:
usage();
@@ -167,8 +204,10 @@ main(int argc, char **argv) {
argv += optind;
/* setup for logging */
- if (!usock)
+ if (!usock && !udpserver)
openlog(tag ? tag : getlogin(), logflags, 0);
+ else if (udpserver)
+ LogSock = udpopenlog(udpserver,udpport);
else
LogSock = myopenlog(usock);
@@ -182,14 +221,14 @@ main(int argc, char **argv) {
for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
len = strlen(*argv);
if (p + len > endp && p > buf) {
- if (!usock)
+ if (!usock && !udpserver)
syslog(pri, "%s", buf);
else
mysyslog(LogSock, logflags, pri, tag, buf);
p = buf;
}
if (len > sizeof(buf) - 1) {
- if (!usock)
+ if (!usock && !udpserver)
syslog(pri, "%s", *argv++);
else
mysyslog(LogSock, logflags, pri, tag, *argv++);
--
2.9.3
From dd49983ad91221b5b547a0225f13d2d91205522f Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Thu, 24 Feb 2011 20:16:39 +0100
Subject: [PATCH 002/115] logger: fix variable type compiler warning
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 9873399..a61959d 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -216,7 +216,7 @@ main(int argc, char **argv) {
/* log input line if appropriate */
if (argc > 0) {
register char *p, *endp;
- int len;
+ size_t len;
for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
len = strlen(*argv);
--
2.9.3
From b363e86d76340e84f1e21d8bce3727e0d7ff539a Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Thu, 24 Feb 2011 20:16:40 +0100
Subject: [PATCH 003/115] logger: support long options
Use getopt_long and usage output changed to match long options.
This patch will also scrutiny argument of formerly undocumented
-P option.
[kzak@redhat.com: - include c.h]
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 71 ++++++++++++++++++++++++++++++++++++++++++++---------
1 file changed, 59 insertions(+), 12 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index a61959d..50a9358 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -49,14 +49,17 @@
#include <sys/un.h>
#include <arpa/inet.h>
#include <netdb.h>
+#include <getopt.h>
+
+#include "c.h"
#include "nls.h"
+#include "strutils.h"
#define SYSLOG_NAMES
#include <syslog.h>
int decode __P((char *, CODE *));
int pencode __P((char *));
-void usage __P((void));
static int optd = 0;
static int udpport = 514;
@@ -140,6 +143,30 @@ mysyslog(int fd, int logflags, int pri, char *tag, char *msg) {
}
}
+static void __attribute__ ((__noreturn__)) usage(FILE *out)
+{
+ fprintf(out,
+ _("\nUsage:\n"
+ " %s [options] message\n"),
+ program_invocation_short_name);
+
+ fprintf(out, _(
+ "\nOptions:\n"
+ " -i, --id log process id\n"
+ " -s, --stderr log message to standard error as well\n"
+ " -f, --file FILE log contents of the specified file\n"
+ " -p, --priority PRI enter message priority\n"
+ " -t, --tag TAG mark every line with tag\n"
+ " -u, --socket SOCK write to socket\n"
+ " -d, --udp use udp (tcp is default)\n"
+ " -n, --server ADDR write to remote syslog server\n"
+ " -P, --port define port number\n"
+ " -V, --version output version information and exit\n"
+ " -h, --help display this help and exit\n\n"));
+
+ exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+}
+
/*
* logger -- read and log utility
*
@@ -153,6 +180,22 @@ main(int argc, char **argv) {
char *usock = NULL;
char *udpserver = NULL;
int LogSock = -1;
+ long tmpport;
+
+ static const struct option longopts[] = {
+ { "id", no_argument, 0, 'i' },
+ { "stderr", no_argument, 0, 's' },
+ { "file", required_argument, 0, 'f' },
+ { "priority", required_argument, 0, 'p' },
+ { "tag", required_argument, 0, 't' },
+ { "socket", required_argument, 0, 'u' },
+ { "udp", no_argument, 0, 'd' },
+ { "server", required_argument, 0, 'n' },
+ { "port", required_argument, 0, 'P' },
+ { "version", no_argument, 0, 'V' },
+ { "help", no_argument, 0, 'h' },
+ { NULL, 0, 0, 0 }
+ };
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
@@ -161,7 +204,8 @@ main(int argc, char **argv) {
tag = NULL;
pri = LOG_NOTICE;
logflags = 0;
- while ((ch = getopt(argc, argv, "f:ip:st:u:dn:P:")) != -1)
+ while ((ch = getopt_long(argc, argv, "f:ip:st:u:dn:P:Vh",
+ longopts, NULL)) != -1)
switch((char)ch) {
case 'f': /* file to log */
if (freopen(optarg, "r", stdin) == NULL) {
@@ -194,11 +238,22 @@ main(int argc, char **argv) {
udpserver = optarg;
break;
case 'P': /* change udp port */
- udpport = atoi(optarg);
+ tmpport = strtol_or_err(optarg,
+ _("failed to parse port number"));
+ if (tmpport < 0 || 65535 < tmpport)
+ errx(EXIT_FAILURE, _("port `%ld' out of range"),
+ tmpport);
+ udpport = (int) tmpport;
break;
+ case 'V':
+ printf(_("%s from %s\n"), program_invocation_short_name,
+ PACKAGE_STRING);
+ exit(EXIT_SUCCESS);
+ case 'h':
+ usage(stdout);
case '?':
default:
- usage();
+ usage(stderr);
}
argc -= optind;
argv += optind;
@@ -316,11 +371,3 @@ decode(name, codetab)
return (-1);
}
-
-void
-usage()
-{
- (void)fprintf(stderr,
- _("usage: logger [-is] [-f file] [-p pri] [-t tag] [-u socket] [ message ... ]\n"));
- exit(1);
-}
--
2.9.3
From 49999d6aef7b98c66d2df5163a3517775ac0780f Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Thu, 24 Feb 2011 20:16:41 +0100
Subject: [PATCH 004/115] logger: use libc error printing facilities
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 72 +++++++++++++++++++++--------------------------------
1 file changed, 29 insertions(+), 43 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 50a9358..5ecec76 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -69,23 +69,18 @@ myopenlog(const char *sock) {
int fd;
static struct sockaddr_un s_addr; /* AF_UNIX address of local logger */
- if (strlen(sock) >= sizeof(s_addr.sun_path)) {
- printf (_("logger: openlog: pathname too long\n"));
- exit(1);
- }
+ if (strlen(sock) >= sizeof(s_addr.sun_path))
+ errx(EXIT_FAILURE, _("openlog %s: pathname too long"), sock);
s_addr.sun_family = AF_UNIX;
(void)strcpy(s_addr.sun_path, sock);
- if ((fd = socket(AF_UNIX, optd ? SOCK_DGRAM : SOCK_STREAM, 0)) == -1) {
- printf (_("socket: %s.\n"), strerror(errno));
- exit (1);
- }
+ if ((fd = socket(AF_UNIX, optd ? SOCK_DGRAM : SOCK_STREAM, 0)) == -1)
+ err(EXIT_FAILURE, _("socket %s"), sock);
+
+ if (connect(fd, (struct sockaddr *) &s_addr, sizeof(s_addr)) == -1)
+ err(EXIT_FAILURE, _("connect %s"), sock);
- if (connect(fd, (struct sockaddr *) &s_addr, sizeof(s_addr)) == -1) {
- printf (_("connect: %s.\n"), strerror(errno));
- exit (1);
- }
return fd;
}
@@ -95,24 +90,19 @@ udpopenlog(const char *servername,int port) {
struct sockaddr_in s_addr;
struct hostent *serverhost;
- if ((serverhost = gethostbyname(servername)) == NULL ){
- printf (_("unable to resolve '%s'\n"), servername);
- exit(1);
- }
+ if ((serverhost = gethostbyname(servername)) == NULL )
+ errx(EXIT_FAILURE, _("unable to resolve '%s'"), servername);
- if ((fd = socket(AF_INET, SOCK_DGRAM , 0)) == -1) {
- printf (_("socket: %s.\n"), strerror(errno));
- exit (1);
- }
+ if ((fd = socket(AF_INET, SOCK_DGRAM , 0)) == -1)
+ err(EXIT_FAILURE, _("socket"));
bcopy(serverhost->h_addr,&s_addr.sin_addr,serverhost->h_length);
s_addr.sin_family=AF_INET;
s_addr.sin_port=htons(port);
- if (connect(fd, (struct sockaddr *) &s_addr, sizeof(s_addr)) == -1) {
- printf (_("connect: %s.\n"), strerror(errno));
- exit(1);
- }
+ if (connect(fd, (struct sockaddr *) &s_addr, sizeof(s_addr)) == -1)
+ err(EXIT_FAILURE, _("connect"));
+
return fd;
}
static void
@@ -205,15 +195,12 @@ main(int argc, char **argv) {
pri = LOG_NOTICE;
logflags = 0;
while ((ch = getopt_long(argc, argv, "f:ip:st:u:dn:P:Vh",
- longopts, NULL)) != -1)
+ longopts, NULL)) != -1) {
switch((char)ch) {
case 'f': /* file to log */
- if (freopen(optarg, "r", stdin) == NULL) {
- int errsv = errno;
- (void)fprintf(stderr, _("logger: %s: %s.\n"),
- optarg, strerror(errsv));
- exit(1);
- }
+ if (freopen(optarg, "r", stdin) == NULL)
+ err(EXIT_FAILURE, _("file %s"),
+ optarg);
break;
case 'i': /* log process id also */
logflags |= LOG_PID;
@@ -255,6 +242,7 @@ main(int argc, char **argv) {
default:
usage(stderr);
}
+ }
argc -= optind;
argv += optind;
@@ -300,7 +288,7 @@ main(int argc, char **argv) {
else
mysyslog(LogSock, logflags, pri, tag, buf);
}
- } else
+ } else {
while (fgets(buf, sizeof(buf), stdin) != NULL) {
/* glibc is buggy and adds an additional newline,
so we have to remove it here until glibc is fixed */
@@ -314,11 +302,13 @@ main(int argc, char **argv) {
else
mysyslog(LogSock, logflags, pri, tag, buf);
}
+ }
if (!usock)
closelog();
else
close(LogSock);
- exit(0);
+
+ return EXIT_SUCCESS;
}
/*
@@ -335,11 +325,9 @@ pencode(s)
if (*s) {
*s = '\0';
fac = decode(save, facilitynames);
- if (fac < 0) {
- (void)fprintf(stderr,
- _("logger: unknown facility name: %s.\n"), save);
- exit(1);
- }
+ if (fac < 0)
+ errx(EXIT_FAILURE,
+ _("unknown facility name: %s."), save);
*s++ = '.';
}
else {
@@ -347,11 +335,9 @@ pencode(s)
s = save;
}
lev = decode(s, prioritynames);
- if (lev < 0) {
- (void)fprintf(stderr,
- _("logger: unknown priority name: %s.\n"), save);
- exit(1);
- }
+ if (lev < 0)
+ errx(EXIT_FAILURE,
+ _("unknown priority name: %s."), save);
return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
}
--
2.9.3
From 0050c529176f9a67e78a6f70674218fa511a8524 Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@justemail.net>
Date: Mon, 15 Aug 2011 14:04:20 +0200
Subject: [PATCH 005/115] logger: improve, sort and slice up usage() help text
Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
---
misc-utils/logger.c | 26 ++++++++++++++------------
1 file changed, 14 insertions(+), 12 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 5ecec76..5ad4ae8 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -137,22 +137,24 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
{
fprintf(out,
_("\nUsage:\n"
- " %s [options] message\n"),
+ " %s [options] [message]\n"),
program_invocation_short_name);
fprintf(out, _(
"\nOptions:\n"
- " -i, --id log process id\n"
- " -s, --stderr log message to standard error as well\n"
- " -f, --file FILE log contents of the specified file\n"
- " -p, --priority PRI enter message priority\n"
- " -t, --tag TAG mark every line with tag\n"
- " -u, --socket SOCK write to socket\n"
- " -d, --udp use udp (tcp is default)\n"
- " -n, --server ADDR write to remote syslog server\n"
- " -P, --port define port number\n"
- " -V, --version output version information and exit\n"
- " -h, --help display this help and exit\n\n"));
+ " -d, --udp use UDP (TCP is default)\n"
+ " -i, --id log the process ID too\n"
+ " -f, --file FILE log the contents of this file\n"
+ " -h, --help display this help text and exit\n"));
+ fprintf(out, _(
+ " -n, --server NAME write to this remote syslog server\n"
+ " -P, --port NUMBER use this UDP port\n"
+ " -p, --priority PRIO mark given message with this priority\n"
+ " -s, --stderr output message to standard error as well\n"));
+ fprintf(out, _(
+ " -t, --tag TAG mark every line with this tag\n"
+ " -u, --socket SOCKET write to this Unix socket\n"
+ " -V, --version output version information and exit\n\n"));
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}
--
2.9.3
From 2da4918695c84afcc01231743d44e60147f885b3 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Tue, 16 Aug 2011 12:23:15 +0200
Subject: [PATCH 006/115] logger: indent usage()
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 32 ++++++++++++++------------------
1 file changed, 14 insertions(+), 18 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 5ad4ae8..a63476f 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -135,26 +135,22 @@ mysyslog(int fd, int logflags, int pri, char *tag, char *msg) {
static void __attribute__ ((__noreturn__)) usage(FILE *out)
{
+ fputs(_("\nUsage:\n"), out);
fprintf(out,
- _("\nUsage:\n"
- " %s [options] [message]\n"),
- program_invocation_short_name);
+ _(" %s [options] [message]\n"), program_invocation_short_name);
- fprintf(out, _(
- "\nOptions:\n"
- " -d, --udp use UDP (TCP is default)\n"
- " -i, --id log the process ID too\n"
- " -f, --file FILE log the contents of this file\n"
- " -h, --help display this help text and exit\n"));
- fprintf(out, _(
- " -n, --server NAME write to this remote syslog server\n"
- " -P, --port NUMBER use this UDP port\n"
- " -p, --priority PRIO mark given message with this priority\n"
- " -s, --stderr output message to standard error as well\n"));
- fprintf(out, _(
- " -t, --tag TAG mark every line with this tag\n"
- " -u, --socket SOCKET write to this Unix socket\n"
- " -V, --version output version information and exit\n\n"));
+ fputs(_("\nOptions:\n"), out);
+ fputs(_(" -d, --udp use UDP (TCP is default)\n"
+ " -i, --id log the process ID too\n"
+ " -f, --file <file> log the contents of this file\n"
+ " -h, --help display this help text and exit\n"), out);
+ fputs(_(" -n, --server <name> write to this remote syslog server\n"
+ " -P, --port <number> use this UDP port\n"
+ " -p, --priority <prio> mark given message with this priority\n"
+ " -s, --stderr output message to standard error as well\n"), out);
+ fputs(_(" -t, --tag <tag> mark every line with this tag\n"
+ " -u, --socket <socket> write to this Unix socket\n"
+ " -V, --version output version information and exit\n\n"), out);
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}
--
2.9.3
From f3970c99b72ebe72a46e9b46fb34dd0a211bf943 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Mon, 12 Sep 2011 16:13:58 +0200
Subject: [PATCH 007/115] logger: non-ANSI definition [smatch scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index a63476f..a331f26 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -313,8 +313,7 @@ main(int argc, char **argv) {
* Decode a symbolic name to a numeric value
*/
int
-pencode(s)
- register char *s;
+pencode(char *s)
{
char *save;
int fac, lev;
@@ -340,9 +339,7 @@ pencode(s)
}
int
-decode(name, codetab)
- char *name;
- CODE *codetab;
+decode(char *name, CODE *codetab)
{
register CODE *c;
--
2.9.3
From 86248cd28a27bdd9a437e389966b0415e106802e Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Thu, 8 Dec 2011 10:20:22 +0100
Subject: [PATCH 008/115] logger: fix remote logging
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Reported-by: Hervé Quillévéré <herve_quillevere@herveq.tk>
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index a331f26..c89fca7 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -281,7 +281,7 @@ main(int argc, char **argv) {
}
}
if (p != buf) {
- if (!usock)
+ if (!usock && !udpserver)
syslog(pri, "%s", buf);
else
mysyslog(LogSock, logflags, pri, tag, buf);
@@ -295,13 +295,13 @@ main(int argc, char **argv) {
if (len > 0 && buf[len - 1] == '\n')
buf[len - 1] = '\0';
- if (!usock)
+ if (!usock && !udpserver)
syslog(pri, "%s", buf);
else
mysyslog(LogSock, logflags, pri, tag, buf);
}
}
- if (!usock)
+ if (!usock && !udpserver)
closelog();
else
close(LogSock);
--
2.9.3
From c05a80ca6385b85c30094390f31615152adfed2e Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Wed, 4 Apr 2012 19:56:48 +0200
Subject: [PATCH 009/115] misc-utils: verify writing to streams was successful
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index c89fca7..e3b67d2 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -52,6 +52,7 @@
#include <getopt.h>
#include "c.h"
+#include "closestream.h"
#include "nls.h"
#include "strutils.h"
@@ -188,6 +189,7 @@ main(int argc, char **argv) {
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
+ atexit(close_stdout);
tag = NULL;
pri = LOG_NOTICE;
--
2.9.3
From 1c935e725eed69edde5ac0db1cce6ab2123dd57a Mon Sep 17 00:00:00 2001
From: Jeremy Huntwork <jhuntwork@lightcubesolutions.com>
Date: Sun, 13 May 2012 16:31:48 +0000
Subject: [PATCH 010/115] Remove use of __P. Its intended usage was to support
pre-ANSI C compilers, but that is not even possible with the modern-day
codebase. Moreover, it breaks compiling on libcs that do not define this
legacy implementation-internal macro.
---
misc-utils/logger.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index e3b67d2..2ab76df 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -59,8 +59,8 @@
#define SYSLOG_NAMES
#include <syslog.h>
-int decode __P((char *, CODE *));
-int pencode __P((char *));
+int decode (char *, CODE *);
+int pencode (char *);
static int optd = 0;
static int udpport = 514;
--
2.9.3
From db41a4298f0719d7197e073b281ce9d36ed02820 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Tue, 15 May 2012 17:44:37 +0200
Subject: [PATCH 011/115] misc-utils: cleanup strtoxx_or_err()
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 13 ++++---------
1 file changed, 4 insertions(+), 9 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index e3b67d2..db3469c 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -63,7 +63,7 @@ int decode __P((char *, CODE *));
int pencode __P((char *));
static int optd = 0;
-static int udpport = 514;
+static uint16_t udpport = 514;
static int
myopenlog(const char *sock) {
@@ -86,7 +86,7 @@ myopenlog(const char *sock) {
}
static int
-udpopenlog(const char *servername,int port) {
+udpopenlog(const char *servername, uint16_t port) {
int fd;
struct sockaddr_in s_addr;
struct hostent *serverhost;
@@ -169,7 +169,6 @@ main(int argc, char **argv) {
char *usock = NULL;
char *udpserver = NULL;
int LogSock = -1;
- long tmpport;
static const struct option longopts[] = {
{ "id", no_argument, 0, 'i' },
@@ -225,12 +224,8 @@ main(int argc, char **argv) {
udpserver = optarg;
break;
case 'P': /* change udp port */
- tmpport = strtol_or_err(optarg,
- _("failed to parse port number"));
- if (tmpport < 0 || 65535 < tmpport)
- errx(EXIT_FAILURE, _("port `%ld' out of range"),
- tmpport);
- udpport = (int) tmpport;
+ udpport = strtou16_or_err(optarg,
+ _("invalid port number argument"));
break;
case 'V':
printf(_("%s from %s\n"), program_invocation_short_name,
--
2.9.3
From adddfd37a1def627b02c5a596f77e44afc35e567 Mon Sep 17 00:00:00 2001
From: maximilian attems <max@stro.at>
Date: Tue, 22 May 2012 16:33:23 +0200
Subject: [PATCH 012/115] misc-utils: cleanup unused strings.h includes
Noticed on klibc building.
Signed-off-by: maximilian attems <max@stro.at>
---
misc-utils/logger.c | 1 -
1 file changed, 1 deletion(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index d027b78..067272a 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -43,7 +43,6 @@
#include <stdio.h>
#include <ctype.h>
#include <string.h>
-#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
--
2.9.3
From 652add4c2cdd005fea8b5a5c198d31ecba5754ce Mon Sep 17 00:00:00 2001
From: Dave Reisner <dreisner@archlinux.org>
Date: Wed, 23 May 2012 16:35:58 -0400
Subject: [PATCH 013/115] logger: avoid explicit fclose(stdout)
This is done for us via an atexit hook since c05a80ca6385b8. Avoids a
useless 'Write error' on exit whenever invoking the tool.
Signed-off-by: Dave Reisner <dreisner@archlinux.org>
---
misc-utils/logger.c | 2 --
1 file changed, 2 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 067272a..19b4c47 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -248,8 +248,6 @@ main(int argc, char **argv) {
else
LogSock = myopenlog(usock);
- (void) fclose(stdout);
-
/* log input line if appropriate */
if (argc > 0) {
register char *p, *endp;
--
2.9.3
From 82054b1d99a982fd0d447d5f2cda6bd9b6ef429f Mon Sep 17 00:00:00 2001
From: Dave Reisner <dreisner@archlinux.org>
Date: Wed, 23 May 2012 16:36:00 -0400
Subject: [PATCH 014/115] logger: mark decode/pencode as static
Move these functions to the top of the file where they can be marked
static and the prototypes can be removed.
Signed-off-by: Dave Reisner <dreisner@archlinux.org>
---
misc-utils/logger.c | 84 +++++++++++++++++++++++------------------------------
1 file changed, 37 insertions(+), 47 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 19b4c47..b3fa546 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -58,12 +58,46 @@
#define SYSLOG_NAMES
#include <syslog.h>
-int decode (char *, CODE *);
-int pencode (char *);
-
static int optd = 0;
static uint16_t udpport = 514;
+static int decode(char *name, CODE *codetab)
+{
+ register CODE *c;
+
+ if (isdigit(*name))
+ return (atoi(name));
+
+ for (c = codetab; c->c_name; c++)
+ if (!strcasecmp(name, c->c_name))
+ return (c->c_val);
+
+ return -1;
+}
+
+static int pencode(char *s)
+{
+ char *save;
+ int fac, lev;
+
+ for (save = s; *s && *s != '.'; ++s);
+ if (*s) {
+ *s = '\0';
+ fac = decode(save, facilitynames);
+ if (fac < 0)
+ errx(EXIT_FAILURE, _("unknown facility name: %s."), save);
+ *s++ = '.';
+ }
+ else {
+ fac = LOG_USER;
+ s = save;
+ }
+ lev = decode(s, prioritynames);
+ if (lev < 0)
+ errx(EXIT_FAILURE, _("unknown priority name: %s."), save);
+ return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
+}
+
static int
myopenlog(const char *sock) {
int fd;
@@ -302,47 +336,3 @@ main(int argc, char **argv) {
return EXIT_SUCCESS;
}
-
-/*
- * Decode a symbolic name to a numeric value
- */
-int
-pencode(char *s)
-{
- char *save;
- int fac, lev;
-
- for (save = s; *s && *s != '.'; ++s);
- if (*s) {
- *s = '\0';
- fac = decode(save, facilitynames);
- if (fac < 0)
- errx(EXIT_FAILURE,
- _("unknown facility name: %s."), save);
- *s++ = '.';
- }
- else {
- fac = LOG_USER;
- s = save;
- }
- lev = decode(s, prioritynames);
- if (lev < 0)
- errx(EXIT_FAILURE,
- _("unknown priority name: %s."), save);
- return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
-}
-
-int
-decode(char *name, CODE *codetab)
-{
- register CODE *c;
-
- if (isdigit(*name))
- return (atoi(name));
-
- for (c = codetab; c->c_name; c++)
- if (!strcasecmp(name, c->c_name))
- return (c->c_val);
-
- return (-1);
-}
--
2.9.3
From 283d626b367c730e1338a43500f85deb7c3afb26 Mon Sep 17 00:00:00 2001
From: Dave Reisner <dreisner@archlinux.org>
Date: Mon, 28 May 2012 14:02:59 -0400
Subject: [PATCH 015/115] logger: use memcpy instead of bcopy
bcopy is marked legacy in POSIX.1-2001 and should not be used.
Signed-off-by: Dave Reisner <dreisner@archlinux.org>
---
misc-utils/logger.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index b3fa546..6f9edc2 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -130,7 +130,7 @@ udpopenlog(const char *servername, uint16_t port) {
if ((fd = socket(AF_INET, SOCK_DGRAM , 0)) == -1)
err(EXIT_FAILURE, _("socket"));
- bcopy(serverhost->h_addr,&s_addr.sin_addr,serverhost->h_length);
+ memcpy(&s_addr.sin_addr, serverhost->h_addr, serverhost->h_length);
s_addr.sin_family=AF_INET;
s_addr.sin_port=htons(port);
--
2.9.3
From 24f4db69724ab5dc8144eddb2d2feaa631245787 Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Fri, 12 Oct 2012 21:28:41 +0100
Subject: [PATCH 016/115] logger: replace gethostbyname() with getaddrinfo()
The gethostbyname() is legacy function which may be withdrawn in a
future.
Reference: http://pubs.opengroup.org/onlinepubs/009695399/functions/gethostbyname.html
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 30 +++++++++++++++---------------
1 file changed, 15 insertions(+), 15 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 6f9edc2..ecdbfe3 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -59,7 +59,6 @@
#include <syslog.h>
static int optd = 0;
-static uint16_t udpport = 514;
static int decode(char *name, CODE *codetab)
{
@@ -119,24 +118,25 @@ myopenlog(const char *sock) {
}
static int
-udpopenlog(const char *servername, uint16_t port) {
- int fd;
- struct sockaddr_in s_addr;
- struct hostent *serverhost;
+udpopenlog(const char *servername, const char *port) {
+ int fd, errcode;
+ struct addrinfo hints, *res;
- if ((serverhost = gethostbyname(servername)) == NULL )
- errx(EXIT_FAILURE, _("unable to resolve '%s'"), servername);
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_socktype = SOCK_DGRAM;
+ hints.ai_family = AF_UNSPEC;
- if ((fd = socket(AF_INET, SOCK_DGRAM , 0)) == -1)
+ errcode = getaddrinfo(servername, port, &hints, &res);
+ if (errcode != 0)
+ errx(EXIT_FAILURE, _("getaddrinfo %s:%s: %s"), servername, port,
+ gai_strerror(errcode));
+ if ((fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1)
err(EXIT_FAILURE, _("socket"));
- memcpy(&s_addr.sin_addr, serverhost->h_addr, serverhost->h_length);
- s_addr.sin_family=AF_INET;
- s_addr.sin_port=htons(port);
-
- if (connect(fd, (struct sockaddr *) &s_addr, sizeof(s_addr)) == -1)
+ if (connect(fd, res->ai_addr, res->ai_addrlen) == -1)
err(EXIT_FAILURE, _("connect"));
+ freeaddrinfo(res);
return fd;
}
static void
@@ -201,6 +201,7 @@ main(int argc, char **argv) {
char *tag, buf[1024];
char *usock = NULL;
char *udpserver = NULL;
+ char *udpport = NULL;
int LogSock = -1;
static const struct option longopts[] = {
@@ -257,8 +258,7 @@ main(int argc, char **argv) {
udpserver = optarg;
break;
case 'P': /* change udp port */
- udpport = strtou16_or_err(optarg,
- _("invalid port number argument"));
+ udpport = optarg;
break;
case 'V':
printf(_("%s from %s\n"), program_invocation_short_name,
--
2.9.3
From e421313dc253856af67cc267d2b33f856f18b0e3 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Fri, 25 Jan 2013 12:05:26 +0100
Subject: [PATCH 017/115] textual: use UTIL_LINUX_VERSION everywhere
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index ecdbfe3..63cd355 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -261,8 +261,7 @@ main(int argc, char **argv) {
udpport = optarg;
break;
case 'V':
- printf(_("%s from %s\n"), program_invocation_short_name,
- PACKAGE_STRING);
+ printf(UTIL_LINUX_VERSION);
exit(EXIT_SUCCESS);
case 'h':
usage(stdout);
--
2.9.3
From b50945d4ac2b415823cdc067efdcb54c80de8145 Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@justemail.net>
Date: Sat, 26 Jan 2013 17:07:51 +0100
Subject: [PATCH 018/115] =?UTF-8?q?textual:=20spell=20and=20encode=20the?=
=?UTF-8?q?=20name=20of=20Arkadiusz=20Mi=C5=9Bkiewicz=20correctly?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
---
misc-utils/logger.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 63cd355..30e8fa4 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -30,7 +30,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * 1999-02-22 Arkadiusz Mi∂kiewicz <misiek@pld.ORG.PL>
+ * 1999-02-22 Arkadiusz Mi≈õkiewicz <misiek@pld.ORG.PL>
* - added Native Language Support
* Sun Mar 21 1999 - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
* - fixed strerr(errno) in gettext calls
--
2.9.3
From fe6999da0a7e6f14f50342666b72a6ec41d1227a Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Tue, 2 Apr 2013 20:42:57 +0100
Subject: [PATCH 019/115] logger: make local sockets to try both stream and
datagram
When journald, from systemd, is in use logger gave following error.
$ logger --socket /dev/log test logger: connect /dev/log: Protocol wrong
type for socket
The journald supports only AF_DGRAM, and nothing else. Support for
AF_STREAM sockets was dropped because it messed up message ordering.
From an users point of view necessity to add --udp (UNIX(TM) Datagram
Protocol?) option when talking to syslog via unix socket felt confusing
and wrong. The command should know what is the socket type, and silently
use the correct one, unless the type is explicitely defined.
CC: Karel Zak <kzak@redhat.com>
Adviced-by: Lennart Poettering <lennart@poettering.net>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 42 +++++++++++++++++++++++++-----------------
1 file changed, 25 insertions(+), 17 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 30e8fa4..a6fafc1 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -58,8 +58,6 @@
#define SYSLOG_NAMES
#include <syslog.h>
-static int optd = 0;
-
static int decode(char *name, CODE *codetab)
{
register CODE *c;
@@ -98,23 +96,33 @@ static int pencode(char *s)
}
static int
-myopenlog(const char *sock) {
- int fd;
- static struct sockaddr_un s_addr; /* AF_UNIX address of local logger */
+myopenlog(const char *sock, int optd)
+{
+ int fd;
+ static struct sockaddr_un s_addr; /* AF_UNIX address of local logger */
- if (strlen(sock) >= sizeof(s_addr.sun_path))
- errx(EXIT_FAILURE, _("openlog %s: pathname too long"), sock);
+ if (strlen(sock) >= sizeof(s_addr.sun_path))
+ errx(EXIT_FAILURE, _("openlog %s: pathname too long"), sock);
- s_addr.sun_family = AF_UNIX;
- (void)strcpy(s_addr.sun_path, sock);
+ s_addr.sun_family = AF_UNIX;
+ (void)strcpy(s_addr.sun_path, sock);
- if ((fd = socket(AF_UNIX, optd ? SOCK_DGRAM : SOCK_STREAM, 0)) == -1)
- err(EXIT_FAILURE, _("socket %s"), sock);
+ if (optd == 0) {
+ if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
+ goto udp_socket;
+ if (connect(fd, (struct sockaddr *)&s_addr, sizeof(s_addr)) == -1) {
+ close(fd);
+ goto udp_socket;
+ }
+ } else {
+ udp_socket:
+ if ((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
+ err(EXIT_FAILURE, _("socket %s"), sock);
+ if (connect(fd, (struct sockaddr *)&s_addr, sizeof(s_addr)) == -1)
+ err(EXIT_FAILURE, _("connect %s"), sock);
+ }
- if (connect(fd, (struct sockaddr *) &s_addr, sizeof(s_addr)) == -1)
- err(EXIT_FAILURE, _("connect %s"), sock);
-
- return fd;
+ return fd;
}
static int
@@ -202,7 +210,7 @@ main(int argc, char **argv) {
char *usock = NULL;
char *udpserver = NULL;
char *udpport = NULL;
- int LogSock = -1;
+ int LogSock = -1, optd = 0;
static const struct option longopts[] = {
{ "id", no_argument, 0, 'i' },
@@ -279,7 +287,7 @@ main(int argc, char **argv) {
else if (udpserver)
LogSock = udpopenlog(udpserver,udpport);
else
- LogSock = myopenlog(usock);
+ LogSock = myopenlog(usock, optd);
/* log input line if appropriate */
if (argc > 0) {
--
2.9.3
From 68265d070d3041d16ab074ba25c610d6ef6c842e Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Fri, 5 Apr 2013 21:17:24 +0100
Subject: [PATCH 020/115] logger: allow to log using tcp transport protocol
This commit fixes error in usage() text, which claimed TCP is default
transport protocol. That was not true, and neither it should be. The
syslog messages has traditionally sent using UDP.
For the logger remains using UDP as first transport, but if it fails a
TCP connection is attempted. If an user wishes remote logging can be
forced to use either UDP or TCP. The service port for UDP is familiar
'syslog', for TCP the port 'syslog-conn' seems like reasonable default.
[kzak@redhat.com: - rename myopenlog to unix_socket(),
- always reset st to -1]
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 136 +++++++++++++++++++++++++++++++++-------------------
1 file changed, 87 insertions(+), 49 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index a6fafc1..7bb93ab 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -58,6 +58,12 @@
#define SYSLOG_NAMES
#include <syslog.h>
+enum {
+ TYPE_UDP = (1 << 1),
+ TYPE_TCP = (1 << 2),
+ ALL_TYPES = TYPE_UDP | TYPE_TCP
+};
+
static int decode(char *name, CODE *codetab)
{
register CODE *c;
@@ -96,57 +102,85 @@ static int pencode(char *s)
}
static int
-myopenlog(const char *sock, int optd)
+unix_socket(const char *path, const int socket_type)
{
- int fd;
+ int fd, i;
static struct sockaddr_un s_addr; /* AF_UNIX address of local logger */
- if (strlen(sock) >= sizeof(s_addr.sun_path))
- errx(EXIT_FAILURE, _("openlog %s: pathname too long"), sock);
+ if (strlen(path) >= sizeof(s_addr.sun_path))
+ errx(EXIT_FAILURE, _("openlog %s: pathname too long"), path);
s_addr.sun_family = AF_UNIX;
- (void)strcpy(s_addr.sun_path, sock);
+ strcpy(s_addr.sun_path, path);
- if (optd == 0) {
- if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
- goto udp_socket;
+ for (i = 2; i; i--) {
+ int st = -1;
+
+ if (i == 2 && socket_type & TYPE_UDP)
+ st = SOCK_DGRAM;
+ if (i == 1 && socket_type & TYPE_TCP)
+ st = SOCK_STREAM;
+ if (st == -1 || (fd = socket(AF_UNIX, st, 0)) == -1)
+ continue;
if (connect(fd, (struct sockaddr *)&s_addr, sizeof(s_addr)) == -1) {
close(fd);
- goto udp_socket;
+ continue;
}
- } else {
- udp_socket:
- if ((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
- err(EXIT_FAILURE, _("socket %s"), sock);
- if (connect(fd, (struct sockaddr *)&s_addr, sizeof(s_addr)) == -1)
- err(EXIT_FAILURE, _("connect %s"), sock);
+ break;
}
+ if (i == 0)
+ err(EXIT_FAILURE, _("socket %s"), path);
+
return fd;
}
static int
-udpopenlog(const char *servername, const char *port) {
- int fd, errcode;
+inet_socket(const char *servername, const char *port, const int socket_type)
+{
+ int fd, errcode, i;
struct addrinfo hints, *res;
+ const char *p = port;
- memset(&hints, 0, sizeof(hints));
- hints.ai_socktype = SOCK_DGRAM;
- hints.ai_family = AF_UNSPEC;
+ for (i = 2; i; i--) {
+ memset(&hints, 0, sizeof(hints));
+ if (i == 2 && socket_type & TYPE_UDP) {
+ hints.ai_socktype = SOCK_DGRAM;
+ if (port == NULL)
+ p = "syslog";
+ }
+ if (i == 1 && socket_type & TYPE_TCP) {
+ hints.ai_socktype = SOCK_STREAM;
+ if (port == NULL)
+ p = "syslog-conn";
+ }
+ if (hints.ai_socktype == 0)
+ continue;
+ hints.ai_family = AF_UNSPEC;
+ errcode = getaddrinfo(servername, p, &hints, &res);
+ if (errcode != 0)
+ errx(EXIT_FAILURE, _("getaddrinfo %s:%s: %s"),
+ servername, p, gai_strerror(errcode));
+ if ((fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) {
+ freeaddrinfo(res);
+ continue;
+ }
+ if (connect(fd, res->ai_addr, res->ai_addrlen) == -1) {
+ freeaddrinfo(res);
+ close(fd);
+ continue;
+ }
- errcode = getaddrinfo(servername, port, &hints, &res);
- if (errcode != 0)
- errx(EXIT_FAILURE, _("getaddrinfo %s:%s: %s"), servername, port,
- gai_strerror(errcode));
- if ((fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1)
- err(EXIT_FAILURE, _("socket"));
+ freeaddrinfo(res);
+ break;
+ }
- if (connect(fd, res->ai_addr, res->ai_addrlen) == -1)
- err(EXIT_FAILURE, _("connect"));
+ if (i == 0)
+ errx(EXIT_FAILURE, _("failed to connect %s port %s"), servername, p);
- freeaddrinfo(res);
return fd;
}
+
static void
mysyslog(int fd, int logflags, int pri, char *tag, char *msg) {
char buf[1000], pid[30], *cp, *tp;
@@ -182,7 +216,8 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
_(" %s [options] [message]\n"), program_invocation_short_name);
fputs(_("\nOptions:\n"), out);
- fputs(_(" -d, --udp use UDP (TCP is default)\n"
+ fputs(_(" -T, --tcp use TCP only\n"), out);
+ fputs(_(" -d, --udp use UDP only\n"
" -i, --id log the process ID too\n"
" -f, --file <file> log the contents of this file\n"
" -h, --help display this help text and exit\n"), out);
@@ -208,9 +243,9 @@ main(int argc, char **argv) {
int ch, logflags, pri;
char *tag, buf[1024];
char *usock = NULL;
- char *udpserver = NULL;
- char *udpport = NULL;
- int LogSock = -1, optd = 0;
+ char *server = NULL;
+ char *port = NULL;
+ int LogSock = -1, socket_type = ALL_TYPES;
static const struct option longopts[] = {
{ "id", no_argument, 0, 'i' },
@@ -220,6 +255,7 @@ main(int argc, char **argv) {
{ "tag", required_argument, 0, 't' },
{ "socket", required_argument, 0, 'u' },
{ "udp", no_argument, 0, 'd' },
+ { "tcp", no_argument, 0, 'T' },
{ "server", required_argument, 0, 'n' },
{ "port", required_argument, 0, 'P' },
{ "version", no_argument, 0, 'V' },
@@ -235,7 +271,7 @@ main(int argc, char **argv) {
tag = NULL;
pri = LOG_NOTICE;
logflags = 0;
- while ((ch = getopt_long(argc, argv, "f:ip:st:u:dn:P:Vh",
+ while ((ch = getopt_long(argc, argv, "f:ip:st:u:dTn:P:Vh",
longopts, NULL)) != -1) {
switch((char)ch) {
case 'f': /* file to log */
@@ -259,14 +295,16 @@ main(int argc, char **argv) {
usock = optarg;
break;
case 'd':
- optd = 1; /* use datagrams */
+ socket_type = TYPE_UDP;
break;
- case 'n': /* udp socket */
- optd = 1; /* use datagrams because udp */
- udpserver = optarg;
+ case 'T':
+ socket_type = TYPE_TCP;
break;
- case 'P': /* change udp port */
- udpport = optarg;
+ case 'n':
+ server = optarg;
+ break;
+ case 'P':
+ port = optarg;
break;
case 'V':
printf(UTIL_LINUX_VERSION);
@@ -282,12 +320,12 @@ main(int argc, char **argv) {
argv += optind;
/* setup for logging */
- if (!usock && !udpserver)
+ if (!usock && !server)
openlog(tag ? tag : getlogin(), logflags, 0);
- else if (udpserver)
- LogSock = udpopenlog(udpserver,udpport);
+ else if (server)
+ LogSock = inet_socket(server, port, socket_type);
else
- LogSock = myopenlog(usock, optd);
+ LogSock = unix_socket(usock, socket_type);
/* log input line if appropriate */
if (argc > 0) {
@@ -297,14 +335,14 @@ main(int argc, char **argv) {
for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
len = strlen(*argv);
if (p + len > endp && p > buf) {
- if (!usock && !udpserver)
+ if (!usock && !server)
syslog(pri, "%s", buf);
else
mysyslog(LogSock, logflags, pri, tag, buf);
p = buf;
}
if (len > sizeof(buf) - 1) {
- if (!usock && !udpserver)
+ if (!usock && !server)
syslog(pri, "%s", *argv++);
else
mysyslog(LogSock, logflags, pri, tag, *argv++);
@@ -316,7 +354,7 @@ main(int argc, char **argv) {
}
}
if (p != buf) {
- if (!usock && !udpserver)
+ if (!usock && !server)
syslog(pri, "%s", buf);
else
mysyslog(LogSock, logflags, pri, tag, buf);
@@ -330,13 +368,13 @@ main(int argc, char **argv) {
if (len > 0 && buf[len - 1] == '\n')
buf[len - 1] = '\0';
- if (!usock && !udpserver)
+ if (!usock && !server)
syslog(pri, "%s", buf);
else
mysyslog(LogSock, logflags, pri, tag, buf);
}
}
- if (!usock && !udpserver)
+ if (!usock && !server)
closelog();
else
close(LogSock);
--
2.9.3
From 2885c533fd11cb91ddd5dae17ed93b4de363496b Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Mon, 8 Apr 2013 16:39:59 +0200
Subject: [PATCH 021/115] logger: make socket initialization in main() more
readable
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 7bb93ab..c83c0b8 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -320,12 +320,12 @@ main(int argc, char **argv) {
argv += optind;
/* setup for logging */
- if (!usock && !server)
- openlog(tag ? tag : getlogin(), logflags, 0);
- else if (server)
+ if (server)
LogSock = inet_socket(server, port, socket_type);
- else
+ else if (usock)
LogSock = unix_socket(usock, socket_type);
+ else
+ openlog(tag ? tag : getlogin(), logflags, 0);
/* log input line if appropriate */
if (argc > 0) {
--
2.9.3
From 98920f80591b61ffdf7bfd99e2dd824406276e78 Mon Sep 17 00:00:00 2001
From: Dennis H Jensen <dennis.h.jensen@siemens.com>
Date: Wed, 22 May 2013 13:12:08 +0200
Subject: [PATCH 022/115] logger: add support for --prio-prefix when logging
stdin
This patch adds a new option to logger that will make it look for a
priority prefix <n> at the beginning of every line. The priority is
a single decimal number formed as explained in syslog(3).
If a prefix is found logger will log the message using the found
facility and level in that prefix, if the prefix doesn't contain a
facility the default facility specified by the -p option will be used.
If no prefix is found, logger will use the priority specified by -p.
[kzak@redhat.com: - add --prio-prefix to usage() output]
Signed-off-by: Dennis H Jensen <dennis.h.jensen@siemens.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 45 +++++++++++++++++++++++++++++++++++++++++----
1 file changed, 41 insertions(+), 4 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index c83c0b8..5b4d055 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -64,6 +64,30 @@ enum {
ALL_TYPES = TYPE_UDP | TYPE_TCP
};
+enum {
+ OPT_PRIO_PREFIX = CHAR_MAX + 1
+};
+
+
+static char* get_prio_prefix(char *msg, int *prio)
+{
+ int p;
+ char *end = NULL;
+ int facility = *prio & LOG_FACMASK;
+
+ errno = 0;
+ p = strtoul(msg + 1, &end, 10);
+
+ if (errno || !end || end == msg + 1 || end[0] != '>')
+ return msg;
+
+ if (p & LOG_FACMASK)
+ facility = p & LOG_FACMASK;
+
+ *prio = facility | (p & LOG_PRIMASK);
+ return end + 1;
+}
+
static int decode(char *name, CODE *codetab)
{
register CODE *c;
@@ -225,6 +249,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
" -P, --port <number> use this UDP port\n"
" -p, --priority <prio> mark given message with this priority\n"
" -s, --stderr output message to standard error as well\n"), out);
+ fputs(_(" --prio-prefix look for a prefix on every line read from stdin\n"), out);
fputs(_(" -t, --tag <tag> mark every line with this tag\n"
" -u, --socket <socket> write to this Unix socket\n"
" -V, --version output version information and exit\n\n"), out);
@@ -240,7 +265,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
*/
int
main(int argc, char **argv) {
- int ch, logflags, pri;
+ int ch, logflags, pri, prio_prefix;
char *tag, buf[1024];
char *usock = NULL;
char *server = NULL;
@@ -260,6 +285,7 @@ main(int argc, char **argv) {
{ "port", required_argument, 0, 'P' },
{ "version", no_argument, 0, 'V' },
{ "help", no_argument, 0, 'h' },
+ { "prio-prefix", no_argument, 0, OPT_PRIO_PREFIX },
{ NULL, 0, 0, 0 }
};
@@ -271,9 +297,10 @@ main(int argc, char **argv) {
tag = NULL;
pri = LOG_NOTICE;
logflags = 0;
+ prio_prefix = 0;
while ((ch = getopt_long(argc, argv, "f:ip:st:u:dTn:P:Vh",
longopts, NULL)) != -1) {
- switch((char)ch) {
+ switch (ch) {
case 'f': /* file to log */
if (freopen(optarg, "r", stdin) == NULL)
err(EXIT_FAILURE, _("file %s"),
@@ -311,6 +338,9 @@ main(int argc, char **argv) {
exit(EXIT_SUCCESS);
case 'h':
usage(stdout);
+ case OPT_PRIO_PREFIX:
+ prio_prefix = 1;
+ break;
case '?':
default:
usage(stderr);
@@ -360,6 +390,8 @@ main(int argc, char **argv) {
mysyslog(LogSock, logflags, pri, tag, buf);
}
} else {
+ char *msg;
+ int default_priority = pri;
while (fgets(buf, sizeof(buf), stdin) != NULL) {
/* glibc is buggy and adds an additional newline,
so we have to remove it here until glibc is fixed */
@@ -368,10 +400,15 @@ main(int argc, char **argv) {
if (len > 0 && buf[len - 1] == '\n')
buf[len - 1] = '\0';
+ msg = buf;
+ pri = default_priority;
+ if (prio_prefix && msg[0] == '<')
+ msg = get_prio_prefix(msg, &pri);
+
if (!usock && !server)
- syslog(pri, "%s", buf);
+ syslog(pri, "%s", msg);
else
- mysyslog(LogSock, logflags, pri, tag, buf);
+ mysyslog(LogSock, logflags, pri, tag, msg);
}
}
if (!usock && !server)
--
2.9.3
From 925aa9e85d8996a14a188ace521f1c5897f645ff Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Wed, 29 May 2013 10:54:51 +0200
Subject: [PATCH 023/115] logger: cleanup usage()
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 32 +++++++++++++++++---------------
1 file changed, 17 insertions(+), 15 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 5b4d055..75f0c8c 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -235,24 +235,26 @@ mysyslog(int fd, int logflags, int pri, char *tag, char *msg) {
static void __attribute__ ((__noreturn__)) usage(FILE *out)
{
- fputs(_("\nUsage:\n"), out);
- fprintf(out,
- _(" %s [options] [message]\n"), program_invocation_short_name);
+ fputs(USAGE_HEADER, out);
+ fprintf(out, _(" %s [options] [message]\n"), program_invocation_short_name);
- fputs(_("\nOptions:\n"), out);
+ fputs(USAGE_OPTIONS, out);
fputs(_(" -T, --tcp use TCP only\n"), out);
- fputs(_(" -d, --udp use UDP only\n"
- " -i, --id log the process ID too\n"
- " -f, --file <file> log the contents of this file\n"
- " -h, --help display this help text and exit\n"), out);
- fputs(_(" -n, --server <name> write to this remote syslog server\n"
- " -P, --port <number> use this UDP port\n"
- " -p, --priority <prio> mark given message with this priority\n"
- " -s, --stderr output message to standard error as well\n"), out);
+ fputs(_(" -d, --udp use UDP only\n"), out);
+ fputs(_(" -i, --id log the process ID too\n"), out);
+ fputs(_(" -f, --file <file> log the contents of this file\n"), out);
+ fputs(_(" -n, --server <name> write to this remote syslog server\n"), out);
+ fputs(_(" -P, --port <number> use this UDP port\n"), out);
+ fputs(_(" -p, --priority <prio> mark given message with this priority\n"), out);
fputs(_(" --prio-prefix look for a prefix on every line read from stdin\n"), out);
- fputs(_(" -t, --tag <tag> mark every line with this tag\n"
- " -u, --socket <socket> write to this Unix socket\n"
- " -V, --version output version information and exit\n\n"), out);
+ fputs(_(" -s, --stderr output message to standard error as well\n"), out);
+ fputs(_(" -t, --tag <tag> mark every line with this tag\n"), out);
+ fputs(_(" -u, --socket <socket> write to this Unix socket\n"), out);
+
+ fputs(USAGE_SEPARATOR, out);
+ fputs(USAGE_HELP, out);
+ fputs(USAGE_VERSION, out);
+ fprintf(out, USAGE_MAN_TAIL("logger(1)"));
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}
--
2.9.3
From 195c36032a313ffdc823d3a3a887fc56d31619b9 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Wed, 29 May 2013 10:56:05 +0200
Subject: [PATCH 024/115] logger: small coding style changes
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 15 +++++++--------
1 file changed, 7 insertions(+), 8 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 75f0c8c..161a7aa 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -125,8 +125,7 @@ static int pencode(char *s)
return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
}
-static int
-unix_socket(const char *path, const int socket_type)
+static int unix_socket(const char *path, const int socket_type)
{
int fd, i;
static struct sockaddr_un s_addr; /* AF_UNIX address of local logger */
@@ -159,8 +158,8 @@ unix_socket(const char *path, const int socket_type)
return fd;
}
-static int
-inet_socket(const char *servername, const char *port, const int socket_type)
+static int inet_socket(const char *servername, const char *port,
+ const int socket_type)
{
int fd, errcode, i;
struct addrinfo hints, *res;
@@ -205,8 +204,8 @@ inet_socket(const char *servername, const char *port, const int socket_type)
return fd;
}
-static void
-mysyslog(int fd, int logflags, int pri, char *tag, char *msg) {
+static void mysyslog(int fd, int logflags, int pri, char *tag, char *msg)
+{
char buf[1000], pid[30], *cp, *tp;
time_t now;
@@ -265,8 +264,8 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
* Reads from an input and arranges to write the result on the system
* log.
*/
-int
-main(int argc, char **argv) {
+int main(int argc, char **argv)
+{
int ch, logflags, pri, prio_prefix;
char *tag, buf[1024];
char *usock = NULL;
--
2.9.3
From 4ce393f4d899d44e1deb8c9e04142011c072b4e0 Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@justemail.net>
Date: Wed, 29 May 2013 21:52:56 +0200
Subject: [PATCH 025/115] textual: fix several typos and angular brackets in
messages
Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
---
misc-utils/logger.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 161a7aa..0717647 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -182,7 +182,7 @@ static int inet_socket(const char *servername, const char *port,
hints.ai_family = AF_UNSPEC;
errcode = getaddrinfo(servername, p, &hints, &res);
if (errcode != 0)
- errx(EXIT_FAILURE, _("getaddrinfo %s:%s: %s"),
+ errx(EXIT_FAILURE, _("failed to resolve name %s port %s: %s"),
servername, p, gai_strerror(errcode));
if ((fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) {
freeaddrinfo(res);
@@ -199,7 +199,7 @@ static int inet_socket(const char *servername, const char *port,
}
if (i == 0)
- errx(EXIT_FAILURE, _("failed to connect %s port %s"), servername, p);
+ errx(EXIT_FAILURE, _("failed to connect to %s port %s"), servername, p);
return fd;
}
@@ -235,7 +235,7 @@ static void mysyslog(int fd, int logflags, int pri, char *tag, char *msg)
static void __attribute__ ((__noreturn__)) usage(FILE *out)
{
fputs(USAGE_HEADER, out);
- fprintf(out, _(" %s [options] [message]\n"), program_invocation_short_name);
+ fprintf(out, _(" %s [options] [<message>]\n"), program_invocation_short_name);
fputs(USAGE_OPTIONS, out);
fputs(_(" -T, --tcp use TCP only\n"), out);
--
2.9.3
From 4b670c01dff7e5106d924a1267c23961b6926b6f Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Fri, 21 Feb 2014 19:25:30 +0000
Subject: [PATCH 026/115] logger: allow user to send structured journald
messages
This feature is hopefully mostly used to give MESSAGE_ID labels for
messages coming from scripts, making search of messages easy. The
logger(1) manual page update should give enough information how to use
--journald option.
[kzak@redhat.com: - add missing #ifdefs
- use xalloc.h]
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 69 insertions(+), 2 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 0717647..c07bfac 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -54,10 +54,15 @@
#include "closestream.h"
#include "nls.h"
#include "strutils.h"
+#include "xalloc.h"
#define SYSLOG_NAMES
#include <syslog.h>
+#ifdef HAVE_JOURNALD
+# include <systemd/sd-journal.h>
+#endif
+
enum {
TYPE_UDP = (1 << 1),
TYPE_TCP = (1 << 2),
@@ -65,7 +70,8 @@ enum {
};
enum {
- OPT_PRIO_PREFIX = CHAR_MAX + 1
+ OPT_PRIO_PREFIX = CHAR_MAX + 1,
+ OPT_JOURNALD
};
@@ -204,6 +210,40 @@ static int inet_socket(const char *servername, const char *port,
return fd;
}
+#ifdef HAVE_JOURNALD
+static int journald_entry(FILE *fp)
+{
+ struct iovec *iovec;
+ char *buf = NULL;
+ ssize_t sz;
+ int n, lines, vectors = 8, ret;
+ size_t dummy = 0;
+
+ iovec = xmalloc(vectors * sizeof(struct iovec));
+ for (lines = 0; /* nothing */ ; lines++) {
+ buf = NULL;
+ sz = getline(&buf, &dummy, fp);
+ if (sz == -1)
+ break;
+ if (0 < sz && buf[sz - 1] == '\n') {
+ sz--;
+ buf[sz] = '\0';
+ }
+ if (lines == vectors) {
+ vectors *= 2;
+ iovec = xrealloc(iovec, vectors * sizeof(struct iovec));
+ }
+ iovec[lines].iov_base = buf;
+ iovec[lines].iov_len = sz;
+ }
+ ret = sd_journal_sendv(iovec, lines);
+ for (n = 0; n < lines; n++)
+ free(iovec[n].iov_base);
+ free(iovec);
+ return ret;
+}
+#endif
+
static void mysyslog(int fd, int logflags, int pri, char *tag, char *msg)
{
char buf[1000], pid[30], *cp, *tp;
@@ -249,6 +289,9 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
fputs(_(" -s, --stderr output message to standard error as well\n"), out);
fputs(_(" -t, --tag <tag> mark every line with this tag\n"), out);
fputs(_(" -u, --socket <socket> write to this Unix socket\n"), out);
+#ifdef HAVE_JOURNALD
+ fputs(_(" --journald[=<file>] write journald entry\n"), out);
+#endif
fputs(USAGE_SEPARATOR, out);
fputs(USAGE_HELP, out);
@@ -272,7 +315,9 @@ int main(int argc, char **argv)
char *server = NULL;
char *port = NULL;
int LogSock = -1, socket_type = ALL_TYPES;
-
+#ifdef HAVE_JOURNALD
+ FILE *jfd = NULL;
+#endif
static const struct option longopts[] = {
{ "id", no_argument, 0, 'i' },
{ "stderr", no_argument, 0, 's' },
@@ -287,6 +332,9 @@ int main(int argc, char **argv)
{ "version", no_argument, 0, 'V' },
{ "help", no_argument, 0, 'h' },
{ "prio-prefix", no_argument, 0, OPT_PRIO_PREFIX },
+#ifdef HAVE_JOURNALD
+ { "journald", optional_argument, 0, OPT_JOURNALD },
+#endif
{ NULL, 0, 0, 0 }
};
@@ -342,6 +390,17 @@ int main(int argc, char **argv)
case OPT_PRIO_PREFIX:
prio_prefix = 1;
break;
+#ifdef HAVE_JOURNALD
+ case OPT_JOURNALD:
+ if (optarg) {
+ jfd = fopen(optarg, "r");
+ if (!jfd)
+ err(EXIT_FAILURE, _("cannot open %s"),
+ optarg);
+ } else
+ jfd = stdin;
+ break;
+#endif
case '?':
default:
usage(stderr);
@@ -351,6 +410,14 @@ int main(int argc, char **argv)
argv += optind;
/* setup for logging */
+#ifdef HAVE_JOURNALD
+ if (jfd) {
+ int ret = journald_entry(jfd);
+ if (stdin != jfd)
+ fclose(jfd);
+ return ret ? EXIT_FAILURE : EXIT_SUCCESS;
+ }
+#endif
if (server)
LogSock = inet_socket(server, port, socket_type);
else if (usock)
--
2.9.3
From ebff016a19b655ff8c087c3cbe42cb8061cc8f97 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Wed, 9 Apr 2014 12:23:30 +0200
Subject: [PATCH 027/115] build-sys: consolidate systemd support
* systemd (since v209) uses only one library (when compiled
without --enable-compat-libs)
* all systemd build-sys stuff is merged into HAVE_SYSTEMD
(automake) and HAVE_LIBSYSTEMD (C macro) now
* all is controlled by --with-systemd, default is to automatically
check for systemd libs
* no more --enable-socket-activation and --enable-journald
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index c07bfac..f3bdc90 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -59,7 +59,7 @@
#define SYSLOG_NAMES
#include <syslog.h>
-#ifdef HAVE_JOURNALD
+#ifdef HAVE_LIBSYSTEMD
# include <systemd/sd-journal.h>
#endif
@@ -210,7 +210,7 @@ static int inet_socket(const char *servername, const char *port,
return fd;
}
-#ifdef HAVE_JOURNALD
+#ifdef HAVE_LIBSYSTEMD
static int journald_entry(FILE *fp)
{
struct iovec *iovec;
@@ -289,7 +289,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
fputs(_(" -s, --stderr output message to standard error as well\n"), out);
fputs(_(" -t, --tag <tag> mark every line with this tag\n"), out);
fputs(_(" -u, --socket <socket> write to this Unix socket\n"), out);
-#ifdef HAVE_JOURNALD
+#ifdef HAVE_LIBSYSTEMD
fputs(_(" --journald[=<file>] write journald entry\n"), out);
#endif
@@ -315,7 +315,7 @@ int main(int argc, char **argv)
char *server = NULL;
char *port = NULL;
int LogSock = -1, socket_type = ALL_TYPES;
-#ifdef HAVE_JOURNALD
+#ifdef HAVE_LIBSYSTEMD
FILE *jfd = NULL;
#endif
static const struct option longopts[] = {
@@ -332,7 +332,7 @@ int main(int argc, char **argv)
{ "version", no_argument, 0, 'V' },
{ "help", no_argument, 0, 'h' },
{ "prio-prefix", no_argument, 0, OPT_PRIO_PREFIX },
-#ifdef HAVE_JOURNALD
+#ifdef HAVE_LIBSYSTEMD
{ "journald", optional_argument, 0, OPT_JOURNALD },
#endif
{ NULL, 0, 0, 0 }
@@ -390,7 +390,7 @@ int main(int argc, char **argv)
case OPT_PRIO_PREFIX:
prio_prefix = 1;
break;
-#ifdef HAVE_JOURNALD
+#ifdef HAVE_LIBSYSTEMD
case OPT_JOURNALD:
if (optarg) {
jfd = fopen(optarg, "r");
@@ -410,7 +410,7 @@ int main(int argc, char **argv)
argv += optind;
/* setup for logging */
-#ifdef HAVE_JOURNALD
+#ifdef HAVE_LIBSYSTEMD
if (jfd) {
int ret = journald_entry(jfd);
if (stdin != jfd)
--
2.9.3
From 047e2888a318ccad5f636edb6692cbdede80d14d Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sun, 11 May 2014 20:26:41 +0100
Subject: [PATCH 028/115] logger: fail when io vector number exceeds maximum
Earlier version silently failed without logging anything.
$ logger --journald=/etc/services ; echo $?
1
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index f3bdc90..fccba38 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -37,6 +37,7 @@
*/
#include <errno.h>
+#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
@@ -231,6 +232,8 @@ static int journald_entry(FILE *fp)
}
if (lines == vectors) {
vectors *= 2;
+ if (IOV_MAX < vectors)
+ errx(EXIT_FAILURE, _("maximum input lines (%d) exceeded"), IOV_MAX);
iovec = xrealloc(iovec, vectors * sizeof(struct iovec));
}
iovec[lines].iov_base = buf;
@@ -415,7 +418,9 @@ int main(int argc, char **argv)
int ret = journald_entry(jfd);
if (stdin != jfd)
fclose(jfd);
- return ret ? EXIT_FAILURE : EXIT_SUCCESS;
+ if (ret)
+ errx(EXIT_FAILURE, "journald entry could not be wrote");
+ return EXIT_SUCCESS;
}
#endif
if (server)
--
2.9.3
From 4d7d1af6745f79cdf591bf45d74a8cd9f4a65a6c Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sun, 11 May 2014 20:26:42 +0100
Subject: [PATCH 029/115] logger: check numeric priority and facility input
values
Earlier use of unknown facility or priority number was accepted, and
resulted in unexpected result. For example when looking journalctl
--priority=7.8 was converted to priotity 0 and facility 1.
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index fccba38..b7f9064 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -99,9 +99,20 @@ static int decode(char *name, CODE *codetab)
{
register CODE *c;
- if (isdigit(*name))
- return (atoi(name));
+ if (name == NULL || *name == '\0')
+ return -1;
+ if (isdigit(*name)) {
+ int num;
+ char *end = NULL;
+ num = strtol(name, &end, 10);
+ if (errno || name == end || (end && *end))
+ return -1;
+ for (c = codetab; c->c_name; c++)
+ if (num == c->c_val)
+ return num;
+ return -1;
+ }
for (c = codetab; c->c_name; c++)
if (!strcasecmp(name, c->c_name))
return (c->c_val);
--
2.9.3
From 89dda6786882d37701b24fb430466b6af71c3f50 Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sun, 11 May 2014 20:26:43 +0100
Subject: [PATCH 030/115] build-sys: remove unnecessary void casts
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index b7f9064..a862e55 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -275,7 +275,7 @@ static void mysyslog(int fd, int logflags, int pri, char *tag, char *msg)
if (!cp)
cp = "<someone>";
}
- (void)time(&now);
+ time(&now);
tp = ctime(&now)+4;
snprintf(buf, sizeof(buf), "<%d>%.15s %.200s%s: %.400s",
--
2.9.3
From 09af3db48e01e5744f15fd8a2395e0fed36526ea Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@justemail.net>
Date: Tue, 22 Jul 2014 22:56:27 +0200
Subject: [PATCH 031/115] textual: fix some typos and inconsistencies in
various messages
Fixing plain typos, miswordings, inconsistent periods, some missing
angular brackets, and a proper pluralization (even when it involves
a constant, because for some languages the precise value matters).
Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
---
misc-utils/logger.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index a862e55..0bf5e53 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -130,7 +130,7 @@ static int pencode(char *s)
*s = '\0';
fac = decode(save, facilitynames);
if (fac < 0)
- errx(EXIT_FAILURE, _("unknown facility name: %s."), save);
+ errx(EXIT_FAILURE, _("unknown facility name: %s"), save);
*s++ = '.';
}
else {
@@ -139,7 +139,7 @@ static int pencode(char *s)
}
lev = decode(s, prioritynames);
if (lev < 0)
- errx(EXIT_FAILURE, _("unknown priority name: %s."), save);
+ errx(EXIT_FAILURE, _("unknown priority name: %s"), save);
return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
}
--
2.9.3
From 633493beaf40ee88cdfb9db62a04a60f2a56c066 Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sun, 22 Jun 2014 22:59:57 +0100
Subject: [PATCH 032/115] logger: ensure program writes everything to syslog
file descriptor
It is fair assumption messages an user is asking to be wrote will be
attempted to be wrote as hard as possible.
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 0bf5e53..37d632e 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -51,6 +51,7 @@
#include <netdb.h>
#include <getopt.h>
+#include "all-io.h"
#include "c.h"
#include "closestream.h"
#include "nls.h"
@@ -281,8 +282,8 @@ static void mysyslog(int fd, int logflags, int pri, char *tag, char *msg)
snprintf(buf, sizeof(buf), "<%d>%.15s %.200s%s: %.400s",
pri, tp, cp, pid, msg);
- if (write(fd, buf, strlen(buf)+1) < 0)
- return; /* error */
+ if (write_all(fd, buf, strlen(buf)+1) < 0)
+ warn(_("write failed"));
}
}
--
2.9.3
From d8b616c216113ba4986a9d537719e5f434dca18b Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sun, 22 Jun 2014 23:02:43 +0100
Subject: [PATCH 033/115] logger: fix indentation issues
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 92 ++++++++++++++++++++++++++---------------------------
1 file changed, 46 insertions(+), 46 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 37d632e..0c6dbbd 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -76,8 +76,7 @@ enum {
OPT_JOURNALD
};
-
-static char* get_prio_prefix(char *msg, int *prio)
+static char *get_prio_prefix(char *msg, int *prio)
{
int p;
char *end = NULL;
@@ -133,8 +132,7 @@ static int pencode(char *s)
if (fac < 0)
errx(EXIT_FAILURE, _("unknown facility name: %s"), save);
*s++ = '.';
- }
- else {
+ } else {
fac = LOG_USER;
s = save;
}
@@ -261,30 +259,30 @@ static int journald_entry(FILE *fp)
static void mysyslog(int fd, int logflags, int pri, char *tag, char *msg)
{
- char buf[1000], pid[30], *cp, *tp;
- time_t now;
+ char buf[1000], pid[30], *cp, *tp;
+ time_t now;
- if (fd > -1) {
- if (logflags & LOG_PID)
- snprintf (pid, sizeof(pid), "[%d]", getpid());
- else
- pid[0] = 0;
- if (tag)
- cp = tag;
- else {
- cp = getlogin();
- if (!cp)
- cp = "<someone>";
- }
- time(&now);
- tp = ctime(&now)+4;
+ if (fd > -1) {
+ if (logflags & LOG_PID)
+ snprintf(pid, sizeof(pid), "[%d]", getpid());
+ else
+ pid[0] = 0;
+ if (tag)
+ cp = tag;
+ else {
+ cp = getlogin();
+ if (!cp)
+ cp = "<someone>";
+ }
+ time(&now);
+ tp = ctime(&now) + 4;
- snprintf(buf, sizeof(buf), "<%d>%.15s %.200s%s: %.400s",
- pri, tp, cp, pid, msg);
+ snprintf(buf, sizeof(buf), "<%d>%.15s %.200s%s: %.400s",
+ pri, tp, cp, pid, msg);
- if (write_all(fd, buf, strlen(buf)+1) < 0)
- warn(_("write failed"));
- }
+ if (write_all(fd, buf, strlen(buf) + 1) < 0)
+ warn(_("write failed"));
+ }
}
static void __attribute__ ((__noreturn__)) usage(FILE *out)
@@ -450,17 +448,19 @@ int main(int argc, char **argv)
for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
len = strlen(*argv);
if (p + len > endp && p > buf) {
- if (!usock && !server)
- syslog(pri, "%s", buf);
- else
- mysyslog(LogSock, logflags, pri, tag, buf);
+ if (!usock && !server)
+ syslog(pri, "%s", buf);
+ else
+ mysyslog(LogSock, logflags, pri, tag,
+ buf);
p = buf;
}
if (len > sizeof(buf) - 1) {
- if (!usock && !server)
- syslog(pri, "%s", *argv++);
- else
- mysyslog(LogSock, logflags, pri, tag, *argv++);
+ if (!usock && !server)
+ syslog(pri, "%s", *argv++);
+ else
+ mysyslog(LogSock, logflags, pri, tag,
+ *argv++);
} else {
if (p != buf)
*p++ = ' ';
@@ -469,31 +469,31 @@ int main(int argc, char **argv)
}
}
if (p != buf) {
- if (!usock && !server)
- syslog(pri, "%s", buf);
- else
- mysyslog(LogSock, logflags, pri, tag, buf);
+ if (!usock && !server)
+ syslog(pri, "%s", buf);
+ else
+ mysyslog(LogSock, logflags, pri, tag, buf);
}
} else {
char *msg;
int default_priority = pri;
while (fgets(buf, sizeof(buf), stdin) != NULL) {
- /* glibc is buggy and adds an additional newline,
- so we have to remove it here until glibc is fixed */
- int len = strlen(buf);
+ /* glibc is buggy and adds an additional newline,
+ so we have to remove it here until glibc is fixed */
+ int len = strlen(buf);
- if (len > 0 && buf[len - 1] == '\n')
- buf[len - 1] = '\0';
+ if (len > 0 && buf[len - 1] == '\n')
+ buf[len - 1] = '\0';
msg = buf;
pri = default_priority;
if (prio_prefix && msg[0] == '<')
msg = get_prio_prefix(msg, &pri);
- if (!usock && !server)
- syslog(pri, "%s", msg);
- else
- mysyslog(LogSock, logflags, pri, tag, msg);
+ if (!usock && !server)
+ syslog(pri, "%s", msg);
+ else
+ mysyslog(LogSock, logflags, pri, tag, msg);
}
}
if (!usock && !server)
--
2.9.3
From 019b97024fde3f07eaf541eef990762483369a11 Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sun, 22 Jun 2014 23:29:36 +0100
Subject: [PATCH 034/115] logger: do not rely only getlogin(3) telling who ran
the command
The getlogin(3) is known not to always work, and when that happens it is
reasonable to try determine user of name by looking process owner and
passwd information.
Reference: http://man7.org/linux/man-pages/man3/getlogin.3.html
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 20 ++++++++++++++------
1 file changed, 14 insertions(+), 6 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 0c6dbbd..5204cef 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -50,6 +50,7 @@
#include <arpa/inet.h>
#include <netdb.h>
#include <getopt.h>
+#include <pwd.h>
#include "all-io.h"
#include "c.h"
@@ -257,6 +258,16 @@ static int journald_entry(FILE *fp)
}
#endif
+static char *xgetlogin()
+{
+ char *cp;
+ struct passwd *pw;
+
+ if (!(cp = getlogin()) || !*cp)
+ cp = (pw = getpwuid(geteuid()))? pw->pw_name : "<someone>";
+ return cp;
+}
+
static void mysyslog(int fd, int logflags, int pri, char *tag, char *msg)
{
char buf[1000], pid[30], *cp, *tp;
@@ -269,11 +280,8 @@ static void mysyslog(int fd, int logflags, int pri, char *tag, char *msg)
pid[0] = 0;
if (tag)
cp = tag;
- else {
- cp = getlogin();
- if (!cp)
- cp = "<someone>";
- }
+ else
+ cp = xgetlogin();
time(&now);
tp = ctime(&now) + 4;
@@ -438,7 +446,7 @@ int main(int argc, char **argv)
else if (usock)
LogSock = unix_socket(usock, socket_type);
else
- openlog(tag ? tag : getlogin(), logflags, 0);
+ openlog(tag ? tag : xgetlogin(), logflags, 0);
/* log input line if appropriate */
if (argc > 0) {
--
2.9.3
From c462a8caf2b885df4a8e769d9056d8f63b82f6be Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sun, 22 Jun 2014 23:43:09 +0100
Subject: [PATCH 035/115] logger: refactor long if clause
When if clause that continues throughout whole function it usually can be
shorten to immediate action, e.g., in this case return on the spot not at
end of the function.
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 34 ++++++++++++++++------------------
1 file changed, 16 insertions(+), 18 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 5204cef..9f9650c 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -273,24 +273,22 @@ static void mysyslog(int fd, int logflags, int pri, char *tag, char *msg)
char buf[1000], pid[30], *cp, *tp;
time_t now;
- if (fd > -1) {
- if (logflags & LOG_PID)
- snprintf(pid, sizeof(pid), "[%d]", getpid());
- else
- pid[0] = 0;
- if (tag)
- cp = tag;
- else
- cp = xgetlogin();
- time(&now);
- tp = ctime(&now) + 4;
-
- snprintf(buf, sizeof(buf), "<%d>%.15s %.200s%s: %.400s",
- pri, tp, cp, pid, msg);
-
- if (write_all(fd, buf, strlen(buf) + 1) < 0)
- warn(_("write failed"));
- }
+ if (fd < 0)
+ return;
+ if (logflags & LOG_PID)
+ snprintf(pid, sizeof(pid), "[%d]", getpid());
+ else
+ pid[0] = 0;
+ if (tag)
+ cp = tag;
+ else
+ cp = xgetlogin();
+ time(&now);
+ tp = ctime(&now) + 4;
+ snprintf(buf, sizeof(buf), "<%d>%.15s %.200s%s: %.400s",
+ pri, tp, cp, pid, msg);
+ if (write_all(fd, buf, strlen(buf) + 1) < 0)
+ warn(_("write failed"));
}
static void __attribute__ ((__noreturn__)) usage(FILE *out)
--
2.9.3
From cfa77d2643bc4db2b04ecab3690d22982f605e79 Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Thu, 26 Jun 2014 21:48:25 +0100
Subject: [PATCH 036/115] logger: add function pointer to choose how logging is
done
This change paves way to adding support for both RFC 3164 and RFC 5424
syslog protocols.
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 105 +++++++++++++++++++++++++++-------------------------
1 file changed, 54 insertions(+), 51 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 9f9650c..ed45f93 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -77,6 +77,14 @@ enum {
OPT_JOURNALD
};
+struct logger_ctl {
+ int fd;
+ int logflags;
+ int pri;
+ char *tag;
+ void (*syslogfp)(struct logger_ctl *ctl, char *msg);
+};
+
static char *get_prio_prefix(char *msg, int *prio)
{
int p;
@@ -268,29 +276,34 @@ static char *xgetlogin()
return cp;
}
-static void mysyslog(int fd, int logflags, int pri, char *tag, char *msg)
+static void syslog_rfc3164(struct logger_ctl *ctl, char *msg)
{
char buf[1000], pid[30], *cp, *tp;
time_t now;
- if (fd < 0)
+ if (ctl->fd < 0)
return;
- if (logflags & LOG_PID)
+ if (ctl->logflags & LOG_PID)
snprintf(pid, sizeof(pid), "[%d]", getpid());
else
pid[0] = 0;
- if (tag)
- cp = tag;
+ if (ctl->tag)
+ cp = ctl->tag;
else
cp = xgetlogin();
time(&now);
tp = ctime(&now) + 4;
snprintf(buf, sizeof(buf), "<%d>%.15s %.200s%s: %.400s",
- pri, tp, cp, pid, msg);
- if (write_all(fd, buf, strlen(buf) + 1) < 0)
+ ctl->pri, tp, cp, pid, msg);
+ if (write_all(ctl->fd, buf, strlen(buf) + 1) < 0)
warn(_("write failed"));
}
+static void syslog_local(struct logger_ctl *ctl, char *msg)
+{
+ syslog(ctl->pri, "%s", msg);
+}
+
static void __attribute__ ((__noreturn__)) usage(FILE *out)
{
fputs(USAGE_HEADER, out);
@@ -328,12 +341,18 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
*/
int main(int argc, char **argv)
{
- int ch, logflags, pri, prio_prefix;
- char *tag, buf[1024];
+ struct logger_ctl ctl = {
+ .fd = -1,
+ .logflags = 0,
+ .pri = LOG_NOTICE,
+ .tag = NULL,
+ };
+ int ch, prio_prefix;
+ char buf[1024];
char *usock = NULL;
char *server = NULL;
char *port = NULL;
- int LogSock = -1, socket_type = ALL_TYPES;
+ int socket_type = ALL_TYPES;
#ifdef HAVE_LIBSYSTEMD
FILE *jfd = NULL;
#endif
@@ -362,10 +381,6 @@ int main(int argc, char **argv)
textdomain(PACKAGE);
atexit(close_stdout);
- tag = NULL;
- pri = LOG_NOTICE;
- logflags = 0;
- prio_prefix = 0;
while ((ch = getopt_long(argc, argv, "f:ip:st:u:dTn:P:Vh",
longopts, NULL)) != -1) {
switch (ch) {
@@ -375,16 +390,16 @@ int main(int argc, char **argv)
optarg);
break;
case 'i': /* log process id also */
- logflags |= LOG_PID;
+ ctl.logflags |= LOG_PID;
break;
case 'p': /* priority */
- pri = pencode(optarg);
+ ctl.pri = pencode(optarg);
break;
case 's': /* log to standard error */
- logflags |= LOG_PERROR;
+ ctl.logflags |= LOG_PERROR;
break;
case 't': /* tag */
- tag = optarg;
+ ctl.tag = optarg;
break;
case 'u': /* unix socket */
usock = optarg;
@@ -439,12 +454,16 @@ int main(int argc, char **argv)
return EXIT_SUCCESS;
}
#endif
- if (server)
- LogSock = inet_socket(server, port, socket_type);
- else if (usock)
- LogSock = unix_socket(usock, socket_type);
- else
- openlog(tag ? tag : xgetlogin(), logflags, 0);
+ if (server) {
+ ctl.fd = inet_socket(server, port, socket_type);
+ ctl.syslogfp = syslog_rfc3164;
+ } else if (usock) {
+ ctl.fd = unix_socket(usock, socket_type);
+ ctl.syslogfp = syslog_rfc3164;
+ } else {
+ openlog(ctl.tag ? ctl.tag : xgetlogin(), ctl.logflags, 0);
+ ctl.syslogfp = syslog_local;
+ }
/* log input line if appropriate */
if (argc > 0) {
@@ -454,35 +473,23 @@ int main(int argc, char **argv)
for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
len = strlen(*argv);
if (p + len > endp && p > buf) {
- if (!usock && !server)
- syslog(pri, "%s", buf);
- else
- mysyslog(LogSock, logflags, pri, tag,
- buf);
+ ctl.syslogfp(&ctl, buf);
p = buf;
}
- if (len > sizeof(buf) - 1) {
- if (!usock && !server)
- syslog(pri, "%s", *argv++);
- else
- mysyslog(LogSock, logflags, pri, tag,
- *argv++);
- } else {
+ if (len > sizeof(buf) - 1)
+ ctl.syslogfp(&ctl, *argv++);
+ else {
if (p != buf)
*p++ = ' ';
memmove(p, *argv++, len);
*(p += len) = '\0';
}
}
- if (p != buf) {
- if (!usock && !server)
- syslog(pri, "%s", buf);
- else
- mysyslog(LogSock, logflags, pri, tag, buf);
- }
+ if (p != buf)
+ ctl.syslogfp(&ctl, buf);
} else {
char *msg;
- int default_priority = pri;
+ int default_priority = ctl.pri;
while (fgets(buf, sizeof(buf), stdin) != NULL) {
/* glibc is buggy and adds an additional newline,
so we have to remove it here until glibc is fixed */
@@ -492,20 +499,16 @@ int main(int argc, char **argv)
buf[len - 1] = '\0';
msg = buf;
- pri = default_priority;
+ ctl.pri = default_priority;
if (prio_prefix && msg[0] == '<')
- msg = get_prio_prefix(msg, &pri);
-
- if (!usock && !server)
- syslog(pri, "%s", msg);
- else
- mysyslog(LogSock, logflags, pri, tag, msg);
+ msg = get_prio_prefix(msg, &ctl.pri);
+ ctl.syslogfp(&ctl, msg);
}
}
if (!usock && !server)
closelog();
else
- close(LogSock);
+ close(ctl.fd);
return EXIT_SUCCESS;
}
--
2.9.3
From c68a1cb490c5a4c142fef5eb153bf003f36226d5 Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sat, 26 Jul 2014 13:10:25 +0100
Subject: [PATCH 037/115] logger: tidy up main() by adding small functions and
ctl data
Add logger_open(), logger_command_line(), logger_stdin(), and
logger_close() functions, and move all remaining option argument
assignments to control structure.
Proposed-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 164 ++++++++++++++++++++++++++++++----------------------
1 file changed, 95 insertions(+), 69 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index ed45f93..b324782 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -81,7 +81,12 @@ struct logger_ctl {
int fd;
int logflags;
int pri;
+ int prio_prefix;
char *tag;
+ char *unix_socket;
+ char *server;
+ char *port;
+ int socket_type;
void (*syslogfp)(struct logger_ctl *ctl, char *msg);
};
@@ -304,6 +309,77 @@ static void syslog_local(struct logger_ctl *ctl, char *msg)
syslog(ctl->pri, "%s", msg);
}
+static void logger_open(struct logger_ctl *ctl)
+{
+ if (ctl->server) {
+ ctl->fd = inet_socket(ctl->server, ctl->port, ctl->socket_type);
+ ctl->syslogfp = syslog_rfc3164;
+ return;
+ }
+ if (ctl->unix_socket) {
+ ctl->fd = unix_socket(ctl->unix_socket, ctl->socket_type);
+ ctl->syslogfp = syslog_rfc3164;
+ return;
+ }
+ openlog(ctl->tag ? ctl->tag : xgetlogin(), ctl->logflags, 0);
+ ctl->syslogfp = syslog_local;
+}
+
+static void logger_command_line(struct logger_ctl *ctl, char **argv)
+{
+ char buf[4096];
+ char *p = buf;
+ const char *endp = buf + sizeof(buf) - 2;
+ size_t len;
+
+ while (*argv) {
+ len = strlen(*argv);
+ if (endp < p + len && p != buf) {
+ ctl->syslogfp(ctl, buf);
+ p = buf;
+ }
+ if (sizeof(buf) - 1 < len) {
+ ctl->syslogfp(ctl, *argv++);
+ continue;
+ }
+ if (p != buf)
+ *p++ = ' ';
+ memmove(p, *argv++, len);
+ *(p += len) = '\0';
+ }
+ if (p != buf)
+ ctl->syslogfp(ctl, buf);
+}
+
+static void logger_stdin(struct logger_ctl *ctl)
+{
+ char *msg;
+ int default_priority = ctl->pri;
+ char buf[1024];
+
+ while (fgets(buf, sizeof(buf), stdin) != NULL) {
+ int len = strlen(buf);
+
+ /* some glibc versions are buggy, they add an additional
+ * newline which is removed here. */
+ if (0 < len && buf[len - 1] == '\n')
+ buf[len - 1] = '\0';
+ msg = buf;
+ ctl->pri = default_priority;
+ if (ctl->prio_prefix && msg[0] == '<')
+ msg = get_prio_prefix(msg, &ctl->pri);
+ ctl->syslogfp(ctl, msg);
+ }
+}
+
+static void logger_close(struct logger_ctl *ctl)
+{
+ if (!ctl->unix_socket && !ctl->server)
+ closelog();
+ else
+ close(ctl->fd);
+}
+
static void __attribute__ ((__noreturn__)) usage(FILE *out)
{
fputs(USAGE_HEADER, out);
@@ -345,14 +421,14 @@ int main(int argc, char **argv)
.fd = -1,
.logflags = 0,
.pri = LOG_NOTICE,
+ .prio_prefix = 0,
.tag = NULL,
+ .unix_socket = NULL,
+ .server = NULL,
+ .port = NULL,
+ .socket_type = ALL_TYPES
};
- int ch, prio_prefix;
- char buf[1024];
- char *usock = NULL;
- char *server = NULL;
- char *port = NULL;
- int socket_type = ALL_TYPES;
+ int ch;
#ifdef HAVE_LIBSYSTEMD
FILE *jfd = NULL;
#endif
@@ -402,19 +478,19 @@ int main(int argc, char **argv)
ctl.tag = optarg;
break;
case 'u': /* unix socket */
- usock = optarg;
+ ctl.unix_socket = optarg;
break;
case 'd':
- socket_type = TYPE_UDP;
+ ctl.socket_type = TYPE_UDP;
break;
case 'T':
- socket_type = TYPE_TCP;
+ ctl.socket_type = TYPE_TCP;
break;
case 'n':
- server = optarg;
+ ctl.server = optarg;
break;
case 'P':
- port = optarg;
+ ctl.port = optarg;
break;
case 'V':
printf(UTIL_LINUX_VERSION);
@@ -422,7 +498,7 @@ int main(int argc, char **argv)
case 'h':
usage(stdout);
case OPT_PRIO_PREFIX:
- prio_prefix = 1;
+ ctl.prio_prefix = 1;
break;
#ifdef HAVE_LIBSYSTEMD
case OPT_JOURNALD:
@@ -442,8 +518,6 @@ int main(int argc, char **argv)
}
argc -= optind;
argv += optind;
-
- /* setup for logging */
#ifdef HAVE_LIBSYSTEMD
if (jfd) {
int ret = journald_entry(jfd);
@@ -454,61 +528,13 @@ int main(int argc, char **argv)
return EXIT_SUCCESS;
}
#endif
- if (server) {
- ctl.fd = inet_socket(server, port, socket_type);
- ctl.syslogfp = syslog_rfc3164;
- } else if (usock) {
- ctl.fd = unix_socket(usock, socket_type);
- ctl.syslogfp = syslog_rfc3164;
- } else {
- openlog(ctl.tag ? ctl.tag : xgetlogin(), ctl.logflags, 0);
- ctl.syslogfp = syslog_local;
- }
-
- /* log input line if appropriate */
- if (argc > 0) {
- register char *p, *endp;
- size_t len;
-
- for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
- len = strlen(*argv);
- if (p + len > endp && p > buf) {
- ctl.syslogfp(&ctl, buf);
- p = buf;
- }
- if (len > sizeof(buf) - 1)
- ctl.syslogfp(&ctl, *argv++);
- else {
- if (p != buf)
- *p++ = ' ';
- memmove(p, *argv++, len);
- *(p += len) = '\0';
- }
- }
- if (p != buf)
- ctl.syslogfp(&ctl, buf);
- } else {
- char *msg;
- int default_priority = ctl.pri;
- while (fgets(buf, sizeof(buf), stdin) != NULL) {
- /* glibc is buggy and adds an additional newline,
- so we have to remove it here until glibc is fixed */
- int len = strlen(buf);
-
- if (len > 0 && buf[len - 1] == '\n')
- buf[len - 1] = '\0';
-
- msg = buf;
- ctl.pri = default_priority;
- if (prio_prefix && msg[0] == '<')
- msg = get_prio_prefix(msg, &ctl.pri);
- ctl.syslogfp(&ctl, msg);
- }
- }
- if (!usock && !server)
- closelog();
+ logger_open(&ctl);
+ if (0 < argc)
+ logger_command_line(&ctl, argv);
else
- close(ctl.fd);
-
+ /* Note. --file <arg> reopens stdin making the below
+ * function to be used for file inputs. */
+ logger_stdin(&ctl);
+ logger_close(&ctl);
return EXIT_SUCCESS;
}
--
2.9.3
From 4de2e8a03859aaab2c25dc98f33409cd28de6acc Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Thu, 26 Jun 2014 16:13:55 +0100
Subject: [PATCH 038/115] logger: add rfc5424 support
Add support the more recent syslog protocol and make it default. The
older BSD syslog protocol can still be used with option --rfc3164.
Protocols are meaningful only when messages are sent to remote syslog
server.
Requested-by: Kodiak Firesmith <ksf@sei.cmu.edu>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 110 insertions(+), 9 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index b324782..4d28d12 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -44,6 +44,7 @@
#include <stdio.h>
#include <ctype.h>
#include <string.h>
+#include <sys/timex.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
@@ -74,20 +75,26 @@ enum {
enum {
OPT_PRIO_PREFIX = CHAR_MAX + 1,
- OPT_JOURNALD
+ OPT_JOURNALD,
+ OPT_RFC3164,
+ OPT_RFC5424
};
struct logger_ctl {
int fd;
int logflags;
int pri;
- int prio_prefix;
char *tag;
char *unix_socket;
char *server;
char *port;
int socket_type;
void (*syslogfp)(struct logger_ctl *ctl, char *msg);
+ unsigned int
+ prio_prefix:1, /* read priority from intput */
+ rfc5424_time:1, /* include time stamp */
+ rfc5424_tq:1, /* include time quality markup */
+ rfc5424_host:1; /* include hostname */
};
static char *get_prio_prefix(char *msg, int *prio)
@@ -304,6 +311,82 @@ static void syslog_rfc3164(struct logger_ctl *ctl, char *msg)
warn(_("write failed"));
}
+static void syslog_rfc5424(struct logger_ctl *ctl, char *msg)
+{
+ char *buf, pid[32], *tag;
+ struct ntptimeval ntptv;
+ char fmt[64], time[64], timeq[80], *hostname;
+ struct timeval tv;
+ struct tm *tm;
+
+ if (ctl->fd < 0)
+ return;
+ if (ctl->rfc5424_time) {
+ gettimeofday(&tv, NULL);
+ if ((tm = localtime(&tv.tv_sec)) != NULL) {
+ strftime(fmt, sizeof(fmt), " %Y-%m-%dT%H:%M:%S.%%06u%z",
+ tm);
+ snprintf(time, sizeof(time), fmt, tv.tv_usec);
+ } else
+ err(EXIT_FAILURE, _("localtime() failed"));
+ } else
+ time[0] = 0;
+ if (ctl->rfc5424_host) {
+ hostname = xgethostname();
+ /* Arbitrary looking 'if (var < strlen()) checks originate from
+ * RFC 5424 - 6 Syslog Message Format definition. */
+ if (255 < strlen(hostname))
+ errx(EXIT_FAILURE, _("hostname '%s' is too long"),
+ hostname);
+ } else
+ hostname = xcalloc(1, sizeof(char));
+ if (ctl->tag)
+ tag = ctl->tag;
+ else
+ tag = xgetlogin();
+ if (48 < strlen(tag))
+ errx(EXIT_FAILURE, _("tag '%s' is too long"), tag);
+ if (ctl->logflags & LOG_PID)
+ snprintf(pid, sizeof(pid), " %d", getpid());
+ else
+ pid[0] = 0;
+ if (ctl->rfc5424_tq) {
+ if (ntp_gettime(&ntptv) == TIME_OK)
+ snprintf(timeq, sizeof(timeq),
+ " [timeQuality tzKnown=\"1\" isSynced=\"1\" syncAccuracy=\"%ld\"]",
+ ntptv.maxerror);
+ else
+ snprintf(timeq, sizeof(timeq),
+ " [timeQuality tzKnown=\"1\" isSynced=\"0\"]");
+ } else
+ timeq[0] = 0;
+ xasprintf(&buf, "<%d>1%s%s%s %s -%s%s %s", ctl->pri, time,
+ hostname[0] ? " " : "", hostname, tag, pid, timeq, msg);
+ if (write_all(ctl->fd, buf, strlen(buf) + 1) < 0)
+ warn(_("write failed"));
+ free(hostname);
+ free(buf);
+}
+
+static void parse_rfc5424_flags(struct logger_ctl *ctl, char *optarg)
+{
+ char *in, *tok;
+
+ in = optarg;
+ while ((tok = strtok(in, ","))) {
+ in = NULL;
+ if (!strcmp(tok, "notime")) {
+ ctl->rfc5424_time = 0;
+ ctl->rfc5424_tq = 0;
+ } else if (!strcmp(tok, "notq"))
+ ctl->rfc5424_tq = 0;
+ else if (!strcmp(tok, "nohost"))
+ ctl->rfc5424_host = 0;
+ else
+ warnx(_("ignoring unknown option argument: %s"), tok);
+ }
+}
+
static void syslog_local(struct logger_ctl *ctl, char *msg)
{
syslog(ctl->pri, "%s", msg);
@@ -313,12 +396,14 @@ static void logger_open(struct logger_ctl *ctl)
{
if (ctl->server) {
ctl->fd = inet_socket(ctl->server, ctl->port, ctl->socket_type);
- ctl->syslogfp = syslog_rfc3164;
+ if (!ctl->syslogfp)
+ ctl->syslogfp = syslog_rfc5424;
return;
}
if (ctl->unix_socket) {
ctl->fd = unix_socket(ctl->unix_socket, ctl->socket_type);
- ctl->syslogfp = syslog_rfc3164;
+ if (!ctl->syslogfp)
+ ctl->syslogfp = syslog_rfc5424;
return;
}
openlog(ctl->tag ? ctl->tag : xgetlogin(), ctl->logflags, 0);
@@ -386,16 +471,19 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
fprintf(out, _(" %s [options] [<message>]\n"), program_invocation_short_name);
fputs(USAGE_OPTIONS, out);
- fputs(_(" -T, --tcp use TCP only\n"), out);
- fputs(_(" -d, --udp use UDP only\n"), out);
fputs(_(" -i, --id log the process ID too\n"), out);
fputs(_(" -f, --file <file> log the contents of this file\n"), out);
- fputs(_(" -n, --server <name> write to this remote syslog server\n"), out);
- fputs(_(" -P, --port <number> use this UDP port\n"), out);
fputs(_(" -p, --priority <prio> mark given message with this priority\n"), out);
fputs(_(" --prio-prefix look for a prefix on every line read from stdin\n"), out);
fputs(_(" -s, --stderr output message to standard error as well\n"), out);
fputs(_(" -t, --tag <tag> mark every line with this tag\n"), out);
+ fputs(_(" -n, --server <name> write to this remote syslog server\n"), out);
+ fputs(_(" -P, --port <number> use this UDP port\n"), out);
+ fputs(_(" -T, --tcp use TCP only\n"), out);
+ fputs(_(" -d, --udp use UDP only\n"), out);
+ fputs(_(" --rfc3164 use the obsolete BSD syslog protocol\n"), out);
+ fputs(_(" --rfc5424[=<notime,notq,nohost>]\n"), out);
+ fputs(_(" use the syslog protocol (default)\n"), out);
fputs(_(" -u, --socket <socket> write to this Unix socket\n"), out);
#ifdef HAVE_LIBSYSTEMD
fputs(_(" --journald[=<file>] write journald entry\n"), out);
@@ -426,7 +514,10 @@ int main(int argc, char **argv)
.unix_socket = NULL,
.server = NULL,
.port = NULL,
- .socket_type = ALL_TYPES
+ .socket_type = ALL_TYPES,
+ .rfc5424_time = 1,
+ .rfc5424_tq = 1,
+ .rfc5424_host = 1,
};
int ch;
#ifdef HAVE_LIBSYSTEMD
@@ -446,6 +537,8 @@ int main(int argc, char **argv)
{ "version", no_argument, 0, 'V' },
{ "help", no_argument, 0, 'h' },
{ "prio-prefix", no_argument, 0, OPT_PRIO_PREFIX },
+ { "rfc3164", no_argument, 0, OPT_RFC3164 },
+ { "rfc5424", optional_argument, 0, OPT_RFC5424 },
#ifdef HAVE_LIBSYSTEMD
{ "journald", optional_argument, 0, OPT_JOURNALD },
#endif
@@ -500,6 +593,14 @@ int main(int argc, char **argv)
case OPT_PRIO_PREFIX:
ctl.prio_prefix = 1;
break;
+ case OPT_RFC3164:
+ ctl.syslogfp = syslog_rfc3164;
+ break;
+ case OPT_RFC5424:
+ ctl.syslogfp = syslog_rfc5424;
+ if (optarg)
+ parse_rfc5424_flags(&ctl, optarg);
+ break;
#ifdef HAVE_LIBSYSTEMD
case OPT_JOURNALD:
if (optarg) {
--
2.9.3
From aab5b44405b9a6ada92e419e5a84cc0d1d4afee9 Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sat, 28 Jun 2014 16:08:28 +0100
Subject: [PATCH 039/115] logger: add process --id=parent optional argument
When scripts send several messages they will be easier to group together
when parent process id is printed rather than id of the each logger
process.
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 35 ++++++++++++++++++++++++++++-------
1 file changed, 28 insertions(+), 7 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 4d28d12..c1af735 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -92,6 +92,7 @@ struct logger_ctl {
void (*syslogfp)(struct logger_ctl *ctl, char *msg);
unsigned int
prio_prefix:1, /* read priority from intput */
+ ppid:1, /* include PPID instead of PID */
rfc5424_time:1, /* include time stamp */
rfc5424_tq:1, /* include time quality markup */
rfc5424_host:1; /* include hostname */
@@ -288,15 +289,25 @@ static char *xgetlogin()
return cp;
}
+static pid_t get_process_id(struct logger_ctl *ctl)
+{
+ pid_t id = 0;
+
+ if (ctl->logflags & LOG_PID)
+ id = ctl->ppid ? getppid() : getpid();
+ return id;
+}
+
static void syslog_rfc3164(struct logger_ctl *ctl, char *msg)
{
char buf[1000], pid[30], *cp, *tp;
time_t now;
+ pid_t process;
if (ctl->fd < 0)
return;
- if (ctl->logflags & LOG_PID)
- snprintf(pid, sizeof(pid), "[%d]", getpid());
+ if ((process = get_process_id(ctl)))
+ snprintf(pid, sizeof(pid), "[%d]", process);
else
pid[0] = 0;
if (ctl->tag)
@@ -318,6 +329,7 @@ static void syslog_rfc5424(struct logger_ctl *ctl, char *msg)
char fmt[64], time[64], timeq[80], *hostname;
struct timeval tv;
struct tm *tm;
+ pid_t process;
if (ctl->fd < 0)
return;
@@ -346,8 +358,8 @@ static void syslog_rfc5424(struct logger_ctl *ctl, char *msg)
tag = xgetlogin();
if (48 < strlen(tag))
errx(EXIT_FAILURE, _("tag '%s' is too long"), tag);
- if (ctl->logflags & LOG_PID)
- snprintf(pid, sizeof(pid), " %d", getpid());
+ if ((process = get_process_id(ctl)))
+ snprintf(pid, sizeof(pid), " %d", process);
else
pid[0] = 0;
if (ctl->rfc5424_tq) {
@@ -471,7 +483,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
fprintf(out, _(" %s [options] [<message>]\n"), program_invocation_short_name);
fputs(USAGE_OPTIONS, out);
- fputs(_(" -i, --id log the process ID too\n"), out);
+ fputs(_(" -i, --id[=pid|ppid] log PID or PPID (default is PID)\n"), out);
fputs(_(" -f, --file <file> log the contents of this file\n"), out);
fputs(_(" -p, --priority <prio> mark given message with this priority\n"), out);
fputs(_(" --prio-prefix look for a prefix on every line read from stdin\n"), out);
@@ -508,6 +520,7 @@ int main(int argc, char **argv)
struct logger_ctl ctl = {
.fd = -1,
.logflags = 0,
+ .ppid = 0,
.pri = LOG_NOTICE,
.prio_prefix = 0,
.tag = NULL,
@@ -524,7 +537,7 @@ int main(int argc, char **argv)
FILE *jfd = NULL;
#endif
static const struct option longopts[] = {
- { "id", no_argument, 0, 'i' },
+ { "id", optional_argument, 0, 'i' },
{ "stderr", no_argument, 0, 's' },
{ "file", required_argument, 0, 'f' },
{ "priority", required_argument, 0, 'p' },
@@ -550,7 +563,7 @@ int main(int argc, char **argv)
textdomain(PACKAGE);
atexit(close_stdout);
- while ((ch = getopt_long(argc, argv, "f:ip:st:u:dTn:P:Vh",
+ while ((ch = getopt_long(argc, argv, "f:i::p:st:u:dTn:P:Vh",
longopts, NULL)) != -1) {
switch (ch) {
case 'f': /* file to log */
@@ -560,6 +573,14 @@ int main(int argc, char **argv)
break;
case 'i': /* log process id also */
ctl.logflags |= LOG_PID;
+ if (optarg) {
+ if (!strcmp(optarg, "ppid"))
+ ctl.ppid = 1;
+ else if (!strcmp(optarg, "pid"))
+ ctl.ppid = 0;
+ else
+ warnx(_("ignoring unknown option argument: %s"), optarg);
+ }
break;
case 'p': /* priority */
ctl.pri = pencode(optarg);
--
2.9.3
From 4288f9f12c892499f08ba4673c34dc1bedc5dc59 Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sat, 28 Jun 2014 23:49:06 +0100
Subject: [PATCH 040/115] logger: make --stderr print remote server messages
Users wish to see the message should include also remote messages, not
only the one sent to locally via libc function.
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index c1af735..7239cc5 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -320,6 +320,8 @@ static void syslog_rfc3164(struct logger_ctl *ctl, char *msg)
ctl->pri, tp, cp, pid, msg);
if (write_all(ctl->fd, buf, strlen(buf) + 1) < 0)
warn(_("write failed"));
+ if (ctl->logflags & LOG_PERROR)
+ fprintf(stderr, "%s\n", buf);
}
static void syslog_rfc5424(struct logger_ctl *ctl, char *msg)
@@ -377,6 +379,8 @@ static void syslog_rfc5424(struct logger_ctl *ctl, char *msg)
if (write_all(ctl->fd, buf, strlen(buf) + 1) < 0)
warn(_("write failed"));
free(hostname);
+ if (ctl->logflags & LOG_PERROR)
+ fprintf(stderr, "%s\n", buf);
free(buf);
}
--
2.9.3
From 2e0fd22d5da49127167b2ff00d8e678a2bf8ac4b Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sat, 26 Jul 2014 15:19:26 +0100
Subject: [PATCH 041/115] logger: improve readablity of pencode() function
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 32 +++++++++++++++-----------------
1 file changed, 15 insertions(+), 17 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 7239cc5..afd9371 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -144,24 +144,22 @@ static int decode(char *name, CODE *codetab)
static int pencode(char *s)
{
- char *save;
- int fac, lev;
+ int facility, level;
+ char *separator;
- for (save = s; *s && *s != '.'; ++s);
- if (*s) {
- *s = '\0';
- fac = decode(save, facilitynames);
- if (fac < 0)
- errx(EXIT_FAILURE, _("unknown facility name: %s"), save);
- *s++ = '.';
- } else {
- fac = LOG_USER;
- s = save;
- }
- lev = decode(s, prioritynames);
- if (lev < 0)
- errx(EXIT_FAILURE, _("unknown priority name: %s"), save);
- return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
+ separator = strchr(s, '.');
+ if (separator) {
+ *separator = '\0';
+ facility = decode(s, facilitynames);
+ if (facility < 0)
+ errx(EXIT_FAILURE, _("unknown facility name: %s"), s);
+ s = ++separator;
+ } else
+ facility = LOG_USER;
+ level = decode(s, prioritynames);
+ if (level < 0)
+ errx(EXIT_FAILURE, _("unknown priority name: %s"), s);
+ return ((level & LOG_PRIMASK) | (facility & LOG_FACMASK));
}
static int unix_socket(const char *path, const int socket_type)
--
2.9.3
From 3d9f4b1d234e4a38ac6a5619c4cd93fc66d1d4c6 Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sun, 27 Jul 2014 18:01:09 +0100
Subject: [PATCH 042/115] logger: warn when --file and command line message are
combined
When --file is combined with command line arguments the later has
silently been ignored. This commit makes user to be aware the logger
will not use command line arguments when --file is specified.
Reported-by: "Daniel 'DaB.' Baur" <debian@daniel.baur4.info>
Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=467244
CC: Andreas Henriksson <andreas@fatal.se>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index afd9371..ac583fd 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -535,6 +535,7 @@ int main(int argc, char **argv)
.rfc5424_host = 1,
};
int ch;
+ int stdout_reopened = 0;
#ifdef HAVE_LIBSYSTEMD
FILE *jfd = NULL;
#endif
@@ -570,8 +571,8 @@ int main(int argc, char **argv)
switch (ch) {
case 'f': /* file to log */
if (freopen(optarg, "r", stdin) == NULL)
- err(EXIT_FAILURE, _("file %s"),
- optarg);
+ err(EXIT_FAILURE, _("file %s"), optarg);
+ stdout_reopened = 1;
break;
case 'i': /* log process id also */
ctl.logflags |= LOG_PID;
@@ -642,6 +643,8 @@ int main(int argc, char **argv)
}
argc -= optind;
argv += optind;
+ if (stdout_reopened && argc)
+ warnx(_("--file <file> and <message> are mutually exclusive, message is ignored"));
#ifdef HAVE_LIBSYSTEMD
if (jfd) {
int ret = journald_entry(jfd);
--
2.9.3
From 133bf77bc28db78dee5b200c428c104a5727398a Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sun, 27 Jul 2014 18:33:22 +0100
Subject: [PATCH 043/115] logger: add hostname to rfc3164 message
This makes the obsolete protocol a little bit more compliant with the
internet standard, but few should care now when we have rfc5424 support,
and rfc3164 feels broken. For example it requires hostname to be not
fully qualified, which is hard to understand, and should make users to
prefer the new protocol.
Reported-by: Frank Thilo <thilo@unix-ag.org>
Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=705217
CC: Andreas Henriksson <andreas@fatal.se>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index ac583fd..9da404d 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -298,7 +298,7 @@ static pid_t get_process_id(struct logger_ctl *ctl)
static void syslog_rfc3164(struct logger_ctl *ctl, char *msg)
{
- char buf[1000], pid[30], *cp, *tp;
+ char buf[1000], pid[30], *cp, *tp, *hostname, *dot;
time_t now;
pid_t process;
@@ -312,10 +312,15 @@ static void syslog_rfc3164(struct logger_ctl *ctl, char *msg)
cp = ctl->tag;
else
cp = xgetlogin();
+ hostname = xgethostname();
+ dot = strchr(hostname, '.');
+ if (dot)
+ *dot = '\0';
time(&now);
tp = ctime(&now) + 4;
- snprintf(buf, sizeof(buf), "<%d>%.15s %.200s%s: %.400s",
- ctl->pri, tp, cp, pid, msg);
+ snprintf(buf, sizeof(buf), "<%d>%.15s %s %.200s%s: %.400s",
+ ctl->pri, tp, hostname, cp, pid, msg);
+ free(hostname);
if (write_all(ctl->fd, buf, strlen(buf) + 1) < 0)
warn(_("write failed"));
if (ctl->logflags & LOG_PERROR)
--
2.9.3
From 852feb7299fd517d546a13e4c0884597b77d0cfa Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Tue, 29 Jul 2014 11:43:01 +0200
Subject: [PATCH 044/115] logger: clean up strings usage
- use allocated buffers to compose the final messages
- initialize static buffers to zero
- remove strlen-after-sprintf
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 66 ++++++++++++++++++++++++++++++-----------------------
1 file changed, 38 insertions(+), 28 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 9da404d..af65af2 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -298,56 +298,63 @@ static pid_t get_process_id(struct logger_ctl *ctl)
static void syslog_rfc3164(struct logger_ctl *ctl, char *msg)
{
- char buf[1000], pid[30], *cp, *tp, *hostname, *dot;
+ char *buf, pid[30] = { '\0' }, *cp, *tp, *hostname, *dot;
time_t now;
pid_t process;
+ int len;
if (ctl->fd < 0)
return;
if ((process = get_process_id(ctl)))
snprintf(pid, sizeof(pid), "[%d]", process);
- else
- pid[0] = 0;
- if (ctl->tag)
- cp = ctl->tag;
- else
- cp = xgetlogin();
+
+ cp = ctl->tag ? ctl->tag : xgetlogin();
+
hostname = xgethostname();
dot = strchr(hostname, '.');
if (dot)
*dot = '\0';
+
time(&now);
tp = ctime(&now) + 4;
- snprintf(buf, sizeof(buf), "<%d>%.15s %s %.200s%s: %.400s",
+
+ len = xasprintf(&buf, "<%d>%.15s %s %.200s%s: %.400s",
ctl->pri, tp, hostname, cp, pid, msg);
- free(hostname);
- if (write_all(ctl->fd, buf, strlen(buf) + 1) < 0)
+
+ if (write_all(ctl->fd, buf, len) < 0)
warn(_("write failed"));
if (ctl->logflags & LOG_PERROR)
fprintf(stderr, "%s\n", buf);
+
+ free(hostname);
+ free(buf);
}
static void syslog_rfc5424(struct logger_ctl *ctl, char *msg)
{
- char *buf, pid[32], *tag;
+ char *buf, *tag = NULL, *hostname = NULL;
+ char pid[32] = { '\0' }, time[64] = { '\0' }, timeq[80] = { '\0' };
struct ntptimeval ntptv;
- char fmt[64], time[64], timeq[80], *hostname;
struct timeval tv;
struct tm *tm;
pid_t process;
+ int len;
if (ctl->fd < 0)
return;
+
if (ctl->rfc5424_time) {
gettimeofday(&tv, NULL);
if ((tm = localtime(&tv.tv_sec)) != NULL) {
+ char fmt[64];
+
strftime(fmt, sizeof(fmt), " %Y-%m-%dT%H:%M:%S.%%06u%z",
tm);
snprintf(time, sizeof(time), fmt, tv.tv_usec);
} else
err(EXIT_FAILURE, _("localtime() failed"));
- } else
- time[0] = 0;
+ }
+
if (ctl->rfc5424_host) {
hostname = xgethostname();
/* Arbitrary looking 'if (var < strlen()) checks originate from
@@ -355,18 +362,16 @@ static void syslog_rfc5424(struct logger_ctl *ctl, char *msg)
if (255 < strlen(hostname))
errx(EXIT_FAILURE, _("hostname '%s' is too long"),
hostname);
- } else
- hostname = xcalloc(1, sizeof(char));
- if (ctl->tag)
- tag = ctl->tag;
- else
- tag = xgetlogin();
+ }
+
+ tag = ctl->tag ? ctl->tag : xgetlogin();
+
if (48 < strlen(tag))
errx(EXIT_FAILURE, _("tag '%s' is too long"), tag);
+
if ((process = get_process_id(ctl)))
snprintf(pid, sizeof(pid), " %d", process);
- else
- pid[0] = 0;
+
if (ctl->rfc5424_tq) {
if (ntp_gettime(&ntptv) == TIME_OK)
snprintf(timeq, sizeof(timeq),
@@ -375,15 +380,20 @@ static void syslog_rfc5424(struct logger_ctl *ctl, char *msg)
else
snprintf(timeq, sizeof(timeq),
" [timeQuality tzKnown=\"1\" isSynced=\"0\"]");
- } else
- timeq[0] = 0;
- xasprintf(&buf, "<%d>1%s%s%s %s -%s%s %s", ctl->pri, time,
- hostname[0] ? " " : "", hostname, tag, pid, timeq, msg);
- if (write_all(ctl->fd, buf, strlen(buf) + 1) < 0)
+ }
+
+ len = xasprintf(&buf, "<%d>1%s%s%s %s -%s%s %s", ctl->pri, time,
+ hostname ? " " : "",
+ hostname ? hostname : "",
+ tag, pid, timeq, msg);
+
+ if (write_all(ctl->fd, buf, len) < 0)
warn(_("write failed"));
- free(hostname);
+
if (ctl->logflags & LOG_PERROR)
fprintf(stderr, "%s\n", buf);
+
+ free(hostname);
free(buf);
}
--
2.9.3
From e598686d3c0925bb79989684ad1034e094f52c4c Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Tue, 29 Jul 2014 11:49:00 +0200
Subject: [PATCH 045/115] logger: fix -i=ppid
For short options (-i) the optional argument contains '='.
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index af65af2..a9d2188 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -592,9 +592,13 @@ int main(int argc, char **argv)
case 'i': /* log process id also */
ctl.logflags |= LOG_PID;
if (optarg) {
- if (!strcmp(optarg, "ppid"))
+ const char *p = optarg;
+
+ if (*p == '=')
+ p++;
+ if (!strcmp(p, "ppid"))
ctl.ppid = 1;
- else if (!strcmp(optarg, "pid"))
+ else if (!strcmp(p, "pid"))
ctl.ppid = 0;
else
warnx(_("ignoring unknown option argument: %s"), optarg);
--
2.9.3
From 1042ce18dfb4fa89daae3c508f66f942be99d0ce Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Tue, 29 Jul 2014 12:08:47 +0200
Subject: [PATCH 046/115] logger: require --{server,socket} to remote
rfc542/rfc3164 log
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index a9d2188..b07edc2 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -435,6 +435,11 @@ static void logger_open(struct logger_ctl *ctl)
ctl->syslogfp = syslog_rfc5424;
return;
}
+
+ if (ctl->syslogfp == syslog_rfc5424 || ctl->syslogfp == syslog_rfc3164)
+ errx(EXIT_FAILURE, _("--server or --socket are required to "
+ "log by --rfc5424 or --rfc3164."));
+
openlog(ctl->tag ? ctl->tag : xgetlogin(), ctl->logflags, 0);
ctl->syslogfp = syslog_local;
}
--
2.9.3
From 7eec45f2d868b89aa3f9059aae1f340a099824d0 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Tue, 29 Jul 2014 12:13:24 +0200
Subject: [PATCH 047/115] logger: fix untranslated message
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index b07edc2..7b10df8 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -675,7 +675,7 @@ int main(int argc, char **argv)
if (stdin != jfd)
fclose(jfd);
if (ret)
- errx(EXIT_FAILURE, "journald entry could not be wrote");
+ errx(EXIT_FAILURE, _("journald entry could not be wrote"));
return EXIT_SUCCESS;
}
#endif
--
2.9.3
From 1d23119072a3a117e7bb83694bd30b9d50961374 Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@justemail.net>
Date: Wed, 6 Aug 2014 22:35:06 +0200
Subject: [PATCH 048/115] textual: remove some inconsistent periods from error
messages
While doing so, also improve translatability and some wordings.
Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
---
misc-utils/logger.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 7b10df8..4836b8f 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -438,7 +438,7 @@ static void logger_open(struct logger_ctl *ctl)
if (ctl->syslogfp == syslog_rfc5424 || ctl->syslogfp == syslog_rfc3164)
errx(EXIT_FAILURE, _("--server or --socket are required to "
- "log by --rfc5424 or --rfc3164."));
+ "log by --rfc5424 or --rfc3164"));
openlog(ctl->tag ? ctl->tag : xgetlogin(), ctl->logflags, 0);
ctl->syslogfp = syslog_local;
--
2.9.3
From 1d57503378bdcd838365d625f6d2d0a09da9c29d Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sat, 9 Aug 2014 00:49:46 +0100
Subject: [PATCH 049/115] logger: allow use of --id=ppid when logging locally
There is no obvious way to make syslog(3) to print both pid or ppid, so
duplicate the libc syslog() to logger. Making the ppid printing work
using unix socket has side effect of local becoming capable to use both
rfc format output, which is hopefully seen as good thing. The
syslog_local() is format wise one-to-one copy with glibc syslog(3)
format.
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 38 +++++++++++++++++++++++++++-----------
1 file changed, 27 insertions(+), 11 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 4836b8f..d1b93d0 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -418,7 +418,30 @@ static void parse_rfc5424_flags(struct logger_ctl *ctl, char *optarg)
static void syslog_local(struct logger_ctl *ctl, char *msg)
{
- syslog(ctl->pri, "%s", msg);
+ char *buf, *tag;
+ char time[32], pid[32];
+ struct timeval tv;
+ struct tm *tm;
+ pid_t process;
+ int len;
+
+ gettimeofday(&tv, NULL);
+ tm = localtime(&tv.tv_sec);
+ strftime(time, sizeof(time), "%h %e %T", tm);
+
+ tag = ctl->tag ? ctl->tag : program_invocation_short_name;
+
+ if ((process = get_process_id(ctl)))
+ snprintf(pid, sizeof(pid), "[%d]", process);
+ else
+ pid[0] = '\0';
+
+ len = xasprintf(&buf, "<%d>%s %s%s: %s", ctl->pri, time, tag, pid, msg);
+ if (write_all(ctl->fd, buf, len) < 0)
+ warn(_("write failed"));
+ if (ctl->logflags & LOG_PERROR)
+ fprintf(stderr, "%s\n", buf);
+ free(buf);
}
static void logger_open(struct logger_ctl *ctl)
@@ -435,12 +458,7 @@ static void logger_open(struct logger_ctl *ctl)
ctl->syslogfp = syslog_rfc5424;
return;
}
-
- if (ctl->syslogfp == syslog_rfc5424 || ctl->syslogfp == syslog_rfc3164)
- errx(EXIT_FAILURE, _("--server or --socket are required to "
- "log by --rfc5424 or --rfc3164"));
-
- openlog(ctl->tag ? ctl->tag : xgetlogin(), ctl->logflags, 0);
+ ctl->fd = unix_socket("/dev/log", ctl->socket_type);
ctl->syslogfp = syslog_local;
}
@@ -493,10 +511,8 @@ static void logger_stdin(struct logger_ctl *ctl)
static void logger_close(struct logger_ctl *ctl)
{
- if (!ctl->unix_socket && !ctl->server)
- closelog();
- else
- close(ctl->fd);
+ if (close(ctl->fd) != 0)
+ err(EXIT_FAILURE, _("close failed"));
}
static void __attribute__ ((__noreturn__)) usage(FILE *out)
--
2.9.3
From 35d3619793aad012bedc8ad7bff47218e2b5d663 Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sat, 9 Aug 2014 01:04:50 +0100
Subject: [PATCH 050/115] logger: remove openlog(3) options
One variable less, and more importantly bit operations become unnecessary
in if statements.
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index d1b93d0..8ae00b2 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -82,7 +82,6 @@ enum {
struct logger_ctl {
int fd;
- int logflags;
int pri;
char *tag;
char *unix_socket;
@@ -92,7 +91,9 @@ struct logger_ctl {
void (*syslogfp)(struct logger_ctl *ctl, char *msg);
unsigned int
prio_prefix:1, /* read priority from intput */
+ pid:1, /* print PID, or PPID if it is enabled as well*/
ppid:1, /* include PPID instead of PID */
+ stderr_printout:1, /* output message to stderr */
rfc5424_time:1, /* include time stamp */
rfc5424_tq:1, /* include time quality markup */
rfc5424_host:1; /* include hostname */
@@ -291,7 +292,7 @@ static pid_t get_process_id(struct logger_ctl *ctl)
{
pid_t id = 0;
- if (ctl->logflags & LOG_PID)
+ if (ctl->pid)
id = ctl->ppid ? getppid() : getpid();
return id;
}
@@ -323,7 +324,7 @@ static void syslog_rfc3164(struct logger_ctl *ctl, char *msg)
if (write_all(ctl->fd, buf, len) < 0)
warn(_("write failed"));
- if (ctl->logflags & LOG_PERROR)
+ if (ctl->stderr_printout)
fprintf(stderr, "%s\n", buf);
free(hostname);
@@ -390,7 +391,7 @@ static void syslog_rfc5424(struct logger_ctl *ctl, char *msg)
if (write_all(ctl->fd, buf, len) < 0)
warn(_("write failed"));
- if (ctl->logflags & LOG_PERROR)
+ if (ctl->stderr_printout)
fprintf(stderr, "%s\n", buf);
free(hostname);
@@ -439,7 +440,7 @@ static void syslog_local(struct logger_ctl *ctl, char *msg)
len = xasprintf(&buf, "<%d>%s %s%s: %s", ctl->pri, time, tag, pid, msg);
if (write_all(ctl->fd, buf, len) < 0)
warn(_("write failed"));
- if (ctl->logflags & LOG_PERROR)
+ if (ctl->stderr_printout)
fprintf(stderr, "%s\n", buf);
free(buf);
}
@@ -557,7 +558,6 @@ int main(int argc, char **argv)
{
struct logger_ctl ctl = {
.fd = -1,
- .logflags = 0,
.ppid = 0,
.pri = LOG_NOTICE,
.prio_prefix = 0,
@@ -611,7 +611,7 @@ int main(int argc, char **argv)
stdout_reopened = 1;
break;
case 'i': /* log process id also */
- ctl.logflags |= LOG_PID;
+ ctl.pid = 1;
if (optarg) {
const char *p = optarg;
@@ -629,7 +629,7 @@ int main(int argc, char **argv)
ctl.pri = pencode(optarg);
break;
case 's': /* log to standard error */
- ctl.logflags |= LOG_PERROR;
+ ctl.stderr_printout = 1;
break;
case 't': /* tag */
ctl.tag = optarg;
--
2.9.3
From 77c3bd5bf63093dfe54206be867c3d0c1fe46510 Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sat, 9 Aug 2014 08:38:58 +0100
Subject: [PATCH 051/115] logger: optimize string initializations
Setting whole array to be completely full of nulls cannot be as quick as
making the only significant member of the array when needed.
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 8ae00b2..884a119 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -299,11 +299,12 @@ static pid_t get_process_id(struct logger_ctl *ctl)
static void syslog_rfc3164(struct logger_ctl *ctl, char *msg)
{
- char *buf, pid[30] = { '\0' }, *cp, *tp, *hostname, *dot;
+ char *buf, pid[30], *cp, *tp, *hostname, *dot;
time_t now;
pid_t process;
int len;
+ *pid = '\0';
if (ctl->fd < 0)
return;
if ((process = get_process_id(ctl)))
@@ -334,13 +335,14 @@ static void syslog_rfc3164(struct logger_ctl *ctl, char *msg)
static void syslog_rfc5424(struct logger_ctl *ctl, char *msg)
{
char *buf, *tag = NULL, *hostname = NULL;
- char pid[32] = { '\0' }, time[64] = { '\0' }, timeq[80] = { '\0' };
+ char pid[32], time[64], timeq[80];
struct ntptimeval ntptv;
struct timeval tv;
struct tm *tm;
pid_t process;
int len;
+ *pid = *time = *timeq = '\0';
if (ctl->fd < 0)
return;
--
2.9.3
From 46ee14df664c8eff323320e5373325d5ebf7134b Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sat, 9 Aug 2014 08:57:51 +0100
Subject: [PATCH 052/115] logger: set function arguments read-only when
possible
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 884a119..c10fe3c 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -88,7 +88,7 @@ struct logger_ctl {
char *server;
char *port;
int socket_type;
- void (*syslogfp)(struct logger_ctl *ctl, char *msg);
+ void (*syslogfp)(const struct logger_ctl *ctl, const char *msg);
unsigned int
prio_prefix:1, /* read priority from intput */
pid:1, /* print PID, or PPID if it is enabled as well*/
@@ -118,7 +118,7 @@ static char *get_prio_prefix(char *msg, int *prio)
return end + 1;
}
-static int decode(char *name, CODE *codetab)
+static int decode(const char *name, CODE *codetab)
{
register CODE *c;
@@ -278,7 +278,7 @@ static int journald_entry(FILE *fp)
}
#endif
-static char *xgetlogin()
+static char *xgetlogin(void)
{
char *cp;
struct passwd *pw;
@@ -288,7 +288,7 @@ static char *xgetlogin()
return cp;
}
-static pid_t get_process_id(struct logger_ctl *ctl)
+static pid_t get_process_id(const struct logger_ctl *ctl)
{
pid_t id = 0;
@@ -297,7 +297,7 @@ static pid_t get_process_id(struct logger_ctl *ctl)
return id;
}
-static void syslog_rfc3164(struct logger_ctl *ctl, char *msg)
+static void syslog_rfc3164(const struct logger_ctl *ctl, const char *msg)
{
char *buf, pid[30], *cp, *tp, *hostname, *dot;
time_t now;
@@ -332,7 +332,7 @@ static void syslog_rfc3164(struct logger_ctl *ctl, char *msg)
free(buf);
}
-static void syslog_rfc5424(struct logger_ctl *ctl, char *msg)
+static void syslog_rfc5424(const struct logger_ctl *ctl, const char *msg)
{
char *buf, *tag = NULL, *hostname = NULL;
char pid[32], time[64], timeq[80];
@@ -419,7 +419,7 @@ static void parse_rfc5424_flags(struct logger_ctl *ctl, char *optarg)
}
}
-static void syslog_local(struct logger_ctl *ctl, char *msg)
+static void syslog_local(const struct logger_ctl *ctl, const char *msg)
{
char *buf, *tag;
char time[32], pid[32];
@@ -465,7 +465,7 @@ static void logger_open(struct logger_ctl *ctl)
ctl->syslogfp = syslog_local;
}
-static void logger_command_line(struct logger_ctl *ctl, char **argv)
+static void logger_command_line(const struct logger_ctl *ctl, char **argv)
{
char buf[4096];
char *p = buf;
@@ -512,7 +512,7 @@ static void logger_stdin(struct logger_ctl *ctl)
}
}
-static void logger_close(struct logger_ctl *ctl)
+static void logger_close(const struct logger_ctl *ctl)
{
if (close(ctl->fd) != 0)
err(EXIT_FAILURE, _("close failed"));
--
2.9.3
From 59c6ac0b92c6f21f9304f7d9b8a9c2bc8430e41f Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Wed, 20 Aug 2014 12:24:40 +0200
Subject: [PATCH 053/115] logger: use generic --id=<id>
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 42 ++++++++++++------------------------------
1 file changed, 12 insertions(+), 30 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index c10fe3c..99a5569 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -83,6 +83,7 @@ enum {
struct logger_ctl {
int fd;
int pri;
+ pid_t pid; /* zero when unwanted */
char *tag;
char *unix_socket;
char *server;
@@ -91,8 +92,6 @@ struct logger_ctl {
void (*syslogfp)(const struct logger_ctl *ctl, const char *msg);
unsigned int
prio_prefix:1, /* read priority from intput */
- pid:1, /* print PID, or PPID if it is enabled as well*/
- ppid:1, /* include PPID instead of PID */
stderr_printout:1, /* output message to stderr */
rfc5424_time:1, /* include time stamp */
rfc5424_tq:1, /* include time quality markup */
@@ -288,27 +287,17 @@ static char *xgetlogin(void)
return cp;
}
-static pid_t get_process_id(const struct logger_ctl *ctl)
-{
- pid_t id = 0;
-
- if (ctl->pid)
- id = ctl->ppid ? getppid() : getpid();
- return id;
-}
-
static void syslog_rfc3164(const struct logger_ctl *ctl, const char *msg)
{
char *buf, pid[30], *cp, *tp, *hostname, *dot;
time_t now;
- pid_t process;
int len;
*pid = '\0';
if (ctl->fd < 0)
return;
- if ((process = get_process_id(ctl)))
- snprintf(pid, sizeof(pid), "[%d]", process);
+ if (ctl->pid)
+ snprintf(pid, sizeof(pid), "[%d]", ctl->pid);
cp = ctl->tag ? ctl->tag : xgetlogin();
@@ -339,7 +328,6 @@ static void syslog_rfc5424(const struct logger_ctl *ctl, const char *msg)
struct ntptimeval ntptv;
struct timeval tv;
struct tm *tm;
- pid_t process;
int len;
*pid = *time = *timeq = '\0';
@@ -372,8 +360,8 @@ static void syslog_rfc5424(const struct logger_ctl *ctl, const char *msg)
if (48 < strlen(tag))
errx(EXIT_FAILURE, _("tag '%s' is too long"), tag);
- if ((process = get_process_id(ctl)))
- snprintf(pid, sizeof(pid), " %d", process);
+ if (ctl->pid)
+ snprintf(pid, sizeof(pid), " %d", ctl->pid);
if (ctl->rfc5424_tq) {
if (ntp_gettime(&ntptv) == TIME_OK)
@@ -425,7 +413,6 @@ static void syslog_local(const struct logger_ctl *ctl, const char *msg)
char time[32], pid[32];
struct timeval tv;
struct tm *tm;
- pid_t process;
int len;
gettimeofday(&tv, NULL);
@@ -434,8 +421,8 @@ static void syslog_local(const struct logger_ctl *ctl, const char *msg)
tag = ctl->tag ? ctl->tag : program_invocation_short_name;
- if ((process = get_process_id(ctl)))
- snprintf(pid, sizeof(pid), "[%d]", process);
+ if (ctl->pid)
+ snprintf(pid, sizeof(pid), "[%d]", ctl->pid);
else
pid[0] = '\0';
@@ -524,7 +511,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
fprintf(out, _(" %s [options] [<message>]\n"), program_invocation_short_name);
fputs(USAGE_OPTIONS, out);
- fputs(_(" -i, --id[=pid|ppid] log PID or PPID (default is PID)\n"), out);
+ fputs(_(" -i, --id[=<id>] log <id> (default is PID)\n"), out);
fputs(_(" -f, --file <file> log the contents of this file\n"), out);
fputs(_(" -p, --priority <prio> mark given message with this priority\n"), out);
fputs(_(" --prio-prefix look for a prefix on every line read from stdin\n"), out);
@@ -560,7 +547,7 @@ int main(int argc, char **argv)
{
struct logger_ctl ctl = {
.fd = -1,
- .ppid = 0,
+ .pid = 0,
.pri = LOG_NOTICE,
.prio_prefix = 0,
.tag = NULL,
@@ -613,19 +600,14 @@ int main(int argc, char **argv)
stdout_reopened = 1;
break;
case 'i': /* log process id also */
- ctl.pid = 1;
if (optarg) {
const char *p = optarg;
if (*p == '=')
p++;
- if (!strcmp(p, "ppid"))
- ctl.ppid = 1;
- else if (!strcmp(p, "pid"))
- ctl.ppid = 0;
- else
- warnx(_("ignoring unknown option argument: %s"), optarg);
- }
+ ctl.pid = strtoul_or_err(optarg, _("failed to parse id"));
+ } else
+ ctl.pid = getpid();
break;
case 'p': /* priority */
ctl.pri = pencode(optarg);
--
2.9.3
From 5ec85227ad3a8e9a8a0ddb1de8bdda30d2abea2f Mon Sep 17 00:00:00 2001
From: Andreas Henriksson <andreas@fatal.se>
Date: Fri, 19 Sep 2014 01:36:37 +0200
Subject: [PATCH 054/115] logger: gettimeofday needs <sys/time.h>
Either works on linux, but kfreebsd build fails
if we don't use the <sys/time.h> include.
According to man gettimeofday the correct include is <sys/time.h>
Signed-off-by: Andreas Henriksson <andreas@fatal.se>
---
misc-utils/logger.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 99a5569..2994dc0 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -40,7 +40,7 @@
#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
-#include <time.h>
+#include <sys/time.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
--
2.9.3
From 451dbcfae1406f526208faf6033d6dcc8ac100da Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@justemail.net>
Date: Mon, 22 Dec 2014 22:57:17 +0100
Subject: [PATCH 055/115] textual: add a docstring to most of the utilities
This adds a concise description of a tool to its usage text.
A first form of this patch was proposed by Steven Honeyman
(see http://www.spinics.net/lists/util-linux-ng/msg09994.html).
Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
---
misc-utils/logger.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 2994dc0..35caf9d 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -510,6 +510,9 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
fputs(USAGE_HEADER, out);
fprintf(out, _(" %s [options] [<message>]\n"), program_invocation_short_name);
+ fputs(USAGE_SEPARATOR, out);
+ fputs(_("Enter messages into the system log.\n"), out);
+
fputs(USAGE_OPTIONS, out);
fputs(_(" -i, --id[=<id>] log <id> (default is PID)\n"), out);
fputs(_(" -f, --file <file> log the contents of this file\n"), out);
--
2.9.3
From 87ee2658353836478af81ca2d8ae5133f4669e31 Mon Sep 17 00:00:00 2001
From: Samuel Thibault <sthibault@debian.org>
Date: Sun, 18 Jan 2015 21:44:54 +0000
Subject: [PATCH 056/115] logger: check availability of ntp_gettime()
Make compilation to work in systems which don't have sys/timex.h and its
ntp_gettime().
Reviewed-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Samuel Thibault <sthibault@debian.org>
---
misc-utils/logger.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 35caf9d..03031da 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -44,7 +44,6 @@
#include <stdio.h>
#include <ctype.h>
#include <string.h>
-#include <sys/timex.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
@@ -67,6 +66,10 @@
# include <systemd/sd-journal.h>
#endif
+#ifdef HAVE_SYS_TIMEX_H
+# include <sys/timex.h>
+#endif
+
enum {
TYPE_UDP = (1 << 1),
TYPE_TCP = (1 << 2),
@@ -325,7 +328,9 @@ static void syslog_rfc5424(const struct logger_ctl *ctl, const char *msg)
{
char *buf, *tag = NULL, *hostname = NULL;
char pid[32], time[64], timeq[80];
+#ifdef HAVE_SYS_TIMEX_H
struct ntptimeval ntptv;
+#endif
struct timeval tv;
struct tm *tm;
int len;
@@ -364,11 +369,13 @@ static void syslog_rfc5424(const struct logger_ctl *ctl, const char *msg)
snprintf(pid, sizeof(pid), " %d", ctl->pid);
if (ctl->rfc5424_tq) {
+#ifdef HAVE_SYS_TIMEX_H
if (ntp_gettime(&ntptv) == TIME_OK)
snprintf(timeq, sizeof(timeq),
" [timeQuality tzKnown=\"1\" isSynced=\"1\" syncAccuracy=\"%ld\"]",
ntptv.maxerror);
else
+#endif
snprintf(timeq, sizeof(timeq),
" [timeQuality tzKnown=\"1\" isSynced=\"0\"]");
}
--
2.9.3
From 54fefa078e54718acbd2489b0f92328f272f9a3c Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@justemail.net>
Date: Sun, 18 Jan 2015 21:17:09 +0100
Subject: [PATCH 057/115] textual: fix spellos and inconsistencies in several
program messages
Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
---
misc-utils/logger.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 35caf9d..a9d4b74 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -678,7 +678,7 @@ int main(int argc, char **argv)
if (stdin != jfd)
fclose(jfd);
if (ret)
- errx(EXIT_FAILURE, _("journald entry could not be wrote"));
+ errx(EXIT_FAILURE, _("journald entry could not be written"));
return EXIT_SUCCESS;
}
#endif
--
2.9.3
From d45867162c0e52d2fd8dfe93ff6776209baf23c8 Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@justemail.net>
Date: Sun, 18 Jan 2015 21:40:31 +0100
Subject: [PATCH 058/115] textual: put option and its description into a single
translatable string
Also improve the description: notime, notq, and nohost are literals,
not things to be replaced by something else -- so no angular brackets.
Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
---
misc-utils/logger.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index a9d4b74..57effb9 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -525,8 +525,8 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
fputs(_(" -T, --tcp use TCP only\n"), out);
fputs(_(" -d, --udp use UDP only\n"), out);
fputs(_(" --rfc3164 use the obsolete BSD syslog protocol\n"), out);
- fputs(_(" --rfc5424[=<notime,notq,nohost>]\n"), out);
- fputs(_(" use the syslog protocol (default)\n"), out);
+ fputs(_(" --rfc5424[=<cut>] use the syslog protocol (the default);\n"
+ " <cut> can be notime, or notq, and/or nohost\n"), out);
fputs(_(" -u, --socket <socket> write to this Unix socket\n"), out);
#ifdef HAVE_LIBSYSTEMD
fputs(_(" --journald[=<file>] write journald entry\n"), out);
--
2.9.3
From d0e875ffd4197ade0c907c89466b58fbc3f745e5 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Mon, 26 Jan 2015 11:26:40 +0100
Subject: [PATCH 059/115] logger: improve usage()
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 28 ++++++++++++++--------------
1 file changed, 14 insertions(+), 14 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 57effb9..5fbf148 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -514,20 +514,20 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
fputs(_("Enter messages into the system log.\n"), out);
fputs(USAGE_OPTIONS, out);
- fputs(_(" -i, --id[=<id>] log <id> (default is PID)\n"), out);
- fputs(_(" -f, --file <file> log the contents of this file\n"), out);
- fputs(_(" -p, --priority <prio> mark given message with this priority\n"), out);
- fputs(_(" --prio-prefix look for a prefix on every line read from stdin\n"), out);
- fputs(_(" -s, --stderr output message to standard error as well\n"), out);
- fputs(_(" -t, --tag <tag> mark every line with this tag\n"), out);
- fputs(_(" -n, --server <name> write to this remote syslog server\n"), out);
- fputs(_(" -P, --port <number> use this UDP port\n"), out);
- fputs(_(" -T, --tcp use TCP only\n"), out);
- fputs(_(" -d, --udp use UDP only\n"), out);
- fputs(_(" --rfc3164 use the obsolete BSD syslog protocol\n"), out);
- fputs(_(" --rfc5424[=<cut>] use the syslog protocol (the default);\n"
- " <cut> can be notime, or notq, and/or nohost\n"), out);
- fputs(_(" -u, --socket <socket> write to this Unix socket\n"), out);
+ fputs(_(" -i, --id[=<id>] log <id> (default is PID)\n"), out);
+ fputs(_(" -f, --file <file> log the contents of this file\n"), out);
+ fputs(_(" -p, --priority <prio> mark given message with this priority\n"), out);
+ fputs(_(" --prio-prefix look for a prefix on every line read from stdin\n"), out);
+ fputs(_(" -s, --stderr output message to standard error as well\n"), out);
+ fputs(_(" -t, --tag <tag> mark every line with this tag\n"), out);
+ fputs(_(" -n, --server <name> write to this remote syslog server\n"), out);
+ fputs(_(" -P, --port <number> use this UDP port\n"), out);
+ fputs(_(" -T, --tcp use TCP only\n"), out);
+ fputs(_(" -d, --udp use UDP only\n"), out);
+ fputs(_(" --rfc3164 use the obsolete BSD syslog protocol\n"), out);
+ fputs(_(" --rfc5424[=<snip>] use the syslog protocol (the default);\n"
+ " <snip> can be notime, or notq, and/or nohost\n"), out);
+ fputs(_(" -u, --socket <socket> write to this Unix socket\n"), out);
#ifdef HAVE_LIBSYSTEMD
fputs(_(" --journald[=<file>] write journald entry\n"), out);
#endif
--
2.9.3
From d77dc29e6e18d39b1845282e8039ac7117f3bd1c Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sun, 15 Feb 2015 09:50:23 +0000
Subject: [PATCH 060/115] logger: add --socket-errors compatibility option
Hello,
Depending viewpoint this change is either regression fix, or
re-regression in context of none-systemd init. I ack the change is sent
very late to be part of v2.26, but then again the excess noise was found
only because of -rc1 was tested in sysvinit environment. IMHO it would
contradict purpose of having rc's if faults will not lead to fixes.
I also want to point out the sysvinit scripts are broken, not the
logger(1), but getting them corrected is practically impossible.
Assuming sysvinit script are further developed by various teams and
distributions who maintain them they should use --socket-error=on in
future, and write scripts that pass without noise. Meanwhile trying to
be clever when to silence errors seems like a reasonable thing to do.
--->8----
From: Sami Kerola <kerolasa@iki.fi>
Date: Sat, 14 Feb 2015 19:05:55 +0000
Subject: [PATCH] logger: add --socket-errors compatibility option
Now when logger(1) has stopped using openlog() for Unix sockets, in
commit mentioned in reference, the lack of /dev/log detected will report
error accordingly. According to Gabriele Balducci this makes sysvinit
style boot scripts to print a lot of errors. So make the logger to
detect whether it should be in compatibility mode, and not report errors
if logging device is missing. That imitates behavior of glibc openlog().
To allow full control to users the /dev/log error messages can be forced
to on or off. The automatic error messaging is explained in manual page.
Reference: 1d57503378bdcd838365d625f6d2d0a09da9c29d
Reported-by: Gabriele Balducci <balducci@units.it>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 75 ++++++++++++++++++++++++++++++++++++++++++++---------
1 file changed, 63 insertions(+), 12 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 21b56bf..a3af7f1 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -63,6 +63,7 @@
#include <syslog.h>
#ifdef HAVE_LIBSYSTEMD
+# include <systemd/sd-daemon.h>
# include <systemd/sd-journal.h>
#endif
@@ -77,10 +78,17 @@ enum {
};
enum {
+ AF_UNIX_ERRORS_OFF = 0,
+ AF_UNIX_ERRORS_ON,
+ AF_UNIX_ERRORS_AUTO
+};
+
+enum {
OPT_PRIO_PREFIX = CHAR_MAX + 1,
OPT_JOURNALD,
OPT_RFC3164,
- OPT_RFC5424
+ OPT_RFC5424,
+ OPT_SOCKET_ERRORS
};
struct logger_ctl {
@@ -94,11 +102,12 @@ struct logger_ctl {
int socket_type;
void (*syslogfp)(const struct logger_ctl *ctl, const char *msg);
unsigned int
- prio_prefix:1, /* read priority from intput */
- stderr_printout:1, /* output message to stderr */
- rfc5424_time:1, /* include time stamp */
- rfc5424_tq:1, /* include time quality markup */
- rfc5424_host:1; /* include hostname */
+ unix_socket_errors:1, /* whether to report or not errors */
+ prio_prefix:1, /* read priority from intput */
+ stderr_printout:1, /* output message to stderr */
+ rfc5424_time:1, /* include time stamp */
+ rfc5424_tq:1, /* include time quality markup */
+ rfc5424_host:1; /* include hostname */
};
static char *get_prio_prefix(char *msg, int *prio)
@@ -165,7 +174,7 @@ static int pencode(char *s)
return ((level & LOG_PRIMASK) | (facility & LOG_FACMASK));
}
-static int unix_socket(const char *path, const int socket_type)
+static int unix_socket(struct logger_ctl *ctl, const char *path, const int socket_type)
{
int fd, i;
static struct sockaddr_un s_addr; /* AF_UNIX address of local logger */
@@ -192,9 +201,14 @@ static int unix_socket(const char *path, const int socket_type)
break;
}
- if (i == 0)
- err(EXIT_FAILURE, _("socket %s"), path);
-
+ if (i == 0) {
+ if (ctl->unix_socket_errors)
+ err(EXIT_FAILURE, _("socket %s"), path);
+ else
+ /* See --socket-errors manual page entry for
+ * explanation of this strange exit. */
+ exit(EXIT_SUCCESS);
+ }
return fd;
}
@@ -414,6 +428,18 @@ static void parse_rfc5424_flags(struct logger_ctl *ctl, char *optarg)
}
}
+static int parse_unix_socket_errors_flags(char *optarg)
+{
+ if (!strcmp(optarg, "off"))
+ return AF_UNIX_ERRORS_OFF;
+ if (!strcmp(optarg, "on"))
+ return AF_UNIX_ERRORS_ON;
+ if (!strcmp(optarg, "auto"))
+ return AF_UNIX_ERRORS_AUTO;
+ warnx(_("invalid argument: %s: using automatic errors"), optarg);
+ return AF_UNIX_ERRORS_AUTO;
+}
+
static void syslog_local(const struct logger_ctl *ctl, const char *msg)
{
char *buf, *tag;
@@ -450,12 +476,12 @@ static void logger_open(struct logger_ctl *ctl)
return;
}
if (ctl->unix_socket) {
- ctl->fd = unix_socket(ctl->unix_socket, ctl->socket_type);
+ ctl->fd = unix_socket(ctl, ctl->unix_socket, ctl->socket_type);
if (!ctl->syslogfp)
ctl->syslogfp = syslog_rfc5424;
return;
}
- ctl->fd = unix_socket("/dev/log", ctl->socket_type);
+ ctl->fd = unix_socket(ctl, "/dev/log", ctl->socket_type);
ctl->syslogfp = syslog_local;
}
@@ -535,6 +561,8 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
fputs(_(" --rfc5424[=<snip>] use the syslog protocol (the default);\n"
" <snip> can be notime, or notq, and/or nohost\n"), out);
fputs(_(" -u, --socket <socket> write to this Unix socket\n"), out);
+ fputs(_(" --socket-errors[=<on|off|auto>]\n"
+ " print connection errors when using Unix sockets\n"), out);
#ifdef HAVE_LIBSYSTEMD
fputs(_(" --journald[=<file>] write journald entry\n"), out);
#endif
@@ -562,6 +590,7 @@ int main(int argc, char **argv)
.prio_prefix = 0,
.tag = NULL,
.unix_socket = NULL,
+ .unix_socket_errors = 0,
.server = NULL,
.port = NULL,
.socket_type = ALL_TYPES,
@@ -571,6 +600,7 @@ int main(int argc, char **argv)
};
int ch;
int stdout_reopened = 0;
+ int unix_socket_errors_mode = AF_UNIX_ERRORS_AUTO;
#ifdef HAVE_LIBSYSTEMD
FILE *jfd = NULL;
#endif
@@ -581,6 +611,7 @@ int main(int argc, char **argv)
{ "priority", required_argument, 0, 'p' },
{ "tag", required_argument, 0, 't' },
{ "socket", required_argument, 0, 'u' },
+ { "socket-errors", required_argument, 0, OPT_SOCKET_ERRORS },
{ "udp", no_argument, 0, 'd' },
{ "tcp", no_argument, 0, 'T' },
{ "server", required_argument, 0, 'n' },
@@ -670,6 +701,9 @@ int main(int argc, char **argv)
jfd = stdin;
break;
#endif
+ case OPT_SOCKET_ERRORS:
+ unix_socket_errors_mode = parse_unix_socket_errors_flags(optarg);
+ break;
case '?':
default:
usage(stderr);
@@ -689,6 +723,23 @@ int main(int argc, char **argv)
return EXIT_SUCCESS;
}
#endif
+ switch (unix_socket_errors_mode) {
+ case AF_UNIX_ERRORS_OFF:
+ ctl.unix_socket_errors = 0;
+ break;
+ case AF_UNIX_ERRORS_ON:
+ ctl.unix_socket_errors = 1;
+ break;
+ case AF_UNIX_ERRORS_AUTO:
+#ifdef HAVE_LIBSYSTEMD
+ ctl.unix_socket_errors = sd_booted();
+#else
+ ctl.unix_socket_errors = 0;
+#endif
+ break;
+ default:
+ abort();
+ }
logger_open(&ctl);
if (0 < argc)
logger_command_line(&ctl, argv);
--
2.9.3
From 52a49e9add7b3618c65782b64c48226311dda01a Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sun, 22 Feb 2015 14:41:45 +0000
Subject: [PATCH 061/115] logger: move /dev/log to pathnames.h
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index a3af7f1..db6fd44 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -56,6 +56,7 @@
#include "c.h"
#include "closestream.h"
#include "nls.h"
+#include "pathnames.h"
#include "strutils.h"
#include "xalloc.h"
@@ -481,7 +482,7 @@ static void logger_open(struct logger_ctl *ctl)
ctl->syslogfp = syslog_rfc5424;
return;
}
- ctl->fd = unix_socket(ctl, "/dev/log", ctl->socket_type);
+ ctl->fd = unix_socket(ctl, _PATH_DEVLOG, ctl->socket_type);
ctl->syslogfp = syslog_local;
}
--
2.9.3
From 2f267611f0d8d1ea6e9a2f404521e9208390bb21 Mon Sep 17 00:00:00 2001
From: Rainer Gerhards <rgerhards@adiscon.com>
Date: Wed, 4 Mar 2015 18:50:30 +0100
Subject: [PATCH 062/115] logger: fix invalid timestamp in rfc5425 format
The timestamp is written as
2015-03-04T15:02:02.566782+0100
unfortunately, this is not an RFC3339 timestamp as demanded by rfc5424.
The colon in the time offset field is missing. The correct timestamp is
2015-03-04T15:02:02.566782+01:00
(Note "+0100" vs. "+01:00")
---
misc-utils/logger.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index db6fd44..7abfcf1 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -359,8 +359,12 @@ static void syslog_rfc5424(const struct logger_ctl *ctl, const char *msg)
if ((tm = localtime(&tv.tv_sec)) != NULL) {
char fmt[64];
- strftime(fmt, sizeof(fmt), " %Y-%m-%dT%H:%M:%S.%%06u%z",
- tm);
+ const size_t i = strftime(fmt, sizeof(fmt),
+ " %Y-%m-%dT%H:%M:%S.%%06u%z ", tm);
+ /* patch TZ info to comply with RFC3339 (we left SP at end) */
+ fmt[i-1] = fmt[i-2];
+ fmt[i-2] = fmt[i-3];
+ fmt[i-3] = ':';
snprintf(time, sizeof(time), fmt, tv.tv_usec);
} else
err(EXIT_FAILURE, _("localtime() failed"));
--
2.9.3
From 3f51c10b39920bd22a2760bd57cb5a5703b0ec2c Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Fri, 20 Feb 2015 19:42:34 +0000
Subject: [PATCH 063/115] logger: fix -i argument parsing regression
With earlier logger it's possible to combine the option -i with other
options, such as -s. But currently:
$:~> logger -is
logger: failed to parse id: 's'
The changed behaviour breaks existing scripts like dhcpcd-run-hooks from
dhcpcd.
Broken-since: aab5b44405b9a6ada92e419e5a84cc0d1d4afee9
Reference: http://comments.gmane.org/gmane.linux.utilities.util-linux-ng/9683
Reported-by: Juergen Daubert <jue@jue.li>
Reviewed-by: Benno Schulenberg <bensberg@justemail.net>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index db6fd44..0604e61 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -89,7 +89,8 @@ enum {
OPT_JOURNALD,
OPT_RFC3164,
OPT_RFC5424,
- OPT_SOCKET_ERRORS
+ OPT_SOCKET_ERRORS,
+ OPT_ID
};
struct logger_ctl {
@@ -548,7 +549,8 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
fputs(_("Enter messages into the system log.\n"), out);
fputs(USAGE_OPTIONS, out);
- fputs(_(" -i, --id[=<id>] log <id> (default is PID)\n"), out);
+ fputs(_(" -i log the logger command's PID\n"), out);
+ fputs(_(" --id[=<id>] log the given <id>, or otherwise the PID\n"), out);
fputs(_(" -f, --file <file> log the contents of this file\n"), out);
fputs(_(" -p, --priority <prio> mark given message with this priority\n"), out);
fputs(_(" --prio-prefix look for a prefix on every line read from stdin\n"), out);
@@ -606,7 +608,7 @@ int main(int argc, char **argv)
FILE *jfd = NULL;
#endif
static const struct option longopts[] = {
- { "id", optional_argument, 0, 'i' },
+ { "id", optional_argument, 0, OPT_ID },
{ "stderr", no_argument, 0, 's' },
{ "file", required_argument, 0, 'f' },
{ "priority", required_argument, 0, 'p' },
@@ -633,7 +635,7 @@ int main(int argc, char **argv)
textdomain(PACKAGE);
atexit(close_stdout);
- while ((ch = getopt_long(argc, argv, "f:i::p:st:u:dTn:P:Vh",
+ while ((ch = getopt_long(argc, argv, "f:ip:st:u:dTn:P:Vh",
longopts, NULL)) != -1) {
switch (ch) {
case 'f': /* file to log */
@@ -642,6 +644,9 @@ int main(int argc, char **argv)
stdout_reopened = 1;
break;
case 'i': /* log process id also */
+ ctl.pid = getpid();
+ break;
+ case OPT_ID:
if (optarg) {
const char *p = optarg;
--
2.9.3
From 7dc2080433c35ac6fa310442f43cd93bf6f949e4 Mon Sep 17 00:00:00 2001
From: Rainer Gerhards <rgerhards@adiscon.com>
Date: Wed, 4 Mar 2015 11:17:20 +0100
Subject: [PATCH 064/115] logger: fix inconsistent format regression when
logging locally
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
The message format when writing to local sockets is inconsistent. Example:
$ ./logger --stderr test
<5>Mär 4 11:03:30 logger: test
$ ./logger -u /dev/log --stderr test
<5>1 2015-03-04T11:03:31.699841+0100 ubuntu1404esp rger - [timeQuality tzKnown="1" isSynced="1" syncAccuracy="29000"] test
The regression was introduced with 4de2e8a03859aaab2c25dc98f33409cd28de6acc
As far as the commit comments and man page indicates, this was meant to affect
remote system logging only, but it also affects local logging when the -u
option is given.
This causes problems with receivers who do not expect full-blown RFC format
on the log socket, like rsyslog. In consequence, this can also affect
log analysis programs and invalidate some of their results.
The patch corrects the behaviour so that the same old-style format is used for
any type of local logging. New-style can always be selected by command line-options.
RFC5424 is still the default for remote logging, as intended in the orignal
commit.
Result with the patch:
$ ./logger --stderr test
<5>Mär 4 11:15:35 logger: test
$ ./logger -u /dev/log --stderr test
<5>Mär 4 11:15:40 logger: test
$ ./logger -u /dev/log --rfc5424 --stderr test
<5>1 2015-03-04T11:21:28.796170+0100 ubuntu1404esp rger - [timeQuality tzKnown="1" isSynced="1" syncAccuracy="27500"] test
---
misc-utils/logger.c | 14 ++++++--------
1 file changed, 6 insertions(+), 8 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 1e7e2dc..6b7ce1b 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -481,14 +481,12 @@ static void logger_open(struct logger_ctl *ctl)
ctl->syslogfp = syslog_rfc5424;
return;
}
- if (ctl->unix_socket) {
- ctl->fd = unix_socket(ctl, ctl->unix_socket, ctl->socket_type);
- if (!ctl->syslogfp)
- ctl->syslogfp = syslog_rfc5424;
- return;
- }
- ctl->fd = unix_socket(ctl, _PATH_DEVLOG, ctl->socket_type);
- ctl->syslogfp = syslog_local;
+ if (!ctl->unix_socket)
+ ctl->unix_socket = _PATH_DEVLOG;
+
+ ctl->fd = unix_socket(ctl, ctl->unix_socket, ctl->socket_type);
+ if (!ctl->syslogfp)
+ ctl->syslogfp = syslog_local;
}
static void logger_command_line(const struct logger_ctl *ctl, char **argv)
--
2.9.3
From 3070ca77ac3b3a99b5392f0d192b71ff872e7109 Mon Sep 17 00:00:00 2001
From: Rainer Gerhards <rgerhards@adiscon.com>
Date: Thu, 5 Mar 2015 15:20:50 +0100
Subject: [PATCH 065/115] logger: fix invalid timestamp regression in local
format
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Since 1d57503378bdcd838365d625f6d2d0a09da9c29d logger no longer uses
the syslog(3) call. The way the local timestamp is generated did not
match the syslog(3) format. Most importantly, the month name is
formatted based on the user's local. For example:
$ ./logger --stderr test with logger 2.26.39-eb651-dirty
<5>Mär 5 14:17:47 logger: test with logger 2.26.39-eb651-dirty
"Mär" like in German "März" for "March".
previously:
$ logger --stderr test with logger 2.25.2
rger: test with logger 2.25.2
In the system log file, this results to the following:
Mar 5 14:17:47 host Mär 5 14:17:47 logger: test with logger 2.26.39-eb651-dirty
Mar 5 14:18:01 host rger: test with logger 2.25.2
This local naming is invalid as of RFC3164. One may argue that
the local log socket traditionally does not have RFC3164 format,
but the timestamp always was as defined in RFC3164 (and along
the lines of the ctime() call). Anything else would also be impractical,
as a syslog parser would otherwise need to know about all
potential locale-specific representations of month names.
This patch corrects the problem and also refactors the timestamp
handling a bit. The same timestamp is needed in local and rfc3164
processing, so there now is a new function to create that stamp.
---
misc-utils/logger.c | 42 +++++++++++++++++++++++++++++-------------
1 file changed, 29 insertions(+), 13 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 1e7e2dc..c6258e6 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -306,9 +306,33 @@ static char *xgetlogin(void)
return cp;
}
+
+/* this creates a timestamp based on current time according to the
+ * fine rules of RFC3164, most importantly it ensures in a portable
+ * way that the month day is correctly written (with a SP instead
+ * of a leading 0). The function uses a static buffer which is
+ * overwritten on the next call (just like ctime() does).
+ */
+static const char *
+rfc3164_current_time(void)
+{
+ static char time[32];
+ struct timeval tv;
+ struct tm *tm;
+ static char *monthnames[] = { "Jan", "Feb", "Mar", "Apr",
+ "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
+
+ gettimeofday(&tv, NULL);
+ tm = localtime(&tv.tv_sec);
+ snprintf(time, sizeof(time),"%s %2d %2.2d:%2.2d:%2.2d",
+ monthnames[tm->tm_mon], tm->tm_mday,
+ tm->tm_hour, tm->tm_min, tm->tm_sec);
+ return time;
+}
+
static void syslog_rfc3164(const struct logger_ctl *ctl, const char *msg)
{
- char *buf, pid[30], *cp, *tp, *hostname, *dot;
+ char *buf, pid[30], *cp, *hostname, *dot;
time_t now;
int len;
@@ -325,11 +349,8 @@ static void syslog_rfc3164(const struct logger_ctl *ctl, const char *msg)
if (dot)
*dot = '\0';
- time(&now);
- tp = ctime(&now) + 4;
-
len = xasprintf(&buf, "<%d>%.15s %s %.200s%s: %.400s",
- ctl->pri, tp, hostname, cp, pid, msg);
+ ctl->pri, rfc3164_current_time(), hostname, cp, pid, msg);
if (write_all(ctl->fd, buf, len) < 0)
warn(_("write failed"));
@@ -449,15 +470,9 @@ static int parse_unix_socket_errors_flags(char *optarg)
static void syslog_local(const struct logger_ctl *ctl, const char *msg)
{
char *buf, *tag;
- char time[32], pid[32];
- struct timeval tv;
- struct tm *tm;
+ char pid[32];
int len;
- gettimeofday(&tv, NULL);
- tm = localtime(&tv.tv_sec);
- strftime(time, sizeof(time), "%h %e %T", tm);
-
tag = ctl->tag ? ctl->tag : program_invocation_short_name;
if (ctl->pid)
@@ -465,7 +480,8 @@ static void syslog_local(const struct logger_ctl *ctl, const char *msg)
else
pid[0] = '\0';
- len = xasprintf(&buf, "<%d>%s %s%s: %s", ctl->pri, time, tag, pid, 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)
--
2.9.3
From d0b6c4bf8d327cb9ce0dcaaae3fac1bf7c1d32a7 Mon Sep 17 00:00:00 2001
From: Rainer Gerhards <rgerhards@adiscon.com>
Date: Thu, 5 Mar 2015 16:44:03 +0100
Subject: [PATCH 066/115] logger: messages are logged with kern.* priority by
default
The default should be user.notice and kern.* should never be used
(syslog(3) forbids this).
This is a severe regression, as messages are now logged to the wrong
bin or not at all. So they get lost and may confuse readers of the
kernel bin.
regression from 2.25.2 to 2.26
---
misc-utils/logger.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 1e7e2dc..5ec722e 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -593,7 +593,7 @@ int main(int argc, char **argv)
struct logger_ctl ctl = {
.fd = -1,
.pid = 0,
- .pri = LOG_NOTICE,
+ .pri = LOG_USER | LOG_NOTICE,
.prio_prefix = 0,
.tag = NULL,
.unix_socket = NULL,
--
2.9.3
From d4c814b18a489dc2aa41e768e6e3cad2f16a8af3 Mon Sep 17 00:00:00 2001
From: Rainer Gerhards <rgerhards@adiscon.com>
Date: Thu, 5 Mar 2015 16:54:34 +0100
Subject: [PATCH 067/115] logger: fix -p kern.* priority is accepted regression
Pre 2.26, syslog(3) was used for local logging, and it did not
accept kern.* priorities. This is re-enabled by the patch.
---
misc-utils/logger.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 5ec722e..06f0759 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -173,6 +173,8 @@ static int pencode(char *s)
level = decode(s, prioritynames);
if (level < 0)
errx(EXIT_FAILURE, _("unknown priority name: %s"), s);
+ if(facility == LOG_KERN)
+ facility = LOG_USER; /* kern is forbidden */
return ((level & LOG_PRIMASK) | (facility & LOG_FACMASK));
}
--
2.9.3
From c95d3209093d284ca267e04fb684002f59401924 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Fri, 6 Mar 2015 11:05:30 +0100
Subject: [PATCH 068/115] logger: add comment
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 6b7ce1b..fab2112 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -98,7 +98,7 @@ struct logger_ctl {
int pri;
pid_t pid; /* zero when unwanted */
char *tag;
- char *unix_socket;
+ char *unix_socket; /* -u <path> or default to _PATH_DEVLOG */
char *server;
char *port;
int socket_type;
--
2.9.3
From 0f1c825b75f2a8e8c1a1979f59c90f20d3631a54 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Fri, 6 Mar 2015 11:27:16 +0100
Subject: [PATCH 069/115] logger: fix whitespace and compiler warning
[-Wunused-variable]
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
misc-utils/logger.c: In function ‘syslog_rfc3164’:
misc-utils/logger.c:336:9: warning: unused variable ‘now’ [-Wunused-variable]
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 0d33b5b..ca50c82 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -326,14 +326,13 @@ rfc3164_current_time(void)
tm = localtime(&tv.tv_sec);
snprintf(time, sizeof(time),"%s %2d %2.2d:%2.2d:%2.2d",
monthnames[tm->tm_mon], tm->tm_mday,
- tm->tm_hour, tm->tm_min, tm->tm_sec);
+ tm->tm_hour, tm->tm_min, tm->tm_sec);
return time;
}
static void syslog_rfc3164(const struct logger_ctl *ctl, const char *msg)
{
char *buf, pid[30], *cp, *hostname, *dot;
- time_t now;
int len;
*pid = '\0';
--
2.9.3
From 4be843064c700fb28cab869893c3d19392c4938d Mon Sep 17 00:00:00 2001
From: Rainer Gerhards <rgerhards@adiscon.com>
Date: Fri, 6 Mar 2015 11:51:31 +0100
Subject: [PATCH 070/115] logger: refactor the way output is written
Previously, output was written in exactly the same way in three
different places. This is now combined into a single function. This
hopefully makes it easier to adapt to changing output needs.
---
misc-utils/logger.c | 25 ++++++++++++-------------
1 file changed, 12 insertions(+), 13 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 8158130..6869f44 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -332,6 +332,15 @@ rfc3164_current_time(void)
return time;
}
+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"));
+ 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 +362,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 +433,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 +485,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);
}
--
2.9.3
From 940a14a3515a0d8cddb338605e788315565fd6cc Mon Sep 17 00:00:00 2001
From: Rainer Gerhards <rgerhards@adiscon.com>
Date: Fri, 6 Mar 2015 12:12:15 +0100
Subject: [PATCH 071/115] logger: bugfix: tcp syslog framing is broken, -T
unusable
Logger can send via plain tcp syslog if -n -T options are given.
However, the framing is broken so that a syslog receiver can not
know where the first message ends and the next one starts. It
actually looks like no framing at all is used. Plain TCP syslog
framing is described in RFC6587.
This patch adds RFC6587 octet-stuffed framing to TCP syslog. For
local logging, this is always fine, for remote logging this is
NOT recommended by the IETF (the RFC is historic). However, a
full blown RFC5425 TLS sender seems to be out of scope for a tool
like logger IMO.
This patch also refactors the way output is written, seperating
the message format generators from the output writer.
---
misc-utils/logger.c | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 6869f44..e19ef05 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -332,11 +332,26 @@ 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);
}
--
2.9.3
From 17d66ccc7516a5e2cf681abc73406559da41eaae Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Fri, 6 Mar 2015 12:15:23 +0100
Subject: [PATCH 072/115] logger: use username as the default tag
Reported-by: Rainer Gerhards <rgerhards@adiscon.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 8158130..308db8b 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -474,7 +474,7 @@ static void syslog_local(const struct logger_ctl *ctl, const char *msg)
char pid[32];
int len;
- tag = ctl->tag ? ctl->tag : program_invocation_short_name;
+ tag = ctl->tag ? ctl->tag : xgetlogin();
if (ctl->pid)
snprintf(pid, sizeof(pid), "[%d]", ctl->pid);
--
2.9.3
From f68b8aa7f5dc1fdf403a2ef64b5dd86f3fdbee95 Mon Sep 17 00:00:00 2001
From: Rainer Gerhards <rgerhards@adiscon.com>
Date: Fri, 6 Mar 2015 15:50:34 +0100
Subject: [PATCH 073/115] logger: permit to send messages larger than 1024
characters
This is an important capability that has been specified in RFC5424.
However, messages larger than 1024 chars are being accepted for years
now by at least rsyslog and syslog-ng.
This patch adds the option --size to permit setting a new max
size, with 1024 being the default.
Note that the size limit is only approximative, as we do not take the
header size in account (RFC talks about total message length).
[[kzak@redhat.com: - add 'S' to getopt_long(),
- rename --message-size to --size
- add the option to bash-completion]
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 23 ++++++++++++++++-------
1 file changed, 16 insertions(+), 7 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 24cbb3b..3740c2e 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -102,6 +102,7 @@ struct logger_ctl {
char *server;
char *port;
int socket_type;
+ size_t max_message_size;
void (*syslogfp)(const struct logger_ctl *ctl, const char *msg);
unsigned int
unix_socket_errors:1, /* whether to report or not errors */
@@ -374,7 +375,7 @@ static void syslog_rfc3164(const struct logger_ctl *ctl, const char *msg)
if (dot)
*dot = '\0';
- len = xasprintf(&buf, "<%d>%.15s %s %.200s%s: %.400s",
+ len = xasprintf(&buf, "<%d>%.15s %s %.200s%s: %s",
ctl->pri, rfc3164_current_time(), hostname, cp, pid, msg);
write_output(ctl, buf, len);
@@ -522,9 +523,9 @@ static void logger_open(struct logger_ctl *ctl)
static void logger_command_line(const struct logger_ctl *ctl, char **argv)
{
- char buf[4096];
+ char *const buf = xmalloc(ctl->max_message_size + 1);
char *p = buf;
- const char *endp = buf + sizeof(buf) - 2;
+ const char *endp = buf + ctl->max_message_size - 1;
size_t len;
while (*argv) {
@@ -533,7 +534,8 @@ static void logger_command_line(const struct logger_ctl *ctl, char **argv)
ctl->syslogfp(ctl, buf);
p = buf;
}
- if (sizeof(buf) - 1 < len) {
+ if (ctl->max_message_size < len) {
+ (*argv)[ctl->max_message_size] = '\0'; /* truncate */
ctl->syslogfp(ctl, *argv++);
continue;
}
@@ -550,9 +552,9 @@ static void logger_stdin(struct logger_ctl *ctl)
{
char *msg;
int default_priority = ctl->pri;
- char buf[1024];
+ char *const buf = xmalloc(ctl->max_message_size + 2);
- while (fgets(buf, sizeof(buf), stdin) != NULL) {
+ while (fgets(buf, ctl->max_message_size+2, stdin) != NULL) {
int len = strlen(buf);
/* some glibc versions are buggy, they add an additional
@@ -588,6 +590,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
fputs(_(" -p, --priority <prio> mark given message with this priority\n"), out);
fputs(_(" --prio-prefix look for a prefix on every line read from stdin\n"), out);
fputs(_(" -s, --stderr output message to standard error as well\n"), out);
+ fputs(_(" -S, --size <size> maximum size for a single message\n"), out);
fputs(_(" -t, --tag <tag> mark every line with this tag\n"), out);
fputs(_(" -n, --server <name> write to this remote syslog server\n"), out);
fputs(_(" -P, --port <number> use this UDP port\n"), out);
@@ -630,6 +633,7 @@ int main(int argc, char **argv)
.server = NULL,
.port = NULL,
.socket_type = ALL_TYPES,
+ .max_message_size = 1024,
.rfc5424_time = 1,
.rfc5424_tq = 1,
.rfc5424_host = 1,
@@ -657,6 +661,7 @@ int main(int argc, char **argv)
{ "prio-prefix", no_argument, 0, OPT_PRIO_PREFIX },
{ "rfc3164", no_argument, 0, OPT_RFC3164 },
{ "rfc5424", optional_argument, 0, OPT_RFC5424 },
+ { "size", required_argument, 0, 'S' },
#ifdef HAVE_LIBSYSTEMD
{ "journald", optional_argument, 0, OPT_JOURNALD },
#endif
@@ -668,7 +673,7 @@ int main(int argc, char **argv)
textdomain(PACKAGE);
atexit(close_stdout);
- while ((ch = getopt_long(argc, argv, "f:ip:st:u:dTn:P:Vh",
+ while ((ch = getopt_long(argc, argv, "f:ip:S:st:u:dTn:P:Vh",
longopts, NULL)) != -1) {
switch (ch) {
case 'f': /* file to log */
@@ -701,6 +706,10 @@ int main(int argc, char **argv)
case 'u': /* unix socket */
ctl.unix_socket = optarg;
break;
+ case 'S': /* max message size */
+ ctl.max_message_size = strtosize_or_err(optarg,
+ _("failed to parse message size"));
+ break;
case 'd':
ctl.socket_type = TYPE_UDP;
break;
--
2.9.3
From 2b3f40c5978569f15e30836cf99f34f31c163d1c Mon Sep 17 00:00:00 2001
From: Rainer Gerhards <rgerhards@adiscon.com>
Date: Fri, 6 Mar 2015 18:52:26 +0100
Subject: [PATCH 074/115] logger: refactor message generation
Previously, the message format was generated in one big step. Now
this is refactored to generate the header independently. This not
only provides a better isolation of functionality, but enables
to calculate the size of the header *before* generating the user
part of the message. That in turn is needed in order to precisely
enforce the message size limit. This is especially important while
processing files, as here parts of the message may be lost if the
receiver truncates the message. The file reader itself tries to
guard against this by reading only the permitted number of bytes,
but without knowing the header size, it would mis-guess here.
Note that when --prio-prefix is given, we still do not know exactly
the header length, because the PRI value is between 1 and 3 bytes.
Unfortunately, we do not know the actual size before reading. With
the current (simple) approach, we need to read the full line before
getting the PRI, so this is a hen-egg problem. To solve this, a
more complex reader would be required. It is questionable if this
is necessary for a tool like logger. So currently, we still have a
2-byte window of uncertainty if --prio-prefix is given.
[kzak@redhat.com: - fix compiler warnings [-Wunused-but-set-variable]]
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 91 +++++++++++++++++++++++++++--------------------------
1 file changed, 47 insertions(+), 44 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 3740c2e..3a4d58e 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -97,13 +97,14 @@ struct logger_ctl {
int fd;
int pri;
pid_t pid; /* zero when unwanted */
+ char *hdr; /* the syslog header (based on protocol) */
char *tag;
char *unix_socket; /* -u <path> or default to _PATH_DEVLOG */
char *server;
char *port;
int socket_type;
size_t max_message_size;
- void (*syslogfp)(const struct logger_ctl *ctl, const char *msg);
+ void (*syslogfp)(struct logger_ctl *ctl);
unsigned int
unix_socket_errors:1, /* whether to report or not errors */
prio_prefix:1, /* read priority from intput */
@@ -338,9 +339,10 @@ rfc3164_current_time(void)
* 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)
+static void write_output(const struct logger_ctl *ctl, const char *const msg)
{
+ char *buf;
+ const size_t len = xasprintf(&buf, "%s%s", ctl->hdr, msg);
if (write_all(ctl->fd, buf, len) < 0)
warn(_("write failed"));
else
@@ -357,10 +359,9 @@ static void write_output(const struct logger_ctl *ctl, const char *const buf,
fprintf(stderr, "%s\n", buf);
}
-static void syslog_rfc3164(const struct logger_ctl *ctl, const char *msg)
+static void syslog_rfc3164_header(struct logger_ctl *const ctl)
{
- char *buf, pid[30], *cp, *hostname, *dot;
- int len;
+ char pid[30], *hostname, *dot;
*pid = '\0';
if (ctl->fd < 0)
@@ -368,32 +369,26 @@ static void syslog_rfc3164(const struct logger_ctl *ctl, const char *msg)
if (ctl->pid)
snprintf(pid, sizeof(pid), "[%d]", ctl->pid);
- cp = ctl->tag ? ctl->tag : xgetlogin();
-
hostname = xgethostname();
dot = strchr(hostname, '.');
if (dot)
*dot = '\0';
- len = xasprintf(&buf, "<%d>%.15s %s %.200s%s: %s",
- ctl->pri, rfc3164_current_time(), hostname, cp, pid, msg);
-
- write_output(ctl, buf, len);
+ xasprintf(&ctl->hdr, "<%d>%.15s %s %.200s%s: ",
+ ctl->pri, rfc3164_current_time(), hostname, ctl->tag, pid);
free(hostname);
- free(buf);
}
-static void syslog_rfc5424(const struct logger_ctl *ctl, const char *msg)
+static void syslog_rfc5424_header(struct logger_ctl *const ctl)
{
- char *buf, *tag = NULL, *hostname = NULL;
+ char *hostname = NULL;
char pid[32], time[64], timeq[80];
#ifdef HAVE_SYS_TIMEX_H
struct ntptimeval ntptv;
#endif
struct timeval tv;
struct tm *tm;
- int len;
*pid = *time = *timeq = '\0';
if (ctl->fd < 0)
@@ -424,10 +419,8 @@ static void syslog_rfc5424(const struct logger_ctl *ctl, const char *msg)
hostname);
}
- tag = ctl->tag ? ctl->tag : xgetlogin();
-
- if (48 < strlen(tag))
- errx(EXIT_FAILURE, _("tag '%s' is too long"), tag);
+ if (48 < strlen(ctl->tag))
+ errx(EXIT_FAILURE, _("tag '%s' is too long"), ctl->tag);
if (ctl->pid)
snprintf(pid, sizeof(pid), " %d", ctl->pid);
@@ -444,15 +437,12 @@ static void syslog_rfc5424(const struct logger_ctl *ctl, const char *msg)
" [timeQuality tzKnown=\"1\" isSynced=\"0\"]");
}
- len = xasprintf(&buf, "<%d>1%s%s%s %s -%s%s %s", ctl->pri, time,
+ xasprintf(&ctl->hdr, "<%d>1%s%s%s %s -%s%s", ctl->pri, time,
hostname ? " " : "",
hostname ? hostname : "",
- tag, pid, timeq, msg);
-
- write_output(ctl, buf, len);
+ ctl->tag, pid, timeq);
free(hostname);
- free(buf);
}
static void parse_rfc5424_flags(struct logger_ctl *ctl, char *optarg)
@@ -486,23 +476,23 @@ static int parse_unix_socket_errors_flags(char *optarg)
return AF_UNIX_ERRORS_AUTO;
}
-static void syslog_local(const struct logger_ctl *ctl, const char *msg)
+static void syslog_local_header(struct logger_ctl *const ctl)
{
- char *buf, *tag;
char pid[32];
- int len;
-
- tag = ctl->tag ? ctl->tag : xgetlogin();
if (ctl->pid)
snprintf(pid, sizeof(pid), "[%d]", ctl->pid);
else
pid[0] = '\0';
- len = xasprintf(&buf, "<%d>%s %s%s: %s", ctl->pri, rfc3164_current_time(),
- tag, pid, msg);
- write_output(ctl, buf, len);
- free(buf);
+ xasprintf(&ctl->hdr, "<%d>%s %s%s: ", ctl->pri, rfc3164_current_time(),
+ ctl->tag, pid);
+}
+
+static void generate_syslog_header(struct logger_ctl *const ctl)
+{
+ free(ctl->hdr);
+ ctl->syslogfp(ctl);
}
static void logger_open(struct logger_ctl *ctl)
@@ -510,7 +500,7 @@ static void logger_open(struct logger_ctl *ctl)
if (ctl->server) {
ctl->fd = inet_socket(ctl->server, ctl->port, ctl->socket_type);
if (!ctl->syslogfp)
- ctl->syslogfp = syslog_rfc5424;
+ ctl->syslogfp = syslog_rfc5424_header;
return;
}
if (!ctl->unix_socket)
@@ -518,11 +508,19 @@ static void logger_open(struct logger_ctl *ctl)
ctl->fd = unix_socket(ctl, ctl->unix_socket, ctl->socket_type);
if (!ctl->syslogfp)
- ctl->syslogfp = syslog_local;
+ ctl->syslogfp = syslog_local_header;
+ if(!ctl->tag)
+ ctl->tag = xgetlogin();
+ generate_syslog_header(ctl);
}
static void logger_command_line(const struct logger_ctl *ctl, char **argv)
{
+ /* note: we never re-generate the syslog header here, even if we
+ * generate multiple messages. If so, we think it is the right thing
+ * to do to report them with the same timestamp, as the user actually
+ * intended to send a single message.
+ */
char *const buf = xmalloc(ctl->max_message_size + 1);
char *p = buf;
const char *endp = buf + ctl->max_message_size - 1;
@@ -531,12 +529,12 @@ static void logger_command_line(const struct logger_ctl *ctl, char **argv)
while (*argv) {
len = strlen(*argv);
if (endp < p + len && p != buf) {
- ctl->syslogfp(ctl, buf);
+ write_output(ctl, buf);
p = buf;
}
if (ctl->max_message_size < len) {
(*argv)[ctl->max_message_size] = '\0'; /* truncate */
- ctl->syslogfp(ctl, *argv++);
+ write_output(ctl, *argv++);
continue;
}
if (p != buf)
@@ -545,16 +543,17 @@ static void logger_command_line(const struct logger_ctl *ctl, char **argv)
*(p += len) = '\0';
}
if (p != buf)
- ctl->syslogfp(ctl, buf);
+ write_output(ctl, buf);
}
static void logger_stdin(struct logger_ctl *ctl)
{
char *msg;
int default_priority = ctl->pri;
- char *const buf = xmalloc(ctl->max_message_size + 2);
+ const size_t max_usrmsg_size = ctl->max_message_size - strlen(ctl->hdr);
+ char *const buf = xmalloc(max_usrmsg_size + 2);
- while (fgets(buf, ctl->max_message_size+2, stdin) != NULL) {
+ while (fgets(buf, max_usrmsg_size+2, stdin) != NULL) {
int len = strlen(buf);
/* some glibc versions are buggy, they add an additional
@@ -565,7 +564,9 @@ static void logger_stdin(struct logger_ctl *ctl)
ctl->pri = default_priority;
if (ctl->prio_prefix && msg[0] == '<')
msg = get_prio_prefix(msg, &ctl->pri);
- ctl->syslogfp(ctl, msg);
+ /* this potentially runs long, date may have changed (and also PRI) */
+ generate_syslog_header(ctl);
+ write_output(ctl, msg);
}
}
@@ -573,6 +574,7 @@ static void logger_close(const struct logger_ctl *ctl)
{
if (close(ctl->fd) != 0)
err(EXIT_FAILURE, _("close failed"));
+ free(ctl->hdr);
}
static void __attribute__ ((__noreturn__)) usage(FILE *out)
@@ -632,6 +634,7 @@ int main(int argc, char **argv)
.unix_socket_errors = 0,
.server = NULL,
.port = NULL,
+ .hdr = NULL,
.socket_type = ALL_TYPES,
.max_message_size = 1024,
.rfc5424_time = 1,
@@ -731,10 +734,10 @@ int main(int argc, char **argv)
ctl.prio_prefix = 1;
break;
case OPT_RFC3164:
- ctl.syslogfp = syslog_rfc3164;
+ ctl.syslogfp = syslog_rfc3164_header;
break;
case OPT_RFC5424:
- ctl.syslogfp = syslog_rfc5424;
+ ctl.syslogfp = syslog_rfc5424_header;
if (optarg)
parse_rfc5424_flags(&ctl, optarg);
break;
--
2.9.3
From b9ef27f59bffbeaf5397538ea16065d4eb6364c8 Mon Sep 17 00:00:00 2001
From: Rainer Gerhards <rgerhards@adiscon.com>
Date: Sat, 7 Mar 2015 11:49:00 +0100
Subject: [PATCH 075/115] logger: bugfix: missing sanity checks with
--prio-prefix option
There were no apparent sanity checks other than applying the logmask
when reading PRI values from files. As such, invalid PRIs (tested with
values 192, 210, and 2100) are accepted. This in turn can trigger
problems in various receivers, especially older versions. See here
for details:
http://www.rsyslog.com/remote-syslog-pri-vulnerability-cve-2014-3683/
Note that 2100 was changed to 52 as described in above link.
This patch refactors PRI processing. Invalid PRIs are detected and in
this case the message is sent with the default priority, with the
invalid pri being part of the message to be sent. This is along the
line of what 2.26 did when it detected the PRI was invalid.
The refactoring now also enables pricese tracking of syslog header
length in all cases, so --size is now strictly obeyed.
[kzak@redhat.com: - fix compiler warning [-Wunused-variable]]
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 81 ++++++++++++++++++++++++++++++-----------------------
1 file changed, 46 insertions(+), 35 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 3a4d58e..575d111 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -114,25 +114,6 @@ struct logger_ctl {
rfc5424_host:1; /* include hostname */
};
-static char *get_prio_prefix(char *msg, int *prio)
-{
- int p;
- char *end = NULL;
- int facility = *prio & LOG_FACMASK;
-
- errno = 0;
- p = strtoul(msg + 1, &end, 10);
-
- if (errno || !end || end == msg + 1 || end[0] != '>')
- return msg;
-
- if (p & LOG_FACMASK)
- facility = p & LOG_FACMASK;
-
- *prio = facility | (p & LOG_PRIMASK);
- return end + 1;
-}
-
static int decode(const char *name, CODE *codetab)
{
register CODE *c;
@@ -548,25 +529,55 @@ static void logger_command_line(const struct logger_ctl *ctl, char **argv)
static void logger_stdin(struct logger_ctl *ctl)
{
- char *msg;
int default_priority = ctl->pri;
- const size_t max_usrmsg_size = ctl->max_message_size - strlen(ctl->hdr);
- char *const buf = xmalloc(max_usrmsg_size + 2);
+ int last_pri = default_priority;
+ size_t max_usrmsg_size = ctl->max_message_size - strlen(ctl->hdr);
+ char *const buf = xmalloc(max_usrmsg_size + 2 + 2);
+ int pri;
+ int c;
+ size_t i;
- while (fgets(buf, max_usrmsg_size+2, stdin) != NULL) {
- int len = strlen(buf);
+ c = getchar();
+ while (c != EOF) {
+ i = 0;
+ if (ctl->prio_prefix) {
+ if (c == '<') {
+ pri = 0;
+ buf[i++] = c;
+ while(isdigit(c = getchar()) && pri <= 191) {
+ buf[i++] = c;
+ pri = pri * 10 + c - '0';
+ }
+ if (c != EOF && c != '\n')
+ buf[i++] = c;
+ if (c == '>' && 0 <= pri && pri <= 191) { /* valid RFC PRI values */
+ i = 0;
+ if (pri < 8)
+ pri |= 8; /* kern facility is forbidden */
+ ctl->pri = pri;
+ } else
+ ctl->pri = default_priority;
- /* some glibc versions are buggy, they add an additional
- * newline which is removed here. */
- if (0 < len && buf[len - 1] == '\n')
- buf[len - 1] = '\0';
- msg = buf;
- ctl->pri = default_priority;
- if (ctl->prio_prefix && msg[0] == '<')
- msg = get_prio_prefix(msg, &ctl->pri);
- /* this potentially runs long, date may have changed (and also PRI) */
- generate_syslog_header(ctl);
- write_output(ctl, msg);
+ if (ctl->pri != last_pri) {
+ generate_syslog_header(ctl);
+ max_usrmsg_size = ctl->max_message_size - strlen(ctl->hdr);
+ last_pri = ctl->pri;
+ }
+ if (c != EOF && c != '\n')
+ c = getchar();
+ }
+ }
+
+ while (c != EOF && c != '\n' && i < max_usrmsg_size) {
+ buf[i++] = c;
+ c = getchar();
+ }
+ buf[i] = '\0';
+
+ write_output(ctl, buf);
+
+ if (c == '\n') /* discard line terminator */
+ c = getchar();
}
}
--
2.9.3
From ae6846b842b4912f03730ac475e19a7916302893 Mon Sep 17 00:00:00 2001
From: Rainer Gerhards <rgerhards@adiscon.com>
Date: Sat, 7 Mar 2015 12:14:21 +0100
Subject: [PATCH 076/115] logger: add --skip-empty-lines to prevent logging
empty lines
Empty log messages are generally considered useless. This option
enables to turn them off when processing files (including stdin).
[kzak@redhat.com: - rename --skip-empty-lines to --skip-empty,
- add the option to getopt_long(),
- add the option to bash-completion]
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 575d111..0a9e4ab 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -111,7 +111,8 @@ struct logger_ctl {
stderr_printout:1, /* output message to stderr */
rfc5424_time:1, /* include time stamp */
rfc5424_tq:1, /* include time quality markup */
- rfc5424_host:1; /* include hostname */
+ rfc5424_host:1, /* include hostname */
+ skip_empty_lines:1; /* do not send empty lines when processing files */
};
static int decode(const char *name, CODE *codetab)
@@ -574,7 +575,8 @@ static void logger_stdin(struct logger_ctl *ctl)
}
buf[i] = '\0';
- write_output(ctl, buf);
+ if(i > 0 || !ctl->skip_empty_lines)
+ write_output(ctl, buf);
if (c == '\n') /* discard line terminator */
c = getchar();
@@ -600,6 +602,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
fputs(_(" -i log the logger command's PID\n"), out);
fputs(_(" --id[=<id>] log the given <id>, or otherwise the PID\n"), out);
fputs(_(" -f, --file <file> log the contents of this file\n"), out);
+ fputs(_(" -e, --skip-empty do not log empty lines when processing files\n"), out);
fputs(_(" -p, --priority <prio> mark given message with this priority\n"), out);
fputs(_(" --prio-prefix look for a prefix on every line read from stdin\n"), out);
fputs(_(" -s, --stderr output message to standard error as well\n"), out);
@@ -651,6 +654,7 @@ int main(int argc, char **argv)
.rfc5424_time = 1,
.rfc5424_tq = 1,
.rfc5424_host = 1,
+ .skip_empty_lines = 0
};
int ch;
int stdout_reopened = 0;
@@ -676,6 +680,7 @@ int main(int argc, char **argv)
{ "rfc3164", no_argument, 0, OPT_RFC3164 },
{ "rfc5424", optional_argument, 0, OPT_RFC5424 },
{ "size", required_argument, 0, 'S' },
+ { "skip-empty", no_argument, 0, 'e' },
#ifdef HAVE_LIBSYSTEMD
{ "journald", optional_argument, 0, OPT_JOURNALD },
#endif
@@ -687,7 +692,7 @@ int main(int argc, char **argv)
textdomain(PACKAGE);
atexit(close_stdout);
- while ((ch = getopt_long(argc, argv, "f:ip:S:st:u:dTn:P:Vh",
+ while ((ch = getopt_long(argc, argv, "ef:ip:S:st:u:dTn:P:Vh",
longopts, NULL)) != -1) {
switch (ch) {
case 'f': /* file to log */
@@ -695,6 +700,9 @@ int main(int argc, char **argv)
err(EXIT_FAILURE, _("file %s"), optarg);
stdout_reopened = 1;
break;
+ case 'e':
+ ctl.skip_empty_lines = 1;
+ break;
case 'i': /* log process id also */
ctl.pid = getpid();
break;
--
2.9.3
From 7d3a07d87fd1c59927a614a04a2751aa4db420d8 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Tue, 10 Mar 2015 11:37:45 +0100
Subject: [PATCH 077/115] logger: check for ntp_gettime() rather than for
timex.h
It seems that musl libc and uClibc without UCLIBC_NTP_LEGACY
does not provide ntp_gettime and compile will fail.
References: https://github.com/karelzak/util-linux/issues/174
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 0a9e4ab..2fd3ddf 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -366,9 +366,6 @@ static void syslog_rfc5424_header(struct logger_ctl *const ctl)
{
char *hostname = NULL;
char pid[32], time[64], timeq[80];
-#ifdef HAVE_SYS_TIMEX_H
- struct ntptimeval ntptv;
-#endif
struct timeval tv;
struct tm *tm;
@@ -408,7 +405,9 @@ static void syslog_rfc5424_header(struct logger_ctl *const ctl)
snprintf(pid, sizeof(pid), " %d", ctl->pid);
if (ctl->rfc5424_tq) {
-#ifdef HAVE_SYS_TIMEX_H
+#ifdef HAVE_NTP_GETTIME
+ struct ntptimeval ntptv;
+
if (ntp_gettime(&ntptv) == TIME_OK)
snprintf(timeq, sizeof(timeq),
" [timeQuality tzKnown=\"1\" isSynced=\"1\" syncAccuracy=\"%ld\"]",
--
2.9.3
From 4826184bce8df85ae357a102e47e06e345536bc6 Mon Sep 17 00:00:00 2001
From: Rainer Gerhards <rgerhards@adiscon.com>
Date: Tue, 10 Mar 2015 17:26:14 +0100
Subject: [PATCH 078/115] logger: fix multiple format bugs in rfc5424 formatter
This is more or less a complete rewrite of the formatter. It had
multiple issue, e.g. a missing field (MSGID?) and invalid handling
of nil values.
---
misc-utils/logger.c | 78 ++++++++++++++++++++++++++++++++++++++---------------
1 file changed, 57 insertions(+), 21 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 2fd3ddf..d393190 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -362,33 +362,53 @@ static void syslog_rfc3164_header(struct logger_ctl *const ctl)
free(hostname);
}
+/* Some field mappings may be controversal, thus I give the reason
+ * why this specific mapping was used:
+ * APP-NAME <-- tag
+ * Some may argue that "logger" is a better fit, but we think
+ * this is better inline of what other implementations do. In
+ * rsyslog, for example, the TAG value is populated from APP-NAME.
+ * PROCID <-- pid
+ * This is a relatively straightforward interpretation from
+ * RFC5424, sect. 6.2.6.
+ * MSGID <-- '-' (NILVALUE)
+ * One may argue that the string "logger" would be better suited
+ * here so that a receiver can identify the sender process.
+ * However, this does not sound like a good match to RFC5424,
+ * sect. 6.2.7. It may be useful to add an option to logger to
+ * specify a message ID.
+ * Note that appendix A.1 of RFC5424 does not provide clear guidance
+ * of how these fields should be used. This is the case because the
+ * IETF working group couldn't arrive at a clear agreement when we
+ * specified RFC5424. The rest of the field mappings should be
+ * pretty clear from RFC5424. -- Rainer Gerhards, 2015-03-10
+ */
+#define NILVALUE "-"
static void syslog_rfc5424_header(struct logger_ctl *const ctl)
{
- char *hostname = NULL;
- char pid[32], time[64], timeq[80];
- struct timeval tv;
- struct tm *tm;
-
- *pid = *time = *timeq = '\0';
if (ctl->fd < 0)
return;
+ char *time;
if (ctl->rfc5424_time) {
+ struct timeval tv;
+ struct tm *tm;
gettimeofday(&tv, NULL);
if ((tm = localtime(&tv.tv_sec)) != NULL) {
char fmt[64];
-
const size_t i = strftime(fmt, sizeof(fmt),
- " %Y-%m-%dT%H:%M:%S.%%06u%z ", tm);
+ "%Y-%m-%dT%H:%M:%S.%%06u%z ", tm);
/* patch TZ info to comply with RFC3339 (we left SP at end) */
fmt[i-1] = fmt[i-2];
fmt[i-2] = fmt[i-3];
fmt[i-3] = ':';
- snprintf(time, sizeof(time), fmt, tv.tv_usec);
+ xasprintf(&time, fmt, tv.tv_usec);
} else
err(EXIT_FAILURE, _("localtime() failed"));
- }
+ } else
+ time = strdup(NILVALUE);
+ char *hostname;
if (ctl->rfc5424_host) {
hostname = xgethostname();
/* Arbitrary looking 'if (var < strlen()) checks originate from
@@ -396,34 +416,50 @@ static void syslog_rfc5424_header(struct logger_ctl *const ctl)
if (255 < strlen(hostname))
errx(EXIT_FAILURE, _("hostname '%s' is too long"),
hostname);
- }
+ } else
+ hostname = strdup(NILVALUE);
+ char *const app_name = ctl->tag;
if (48 < strlen(ctl->tag))
errx(EXIT_FAILURE, _("tag '%s' is too long"), ctl->tag);
+ char *procid;
if (ctl->pid)
- snprintf(pid, sizeof(pid), " %d", ctl->pid);
+ xasprintf(&procid, "%d", ctl->pid);
+ else
+ procid = strdup(NILVALUE);
+ char *const msgid = strdup(NILVALUE);
+
+ char *structured_data;
if (ctl->rfc5424_tq) {
#ifdef HAVE_NTP_GETTIME
struct ntptimeval ntptv;
-
if (ntp_gettime(&ntptv) == TIME_OK)
- snprintf(timeq, sizeof(timeq),
- " [timeQuality tzKnown=\"1\" isSynced=\"1\" syncAccuracy=\"%ld\"]",
+ xasprintf(&structured_data,
+ "[timeQuality tzKnown=\"1\" isSynced=\"1\" syncAccuracy=\"%ld\"]",
ntptv.maxerror);
else
#endif
- snprintf(timeq, sizeof(timeq),
- " [timeQuality tzKnown=\"1\" isSynced=\"0\"]");
+ xasprintf(&structured_data,
+ "[timeQuality tzKnown=\"1\" isSynced=\"0\"]");
}
- xasprintf(&ctl->hdr, "<%d>1%s%s%s %s -%s%s", ctl->pri, time,
- hostname ? " " : "",
- hostname ? hostname : "",
- ctl->tag, pid, timeq);
+ xasprintf(&ctl->hdr, "<%d>1 %s %s %s %s %s %s ",
+ ctl->pri,
+ time,
+ hostname,
+ app_name,
+ procid,
+ msgid,
+ structured_data);
+ free(time);
free(hostname);
+ /* app_name points to ctl->tag, do NOT free! */
+ free(procid);
+ free(msgid);
+ free(structured_data);
}
static void parse_rfc5424_flags(struct logger_ctl *ctl, char *optarg)
--
2.9.3
From 55f5bc662e69a96c666d097eb5110ea8b3e93d20 Mon Sep 17 00:00:00 2001
From: Rainer Gerhards <rgerhards@adiscon.com>
Date: Tue, 10 Mar 2015 17:47:30 +0100
Subject: [PATCH 079/115] logger: add --msgid option, permits to set RFC5424
MSGID field
---
misc-utils/logger.c | 17 +++++++++++++----
1 file changed, 13 insertions(+), 4 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index d393190..f6e7837 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -90,6 +90,7 @@ enum {
OPT_RFC3164,
OPT_RFC5424,
OPT_SOCKET_ERRORS,
+ OPT_MSGID,
OPT_ID
};
@@ -99,6 +100,7 @@ struct logger_ctl {
pid_t pid; /* zero when unwanted */
char *hdr; /* the syslog header (based on protocol) */
char *tag;
+ char *msgid;
char *unix_socket; /* -u <path> or default to _PATH_DEVLOG */
char *server;
char *port;
@@ -371,12 +373,11 @@ static void syslog_rfc3164_header(struct logger_ctl *const ctl)
* PROCID <-- pid
* This is a relatively straightforward interpretation from
* RFC5424, sect. 6.2.6.
- * MSGID <-- '-' (NILVALUE)
+ * MSGID <-- msgid (from --msgid)
* One may argue that the string "logger" would be better suited
* here so that a receiver can identify the sender process.
* However, this does not sound like a good match to RFC5424,
- * sect. 6.2.7. It may be useful to add an option to logger to
- * specify a message ID.
+ * sect. 6.2.7.
* Note that appendix A.1 of RFC5424 does not provide clear guidance
* of how these fields should be used. This is the case because the
* IETF working group couldn't arrive at a clear agreement when we
@@ -429,7 +430,7 @@ static void syslog_rfc5424_header(struct logger_ctl *const ctl)
else
procid = strdup(NILVALUE);
- char *const msgid = strdup(NILVALUE);
+ char *const msgid = strdup((ctl->msgid) ? ctl->msgid : NILVALUE);
char *structured_data;
if (ctl->rfc5424_tq) {
@@ -650,6 +651,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
fputs(_(" --rfc3164 use the obsolete BSD syslog protocol\n"), out);
fputs(_(" --rfc5424[=<snip>] use the syslog protocol (the default);\n"
" <snip> can be notime, or notq, and/or nohost\n"), out);
+ fputs(_(" --msgid set rfc5424 MSGID field, ignored for non-rfc5424 format\n"), out);
fputs(_(" -u, --socket <socket> write to this Unix socket\n"), out);
fputs(_(" --socket-errors[=<on|off|auto>]\n"
" print connection errors when using Unix sockets\n"), out);
@@ -684,6 +686,7 @@ int main(int argc, char **argv)
.server = NULL,
.port = NULL,
.hdr = NULL,
+ .msgid = NULL,
.socket_type = ALL_TYPES,
.max_message_size = 1024,
.rfc5424_time = 1,
@@ -715,6 +718,7 @@ int main(int argc, char **argv)
{ "rfc3164", no_argument, 0, OPT_RFC3164 },
{ "rfc5424", optional_argument, 0, OPT_RFC5424 },
{ "size", required_argument, 0, 'S' },
+ { "msgid", required_argument, 0, OPT_MSGID },
{ "skip-empty", no_argument, 0, 'e' },
#ifdef HAVE_LIBSYSTEMD
{ "journald", optional_argument, 0, OPT_JOURNALD },
@@ -795,6 +799,11 @@ int main(int argc, char **argv)
if (optarg)
parse_rfc5424_flags(&ctl, optarg);
break;
+ case OPT_MSGID:
+ if(strchr(optarg, ' '))
+ err(EXIT_FAILURE, _("--msgid cannot contain space"));
+ ctl.msgid = optarg;
+ break;
#ifdef HAVE_LIBSYSTEMD
case OPT_JOURNALD:
if (optarg) {
--
2.9.3
From 2cb40465320ca3f0b9425a0631ee7c970276b06b Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Wed, 11 Mar 2015 10:51:13 +0100
Subject: [PATCH 080/115] logger: small change in usage()
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index f6e7837..9c6d6a2 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -649,7 +649,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
fputs(_(" -T, --tcp use TCP only\n"), out);
fputs(_(" -d, --udp use UDP only\n"), out);
fputs(_(" --rfc3164 use the obsolete BSD syslog protocol\n"), out);
- fputs(_(" --rfc5424[=<snip>] use the syslog protocol (the default);\n"
+ fputs(_(" --rfc5424[=<snip>] use the syslog protocol (the default for remote);\n"
" <snip> can be notime, or notq, and/or nohost\n"), out);
fputs(_(" --msgid set rfc5424 MSGID field, ignored for non-rfc5424 format\n"), out);
fputs(_(" -u, --socket <socket> write to this Unix socket\n"), out);
--
2.9.3
From 7ff6948e59172c1942544c9a064708e5666ba20d Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Fri, 13 Mar 2015 13:35:20 +0100
Subject: [PATCH 081/115] logger: use xstrdup()
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 9c6d6a2..ab734dd 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -407,7 +407,7 @@ static void syslog_rfc5424_header(struct logger_ctl *const ctl)
} else
err(EXIT_FAILURE, _("localtime() failed"));
} else
- time = strdup(NILVALUE);
+ time = xstrdup(NILVALUE);
char *hostname;
if (ctl->rfc5424_host) {
@@ -418,7 +418,7 @@ static void syslog_rfc5424_header(struct logger_ctl *const ctl)
errx(EXIT_FAILURE, _("hostname '%s' is too long"),
hostname);
} else
- hostname = strdup(NILVALUE);
+ hostname = xstrdup(NILVALUE);
char *const app_name = ctl->tag;
if (48 < strlen(ctl->tag))
@@ -428,9 +428,9 @@ static void syslog_rfc5424_header(struct logger_ctl *const ctl)
if (ctl->pid)
xasprintf(&procid, "%d", ctl->pid);
else
- procid = strdup(NILVALUE);
+ procid = xstrdup(NILVALUE);
- char *const msgid = strdup((ctl->msgid) ? ctl->msgid : NILVALUE);
+ char *const msgid = xstrdup((ctl->msgid) ? ctl->msgid : NILVALUE);
char *structured_data;
if (ctl->rfc5424_tq) {
--
2.9.3
From 8d341322dbf4ef80dea7821c858292c7ba5e0b25 Mon Sep 17 00:00:00 2001
From: Stef Walter <stef@thewalter.net>
Date: Sun, 15 Mar 2015 14:23:32 +0100
Subject: [PATCH 082/115] logger: Fix use of errno after strtol() without
zeroing first
References: https://bugzilla.redhat.com/show_bug.cgi?id=1202104
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index ab734dd..9613b95 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -127,6 +127,7 @@ static int decode(const char *name, CODE *codetab)
int num;
char *end = NULL;
+ errno = 0;
num = strtol(name, &end, 10);
if (errno || name == end || (end && *end))
return -1;
--
2.9.3
From 9a13f968e67a43fafb9edda16ddf6c635ba3ba60 Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sun, 15 Mar 2015 12:54:47 +0000
Subject: [PATCH 083/115] logger: tidy few indentation issues
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 119 ++++++++++++++++++++++++++--------------------------
1 file changed, 60 insertions(+), 59 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 9613b95..fccd39c 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -160,8 +160,8 @@ static int pencode(char *s)
level = decode(s, prioritynames);
if (level < 0)
errx(EXIT_FAILURE, _("unknown priority name: %s"), s);
- if(facility == LOG_KERN)
- facility = LOG_USER; /* kern is forbidden */
+ if (facility == LOG_KERN)
+ facility = LOG_USER; /* kern is forbidden */
return ((level & LOG_PRIMASK) | (facility & LOG_FACMASK));
}
@@ -295,21 +295,21 @@ static char *xgetlogin(void)
return cp;
}
-
/* this creates a timestamp based on current time according to the
* fine rules of RFC3164, most importantly it ensures in a portable
* way that the month day is correctly written (with a SP instead
* of a leading 0). The function uses a static buffer which is
* overwritten on the next call (just like ctime() does).
*/
-static const char *
-rfc3164_current_time(void)
+static const char *rfc3164_current_time(void)
{
static char time[32];
struct timeval tv;
struct tm *tm;
- static char *monthnames[] = { "Jan", "Feb", "Mar", "Apr",
- "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
+ static char *monthnames[] = {
+ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
+ "Sep", "Oct", "Nov", "Dec"
+ };
gettimeofday(&tv, NULL);
tm = localtime(&tv.tv_sec);
@@ -330,16 +330,15 @@ static void write_output(const struct logger_ctl *ctl, const char *const msg)
const size_t len = xasprintf(&buf, "%s%s", ctl->hdr, msg);
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"));
+ 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);
}
@@ -388,29 +387,35 @@ static void syslog_rfc3164_header(struct logger_ctl *const ctl)
#define NILVALUE "-"
static void syslog_rfc5424_header(struct logger_ctl *const ctl)
{
- if (ctl->fd < 0)
- return;
-
char *time;
+ char *hostname;
+ char *const app_name = ctl->tag;
+ char *procid;
+ char *const msgid = xstrdup(ctl->msgid ? ctl->msgid : NILVALUE);
+ char *structured_data;
+
+ if (ctl->fd < 0)
+ return;
+
if (ctl->rfc5424_time) {
struct timeval tv;
struct tm *tm;
+
gettimeofday(&tv, NULL);
if ((tm = localtime(&tv.tv_sec)) != NULL) {
char fmt[64];
const size_t i = strftime(fmt, sizeof(fmt),
- "%Y-%m-%dT%H:%M:%S.%%06u%z ", tm);
+ "%Y-%m-%dT%H:%M:%S.%%06u%z ", tm);
/* patch TZ info to comply with RFC3339 (we left SP at end) */
- fmt[i-1] = fmt[i-2];
- fmt[i-2] = fmt[i-3];
- fmt[i-3] = ':';
+ fmt[i - 1] = fmt[i - 2];
+ fmt[i - 2] = fmt[i - 3];
+ fmt[i - 3] = ':';
xasprintf(&time, fmt, tv.tv_usec);
} else
err(EXIT_FAILURE, _("localtime() failed"));
} else
time = xstrdup(NILVALUE);
- char *hostname;
if (ctl->rfc5424_host) {
hostname = xgethostname();
/* Arbitrary looking 'if (var < strlen()) checks originate from
@@ -421,22 +426,18 @@ static void syslog_rfc5424_header(struct logger_ctl *const ctl)
} else
hostname = xstrdup(NILVALUE);
- char *const app_name = ctl->tag;
if (48 < strlen(ctl->tag))
errx(EXIT_FAILURE, _("tag '%s' is too long"), ctl->tag);
- char *procid;
if (ctl->pid)
xasprintf(&procid, "%d", ctl->pid);
else
procid = xstrdup(NILVALUE);
- char *const msgid = xstrdup((ctl->msgid) ? ctl->msgid : NILVALUE);
-
- char *structured_data;
if (ctl->rfc5424_tq) {
#ifdef HAVE_NTP_GETTIME
struct ntptimeval ntptv;
+
if (ntp_gettime(&ntptv) == TIME_OK)
xasprintf(&structured_data,
"[timeQuality tzKnown=\"1\" isSynced=\"1\" syncAccuracy=\"%ld\"]",
@@ -528,8 +529,8 @@ static void logger_open(struct logger_ctl *ctl)
ctl->fd = unix_socket(ctl, ctl->unix_socket, ctl->socket_type);
if (!ctl->syslogfp)
ctl->syslogfp = syslog_local_header;
- if(!ctl->tag)
- ctl->tag = xgetlogin();
+ if (!ctl->tag)
+ ctl->tag = xgetlogin();
generate_syslog_header(ctl);
}
@@ -582,9 +583,9 @@ static void logger_stdin(struct logger_ctl *ctl)
if (c == '<') {
pri = 0;
buf[i++] = c;
- while(isdigit(c = getchar()) && pri <= 191) {
- buf[i++] = c;
- pri = pri * 10 + c - '0';
+ while (isdigit(c = getchar()) && pri <= 191) {
+ buf[i++] = c;
+ pri = pri * 10 + c - '0';
}
if (c != EOF && c != '\n')
buf[i++] = c;
@@ -612,10 +613,10 @@ static void logger_stdin(struct logger_ctl *ctl)
}
buf[i] = '\0';
- if(i > 0 || !ctl->skip_empty_lines)
+ if (i > 0 || !ctl->skip_empty_lines)
write_output(ctl, buf);
- if (c == '\n') /* discard line terminator */
+ if (c == '\n') /* discard line terminator */
c = getchar();
}
}
@@ -702,29 +703,29 @@ int main(int argc, char **argv)
FILE *jfd = NULL;
#endif
static const struct option longopts[] = {
- { "id", optional_argument, 0, OPT_ID },
- { "stderr", no_argument, 0, 's' },
- { "file", required_argument, 0, 'f' },
- { "priority", required_argument, 0, 'p' },
- { "tag", required_argument, 0, 't' },
- { "socket", required_argument, 0, 'u' },
+ { "id", optional_argument, 0, OPT_ID },
+ { "stderr", no_argument, 0, 's' },
+ { "file", required_argument, 0, 'f' },
+ { "priority", required_argument, 0, 'p' },
+ { "tag", required_argument, 0, 't' },
+ { "socket", required_argument, 0, 'u' },
{ "socket-errors", required_argument, 0, OPT_SOCKET_ERRORS },
- { "udp", no_argument, 0, 'd' },
- { "tcp", no_argument, 0, 'T' },
- { "server", required_argument, 0, 'n' },
- { "port", required_argument, 0, 'P' },
- { "version", no_argument, 0, 'V' },
- { "help", no_argument, 0, 'h' },
- { "prio-prefix", no_argument, 0, OPT_PRIO_PREFIX },
- { "rfc3164", no_argument, 0, OPT_RFC3164 },
- { "rfc5424", optional_argument, 0, OPT_RFC5424 },
- { "size", required_argument, 0, 'S' },
- { "msgid", required_argument, 0, OPT_MSGID },
- { "skip-empty", no_argument, 0, 'e' },
+ { "udp", no_argument, 0, 'd' },
+ { "tcp", no_argument, 0, 'T' },
+ { "server", required_argument, 0, 'n' },
+ { "port", required_argument, 0, 'P' },
+ { "version", no_argument, 0, 'V' },
+ { "help", no_argument, 0, 'h' },
+ { "prio-prefix", no_argument, 0, OPT_PRIO_PREFIX },
+ { "rfc3164", no_argument, 0, OPT_RFC3164 },
+ { "rfc5424", optional_argument, 0, OPT_RFC5424 },
+ { "size", required_argument, 0, 'S' },
+ { "msgid", required_argument, 0, OPT_MSGID },
+ { "skip-empty", no_argument, 0, 'e' },
#ifdef HAVE_LIBSYSTEMD
- { "journald", optional_argument, 0, OPT_JOURNALD },
+ { "journald", optional_argument, 0, OPT_JOURNALD },
#endif
- { NULL, 0, 0, 0 }
+ { NULL, 0, 0, 0 }
};
setlocale(LC_ALL, "");
@@ -801,8 +802,8 @@ int main(int argc, char **argv)
parse_rfc5424_flags(&ctl, optarg);
break;
case OPT_MSGID:
- if(strchr(optarg, ' '))
- err(EXIT_FAILURE, _("--msgid cannot contain space"));
+ if (strchr(optarg, ' '))
+ err(EXIT_FAILURE, _("--msgid cannot contain space"));
ctl.msgid = optarg;
break;
#ifdef HAVE_LIBSYSTEMD
--
2.9.3
From d5f930614b12c38aeb8cf6f41384e2f5f9f90e9e Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sun, 15 Mar 2015 12:54:48 +0000
Subject: [PATCH 084/115] logger: check xgethostname() return value
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 17 ++++++++++-------
1 file changed, 10 insertions(+), 7 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index fccd39c..5af2446 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -343,9 +343,10 @@ static void write_output(const struct logger_ctl *ctl, const char *const msg)
fprintf(stderr, "%s\n", buf);
}
+#define NILVALUE "-"
static void syslog_rfc3164_header(struct logger_ctl *const ctl)
{
- char pid[30], *hostname, *dot;
+ char pid[30], *hostname;
*pid = '\0';
if (ctl->fd < 0)
@@ -353,10 +354,12 @@ static void syslog_rfc3164_header(struct logger_ctl *const ctl)
if (ctl->pid)
snprintf(pid, sizeof(pid), "[%d]", ctl->pid);
- hostname = xgethostname();
- dot = strchr(hostname, '.');
- if (dot)
- *dot = '\0';
+ if ((hostname = xgethostname())) {
+ char *dot = strchr(hostname, '.');
+ if (dot)
+ *dot = '\0';
+ } else
+ hostname = xstrdup(NILVALUE);
xasprintf(&ctl->hdr, "<%d>%.15s %s %.200s%s: ",
ctl->pri, rfc3164_current_time(), hostname, ctl->tag, pid);
@@ -384,7 +387,6 @@ static void syslog_rfc3164_header(struct logger_ctl *const ctl)
* specified RFC5424. The rest of the field mappings should be
* pretty clear from RFC5424. -- Rainer Gerhards, 2015-03-10
*/
-#define NILVALUE "-"
static void syslog_rfc5424_header(struct logger_ctl *const ctl)
{
char *time;
@@ -417,7 +419,8 @@ static void syslog_rfc5424_header(struct logger_ctl *const ctl)
time = xstrdup(NILVALUE);
if (ctl->rfc5424_host) {
- hostname = xgethostname();
+ if (!(hostname = xgethostname()))
+ hostname = xstrdup(NILVALUE);
/* Arbitrary looking 'if (var < strlen()) checks originate from
* RFC 5424 - 6 Syslog Message Format definition. */
if (255 < strlen(hostname))
--
2.9.3
From 8fce3924e51eed84793dc8d4751c6dd2de17d966 Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sun, 15 Mar 2015 12:54:53 +0000
Subject: [PATCH 085/115] logger: use errx() when checking user input
Additionally inform in usage() the --msgid requires an argument.
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 5af2446..8255ce6 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -656,7 +656,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
fputs(_(" --rfc3164 use the obsolete BSD syslog protocol\n"), out);
fputs(_(" --rfc5424[=<snip>] use the syslog protocol (the default for remote);\n"
" <snip> can be notime, or notq, and/or nohost\n"), out);
- fputs(_(" --msgid set rfc5424 MSGID field, ignored for non-rfc5424 format\n"), out);
+ fputs(_(" --msgid <msgid> set rfc5424 message id field\n"), out);
fputs(_(" -u, --socket <socket> write to this Unix socket\n"), out);
fputs(_(" --socket-errors[=<on|off|auto>]\n"
" print connection errors when using Unix sockets\n"), out);
@@ -806,7 +806,7 @@ int main(int argc, char **argv)
break;
case OPT_MSGID:
if (strchr(optarg, ' '))
- err(EXIT_FAILURE, _("--msgid cannot contain space"));
+ errx(EXIT_FAILURE, _("--msgid cannot contain space"));
ctl.msgid = optarg;
break;
#ifdef HAVE_LIBSYSTEMD
--
2.9.3
From 773df0fa2f6c12581cc8649a08b9d825456a247c Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Mon, 16 Mar 2015 11:51:05 +0100
Subject: [PATCH 086/115] logger: fix rfc5424 format crash
$ logger --rfc5424=notq message
Segmentation fault (core dumped)
Reported-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 8255ce6..10b23dc 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -449,7 +449,8 @@ static void syslog_rfc5424_header(struct logger_ctl *const ctl)
#endif
xasprintf(&structured_data,
"[timeQuality tzKnown=\"1\" isSynced=\"0\"]");
- }
+ } else
+ structured_data = xstrdup(NILVALUE);
xasprintf(&ctl->hdr, "<%d>1 %s %s %s %s %s %s ",
ctl->pri,
--
2.9.3
From fd343a05726658514fc9679cc226ea8aa89549d6 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Mon, 16 Mar 2015 12:38:46 +0100
Subject: [PATCH 087/115] logger: add --no-act for testing
* force --journal mode to also output to stderr when the option
--stderr specified on command line
* add --no-act to avoid all write() operations to make it possible to
write tests without "spam" system logs
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 46 ++++++++++++++++++++++++++++++++--------------
1 file changed, 32 insertions(+), 14 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 10b23dc..be8ff37 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -91,6 +91,7 @@ enum {
OPT_RFC5424,
OPT_SOCKET_ERRORS,
OPT_MSGID,
+ OPT_NOACT,
OPT_ID
};
@@ -109,6 +110,7 @@ struct logger_ctl {
void (*syslogfp)(struct logger_ctl *ctl);
unsigned int
unix_socket_errors:1, /* whether to report or not errors */
+ noact:1, /* do not write to sockets */
prio_prefix:1, /* read priority from intput */
stderr_printout:1, /* output message to stderr */
rfc5424_time:1, /* include time stamp */
@@ -250,12 +252,12 @@ static int inet_socket(const char *servername, const char *port,
}
#ifdef HAVE_LIBSYSTEMD
-static int journald_entry(FILE *fp)
+static int journald_entry(struct logger_ctl *ctl, FILE *fp)
{
struct iovec *iovec;
char *buf = NULL;
ssize_t sz;
- int n, lines, vectors = 8, ret;
+ int n, lines, vectors = 8, ret = 0;
size_t dummy = 0;
iovec = xmalloc(vectors * sizeof(struct iovec));
@@ -277,7 +279,13 @@ static int journald_entry(FILE *fp)
iovec[lines].iov_base = buf;
iovec[lines].iov_len = sz;
}
- ret = sd_journal_sendv(iovec, lines);
+
+ if (!ctl->noact)
+ ret = sd_journal_sendv(iovec, lines);
+ if (ctl->stderr_printout) {
+ for (n = 0; n < lines; n++)
+ fprintf(stderr, "%s\n", (char *) iovec[n].iov_base);
+ }
for (n = 0; n < lines; n++)
free(iovec[n].iov_base);
free(iovec);
@@ -328,17 +336,22 @@ static void write_output(const struct logger_ctl *ctl, const char *const msg)
{
char *buf;
const size_t len = xasprintf(&buf, "%s%s", ctl->hdr, msg);
- 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)
+
+ if (!ctl->noact) {
+ 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);
}
@@ -645,6 +658,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
fputs(_(" --id[=<id>] log the given <id>, or otherwise the PID\n"), out);
fputs(_(" -f, --file <file> log the contents of this file\n"), out);
fputs(_(" -e, --skip-empty do not log empty lines when processing files\n"), out);
+ fputs(_(" --no-act do everything except the write the log\n"), out);
fputs(_(" -p, --priority <prio> mark given message with this priority\n"), out);
fputs(_(" --prio-prefix look for a prefix on every line read from stdin\n"), out);
fputs(_(" -s, --stderr output message to standard error as well\n"), out);
@@ -710,6 +724,7 @@ int main(int argc, char **argv)
{ "id", optional_argument, 0, OPT_ID },
{ "stderr", no_argument, 0, 's' },
{ "file", required_argument, 0, 'f' },
+ { "no-act", no_argument, 0, OPT_NOACT, },
{ "priority", required_argument, 0, 'p' },
{ "tag", required_argument, 0, 't' },
{ "socket", required_argument, 0, 'u' },
@@ -824,6 +839,9 @@ int main(int argc, char **argv)
case OPT_SOCKET_ERRORS:
unix_socket_errors_mode = parse_unix_socket_errors_flags(optarg);
break;
+ case OPT_NOACT:
+ ctl.noact = 1;
+ break;
case '?':
default:
usage(stderr);
@@ -835,7 +853,7 @@ int main(int argc, char **argv)
warnx(_("--file <file> and <message> are mutually exclusive, message is ignored"));
#ifdef HAVE_LIBSYSTEMD
if (jfd) {
- int ret = journald_entry(jfd);
+ int ret = journald_entry(&ctl, jfd);
if (stdin != jfd)
fclose(jfd);
if (ret)
--
2.9.3
From ef5fb2800153fc05d19df202e4ee182fb72f1d86 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Mon, 16 Mar 2015 13:26:52 +0100
Subject: [PATCH 088/115] logger: add -DTEST_LOGGER
"make test_logger" now compiles logger(1) test program
to overwrite system datetime stuff, hostname and PID, for example:
export TZ=GMT
export LOGGER_TEST_TIMEOFDAY=1234567890.123456
export LOGGER_TEST_HOSTNAME=foo
export LOGGER_TEST_GETPID=123
./test_logger --rfc5424 --no-act --stderr -i --tag MyTag mesg
<13>1 2009-02-13T23:31:30.123456+00:00 foo MyTag 123 - [timeQuality tzKnown="1" isSynced="0"] mesg
if the LOGGER_TEST_* variables are not specified then default to
standard logger(1) behavior.
Note that it would be possible to use for example "unshare --utc" to
make hostname stable and portable, but LOGGER_TEST_* variables allow
to keep the tests less complex.
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++------
1 file changed, 50 insertions(+), 6 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index be8ff37..17325ce 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -119,6 +119,50 @@ struct logger_ctl {
skip_empty_lines:1; /* do not send empty lines when processing files */
};
+/*
+ * For tests we want to be able to control datetime outputs
+ */
+#ifdef TEST_LOGGER
+static inline int logger_gettimeofday(struct timeval *tv, struct timezone *tz)
+{
+ char *str = getenv("LOGGER_TEST_TIMEOFDAY");
+ uintmax_t sec, usec;
+
+ if (str && sscanf(str, "%ju.%ju", &sec, &usec) == 2) {
+ tv->tv_sec = sec;
+ tv->tv_usec = usec;
+ return tv->tv_sec == sec && tv->tv_usec == usec;
+ }
+
+ return gettimeofday(tv, tz);
+}
+
+static inline char *logger_xgethostname(void)
+{
+ char *str = getenv("LOGGER_TEST_HOSTNAME");
+ return str ? xstrdup(str) : xgethostname();
+}
+
+static inline pid_t logger_getpid(void)
+{
+ char *str = getenv("LOGGER_TEST_GETPID");
+ unsigned int pid;
+
+ if (str && sscanf(str, "%u", &pid) == 1)
+ return pid;
+ return getpid();
+}
+
+
+#undef HAVE_NTP_GETTIME /* force to default non-NTP */
+
+#else /* !TEST_LOGGER */
+# define logger_gettimeofday(x, y) gettimeofday(x, y)
+# define logger_xgethostname xgethostname
+# define logger_getpid getpid
+#endif
+
+
static int decode(const char *name, CODE *codetab)
{
register CODE *c;
@@ -319,7 +363,7 @@ static const char *rfc3164_current_time(void)
"Sep", "Oct", "Nov", "Dec"
};
- gettimeofday(&tv, NULL);
+ logger_gettimeofday(&tv, NULL);
tm = localtime(&tv.tv_sec);
snprintf(time, sizeof(time),"%s %2d %2.2d:%2.2d:%2.2d",
monthnames[tm->tm_mon], tm->tm_mday,
@@ -367,7 +411,7 @@ static void syslog_rfc3164_header(struct logger_ctl *const ctl)
if (ctl->pid)
snprintf(pid, sizeof(pid), "[%d]", ctl->pid);
- if ((hostname = xgethostname())) {
+ if ((hostname = logger_xgethostname())) {
char *dot = strchr(hostname, '.');
if (dot)
*dot = '\0';
@@ -416,7 +460,7 @@ static void syslog_rfc5424_header(struct logger_ctl *const ctl)
struct timeval tv;
struct tm *tm;
- gettimeofday(&tv, NULL);
+ logger_gettimeofday(&tv, NULL);
if ((tm = localtime(&tv.tv_sec)) != NULL) {
char fmt[64];
const size_t i = strftime(fmt, sizeof(fmt),
@@ -432,7 +476,7 @@ static void syslog_rfc5424_header(struct logger_ctl *const ctl)
time = xstrdup(NILVALUE);
if (ctl->rfc5424_host) {
- if (!(hostname = xgethostname()))
+ if (!(hostname = logger_xgethostname()))
hostname = xstrdup(NILVALUE);
/* Arbitrary looking 'if (var < strlen()) checks originate from
* RFC 5424 - 6 Syslog Message Format definition. */
@@ -764,7 +808,7 @@ int main(int argc, char **argv)
ctl.skip_empty_lines = 1;
break;
case 'i': /* log process id also */
- ctl.pid = getpid();
+ ctl.pid = logger_getpid();
break;
case OPT_ID:
if (optarg) {
@@ -774,7 +818,7 @@ int main(int argc, char **argv)
p++;
ctl.pid = strtoul_or_err(optarg, _("failed to parse id"));
} else
- ctl.pid = getpid();
+ ctl.pid = logger_getpid();
break;
case 'p': /* priority */
ctl.pri = pencode(optarg);
--
2.9.3
From 98e90a49019995a9779768cf9a307638fbc9d7b1 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Mon, 23 Mar 2015 11:40:45 +0100
Subject: [PATCH 089/115] logger: fix LOGGER_TEST_TIMEOFDAY check
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 17325ce..edc9483 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -131,7 +131,7 @@ static inline int logger_gettimeofday(struct timeval *tv, struct timezone *tz)
if (str && sscanf(str, "%ju.%ju", &sec, &usec) == 2) {
tv->tv_sec = sec;
tv->tv_usec = usec;
- return tv->tv_sec == sec && tv->tv_usec == usec;
+ return tv->tv_sec >= 0 && tv->tv_usec >= 0 ? 0 : -EINVAL;
}
return gettimeofday(tv, tz);
--
2.9.3
From 4a8919a4e5b28a47cd61fc8b774a6eaee943b90e Mon Sep 17 00:00:00 2001
From: Patrick Plagwitz <patrick.plagwitz@fau.de>
Date: Mon, 6 Apr 2015 22:50:46 +0100
Subject: [PATCH 090/115] logger: generate header when reading message from
stdin
This change fixes crashing error, that ought not to be simply avoided.
$ echo foo | logger -n localhost
Segmentation fault (core dumped)
If the ctl->hdr is just checked not to be NULL syslog message will not
have valid header, so generating such is not optional when reading
message from stdin and writing it to remote destination.
Reviewed-by: Bernhard Voelker <mail@bernhard-voelker.de>
Signed-off-by: Patrick Plagwitz <patrick.plagwitz@fau.de>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index edc9483..753cd7f 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -582,14 +582,14 @@ static void logger_open(struct logger_ctl *ctl)
ctl->fd = inet_socket(ctl->server, ctl->port, ctl->socket_type);
if (!ctl->syslogfp)
ctl->syslogfp = syslog_rfc5424_header;
- return;
- }
- if (!ctl->unix_socket)
- ctl->unix_socket = _PATH_DEVLOG;
+ } else {
+ if (!ctl->unix_socket)
+ ctl->unix_socket = _PATH_DEVLOG;
- ctl->fd = unix_socket(ctl, ctl->unix_socket, ctl->socket_type);
- if (!ctl->syslogfp)
- ctl->syslogfp = syslog_local_header;
+ ctl->fd = unix_socket(ctl, ctl->unix_socket, ctl->socket_type);
+ if (!ctl->syslogfp)
+ ctl->syslogfp = syslog_local_header;
+ }
if (!ctl->tag)
ctl->tag = xgetlogin();
generate_syslog_header(ctl);
--
2.9.3
From c3dd2ecd5fcaf30860d5fcfd74edfd70a3dd7603 Mon Sep 17 00:00:00 2001
From: Sami Kerola <sami.kerola@lastminute.com>
Date: Tue, 7 Apr 2015 08:58:19 +0100
Subject: [PATCH 091/115] logger: fix memory leaks
Reported-by: Patrick Plagwitz <patrick.plagwitz@fau.de>
Signed-off-by: Sami Kerola <sami.kerola@lastminute.com>
---
misc-utils/logger.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 753cd7f..6316a76 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -395,9 +395,9 @@ static void write_output(const struct logger_ctl *ctl, const char *const msg)
warn(_("write failed"));
}
}
-
if (ctl->stderr_printout)
fprintf(stderr, "%s\n", buf);
+ free(buf);
}
#define NILVALUE "-"
@@ -625,6 +625,7 @@ static void logger_command_line(const struct logger_ctl *ctl, char **argv)
}
if (p != buf)
write_output(ctl, buf);
+ free(buf);
}
static void logger_stdin(struct logger_ctl *ctl)
--
2.9.3
From bcf7e149393ee41af3e092a58dba643c8c091c28 Mon Sep 17 00:00:00 2001
From: Ruediger Meier <ruediger.meier@ga-group.nl>
Date: Thu, 26 Mar 2015 14:20:29 +0100
Subject: [PATCH 092/115] logger: --stderr and --no-act turn "auto-errors" on
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
---
misc-utils/logger.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 6316a76..8908dfc 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -914,10 +914,9 @@ int main(int argc, char **argv)
ctl.unix_socket_errors = 1;
break;
case AF_UNIX_ERRORS_AUTO:
+ ctl.unix_socket_errors = ctl.noact || ctl.stderr_printout;
#ifdef HAVE_LIBSYSTEMD
- ctl.unix_socket_errors = sd_booted();
-#else
- ctl.unix_socket_errors = 0;
+ ctl.unix_socket_errors |= !!sd_booted();
#endif
break;
default:
--
2.9.3
From 9b83e7a48d023b2822de930c9408a127654144f1 Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sun, 19 Jul 2015 21:19:12 +0100
Subject: [PATCH 093/115] logger: do not exit when socket errors are not
enforced
The libc openlog(3) does not have error detection whether unix socket
could be opened. As a side effect that made it possible to use logger
even if syslogd was not running. Of course user message in these cases
were lost. This change makes the logger do behave similar way again, so
that sysvinit scripts can successfully pipe messages to logger when ever.
Addresses: https://bugs.debian.org/787864
Addresses: https://bugs.debian.org/790875
Reported-by: Andreas Beckmann <anbe@debian.org>
Reported-by: Andreas Henriksson <andreas@fatal.se>
Tested-by: Robie Basak <robie.basak@ubuntu.com>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 8908dfc..9947b75 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -242,9 +242,10 @@ static int unix_socket(struct logger_ctl *ctl, const char *path, const int socke
if (ctl->unix_socket_errors)
err(EXIT_FAILURE, _("socket %s"), path);
else
- /* See --socket-errors manual page entry for
- * explanation of this strange exit. */
- exit(EXIT_SUCCESS);
+ /* openlog(3) compatibility, socket errors are
+ * not reported, but ignored silently */
+ ctl->noact = 1;
+ return -1;
}
return fd;
}
@@ -685,7 +686,7 @@ static void logger_stdin(struct logger_ctl *ctl)
static void logger_close(const struct logger_ctl *ctl)
{
- if (close(ctl->fd) != 0)
+ if (ctl->fd != -1 && close(ctl->fd) != 0)
err(EXIT_FAILURE, _("close failed"));
free(ctl->hdr);
}
--
2.9.3
From b6b67955ef31ff4f26a05c2de8bf1679a20d64ba Mon Sep 17 00:00:00 2001
From: Alex Bligh <alex@alex.org.uk>
Date: Wed, 15 Jul 2015 19:01:48 +0100
Subject: [PATCH 094/115] logger: Add support to logger for RFC6587 octet
counting
This patch adds support to logger for RFC6587 octet counting.
RFC6587 provides support for two sorts of framing:
1. Octet counting (at RFC6587 s3.4.1)
In essence each frame is preceded by a decimal length and a
space.
2. Non-transparent framing (at RFC6587 s3.4.2), also called
'octet stuffing'
In essence each frame is terminated by a `\n`
Prior to this patch, logger used option 2 (non-transparent framing)
on TCP, and used no framing on UDP. After this patch, the default
behaviour is unchanged, but if the '--octet-count' option is supplied,
option 1 is used for both TCP and UDP. Arguably octet count framing
makes little sense on UDP, but some servers provide it and this
allows testing of those servers.
Signed-off-by: Alex Bligh <alex@alex.org.uk>
---
misc-utils/logger.c | 24 +++++++++++++++++-------
1 file changed, 17 insertions(+), 7 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 9947b75..181121a 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -92,7 +92,8 @@ enum {
OPT_SOCKET_ERRORS,
OPT_MSGID,
OPT_NOACT,
- OPT_ID
+ OPT_ID,
+ OPT_OCTET_COUNT
};
struct logger_ctl {
@@ -116,7 +117,8 @@ struct logger_ctl {
rfc5424_time:1, /* include time stamp */
rfc5424_tq:1, /* include time quality markup */
rfc5424_host:1, /* include hostname */
- skip_empty_lines:1; /* do not send empty lines when processing files */
+ skip_empty_lines:1, /* do not send empty lines when processing files */
+ octet_count:1; /* use RFC6587 octet counting */
};
/*
@@ -373,19 +375,22 @@ static const char *rfc3164_current_time(void)
}
/* 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.
+ * we use RFC6587 octet-stuffing (unless octet-counting is selected).
+ * This is not great, but doing full blown RFC5425 (TLS) looks like
+ * it is too much for the logger utility. If octet-counting is
+ * selected, we use that.
*/
static void write_output(const struct logger_ctl *ctl, const char *const msg)
{
char *buf;
- const size_t len = xasprintf(&buf, "%s%s", ctl->hdr, msg);
+ const size_t len = ctl->octet_count ?
+ xasprintf(&buf, "%zu %s%s", strlen(ctl->hdr)+strlen(msg), ctl->hdr, msg):
+ xasprintf(&buf, "%s%s", ctl->hdr, msg);
if (!ctl->noact) {
if (write_all(ctl->fd, buf, len) < 0)
warn(_("write failed"));
- else if (ctl->socket_type == TYPE_TCP) {
+ else if ((ctl->socket_type == TYPE_TCP) && !ctl->octet_count) {
/* 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
@@ -706,6 +711,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
fputs(_(" -e, --skip-empty do not log empty lines when processing files\n"), out);
fputs(_(" --no-act do everything except the write the log\n"), out);
fputs(_(" -p, --priority <prio> mark given message with this priority\n"), out);
+ fputs(_(" --octet-count use rfc6587 octet counting\n"), out);
fputs(_(" --prio-prefix look for a prefix on every line read from stdin\n"), out);
fputs(_(" -s, --stderr output message to standard error as well\n"), out);
fputs(_(" -S, --size <size> maximum size for a single message\n"), out);
@@ -781,6 +787,7 @@ int main(int argc, char **argv)
{ "port", required_argument, 0, 'P' },
{ "version", no_argument, 0, 'V' },
{ "help", no_argument, 0, 'h' },
+ { "octet-count", no_argument, 0, OPT_OCTET_COUNT },
{ "prio-prefix", no_argument, 0, OPT_PRIO_PREFIX },
{ "rfc3164", no_argument, 0, OPT_RFC3164 },
{ "rfc5424", optional_argument, 0, OPT_RFC5424 },
@@ -855,6 +862,9 @@ int main(int argc, char **argv)
exit(EXIT_SUCCESS);
case 'h':
usage(stdout);
+ case OPT_OCTET_COUNT:
+ ctl.octet_count = 1;
+ break;
case OPT_PRIO_PREFIX:
ctl.prio_prefix = 1;
break;
--
2.9.3
From 28bad822c4516a4365262b6023f8d24858e140f0 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Wed, 5 Aug 2015 13:23:34 +0200
Subject: [PATCH 095/115] logger: improve readability [smatch scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 181121a..e4a6c5a 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -243,11 +243,11 @@ static int unix_socket(struct logger_ctl *ctl, const char *path, const int socke
if (i == 0) {
if (ctl->unix_socket_errors)
err(EXIT_FAILURE, _("socket %s"), path);
- else
- /* openlog(3) compatibility, socket errors are
- * not reported, but ignored silently */
- ctl->noact = 1;
- return -1;
+
+ /* openlog(3) compatibility, socket errors are
+ * not reported, but ignored silently */
+ ctl->noact = 1;
+ return -1;
}
return fd;
}
--
2.9.3
From 4299ed1ce2f088cced8a5b9420811ddce27eae8e Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Thu, 1 Oct 2015 14:48:15 +0200
Subject: [PATCH 096/115] logger: add --sd-id and -sd-param
This patch add support for RFC 5424 structured data elements. For
example:
logger --rfc5424 --sd-id zoo@123 \
--sd-param tiger=\"hungry\" \
--sd-param zebra=\"running\" \
--sd-id manager@123 \
--sd-param onMeeting=\"yes\" \
"this is message"
produces:
<13>1 2015-10-01T14:07:59.168662+02:00 ws kzak - - [timeQuality tzKnown="1" isSynced="1" syncAccuracy="218616"][zoo@123 tiger="hungry" zebra="running"][manager@123 onMeeting="yes"] this is message
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 246 +++++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 232 insertions(+), 14 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index e4a6c5a..c8ec8fa 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -59,6 +59,8 @@
#include "pathnames.h"
#include "strutils.h"
#include "xalloc.h"
+#include "strv.h"
+#include "list.h"
#define SYSLOG_NAMES
#include <syslog.h>
@@ -93,9 +95,19 @@ enum {
OPT_MSGID,
OPT_NOACT,
OPT_ID,
+ OPT_STRUCTURED_DATA_ID,
+ OPT_STRUCTURED_DATA_PARAM,
OPT_OCTET_COUNT
};
+/* rfc5424 structured data */
+struct structured_data {
+ char *id; /* SD-ID */
+ char **params; /* array with SD-PARAMs */
+
+ struct list_head sds;
+};
+
struct logger_ctl {
int fd;
int pri;
@@ -108,7 +120,11 @@ struct logger_ctl {
char *port;
int socket_type;
size_t max_message_size;
+ struct list_head user_sds; /* user defined rfc5424 structured data */
+ struct list_head reserved_sds; /* standard rfc5424 structured data */
+
void (*syslogfp)(struct logger_ctl *ctl);
+
unsigned int
unix_socket_errors:1, /* whether to report or not errors */
noact:1, /* do not write to sockets */
@@ -430,7 +446,177 @@ static void syslog_rfc3164_header(struct logger_ctl *const ctl)
free(hostname);
}
-/* Some field mappings may be controversal, thus I give the reason
+static inline struct list_head *get_user_structured_data(struct logger_ctl *ctl)
+{
+ return &ctl->user_sds;
+}
+
+static inline struct list_head *get_reserved_structured_data(struct logger_ctl *ctl)
+{
+ return &ctl->reserved_sds;
+}
+
+static int has_structured_data_id(struct list_head *ls, const char *id)
+{
+ struct list_head *p;
+
+ if (!ls || list_empty(ls))
+ return 0;
+
+ list_for_each(p, ls) {
+ struct structured_data *sd = list_entry(p, struct structured_data, sds);
+ if (sd->id && strcmp(sd->id, id) == 0)
+ return 1;
+ }
+
+ return 0;
+}
+
+static void add_structured_data_id(struct list_head *ls, const char *id)
+{
+ struct structured_data *sd;
+
+ assert(id);
+
+ if (has_structured_data_id(ls, id))
+ errx(EXIT_FAILURE, _("structured data ID '%s' is not unique"), id);
+
+ sd = xcalloc(1, sizeof(*sd));
+ INIT_LIST_HEAD(&sd->sds);
+ sd->id = xstrdup(id);
+
+ list_add_tail(&sd->sds, ls);
+}
+
+static void add_structured_data_param(struct list_head *ls, const char *param)
+{
+ struct structured_data *sd;
+
+ if (list_empty(ls))
+ errx(EXIT_FAILURE, _("--sd-id no specified for --sd-param %s"), param);
+
+ assert(param);
+
+ sd = list_last_entry(ls, struct structured_data, sds);
+
+ if (strv_extend(&sd->params, param))
+ err_oom();
+}
+
+static void add_structured_data_paramf(struct list_head *ls, const char *fmt, ...)
+{
+ struct structured_data *sd;
+ va_list ap;
+ int x;
+
+ assert(!list_empty(ls));
+ assert(fmt);
+
+ sd = list_last_entry(ls, struct structured_data, sds);
+ va_start(ap, fmt);
+ x = strv_extendv(&sd->params, fmt, ap);
+ va_end(ap);
+
+ if (x)
+ err_oom();
+}
+
+static char *strdup_structured_data(struct structured_data *sd)
+{
+ char *res, *tmp;
+
+ if (strv_isempty(sd->params))
+ return NULL;
+
+ xasprintf(&res, "[%s %s]", sd->id,
+ (tmp = strv_join(sd->params, " ")));
+ free(tmp);
+ return res;
+}
+
+static char *strdup_structured_data_list(struct list_head *ls)
+{
+ struct list_head *p;
+ char *res = NULL;
+
+ list_for_each(p, ls) {
+ struct structured_data *sd = list_entry(p, struct structured_data, sds);
+ char *one = strdup_structured_data(sd);
+ char *tmp = res;
+
+ if (!one)
+ continue;
+ res = strappend(tmp, one);
+ free(tmp);
+ free(one);
+ }
+
+ return res;
+}
+
+static char *get_structured_data_string(struct logger_ctl *ctl)
+{
+ char *sys = NULL, *usr = NULL, *res;
+
+ if (!list_empty(&ctl->reserved_sds))
+ sys = strdup_structured_data_list(&ctl->reserved_sds);
+ if (!list_empty(&ctl->user_sds))
+ usr = strdup_structured_data_list(&ctl->user_sds);
+
+ if (sys && usr) {
+ res = strappend(sys, usr);
+ free(sys);
+ free(usr);
+ } else
+ res = sys ? sys : usr;
+
+ return res;
+}
+
+static int valid_structured_data_param(const char *str)
+{
+ char *eq = strchr(str, '='),
+ *qm1 = strchr(str, '"'),
+ *qm2 = qm1 ? strchr(qm1 + 1, '"') : NULL;
+
+ if (!eq || !qm1 || !qm2) /* something is missing */
+ return 0;
+
+ /* foo="bar" */
+ return eq > str && eq < qm1 && eq + 1 == qm1 && qm1 < qm2 && *(qm2 + 1) == '\0';
+}
+
+/* SD-ID format:
+ * name@<private enterprise number>, e.g., "ourSDID@32473"
+ */
+static int valid_structured_data_id(const char *str)
+{
+ char *at = strchr(str, '@');
+ const char *p;
+
+ /* standardized IDs without @<digits> */
+ if (!at && (strcmp(str, "timeQuality") == 0 ||
+ strcmp(str, "origin") == 0 ||
+ strcmp(str, "meta") == 0))
+ return 1;
+
+ if (!at || at == str || !*(at + 1))
+ return 0;
+ if (!isdigit_string(at + 1))
+ return 0;
+
+ /* check for forbidden chars in the <name> */
+ for (p = str; p < at; p++) {
+ if (*p == '[' || *p == '=' || *p == '"' || *p == '@')
+ return 0;
+ if (isblank((unsigned int) *p) || iscntrl((unsigned int) *p))
+ return 0;
+ }
+ return 1;
+}
+
+
+/* Some field mappings may be controversial, thus I give the reason
* why this specific mapping was used:
* APP-NAME <-- tag
* Some may argue that "logger" is a better fit, but we think
@@ -457,7 +643,8 @@ static void syslog_rfc5424_header(struct logger_ctl *const ctl)
char *const app_name = ctl->tag;
char *procid;
char *const msgid = xstrdup(ctl->msgid ? ctl->msgid : NILVALUE);
- char *structured_data;
+ char *structured = NULL;
+ struct list_head *sd;
if (ctl->fd < 0)
return;
@@ -500,20 +687,29 @@ static void syslog_rfc5424_header(struct logger_ctl *const ctl)
else
procid = xstrdup(NILVALUE);
- if (ctl->rfc5424_tq) {
+ sd = get_reserved_structured_data(ctl);
+
+ /* time quality structured data (maybe overwriten by --sd-id timeQuality) */
+ if (ctl->rfc5424_tq && !has_structured_data_id(sd, "timeQuality")) {
+
+ add_structured_data_id(sd, "timeQuality");
+ add_structured_data_param(sd, "tzKnown=\"1\"");
+
#ifdef HAVE_NTP_GETTIME
struct ntptimeval ntptv;
- if (ntp_gettime(&ntptv) == TIME_OK)
- xasprintf(&structured_data,
- "[timeQuality tzKnown=\"1\" isSynced=\"1\" syncAccuracy=\"%ld\"]",
- ntptv.maxerror);
- else
+ if (ntp_gettime(&ntptv) == TIME_OK) {
+ add_structured_data_param(sd, "isSynced=\"1\"");
+ add_structured_data_paramf(sd, "syncAccuracy=\"%ld\"", ntptv.maxerror);
+ } else
#endif
- xasprintf(&structured_data,
- "[timeQuality tzKnown=\"1\" isSynced=\"0\"]");
- } else
- structured_data = xstrdup(NILVALUE);
+ add_structured_data_paramf(sd, "isSynced=\"0\"");
+ }
+
+ /* convert all structured data to string */
+ structured = get_structured_data_string(ctl);
+ if (!structured)
+ structured = xstrdup(NILVALUE);
xasprintf(&ctl->hdr, "<%d>1 %s %s %s %s %s %s ",
ctl->pri,
@@ -522,14 +718,14 @@ static void syslog_rfc5424_header(struct logger_ctl *const ctl)
app_name,
procid,
msgid,
- structured_data);
+ structured);
free(time);
free(hostname);
/* app_name points to ctl->tag, do NOT free! */
free(procid);
free(msgid);
- free(structured_data);
+ free(structured);
}
static void parse_rfc5424_flags(struct logger_ctl *ctl, char *optarg)
@@ -723,6 +919,8 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
fputs(_(" --rfc3164 use the obsolete BSD syslog protocol\n"), out);
fputs(_(" --rfc5424[=<snip>] use the syslog protocol (the default for remote);\n"
" <snip> can be notime, or notq, and/or nohost\n"), out);
+ fputs(_(" --sd-id <id> rfc5424 structured data ID\n"), out);
+ fputs(_(" --sd-param <data> rfc5424 structured data name=value\n"), out);
fputs(_(" --msgid <msgid> set rfc5424 message id field\n"), out);
fputs(_(" -u, --socket <socket> write to this Unix socket\n"), out);
fputs(_(" --socket-errors[=<on|off|auto>]\n"
@@ -794,6 +992,8 @@ int main(int argc, char **argv)
{ "size", required_argument, 0, 'S' },
{ "msgid", required_argument, 0, OPT_MSGID },
{ "skip-empty", no_argument, 0, 'e' },
+ { "sd-id", required_argument, 0, OPT_STRUCTURED_DATA_ID },
+ { "sd-param", required_argument, 0, OPT_STRUCTURED_DATA_PARAM },
#ifdef HAVE_LIBSYSTEMD
{ "journald", optional_argument, 0, OPT_JOURNALD },
#endif
@@ -805,6 +1005,9 @@ int main(int argc, char **argv)
textdomain(PACKAGE);
atexit(close_stdout);
+ INIT_LIST_HEAD(&ctl.user_sds);
+ INIT_LIST_HEAD(&ctl.reserved_sds);
+
while ((ch = getopt_long(argc, argv, "ef:ip:S:st:u:dTn:P:Vh",
longopts, NULL)) != -1) {
switch (ch) {
@@ -898,6 +1101,16 @@ int main(int argc, char **argv)
case OPT_NOACT:
ctl.noact = 1;
break;
+ case OPT_STRUCTURED_DATA_ID:
+ if (!valid_structured_data_id(optarg))
+ errx(EXIT_FAILURE, _("invalid structured data ID: '%s'"), optarg);
+ add_structured_data_id(get_user_structured_data(&ctl), optarg);
+ break;
+ case OPT_STRUCTURED_DATA_PARAM:
+ if (!valid_structured_data_param(optarg))
+ errx(EXIT_FAILURE, _("invalid structured data parameter: '%s'"), optarg);
+ add_structured_data_param(get_user_structured_data(&ctl), optarg);
+ break;
case '?':
default:
usage(stderr);
@@ -917,6 +1130,11 @@ int main(int argc, char **argv)
return EXIT_SUCCESS;
}
#endif
+
+ /* user overwrites build-in SD-ELEMENT */
+ if (has_structured_data_id(get_user_structured_data(&ctl), "timeQuality"))
+ ctl.rfc5424_tq = 0;
+
switch (unix_socket_errors_mode) {
case AF_UNIX_ERRORS_OFF:
ctl.unix_socket_errors = 0;
--
2.9.3
From fc20393cdb5ed960fa4d6822c2fbf15fc191a6a0 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Tue, 6 Oct 2015 09:16:33 +0200
Subject: [PATCH 097/115] logger: fix messages separation on UNIX socket
The function write_output() add additional \n after each message on
TYPE_TPC. This is required by syslog daemons, otherwise you will see
multiple log messages merged together in your log file, for example:
Oct 6 09:01:40 ws kzak: AAA<14>Oct 6 09:01:40 kzak: BBB
for
printf "AAA\nBBB\n" | logger -p info -u <any-socket>
Unfortunately, the connection initialization functions keep the
default ALL_TYPES as connection type and nowhere in the control struct
is info about the final real connection type. The problem is invisible
when you specify --tpc or --udp on logger command line.
Addresses: https://github.com/karelzak/util-linux/issues/225
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 34 +++++++++++++++++++++++-----------
1 file changed, 23 insertions(+), 11 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index c8ec8fa..6a96554 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -229,9 +229,9 @@ static int pencode(char *s)
return ((level & LOG_PRIMASK) | (facility & LOG_FACMASK));
}
-static int unix_socket(struct logger_ctl *ctl, const char *path, const int socket_type)
+static int unix_socket(struct logger_ctl *ctl, const char *path, int *socket_type)
{
- int fd, i;
+ int fd, i, type = -1;
static struct sockaddr_un s_addr; /* AF_UNIX address of local logger */
if (strlen(path) >= sizeof(s_addr.sun_path))
@@ -243,10 +243,14 @@ static int unix_socket(struct logger_ctl *ctl, const char *path, const int socke
for (i = 2; i; i--) {
int st = -1;
- if (i == 2 && socket_type & TYPE_UDP)
+ if (i == 2 && *socket_type & TYPE_UDP) {
st = SOCK_DGRAM;
- if (i == 1 && socket_type & TYPE_TCP)
+ type = TYPE_UDP;
+ }
+ if (i == 1 && *socket_type & TYPE_TCP) {
st = SOCK_STREAM;
+ type = TYPE_TCP;
+ }
if (st == -1 || (fd = socket(AF_UNIX, st, 0)) == -1)
continue;
if (connect(fd, (struct sockaddr *)&s_addr, sizeof(s_addr)) == -1) {
@@ -265,25 +269,30 @@ static int unix_socket(struct logger_ctl *ctl, const char *path, const int socke
ctl->noact = 1;
return -1;
}
+
+ /* replace ALL_TYPES with the real TYPE_* */
+ if (type > 0 && type != *socket_type)
+ *socket_type = type;
return fd;
}
-static int inet_socket(const char *servername, const char *port,
- const int socket_type)
+static int inet_socket(const char *servername, const char *port, int *socket_type)
{
- int fd, errcode, i;
+ int fd, errcode, i, type = -1;
struct addrinfo hints, *res;
const char *p = port;
for (i = 2; i; i--) {
memset(&hints, 0, sizeof(hints));
- if (i == 2 && socket_type & TYPE_UDP) {
+ if (i == 2 && *socket_type & TYPE_UDP) {
hints.ai_socktype = SOCK_DGRAM;
+ type = TYPE_UDP;
if (port == NULL)
p = "syslog";
}
- if (i == 1 && socket_type & TYPE_TCP) {
+ if (i == 1 && *socket_type & TYPE_TCP) {
hints.ai_socktype = SOCK_STREAM;
+ type = TYPE_TCP;
if (port == NULL)
p = "syslog-conn";
}
@@ -311,6 +320,9 @@ static int inet_socket(const char *servername, const char *port,
if (i == 0)
errx(EXIT_FAILURE, _("failed to connect to %s port %s"), servername, p);
+ /* replace ALL_TYPES with the real TYPE_* */
+ if (type > 0 && type != *socket_type)
+ *socket_type = type;
return fd;
}
@@ -781,14 +793,14 @@ static void generate_syslog_header(struct logger_ctl *const ctl)
static void logger_open(struct logger_ctl *ctl)
{
if (ctl->server) {
- ctl->fd = inet_socket(ctl->server, ctl->port, ctl->socket_type);
+ ctl->fd = inet_socket(ctl->server, ctl->port, &ctl->socket_type);
if (!ctl->syslogfp)
ctl->syslogfp = syslog_rfc5424_header;
} else {
if (!ctl->unix_socket)
ctl->unix_socket = _PATH_DEVLOG;
- ctl->fd = unix_socket(ctl, ctl->unix_socket, ctl->socket_type);
+ ctl->fd = unix_socket(ctl, ctl->unix_socket, &ctl->socket_type);
if (!ctl->syslogfp)
ctl->syslogfp = syslog_local_header;
}
--
2.9.3
From 94a28496aa3fcec48dc58917342ec6bcc23f0c31 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Tue, 27 Oct 2015 13:19:16 +0100
Subject: [PATCH 098/115] logger: use iovec and sendmsg() to send message
The iovec based solutions allow to send multiple strings by one
syscall (for example additional \n messages separator). We can also
use it to send additional socket header metadata (e.g.
SCM_CREDENTIALS) later.
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 30 +++++++++++++++++++-----------
1 file changed, 19 insertions(+), 11 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 6a96554..9878c82 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -416,18 +416,26 @@ static void write_output(const struct logger_ctl *ctl, const char *const msg)
xasprintf(&buf, "%s%s", ctl->hdr, msg);
if (!ctl->noact) {
- if (write_all(ctl->fd, buf, len) < 0)
- warn(_("write failed"));
- else if ((ctl->socket_type == TYPE_TCP) && !ctl->octet_count) {
- /* 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"));
+ struct msghdr msg = { 0 };
+ struct iovec iov[2];
+ size_t iovlen = 0;
+
+ iov[0].iov_base = buf;
+ iov[0].iov_len = len;
+ iovlen++;
+
+ /* add extra \n to make sure message is terminated */
+ if ((ctl->socket_type == TYPE_TCP) && !ctl->octet_count) {
+ iov[1].iov_base = "\n";
+ iov[1].iov_len = 1;
+ iovlen++;
}
+
+ msg.msg_iov = iov;
+ msg.msg_iovlen = iovlen;
+
+ if (sendmsg(ctl->fd, &msg, 0) < 0)
+ warn(_("send message failed"));
}
if (ctl->stderr_printout)
fprintf(stderr, "%s\n", buf);
--
2.9.3
From 17c8aa1dc0f05fb68ccd78aa725f8fe8a5cc269d Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Tue, 27 Oct 2015 18:44:00 +0100
Subject: [PATCH 099/115] logger: use iovec for all message
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 63 +++++++++++++++++++++++++++++++++++++----------------
1 file changed, 44 insertions(+), 19 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 9878c82..d5683b6 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -402,6 +402,22 @@ static const char *rfc3164_current_time(void)
return time;
}
+#define next_iovec(ary, idx) __extension__ ({ \
+ assert(ARRAY_SIZE(ary) > idx); \
+ assert(idx >= 0); \
+ &ary[idx++]; \
+})
+
+#define iovec_add_string(ary, idx, str, len) \
+ do { \
+ struct iovec *v = next_iovec(ary, idx); \
+ v->iov_base = (void *) str; \
+ v->iov_len = len ? len : strlen(str); \
+ } while (0)
+
+#define iovec_memcmp(ary, idx, str, len) \
+ memcmp((ary)[(idx) - 1].iov_base, str, len)
+
/* writes generated buffer to desired destination. For TCP syslog,
* we use RFC6587 octet-stuffing (unless octet-counting is selected).
* This is not great, but doing full blown RFC5425 (TLS) looks like
@@ -410,26 +426,28 @@ static const char *rfc3164_current_time(void)
*/
static void write_output(const struct logger_ctl *ctl, const char *const msg)
{
- char *buf;
- const size_t len = ctl->octet_count ?
- xasprintf(&buf, "%zu %s%s", strlen(ctl->hdr)+strlen(msg), ctl->hdr, msg):
- xasprintf(&buf, "%s%s", ctl->hdr, msg);
+ struct iovec iov[4];
+ int iovlen = 0;
+ char *octet = NULL;
+
+ /* 1) octen count */
+ if (ctl->octet_count) {
+ size_t len = xasprintf(&octet, "%zu ", strlen(ctl->hdr) + strlen(msg));
+ iovec_add_string(iov, iovlen, octet, len);
+ }
+
+ /* 2) header */
+ iovec_add_string(iov, iovlen, ctl->hdr, 0);
+
+ /* 3) message */
+ iovec_add_string(iov, iovlen, msg, 0);
if (!ctl->noact) {
struct msghdr msg = { 0 };
- struct iovec iov[2];
- size_t iovlen = 0;
- iov[0].iov_base = buf;
- iov[0].iov_len = len;
- iovlen++;
-
- /* add extra \n to make sure message is terminated */
- if ((ctl->socket_type == TYPE_TCP) && !ctl->octet_count) {
- iov[1].iov_base = "\n";
- iov[1].iov_len = 1;
- iovlen++;
- }
+ /* 4) add extra \n to make sure message is terminated */
+ if ((ctl->socket_type == TYPE_TCP) && !ctl->octet_count)
+ iovec_add_string(iov, iovlen, "\n", 1);
msg.msg_iov = iov;
msg.msg_iovlen = iovlen;
@@ -437,9 +455,16 @@ static void write_output(const struct logger_ctl *ctl, const char *const msg)
if (sendmsg(ctl->fd, &msg, 0) < 0)
warn(_("send message failed"));
}
- if (ctl->stderr_printout)
- fprintf(stderr, "%s\n", buf);
- free(buf);
+
+ if (ctl->stderr_printout) {
+ /* make sure it's terminated for stderr */
+ if (iovec_memcmp(iov, iovlen, "\n", 1) != 0)
+ iovec_add_string(iov, iovlen, "\n", 1);
+
+ ignore_result( writev(STDERR_FILENO, iov, iovlen) );
+ }
+
+ free(octet);
}
#define NILVALUE "-"
--
2.9.3
From 27a9eb5359c13e403cee2f89c0545c892b5327e3 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Thu, 29 Oct 2015 11:18:21 +0100
Subject: [PATCH 100/115] logger: use --id as local socket credentials
If you have really paranoid syslog (or systemd who listens on /dev/log)
then it replaces in the message PID with a real PID from socket header
credentials:
# echo $PPID
1550
# logger -p info --stderr --id=$PPID "This is message baby!"
<14>Oct 29 11:22:13 kzak[1550]: This is message baby!
# journald -n 1
Oct 29 11:22:13 ws kzak[22100]: This is message baby!
^^^^^
This patch forces kernel to accept another *valid* PID if logger(1)
executed with root permissions; improved version:
# logger -p info --stderr --id=$PPID "This is message baby!"
<14>Oct 29 11:26:00 kzak[1550]: This is message baby!
# journald -n 1
Oct 29 11:26:00 ws kzak[1550]: This is message baby!
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 28 ++++++++++++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index d5683b6..e4cb12a 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -51,6 +51,8 @@
#include <netdb.h>
#include <getopt.h>
#include <pwd.h>
+#include <sys/types.h>
+#include <signal.h>
#include "all-io.h"
#include "c.h"
@@ -444,6 +446,12 @@ static void write_output(const struct logger_ctl *ctl, const char *const msg)
if (!ctl->noact) {
struct msghdr msg = { 0 };
+ struct cmsghdr *cmhp;
+ struct ucred *cred;
+ union {
+ struct cmsghdr cmh;
+ char control[CMSG_SPACE(sizeof(struct ucred))];
+ } cbuf;
/* 4) add extra \n to make sure message is terminated */
if ((ctl->socket_type == TYPE_TCP) && !ctl->octet_count)
@@ -452,6 +460,26 @@ static void write_output(const struct logger_ctl *ctl, const char *const msg)
msg.msg_iov = iov;
msg.msg_iovlen = iovlen;
+ /* syslog/journald may follow local socket credentials rather
+ * than in the message PID. If we use --id as root than we can
+ * force kernel to accept another valid PID than the real logger(1)
+ * PID.
+ */
+ if (ctl->pid && !ctl->server && ctl->pid != getpid()
+ && geteuid() == 0 && kill(ctl->pid, 0) == 0) {
+
+ msg.msg_control = cbuf.control;
+ msg.msg_controllen = CMSG_SPACE(sizeof(struct ucred)); //sizeof(cbuf);
+
+ cmhp = CMSG_FIRSTHDR(&msg);
+ cmhp->cmsg_len = CMSG_LEN(sizeof(struct ucred));
+ cmhp->cmsg_level = SOL_SOCKET;
+ cmhp->cmsg_type = SCM_CREDENTIALS;
+ cred = (struct ucred *) CMSG_DATA(cmhp);
+
+ cred->pid = ctl->pid;
+ }
+
if (sendmsg(ctl->fd, &msg, 0) < 0)
warn(_("send message failed"));
}
--
2.9.3
From 9b28da2d1f88a99c37477ff63a23e8b9edbb1026 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Thu, 29 Oct 2015 11:32:50 +0100
Subject: [PATCH 101/115] logger: remove unnecessary comment
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index e4cb12a..eda160a 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -469,7 +469,7 @@ static void write_output(const struct logger_ctl *ctl, const char *const msg)
&& geteuid() == 0 && kill(ctl->pid, 0) == 0) {
msg.msg_control = cbuf.control;
- msg.msg_controllen = CMSG_SPACE(sizeof(struct ucred)); //sizeof(cbuf);
+ msg.msg_controllen = CMSG_SPACE(sizeof(struct ucred));
cmhp = CMSG_FIRSTHDR(&msg);
cmhp->cmsg_len = CMSG_LEN(sizeof(struct ucred));
--
2.9.3
From 2ec100a180e305d972ff7567aadbc3a4be6a3e4b Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sat, 31 Oct 2015 12:27:36 +0000
Subject: [PATCH 102/115] logger: shadow declaration
misc-utils/logger.c:448:17: warning: declaration of 'msg' shadows a
parameter [-Wshadow]
misc-utils/logger.c:429:74: note: shadowed declaration is here
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index eda160a..e439266 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -445,7 +445,7 @@ static void write_output(const struct logger_ctl *ctl, const char *const msg)
iovec_add_string(iov, iovlen, msg, 0);
if (!ctl->noact) {
- struct msghdr msg = { 0 };
+ struct msghdr message = { 0 };
struct cmsghdr *cmhp;
struct ucred *cred;
union {
@@ -457,8 +457,8 @@ static void write_output(const struct logger_ctl *ctl, const char *const msg)
if ((ctl->socket_type == TYPE_TCP) && !ctl->octet_count)
iovec_add_string(iov, iovlen, "\n", 1);
- msg.msg_iov = iov;
- msg.msg_iovlen = iovlen;
+ message.msg_iov = iov;
+ message.msg_iovlen = iovlen;
/* syslog/journald may follow local socket credentials rather
* than in the message PID. If we use --id as root than we can
@@ -468,10 +468,10 @@ static void write_output(const struct logger_ctl *ctl, const char *const msg)
if (ctl->pid && !ctl->server && ctl->pid != getpid()
&& geteuid() == 0 && kill(ctl->pid, 0) == 0) {
- msg.msg_control = cbuf.control;
- msg.msg_controllen = CMSG_SPACE(sizeof(struct ucred));
+ message.msg_control = cbuf.control;
+ message.msg_controllen = CMSG_SPACE(sizeof(struct ucred));
- cmhp = CMSG_FIRSTHDR(&msg);
+ cmhp = CMSG_FIRSTHDR(&message);
cmhp->cmsg_len = CMSG_LEN(sizeof(struct ucred));
cmhp->cmsg_level = SOL_SOCKET;
cmhp->cmsg_type = SCM_CREDENTIALS;
@@ -480,7 +480,7 @@ static void write_output(const struct logger_ctl *ctl, const char *const msg)
cred->pid = ctl->pid;
}
- if (sendmsg(ctl->fd, &msg, 0) < 0)
+ if (sendmsg(ctl->fd, &message, 0) < 0)
warn(_("send message failed"));
}
--
2.9.3
From b9710f1f0850100e59503729f6e12eaa0ded7314 Mon Sep 17 00:00:00 2001
From: Ruediger Meier <ruediger.meier@ga-group.nl>
Date: Tue, 23 Feb 2016 00:54:41 +0100
Subject: [PATCH 103/115] misc: fix compiler warnungs (unsigned/signed)
These ones should be fixed:
libblkid/src/probe.c:393:39: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libblkid/src/probe.c:907:25: warning: signed and unsigned type in conditional expression [-Wsign-compare]
libblkid/src/probe.c:1221:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libblkid/src/partitions/partitions.c:540:47: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libblkid/src/partitions/partitions.c:1043:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libblkid/src/partitions/partitions.c:1056:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libblkid/src/partitions/partitions.c:1057:37: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libblkid/src/partitions/partitions.c:1061:38: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libblkid/src/partitions/partitions.c:1199:27: warning: signed and unsigned type in conditional expression [-Wsign-compare]
libblkid/src/partitions/partitions.c:1410:26: warning: signed and unsigned type in conditional expression [-Wsign-compare]
libblkid/src/partitions/partitions.c:1431:25: warning: signed and unsigned type in conditional expression [-Wsign-compare]
libblkid/src/superblocks/linux_raid.c:151:8: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
libblkid/src/superblocks/linux_raid.c:155:2: warning: comparison of unsigned expression < 0 is always false [-Wtype-limits]
libblkid/src/superblocks/superblocks.c:375:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libblkid/src/superblocks/xfs.c:141:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libsmartcols/src/table.c:333:24: warning: signed and unsigned type in conditional expression [-Wsign-compare]
libsmartcols/src/table.c:344:25: warning: signed and unsigned type in conditional expression [-Wsign-compare]
libsmartcols/src/table_print.c:753:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libfdisk/src/ask.c:364:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libfdisk/src/utils.c:33:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libfdisk/src/context.c:435:56: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libfdisk/src/context.c:730:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libfdisk/src/script.c:557:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libfdisk/src/dos.c:1791:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
libfdisk/src/gpt.c:813:42: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
misc-utils/logger.c:408:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
misc-utils/logger.c:408:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
misc-utils/logger.c:408:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
misc-utils/logger.c:408:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
misc-utils/logger.c:408:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
disk-utils/partx.c:140:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
disk-utils/partx.c:551:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
disk-utils/partx.c:640:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
---
misc-utils/logger.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index e439266..e6189d4 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -405,7 +405,7 @@ static const char *rfc3164_current_time(void)
}
#define next_iovec(ary, idx) __extension__ ({ \
- assert(ARRAY_SIZE(ary) > idx); \
+ assert(ARRAY_SIZE(ary) > (size_t)idx); \
assert(idx >= 0); \
&ary[idx++]; \
})
--
2.9.3
From 7db029e54a4c0fdab43e8f645e4f91314747887c Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Wed, 24 Feb 2016 10:40:08 +0100
Subject: [PATCH 104/115] logger: always update header when writing stdin line
Addresses: http://bugs.debian.org/798239
Reported-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index e439266..1e893a9 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -905,6 +905,11 @@ static void logger_command_line(const struct logger_ctl *ctl, char **argv)
static void logger_stdin(struct logger_ctl *ctl)
{
+ /* note: we re-generate the the syslog header for each log message to
+ * update header timestamps and to reflect possible priority changes.
+ * The initial header is generated by logger_open().
+ */
+ int has_header = 1;
int default_priority = ctl->pri;
int last_pri = default_priority;
size_t max_usrmsg_size = ctl->max_message_size - strlen(ctl->hdr);
@@ -935,7 +940,7 @@ static void logger_stdin(struct logger_ctl *ctl)
ctl->pri = default_priority;
if (ctl->pri != last_pri) {
- generate_syslog_header(ctl);
+ has_header = 0;
max_usrmsg_size = ctl->max_message_size - strlen(ctl->hdr);
last_pri = ctl->pri;
}
@@ -950,8 +955,12 @@ static void logger_stdin(struct logger_ctl *ctl)
}
buf[i] = '\0';
- if (i > 0 || !ctl->skip_empty_lines)
+ if (i > 0 || !ctl->skip_empty_lines) {
+ if (!has_header)
+ generate_syslog_header(ctl);
write_output(ctl, buf);
+ has_header = 0;
+ }
if (c == '\n') /* discard line terminator */
c = getchar();
--
2.9.3
From 4199e9891eb4b024792b18b21f576331d370fdc1 Mon Sep 17 00:00:00 2001
From: Ruediger Meier <ruediger.meier@ga-group.nl>
Date: Fri, 19 Feb 2016 01:44:14 +0100
Subject: [PATCH 105/115] logger: use SCM_CREDENTIALS on LINUX only
This is a build fix of FreeBSD and OSX. Basically we revert
the following commit for non-linux systems:
27a9eb53 "use --id as local socket credentials"
Note I could also compile it like this:
#ifdef HAVE_SYS_UCRED_H
# define _WANT_UCRED
# include <sys/param.h>
# include <sys/ucred.h>
# define SCM_CREDENTIALS SCM_CREDS
#endif
[...]
#ifdef _linux_
cred->pid = ctl->pid;
#endif
[...]
... but don't know how to test whether it does what it
should.
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
---
misc-utils/logger.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 4201e43..d9aee0e 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -446,12 +446,14 @@ static void write_output(const struct logger_ctl *ctl, const char *const msg)
if (!ctl->noact) {
struct msghdr message = { 0 };
+#ifdef SCM_CREDENTIALS
struct cmsghdr *cmhp;
struct ucred *cred;
union {
struct cmsghdr cmh;
char control[CMSG_SPACE(sizeof(struct ucred))];
} cbuf;
+#endif
/* 4) add extra \n to make sure message is terminated */
if ((ctl->socket_type == TYPE_TCP) && !ctl->octet_count)
@@ -460,6 +462,7 @@ static void write_output(const struct logger_ctl *ctl, const char *const msg)
message.msg_iov = iov;
message.msg_iovlen = iovlen;
+#ifdef SCM_CREDENTIALS
/* syslog/journald may follow local socket credentials rather
* than in the message PID. If we use --id as root than we can
* force kernel to accept another valid PID than the real logger(1)
@@ -479,6 +482,7 @@ static void write_output(const struct logger_ctl *ctl, const char *const msg)
cred->pid = ctl->pid;
}
+#endif
if (sendmsg(ctl->fd, &message, 0) < 0)
warn(_("send message failed"));
--
2.9.3
From eaac9f88f9020701496f704f940cb96c97bc4e6a Mon Sep 17 00:00:00 2001
From: Ruediger Meier <ruediger.meier@ga-group.nl>
Date: Mon, 22 Feb 2016 17:25:21 +0100
Subject: [PATCH 106/115] misc: fix some includes
features.h: any glibc header includes this already
libgen.h: was unused there
sys/uio.h: for writev(3p)
sys/queue.h seems like it was never used
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
---
misc-utils/logger.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index d9aee0e..896d1b8 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -53,6 +53,7 @@
#include <pwd.h>
#include <sys/types.h>
#include <signal.h>
+#include <sys/uio.h>
#include "all-io.h"
#include "c.h"
--
2.9.3
From f5fceb401e2f6edb39dc07fed8eb3c52ef062001 Mon Sep 17 00:00:00 2001
From: Ruediger Meier <ruediger.meier@ga-group.nl>
Date: Tue, 8 Mar 2016 11:23:14 +0100
Subject: [PATCH 107/115] logger: fix compiler warning, const facilitynames
This was the warning on FreeBSD:
misc-utils/logger.c:221:24: warning: passing 'const CODE [25]' to parameter of type 'CODE *' (aka 'struct _code *') discards qualifiers
[-Wincompatible-pointer-types-discards-qualifiers]
facility = decode(s, facilitynames);
^~~~~~~~~~~~~
misc-utils/logger.c:187:43: note: passing argument to parameter 'codetab' here
static int decode(const char *name, CODE *codetab)
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
---
misc-utils/logger.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 896d1b8..6662a27 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -184,9 +184,9 @@ static inline pid_t logger_getpid(void)
#endif
-static int decode(const char *name, CODE *codetab)
+static int decode(const char *name, const CODE *codetab)
{
- register CODE *c;
+ register const CODE *c;
if (name == NULL || *name == '\0')
return -1;
--
2.9.3
From 8e2e9144ef8b7bbe9d7a97244567bad608784ef2 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Thu, 10 Mar 2016 14:30:32 +0100
Subject: [PATCH 108/115] misc: remove duplicate includes
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 1 -
1 file changed, 1 deletion(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 6662a27..7b88b17 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -51,7 +51,6 @@
#include <netdb.h>
#include <getopt.h>
#include <pwd.h>
-#include <sys/types.h>
#include <signal.h>
#include <sys/uio.h>
--
2.9.3
From 4e5411f623216c7ca700d5d63b5c2ec62ccc22ba Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sun, 13 Mar 2016 10:31:40 +0000
Subject: [PATCH 109/115] logger: fix memory leak [ASAN and valgrind]
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 7b88b17..1ef12cd 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -341,8 +341,10 @@ static int journald_entry(struct logger_ctl *ctl, FILE *fp)
for (lines = 0; /* nothing */ ; lines++) {
buf = NULL;
sz = getline(&buf, &dummy, fp);
- if (sz == -1)
+ if (sz == -1) {
+ free(buf);
break;
+ }
if (0 < sz && buf[sz - 1] == '\n') {
sz--;
buf[sz] = '\0';
--
2.9.3
From 426cdc0ac0f210b7e59f339f065d2dfe0935a223 Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@justemail.net>
Date: Wed, 16 Mar 2016 09:55:52 +0100
Subject: [PATCH 110/115] logger: correct the grammar of an error message
Signed-off-by: Benno Schulenberg <bensberg@justemail.net>
---
misc-utils/logger.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 1ef12cd..b7b08eb 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -572,7 +572,7 @@ static void add_structured_data_param(struct list_head *ls, const char *param)
struct structured_data *sd;
if (list_empty(ls))
- errx(EXIT_FAILURE, _("--sd-id no specified for --sd-param %s"), param);
+ errx(EXIT_FAILURE, _("--sd-id was not specified for --sd-param %s"), param);
assert(param);
--
2.9.3
From 9e93004171eb0c4c288051b2d7bb37f97a0ae430 Mon Sep 17 00:00:00 2001
From: Sebastian Rasmussen <sebras@gmail.com>
Date: Sun, 29 May 2016 23:11:53 +0200
Subject: [PATCH 111/115] misc: Fix various typos
Fix various typos in error messages, warnings, debug strings,
comments and names of static functions.
Signed-off-by: Sebastian Rasmussen <sebras@gmail.com>
---
misc-utils/logger.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index b7b08eb..b01538b 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -130,7 +130,7 @@ struct logger_ctl {
unsigned int
unix_socket_errors:1, /* whether to report or not errors */
noact:1, /* do not write to sockets */
- prio_prefix:1, /* read priority from intput */
+ prio_prefix:1, /* read priority from input */
stderr_printout:1, /* output message to stderr */
rfc5424_time:1, /* include time stamp */
rfc5424_tq:1, /* include time quality markup */
@@ -768,7 +768,7 @@ static void syslog_rfc5424_header(struct logger_ctl *const ctl)
sd = get_reserved_structured_data(ctl);
- /* time quality structured data (maybe overwriten by --sd-id timeQuality) */
+ /* time quality structured data (maybe overwritten by --sd-id timeQuality) */
if (ctl->rfc5424_tq && !has_structured_data_id(sd, "timeQuality")) {
add_structured_data_id(sd, "timeQuality");
--
2.9.3
From 1c7227598824b1d9140298e9fe5742cae4131130 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Tue, 21 Jun 2016 13:32:14 +0200
Subject: [PATCH 112/115] logger: be more precise about --port description
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index b01538b..9f2940c 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -1001,7 +1001,7 @@ static void __attribute__ ((__noreturn__)) usage(FILE *out)
fputs(_(" -S, --size <size> maximum size for a single message\n"), out);
fputs(_(" -t, --tag <tag> mark every line with this tag\n"), out);
fputs(_(" -n, --server <name> write to this remote syslog server\n"), out);
- fputs(_(" -P, --port <number> use this UDP port\n"), out);
+ fputs(_(" -P, --port <port> use this port for UDP or TCP connection\n"), out);
fputs(_(" -T, --tcp use TCP only\n"), out);
fputs(_(" -d, --udp use UDP only\n"), out);
fputs(_(" --rfc3164 use the obsolete BSD syslog protocol\n"), out);
--
2.9.3
From f1f5f21ee664c232df1454a322f2570301ee1add Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Mon, 18 Jul 2016 21:49:27 +0100
Subject: [PATCH 113/115] logger: remove trailing spaces when outputing to
journal
Issues:
1. Whitespace-ish \r is not stripped, while it should be.
2. In journal \r is considered unprintable.
Lennart: "it is the duty of the client side to drop the trailing whitespace,
which "logger" doesn't do".
Reported-by: Ivan Babrou <ibobrik@gmail.com>
Explained-by: Lennart Poettering <lennart@poettering.net>
Reference: https://github.com/systemd/systemd/issues/3416
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 9f2940c..70fac68 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -341,14 +341,11 @@ static int journald_entry(struct logger_ctl *ctl, FILE *fp)
for (lines = 0; /* nothing */ ; lines++) {
buf = NULL;
sz = getline(&buf, &dummy, fp);
- if (sz == -1) {
+ if (sz == -1 ||
+ (sz = rtrim_whitespace((unsigned char *) buf)) == 0) {
free(buf);
break;
}
- if (0 < sz && buf[sz - 1] == '\n') {
- sz--;
- buf[sz] = '\0';
- }
if (lines == vectors) {
vectors *= 2;
if (IOV_MAX < vectors)
--
2.9.3
From da0788fb9782f5c13c345f462c91c1f8640c90c3 Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Mon, 4 Jul 2016 22:14:41 +0100
Subject: [PATCH 114/115] logger: simplify if clause [oclint]
This has effect of collapsing rather long indentation block, so commit
separately.
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 46 +++++++++++++++++++++++-----------------------
1 file changed, 23 insertions(+), 23 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index 70fac68..dbb9cdc 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -924,32 +924,32 @@ static void logger_stdin(struct logger_ctl *ctl)
c = getchar();
while (c != EOF) {
i = 0;
- if (ctl->prio_prefix) {
- if (c == '<') {
- pri = 0;
+ if (ctl->prio_prefix && c == '<') {
+ pri = 0;
+ buf[i++] = c;
+ while (isdigit(c = getchar()) && pri <= 191) {
buf[i++] = c;
- while (isdigit(c = getchar()) && pri <= 191) {
- buf[i++] = c;
- pri = pri * 10 + c - '0';
- }
- if (c != EOF && c != '\n')
- buf[i++] = c;
- if (c == '>' && 0 <= pri && pri <= 191) { /* valid RFC PRI values */
- i = 0;
- if (pri < 8)
- pri |= 8; /* kern facility is forbidden */
- ctl->pri = pri;
- } else
- ctl->pri = default_priority;
+ pri = pri * 10 + c - '0';
+ }
+ if (c != EOF && c != '\n')
+ buf[i++] = c;
+ if (c == '>' && 0 <= pri && pri <= 191) {
+ /* valid RFC PRI values */
+ i = 0;
+ if (pri < 8) /* kern facility is forbidden */
+ pri |= 8;
+ ctl->pri = pri;
+ } else
+ ctl->pri = default_priority;
- if (ctl->pri != last_pri) {
- has_header = 0;
- max_usrmsg_size = ctl->max_message_size - strlen(ctl->hdr);
- last_pri = ctl->pri;
- }
- if (c != EOF && c != '\n')
- c = getchar();
+ if (ctl->pri != last_pri) {
+ has_header = 0;
+ max_usrmsg_size =
+ ctl->max_message_size - strlen(ctl->hdr);
+ last_pri = ctl->pri;
}
+ if (c != EOF && c != '\n')
+ c = getchar();
}
while (c != EOF && c != '\n' && i < max_usrmsg_size) {
--
2.9.3
From eb2306e675d9ba5e348938e473f5a6f96400980f Mon Sep 17 00:00:00 2001
From: Sami Kerola <kerolasa@iki.fi>
Date: Sun, 3 Jul 2016 13:20:30 +0100
Subject: [PATCH 115/115] misc: fix declarations shadowing variables in the
global scope [oclint]
Fixes multiple occurences of 'optarg' overwrites.
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/logger.c | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index dbb9cdc..4fb650a 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -804,11 +804,11 @@ static void syslog_rfc5424_header(struct logger_ctl *const ctl)
free(structured);
}
-static void parse_rfc5424_flags(struct logger_ctl *ctl, char *optarg)
+static void parse_rfc5424_flags(struct logger_ctl *ctl, char *s)
{
char *in, *tok;
- in = optarg;
+ in = s;
while ((tok = strtok(in, ","))) {
in = NULL;
if (!strcmp(tok, "notime")) {
@@ -823,15 +823,15 @@ static void parse_rfc5424_flags(struct logger_ctl *ctl, char *optarg)
}
}
-static int parse_unix_socket_errors_flags(char *optarg)
+static int parse_unix_socket_errors_flags(char *s)
{
- if (!strcmp(optarg, "off"))
+ if (!strcmp(s, "off"))
return AF_UNIX_ERRORS_OFF;
- if (!strcmp(optarg, "on"))
+ if (!strcmp(s, "on"))
return AF_UNIX_ERRORS_ON;
- if (!strcmp(optarg, "auto"))
+ if (!strcmp(s, "auto"))
return AF_UNIX_ERRORS_AUTO;
- warnx(_("invalid argument: %s: using automatic errors"), optarg);
+ warnx(_("invalid argument: %s: using automatic errors"), s);
return AF_UNIX_ERRORS_AUTO;
}
--
2.9.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment