内核态下进行read/write系统调用的问题
写了一个模块,想在模块初始化时调用本地文件中的一些配置信息
但不知内核态下能不能调用open(),read()这些文件操作函数
写了一个小程序想验证,编译没有问题,但插入设备时报错:
[root@Vanillasky temp]# /sbin/insmod temp.o
temp.o: unresolved symbol errno
[root@Vanillasky temp]#
问题就出在这个errno,先看看我的程序吧(很简单的):
#if defined(CONFIG_SMP)
#define __SMP__
#endif
#if defined(CONFIG_MODVERSIONS)
#define MODVERSIONS
#include <linux/modversions.h>
#endif
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/stat.h>
#include <linux/fcntl.h>
#define BUFLEN 50
MODULE_LICENSE("GPL");
#define __KERNEL_SYSCALLS__
#include <linux/unistd.h>
int init_module(void)
{
int fd,n;
char* buffer=NULL;
fd=open("/home/sophia/module/Hello",O_RDWR,S_IRWXU);
if((n=read(fd,buffer,BUFLEN))<0) printk(KERN_DEBUG "Sophia: Read file error!\n");
printk(KERN_DEBUG "Sophia: Hello, kernel!\n");
printk(KERN_DEBUG "%s",buffer);
return 0;
}
void cleanup_module(void)
{
printk(KERN_DEBUG "Sophia: Good-bye, kernel!\n");
}
编译:
gcc -D__KERNEL__ -I/usr/src/linux-2.4.20-8/include/ -DMODULE -Wall -O2 -c temp.c -o temp.o
没有错误
我的程序本来是没有定义errno的。后来查了查,在linux/unistd.h里面有定义
extern int errno;但我include了这个文件的呀!
真搞不懂,怎么定义了的变量都会被系统认为是unresolved symbol?
后来看了一下/proc/ksyms,发现跟errno有关的只有这一行:
c88143d0 journal_errno_R1f65a45f [jbd]
不知道跟我出现的问题有没有关系。
请大家不吝赐教,多谢!!