From owner-freebsd-questions@FreeBSD.ORG Wed Oct 25 03:59:10 2006 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 7D77E16A407 for ; Wed, 25 Oct 2006 03:59:10 +0000 (UTC) (envelope-from lavalamp@spiritual-machines.org) Received: from mail.digitalfreaks.org (arbitor.digitalfreaks.org [216.151.95.158]) by mx1.FreeBSD.org (Postfix) with ESMTP id 3B3E143D4C for ; Wed, 25 Oct 2006 03:59:10 +0000 (GMT) (envelope-from lavalamp@spiritual-machines.org) Received: by mail.digitalfreaks.org (Postfix, from userid 1022) id 03F8D18098; Tue, 24 Oct 2006 23:59:02 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by mail.digitalfreaks.org (Postfix) with ESMTP id 0326D18092 for ; Tue, 24 Oct 2006 23:59:02 -0400 (EDT) Date: Tue, 24 Oct 2006 23:59:01 -0400 (EDT) From: "Brian A. Seklecki" X-X-Sender: lavalamp@arbitor.digitalfreaks.org To: freebsd-questions@freebsd.org Message-ID: <20061024235219.I63561@arbitor.digitalfreaks.org> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed Subject: Reading /dev/klog / log(9) X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 25 Oct 2006 03:59:10 -0000 /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*