<div dir="ltr"><div class="gmail_default" style><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif">Hi,</font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><br></font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif">during the re-factoring the ipfw2 module, i want to add a logic to determine whether the module is allowed to be loaded into system. so i added a piece of code in MOD_LOAD.</font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><br></font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif">but if i create a test module with below code in "modevent"</font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><br></font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><span class="" style="white-space:pre"> </span>switch (type) {</font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><span class="" style="white-space:pre"> </span>case MOD_LOAD:</font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><span class="" style="white-space:pre"> </span>err = EEXISTS;</font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><span class="" style="white-space:pre"> </span>break;</font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><span class="" style="white-space:pre"> </span>case MOD_UNLOAD:</font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><br></font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><span class="" style="white-space:pre"> </span>break;</font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><span class="" style="white-space:pre"> </span>default:</font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><br></font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif">that means it will do nothing just return "load error" and the error code is "file exists" or "module exists".</font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><br></font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif">so when we loaded it , the module will not be loaded. but when we run kldstat, we can see the module named registered.</font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><br></font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif">below is what I found.</font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><br></font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif">when we kldload AAA module, actually it is calling functions in 3 layers</font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif">syscall ----- linker -------module</font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><br></font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif">linker_load_file and linker_unload_file functions (in kern_linker.c)are used to load/unload the module, it will register the name in an array named linker_files. ( when we kldstat, actually is printing this array, i think )</font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><br></font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif">and linker... will call module_register ( in kern_module.c ). </font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><br></font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif">anway, the module was loaded in function module_register_init line 112 of kern_modules.c</font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><br></font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><span class="" style="white-space:pre"> </span>error = MOD_EVENT(mod, MOD_LOAD);</font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><span class="" style="white-space:pre"> </span>if (error) {</font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><span class="" style="white-space:pre"> </span>module_unload(mod);<span class="" style="white-space:pre"> </span>/* ignore error */</font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><span class="" style="white-space:pre"> </span>module_release(mod);</font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><span class="" style="white-space:pre"> </span>kprintf("module_register_init: MOD_LOAD (%s, %lx, %p) error %d\n",</font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><span class="" style="white-space:pre"> </span> </font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><br></font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif">so when the MOD_LOAD return error, it will call module_unload and module_release.</font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><br></font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif">but this function was invoked by the linker, and this function never return any information, so the linker will never know what is going on inside the function.</font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><br></font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif">so the module is still registered inside the linker_files. i did not spend much time on this, so that is all about what i found. </font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><br></font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif"><br></font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif">Regards,</font></div><div class="gmail_default" style><font color="#000000" face="verdana, sans-serif">Bill Yuan</font></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 8 February 2015 at 21:05, bycn82 <span dir="ltr"><<a href="mailto:bycn82@gmail.com" target="_blank">bycn82@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small;color:rgb(0,0,0)"><i>Hi,</i></div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small;color:rgb(0,0,0)"><br></div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small;color:rgb(0,0,0)"><i>during the re-factoring the ipfw2 module, i want to add a logic to determine whether the module is allowed to be loaded into system. so i added a piece of code in MOD_LOAD.</i></div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small;color:rgb(0,0,0)"><i><br></i></div><div class="gmail_default" style="font-family:verdana,sans-serif;font-size:small;color:rgb(0,0,0)"><i>but if i create a test module with below code in "modevent"</i></div><div class="gmail_default"><font face="verdana, sans-serif"><i><div class="gmail_default" style="color:rgb(0,0,0)"> switch (type) {</div><div class="gmail_default" style="color:rgb(0,0,0)"> case MOD_LOAD:</div><div class="gmail_default" style="color:rgb(0,0,0)"> err = EEXISTS;</div><div class="gmail_default" style="color:rgb(0,0,0)"> break;</div><div class="gmail_default" style="color:rgb(0,0,0)"> case MOD_UNLOAD:</div><div class="gmail_default" style="color:rgb(0,0,0)"><br></div><div class="gmail_default" style="color:rgb(0,0,0)"> break;</div><div class="gmail_default" style="color:rgb(0,0,0)"> default:</div><div class="gmail_default" style="color:rgb(0,0,0)"><br></div><div class="gmail_default" style="color:rgb(0,0,0)">that means it will do nothing just return "load error" and the error code is "file exists" or "module exists".</div><div class="gmail_default" style="color:rgb(0,0,0)"><br></div><div class="gmail_default"><font color="#000000">so </font><font color="#ff0000">when we loaded it , the module will not be loaded. but when we run kldstat, we can see the module named registered.</font></div><div class="gmail_default" style="color:rgb(0,0,0)"><br></div><div class="gmail_default" style="color:rgb(0,0,0)">below is what I found.</div><div class="gmail_default" style="color:rgb(0,0,0)"><br></div><div class="gmail_default" style="color:rgb(0,0,0)">when we kldload AAA module, actually there are </div><div class="gmail_default" style="color:rgb(0,0,0)">kldload syscall ----- linker -------module</div><div class="gmail_default" style="color:rgb(0,0,0)"><br></div><div class="gmail_default"><font color="#000000">linker_load_file and </font><i><div class="gmail_default" style="display:inline!important"><font color="#000000">linker_unload_file functions (in kern_linker.c)are used to load/unload the module, it will register the name in an array named linker_files. ( when we kldstat, actually is printing this array, i think )</font></div></i></div><div class="gmail_default"><i><div class="gmail_default" style="display:inline!important"><font color="#000000"><br></font></div></i></div><div class="gmail_default"><i><div class="gmail_default" style="display:inline!important"><font color="#000000">and linker... will call module_register ( in kern_module.c ). </font></div></i></div><div class="gmail_default"><i><div class="gmail_default" style="display:inline!important"><font color="#000000"><br></font></div></i></div><div class="gmail_default"><i><div class="gmail_default" style="display:inline!important"><font color="#000000">anway, the module was loaded in function </font></div></i><font color="#000000">module_register_init line 112 of kern_modules.c</font></div><div class="gmail_default"><font color="#000000"><br></font></div><div class="gmail_default"><font color="#000000"><div class="gmail_default"> error = MOD_EVENT(mod, MOD_LOAD);</div><div class="gmail_default"> if (error) {</div><div class="gmail_default"><span style="white-space:pre-wrap"> </span>module_unload(mod);<span style="white-space:pre-wrap"> </span>/* ignore error */</div><div class="gmail_default"><span style="white-space:pre-wrap"> </span>module_release(mod);</div><div class="gmail_default"><span style="white-space:pre-wrap"> </span>kprintf("module_register_init: MOD_LOAD (%s, %lx, %p) error %d\n",</div><div class="gmail_default"><span style="white-space:pre-wrap"> </span> </div><div class="gmail_default"><br></div><div class="gmail_default"><br></div><div class="gmail_default">so when the MOD_LOAD return error, it will call <i style="color:rgb(34,34,34)"><div class="gmail_default" style="display:inline!important"><i><div class="gmail_default" style="display:inline!important"><font color="#000000"><div class="gmail_default" style="display:inline!important">module_unload and </div></font></div></i></div></i><i style="color:rgb(34,34,34)"><div class="gmail_default" style="display:inline!important"><i><div class="gmail_default" style="display:inline!important"><font color="#000000"><div class="gmail_default" style="display:inline!important">module_release.</div></font></div></i></div></i></div><div class="gmail_default"><br></div><div class="gmail_default">but this function was invoked by the linker, and this function never return any information, so the linker will never know what is going on inside the function.</div><div class="gmail_default"><br></div><div class="gmail_default">so the module is still registered inside the linker_files. i did not spend much time on this, so that is all about what i found. </div><div class="gmail_default"><br></div><div class="gmail_default"><br></div><div class="gmail_default">Regards,</div><div class="gmail_default">Bill Yuan</div><div class="gmail_default"><br></div><div class="gmail_default"><br></div><div class="gmail_default"><br></div><div class="gmail_default"><br></div><div class="gmail_default"><br></div></font></div></i></font></div></div>
</blockquote></div><br></div>