📄 module_parameters.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head> <title></title> <link rel="stylesheet" media="screen" type="text/css" href="./style.css" /> <link rel="stylesheet" media="screen" type="text/css" href="./design.css" /> <link rel="stylesheet" media="print" type="text/css" href="./print.css" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><a href=start.html>start</a></br><h4><a name="kernel_module_parameters" id="kernel_module_parameters">Kernel Module Parameters</a></h4><div class="level4"><p>A Kernel Module Parameter is an effective way of defining variables within a module that can be altered at load time without the need to recompile the module. The 2.6 Kernel has also given us a means to adjust these parameters while the system is running.</p><p>A parameter used to be defined using the MODULE_PARM macro. Apart from missing an A in PARAM this macro was restricted in what you could do when setting up a variable to be used.</p><p>Now a new way of setting up a module parameter is to use a new macro:</p><p> module_param(name, type, perm)</p><ul><li class="level1"><div class="li"> name is the variable name</div></li><li class="level1"><div class="li"> type defines the variable type which can now be “user defined”.</div></li><li class="level1"><div class="li"> perm controls the visibility and access of the param in sysfs</div></li></ul><p>A code example helps: </p><pre class="code c"><span class="co1">// define the parameter</span><span class="kw4">static</span> <span class="kw4">int</span> debug; <span class="co1">// use this to turn it into a param</span>module_param<span class="br0">(</span>debug, <span class="kw4">int</span>, <span class="nu0">666</span><span class="br0">)</span>; <span class="co1">//or this</span>module_param<span class="br0">(</span>debug, <span class="kw4">int</span>, S_IRUGO | S_IWUSR <span class="br0">)</span> </pre><p>Assuming that sysfs has been mounted on /sys as follows </p><pre class="code"> mount -t sysfs sysfs /sys</pre><p> Look at the file system entry </p><pre class="code">ls /sys/module/mymodule/parametersdebug</pre><p> Set the value </p><pre class="code">echo 23 -n > /sys/module/mymodule/parameters/debug </pre><p> Inspect the value </p><pre class="code">cat /sys/module/mymodule/parameters/debug23</pre><p> This is a very useful way to add the ability to modify module behavior both at load and run time.</p></div><h4><a name="user_defined_parameter_types" id="user_defined_parameter_types">User Defined Parameter Types</a></h4><div class="level4"><p> You can also create your own variable types. To do this you need to define functions to allow the module loader to understand how to parse and present the data in the parameter.</p><p>Lets call a type <strong>newtype</strong></p><p>The three functions that need to be defined are: </p><ul><li class="level1"><div class="li"> param_set_newtype - parse the string to set the type</div></li><li class="level1"><div class="li"> param_get_newtype - show the meaning of the param structure as a string</div></li><li class="level1"><div class="li"> param_check_newtype - check the variable type</div></li></ul><p> A small code example may help:</p><pre class="code c"> <span class="kw4">struct</span> newtype <span class="br0">{</span> <span class="kw4">int</span> test; <span class="kw4">int</span> val; <span class="br0">}</span>; <span class="co2">#define param_check_newtype(name, p)__param_check(name, p, newtype)</span> <span class="kw4">int</span> param_set_newtype<span class="br0">(</span><span class="kw4">const</span> <span class="kw4">char</span> * val, <span class="kw4">struct</span> kernel_param *par <span class="br0">)</span> <span class="br0">{</span> <span class="kw4">struct</span> newtype * nt = <span class="br0">(</span><span class="kw4">struct</span> newtype *<span class="br0">)</span> par; <span class="kw1">if</span> <span class="br0">(</span>strncmp<span class="br0">(</span>val,<span class="st0">"on"</span>,<span class="nu0">2</span><span class="br0">)</span>==<span class="nu0">0</span><span class="br0">)</span> <span class="br0">{</span> nt->test = <span class="nu0">1</span>; <span class="br0">}</span> <span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">(</span>strncmp<span class="br0">(</span>val,<span class="st0">"off"</span>,<span class="nu0">3</span><span class="br0">)</span> <span class="br0">{</span> nt->test = <span class="nu0">2</span>; <span class="br0">}</span> <span class="kw1">else</span> <span class="br0">{</span> nt->test = <span class="nu0">0</span>; <span class="br0">}</span> <span class="kw1">return</span> <span class="nu0">0</span>;<span class="br0">}</span> <span class="kw4">int</span> param_get_newtype<span class="br0">(</span><span class="kw4">char</span> * buf, <span class="kw4">struct</span> kernel_param *par <span class="br0">)</span> <span class="br0">{</span> <span class="kw4">struct</span> newtype * nt = <span class="br0">(</span><span class="kw4">struct</span> newtype *<span class="br0">)</span> par; <span class="kw4">int</span> len = <span class="nu0">0</span>; <span class="kw1">switch</span> <span class="br0">(</span> nt->test <span class="br0">)</span> <span class="br0">{</span> <span class="kw1">case</span> <span class="nu0">1</span>: len += sprintf<span class="br0">(</span>buf,<span class="st0">"ON"</span><span class="br0">)</span>; <span class="kw2">break</span>; <span class="kw1">case</span> <span class="nu0">2</span>: len += sprintf<span class="br0">(</span>buf,<span class="st0">"Off"</span><span class="br0">)</span>; <span class="kw2">break</span>; <span class="kw1">default</span>: len += sprintf<span class="br0">(</span>buf,<span class="st0">"DUNNO?"</span><span class="br0">)</span>; <span class="kw2">break</span>; <span class="br0">}</span> <span class="kw1">return</span> len;<span class="br0">}</span> <span class="kw4">struct</span> newtype mypar; module_param <span class="br0">(</span>mypar, newtype, <span class="nu0">0666</span> <span class="br0">)</span>; </pre><p><strong>Big Bonus</strong></p><p>If this module is statically compiled into the kernel then the parameter is still available as a boot time option in the kernel command line. </p><pre class="code"> mymodule.test=OFF</pre></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -