Reading /dev/klog / log(9)

Brian A. Seklecki lavalamp at spiritual-machines.org
Wed Oct 25 03:59:10 UTC 2006


/dev/klog seems to be special in that, even in O_NONBLOCk, it:

*) Never blocks, but does(?)
*) Always returns read(2) = 0
*) Never returns EOF

I'm trying to read out the contents silently via a shell script prior to 
starting syslog-ng.  I.e., "drain it".

One would think any I/O manipulation utility (dd/pax/cpio/cat, etc.) would 
work. The problem is that dd(1) w/ count=1 && bs=1 will always hang after 
reading the last byte is read.

If I had some way of couting the bytes that could be read using stat(1) or 
fstat(1), I could pass the correct count=$val

I've been staring at syslogd.c on FreeBSD for a while, but I'm still at a 
lost as to the logic of how it even gets into klog_read();  I don't see 
any select(2) or stat(2) logic.

Anyway, this perl script sort of explains where I'm trying to go:

$BUFSIZ=1;

sysopen(KLOG, "/dev/klog", O_RDONLY|O_NONBLOCK);

$oldbuffer="init";

while (1) {
     undef($rv);
     $rv = sysread(KLOG, $buffer, $BUFSIZ);
     if (!defined($rv) && $! == EAGAIN) {
         # would block
         print "$rv\n";
         print "Would block";
         exit;
     } else {
         #successfully read $rv bytes from HANDLE
         print "No Block";
         print "$rv\n";
         print "Buffer: \"" . $buffer . "\"\n";
         chomp($buffer);
         print "Buffer Chomped: \"" . $buffer . "\"\n";
         print "Old Buffer: \"" . $oldbuffer . "\"\n";
         print "Old Buffer Unchomped: \"" . $oldbufferunchomped . "\"\n";
         #if ( ($buffer eq "") && ($oldbuffer eq "")) {
         if ($buffer eq "") {
              print "match, exit!\n";
         }
         $oldbuffer = $buffer
     }
}
sysclose (KLOG);


~BAS

l8*


More information about the freebsd-questions mailing list