Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F95971377
D15011.id41267.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
16 KB
Referenced Files
None
Subscribers
None
D15011.id41267.diff
View Options
Index: usr.bin/wall/ttymsg.h
===================================================================
--- usr.bin/wall/ttymsg.h
+++ usr.bin/wall/ttymsg.h
@@ -1,3 +1,5 @@
/* $FreeBSD$ */
+#define TTYMSG_IOV_MAX 32
+
const char *ttymsg(struct iovec *, int, const char *, int);
Index: usr.bin/wall/ttymsg.c
===================================================================
--- usr.bin/wall/ttymsg.c
+++ usr.bin/wall/ttymsg.c
@@ -61,7 +61,7 @@
const char *
ttymsg(struct iovec *iov, int iovcnt, const char *line, int tmout)
{
- struct iovec localiov[7];
+ struct iovec localiov[TTYMSG_IOV_MAX];
ssize_t left, wret;
int cnt, fd;
char device[MAXNAMLEN] = _PATH_DEV;
Index: usr.sbin/syslogd/syslogd.8
===================================================================
--- usr.sbin/syslogd/syslogd.8
+++ usr.sbin/syslogd/syslogd.8
@@ -28,7 +28,7 @@
.\" @(#)syslogd.8 8.1 (Berkeley) 6/6/93
.\" $FreeBSD$
.\"
-.Dd November 28, 2017
+.Dd April 8, 2018
.Dt SYSLOGD 8
.Os
.Sh NAME
@@ -36,7 +36,7 @@
.Nd log systems messages
.Sh SYNOPSIS
.Nm
-.Op Fl 468ACcdFHkNnosTuv
+.Op Fl 4568ACcdFHkNnosTuv
.Op Fl a Ar allowed_peer
.Op Fl b Ar bind_address
.Op Fl f Ar config_file
@@ -59,6 +59,8 @@
Force
.Nm
to use IPv4 addresses only.
+.It Fl 5
+Generate log messages using the RFC 5424 message format.
.It Fl 6
Force
.Nm
Index: usr.sbin/syslogd/syslogd.c
===================================================================
--- usr.sbin/syslogd/syslogd.c
+++ usr.sbin/syslogd/syslogd.c
@@ -136,6 +136,7 @@
#include <netdb.h>
#include <paths.h>
#include <signal.h>
+#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -367,9 +368,12 @@
static int needdofsync = 0; /* Are any file(s) waiting to be fsynced? */
static struct pidfh *pfh;
static int sigpipe[2]; /* Pipe to catch a signal during select(). */
+static bool BSDOutputFormat = true; /* Use RFC 3164 to format messages. */
static volatile sig_atomic_t MarkSet, WantDie, WantInitialize, WantReapchild;
+struct iovlist;
+
static int allowaddr(char *);
static int addfile(struct filed *);
static int addpeer(struct peer *);
@@ -386,6 +390,7 @@
static void domark(int);
static void fprintlog_first(struct filed *, const char *, const char *,
const char *, const char *, const char *, const char *, int);
+static void fprintlog_write(struct filed *, struct iovlist *, int);
static void fprintlog_successive(struct filed *, int);
static void init(int);
static void logerror(const char *);
@@ -498,7 +503,7 @@
if (madvise(NULL, 0, MADV_PROTECT) != 0)
dprintf("madvise() failed: %s\n", strerror(errno));
- while ((ch = getopt(argc, argv, "468Aa:b:cCdf:FHkl:m:nNop:P:sS:Tuv"))
+ while ((ch = getopt(argc, argv, "4568Aa:b:cCdf:FHkl:m:nNop:P:sS:Tuv"))
!= -1)
switch (ch) {
#ifdef INET
@@ -506,6 +511,9 @@
family = PF_INET;
break;
#endif
+ case '5':
+ BSDOutputFormat = false;
+ break;
#ifdef INET6
case '6':
family = PF_INET6;
@@ -881,7 +889,7 @@
{
fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
- "usage: syslogd [-468ACcdFHknosTuv] [-a allowed_peer]",
+ "usage: syslogd [-4568ACcdFHknosTuv] [-a allowed_peer]",
" [-b bind_address] [-f config_file]",
" [-l [mode:]path] [-m mark_interval]",
" [-P pid_file] [-p log_socket]",
@@ -1558,177 +1566,112 @@
}
}
-#define IOV_SIZE 7
+/*
+ * List of iovecs to which entries can be appended.
+ * Used for constructing the message to be logged.
+ */
+struct iovlist {
+ struct iovec iov[TTYMSG_IOV_MAX];
+ size_t iovcnt;
+ size_t totalsize;
+};
+
static void
-fprintlog_first(struct filed *f, const char *hostname, const char *app_name,
- const char *procid, const char *msgid __unused,
- const char *structured_data __unused, const char *msg, int flags)
+iovlist_init(struct iovlist *il)
{
- struct iovec iov[IOV_SIZE];
- struct addrinfo *r;
- int l, lsent = 0;
- char tagged_msg[MAXLINE + 1], line[MAXLINE + 1], greetings[200];
- char nul[] = "", space[] = " ", lf[] = "\n", crlf[] = "\r\n";
- char timebuf[RFC3164_DATELEN + 1];
- const char *msgret;
- if (strftime(timebuf, sizeof(timebuf), RFC3164_DATEFMT,
- &f->f_lasttime.tm) == 0)
- timebuf[0] = '\0';
- if (f->f_type == F_WALL) {
- /* The time displayed is not synchornized with the other log
- * destinations (like messages). Following fragment was using
- * ctime(&now), which was updating the time every 30 sec.
- * With f_lasttime, time is synchronized correctly.
- */
- iov[0] = (struct iovec){
- .iov_base = greetings,
- .iov_len = snprintf(greetings, sizeof(greetings),
- "\r\n\7Message from syslogd@%s "
- "at %.24s ...\r\n", hostname, timebuf)
- };
- if (iov[0].iov_len >= sizeof(greetings))
- iov[0].iov_len = sizeof(greetings) - 1;
- iov[1] = (struct iovec){
- .iov_base = nul,
- .iov_len = 0
- };
- } else {
- iov[0] = (struct iovec){
- .iov_base = timebuf,
- .iov_len = strlen(timebuf)
- };
- iov[1] = (struct iovec){
- .iov_base = space,
- .iov_len = 1
- };
- }
+ il->iovcnt = 0;
+ il->totalsize = 0;
+}
- if (LogFacPri) {
- static char fp_buf[30]; /* Hollow laugh */
- int fac = f->f_prevpri & LOG_FACMASK;
- int pri = LOG_PRI(f->f_prevpri);
- const char *f_s = NULL;
- char f_n[5]; /* Hollow laugh */
- const char *p_s = NULL;
- char p_n[5]; /* Hollow laugh */
-
- if (LogFacPri > 1) {
- const CODE *c;
-
- for (c = facilitynames; c->c_name; c++) {
- if (c->c_val == fac) {
- f_s = c->c_name;
- break;
- }
- }
- for (c = prioritynames; c->c_name; c++) {
- if (c->c_val == pri) {
- p_s = c->c_name;
- break;
- }
- }
- }
- if (!f_s) {
- snprintf(f_n, sizeof f_n, "%d", LOG_FAC(fac));
- f_s = f_n;
- }
- if (!p_s) {
- snprintf(p_n, sizeof p_n, "%d", pri);
- p_s = p_n;
- }
- snprintf(fp_buf, sizeof fp_buf, "<%s.%s> ", f_s, p_s);
- iov[2] = (struct iovec){
- .iov_base = fp_buf,
- .iov_len = strlen(fp_buf)
- };
- } else {
- iov[2] = (struct iovec){
- .iov_base = nul,
- .iov_len = 0
- };
- }
- /* Prepend the application name to the message if provided. */
- if (app_name != NULL) {
- if (procid != NULL)
- snprintf(tagged_msg, sizeof(tagged_msg),
- "%s[%s]: %s", app_name, procid, msg);
- else
- snprintf(tagged_msg, sizeof(tagged_msg),
- "%s: %s", app_name, msg);
- msg = tagged_msg;
- }
- iov[3] = (struct iovec){
- .iov_base = __DECONST(char *, hostname),
- .iov_len = strlen(hostname)
- };
- iov[4] = (struct iovec){
- .iov_base = space,
- .iov_len = 1
- };
- iov[5] = (struct iovec){
- .iov_base = __DECONST(char *, msg),
- .iov_len = strlen(msg)
+static void
+iovlist_append(struct iovlist *il, const char *str)
+{
+ size_t size;
+
+ assert(il->iovcnt < nitems(il->iov));
+ size = strlen(str);
+ il->iov[il->iovcnt++] = (struct iovec){
+ .iov_base = __DECONST(char *, str),
+ .iov_len = size,
};
- dprintf("Logging to %s", TypeNames[f->f_type]);
- f->f_time = now;
+ il->totalsize += size;
+}
- switch (f->f_type) {
- case F_UNUSED:
- dprintf("\n");
- break;
+static void
+iovlist_truncate(struct iovlist *il, size_t size)
+{
+ struct iovec *last;
+ size_t diff;
+
+ while (size > il->totalsize) {
+ diff = size - il->totalsize;
+ last = &il->iov[il->iovcnt - 1];
+ if (diff >= last->iov_len) {
+ /* Remove the last iovec entirely. */
+ --il->iovcnt;
+ il->totalsize -= last->iov_len;
+ } else {
+ /* Remove the last iovec partially. */
+ last->iov_len -= diff;
+ il->totalsize -= diff;
+ }
+ }
+}
+
+static void
+fprintlog_write(struct filed *f, struct iovlist *il, int flags)
+{
+ struct msghdr msghdr;
+ struct addrinfo *r;
+ struct socklist *sl;
+ const char *msgret;
+ ssize_t lsent;
+ switch (f->f_type) {
case F_FORW:
+ /* Truncate messages to RFC 5426 recommended size. */
dprintf(" %s", f->fu_forw_hname);
switch (f->fu_forw_addr->ai_addr->sa_family) {
#ifdef INET
case AF_INET:
dprintf(":%d\n",
ntohs(satosin(f->fu_forw_addr->ai_addr)->sin_port));
+ iovlist_truncate(il, 480);
break;
#endif
#ifdef INET6
case AF_INET6:
dprintf(":%d\n",
ntohs(satosin6(f->fu_forw_addr->ai_addr)->sin6_port));
+ iovlist_truncate(il, 1180);
break;
#endif
default:
dprintf("\n");
}
- /* check for local vs remote messages */
- if (strcasecmp(hostname, LocalHostName))
- l = snprintf(line, sizeof line - 1,
- "<%d>%.15s Forwarded from %s: %s",
- f->f_prevpri, (char *)iov[0].iov_base,
- hostname, (char *)iov[5].iov_base);
- else
- l = snprintf(line, sizeof line - 1, "<%d>%.15s %s",
- f->f_prevpri, (char *)iov[0].iov_base,
- (char *)iov[5].iov_base);
- if (l < 0)
- l = 0;
- else if (l > MAXLINE)
- l = MAXLINE;
+ lsent = 0;
for (r = f->fu_forw_addr; r; r = r->ai_next) {
- struct socklist *sl;
-
+ memset(&msghdr, 0, sizeof(msghdr));
+ msghdr.msg_name = r->ai_addr;
+ msghdr.msg_namelen = r->ai_addrlen;
+ msghdr.msg_iov = il->iov;
+ msghdr.msg_iovlen = il->iovcnt;
STAILQ_FOREACH(sl, &shead, next) {
if (sl->sl_ss.ss_family == AF_LOCAL ||
sl->sl_ss.ss_family == AF_UNSPEC ||
sl->sl_socket < 0)
continue;
- lsent = sendto(sl->sl_socket, line, l, 0,
- r->ai_addr, r->ai_addrlen);
- if (lsent == l)
+ lsent = sendmsg(sl->sl_socket, &msghdr, 0);
+ if (lsent == (ssize_t)il->totalsize)
break;
}
- if (lsent == l && !send_to_all)
+ if (lsent == (ssize_t)il->totalsize && !send_to_all)
break;
}
- dprintf("lsent/l: %d/%d\n", lsent, l);
- if (lsent != l) {
+ dprintf("lsent/totalsize: %zd/%zu\n", lsent, il->totalsize);
+ if (lsent != (ssize_t)il->totalsize) {
int e = errno;
logerror("sendto");
errno = e;
@@ -1758,11 +1701,8 @@
case F_FILE:
dprintf(" %s\n", f->fu_fname);
- iov[6] = (struct iovec){
- .iov_base = lf,
- .iov_len = 1
- };
- if (writev(f->f_file, iov, nitems(iov)) < 0) {
+ iovlist_append(il, "\n");
+ if (writev(f->f_file, il->iov, il->iovcnt) < 0) {
/*
* If writev(2) fails for potentially transient errors
* like the filesystem being full, ignore it.
@@ -1782,10 +1722,7 @@
case F_PIPE:
dprintf(" %s\n", f->fu_pipe_pname);
- iov[6] = (struct iovec){
- .iov_base = lf,
- .iov_len = 1
- };
+ iovlist_append(il, "\n");
if (f->fu_pipe_pid == 0) {
if ((f->f_file = p_open(f->fu_pipe_pname,
&f->fu_pipe_pid)) < 0) {
@@ -1793,7 +1730,7 @@
break;
}
}
- if (writev(f->f_file, iov, nitems(iov)) < 0) {
+ if (writev(f->f_file, il->iov, il->iovcnt) < 0) {
int e = errno;
deadq_enter(f->fu_pipe_pid, f->fu_pipe_pname);
@@ -1812,12 +1749,9 @@
case F_TTY:
dprintf(" %s%s\n", _PATH_DEV, f->fu_fname);
- iov[6] = (struct iovec){
- .iov_base = crlf,
- .iov_len = 2
- };
+ iovlist_append(il, "\r\n");
errno = 0; /* ttymsg() only sometimes returns an errno */
- if ((msgret = ttymsg(iov, nitems(iov), f->fu_fname, 10))) {
+ if ((msgret = ttymsg(il->iov, il->iovcnt, f->fu_fname, 10))) {
f->f_type = F_UNUSED;
logerror(msgret);
}
@@ -1826,14 +1760,190 @@
case F_USERS:
case F_WALL:
dprintf("\n");
- iov[6] = (struct iovec){
- .iov_base = crlf,
- .iov_len = 2
- };
- wallmsg(f, iov, nitems(iov));
+ iovlist_append(il, "\r\n");
+ wallmsg(f, il->iov, il->iovcnt);
break;
}
+}
+
+static void
+fprintlog_rfc5424(struct filed *f, const char *hostname, const char *app_name,
+ const char *procid, const char *msgid, const char *structured_data,
+ const char *msg, int flags)
+{
+ struct iovlist il;
+ suseconds_t usec;
+ int i;
+ char timebuf[33], priority_number[5];
+
+ iovlist_init(&il);
+ if (f->f_type == F_WALL)
+ iovlist_append(&il, "\r\n\aMessage from syslogd ...\r\n");
+ iovlist_append(&il, "<");
+ snprintf(priority_number, sizeof(priority_number), "%d", f->f_prevpri);
+ iovlist_append(&il, priority_number);
+ iovlist_append(&il, ">1 ");
+ if (strftime(timebuf, sizeof(timebuf), "%FT%T.______%z",
+ &f->f_lasttime.tm) == sizeof(timebuf) - 2) {
+ /* Add colon to the time zone offset, which %z doesn't do. */
+ timebuf[32] = '\0';
+ timebuf[31] = timebuf[30];
+ timebuf[30] = timebuf[29];
+ timebuf[29] = ':';
+
+ /* Overwrite space for microseconds with actual value. */
+ usec = f->f_lasttime.usec;
+ for (i = 25; i >= 20; --i) {
+ timebuf[i] = usec % 10 + '0';
+ usec /= 10;
+ }
+ iovlist_append(&il, timebuf);
+ } else
+ iovlist_append(&il, "-");
+ iovlist_append(&il, " ");
+ iovlist_append(&il, hostname);
+ iovlist_append(&il, " ");
+ iovlist_append(&il, app_name == NULL ? "-" : app_name);
+ iovlist_append(&il, " ");
+ iovlist_append(&il, procid == NULL ? "-" : procid);
+ iovlist_append(&il, " ");
+ iovlist_append(&il, msgid == NULL ? "-" : msgid);
+ iovlist_append(&il, " ");
+ iovlist_append(&il, structured_data == NULL ? "-" : structured_data);
+ iovlist_append(&il, " ");
+ iovlist_append(&il, msg);
+
+ fprintlog_write(f, &il, flags);
+}
+
+static void
+fprintlog_bsd(struct filed *f, const char *hostname, const char *app_name,
+ const char *procid, const char *msg, int flags)
+{
+ struct iovlist il;
+ const CODE *c;
+ int facility, priority;
+ char timebuf[RFC3164_DATELEN + 1], facility_number[5],
+ priority_number[5];
+ bool facility_found, priority_found;
+
+ if (strftime(timebuf, sizeof(timebuf), RFC3164_DATEFMT,
+ &f->f_lasttime.tm) == 0)
+ timebuf[0] = '\0';
+
+ iovlist_init(&il);
+ switch (f->f_type) {
+ case F_FORW:
+ /* Message forwarded over the network. */
+ iovlist_append(&il, "<");
+ snprintf(priority_number, sizeof(priority_number), "%d",
+ f->f_prevpri);
+ iovlist_append(&il, priority_number);
+ iovlist_append(&il, ">");
+ iovlist_append(&il, timebuf);
+ if (strcasecmp(hostname, LocalHostName) != 0) {
+ iovlist_append(&il, " Forwarded from ");
+ iovlist_append(&il, hostname);
+ iovlist_append(&il, ":");
+ }
+ iovlist_append(&il, " ");
+ break;
+
+ case F_WALL:
+ /* Message written to terminals. */
+ iovlist_append(&il, "\r\n\aMessage from syslogd@");
+ iovlist_append(&il, hostname);
+ iovlist_append(&il, " at ");
+ iovlist_append(&il, timebuf);
+ iovlist_append(&il, " ...\r\n");
+ break;
+
+ default:
+ /* Message written to files. */
+ iovlist_append(&il, timebuf);
+ iovlist_append(&il, " ");
+ iovlist_append(&il, hostname);
+ iovlist_append(&il, " ");
+
+ if (LogFacPri) {
+ iovlist_append(&il, "<");
+
+ facility = f->f_prevpri & LOG_FACMASK;
+ facility_found = false;
+ if (LogFacPri > 1) {
+ for (c = facilitynames; c->c_name; c++) {
+ if (c->c_val == facility) {
+ iovlist_append(&il, c->c_name);
+ facility_found = true;
+ break;
+ }
+ }
+ }
+ if (!facility_found) {
+ snprintf(facility_number,
+ sizeof(facility_number), "%d",
+ LOG_FAC(facility));
+ iovlist_append(&il, facility_number);
+ }
+
+ iovlist_append(&il, ".");
+
+ priority = LOG_PRI(f->f_prevpri);
+ priority_found = false;
+ if (LogFacPri > 1) {
+ for (c = prioritynames; c->c_name; c++) {
+ if (c->c_val == priority) {
+ iovlist_append(&il, c->c_name);
+ priority_found = true;
+ break;
+ }
+ }
+ }
+ if (!priority_found) {
+ snprintf(priority_number,
+ sizeof(priority_number), "%d", priority);
+ iovlist_append(&il, priority_number);
+ }
+
+ iovlist_append(&il, "> ");
+ }
+ break;
+ }
+
+ /* Message body with application name and process ID prefixed. */
+ if (app_name != NULL) {
+ iovlist_append(&il, app_name);
+ if (procid != NULL) {
+ iovlist_append(&il, "[");
+ iovlist_append(&il, procid);
+ iovlist_append(&il, "]");
+ }
+ iovlist_append(&il, ": ");
+ }
+ iovlist_append(&il, msg);
+
+ fprintlog_write(f, &il, flags);
+}
+
+static void
+fprintlog_first(struct filed *f, const char *hostname, const char *app_name,
+ const char *procid, const char *msgid __unused,
+ const char *structured_data __unused, const char *msg, int flags)
+{
+
+ dprintf("Logging to %s", TypeNames[f->f_type]);
+ f->f_time = now;
f->f_prevcount = 0;
+ if (f->f_type == F_UNUSED) {
+ dprintf("\n");
+ return;
+ }
+
+ if (BSDOutputFormat)
+ fprintlog_bsd(f, hostname, app_name, procid, msg, flags);
+ else
+ fprintlog_rfc5424(f, hostname, app_name, procid, msgid,
+ structured_data, msg, flags);
}
/*
@@ -2229,8 +2339,10 @@
if (gethostname(LocalHostName, sizeof(LocalHostName)))
err(EX_OSERR, "gethostname() failed");
if ((p = strchr(LocalHostName, '.')) != NULL) {
- *p++ = '\0';
- LocalDomain = p;
+ /* RFC 5424 prefers logging FQDNs. */
+ if (BSDOutputFormat)
+ *p = '\0';
+ LocalDomain = p + 1;
} else {
LocalDomain = "";
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Tue, Sep 24, 4:55 AM (10 h, 22 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
12631563
Default Alt Text
D15011.id41267.diff (16 KB)
Attached To
Mode
D15011: Add RFC 5424 syslog message output to syslogd.
Attached
Detach File
Event Timeline
Log In to Comment