ºìÁªLinuxÃÅ»§
Linux°ïÖú

Linux 2.6ÄÚºËÖеÄÎļþϵͳ±ä»¯Í¨Öª»úÖÆ

·¢²¼Ê±¼ä:2006-05-25 10:05:32À´Ô´:ºìÁª×÷Õß:ckaces
Ò»¡¢ ÒýÑÔ

ÖÚËùÖÜÖª£¬Linux ×ÀÃæϵͳÓë MAC »ò Windows Ïà±ÈÓÐÐí¶à²»ÈçÈËÒâµÄµØ·½£¬ÎªÁ˸ÄÉÆÕâÖÖ×´¿ö£¬¿ªÔ´ÉçÇøÌá³öÓû§Ì¬ÐèÒªÄÚºËÌṩһЩ»úÖÆ£¬ÒÔ±ãÓû§Ì¬Äܹ»¼°Ê±µØµÃÖªÄں˻òµ×²ãÓ²¼þÉ豸·¢ÉúÁËʲô£¬´Ó¶øÄܹ»¸üºÃµØ¹ÜÀíÉ豸£¬¸øÓû§Ìṩ¸üºÃµÄ·þÎñ£¬Èç hotplug¡¢udev ºÍ inotify ¾ÍÊÇÕâÖÖÐèÇó´ßÉúµÄ¡£Hotplug ÊÇÒ»ÖÖÄÚºËÏòÓû§Ì¬Ó¦ÓÃͨ±¨¹ØÓÚÈȲå°ÎÉ豸һЩʼþ·¢ÉúµÄ»úÖÆ£¬×ÀÃæϵͳÄܹ»ÀûÓÃËü¶ÔÉ豸½øÐÐÓÐЧµÄ¹ÜÀí£¬udev ¶¯Ì¬µØά»¤ /dev ϵÄÉ豸Îļþ£¬inotify ÊÇÒ»ÖÖÎļþϵͳµÄ±ä»¯Í¨Öª»úÖÆ£¬ÈçÎļþÔö¼Ó¡¢É¾³ýµÈʼþ¿ÉÒÔÁ¢¿ÌÈÃÓû§Ì¬µÃÖª£¬¸Ã»úÖÆÊÇÖøÃûµÄ×ÀÃæËÑË÷ÒýÇæÏîÄ¿ beagle ÒýÈëµÄ£¬²¢ÔÚ Gamin µÈÏîÄ¿Öб»Ó¦Óá£

ÊÂʵÉÏ£¬ÔÚ inotify ֮ǰÒѾ­´æÔÚÒ»ÖÖÀàËƵĻúÖƽРdnotify£¬µ«ÊÇËü´æÔÚÐí¶àȱÏÝ£º

1. ¶ÔÓÚÏë¼àÊÓµÄÿһ¸öĿ¼£¬Óû§¶¼ÐèÒª´ò¿ªÒ»¸öÎļþÃèÊö·û£¬Òò´ËÈç¹ûÐèÒª¼àÊÓµÄĿ¼½Ï¶à£¬½«µ¼Ö´ò¿ªÐí¶àÎļþÃèÊö·û£¬ÌرðÊÇ£¬Èç¹û±»¼àÊÓĿ¼ÔÚÒƶ¯½éÖÊÉÏ£¨Èç¹âÅÌºÍ USB ÅÌ£©£¬½«µ¼ÖÂÎÞ·¨ umount ÕâЩÎļþϵͳ£¬ÒòΪʹÓà dnotify µÄÓ¦Óôò¿ªµÄÎļþÃèÊö·ûÔÚʹÓøÃÎļþϵͳ¡£

2. dnotify ÊÇ»ùÓÚĿ¼µÄ£¬ËüÖ»Äܵõ½Ä¿Â¼±ä»¯Ê¼þ£¬µ±È»ÔÚĿ¼ÄÚµÄÎļþµÄ±ä»¯»áÓ°Ïìµ½ÆäËùÔÚĿ¼´Ó¶øÒý·¢Ä¿Â¼±ä»¯Ê¼þ£¬µ«ÊÇÒªÏëͨ¹ýĿ¼Ê¼þÀ´µÃÖªÄĸöÎļþ±ä»¯£¬ÐèÒª»º´æÐí¶à stat ½á¹¹µÄÊý¾Ý¡£

3. Dnotify µÄ½Ó¿Ú·Ç³£²»ÓѺã¬ËüʹÓà signal.

Inotify ÊÇΪÌæ´ú dnotify ¶øÉè¼ÆµÄ£¬Ëü¿Ë·þÁË dnotify µÄȱÏÝ£¬ÌṩÁ˸üºÃÓõģ¬¼ò½à¶øÇ¿´óµÄÎļþ±ä»¯Í¨Öª»úÖÆ£º

