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;
}

使用用用程序測試驅動程序。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章