proc实验

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/sched.h>
#include <linux/uaccess.h>

#define STRINGLEN 1024
char global_buffer[STRINGLEN] = "hello";

struct proc_dir_entry *example_dir, *data_file;

static ssize_t proc_read(struct file *file, char __user *buf, size_t count, loff_t *ppos);
static ssize_t proc_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos);

static const struct file_operations proc_fops = {
        .owner = THIS_MODULE,
        .read = proc_read,
        .write = proc_write,
};

static ssize_t proc_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
        int len;

        if (*ppos > 0)
                return 0;
        printk("----start read-----\n");
        //printk("the string is >>>>> %s\n", global_buffer);

        len = strlen(global_buffer) + 1;
        if (copy_to_user(buf, global_buffer, len))
                return -EFAULT;

        *ppos += len;

        return len;
}

static ssize_t proc_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
{
        int len;

        if (count >= STRINGLEN)
                len = STRINGLEN - 1;
        else
                len = count;
        
        printk("-----start write-----\n");
        
        if (copy_from_user(global_buffer, buf, len))
                return -EFAULT;

        global_buffer[len] = '\0';

        return len;
}


static int __init proc_test_init(void)
{
        example_dir = proc_mkdir("proc_test", NULL);

        //创建proc文件并关联file_operations
        data_file = proc_create("data", 0777, example_dir, &proc_fops);//要创建的设备节点的名称;文件的访问权限;父节点;该文件的操作函数

        printk("proc_test init success!\n");

        return 0;
}

static void __exit proc_test_exit(void)
{
        remove_proc_entry("data", example_dir);
        remove_proc_entry("proc_test", NULL);
}

module_init(proc_test_init);
module_exit(proc_test_exit);

MODULE_LICENSE("GPL");

makefile

KERNEL_DIR=../ebf_linux_kernel/build_image/build

ARCH=arm
CROSS_COMPILE=arm-linux-gnueabihf-
export  ARCH  CROSS_COMPILE

obj-m := proc_test.o
all:
	$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) modules

.PHONE:clean copy

clean:
	$(MAKE) -C $(KERNEL_DIR) M=$(CURDIR) clean	

copy:
	sudo  cp  *.ko  /home/embedfire/workdir

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值