1. Inotify ²»ÐèÒª¶Ô±»¼àÊÓµÄÄ¿±ê´ò¿ªÎļþÃèÊö·û£¬¶øÇÒÈç¹û±»¼àÊÓÄ¿±êÔÚ¿ÉÒƶ¯½éÖÊÉÏ£¬ÄÇôÔÚ umount ¸Ã½éÖÊÉϵÄÎļþϵͳºó£¬±»¼àÊÓÄ¿±ê¶ÔÓ¦µÄ watch ½«±»×Ô¶¯É¾³ý£¬²¢ÇÒ»á²úÉúÒ»¸ö umount ʼþ¡£

2. Inotify ¼È¿ÉÒÔ¼àÊÓÎļþ£¬Ò²¿ÉÒÔ¼àÊÓĿ¼¡£

3. Inotify ʹÓÃϵͳµ÷Óöø·Ç SIGIO À´Í¨ÖªÎļþϵͳʼþ¡£

4. Inotify ʹÓÃÎļþÃèÊö·û×÷Ϊ½Ó¿Ú£¬Òò¶ø¿ÉÒÔʹÓÃͨ³£µÄÎļþ I/O ²Ù×÷select ºÍ poll À´¼àÊÓÎļþϵͳµÄ±ä»¯¡£

Inotify ¿ÉÒÔ¼àÊÓµÄÎļþϵͳʼþ°üÀ¨£º

IN_ACCESS£¬¼´Îļþ±»·ÃÎÊIN_MODIFY£¬Îļþ±» write IN_ATTRIB£¬ÎļþÊôÐÔ±»Ð޸ģ¬Èç chmod¡¢chown¡¢touch µÈIN_CLOSE_WRITE£¬¿ÉдÎļþ±» close IN_CLOSE_NOWRITE£¬²»¿ÉдÎļþ±» close IN_OPEN£¬Îļþ±» open IN_MOVED_FROM£¬Îļþ±»ÒÆ×ߣ¬Èç mv IN_MOVED_TO£¬Îļþ±»ÒÆÀ´£¬Èç mv¡¢cp IN_CREATE£¬´´½¨ÐÂÎļþIN_DELETE£¬Îļþ±»É¾³ý£¬Èç rm IN_DELETE_SELF£¬×Ôɾ³ý£¬¼´Ò»¸ö¿ÉÖ´ÐÐÎļþÔÚÖ´ÐÐʱɾ³ý×Ô¼ºIN_MOVE_SELF£¬×ÔÒƶ¯£¬¼´Ò»¸ö¿ÉÖ´ÐÐÎļþÔÚÖ´ÐÐʱÒƶ¯×Ô¼ºIN_UNMOUNT£¬ËÞÖ÷Îļþϵͳ±» umount IN_CLOSE£¬Îļþ±»¹Ø±Õ£¬µÈͬÓÚ£¨IN_CLOSE_WRITE | IN_CLOSE_NOWRITE£©

IN_MOVE£¬Îļþ±»Òƶ¯£¬µÈͬÓÚ£¨IN_MOVED_FROM | IN_MOVED_TO£©

×¢£ºÉÏÃæËù˵µÄÎļþÒ²°üÀ¨Ä¿Â¼¡£

¶þ¡¢Óû§½Ó¿Ú

ÔÚÓû§Ì¬£¬inotify ͨ¹ýÈý¸öϵͳµ÷ÓúÍÔÚ·µ»ØµÄÎļþÃèÊö·ûÉϵÄÎļþ I/ ²Ù×÷À´Ê¹Óã¬Ê¹Óà inotify µÄµÚÒ»²½ÊÇ´´½¨ inotify ʵÀý£º


int fd = inotify_init ();


ÿһ¸ö inotify ʵÀý¶ÔÓ¦Ò»¸ö¶ÀÁ¢µÄÅÅÐòµÄ¶ÓÁС£

ÎļþϵͳµÄ±ä»¯Ê¼þ±»³Æ×ö watches µÄÒ»¸ö¶ÔÏó¹ÜÀí£¬Ã¿Ò»¸ö watch ÊÇÒ»¸ö¶þÔª×飨Ŀ±ê£¬Ê¼þÑÚÂ룩£¬Ä¿±ê¿ÉÒÔÊÇÎļþ»òĿ¼£¬Ê¼þÑÚÂë±íʾӦÓÃÏ£Íû¹Ø×¢µÄ inotify ʼþ£¬Ã¿Ò»¸öλ¶ÔÓ¦Ò»¸ö inotify ʼþ¡£Watch ¶ÔÏóͨ¹ý watchÃèÊö·ûÒýÓã¬watches ͨ¹ýÎļþ»òĿ¼µÄ·¾¶ÃûÀ´Ìí¼Ó¡£Ä¿Â¼ watches ½«·µ»ØÔÚ¸ÃĿ¼ÏµÄËùÓÐÎļþÉÏÃæ·¢ÉúµÄʼþ¡£

ÏÂÃ溯ÊýÓÃÓÚÌí¼ÓÒ»¸ö watch£º


int wd = inotify_add_watch (fd, path, mask);


