这是简单的sys_call_table挂钩代码

#include <asm/unistd.h>
#include <linux/autoconf.h>
#include <linux/in.h>
#include <linux/init_task.h>
#include <linux/ip.h>
#include <linux/kernel.h>
#include <linux/kmod.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/skbuff.h>
#include <linux/stddef.h>
#include <linux/string.h>
#include <linux/syscalls.h>
#include <linux/tcp.h>
#include <linux/types.h>
#include <linux/unistd.h>
#include <linux/version.h>
#include <linux/workqueue.h>

ssize_t *sys_call_table = (ssize_t *)0xc0026e04;

asmlinkage ssize_t (*orig_open)(const char *pathname, int flags);

asmlinkage ssize_t hacked_open(const char *pathname, int flags)
{
    printk(KERN_INFO "SYS_OPEN called : %s\n", pathname);
    return orig_open(pathname, flags);
}

int init_module(void)
{
    orig_open = sys_call_table[__NR_open];      /* line 33 */
    sys_call_table[__NR_open] = hacked_open;    /* line 34 */
    return 0;
}

void cleanup_module(void)
{
    sys_call_table[__NR_open] = orig_open;      /* line 40 */
}

MODULE_LICENSE("GPL");

我收到如下警告
此代码工作正常,但我想删除警告。我该怎么办?
/home/tester/tools/lkm/a.c:在函数“init_module”中:
/home/tester/tools/lkm/a.c:33:警告:赋值使指针中的整数不带强制转换
/home/tester/tools/lkm/a.c:34:警告:赋值使指针中的整数不带强制转换
/home/tester/tools/lkm/a.c:在函数“cleanup_module”中:
/home/tester/tools/lkm/a.c:40:警告:赋值使指针中的整数不带强制转换

最佳答案

如果你想让你的编译器沉默,你必须添加类型转换(即使这通常是个坏主意,这是你的编译器如何转换它)。

ssize_t *sys_call_table = (ssize_t *)0xc0026e04;

typedef ssize_t (*ftype)(const char *, int);

ftype orig_open;

ssize_t hacked_open(const char *pathname, int flags)
{
    printf("SYS_OPEN called : %s\n", pathname);
    return orig_open(pathname, flags);
}

int init_module(void)
{
    orig_open = (ftype)sys_call_table[__NR_open];
    sys_call_table[__NR_open] = (ssize_t)hacked_open;
    return 0;
}

关于c - linux内核模块分配使指针的整数不进行强制转换,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13323455/

10-13 07:27
查看更多