Subject: Re: get a message if a file chanced/get a new record
To: Martin Lonkwitz <martin.lonkwitz@db.com>
From: Luke Mewburn <lukem@wasabisystems.com>
List: tech-kern
Date: 04/20/2001 17:54:57
On Fri, Apr 20, 2001 at 12:33:19PM +0900, Martin Lonkwitz wrote:
> I want to write a C program wich monitors the utmp file and will get a
> message if a new record is added to utmp.
> My idea was to use "fattach" cause I also want to get a message if a new
> line is added at the /var/adm/messages
> Is this a good way or would there be a better way to monitor a file ?
> Any examples available ?
I'm in the process of porting the kqueue()/kevent() mechanism from FreeBSD
into NetBSD. Part of this software allows the ability to monitor files
and directories for changes. E.g, with it you would do something like:
int kq, fd, n;
struct kevent event[1];
kq = kqueue();
fd = open(_PATH_UTMP, O_RDONLY);
EV_SET(&event[0], fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
NOTE_WRITE | NOTE_EXTEND, NULL, NULL);
n = kevent(kq, event, 1, NULL, 0, NULL);
/* do your stuff */
n = event(kq, NULL, 0, event, 1, NULL);
if (event[0].fflags & NOTE_WRITE)
/* file has been written to */
if (event[0].fflags & NOTE_EXTEND)
/* file has grown (usually raises NOTE_WRITE at same time) */
/* ... */
This work was written by Jonathan Lemon for FreeBSD. More info on the
work is available at http://people.freebsd.org/~jlemon/
Hopefully this will appear in NetBSD in the near future. I'm fairly
close to finishing testing the integration of the code into the
kernel.
Luke.
--
Luke Mewburn <lukem@wasabisystems.com> http://www.wasabisystems.com
Luke Mewburn <lukem@netbsd.org> http://www.netbsd.org
Wasabi Systems - providing NetBSD sales, support and service.