/*
* Syslogd
* Nigel Roles ([email protected]) 24/7/2000
* Steve Simon 26/11/2004
*/
#include <u.h>
#include <libc.h>
#include <ip.h>
char *facname[] = {
[0] "syslog.kernel",
[1] "syslog.user",
[2] "syslog.mail",
[3] "syslog.daemon",
[4] "syslog.auth",
[5] "syslog.syslog",
[6] "syslog.lpr",
[7] "syslog.news",
[8] "syslog.uucp",
[9] "syslog.cron",
[10]"syslog.security",
[11]"syslog.ftp",
[12]"syslog.ntp",
[13]"syslog.audit",
[14]"syslog.logalert",
[15]"syslog.clock",
[16]"syslog.local0",
[17]"syslog.local1",
[18]"syslog.local2",
[19]"syslog.local3",
[20]"syslog.local4",
[21]"syslog.local5",
[22]"syslog.local6",
[23]"syslog.local7",
};
char *priname[] = {
[0] "emergency",
[1] "alert",
[2] "critical",
[3] "error",
[4] "warning",
[5] "notice",
[6] "info",
[7] "debug",
};
#pragma varargck type "U" Udphdr*
int udphdrfmt(Fmt *f);
void usage(void);
void
main(int argc, char **argv)
{
uchar buf[8192];
int ctl, netfd, nb, pri, fac;
char *p, mntpt[32], data[64], devdir[1024];
setnetmtpt(mntpt, sizeof(mntpt), nil);
ARGBEGIN{
case 'x':
setnetmtpt(mntpt, sizeof(mntpt), EARGF(usage()));
break;
default:
usage();
}ARGEND;
if (argc != 0)
usage();
fmtinstall('I', eipfmt);
fmtinstall('U', udphdrfmt);
/* udp=syslog port=514 */
if ((ctl = announce(netmkaddr("*", "udp", "syslog"), devdir)) == -1)
sysfatal("can't announce %s: %r", data);
if(fprint(ctl, "headers") == -1)
sysfatal("can't set udp headers: %r");
snprint(data, sizeof(data), "%s/data", devdir);
if ((netfd = open(data, ORDWR)) == -1)
sysfatal("can't open %s: %r", data);
close(ctl);
switch(fork()){
case 0: exits(0);
case -1: sysfatal("fork failed %r\n");
default: break;
}
while((nb = read(netfd, buf, sizeof(buf))) >= 0) {
buf[nb] = 0;
p = (char *)buf + Udphdrsize;
if (*p == '<') {
p++;
pri = strtol(p, &p, 10);
if (*p == '>')
p++;
}
else
pri = 0;
fac = pri >> 3;
pri = pri & 7;
if (fac >= nelem(facname) || !facname[fac])
syslog(0, "syslog.other", "%U %s (fac=%d) %s", (Udphdr *)buf, priname[pri], fac, p);
else
syslog(0, facname[fac], "%U %s %s", (Udphdr *)buf, priname[pri], p);
}
exits(0);
}
int
udphdrfmt(Fmt *f)
{
Udphdr *h;
h = va_arg(f->args, Udphdr *);
return fmtprint(f, "udp!%I!%ud", h->raddr, (h->rport[0]<<8)|h->rport[1]);
}
void
usage(void)
{
fprint(2, "usage: %s [-x net]\n", argv0);
exits("usage");
}
|