tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Examples of kfilter_register(9)?



In article <alpine.DEB.1.10.0909010818430.24747%calanda.fehu.org@localhost>,
Hubert Feyrer  <hubert%feyrer.de@localhost> wrote:
>On Mon, 31 Aug 2009, Marc Tooley wrote:
>> monkey around with it in userspace, but I'm still interested in seeing
>> an example, or hearing a description of what it was designed to do and
>> what purpose it was designed to serve.
>
>Unless I'm totally off-track here, there is a userland interface: The code 
>below watches a given directory, and if any file is written or linked to 
>it runs a given command. Maybe that helps... based on code by someone 
>elase (I forgot who... sorry! :).

Here's another example.

christos

#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <err.h>

int
main(int argc, char *argv[])
{
        int fd, kq, nev;
        struct kevent ev, ch;
        static const struct timespec tout = { 1, 0 };

        if ((fd = open(argv[1], O_RDONLY)) == -1)
                err(1, "Cannot open `%s'", argv[1]);

        if ((kq = kqueue()) == -1)
                err(1, "Cannot create kqueue");

        EV_SET(&ch, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, 
            NOTE_DELETE|NOTE_WRITE|NOTE_EXTEND|NOTE_ATTRIB|NOTE_LINK|
            NOTE_RENAME|NOTE_REVOKE, 0, 0);
        for (;;) {
                nev = kevent(kq, &ch, 1, &ev, 1, &tout);
                if (nev == -1)
                        err(1, "kevent");
                if (nev == 0)
                        continue;
                if (ev.fflags & NOTE_DELETE) {
                        printf("deleted ");
                        ev.fflags &= ~NOTE_DELETE;
                }
                if (ev.fflags & NOTE_WRITE) {
                        printf("written ");
                        ev.fflags &= ~NOTE_WRITE;
                }
                if (ev.fflags & NOTE_EXTEND) {
                        printf("extended ");
                        ev.fflags &= ~NOTE_EXTEND;
                }
                if (ev.fflags & NOTE_ATTRIB) {
                        printf("chmod/chown ");
                        ev.fflags &= ~NOTE_ATTRIB;
                }
                if (ev.fflags & NOTE_LINK) {
                        printf("hardlinked ");
                        ev.fflags &= ~NOTE_LINK;
                }
                if (ev.fflags & NOTE_RENAME) {
                        printf("renamed ");
                        ev.fflags &= ~NOTE_RENAME;
                }
                if (ev.fflags & NOTE_REVOKE) {
                        printf("revoked ");
                        ev.fflags &= ~NOTE_REVOKE;
                }
                printf("\n");
                if (ev.fflags)
                        warnx("unknown event 0x%x\n", ev.fflags);
        }
}




Home | Main Index | Thread Index | Old Index