Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 24 Oct 2006 23:59:01 -0400 (EDT)
From:      "Brian A. Seklecki" <lavalamp@spiritual-machines.org>
To:        freebsd-questions@freebsd.org
Subject:   Reading /dev/klog / log(9)
Message-ID:  <20061024235219.I63561@arbitor.digitalfreaks.org>

next in thread | raw e-mail | index | archive | help

/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*



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20061024235219.I63561>