title: What are watch descriptors really ? (Linux inotify subsystem)
body: <p>I'm currently using the inotify() system for monitoring the activity of certain directories in the filesystem in my C code. </p>
  
  <p>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</p>
  
  <pre><code>event_notifier=inotify_init();
  </code></pre>
  
  <p>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</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>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"</p>
  
  <p>When an event comes in, I can read the event_notifier for an array of <strong>struct inotify_event</strong> s. This inotify_event structure has the following fields :</p>
  
  <pre><code>struct inotify_event  
  {
    int wd; //Watch descriptor   
     ...  
    uint32_t len; //Size of 'name' field  
    char name[];  //null terminated name  
  }
  </code></pre>
  
  <p>To read events, you simply do</p>
  
  <pre><code>read(event_notifier, buffer, sizeof(buffer))
  struct inotify_event* event;
  event=(struct inotify_event*)buffer; //Assuming only one event will occur
  </code></pre>
  
  <p>I am interested in finding out which directory the notification came from. But when I stat() the watch descriptor, it gives me nothing </p>
  
  <pre><code>struct stat fileinfo;
  fstat(event-&gt;wd, &amp;fileinfo);
  printf("\n Size of file is %l",fileinfo_st.size);
  </code></pre>
  
  <p>Even readlink() on /proc/self/fd/event->fd did not yield any filename.</p>
  
  <pre><code>char filename[25]; 
  readlink("/proc/self/fd/event-wd",filename,sizeof(filename));
  printf("\n The filename is %s",filename);
  </code></pre>
  
  <p>I have two questions :   </p>
  
  <p>1) What is the watch descriptor pointing to exactly ? <strong>What is it good for ?</strong><br>
  2) How can I tell <strong>which directory the notification is coming from</strong> ?   </p>