]> Zhao Yanbai Git Server - minix.git/commitdiff
usyslogd - microsyslogd that has a hard-coded configuration (read from
authorBen Gras <ben@minix3.org>
Fri, 8 Jul 2005 17:21:50 +0000 (17:21 +0000)
committerBen Gras <ben@minix3.org>
Fri, 8 Jul 2005 17:21:50 +0000 (17:21 +0000)
/dev/klog and write to /var/log/messages). It's written so that more
sources and outputs could be added easily though.

commands/simple/Makefile
commands/simple/usyslogd.c [new file with mode: 0644]

index 60f8e5c53f8b050aa50f403942bc336241312f86..005d041e1c3eecb34d4e8ead87eb7e245e9d6469 100755 (executable)
@@ -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 (file)
index 0000000..1cca545
--- /dev/null
@@ -0,0 +1,105 @@
+/* Microsyslogd that does basic syslogging.
+ */
+
+#include <stdio.h>
+#include <sys/select.h>
+#include <sys/utsname.h>
+#include <sys/types.h>
+#include <time.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+
+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;
+}
+