日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

詳解Linux 2.6內(nèi)核新文件系統(tǒng)變化機(jī)制( 四 )


};
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; i wd_array[i].name = monitored_files[i];
wd = inotify_add_watch(fd, wd_array[i].name, IN_ALL_EVENTS);
if (wd < 0) {
printf("Can"t add watch for %s.n", wd_array[i].name);
exit(-1);
}
wd_array[i].wd = wd;
}
while(len = read(fd, buffer, MAX_BUF_SIZE)) {
offset = buffer;
printf("Some event happens, len = %d.n", len);
event = (struct inotify_event *)buffer;
while (((char *)event - buffer) < len) {
if (event->mask & IN_ISDIR) {
memcpy(strbuf, "Direcotory", 11);
}
else {
memcpy(strbuf, "File", 5);
}
printf("Object type: %sn", strbuf);
for (i=0; i if (event->wd != wd_array[i].wd) continue;
printf("Object name: %sn", wd_array[i].name);
break;
}
printf("Event mask: Xn", event->mask);
for (i=0; i if (event_array[i][0] == "") continue;
if (event->mask & (1< printf("Event: %sn", event_array[i]);
}
}
tmp_len = sizeof(struct inotify_event)event->len;
event = (struct inotify_event *)(offsettmp_len)
offset= tmp_len;
}
}
}

該程序?qū)⒈O(jiān)視發(fā)生在當(dāng)前目錄下的文件 tmp_file 與當(dāng)前目錄下的目錄 tmp_dir 上的所有文件系統(tǒng)事件,同時(shí)它也將監(jiān)視發(fā)生在文件 /mnt/sda3/windows_file 上的文件系統(tǒng)事件,注意,/mnt/sda3 是 SATA 硬盤(pán)分區(qū) 3 的掛接點(diǎn) 。
細(xì)心的讀者可能注意到,該程序首部使用 _syscallN 來(lái)聲明 inotify 系統(tǒng)調(diào)用,原因是這些系統(tǒng)調(diào)用是在最新的穩(wěn)定內(nèi)核 2.6.13 中引入的,glibc 并沒(méi)有實(shí)現(xiàn)這些系統(tǒng)調(diào)用的庫(kù)函數(shù)版本,因此,為了能在程序中使用這些系統(tǒng)調(diào)用,必須通過(guò) _syscallN 來(lái)聲明這些新的系統(tǒng),其中的 N 為要聲明的系統(tǒng)調(diào)用實(shí)際的參數(shù)數(shù) 。還有需要注意的地方是系統(tǒng)的頭文件必須與被啟動(dòng)的內(nèi)核匹配,為了讓上面的程序能夠成功編譯,必須讓 2.6.13 的內(nèi)核頭文件(包括 include/linux/*, include/asm/* 和 include/asm-generic/*)在頭文件搜索路徑內(nèi),并且是第一優(yōu)先搜索的頭文件路徑,因?yàn)?_syscallN 需要用到這些頭文件中的 linux/unistd.h 和 asm/unistd.h,它們包含了 inotify 的三個(gè)系統(tǒng)調(diào)用的系統(tǒng)調(diào)用號(hào) __NR_inotify_init、__NR_inotify_add_watch 和 __NR_inotify_rm_watch 。
因此,要想成功編譯此程序,只要把用戶編譯好的內(nèi)核的頭文件拷貝到該程序所在的路徑,并使用如下命令編譯即可:

$gcc -o inotify_example; -I. inotify_example.c

注意:當(dāng)前目錄下應(yīng)當(dāng)包含 linux、asm 和 asm-generic 三個(gè)已編譯好的 2.6.13 內(nèi)核的有文件目錄,asm 是一個(gè)鏈接,因此拷貝 asm 頭文件的時(shí)候需要拷貝 asm 與 asm-ARCH(對(duì)于 x86 平臺(tái)應(yīng)當(dāng)是 asm-i386) 。然后,為了運(yùn)行該程序,需要在當(dāng)前目錄下創(chuàng)建文件 tmp_file 和目錄 tmp_dir,對(duì)于/mnt/sda3/windows_file 文件,用戶需要依自己的實(shí)際情況而定,可能是/mnt/dosc/windows_file,即 /mnt/dosc 是一個(gè) FAT32 的 windows 硬盤(pán),因此用戶在編譯該程序時(shí)需要根據(jù)自己的實(shí)際情況來(lái)修改 /mnt/sda3 。Windows_file 是在被 mount 硬盤(pán)上創(chuàng)建的一個(gè)文件,為了運(yùn)行該程序,它必須被創(chuàng)建 。

推薦閱讀