fd ÊÇ inotify_init() ·µ»ØµÄÎļþÃèÊö·û£¬path ÊDZ»¼àÊÓµÄÄ¿±êµÄ·¾¶Ãû£¨¼´ÎļþÃû»òĿ¼Ãû£©£¬mask ÊÇʼþÑÚÂë, ÔÚÍ·Îļþ linux/inotify.h Öж¨ÒåÁËÿһλ´ú±íµÄʼþ¡£¿ÉÒÔʹÓÃͬÑùµÄ·½Ê½À´ÐÞ¸ÄʼþÑÚÂ룬¼´¸Ä±äÏ£Íû±»Í¨ÖªµÄinotify ʼþ¡£Wd ÊÇ watch ÃèÊö·û¡£

ÏÂÃæµÄº¯ÊýÓÃÓÚɾ³ýÒ»¸ö watch£º


int ret = inotify_rm_watch (fd, wd);




fd ÊÇ inotify_init() ·µ»ØµÄÎļþÃèÊö·û£¬wd ÊÇ inotify_add_watch() ·µ»ØµÄ watch ÃèÊö·û¡£Ret ÊǺ¯ÊýµÄ·µ»ØÖµ¡£

ÎļþʼþÓÃÒ»¸ö inotify_event ½á¹¹±íʾ£¬Ëüͨ¹ýÓÉ inotify_init() ·µ»ØµÄÎļþÃèÊö·ûʹÓÃͨ³£Îļþ¶ÁÈ¡º¯Êý read À´»ñµÃ

£º
struct inotify_event {
__s32 wd; /* watch descriptor */
__u32 mask; /* watch mask */
__u32 cookie; /* cookie to synchronize two events */
__u32 len; /* length (including nulls) of name */
char name[0]; /* stub for possible name */
};




½á¹¹ÖÐµÄ wd Ϊ±»¼àÊÓÄ¿±êµÄ watch ÃèÊö·û£¬mask ΪʼþÑÚÂ룬len Ϊ name×Ö·û´®µÄ³¤¶È£¬name Ϊ±»¼àÊÓÄ¿±êµÄ·¾¶Ãû£¬¸Ã½á¹¹µÄ name ×Ö¶ÎΪһ¸ö×®£¬ËüÖ»ÊÇΪÁËÓû§·½ÃæÒýÓÃÎļþÃû£¬ÎļþÃûÊDZ䳤µÄ£¬Ëüʵ¼Ê½ô¸úÔڸýṹµÄºóÃ棬ÎļþÃû½«±» 0 Ìî³äÒÔʹÏÂÒ»¸öʼþ½á¹¹Äܹ» 4 ×Ö½Ú¶ÔÆë¡£×¢Ò⣬len Ò²°ÑÌî³ä×Ö½ÚÊýͳ¼ÆÔÚÄÚ¡£

ͨ¹ý read µ÷ÓÿÉÒÔÒ»´Î»ñµÃ¶à¸öʼþ£¬Ö»ÒªÌṩµÄ buf ×ã¹»´ó¡£


size_t len = read (fd, buf, BUF_LEN);


buf ÊÇÒ»¸ö inotify_event ½á¹¹µÄÊý×éÖ¸Õ룬BUF_LEN Ö¸¶¨Òª¶ÁÈ¡µÄ×ܳ¤¶È£¬buf ´óСÖÁÉÙÒª²»Ð¡ÓÚ BUF_LEN£¬¸Ãµ÷Ó÷µ»ØµÄʼþÊýÈ¡¾öÓÚ BUF_LEN ÒÔ¼°Ê¼þÖÐÎļþÃûµÄ³¤¶È¡£Len Ϊʵ¼Ê¶ÁÈ¥µÄ×Ö½ÚÊý£¬¼´»ñµÃµÄʼþµÄ×ܳ¤¶È¡£

¿ÉÒÔÔÚº¯Êý inotify_init() ·µ»ØµÄÎļþÃèÊö·û fd ÉÏʹÓà select() »òpoll(), Ò²¿ÉÒÔÔÚ fd ÉÏʹÓà ioctl ÃüÁî FIONREAD À´µÃµ½µ±Ç°¶ÓÁеij¤¶È¡£close(fd)½«É¾³ýËùÓÐÌí¼Óµ½ fd ÖÐµÄ watch ²¢×ö±ØÒªµÄÇåÀí¡£


int inotify_init (void);
int inotify_add_watch (int fd, const char *path, __u32 mask);
int inotify_rm_watch (int fd, __u32 mask);


Èý¡¢ÄÚºËʵÏÖ»úÀí

ÔÚÄÚºËÖУ¬Ã¿Ò»¸ö inotify ʵÀý¶ÔÓ¦Ò»¸ö inotify_device ½á¹¹£º


