linux+sys_open,请问,linux 驱动程序中如何读取文件,我曾用sys_open试图打开一个文件,但死机了,请大侠指教,谢谢!!!...

附上一个以前另一论坛上的贴,也许会有帮助:

各位大侠,我用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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值