I'm currently using the inotify() system for monitoring the activity of certain directories in the filesystem in my C code. 

Now, the procedure for using one of these things is as follows. You take an integer(say event\_notifier), turn it into an inotify descriptor using inotify\_init(), like so

```
event_notifier=inotify_init();

```

Now, suppose I want to keep a watch over events on multiple directories. I will then add watches to this event\_notifier over those 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, I can add watches over multiple directories. Each of these calls returns a "watch descriptor" (wd1, wd2, wd3.. wdn above). Whenever an event occurs in any of the directories, the inotify system sends an event to the inotify file descriptor event\_notifier along with the watch descriptor (wd1, wd2...wdn) corresponding to that particular "watched directory"

When an event comes in, I can read the event\_notifier for an array of **struct inotify\_event** s. This inotify\_event structure has the following fields :

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

```

To read events, you simply do

```
read(event_notifier, buffer, sizeof(buffer))
struct inotify_event* event;
event=(struct inotify_event*)buffer; //Assuming only one event will occur

```

I am interested in finding out which directory the notification came from. But when I stat() the watch descriptor, it gives me nothing 

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

```

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

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

```

I have two questions : 

1) What is the watch descriptor pointing to exactly ? **What is it good for ?**  

2) How can I tell **which directory the notification is coming from** ?