看板 LinuxDev 關於我們 聯絡資訊
其實kernel source code是最好的老師,找一個kernel module 看一下就知道 該怎么做了。 我試著解釋一下吧。 首先你要想清楚是打算把A.c,B.c,m.c都compile成不同的.ko檔案,還是 把A.c,B.c,m.c都compile到同一個.ko檔案中。 如果你打算把A.c,B.c,m.c編譯成一個.ko檔案.比如m.ko 你可以再添加兩個文件:A.h,B.h。 在A.h中聲明functionA的prototype,在B.h中聲明functionB的prototype。 在m.c添加如下行: #include "A.h" #include "B.h" (注:A.h和B.h跟A.c,B.c,m.c在同一個目錄下) 。 如果你要把他們compile成不同的.ko檔案。那么為了讓A.c,B.c,m.c中的 funtion能夠彼此呼叫,你需要export functionA和functionB。 ※ 引述《RiverJackson (JACK)》之銘言: : 非常感謝仔細的講解大概有一點sense;:了 : 雖然driver還在摸索中 但是我想表達的架構大概如下(之前可能講錯了= =) : A.c 有functionA 將會用來呼叫 functionB : 這裡的functionA會有while(1) 作polling的動作 : 藉由functionB可以讀到一些值 然後想把這些值寫到 /proc/debug ? : 然後userspace 可能有一支app 去讀/proc/debug然後作一些處理 ? : 不知道這樣的觀念對不對 ? : B.c 有functionB : m.c 編譯成module的檔案 會有一個溝通介面 像是 /proc/debug : 會有一個functionM_init 會去呼叫到functionA : 1.m.c 的funcitonM_init會呼叫到functionA 但是在A.c 並沒有把functionA export出來 : 所以 A.c 看起來是編譯成*.o檔在跟 m.c編譯成module ? : 如果functionA有被export出來的話 表示他是編譯在kernel : 2.在kernel裡面有一些code被compiler 成 module表示 lsmod 就可以看到這些註冊的 : device ? 是的。 : 3.所以產生/proc/debug 可以放到m.c去產生 : 像是debug_entry = create_proc_read_entry(...) : 那我要在A.c 寫東西到 /proc/debug 這裡的話 就卡住了 要用什麼函數 把值丟進去? : 有些觀念還不是很熟 請多多包含:D 下面是一個范例,你可以借鑒一下: #include <linux/kernel.h> #include <linux/module.h> #include <linux/proc_fs.h> #include <linux/init.h> #include <linux/sched.h> MODULE_LICENSE("GPL"); MODULE_AUTHOR("mq110"); static char *buf; static int ps_read(char *page, char **start, off_t offset,int count, int *eo f, void *data) { char tmp[128]; struct task_struct *p; rwlock_t tasklist=RW_LOCK_UNLOCKED; if(offset >0) return 0; memset(buf,0,sizeof(buf)); read_lock(&tasklist); for_each_process(p) { sprintf(tmp,"%d\t\t%d\t\t\t%s\n",p->pid,p->parent->pid,p->co mm); strcat(buf,tmp); memset(tmp,0,sizeof(tmp)); } read_unlock(&tasklist); *start=buf; return strlen(buf); } static __init int ps_init(void) { struct proc_dir_entry *entry; buf = (char *)kmalloc(1024*8,GFP_KERNEL); if(buf == NULL) goto error1; entry = create_proc_entry("_ps", 0444, &proc_root); if(entry == 0) goto error2; entry->mode = S_IFREG | 0444; entry->size = 0; entry->read_proc = ps_read; return 0; error2: kfree(buf); printk(KERN_ERR "create_proc_entry() failed !\n"); return -1; error1: printk(KERN_ERR "kmalloc() failed !\n"); return -1; } static __exit void ps_cleanup(void) { remove_proc_entry("_ps", &proc_root); kfree(buf); } module_init(ps_init); module_exit(ps_cleanup); : ※ 引述《richardhesid (張騫)》之銘言: : : 用語上有些差異,請見諒! : : 我的理解是,你想在A.c中的functionA中call functionB. : : 這取決與你的A.c和B.c是如何compile的。 : : 如果a.c和b.c都是compile into kernel。 : : 你可以在a.c中call funtionB,只要在A.c開始處申明extern functionB(); : : 如果A.c compile as module.你必須在B.c中export symbol functionB。 : : 你必須在B.c中使用EXPORT_SYMBOL(functionB)。 : : 然后同樣在A.c開始處,申明extern functionB(); : : 產生proc跟是否compile as module沒有關系。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 60.191.37.122