forked from Lainports/opnsense-ports
202 lines
4.8 KiB
Diff
202 lines
4.8 KiB
Diff
diff -urN syslogd.c syslogd.c
|
|
--- usr.sbin/syslogd/syslogd.c 2011-06-01 12:44:59.002094254 +1200
|
|
+++ usr.sbin/syslogd/syslogd.c 2011-06-01 12:45:29.840070134 +1200
|
|
@@ -88,6 +88,7 @@
|
|
#include <sys/resource.h>
|
|
#include <sys/syslimits.h>
|
|
#include <sys/types.h>
|
|
+#include <sys/mman.h>
|
|
|
|
#include <netinet/in.h>
|
|
#include <netdb.h>
|
|
@@ -109,6 +110,8 @@
|
|
#include <utmpx.h>
|
|
|
|
#include "pathnames.h"
|
|
+#include "clog.h"
|
|
+
|
|
#include "ttymsg.h"
|
|
|
|
#define SYSLOG_NAMES
|
|
@@ -182,6 +185,11 @@
|
|
char f_pname[MAXPATHLEN];
|
|
pid_t f_pid;
|
|
} f_pipe;
|
|
+ struct {
|
|
+ char f_rname[MAXPATHLEN];
|
|
+ struct clog_footer *f_footer;
|
|
+ size_t f_size;
|
|
+ } f_ring;
|
|
} f_un;
|
|
char f_prevline[MAXSVLINE]; /* last message logged */
|
|
char f_lasttime[16]; /* time of last occurrence */
|
|
@@ -260,10 +268,12 @@
|
|
#define F_USERS 5 /* list of users */
|
|
#define F_WALL 6 /* everyone logged on */
|
|
#define F_PIPE 7 /* pipe to program */
|
|
+#define F_RING 8 /* ring buffer (circular log) */
|
|
|
|
-const char *TypeNames[8] = {
|
|
+const char *TypeNames[9] = {
|
|
"UNUSED", "FILE", "TTY", "CONSOLE",
|
|
- "FORW", "USERS", "WALL", "PIPE"
|
|
+ "FORW", "USERS", "WALL", "PIPE",
|
|
+ "RING"
|
|
};
|
|
|
|
static struct filed *Files; /* Log files that we write to */
|
|
@@ -327,6 +337,8 @@
|
|
static void printline(const char *, char *, int);
|
|
static void printsys(char *);
|
|
static int p_open(const char *, pid_t *);
|
|
+static ssize_t rbwrite(struct filed *, char *, size_t);
|
|
+static ssize_t rbwritev(struct filed *, struct iovec *, int);
|
|
static void readklog(void);
|
|
static void reapchild(int);
|
|
static void usage(void);
|
|
@@ -1279,6 +1291,20 @@
|
|
}
|
|
break;
|
|
|
|
+ case F_RING:
|
|
+ dprintf(" %s\n", f->f_un.f_ring.f_rname);
|
|
+ v->iov_base = "\n";
|
|
+ v->iov_len = 1;
|
|
+ if (rbwritev(f, iov, 7)==-1) {
|
|
+ int e = errno;
|
|
+ (void)munmap(f->f_un.f_ring.f_footer,sizeof(struct clog_footer));
|
|
+ (void)close(f->f_file);
|
|
+ f->f_type = F_UNUSED;
|
|
+ errno = e;
|
|
+ logerror(f->f_un.f_fname);
|
|
+ }
|
|
+ break;
|
|
+
|
|
case F_PIPE:
|
|
dprintf(" %s\n", f->f_un.f_pipe.f_pname);
|
|
v->iov_base = lf;
|
|
@@ -1581,6 +1607,10 @@
|
|
}
|
|
f->f_un.f_pipe.f_pid = 0;
|
|
break;
|
|
+ case F_RING:
|
|
+ (void)munmap(f->f_un.f_ring.f_footer,sizeof(struct clog_footer));
|
|
+ (void)close(f->f_file);
|
|
+ break;
|
|
}
|
|
next = f->f_next;
|
|
if (f->f_program) free(f->f_program);
|
|
@@ -1722,6 +1752,10 @@
|
|
}
|
|
break;
|
|
|
|
+ case F_RING:
|
|
+ printf("%s", f->f_un.f_ring.f_rname);
|
|
+ break;
|
|
+
|
|
case F_PIPE:
|
|
printf("%s", f->f_un.f_pipe.f_pname);
|
|
break;
|
|
@@ -1772,6 +1806,7 @@
|
|
const char *p, *q;
|
|
char *bp;
|
|
char buf[MAXLINE], ebuf[100];
|
|
+ struct stat sb;
|
|
|
|
dprintf("cfline(\"%s\", f, \"%s\", \"%s\")\n", line, prog, host);
|
|
|
|
@@ -1979,6 +2014,40 @@
|
|
}
|
|
break;
|
|
|
|
+ case '%':
|
|
+ if ((f->f_file = open(p+1, O_RDWR, 0 )) < 0) {
|
|
+ f->f_type = F_UNUSED;
|
|
+ logerror(p+1);
|
|
+ break;
|
|
+ }
|
|
+ if (fstat(f->f_file, &sb)<0) {
|
|
+ (void)close(f->f_file);
|
|
+ f->f_type = F_UNUSED;
|
|
+ logerror(p+1);
|
|
+ break;
|
|
+ }
|
|
+ f->f_un.f_ring.f_footer = mmap(NULL,sizeof(struct clog_footer),
|
|
+ PROT_READ|PROT_WRITE, MAP_SHARED, f->f_file,
|
|
+ sb.st_size - sizeof(struct clog_footer));
|
|
+ if (f->f_un.f_ring.f_footer == NULL) {
|
|
+ (void)close(f->f_file);
|
|
+ f->f_type = F_UNUSED;
|
|
+ logerror(p+1);
|
|
+ break;
|
|
+ }
|
|
+ if (memcmp(&(f->f_un.f_ring.f_footer->cf_magic), MAGIC_CONST, 4) != 0) {
|
|
+ (void)munmap(f->f_un.f_ring.f_footer, sizeof(struct clog_footer));
|
|
+ (void)close(f->f_file);
|
|
+ f->f_type = F_UNUSED;
|
|
+ errno = ENODEV;
|
|
+ logerror(p+1);
|
|
+ break;
|
|
+ }
|
|
+ f->f_un.f_ring.f_size = sb.st_size;
|
|
+ (void)strcpy(f->f_un.f_ring.f_rname, p + 1);
|
|
+ f->f_type = F_RING;
|
|
+ break;
|
|
+
|
|
case '|':
|
|
f->f_un.f_pipe.f_pid = 0;
|
|
(void)strlcpy(f->f_un.f_pipe.f_pname, p + 1,
|
|
@@ -2710,6 +2779,53 @@
|
|
return (socks);
|
|
}
|
|
|
|
+static ssize_t
|
|
+rbwritev(struct filed *f, struct iovec *iov, int iovcnt)
|
|
+{
|
|
+ ssize_t out = 0;
|
|
+ ssize_t n;
|
|
+ int i;
|
|
+
|
|
+ for (i = 0; i < iovcnt; i++) {
|
|
+ n = rbwrite(f, iov[i].iov_base, iov[i].iov_len);
|
|
+ if (n == -1)
|
|
+ return (-1);
|
|
+ out += n;
|
|
+ }
|
|
+ return (out);
|
|
+}
|
|
+
|
|
+static ssize_t
|
|
+rbwrite(struct filed *f, char *buf, size_t nbytes)
|
|
+{
|
|
+ size_t maxwrite = f->f_un.f_ring.f_footer->cf_max - f->f_un.f_ring.f_footer->cf_next;
|
|
+ ssize_t err;
|
|
+ ssize_t out = 0;
|
|
+
|
|
+ f->f_un.f_ring.f_footer->cf_lock = 1;
|
|
+ while (nbytes > 0) {
|
|
+ maxwrite = f->f_un.f_ring.f_footer->cf_max - f->f_un.f_ring.f_footer->cf_next;
|
|
+ if (maxwrite > nbytes)
|
|
+ maxwrite = nbytes;
|
|
+ err = pwrite(f->f_file, buf, maxwrite, f->f_un.f_ring.f_footer->cf_next);
|
|
+ if (err == -1) {
|
|
+ f->f_un.f_ring.f_footer->cf_lock = 0;
|
|
+ return (-1);
|
|
+ }
|
|
+ nbytes -= err;
|
|
+ out += err;
|
|
+ buf += err;
|
|
+ f->f_un.f_ring.f_footer->cf_next += err;
|
|
+ if (f->f_un.f_ring.f_footer->cf_next == f->f_un.f_ring.f_footer->cf_max) {
|
|
+ f->f_un.f_ring.f_footer->cf_next = 0;
|
|
+ f->f_un.f_ring.f_footer->cf_wrap = 1;
|
|
+ }
|
|
+
|
|
+ }
|
|
+ f->f_un.f_ring.f_footer->cf_lock = 0;
|
|
+ return (out);
|
|
+}
|
|
+
|
|
static void
|
|
double_rbuf(int fd)
|
|
{
|