struct inotify_device {
wait_queue_head_t wq; /* wait queue for i/o */
struct idr idr; /* idr mapping wd -> watch */
struct semaphore sem; /* protects this bad boy */
struct list_head events; /* list of queued events */
struct list_head watches; /* list of watches */
atomic_t count; /* reference count */
struct user_struct *user; /* user who opened this dev */
unsigned int queue_size; /* size of the queue (bytes) */
unsigned int event_count; /* number of pending events */
unsigned int max_events; /* maximum number of events */
u32 last_wd; /* the last wd allocated */
};



d_list Ö¸ÏòËùÓÐ inotify_device ×é³ÉµÄÁбíµÄ£¬i_list Ö¸ÏòËùÓб»¼àÊÓ inode ×é³ÉµÄÁÐ±í£¬count ÊÇÒýÓüÆÊý£¬dev Ö¸Ïò¸Ã watch ËùÔÚµÄ inotify ʵÀý¶ÔÓ¦µÄ inotify_device ½á¹¹£¬inode Ö¸Ïò¸Ã watch Òª¼àÊ inode£¬wd ÊÇ·ÖÅä¸ø¸Ã watch µÄÃèÊö·û£¬mask ÊǸà watch µÄʼþÑÚÂ룬±íʾËü¶ÔÄÄЩÎļþϵͳʼþ¸ÐÐËȤ¡£

½á¹¹ inotify_device ÔÚÓû§Ì¬µ÷Óà inotify_init£¨£© ʱ´´½¨£¬µ±¹Ø±Õ inotify_init£¨£©·µ»ØµÄÎļþÃèÊö·ûʱ½«±»ÊÍ·Å¡£½á¹¹ inotify_watch ÔÚÓû§Ì¬µ÷Óà inotify_add_watch£¨£©Ê±´´½¨£¬ÔÚÓû§Ì¬µ÷Óà inotify_rm_watch£¨£© »ò close£¨fd£© ʱ±»ÊÍ·Å¡£

ÎÞÂÛÊÇĿ¼»¹ÊÇÎļþ£¬ÔÚÄÚºËÖж¼¶ÔÓ¦Ò»¸ö inode ½á¹¹£¬inotify ϵͳÔÚ inode ½á¹¹ÖÐÔö¼ÓÁËÁ½¸ö×ֶΣº



struct inotify_watch {
struct list_head d_list; /* entry in inotify_device's list */
struct list_head i_list; /* entry in inode's list */
atomic_t count; /* reference count */
struct inotify_device *dev; /* associated device */
struct inode *inode; /* associated inode */
s32 wd; /* watch descriptor */
u32 mask; /* event mask for this watch */
};




d_list Ö¸ÏòËùÓÐ inotify_device ×é³ÉµÄÁбíµÄ£¬i_list Ö¸ÏòËùÓб»¼àÊÓ inode ×é³ÉµÄÁÐ±í£¬count ÊÇÒýÓüÆÊý£¬dev Ö¸Ïò¸Ã watch ËùÔÚµÄ inotify ʵÀý¶ÔÓ¦µÄ inotify_device ½á¹¹£¬inode Ö¸Ïò¸Ã watch Òª¼àÊ inode£¬wd ÊÇ·ÖÅä¸ø¸Ã watch µÄÃèÊö·û£¬mask ÊǸà watch µÄʼþÑÚÂ룬±íʾËü¶ÔÄÄЩÎļþϵͳʼþ¸ÐÐËȤ¡£

½á¹¹ inotify_device ÔÚÓû§Ì¬µ÷Óà inotify_init£¨£© ʱ´´½¨£¬µ±¹Ø±Õ inotify_init£¨£©·µ»ØµÄÎļþÃèÊö·ûʱ½«±»ÊÍ·Å¡£½á¹¹ inotify_watch ÔÚÓû§Ì¬µ÷Óà inotify_add_watch£¨£©Ê±´´½¨£¬ÔÚÓû§Ì¬µ÷Óà inotify_rm_watch£¨£© »ò close£¨fd£© ʱ±»ÊÍ·Å¡£

ÎÞÂÛÊÇĿ¼»¹ÊÇÎļþ£¬ÔÚÄÚºËÖж¼¶ÔÓ¦Ò»¸ö inode ½á¹¹£¬inotify ϵͳÔÚ inode ½á¹¹ÖÐÔö¼ÓÁËÁ½¸ö×ֶΣº



#ifdef CONFIG_INOTIFY
struct list_head inotify_watches; /* watches on this inode */
struct semaphore inotify_sem; /* protects the watches list */
#endif




inotify_watches ÊÇÔÚ±»¼àÊÓÄ¿±êÉ쵀 watch ÁÐ±í£¬Ã¿µ±Óû§µ÷Óà inotify_add_watch£¨£©Ê±£¬Äں˾ÍΪÌí¼ÓµÄ watch ´´½¨Ò»¸ö inotify_watch ½á¹¹£¬²¢°ÑËü²åÈëµ½±»¼àÊÓÄ¿±ê¶ÔÓ¦µÄ inode µÄ inotify_watches ÁÐ±í¡£inotify_sem ÓÃÓÚͬ²½¶Ô inotify_watches ÁбíµÄ·ÃÎÊ¡£µ±Îļþϵͳ·¢ÉúµÚÒ»²¿·ÖÌáµ½µÄʼþ֮һʱ£¬ÏàÓ¦µÄÎļþϵͳ´úÂ뽫ÏÔʾµ÷ÓÃfsnotify_* À´°ÑÏàÓ¦µÄʼþ±¨¸æ¸ø inotify ϵͳ£¬ÆäÖÐ*ºÅ¾ÍÊÇÏàÓ¦µÄʼþÃû£¬Ä¿Ç°ÊµÏÖ°üÀ¨£º

