2014-08-30 4 views
0

원래 do_fork 주소를 호출하기 전에 내 보낸 심볼 'do_fork'의 주소를 내 함수를 가리 키도록 변경하려고 시도한 모듈을 작성했습니다. 지금까지 나는 좌상 피연산자로 요구되는 lvalue 에러를주는 것처럼 주소를 바꿀 수 없다. '커널 함수 포인터 주소 변경

do_fork() 포인터를 내 함수 fake_fork()로 변경하는 방법을 모르겠습니다.

#include <linux/fs.h> 
#include <linux/init.h> 
#include <linux/proc_fs.h> 
#include <linux/seq_file.h> 
#include <linux/sched.h> 
#include <linux/module.h> 

int c=0; 

long fake_fork(unsigned long a, unsigned long b, unsigned long c, int __user *d, int __user *e) 
{ 
    ++c; 

    return do_fork(a, b, c, d, e); 
} 

EXPORT_SYMBOL(fake_fork); 

static int fork_proc_show(struct seq_file *m, void *v) 
{ 
    seq_printf(m, "System Call fork called: %d times.\n", c); 
    return 0; 
} 

static int fork_proc_open(struct inode *inode, struct file *file) 
{ 
    return single_open(file, fork_proc_show, NULL); 
} 

static const struct file_operations fork_proc_fops = { 
    .open  = fork_proc_open, 
    .read  = seq_read, 
    .llseek  = seq_lseek, 
    .release = single_release, 
}; 

static int __init proc_fork_init(void) 
{ 
    do_fork = fake_fork; // <-- Not working 

    printk("init proc forkcounter\n"); 
    proc_create("forkcounter", 0, NULL, &fork_proc_fops); 
    return 0; 
} 

static void __exit cleanup_fork_module(void) 
{ 
    remove_proc_entry("forkcounter",NULL); 

    printk("cleanup proc forkcounter\n"); 
} 

module_init(proc_fork_init); 
module_exit(cleanup_fork_module); 
+0

'fake_fork()'를 직접 호출해야하는 이유는 무엇입니까? –

답변

3

당신은 do_fork을 변경할 수 없습니다, 그것은 실행시에 상수이다. do_fork() 기능의 주소입니다.

함수에 아무 것도 지정할 수 없습니다. 함수의 이름이 변수가 아니기 때문입니다. 상수 포인터입니다.

그것은

5 = 2 + 2; 

같은 오류 메시지가 얻을 것과 동일합니다.

나는 do_fork()이 호출 될 때마다 함수를 호출하기를 원한다고 가정합니다. 그것을 구현하는 것이 더 복잡해질 것입니다. 예를 들어 this link으로 시작합니다.

+0

답장을 보내 주셔서 감사합니다. 모든 EXPORT_SYMBOL 기호에 대해 동일합니까? 모든 상수이며 변경할 수 없습니까? –

+0

네,하지만 EXPORT_SYMBOL 때문에 안됩니다. 예를 들어,'fake_fork'에 할당 할 수 없지만'EXPORT_SYMBOL (fake_fork)'를 제거하면 여전히 할당 할 수 없습니다. 함수의 이름이 변수가 아니기 때문입니다. 상수 포인터입니다. –

+0

나는 내 대답을 –