如何在加载模块以处理open()时避免错误?

时间:2019-04-22 21:38:59

标签: c linux linux-kernel system-calls kernel-module

我正在尝试加载以下代码:

#include <linux/module.h>
#include <linux/kernel.h>
#include <sys/syscall.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <asm/uaccess.h>

extern void *sys_call_table[];
int (*orig_open)(const char *pathname, int flag, int mode);

int own_open(const char *pathname, int flag, int mode)
{
    char *kernel_path;
    char hide[]="test.txt";

    kernel_path=(char *)kmalloc(255,GFP_KERNEL);
    copy_from_user(kernel_path, pathname, 255);

    if(strstr(kernel_path,(char *)&hide) != NULL) {

    kfree(kernel_path);
    return -ENOENT;
    }

    else {

    kfree(kernel_path);
    return orig_open(pathname, flag, mode);
    }
}
int init_module()
{
    orig_open=sys_call_table[SYS_open];
    sys_call_table[SYS_open]=own_open;
    return 0;
}

void cleanup_module()
{
    sys_call_table[SYS_open]=orig_open;
}

但是结果是我得到了错误:

sys_open_call.c:34:30: error: ‘__NR_open’ undeclared (first use in this function)
     orig_open=sys_call_table[SYS_open];
                              ^

sys_open_call.c: In function ‘cleanup_module’:
sys_open_call.c:41:20: error: ‘__NR_open’ undeclared (first use in this function)
     sys_call_table[SYS_open]=orig_open;
                    ^

make: *** [sys_open_call.o] Error 1

如何声明它?

0 个答案:

没有答案