fsnotify_move£¬Îļþ´ÓÒ»¸öĿ¼Òƶ¯µ½ÁíÒ»¸öĿ¼fsnotify_nameremove£¬Îļþ´ÓĿ¼ÖÐɾ³ýfsnotify_inoderemove£¬×Ôɾ³ýfsnotify_create£¬´´½¨ÐÂÎļþfsnotify_mkdir£¬´´½¨ÐÂĿ¼fsnotify_access£¬Îļþ±»¶Áfsnotify_modify£¬Îļþ±»Ð´fsnotify_open£¬Îļþ±»´ò¿ªfsnotify_close£¬Îļþ±»¹Ø±Õfsnotify_xattr£¬ÎļþµÄÀ©Õ¹ÊôÐÔ±»ÐÞ¸Äfsnotify_change£¬Îļþ±»Ð޸ĻòÔ­Êý¾Ý±»ÐÞ¸ÄÓÐÒ»¸öÀýÍâÇé¿ö£¬¾ÍÊÇ inotify_unmount_inodes£¬Ëü»áÔÚÎļþϵͳ±» umount ʱµ÷ÓÃÀ´Í¨Öª umount ʼþ¸ø inotify ϵͳ¡£

ÒÔÉÏÌáµ½µÄ֪ͨº¯Êý×îºó¶¼µ÷Óà inotify_inode_queue_event£¨inotify_unmount_inodesÖ±½Óµ÷Óà inotify_dev_queue_event £©£¬¸Ãº¯ÊýÊ×ÏÈÅж϶ÔÓ¦µÄinodeÊÇ·ñ±»¼àÊÓ£¬Õâͨ¹ý²é¿´ inotify_watches ÁбíÊÇ·ñΪ¿ÕÀ´ÊµÏÖ£¬Èç¹û·¢ÏÖ inode ûÓб»¼àÊÓ£¬Ê²Ã´Ò²²»×ö£¬Á¢¿Ì·µ»Ø£¬·´Ö®£¬±éÀú inotify_watches ÁÐ±í£¬¿´ÊÇ·ñµ±Ç°µÄÎļþ²Ù×÷ʼþ±»Ä³¸ö watch ¼àÊÓ£¬Èç¹ûÊÇ£¬µ÷Óà inotify_dev_queue_event£¬·ñÔò£¬·µ»Ø¡£º¯Êýinotify_dev_queue_event Ê×ÏÈÅжϸÃʼþÊÇ·ñÊÇÉÏÒ»¸öʼþµÄÖظ´£¬Èç¹ûÊǾͶªÆú¸Ãʼþ²¢·µ»Ø£¬·ñÔò£¬ËüÅжÏÊÇ·ñ inotify ʵÀý¼´ inotify_device µÄʼþ¶ÓÁÐÊÇ·ñÒç³ö£¬Èç¹ûÒç³ö£¬²úÉúÒ»¸öÒç³öʼþ£¬·ñÔò²úÉúÒ»¸öµ±Ç°µÄÎļþ²Ù×÷ʼþ£¬ÕâЩʼþͨ¹ýkernel_event ¹¹½¨£¬kernel_event ½«´´½¨Ò»¸ö inotify_kernel_event ½á¹¹£¬È»ºó°Ñ¸Ã½á¹¹²åÈëµ½¶ÔÓ¦µÄ inotify_device µÄ events ʼþÁÐ±í£¬È»ºó»½ÐѵȴýÔÚinotify_device ½á¹¹ÖÐµÄ wq Ö¸ÏòµÄµÈ´ý¶ÓÁС£Ïë¼àÊÓÎļþϵͳʼþµÄÓû§Ì¬½ø³ÌÔÚinotify ʵÀý£¨¼´ inotify_init£¨£© ·µ»ØµÄÎļþÃèÊö·û£©Éϵ÷Óà read ʱµ«Ã»ÓÐʼþʱ¾Í¹ÒÔڵȴý¶ÓÁÐ wq ÉÏ¡£

ËÄ¡¢Ê¹ÓÃʾÀý

ÏÂÃæÊÇÒ»¸öʹÓà inotify À´¼àÊÓÎļþϵͳʼþµÄÀý×Ó£º



#include
#include
#include

_syscall0(int, inotify_init)
_syscall3(int, inotify_add_watch, int, fd, const char *, path, __u32, mask)
_syscall2(int, inotify_rm_watch, int, fd, __u32, mask)

