linux中proc权限不够,linux下proc文件的读写

#include#define STRINGLEN 1024

char global_buffer[STRINGLEN];

struct proc_dir_entry *hello_file;

struct proc_dir_entry *example_dir1;

struct proc_dir_entry *example_dir2;

int proc_read_hello(char *page, char **start, off_t off, int count, int *eof,void *data) {

int len;

len = sprintf(page, global_buffer); //把global_buffer的内容显示给访问者

return len;

}

int proc_write_hello(struct file *file, const char *buffer, unsigned long count,void *data) {

unsigned long len;

int ret;

if(count == STRINGLEN){

len = STRINGLEN - 1;

}

else{

len = count;

}

ret = copy_from_user(global_buffer, buffer, len);

global_buffer[len] = '\0';

return len;

}

static int __init proc_test_init(void) {

example_dir1 = proc_mkdir("proc_test1", NULL);

example_dir2 = proc_mkdir("proc_test2", example_dir1);

hello_file = create_proc_entry("hello", S_IRUGO, example_dir2);

strcpy(global_buffer, "hello");

hello_file->read_proc = proc_read_hello;

hello_file->write_proc = proc_write_hello;

return 0;

}

static void __exit proc_test_exit(void) {

remove_proc_entry("hello", example_dir2);

remove_proc_entry("proc_test2", example_dir1);

remove_proc_entry("proc_test1", NULL);

}

module_init(proc_test_init);

module_exit(proc_test_exit);

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值