What are watch descriptors? (Linux subsystem inotify)

I am currently using the inotify () system to monitor the activity of certain directories in the file system in my C code.

Now the procedure for using one of the following things is as follows. You take an integer (say event_notifier), turn it into an inotify descriptor using inotify_init (), for example

event_notifier=inotify_init();

Now suppose I want to keep track of events in several directories. Then I will add a clock to this event_notifier in these 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 hours to several directories. Each of these calls returns a "clock descriptor" (wd1, wd2, wd3 .. wdn above). Whenever an event occurs in any of the directories, the inotify system sends the event to the inotify event_notifier descriptor description file along with the clock descriptor (wd1, wd2 ... wdn) corresponding to this particular "observable directory"

When an event arrives, I can read the event_notifier for the struct inotify_event array . 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 to know from which directory the notification came from. But when I stat () the clock descriptor it gives 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 return a single file name.

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

I have two questions:

1) , ? ?
2) , ?

+4
2

, ? ?

. , inotify .

, "" . , :

cat  /proc/sys/fs/inotify/max_user_watches

- , , :

sudo sysctl -w fs.inotify.max_user_watches=XXXXXX

, ?

inotify, () . , . PHP, , . Github. , PHP, , . ( inotify PHP C)

+1

  1.  
  2. , ?   ,
int. , .  
 
?   . inotify .


( ) . - . .

, ?   .
wd1 = inotify_add_watch(
   event_notifier,
   "/../path..to..directory1/../",
   IN_ALL_EVENTS
   );

0

Source: https://habr.com/ru/post/1545482/


All Articles