Saturday, 15 January 2011

c - What are watch descriptors really ? (Linux inotify subsystem) -



c - What are watch descriptors really ? (Linux inotify subsystem) -

i'm using inotify() scheme monitoring activity of directories in filesystem in c code.

now, procedure using 1 of these things follows. take integer(say event_notifier), turn inotify descriptor using inotify_init(), so

event_notifier=inotify_init();

now, suppose want maintain watch on events on multiple directories. add together watches event_notifier on directories

wd1 = inotify_add_watch(event_notifier,"/../path..to..directory1/../",in_all_events); wd2 = inotify_add_watch(event_notifier,"/../path..to..directory2/../",in_all_events); wd3 = inotify_add_watch(event_notifier,"/../path..to..directory3/../",in_all_events); . . . . wdn = inotify_add_watch(event_notifier,"/../path..to..directoryn/../",in_all_events);

now, can add together watches on multiple directories. each of these calls returns "watch descriptor" (wd1, wd2, wd3.. wdn above). whenever event occurs in of directories, inotify scheme sends event inotify file descriptor event_notifier along watch descriptor (wd1, wd2...wdn) corresponding particular "watched directory"

when event comes in, can read event_notifier array of struct inotify_event s. inotify_event construction has next fields :

struct inotify_event { int wd; //watch descriptor ... uint32_t len; //size of 'name' field char name[]; //null terminated name }

to read events, do

read(event_notifier, buffer, sizeof(buffer)) struct inotify_event* event; event=(struct inotify_event*)buffer; //assuming 1 event occur

i interested in finding out directory notification came from. when stat() watch descriptor, gives me nil

struct stat fileinfo; fstat(event->wd, &fileinfo); printf("\n size of file %l",fileinfo_st.size);

even readlink() on /proc/self/fd/event->fd did not yield filename.

char filename[25]; readlink("/proc/self/fd/event-wd",filename,sizeof(filename)); printf("\n filename %s",filename);

i have 2 questions :

1) watch descriptor pointing ? what ? 2) how can tell which directory notification coming from ?

what watch descriptor pointing ? ?

the watch descriptor isn't file scheme object or file descriptor. resource descriptor used inotify subsystem link events watched resource , gives possibility specify watches when removing them.

you should note number of possible "open" watch descriptors limited on system. can maximum value using:

cat /proc/sys/fs/inotify/max_user_watches

if reason need more value, can set value using:

sudo sysctl -w fs.inotify.max_user_watches=xxxxxx

how can tell directory notification coming ?

using inotify extension not possible total path file (directory) event structures. application code need special lookup tables storing links between watch descriptors , total path names. did 1 time in php, because felt needed that. may have @ code on github. said, it's php may help understand i'm doing. (the inotify scheme phone call signatures same in php , c)

c linux unix

No comments:

Post a Comment