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.
This commit is contained in:
Ben Gras 2005-07-08 17:21:50 +00:00
parent 2b3961eb91
commit c021dafe27
2 changed files with 114 additions and 0 deletions

View file

@ -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 $? $@

105
commands/simple/usyslogd.c Normal file
View file

@ -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;
}