2012-08-28 163 views
2

我介紹自己在驅動程序編程中通過一本書。LINUX kill_pid失敗

這本書是關於2.6內核的Linux驅動程序編程。 但現在我遇到了一個問題。 我從書中複製了以下代碼。 並試圖理解它。到現在爲止還挺好。

#include <linux/module.h> 
#include <linux/version.h> 
#include <linux/init.h> 
#include <linux/completion.h> 


static int thread_id=0; 
static wait_queue_head_t wq; 
static DECLARE_COMPLETION(on_exit); 
static int thread_code(void *data) 
{ 
    unsigned long timeout; 
    int i; 
    daemonize("MyKThread"); 
    allow_signal(SIGTERM); 
    for(i=0; i<10; i++) { 
     timeout=HZ; // wait 1 second 
     timeout=wait_event_interruptible_timeout(
      wq, (timeout==0), timeout); 
     printk("thread_code: woke up ...\n"); 
     if(timeout==-ERESTARTSYS) { 
      printk("got signal, break\n"); 
      break; 
     } 
    } 
    thread_id = 0; 
    complete_and_exit(&on_exit, 0); 
} 
static int __init kthread_init(void) 
{ 
    init_waitqueue_head(&wq); 
    thread_id=kernel_thread(thread_code, NULL, CLONE_KERNEL); 
    printk("thread_id %u",thread_id); 
    if(thread_id==0) 
     return -EIO; 
    return 0; 
} 
static void __exit kthread_exit(void) 
{ 

printk("thread_code: exit ...\n"); 
printk("thread_id %u\n",thread_id); 
    if(thread_id) 
    { 
     kill_pid(thread_id, SIGTERM, 1);//kill_proc --> kill_pid 
    } 
    wait_for_completion(&on_exit); 

} 
module_init(kthread_init); 
module_exit(kthread_exit); 
MODULE_LICENSE("GPL"); 

起初我用kill_pid替換了kill_proc。

但是,如果我運行PROGRAMM和小費rmmod的...我得到這個消息:

thread_code: exit ... 
thread_id 995 
Unable to handle kernel paging request for data at address 0x000003eb 
Faulting instruction address: 0xc0047998 
Oops: Kernel access of bad area, sig: 11 [#1] 
SBC8548 
last sysfs file: 
Modules linked in: bsp_6_9(-) 
NIP: c0047998 LR: c0040540 CTR: c0040578 
REGS: cf987de0 TRAP: 0300 Not tainted (2.6.36) 
MSR: 00029000 <EE,ME,CE> CR: 40000424 XER: 00000000 
DEAR: 000003eb, ESR: 00000000 
TASK = cf9fcdb0[997] 'rmmod' THREAD: cf986000 
GPR00: 00000000 cf987e90 cf9fcdb0 000003e3 00000000 000003e3 c015b6c8 000023fc 
GPR08: 00000034 000003e3 000023fc c0040578 20000422 1004a180 00000240 00000000 
GPR16: 101042f0 1009ea94 100f0000 10043008 00000000 bf91a110 10001bcc bf91a11c 
GPR24: bf91a118 00000000 c02cca7c c02cca88 0000000f 00000001 000003e3 fffffffd 
NIP [c0047998] pid_task+0x18/0x38 
LR [c0040540] kill_pid_info+0x2c/0x64 
Call Trace: 
[cf987e90] [c015b6c8] serial8250_console_putchar+0x0/0x4c (unreliable) 
[cf987eb0] [d102a16c] kthread_exit+0x50/0x124 [bsp_6_9] 
[cf987ed0] [c006026c] sys_delete_module+0x174/0x210 
[cf987f40] [c000dae0] ret_from_syscall+0x0/0x3c 
--- Exception: c01 at 0xff3b2bc 
    LR = 0x10000f80 
Instruction dump: 
8127000c 90890004 3c000020 60000200 90030010 4e800020 2c030000 4082000c 
38600000 4e800020 5480103a 7d230214 <80690008> 2f830000 41beffe8 54892036 
---[ end trace d2ac023715aedf88 ]--- 
Segmentation fault 
-bash-3.2# 

Message from [email protected] at Thu Jan 1 01:00:10 1970 ... 

MPC_Embedded_Linux kernel: last sysfs file: thread_code: woke up ... 

也許發送kill_pid序列是不正確的,但我沒有發現任何解決方案。

感謝您的幫助

彼得

回答

3

kill_pid需要struct pid *作爲第一個參數,而不是一個pid_t(即它需要一個指向進程描述符結構,而不是進程ID)。您可以使用find_vpid等功能查找struct pid *

+0

非常感謝您的快速回復。 – Peter