1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-02 08:42:48 +00:00

Generate fully RFC3164 compliant messages, with timestamp and hostname.

Allow to set hostname to any string with -H.

MFC after:	2 months
This commit is contained in:
Gleb Smirnoff 2017-12-05 19:55:53 +00:00
parent 50387adce2
commit 65547fb33d
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=326574
2 changed files with 42 additions and 13 deletions

View File

@ -28,7 +28,7 @@
.\" @(#)logger.1 8.1 (Berkeley) 6/6/93 .\" @(#)logger.1 8.1 (Berkeley) 6/6/93
.\" $FreeBSD$ .\" $FreeBSD$
.\" .\"
.Dd December 23, 2016 .Dd December 5, 2017
.Dt LOGGER 1 .Dt LOGGER 1
.Os .Os
.Sh NAME .Sh NAME
@ -38,6 +38,7 @@
.Nm .Nm
.Op Fl 46Ais .Op Fl 46Ais
.Op Fl f Ar file .Op Fl f Ar file
.Op Fl H Ar hostname
.Op Fl h Ar host .Op Fl h Ar host
.Op Fl P Ar port .Op Fl P Ar port
.Op Fl p Ar pri .Op Fl p Ar pri
@ -77,6 +78,11 @@ Log the message to standard error, as well as the system log.
.It Fl f Ar file .It Fl f Ar file
Read the contents of the specified file into syslog. Read the contents of the specified file into syslog.
This option is ignored when a message is also specified. This option is ignored when a message is also specified.
.It Fl H Ar hostname
Set the hostname in the header of the message to specified value.
If not specified, host part of
.Xr gethostname 3
will be used.
.It Fl h Ar host .It Fl h Ar host
Send the message to the remote system Send the message to the remote system
.Ar host .Ar host

View File

@ -44,7 +44,7 @@ static char sccsid[] = "@(#)logger.c 8.1 (Berkeley) 6/6/93";
#include <sys/cdefs.h> #include <sys/cdefs.h>
__FBSDID("$FreeBSD$"); __FBSDID("$FreeBSD$");
#include <sys/types.h> #include <sys/param.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
@ -54,6 +54,7 @@ __FBSDID("$FreeBSD$");
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <time.h>
#include <unistd.h> #include <unistd.h>
#define SYSLOG_NAMES #define SYSLOG_NAMES
@ -71,8 +72,8 @@ static int decode(char *, const CODE *);
static int pencode(char *); static int pencode(char *);
static ssize_t socksetup(const char *, const char *, const char *, static ssize_t socksetup(const char *, const char *, const char *,
struct socks **); struct socks **);
static void logmessage(int, const char *, struct socks *, ssize_t, static void logmessage(int, const char *, const char *, const char *,
const char *); struct socks *, ssize_t, const char *);
static void usage(void); static void usage(void);
#ifdef INET6 #ifdef INET6
@ -93,19 +94,22 @@ main(int argc, char *argv[])
{ {
struct socks *socks; struct socks *socks;
ssize_t nsock; ssize_t nsock;
time_t now;
int ch, logflags, pri; int ch, logflags, pri;
char *tag, *host, buf[1024]; char *tag, *host, buf[1024], *timestamp, tbuf[26],
*hostname, hbuf[MAXHOSTNAMELEN];
const char *svcname, *src; const char *svcname, *src;
tag = NULL; tag = NULL;
host = NULL; host = NULL;
hostname = NULL;
svcname = "syslog"; svcname = "syslog";
src = NULL; src = NULL;
socks = NULL; socks = NULL;
pri = LOG_USER | LOG_NOTICE; pri = LOG_USER | LOG_NOTICE;
logflags = 0; logflags = 0;
unsetenv("TZ"); unsetenv("TZ");
while ((ch = getopt(argc, argv, "46Af:h:iP:p:S:st:")) != -1) while ((ch = getopt(argc, argv, "46Af:H:h:iP:p:S:st:")) != -1)
switch((char)ch) { switch((char)ch) {
case '4': case '4':
family = PF_INET; family = PF_INET;
@ -123,6 +127,9 @@ main(int argc, char *argv[])
err(1, "%s", optarg); err(1, "%s", optarg);
setvbuf(stdin, 0, _IONBF, 0); setvbuf(stdin, 0, _IONBF, 0);
break; break;
case 'H': /* hostname to set in message header */
hostname = optarg;
break;
case 'h': /* hostname to deliver to */ case 'h': /* hostname to deliver to */
host = optarg; host = optarg;
break; break;
@ -168,6 +175,17 @@ main(int argc, char *argv[])
openlog(tag, logflags, 0); openlog(tag, logflags, 0);
(void) fclose(stdout); (void) fclose(stdout);
(void )time(&now);
(void )ctime_r(&now, tbuf);
tbuf[19] = '\0';
timestamp = tbuf + 4;
if (hostname == NULL) {
hostname = hbuf;
(void )gethostname(hbuf, MAXHOSTNAMELEN);
*strchr(hostname, '.') = '\0';
}
/* log input line if appropriate */ /* log input line if appropriate */
if (argc > 0) { if (argc > 0) {
char *p, *endp; char *p, *endp;
@ -176,11 +194,13 @@ main(int argc, char *argv[])
for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) { for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
len = strlen(*argv); len = strlen(*argv);
if (p + len > endp && p > buf) { if (p + len > endp && p > buf) {
logmessage(pri, tag, socks, nsock, buf); logmessage(pri, timestamp, hostname, tag,
socks, nsock, buf);
p = buf; p = buf;
} }
if (len > sizeof(buf) - 1) if (len > sizeof(buf) - 1)
logmessage(pri, tag, socks, nsock, *argv++); logmessage(pri, timestamp, hostname, tag,
socks, nsock, *argv++);
else { else {
if (p != buf) if (p != buf)
*p++ = ' '; *p++ = ' ';
@ -189,10 +209,12 @@ main(int argc, char *argv[])
} }
} }
if (p != buf) if (p != buf)
logmessage(pri, tag, socks, nsock, buf); logmessage(pri, timestamp, hostname, tag, socks, nsock,
buf);
} else } else
while (fgets(buf, sizeof(buf), stdin) != NULL) while (fgets(buf, sizeof(buf), stdin) != NULL)
logmessage(pri, tag, socks, nsock, buf); logmessage(pri, timestamp, hostname, tag, socks, nsock,
buf);
exit(0); exit(0);
} }
@ -320,8 +342,8 @@ socksetup(const char *src, const char *dst, const char *svcname,
* Send the message to syslog, either on the local host, or on a remote host * Send the message to syslog, either on the local host, or on a remote host
*/ */
static void static void
logmessage(int pri, const char *tag, struct socks *sk, ssize_t nsock, logmessage(int pri, const char *timestamp, const char *hostname,
const char *buf) const char *tag, struct socks *sk, ssize_t nsock, const char *buf)
{ {
char *line; char *line;
int len, i, lsent; int len, i, lsent;
@ -330,7 +352,8 @@ logmessage(int pri, const char *tag, struct socks *sk, ssize_t nsock,
syslog(pri, "%s", buf); syslog(pri, "%s", buf);
return; return;
} }
if ((len = asprintf(&line, "<%d>%s: %s", pri, tag, buf)) == -1) if ((len = asprintf(&line, "<%d>%s %s %s: %s", pri, timestamp,
hostname, tag, buf)) == -1)
errx(1, "asprintf"); errx(1, "asprintf");
lsent = -1; lsent = -1;