From: Ben Gras Date: Fri, 8 Jul 2005 17:21:50 +0000 (+0000) Subject: usyslogd - microsyslogd that has a hard-coded configuration (read from X-Git-Tag: v3.1.0~642 X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/zpipe.c?a=commitdiff_plain;h=c021dafe270bf5fef5d9602d48fbb06701366fbb;p=minix.git usyslogd - microsyslogd that has a hard-coded configuration (read from /dev/klog and write to /var/log/messages). It's written so that more sources and outputs could be added easily though. --- diff --git a/commands/simple/Makefile b/commands/simple/Makefile index 60f8e5c53..005d041e1 100755 --- a/commands/simple/Makefile +++ b/commands/simple/Makefile @@ -187,6 +187,7 @@ ALL = \ update \ uud \ uue \ + usyslogd \ vol \ wc \ which \ @@ -792,6 +793,10 @@ uud: uud.c $(CCLD) -o $@ $? @install -S 4kw $@ +usyslogd: usyslogd.c + $(CCLD) -o $@ $? + @install -S 4kw $@ + uue: uue.c $(CCLD) -o $@ $? @install -S 4kw $@ @@ -1010,6 +1015,7 @@ install: \ /usr/bin/uudecode \ /usr/bin/uue \ /usr/bin/uuencode \ + /usr/bin/usyslogd \ /usr/bin/vol \ /usr/bin/wc \ /usr/bin/which \ @@ -1532,6 +1538,9 @@ install: \ /usr/bin/uudecode: /usr/bin/uud install -l $? $@ +/usr/bin/usyslogd: usyslogd + install -cs -o bin $? $@ + /usr/bin/uue: uue install -cs -o bin $? $@ diff --git a/commands/simple/usyslogd.c b/commands/simple/usyslogd.c new file mode 100644 index 000000000..1cca54599 --- /dev/null +++ b/commands/simple/usyslogd.c @@ -0,0 +1,105 @@ +/* Microsyslogd that does basic syslogging. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +char *nodename; + +void logline(FILE *outfp, char *proc, char *line) +{ + time_t now; + struct tm *tm; + char *d, *s; + time(&now); + tm = localtime(&now); + d=asctime(tm); + + /* Trim off year and newline. */ + if((s=strrchr(d, ' '))) + *s = '\0'; + fprintf(outfp, "%s %s kernel: %s\n", d, nodename, line); +} + +void copy(int in_fd, FILE *outfp) +{ + static char linebuf[5*1024]; + int l, acc = 0; + while((l=read(in_fd, linebuf, sizeof(linebuf)-2)) > 0) { + char *b, *eol; + int i; + acc += l; + for(i = 0; i < l; i++) + if(linebuf[i] == '\0') + linebuf[i] = ' '; + if(linebuf[l-1] == '\n') l--; + linebuf[l] = '\n'; + linebuf[l+1] = '\0'; + b = linebuf; + while(eol = strchr(b, '\n')) { + *eol = '\0'; + logline(outfp, "kernel", b); + b = eol+1; + } + } + + /* Nothing sensible happened? Avoid busy-looping. */ + if(!acc) sleep(1); + + return; +} + +int +main(int argc, char *argv[]) +{ + int config_fd, klog_fd, n, maxfd; + char *nn; + FILE *logfp; + struct utsname utsname; + + if(uname(&utsname) < 0) { + perror("uname"); + return 1; + } + + nodename = utsname.nodename; + if((nn=strchr(nodename, '.'))) + *nn = '\0'; + + if((klog_fd = open("/dev/klog", O_NONBLOCK | O_RDONLY)) < 0) { + perror("/dev/klog"); + return 1; + } + + if(!(logfp = fopen("/var/log/messages", "a"))) { + perror("/var/log/messages"); + return 1; + } + + maxfd = klog_fd; + + while(1) { + fd_set fds; + FD_ZERO(&fds); + FD_SET(klog_fd, &fds); + n = select(maxfd+1, &fds, NULL, NULL, NULL); + if(n <= 0) { + sleep(1); + continue; + } + if(FD_ISSET(klog_fd, &fds)) { + copy(klog_fd, logfp); + } + fflush(logfp); + } + + return 0; +} +