char * monitored_files[] = {
"./tmp_file",
"./tmp_dir",
"/mnt/sda3/windows_file"
};

struct wd_name {
int wd;
char * name;
};

#define WD_NUM 3
struct wd_name wd_array[WD_NUM];

char * event_array[] = {
"File was accessed",
"File was modified",
"File attributes were changed",
"writtable file closed",
"Unwrittable file closed",
"File was opened",
"File was moved from X",
"File was moved to Y",
"Subfile was created",
"Subfile was deleted",
"Self was deleted",
"Self was moved",
"",
"Backing fs was unmounted",
"Event queued overflowed",
"File was ignored"
};
#define EVENT_NUM 16
#define MAX_BUF_SIZE 1024

int main(void)
{
int fd;
int wd;
char buffer[1024];
char * offset = NULL;
struct inotify_event * event;
int len, tmp_len;
char strbuf[16];
int i = 0;

fd = inotify_init();
if (fd < 0) {
printf("Fail to initialize inotify.\n");
exit(-1);
}

for (i=0; imask & IN_ISDIR) {
memcpy(strbuf, "Direcotory", 11);
}
else {
memcpy(strbuf, "File", 5);
}
printf("Object type: %s\n", strbuf);
for (i=0; iwd != wd_array[i].wd) continue;
printf("Object name: %s\n", wd_array[i].name);
break;
}
printf("Event mask: %08X\n", event->mask);
for (i=0; imask & (1 event = (struct inotify_event *)(offset + tmp_len);
offset += tmp_len;
}
}
}




¸Ã³ÌÐò½«¼àÊÓ·¢ÉúÔÚµ±Ç°Ä¿Â¼ÏµÄÎļþ tmp_file Ó뵱ǰĿ¼ÏµÄĿ¼ tmp_dir ÉϵÄËùÓÐÎļþϵͳʼþ£¬ ͬʱËüÒ²½«¼àÊÓ·¢ÉúÔÚÎļþ /mnt/sda3/windows_file ÉϵÄÎļþϵͳʼþ£¬×¢Ò⣬/mnt/sda3 ÊÇ SATA Ó²ÅÌ·ÖÇø 3 µÄ¹Ò½Óµã¡£

