{"id":3379,"date":"2011-10-05T23:21:04","date_gmt":"2011-10-05T15:21:04","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=3379"},"modified":"2011-10-06T00:55:08","modified_gmt":"2011-10-05T16:55:08","slug":"inotify%e6%9c%ba%e5%88%b6%e5%9c%a8%e5%86%85%e6%a0%b8%e6%80%81%e4%b8%8b%e7%9a%84%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/3379","title":{"rendered":"inotify\u673a\u5236\u5728\u5185\u6838\u6001\u4e0b\u7684\u4f7f\u7528\u65b9\u6cd5"},"content":{"rendered":"<p>inotify\u662f\u4e00\u79cd\u4f7f\u7528\u7b80\u5355\u800c\u529f\u80fd\u5f3a\u5927\u7684\u6587\u4ef6\u7cfb\u7edf\u4e8b\u4ef6\u76d1\u63a7\u673a\u5236\u3002\u5728\u7528\u6237\u6001\u4e0b\u901a\u8fc7\u4e00\u7ec4\u7b80\u5355\u7684\u7cfb\u7edf\u8c03\u7528\u5373\u53ef\u4f7f\u7528inotify\u673a\u5236\u76d1\u63a7\u6587\u4ef6\u7684\u53d8\u5316\uff0c\u800c\u5728\u5185\u6838\u6001\u4e2d\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4e00\u7ec4API\u6765inotify\u673a\u5236\u3002<\/p>\n<h3>1.\u521b\u5efa\u5e76\u521d\u59cb\u5316inotify\u5b9e\u4f8b<\/h3>\n<p>\u4e3a\u4e86\u5728\u5185\u6838\u4e2d\u4f7f\u7528inotify\u673a\u5236\uff0c\u5fc5\u987b\u9996\u5148\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e00\u4e2ainotify\u5b9e\u4f8b\uff0c\u6bcf\u4e2ainotify\u5b9e\u4f8b\u5176\u5b9e\u5bf9\u5e94\u7684\u662f\u4e00\u4e2a\u6570\u636e\u7ed3\u6784inotify_handle\u3002<\/p>\n<pre class=\"brush:c\">struct inotify_handle {\r\n\tstruct idr\t\tidr;\t\t\/* idr mapping wd -&gt; watch *\/\r\n\tstruct mutex\t\tmutex;\t\t\/* protects this bad boy *\/\r\n\tstruct list_head\twatches;\t\/* list of watches *\/\r\n\tatomic_t\t\tcount;\t\t\/* reference count *\/\r\n\tu32\t\t\tlast_wd;\t\/* the last wd allocated *\/\r\n\tconst struct inotify_operations *in_ops; \/* inotify caller operations *\/\r\n};<\/pre>\n<p>\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e00\u4e2ainotify\u5b9e\u4f8b\u7684\u8fc7\u7a0b\u5176\u5b9e\u5c31\u662f\u5206\u914d\u5e76\u521d\u59cb\u5316\u4e00\u4e2ainotify_handle\u7ed3\u6784\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u901a\u8fc7inotify_init()\u5b8c\u6210\u3002\u6b64\u5916\uff0c\u6bcf\u4e2ainotify\u5b9e\u4f8b\u90fd\u4e0e\u4e00\u4e2ainotify_operations\u7ed3\u6784\u76f8\u5173\u8054\uff0c\u8be5\u7ed3\u6784\u4e2d\u6709\u4e24\u4e2a\u94a9\u5b50\u51fd\u6570\uff0c\u5206\u522b\u5b9a\u4e49\u4e86\u4e8b\u4ef6\u5904\u7406\u548c\u9500\u6bc1watch\u4e24\u4e2a\u51fd\u6570\u7684\u63a5\u53e3\uff0c\u8fd9\u4e24\u4e2a\u94a9\u5b50\u51fd\u6570\u5e94\u8be5\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u6765\u5b9e\u73b0\u3002<\/p>\n<pre class=\"brush:c\">struct inotify_operations {\r\n\tvoid (*handle_event)(struct inotify_watch *, u32, u32, u32,\r\n\t\t\t     const char *, struct inode *);\r\n\tvoid (*destroy_watch)(struct inotify_watch *);\r\n};<\/pre>\n<p>\u4e00\u4e2ainotify_init()\u901a\u7528\u7684\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\">struct inotify_operations *ops;\r\nops-&gt;handle_event = my_handle_event;\r\nops-&gt;destroy_watch = my_destroy_watch;\r\nstruct inotify_handle *ih = inotify_init(ops);<\/pre>\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c\u901a\u8fc7\u5411inotify_init()\u4e2d\u4f20\u5165\u4e00\u4e2ainotify_operations\u7ed3\u6784\u7684\u53d8\u91cf\u6765\u5b9e\u73b0\u94a9\u5b50\u51fd\u6570\u548cinotify\u5b9e\u4f8b\u4e4b\u95f4\u7684\u5173\u8054\u3002<\/p>\n<h3>2.\u6dfb\u52a0watch<\/h3>\n<p>\u4eceinotify\u5b9e\u4f8b\u6240\u5bf9\u5e94\u7684\u6570\u636e\u7ed3\u6784inotify_handle\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u6bcf\u4e2ainotify\u5b9e\u4f8b\u90fd\u62e5\u6709\u4e00\u4e2a\u7531watch\u7ec4\u6210\u7684\u94fe\u8868\u3002\u8be5\u53cc\u94fe\u8868\u4e0a\u7684\u6bcf\u4e2awatch\u5373\u4ee3\u8868\u8be5inotify\u5b9e\u4f8b\u6240\u76d1\u63a7\u7684\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u53ef\u80fd\u662f\u6587\u4ef6\uff0c\u4e5f\u53ef\u80fd\u662f\u76ee\u5f55\u3002\u6bcf\u4e2awatch\u5728\u5185\u6838\u4e2d\u7684\u8868\u793a\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\">struct inotify_watch {\r\n\tstruct list_head\th_list;\t\/* entry in inotify_handle's list *\/\r\n\tstruct list_head\ti_list;\t\/* entry in inode's list *\/\r\n\tatomic_t\t\tcount;\t\/* reference count *\/\r\n\tstruct inotify_handle\t*ih;\t\/* associated inotify handle *\/\r\n\tstruct inode\t\t*inode;\t\/* associated inode *\/\r\n\t__s32\t\t\twd;\t\/* watch descriptor *\/\r\n\t__u32\t\t\tmask;\t\/* event mask for this watch *\/\r\n};<\/pre>\n<p>\u8be5\u7ed3\u6784\u4e2d\u6bcf\u4e2a\u5b57\u6bb5\u7684\u542b\u4e49\u5982\u4e0b\uff1a<\/p>\n<p><strong>h_list<\/strong>\uff1a\u4e00\u4e2ainotify\u5b9e\u4f8b\u4e2d\u6240\u6709watch\u7ec4\u6210\u4e00\u4e2a\u53cc\u94fe\u8868\uff0ch_list\u8868\u793a\u5f53\u524dwatch\u5728\u8be5\u53cc\u94fe\u8868\u4e2d\u6240\u5904\u7684\u7ed3\u70b9\u3002\u8be5\u53cc\u94fe\u8868\u7684\u8868\u5934\u5373\u4e3ainotify_handle\u7ed3\u6784\u4e2d\u7684watches\u5b57\u6bb5\u3002<\/p>\n<p><strong>i_list<\/strong>\uff1a\u4e00\u4e2a\u6587\u4ef6\u53ef\u80fd\u88ab\u591a\u4e2ainotify\u5b9e\u4f8b\u76d1\u63a7\uff0c\u800c\u88ab\u76d1\u63a7\u4e00\u6b21\u5c31\u4ea7\u751f\u4e00\u4e2awatch\uff0c\u8be5\u6587\u4ef6\u5bf9\u5e94\u7684\u6240\u6709watch\u7ec4\u6210\u4e00\u4e2a\u53cc\u94fe\u8868\uff0ci_list\u8868\u793a\u5f53\u524dwatch\u5728\u8be5\u53cc\u94fe\u8868\u4e2d\u6240\u5904\u7684\u7ed3\u70b9\u3002\u8be5\u53cc\u94fe\u8868\u7684\u8868\u5934\u5373\u4e3ainode\u7ed3\u6784\u4e2d\u7684inotify_watches\u5b57\u6bb5\u3002<\/p>\n<p><strong>count<\/strong>\uff1a\u8868\u793a\u8be5watch\u88ab\u5f15\u7528\u7684\u6b21\u6570\u3002<\/p>\n<p><strong>ih<\/strong>\uff1a\u6bcf\u4e2awatch\u5fc5\u7136\u5c5e\u4e8e\u67d0\u4e2ainotify\u5b9e\u4f8b\uff0c\u8be5\u5b57\u6bb5\u6307\u5411\u5f53\u524dwatch\u6240\u5c5e\u7684inotify_handle\u7ed3\u6784\u3002<\/p>\n<p><strong>inode<\/strong>\uff1a\u8be5\u5b57\u6bb5\u6307\u5411\u5f53\u524dwatch\u6240\u5173\u8054\u7684\u6587\u4ef6\u7684inode\u3002<\/p>\n<p><strong>wd<\/strong>\uff1a\u8868\u793a\u5f53\u524dwatch\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/p>\n<p><strong>mask<\/strong>\uff1a\u8868\u793a\u5f53\u524d\u76d1\u63a7\u5bf9\u8c61\u6240\u5bf9\u5e94\u7684\u4e8b\u4ef6\u63a9\u7801\u3002<\/p>\n<p>\u5f53\u521d\u59cb\u5316\u5b8c\u4e00\u4e2ainotify\u5b9e\u4f8b\u540e\uff0c\u901a\u8fc7inotify_add_watch()\u5373\u53ef\u5411\u8be5\u5b9e\u4f8b\u4e2d\u6dfb\u52a0watch\u3002\u4e0d\u8fc7\u5728\u6dfb\u52a0\u524d\u8fd8\u9700\u901a\u8fc7inotify_init_watch()\u5bf9\u6bcf\u4e2awatch\u8fdb\u884c\u521d\u59cb\u5316\u3002<\/p>\n<p>\u5bf9watch\u521d\u59cb\u5316\u5b8c\u6210\u7684\u4e3b\u8981\u5de5\u4f5c\u5373\u521d\u59cb\u5316\u4e24\u4e2a\u94fe\u8868\u7ed3\u70b9\u3001\u8be5watch\u7684\u5f15\u7528\u8ba1\u6570\u7b49\u3002\u5411inotify\u5b9e\u4f8b\u6dfb\u52a0\u4e00\u4e2awatch\u4e3b\u8981\u5b8c\u6210\u7684\u5de5\u4f5c\u5373\u4e3a\u5411\u4e24\u4e2awatch\u94fe\u8868\u4e2d\u6dfb\u52a0\u5f53\u524d\u7684watch\u7ed3\u70b9\uff0c\u5e76\u4e14\u66f4\u65b0watch\u7684\u5f15\u7528\u8ba1\u6570\u7b49\u3002<\/p>\n<h3>3.\u5220\u9664\u4e00\u4e2awatch<\/h3>\n<p>\u4ece\u6307\u5b9a\u7684inotify\u5b9e\u4f8b\u4e2d\u5220\u9664\u4e00\u4e2awatch\u6240\u5b8c\u6210\u7684\u5de5\u4f5c\u5176\u5b9e\u548c\u6dfb\u52a0watch\u7684\u8fc7\u7a0b\u76f8\u53cd\uff0c\u901a\u8fc7inotify_rm_watch()\u5373\u53ef\u5b8c\u6210\u3002<\/p>\n<h3>4.\u5bf9\u76d1\u63a7\u4e8b\u4ef6\u7684\u5904\u7406<\/h3>\n<p>handle_event()\u901a\u5e38\u5e76\u4e0d\u5bf9\u5177\u4f53\u7684\u76d1\u63a7\u5bf9\u8c61\u505a\u5904\u7406\uff0c\u800c\u662f\u4f5c\u4e3a\u6240\u6709\u76d1\u63a7\u4e8b\u4ef6\u7684\u5165\u53e3\u70b9\uff1b\u63a5\u7740\u6839\u636e\u6bcf\u4e2a\u4e8b\u4ef6\u7684\u63a9\u7801\u505a\u51fa\u201c\u5206\u6d41\u201d\u52a8\u4f5c\uff0c\u5373\u5bf9\u5177\u4f53\u7684\u76d1\u63a7\u4e8b\u4ef6\u505a\u4ee5\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>inotify\u662f\u4e00\u79cd\u4f7f\u7528\u7b80\u5355\u800c\u529f\u80fd\u5f3a\u5927\u7684\u6587\u4ef6\u7cfb\u7edf\u4e8b\u4ef6\u76d1\u63a7\u673a\u5236\u3002\u5728\u7528\u6237\u6001\u4e0b\u901a\u8fc7\u4e00\u7ec4\u7b80\u5355\u7684\u7cfb\u7edf\u8c03\u7528\u5373\u53ef\u4f7f\u7528inot [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[110],"tags":[252,230],"class_list":["post-3379","post","type-post","status-publish","format-standard","hentry","category-linux","tag-inotify","tag-230"],"views":6763,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3379","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=3379"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3379\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=3379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=3379"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=3379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}