• proc增加节点的一个例子


    procfs作为linux的一种伪文件系统,主要用于内核与用户层交换信息;

    在linux中的/proc就是作为procfs使用,该目录下面有cpu,mtd,gio等固有的节点等,如果用cat命令去查看这些文件,就能够看到这些文件所代表设备的相关信息;

    同时procfs也可以提供给用户层写的操作,只要在相关文件驱动中支持就可以;

    下面是一个简单的添加一个proc节点的例子,参考 http://www.tldp.org/LDP/lkmpg/2.6/html/x710.html

    【环境】

    内核用的是TI的2.6.37_IPNC_DM365_4.0.0

    【Makefile】

    #TARGET = cmemk.ko

    obj-m += myproc.o

    LINUXKERNEL_INSTALL_DIR = /home/mao/workroom/build_for_nand/psp/trunk/ipnc_psp_03_21_00_04/ti-davinci
    MAKE_ENV = ARCH=arm CROSS_COMPILE=arm_v5t_le-

    debug: EXTRA_CFLAGS += -D__DEBUG

    .PHONY: clean debug release install default

    default: debug

    docs:

    release:
    make -C $(LINUXKERNEL_INSTALL_DIR) M=`pwd` $(MAKE_ENV) \
    EXTRA_CFLAGS="$(EXTRA_CFLAGS)"

    debug:
    make -C $(LINUXKERNEL_INSTALL_DIR) M=`pwd` $(MAKE_ENV) \
    EXTRA_CFLAGS="$(EXTRA_CFLAGS)" modules

    install:
    install -d $(EXEC_DIR)

    clean:
    -rm -rf *.o *mod* *.ko .cmem* .tmp*

    【源码】

    #include <linux/module.h>
    #include <linux/moduleparam.h>
    #include <linux/kernel.h>
    #include <linux/timer.h>
    #include <linux/string.h>
    #include <linux/slab.h>
    #include <linux/blkdev.h>
    #include <linux/delay.h>
    #include <linux/init.h>
    #include <linux/completion.h>
    #include <linux/unistd.h>
    #include <linux/spinlock.h>
    #include <linux/kmod.h>
    #include <linux/interrupt.h>
    #include <linux/notifier.h>
    #include <linux/cpu.h>
    #include <linux/mutex.h>
    #include <linux/proc_fs.h>

    #define procfs_name "helloworld"

    /**
    * This structure hold information about the /proc file
    *
    */
    struct proc_dir_entry *Our_Proc_File;

    /* Put data into the proc fs file.
    *
    * Arguments
    * =========
    * 1. The buffer where the data is to be inserted, if
    * you decide to use it.
    * 2. A pointer to a pointer to characters. This is
    * useful if you don't want to use the buffer
    * allocated by the kernel.
    * 3. The current position in the file
    * 4. The size of the buffer in the first argument.
    * 5. Write a "1" here to indicate EOF.
    * 6. A pointer to data (useful in case one common
    * read for multiple /proc/... entries)
    *
    * Usage and Return Value
    * ======================
    * A return value of zero means you have no further
    * information at this time (end of file). A negative
    * return value is an error condition.
    *
    * For More Information
    * ====================
    * The way I discovered what to do with this function
    * wasn't by reading documentation, but by reading the
    * code which used it. I just looked to see what uses
    * the get_info field of proc_dir_entry struct (I used a
    * combination of find and grep, if you're interested),
    * and I saw that it is used in <kernel source
    * directory>/fs/proc/array.c.
    *
    * If something is unknown about the kernel, this is
    * usually the way to go. In Linux we have the great
    * advantage of having the kernel source code for
    * free - use it.
    */
    int
    procfile_read(char *buffer,
    char **buffer_location,
    off_t offset, int buffer_length, int *eof, void *data)
    {
    int ret;

    printk(KERN_INFO "procfile_read (/proc/%s) called\n", procfs_name);

    /*
    * We give all of our information in one go, so if the
    * user asks us if we have more information the
    * answer should always be no.
    *
    * This is important because the standard read
    * function from the library would continue to issue
    * the read system call until the kernel replies
    * that it has no more information, or until its
    * buffer is filled.
    */
    if (offset > 0) {
    /* we have finished to read, return 0 */
    ret = 0;
    } else {
    /* fill the buffer, return the buffer size */
    ret = sprintf(buffer, "HelloWorld!\n");
    }

    return ret;
    }

    int init_module()
    {
    Our_Proc_File = create_proc_entry(procfs_name, 0644, NULL);

    if (Our_Proc_File == NULL) {
    remove_proc_entry(procfs_name, NULL);
    printk(KERN_ALERT "Error: Could not initialize /proc/%s\n",
    procfs_name);
    return -ENOMEM;
    }

    Our_Proc_File->read_proc = procfile_read;
    //Our_Proc_File->owner = THIS_MODULE;
    Our_Proc_File->mode = S_IFREG | S_IRUGO;
    Our_Proc_File->uid = 0;
    Our_Proc_File->gid = 0;
    Our_Proc_File->size = 37;

    printk(KERN_INFO "/proc/%s created\n", procfs_name);
    return 0; /* everything is ok */
    }

    void cleanup_module()
    {
    remove_proc_entry(procfs_name, NULL);
    printk(KERN_INFO "/proc/%s removed\n", procfs_name);
    }

  • 相关阅读:
    【实用代码】圆弧边框、阴影
    2015.7.30 第十五课 sql(新建数据库、创建表、注释、查询语句、新增、更新、删除、联合查询)
    【转】WEB前端开发工程师成长之路(计划)
    2015.7.29 第十三、十四课 jq实例(磁力图片、轮播图、腾讯课堂菜单、可弹出红色菜单、砸蛋游戏)
    【周末作业】2015.7.15 第二周 css作业及答案
    【课堂实例】轮播图
    2015.7.23 第十二课 课程重点(jq元素:获取、添加、删除、设置、绑定、显示隐藏、回调、动画)
    2015.7.22 第十一课 课程重点(jq:元素获取、添加、删除、过滤、判断、遍历、取值,样式设置、改变对象、切换、判断)
    【其它】喜欢的歌汇总
    2015.7.21 第十课 课程重点(jq选择器:id、类、标签、奇偶、eq、nth-child、子元素、属性、包含、位置、过滤)
  • 原文地址:https://www.cnblogs.com/bluebbc/p/2737573.html
Copyright © 2020-2023  润新知