2

I have the following code:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <event.h>

void fd_cb(int fd,short event,void *arg){
        printf("changed\n");
}

int main(int argc, const char* argv[]){
        struct event eoh_ev;
        FILE *fp;
        int fd;
        fp=fopen("/var/log/syslog","rw");
        fd=fileno(fp);

        event_init();
        event_set(&eoh_ev,fd,EV_READ|EV_WRITE,fd_cb,NULL);
        event_add(&eoh_ev,NULL);
        event_dispatch();


        return 0;
}

As you can see, I'm trying to call fd_cb(...) when something is written to /var/log/syslog.

The problem is, "changed" never gets printed!

I'm running the code as root.

Many thanks in advance,

Eamorr
  • 9,872
  • 34
  • 125
  • 209
  • 2
    You may wish to use [`inotify`](http://en.wikipedia.org/wiki/Inotify) to receive events from file changes. – Steve-o Nov 27 '12 at 15:01

1 Answers1

4

Libevent is designed to work on the same file descriptors that poll or select support. Those system calls are not designed to check for file change events. They are designed to return when a file descriptor can be read or written without blocking, something that isn't very meaningful for regular files (reads and writes to regular files either never block or can always block, depending on how you look at it). In other words - libevent on file descriptors other than sockets, pipes and fifo:s will not work.

There are other mechanisms for checking if a file has changed, but those are not portable.

Art
  • 19,807
  • 1
  • 34
  • 60
  • OK, cool. Thanks for the answer. I'm actually trying to monitor "/sys/class/gpio/gpio4/value" – Eamorr Nov 27 '12 at 15:07