LINUX kill_pid fails

2

I introduce my self in the driver programming via a book.

This book is about Linux-driver programming with a 2.6 Kernel. But now I met a problem. I copied the following code from the book. And tried to understand it. So far, so good.

#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");

At first I replaced kill_proc with kill_pid.

But if I run the programm and tip rmmod ... I get this message:

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 syslogd@ at Thu Jan  1 01:00:10 1970 ...

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

Maybe the send kill_pid sequence is not right, but I found no solution.

c
linux
linux-kernel
linux-device-driver
embedded-linux
asked on Stack Overflow Aug 28, 2012 by Peter • edited Dec 26, 2019 by red0ct

1 Answer

3

kill_pid takes a struct pid * as the first argument, not a pid_t (i.e. it takes a pointer to a process descriptor structure, not the process ID). You can look up the struct pid * using a function like find_vpid.

answered on Stack Overflow Aug 28, 2012 by nneonneo

User contributions licensed under CC BY-SA 3.0