ϸÐĵĶÁÕß¿ÉÄÜ×¢Òâµ½£¬¸Ã³ÌÐòÊײ¿Ê¹Óà _syscallN À´ÉùÃ÷ inotify ϵͳµ÷Óã¬Ô­ÒòÊÇÕâЩϵͳµ÷ÓÃÊÇÔÚ×îеÄÎȶ¨ÄÚºË 2.6.13 ÖÐÒýÈëµÄ£¬glibc ²¢Ã»ÓÐʵÏÖÕâЩϵͳµ÷ÓõĿ⺯Êý°æ±¾£¬Òò´Ë£¬ÎªÁËÄÜÔÚ³ÌÐòÖÐʹÓÃÕâЩϵͳµ÷Ó㬱ØÐëͨ¹ý _syscallN À´ÉùÃ÷ÕâЩеÄϵͳ£¬ÆäÖÐµÄ N ΪҪÉùÃ÷µÄϵͳµ÷ÓÃʵ¼ÊµÄ²ÎÊýÊý¡£»¹ÓÐÐèҪעÒâµÄµØ·½ÊÇϵͳµÄÍ·Îļþ±ØÐëÓë±»Æô¶¯µÄÄÚºËÆ¥Å䣬ΪÁËÈÃÉÏÃæµÄ³ÌÐòÄܹ»³É¹¦±àÒ룬±ØÐëÈà 2.6.13 µÄÄÚºËÍ·Îļþ£¨°üÀ¨ include/linux/*£¬ include/asm/* ºÍ include/asm-generic/*£©ÔÚÍ·ÎļþËÑË÷·¾¶ÄÚ£¬²¢ÇÒÊǵÚÒ»ÓÅÏÈËÑË÷µÄÍ·Îļþ·¾¶£¬ÒòΪ _syscallN ÐèÒªÓõ½ÕâЩͷÎļþÖÐµÄ linux/unistd.h ºÍ asm/unistd.h£¬ËüÃÇ°üº¬ÁË inotify µÄÈý¸öϵͳµ÷ÓõÄϵͳµ÷ÓúŠ__NR_inotify_init¡¢__NR_inotify_add_watch ºÍ __NR_inotify_rm_watch.

Òò´Ë£¬ÒªÏë³É¹¦±àÒë´Ë³ÌÐò£¬Ö»Òª°ÑÓû§±àÒëºÃµÄÄں˵ÄÍ·Îļþ¿½±´µ½¸Ã³ÌÐòËùÔڵķ¾¶£¬²¢Ê¹ÓÃÈçÏÂÃüÁî±àÒë¼´¿É£º


$gcc -o inotify_example -I. inotify_example.c


×¢Ò⣺µ±Ç°Ä¿Â¼ÏÂÓ¦µ±°üº¬ linux¡¢asm ºÍ asm-generic Èý¸öÒѱàÒëºÃµÄ 2.6.13 Äں˵ÄÓÐÎļþĿ¼£¬asm ÊÇÒ»¸öÁ´½Ó£¬Òò´Ë¿½±´ asm Í·ÎļþµÄʱºòÐèÒª¿½±´ asm Óë asm-ARCH£¨¶ÔÓÚ x86 ƽ̨Ӧµ±ÊÇ asm-i386£©¡£È»ºó£¬ÎªÁËÔËÐиóÌÐò£¬ÐèÒªÔÚµ±Ç°Ä¿Â¼Ï´´½¨Îļþ tmp_file ºÍĿ¼ tmp_dir£¬¶ÔÓÚ/mnt/sda3/windows_file Îļþ£¬Óû§ÐèÒªÒÀ×Ô¼ºµÄʵ¼ÊÇé¿ö¶ø¶¨£¬¿ÉÄÜÊÇ/mnt/dosc/windows_file£¬¼´ /mnt/dosc ÊÇÒ»¸ö FAT32 µÄ windows Ó²ÅÌ£¬Òò´ËÓû§ÔÚ±àÒë¸Ã³ÌÐòʱÐèÒª¸ù¾Ý×Ô¼ºµÄʵ¼ÊÇé¿öÀ´ÐÞ¸Ä /mnt/sda3.Windows_file ÊÇÔÚ±» mount Ó²ÅÌÉÏ´´½¨µÄÒ»¸öÎļþ£¬ÎªÁËÔËÐиóÌÐò£¬Ëü±ØÐë±»´´½¨¡£

ÒÔÏÂÊÇ×÷ÕßÔÚ redhat 9.0 ÉÏÔËÐд˳ÌÐòµÃµ½µÄһЩ½á¹û£º

µ±ÔËÐд˳ÌÐòµÄʱºòÔÚÁíÒ»¸öÐéÄâÖÕ¶ËÖ´ÐÐ cat ./tmp_file£¬´Ë³ÌÐòµÄÊä³öΪ£º


Some event happens, len = 48.
Object type: File
Object name: ./tmp_file
Event mask: 00000020
Event: File was opened
Object type: File
Object name: ./tmp_file
Event mask: 00000001
Event: File was accessed
Object type: File
Object name: ./tmp_file
Event mask: 00000010
Event: Unwrittable file closed



ÒÔÉÏʼþÇå³þµØ˵Ã÷ÁË cat Ö¸ÁîÖ´ÐÐÁËÎļþ open ºÍ close ²Ù×÷£¬µ±È» open ºÍ close²Ù×÷¶¼ÊôÓÚ access ²Ù×÷£¬ÈκζÔÎļþµÄ²Ù×÷¶¼ÊÇ access ²Ù×÷¡£

´ËÍ⣬ÔËÐÐ vi ./tmp_file£¬·¢ÏÖ viʵ¼ÊÔڱ༭Îļþʱ¸´ÖÆÁËÒ»¸ö¸±±¾£¬ÔÚδ±£´æ֮ǰÊǶԸ±±¾½øÐвÙ×÷¡£ÔËÐÐ vi ./tmp_file£¬ Ð޸IJ¢±£´æÍ˳öʱ£¬·¢ÏÖ vi ʵ¼ÊÔÚ±£´æÐÞ¸Äʱɾ³ýÁË×î³õµÄÎļþ²¢°ÑÄǸö¸±±¾ÎļþÃû¸ü¸ÄΪ×î³õµÄÎļþµÄÃû³Æ¡£×¢Ò⣬ʼþ"File was ignored"±íʾϵͳ°Ñ¸ÃÎļþ¶ÔÓ¦µÄ watch ´Ó inotify ʵÀýµÄ watch ÁбíÖÐɾ³ý£¬ÒòΪÎļþÒѾ­±»É¾³ý¡£¶ÁÕß¿ÉÒÔ×Ô¼º·Ö±ðÖ´ÐÐÃüÁecho "abc" > ./tmp_file ¡¢rm -f tmp_file¡¢ ls tmp_dir¡¢ cd tmp_dir£»touch c.txt¡¢ rm c.txt ¡¢ umount /mnt/sda3£¨Êµ¼ÊÓû§ÐèҪʹÓÃ×Ô¼ºµ±Ê±µÄ mount µã·¾¶Ãû£©£¬È»ºó·ÖÎöһϽá¹û¡£Umount ´¥·¢Á½¸öʼþ£¬Ò»¸ö±íʾÎļþÒѾ­±»É¾³ý»ò²»ÔÚ´æÔÚ£¬ÁíÒ»¸ö±íʾ¸ÃÎļþµÄ watch±»´Ó watch ÁбíÖÐɾ³ý¡£

Îå¡¢µäÐÍÓ¦ÓÃ

beagle ÊÇ GNOME µÄ×ÀÃæËÑË÷ÒýÇæÏîÄ¿£¬inotify µÄÒýÈë¾ÍÊÇÍêÈ«ÊÜËüµÄÇý¶¯¶ø×öµÄ¡£¶ÔÓÚ×ÀÃæËÑË÷ÒýÇ棬ËüÒ»°ã×÷Ϊһ¸öÓÅÏȼ¶ºÜµÍµÄºǫ́½ø³ÌÔËÐУ¬ Ö»ÓÐÔÚϵͳûÓÐÆäËûÈÎÎñ¿ÉÔËÐÐʱ²Å±»µ÷¶ÈÖ´ÐУ¬×ÀÃæËÑË÷ÒýÇæµÄÖ÷ÒªÓÃ;¾ÍÊÇΪϵͳµÄÎļþϵͳµÄÎļþ½¨Á¢Ë÷ÒýÊý¾Ý¿â£¬ÒÔ±ãÓû§ÔÚÐèҪijÎļþµ«ÓÖÏë²»Æð´æ·ÅÔÚÄÄÀïʱÄܹ»¸ù¾ÝijЩ¹Ø¼ü×Ö»òÌØÕ÷¿ìËÙµØËÑË÷µ½ÐèÒªµÄÎļþ£¬¾ÍÏóʹÓÃÍøÂçËÑË÷ÒýÇæ google Ò»Ñù±ã½Ý¡£ÎļþϵͳÓиöÌصã¾ÍÊÇÖ»ÓÐijЩÎļþ»á±ä»¯£¬Òò´Ë×ÀÃæËÑË÷ÒýÇæÔÚµÚÒ»´Î½¨Á¢ÍêË÷ÒýÊý¾Ý¿âºó£¬Ã»±ØÒªÖظ´±éÀúËùÓеÄÎļþ½¨Á¢ÐµÄË÷Òý£¬ËüÖ»ÐèÒª¸üÐÂÐÞ¸ÄÁ˵ÄÎļþµÄË÷Òý£¬½¨Á¢ÐÂÔö¼ÓµÄÎļþµÄË÷Òý£¬É¾³ýÒѾ­É¾³ýµÄÎļþµÄË÷Òý¾Í×ã¹»ÁË£¬ÕâÑù×ÀÃæËÑË÷ÒýÇæÐèÒª×öµÄ¹¤×÷¾Í´ó´óµØ¼õÉÙ¡£Inotify ¾ÍÊÇΪÕâÒ»ÒâͼרÃÅÉè¼ÆµÄ£¬beagle ΪÐèÒª¼àÊÓµÄĿ¼»òÎļþ´´½¨ÁËinotify ʵÀý£¬È»ºóËü¾ÍµÈ´ý¸Ã inotify ÉÏ·¢ÉúÎļþϵͳʼþ£¬Èç¹ûûÓÐÈκÎÎļþ±ä»¯£¬beagle ½«²»ÐèÒªÈκοªÏú£¬Ö»ÓÐÔÚÓб»¼àÊÓµÄʼþ·¢Éúʱ£¬beagle ²Å±»»½ÐѲ¢¸ù¾Ýʵ¼ÊʼþÀ´¸üжÔÓ¦µÄÎļþµÄË÷Òý£¬È»ºó¼ÌÐø˯ÃߵȴýÏÂÒ»¸öÎļþϵͳʼþ·¢Éú¡£ÔÚ SuSe 9.3 ºÍ¼´½«·¢²¼µÄ 10.0 ÖоͰüº¬Á˸Ã×ÀÃæËÑË÷ÒýÇ棬ËüÄܹ»ÎªÎĵµ¡¢email¡¢ÒôÀÖ¡¢Í¼ÏóºÍÓ¦ÓõȽ¨Á¢Ë÷Òý¡£Ê¹Óùý windows ϵÄ×ÀÃæËÑË÷ÒýÇæµÄ¶ÁÕß¶Ô google ºÍ yahoo ÒÔ¼° Microsoft µÄ×ÀÃæËÑË÷ÒýÇæÓÐÉî¿ÌµÄÌå»á£¬¸ÐÐËȤ¶ÁÕß¿ÉÒÔ°²×° SuSe ʹÓÃһϡ£

Áù¡¢Ð¡½á

inotify ÊÇÔÚ 2.6.13 ÖÐÒýÈëµÄй¦ÄÜ£¬ËüΪÓû§Ì¬¼àÊÓÎļþϵͳµÄ±ä»¯ÌṩÁËÇ¿´óµÄÖ§³Ö£¬±¾ÎÄÏ꾡µØ½éÉÜÁËÆäÆðÔ´¡¢ÄÚºËʵÏÖ¡¢Óû§½Ó¿ÚÒÔ¼°Ê¹Óã¬ÓÐÐËȤµÄ¶ÁÕß¿ÉÒÔ¶Á 2.6.13µÄÏà¹ØÔ´ÂëÀ´½øÒ»²½Á˽âÆäʵÏÖϸ½Ú¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