分享

Ubuntu下module_param()j解释与实例

 无衍 2011-09-17
 

Ubuntu下module_param()j解释与实例

分类: Linux 19人阅读 评论(0) 收藏 举报

1.引入module_param目的。 
          在用户态下编程可以通过main()来传递命令行参数,而编写一个内核模块则可通过module_param()来传递命令行参数. 也就是内核允许对驱动程序在加载的时候传递参数,e.g.insmod hello who="world" times=5 其中who,times 均为要传递的参数变量。


2.module_param()的定义 
         module_param宏是Linux 2.6内核中新增的,该宏被定义在include/linux/moduleparam.h文件中,具体定义如下(我从源码那里找来的http://tomoyo./cgi-bin/lxr/source/include/linux/moduleparam.h):
/**
 77  * module_param - typesafe helper for a module/cmdline parameter
 78  * @value: the variable to alter, and exposed parameter name.          
 79  * @type: the type of the parameter
 80  * @perm: visibility in sysfs.
 81  *
 82  * @value becomes the module parameter, or (prefixed by KBUILD_MODNAME and a
 83  * ".") the kernel commandline parameter.  Note that - is changed to _, so
 84  * the user can use "foo-bar=1" even for variable "foo_bar".
 85  *
 86  * @perm is 0 if the the variable is not to appear in sysfs, or 0444
 87  * for world-readable, 0644 for root-writable, etc.  Note that if it
 88  * is writable, you may need to use kparam_block_sysfs_write() around
 89  * accesses (esp. charp, which can be kfreed when it changes).
 90  *
 91  * The @type is simply pasted to refer to a param_ops_##type and a
 92  * param_check_##type: for convenience many standard types are provided but
 93  * you can create your own by defining those variables.
 94  *
 95  * Standard types are:
 96  *      byte, short, ushort, int, uint, long, ulong
 97  *      charp: a character pointer
 98  *      bool: a bool, values 0/1, y/n, Y/N.
 99  *      invbool: the above, only sense-reversed (N = true).
100  */
101 #define module_param(name, type, perm)                          /
102         module_param_named(name, name, type, perm)
103 
104 /**
105  * module_param_named - typesafe helper for a renamed module/cmdline parameter
106  * @name: a valid C identifier which is the parameter name.
107  * @value: the actual lvalue to alter.
108  * @type: the type of the parameter
109  * @perm: visibility in sysfs.
110  *
111  * Usually it's a good idea to have variable names and user-exposed names the
112  * same, but that's harder if the variable must be non-static or is inside a
113  * structure.  This allows exposure under a different name.
114  */
          其中使用了 3 个参数:要传递的参数变量名, 变量的数据类型, 以及访问参数的权限。
          注:宏的第三个参数用于指定访问权限,如果为 0,该参数将不出现在 sysfs 文件系统中,允许的访问权限为      S_IRUSR,S_IWUSR,S_IRGRP,S_IWGRP,S_IROTH 和S_IWOTH 的组合,它们分别对应于用户读,用户写,用户组读,用户组写,其他用户读和其他用户写,因此用文件的访问权限设置是一致的。
而权限的设置可以参考如下:(也可以自己定义,它类似chmod中权限的使用)
          权限在include/linux/stat.h中有定义:
比如:
#define S_IRWXU 00700
#define S_IRUSR 00400
#define S_IWUSR 00200
#define S_IXUSR 00100

#define S_IRWXG 00070
#define S_IRGRP 00040
#define S_IWGRP 00020
#define S_IXGRP 00010

#define S_IRWXO 00007
#define S_IROTH 00004
#define S_IWOTH 00002
#define S_IXOTH 00001


3.实例 
在ubuntu 10.10下,内核版本2.26.35
/**module_param()**/
/*This is a test to learn how it work*/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("SUZHAODA");
MODULE_DESCRIPTION("Module_param() test");

static char *who = "world";
static int times = 1;


module_param(times,int,S_IRUSR);
module_param(who,charp,S_IRUSR);

static int hello_init(void)
{
    int i;
    for(i = 0;i < times;i++)
        printk(KERN_ALERT "(%d) hello,%s/n",i,who);
         return 0;
}

static void hello_exit(void)
{
    printk(KERN_ALERT "Goodbye,%s!/n",who);
}

module_init(hello_init);
module_exit(hello_exit);

4.makefile 
obj-m := hello.o
KERNELBUILD := /lib/modules/$(shell uname -r)/build

default:
    make -C $(KERNELBUILD) M=$(shell pwd) modules
clean:
    rm -rf *.o *.ko *.mod.c .*.cmd *.markers *.order *.symvers .tmp_version


5.实现

sudo make

sudo insmod hello.ko who="world" times=5

dmesg

 

rmmdo hello

dmesg

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多