附上一个以前另一论坛上的贴,也许会有帮助:
各位大侠,我用sys_open和sys_read怎么读不出东西来呀?用sys_open函数返回什么值是成功打开文件?我运行了一次,返回值是 -14.我的源代码如下,请各位老兄会诊一下:
#define MODULE
#include
#include
#include
#include
#include
#include
/*#include */
#include
#include
#include
#include
#include
#define special_file(m) (S_ISCHR(m)||S_ISBLK(m)||S_ISFIFO(m)||S_ISSOCK(m))
asmlinkage long sys_open(const char * filename, int flags, int mode)
{
char * tmp;
int fd, error;
#if BITS_PER_LONG != 32
flags |= O_LARGEFILE;
#endif
tmp = getname(filename);
fd = PTR_ERR(tmp);
if (!IS_ERR(tmp)) {
fd = get_unused_fd();
if (fd >= 0) {
struct file *f = filp_open(tmp, flags, mode);
error = PTR_ERR(f);
if (IS_ERR(f))
goto out_error;
fd_install(fd, f);
}
out:
putname(tmp);
}
return fd;
out_error:
put_unused_fd(fd);
fd = error;
goto out;
}
int init_module(void)
{
/*struct file *fd;*/
int fd;
char buffer[60]=\"the buffer is\";
if((fd=sys_open(\"/root/chengyw/debug/data-debug\",0x01,0))!=0)
{
printk(\"open successfuly fd=%d \\n\",fd);
sys_read(fd,buffer,60);
printk(\"the buffer is\\n%s\\n\",buffer);
}
else
{printk(\"open error\\n\");}
return 0;
}
void cleanup_module(void)
{
/* if (flag == 1)
fput(\"/root/chengyw/debug/data-debug\");
*/
printk(\"Goodbye\\n\");
}
答复:
kernel-to-kernel system calls must be wrapped by the following
code:
mm_segment_t fs;
fs = get_fs(); /* save previous value */
set_fs (get_ds()); /* use kernel limit */
/* system calls can be invoked */
set_fs(fs); /* restore before returning to
user space */]
There\'s no need to wrap each individual system call, so several
calls can be performed between set_fs()...set_fs() pairs. It\'s
important, however, that the original fs is restored before
returning to user space. Otherwise, the user program that executed
this code will retain permission to overwrite kernel memory by
passing bogus pointers to further read (or ioctl) system calls.