kqueue / kevent というものを初めて使ってみた;

#include <stdio.h>

#include <sys/types.h>
#include <dirent.h>

#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>

main () {
 /*
  *  Open current working directory
  */
  DIR *d;
  int dfd;
  int err;

  if (!(d = opendir("."))) {
    perror("opendir");
    exit(1);
  }

 /*
  *  create and register kernel event queue
  */
  int kq;
  struct kevent kev;

  if ((kq = kqueue()) == -1) {
    perror("kqueue");
    closedir(d);
    exit(1);
  }

  EV_SET(&kev, dirfd(d), EVFILT_VNODE, EV_ADD|EV_CLEAR, NOTE_WRITE, 0, NULL);

  if (kevent(kq, &kev, 1, NULL, 0, NULL) < 0) {
    perror("kevent on register");
    closedir(d);
    exit(1);
  }

  int retval;

 /*
  *  then go monitoring
  */
  if (0 < kevent(kq, NULL, 0, &kev, 1, NULL)) {
    printf("dir changed.\n");
  } else {
    perror("kevent on nofity");
    closedir(d);
    exit(1);
  }

 /*
  *  normal exit
  */
  closedir(d);
  exit(0);
}

Front page   New Page list Search Recent changes   Help   RSS of recent changes