Wednesday, April 01, 2009

Linux inotify example

1. inotify_init() - get a fd
2. inotify_add_watch() - need to specify the directory and event(s) want to monitor
3. read() - to check if there is any changes

need to verify: is it possible to get the event when a input device is active by monitoring /dev/input/eventX

The Darke Side: Linux inotify example: "void get_event (int fd, const char * target)
{
ssize_t len, i = 0;
char action[81+FILENAME_MAX] = {0};
char buff[BUFF_SIZE] = {0};

len = read (fd, buff, BUFF_SIZE);

while (i < len) {
struct inotify_event *pevent = (struct inotify_event *)&buff[i];
char action[81+FILENAME_MAX] = {0};

if (pevent->len)
strcpy (action, pevent->name);
else
strcpy (action, target);

if (pevent->mask & IN_ACCESS)
strcat(action, ' was read');
if (pevent->mask & IN_ATTRIB)
strcat(action, ' Metadata changed');
if (pevent->mask & IN_CLOSE_WRITE)"

1 comment:

Eason said...

ya, it seems to be very useful when combine inotify and poll functions.