inotify 简单应用

这里的简单应用遵循以上的通用逻辑。 我们使用一个信号处理程序来监控 ctrl-cSIGINT)并且 重置一个标志keep_running)使应用了解终止操作。 真实的 inotify 调用在 utility 例程当中完成。注意, 我们还创建了一个队列,这样能够将事件从 inotify 底层对象中清除,留着稍后处理。在真实的应用当中,您可能希望用其他具有更高优先级)的线程来完成这一操作。 这里的列举的应用程序,只是为了对一般原理进行举例说明。我们采用了一个简单的事件链表, 队列中的每一条目都包含原始事件加上用于存储指向队列中下一事件指针的空间。

主程序

清单 2 中展示了信号处理程序和主程序。在本例当中,对所有在命令行中出现过的文件或目录进行监控, 并利用事件掩码 IN_ALL_EVENTS 来监控每一对象的所有事件。 在真实的应用程序中,您可能只希望追踪文件与目录的创建或删除事件,因此您可以掩去打开、关闭以及属性改变事件。 如果您对文件或目录的重命名和移动不感兴趣,您也可以掩去各种移动事件。关于更多细节,参见 inotify 帮助信息。

清单 2. inotify-test.c 的简单主程序

  1. /* Signal handler that simply resets a flag to cause termination */  
  2. void signal_handler (int signum)  
  3. {  
  4. keep_running = 0;  
  5. }  
  6. int main (int argc, char **argv)  
  7. {  
  8. /* This is the file descriptor for the inotify watch */  
  9. int inotify_fd;  
  10. keep_running = 1;  
  11. /* Set a ctrl-c signal handler */  
  12. if (signal (SIGINT, signal_handler) == SIG_IGN)  
  13. {  
  14. /* Reset to SIG_IGN (ignore) if that was the prior state */  
  15. signal (SIGINT, SIG_IGN);  
  16. }  
  17. /* First we open the inotify dev entry */  
  18. inotify_fd = open_inotify_fd ();  
  19. if (inotify_fd > 0)  
  20. {  
  21. /* We will need a place to enqueue inotify events,  
  22. this is needed because if you do not read events  
  23. fast enough, you will miss them. This queue is   
  24. probably too small if you are monitoring something  
  25. like a directory with a lot of files and the directory   
  26. is deleted.  
  27. */  
  28. queue_t q;  
  29. q = queue_create (128);  
  30. /* This is the watch descriptor returned for each item we are   
  31. watching. A real application might keep these for some use   
  32. in the application. This sample only makes sure that none of  
  33. the watch descriptors is less than 0.  
  34. */  
  35. int wd;  
  36. /* Watch all events (IN_ALL_EVENTS) for the directories and   
  37. files passed in as arguments.  
  38. Read the article for why you might want to alter this for   
  39. more efficient inotify use in your app.   
  40. */  
  41. int index;  
  42. wd = 0;  
  43. printf("\n");  
  44. for (index = 1; (index < argc) && (wd >= 0); index++)   
  45. {  
  46. wd = watch_dir (inotify_fd, argv[index], IN_ALL_EVENTS);  
  47. }  
  48. if (wd > 0)   
  49. {  
  50. /* Wait for events and process them until a   
  51. termination condition is detected  
  52. */  
  53. process_inotify_events (q, inotify_fd);  
  54. }  
  55. printf ("\nTerminating\n");  
  56. /* Finish up by closing the fd, destroying the queue,  
  57. and returning a proper code  
  58. */  
  59. close_inotify_fd (inotify_fd);  
  60. queue_destroy (q);  
  61. }  
  62. return 0;  

利用 inotify_init 打开文件描述符

清单 3 展示了用于创建 inotify 实例的简单应用函数,并为其获得一个文件描述符。文件描述符返回给了调用者。 如果出现错误,返回值将为负。

清单 3. 使用 inotify_init

  1. /* Create an inotify instance and open a file descriptor  
  2. to access it */  
  3. int open_inotify_fd ()  
  4. {  
  5. int fd;  
  6. watched_items = 0;  
  7. fd = inotify_init ();  
  8. if (fd < 0)  
  9. {  
  10. perror ("inotify_init () = ");  
  11. }  
  12. return fd;  

利用 inotify_add_watch 来增加监控

有了用于 inotify 实例的文件描述符之后,就需要增加一个或多个监控。 可以使用掩码来设置想要监控的事件。在本例当中,采用掩码 IN_ALL_EVENTS,来监控全部的有效事件。

清单 4. 使用 inotify_add_watch

  1. int watch_dir (int fd, const char *dirname, unsigned long mask)  
  2. {  
  3. int wd;  
  4. wd = inotify_add_watch (fd, dirname, mask);  
  5. if (wd < 0)  
  6. {  
  7. printf ("Cannot add watch for \"%s\" with event mask %lX", dirname,  
  8. mask);  
  9. fflush (stdout);  
  10. perror (" ");  
  11. }  
  12. else  
  13. {  
  14. watched_items++;  
  15. printf ("Watching %s WD=%d\n", dirname, wd);  
  16. printf ("Watching = %d items\n", watched_items);   
  17. }  
  18. return wd;  


相关内容