天天看點

inotify通知機制

#include <unistd.h>
#include <stdio.h>
#include <sys/inotify.h>
#include <string.h>
#include <errno.h>


/*
 *參考: frameworks\native\services\inputflinger\EventHub.cpp
 */

/*Usage: inotify <dir> */

/*
*	inotify主要功能:它是一個核心用于通知使用者空間程式檔案系統變化的機制
*	衆所周知,Linux 桌面系統與 MAC 或 Windows 相比有許多不如人意的地方,
	為了改善這種狀況,開源社群提出使用者态需要核心提供一些機制,以便使用者
	态能夠及時地得知核心或底層硬體裝置發生了什麼,進而能夠更好地管理設
	備,給使用者提供更好的服務,如 hotplug、udev 和 inotify 就是這種需求
	催生的。
	Hotplug 是一種核心向使用者态應用通報關于熱插拔裝置一些事件發生的機制,
	桌面系統能夠利用它對裝置進行有效的管理
	udev 動态地維護 /dev 下的裝置檔案
	inotify 是一種檔案系統的變化通知機制,如檔案增加、删除等事件可以立
	刻讓使用者态得知,該機制是著名的桌面搜尋引擎項目 beagle 引入的,并在
	Gamin 等項目中被應用。
*	
*/

#if 0
len長度配置設定以16位元組對齊配置設定
如name為hi,len為16
  name為hello_world_123456 len為48

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 */
};
#endif

int read_process_inotify_fd(int fd)
{
	int res;
	char event_buf[512];
	int event_size;
	int event_pos = 0;
	struct inotify_event *event;

	/* read */	
	res = read(fd, event_buf, sizeof(event_buf));

	printf("res:%d (int)sizeof(*event):%d\n",res,(int)sizeof(*event));
	if(res < (int)sizeof(*event)) {
		if(errno == EINTR)
			return 0;
		printf("could not get event, %s\n", strerror(errno));
		return -1;
	}

	/* process
	 * 讀到的資料是1個或者多個inotify_event,如touch/rm 多個檔案,需要周遊處理
	 * 它們的長度不一樣,逐個處理
	 */

	while(res >= (int)sizeof(*event)) {
		event = (struct inotify_event *)(event_buf + event_pos);
		printf("event->wd:%d event->mask:0x%x event->len:%d event->name:%s\n",event->wd,event->mask,event->len,event->name);
		if(event->len) {
			if(event->mask & IN_CREATE) {
				printf("create file: %s\n", event->name);
			} else {
				printf("delete file: %s\n", event->name);
			}
		}
		event_size = sizeof(*event) + event->len;
		res -= event_size;
		event_pos += event_size;
	}

	return 0;
}

int main(int argc, char **argv)
{
	int mINotifyFd;
	int result;

	if (argc != 2){
		printf("Usage: %s <dir>\n", argv[0]);
		return -1;
	}

	/* 1.inotify_init 建立 inotify 執行個體*/

	mINotifyFd = inotify_init();

	/* 
	每一個 inotify 執行個體對應一個獨立的排序的隊列。
    檔案系統的變化事件被稱做 watches 的一個對象管理,
    每一個 watch 是一個二進制組(目标,事件掩碼),
    目标可以是檔案或目錄,事件掩碼表示應用希望關注的 inotify 事件,
    每一個位對應一個 inotify 事件。Watch 對象通過 watch描述符引用,
    watches 通過檔案或目錄的路徑名來添加。目錄 watches 将傳回在該
    目錄下的所有檔案上面發生的事件。
    */

	/* 2.add watch 添加一個 watch
		int inotify_add_watch(int fd, const char *pathname, uint32_t mask)
		fd: 		fd是inotify_init()傳回的檔案描述符
		pathname: 	pathname是被監視的目标的路徑名,即檔案名或目錄名
		mask:		mask是事件掩碼, 在頭檔案linux/inotify.h中定義了每一位代表的事件
					IN_DELETE:監聽删除
					IN_CREATE:監聽建立
		return value:傳回非負的watch descriptor(監聽描述符)
	*/
	result = inotify_add_watch(mINotifyFd, argv[1], IN_DELETE | IN_CREATE);

	/* read */
	while (1) {
		read_process_inotify_fd(mINotifyFd);
	}

	return 0;
}

           

調試結果: [email protected]:~/code/C$ gcc inotify.c -o inotify.c [email protected]:~/code/C$ mkdir tmp [email protected]:~/code/C$ ./inotify tmp& [email protected]:~/code/C$ touch tmp/test tmp/hello res:32 (int)sizeof(*event):16 event->wd:1 event->mask:0x100 event->len:16 event->name:test create file: test res:32 (int)sizeof(*event):16 event->wd:1 event->mask:0x100 event->len:16 event->name:hello create file: hello [email protected]:~/code/C$ rm tmp/test tmp/hello res:32 (int)sizeof(*event):16 event->wd:1 event->mask:0x200 event->len:16 event->name:test delete file: test res:32 (int)sizeof(*event):16 event->wd:1 event->mask:0x200 event->len:16 event->name:hello delete file: hello