Linux内核proc目录下属性文件的驱动示例

在Linux内核中常用的驱动时字符设备驱动,常常在dev目录下创建我们驱动的设备节点,但是在proc目录和sys目录下也有与

我们设备去哦那个相关的属性值,以proc文件的文件属性的驱动为例。

对于proc目录下的驱动编写与字符驱动的编写流程一致,并且文件的结构体也是一样的,只是创捷proc的节点的方式不太一样,他有一个目录的创建,在字符驱动中可能也是有目录的创建,只是笔者没见过而已

驱动示例代码:proc_drv.c

// proc_drv.c

#include <linux/module.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h>
#include <linux/slab.h>

#define IGH_EC_PROC_DIR "test"
#define IGH_GFAR_PROC_NAME "value"

static int test_proc_show(struct seq_file *m, void *v);
static int test_proc_open(struct inode *inode, struct file *file);
static ssize_t test_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *f_pos);
static int test_proc_init(void);
static void test_proc_exit(void);

static int test_proc_show(struct seq_file *m, void *v)
{
    return 0;
}

static int test_proc_open(struct inode *inode, struct file *file)
{
    return single_open(file, test_proc_show, NULL);
}

static ssize_t test_proc_write(struct file *file, const char __user *buffer, size_t count, loff_t *f_pos)
{
#if 0
    char *tmp = kzalloc((count+1), GFP_KERNEL);
    if (!tmp)
        return -ENOMEM;

    if (copy_from_user(tmp,buffer,count)) {
        kfree(tmp);
        return -EFAULT;
    }
    printk("%s Get user str :%s\n", __func__, tmp);
    kfree(tmp);

    return count;
#endif

    size_t ret = count;

    return (ret + 1);
}

ssize_t test_proc_read(struct file *file, char __user *buf, size_t len, loff_t *off)
{
#if 0
    int len_copy, ret;
    char data[32] = "1234566789";

    // fl->f_pos表示当前文件描述符对文件的偏移, len表示用户进程想要读的大小

    if ((file->f_pos + len) > strlen(data)) //如果剩下没读的数据长度少于len,则只复制出剩下没读部分
        len_copy = strlen(data) - file->f_pos;
    else
        len_copy = len; //如果剩下的数据长度超出len,则本次复制len字节

    ret = copy_to_user(buf, data+file->f_pos, len_copy);

    //内容复制后,需要改变文件描述符的位置偏移
    *off += len_copy - ret;  //在read/write函数里必须通过off来改变
    return len_copy - ret;
#endif

    int ret = len;

    return (ret + 1);
}

static struct file_operations test_proc_fops = {
    .owner	= THIS_MODULE,
    .open	= test_proc_open,
    .release    = single_release,
    .read	= test_proc_read,
    .llseek	= seq_lseek,
    .write 	= test_proc_write,
};
struct proc_dir_entry *test_proc_dir = NULL;

static int test_proc_init(void)
{
    struct proc_dir_entry* file;
    test_proc_dir = proc_mkdir(IGH_EC_PROC_DIR, NULL);
    if (test_proc_dir == NULL) {
        printk("%s proc create %s failed\n", __func__, IGH_EC_PROC_DIR);
        return -EINVAL;
    }
    file = proc_create(IGH_GFAR_PROC_NAME, 0777, test_proc_dir, &test_proc_fops);
    if (!file) {
        printk("%s proc_create failed!\n", __func__);
        return -ENOMEM;
    }

    return 0;
}

static void test_proc_exit(void)
{
    remove_proc_entry(IGH_GFAR_PROC_NAME, test_proc_dir);
    remove_proc_entry(IGH_EC_PROC_DIR, NULL);
}

static int __init my_init(void)
{
    test_proc_init();

    return 0;
}

static void __exit my_exit(void)
{
    test_proc_exit();
}

module_init(my_init);
module_exit(my_exit);

MODULE_AUTHOR("wangsheng");
MODULE_LICENSE("GPL");

Makefile

obj-m := proc_drv.o
KERNELDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
modules:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) clean

编译驱动,然后将驱动加载到系统中,就会在/proc/test/产生一个value的属性节点

应用层的测试程序:proc_test.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main (int argc, char *argv[])
{
    int fd = -1;
    int ret = -1;
    char buf[1024] = {0};
    int i = 0;

    // 打开
    fd = open ("/proc/test/value", O_RDWR);
    if (fd < 0) {
        printf ("open error.\n");
        return -1;
    }

    // 写
    ret = write (fd, "abcdefg", strlen ("abcdefg"));
    if (ret < 0) {
        printf ("write error.\n");
        return -1;
    }

    // 读
    ret = read (fd, buf, 16);
    if (ret < 0) {
        printf ("read error.\n");
        return -1;
    }

    for (i = 0; i < 16; ++i) {
        printf ("%.2x ", buf[i]);
    }
    printf ("\n");

    // 关闭
    close (fd);

    return 0;
}

使用用用程序测试驱动程序。

 

如果觉得写得不错,烦请微信搜索公众号 "郑州行疆户外" 了解程序员的户外业余喜好。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值