aboutsummaryrefslogtreecommitdiff
path: root/usr.bin/logger
diff options
context:
space:
mode:
authorGleb Smirnoff <glebius@FreeBSD.org>2017-12-05 19:55:53 +0000
committerGleb Smirnoff <glebius@FreeBSD.org>2017-12-05 19:55:53 +0000
commit65547fb33db901a9f352aacb0ed45ce68b0bd275 (patch)
tree9a604b840873b58f117ec564dcc06fa3fe432e9d /usr.bin/logger
parent50387adce2243bd828b0ee56d0f026c41655149b (diff)
downloadsrc-65547fb33db901a9f352aacb0ed45ce68b0bd275.tar.gz
src-65547fb33db901a9f352aacb0ed45ce68b0bd275.zip
Notes
Diffstat (limited to 'usr.bin/logger')
-rw-r--r--usr.bin/logger/logger.18
-rw-r--r--usr.bin/logger/logger.c47
2 files changed, 42 insertions, 13 deletions
diff --git a/usr.bin/logger/logger.1 b/usr.bin/logger/logger.1
index e488f86232d4..7a64320cfe49 100644
--- a/usr.bin/logger/logger.1
+++ b/usr.bin/logger/logger.1
@@ -28,7 +28,7 @@
.\" @(#)logger.1 8.1 (Berkeley) 6/6/93
.\" $FreeBSD$
.\"
-.Dd December 23, 2016
+.Dd December 5, 2017
.Dt LOGGER 1
.Os
.Sh NAME
@@ -38,6 +38,7 @@
.Nm
.Op Fl 46Ais
.Op Fl f Ar file
+.Op Fl H Ar hostname
.Op Fl h Ar host
.Op Fl P Ar port
.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
Read the contents of the specified file into syslog.
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
Send the message to the remote system
.Ar host
diff --git a/usr.bin/logger/logger.c b/usr.bin/logger/logger.c
index 0c29dd98dbe7..f88ad1ee8cf3 100644
--- a/usr.bin/logger/logger.c
+++ b/usr.bin/logger/logger.c
@@ -44,7 +44,7 @@ static char sccsid[] = "@(#)logger.c 8.1 (Berkeley) 6/6/93";
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
-#include <sys/types.h>
+#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
@@ -54,6 +54,7 @@ __FBSDID("$FreeBSD$");
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <time.h>
#include <unistd.h>
#define SYSLOG_NAMES
@@ -71,8 +72,8 @@ static int decode(char *, const CODE *);
static int pencode(char *);
static ssize_t socksetup(const char *, const char *, const char *,
struct socks **);
-static void logmessage(int, const char *, struct socks *, ssize_t,
- const char *);
+static void logmessage(int, const char *, const char *, const char *,
+ struct socks *, ssize_t, const char *);
static void usage(void);
#ifdef INET6
@@ -93,19 +94,22 @@ main(int argc, char *argv[])
{
struct socks *socks;
ssize_t nsock;
+ time_t now;
int ch, logflags, pri;
- char *tag, *host, buf[1024];
+ char *tag, *host, buf[1024], *timestamp, tbuf[26],
+ *hostname, hbuf[MAXHOSTNAMELEN];
const char *svcname, *src;
tag = NULL;
host = NULL;
+ hostname = NULL;
svcname = "syslog";
src = NULL;
socks = NULL;
pri = LOG_USER | LOG_NOTICE;
logflags = 0;
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) {
case '4':
family = PF_INET;
@@ -123,6 +127,9 @@ main(int argc, char *argv[])
err(1, "%s", optarg);
setvbuf(stdin, 0, _IONBF, 0);
break;
+ case 'H': /* hostname to set in message header */
+ hostname = optarg;
+ break;
case 'h': /* hostname to deliver to */
host = optarg;
break;
@@ -168,6 +175,17 @@ main(int argc, char *argv[])
openlog(tag, logflags, 0);
(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 */
if (argc > 0) {
char *p, *endp;
@@ -176,11 +194,13 @@ main(int argc, char *argv[])
for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
len = strlen(*argv);
if (p + len > endp && p > buf) {
- logmessage(pri, tag, socks, nsock, buf);
+ logmessage(pri, timestamp, hostname, tag,
+ socks, nsock, buf);
p = buf;
}
if (len > sizeof(buf) - 1)
- logmessage(pri, tag, socks, nsock, *argv++);
+ logmessage(pri, timestamp, hostname, tag,
+ socks, nsock, *argv++);
else {
if (p != buf)
*p++ = ' ';
@@ -189,10 +209,12 @@ main(int argc, char *argv[])
}
}
if (p != buf)
- logmessage(pri, tag, socks, nsock, buf);
+ logmessage(pri, timestamp, hostname, tag, socks, nsock,
+ buf);
} else
while (fgets(buf, sizeof(buf), stdin) != NULL)
- logmessage(pri, tag, socks, nsock, buf);
+ logmessage(pri, timestamp, hostname, tag, socks, nsock,
+ buf);
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
*/
static void
-logmessage(int pri, const char *tag, struct socks *sk, ssize_t nsock,
- const char *buf)
+logmessage(int pri, const char *timestamp, const char *hostname,
+ const char *tag, struct socks *sk, ssize_t nsock, const char *buf)
{
char *line;
int len, i, lsent;
@@ -330,7 +352,8 @@ logmessage(int pri, const char *tag, struct socks *sk, ssize_t nsock,
syslog(pri, "%s", buf);
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");
lsent = -1;