tech-kern archive

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

Re: kqueue - Specific change information.

On Fri, 28 Mar 2008, Adam Panayis wrote:
Hi, I would like to be notified of changes made within a folder but not have to rescan the folder to get an updated listing everytime I am notified of a change.

Is there a method using Kqueues by which I can monitor a folder and then, when say, a file is added retrieve that specific info? i.e, "file x added in folder y" as opposed to just getting "something happened in folder y"

Is this possible?

Try the code appended below. Run:

        % mkdir /tmp/kev
        % ./kev2 /tmp/kev /tmp/kev.log
        Waiting for /tmp/kev to get touched...
-->  (*** run "touch /tmp/kev/x" in other window ***)
        Ran: ( date ; ls -l '/tmp/kev' ) >/tmp/kev.log
        % cat /tmp/kev.log
        Wed Feb 13 16:58:33 CET 2008
        total 0
        -rw-r--r--  1 feyrer  wheel  0 Feb 13 16:58 x

Fiddling with the arguments to find out what exactly happened is left as an exercise to you.

If your question was on how to do that from within the kernel instead of from userland -> I have no idea. :)

 - Hubert

/* kev2.c */
#include <stdio.h>
#include <sys/event.h>
#include <sys/time.h>
#include <assert.h>
#include <fcntl.h>
#include <stdlib.h>

void usage(void)
    printf("Usage: %s dir-to-monitor logfile\n",
    exit (0);

int main(int argc, char *argv[])
    char cmd[256];
    int qd, fd;
    struct kevent filter, result;
    int n;

    if (argc != 3)

    sprintf(cmd, "( date ; ls -l '%s' ) >%s", argv[1], argv[2]);

    qd = kqueue();
    assert(qd > 0);

    fd = open(argv[1], O_RDONLY);
    assert(fd > 0);

    EV_SET( &filter, fd, EVFILT_VNODE, EV_ADD,

    printf("Waiting for %s to get touched...\n", argv[1]);

    /* n = */ kevent(qd, &filter, 1, &result, 1, NULL);
    /* assert(n > 0); *//* speed up! */

    /* should check 'result' here... */

    printf("Ran: %s\n", cmd);

    return 0;

Home | Main Index | Thread Index | Old Index