Add dependecy on another module while building a kernel module - linux

I am trying trying to use an external module that I have written from another kernel module. The second module is the cr_module from the Berkeley lab checkpoint/restart).
So, I create a module A that exports some functions and then created another module B which uses A's functions. I installed the module A so that its headers go with the rest of headers in the linux sources. Module B can not include the headers and link to Module A's functions when I copy the Module.symvers file from Module A sources to Module B sources. This way all goes fine.
However, since blcr uses automake and autconf I can not figure out how to tell kbuild to use Module A's functions. I copied Module A's Module.symvers to all subdirectories in blcr but still it can not link to those functions. Somehow modpost can not see this file (Module.symvers).
Any help figuring this out?
Thanks.

Related

How can I include additional modules in a NodeJS custom binary?

I am building a custom binary of NodeJS from the latest code base for an embedded system. I have a couple modules that I would like to ship as standard with the binary - or even run a custom script the is compiled into the binary and can be invoked through a command line option.
So two questions:
1) I vaguely remember that node allowed to include custom modules during build time but I went through the latest 5.9.0 configure script and I can't see anything related - or maybe I am missing it.
2) Did someone already do something similar? If yes, what were the best practices you came up with?
I am not looking for something like Electron or other binary bundlers but actually building into the node binary.
Thanks,
Andy
So I guess I figure it out much faster that I thought.
For anyone else, you can add any NPM module to it and just add the actual source files to the node.gyp configuration file.
Compile it and run the custom binary. It's all in there now.
> var cmu = require("cmu");
undefined
> cmu
{ version: [Function] }
> cmu.version()
'It worked!'
> `
After studying this for quite a while, I have to say that the flyandi's answer is not quite true. You cannot add any NPM module just by adding it to the node.gyp.
You can only add pure JavaScript modules this way. To be able to embed a C++ module (I deliberately don't use the word "native", because that one is quite ambiguous in nodeJS terminology - just look at the sources).
To summarize this:
To embed a JS module to your custom nodejs, just add it in the library_files section of the node.gyp file. Also note that it should be placed within the lib folder, otherwise you'll have troubles requiring the module. That's because the name/path listed in node.gyp / library_files is used to encode the id of the module in the node_javascript.cc intermediate file which is then used when searching for the built-in modules.
To embed a native module is much more difficult. The best way I have found so far is to build the module as a static library instead of dynamic, which for cmake(-js) based module you can achieve by changing the SHARED parameter to STATIC like this:
add_library(${PROJECT_NAME} STATIC ${SRC})
instead of:
add_library(${PROJECT_NAME} SHARED ${SRC})
And also changing the suffix:
set_target_properties(
${PROJECT_NAME}
PROPERTIES
PREFIX ""
SUFFIX ".lib") /* instead of .node */
Then you can link it from node.gyp by adding this section:
'link_settings': {
'libraries' : [
"path/to/my/library.lib",
#...add other static dependencies
],
},
(how to do this with node-gyp based project should be quite ease to google)
This allows you to build the module, but you won't be able to require it, because require() function in node can only be used to load built-in JS modules, external JS modules or external dynamic node modules. But now we have a built-in C++ module. Well, lot of node integrated modules are C++, but they always have a JS wrapper in /lib, and those wrappers they use process.binding() to load the C++ module. That is, process.binding() is sort of a require() function for integrated C++ modules.
That said, we also need to call require.binding() instead of require to load our integrated module. To be able to do that, we have to make our module "built-in" first.
We can do that by replacing
NODE_MODULE(mymodule, InitAll)
int the module definition with
NODE_BUILTIN_MODULE_CONTEXT_AWARE(mymodule, InitAll)
which will register it as internal module and from now on we can process.binding() it.
Note that NODE_BUILTIN_MODULE_CONTEXT_AWARE is not defined in node.h as NODE_MODULE but in node_internals.h so you either have to include that one, or copy the macro definition over to your cpp file (the first one is of course better because the nodejs API tends to change quite often...).
The last thing we need to do is to list our newly integrated module among the others so that the node knows to initialize them (that is include them within the list of modules used when searching for the modules loaded with process.binding()). In node_internals.h there is this macro:
#define NODE_BUILTIN_STANDARD_MODULES(V) \
V(async_wrap) \
V(buffer) \
V(cares_wrap) \
...
So just add the your module to the list the same way as the others V(mymodule).
I might have forgotten some step, so ask in the comments if you think I have missed something.
If you wonder why would anyone even want to do this... You can come up with several reasons, but here's one most important to me: Those package managers used to pack your project within one executable (like pkg or nexe) work only with node-gyp based modules. If you, like me, need to use cmake based module, the final executable won't work...

Node.js - searching for the "util" module; identify core modules

I'm really a big fan of Swagger's node module, but one thing is driving me crazy:
The sample app contains the following line (api/controller/hello_world.js):
var util = require('util');
But I just can't find this module. I tried to
list it with npm list but nothing
search for it with Spotlight (util.js)
My question is: How can I list the actually loaded modules in nodejs?
TimWolla's answer solves your particular problem with respect to the util module.
As for the more generic question:
How can I list the actually loaded modules in nodejs?
This answer tells you how to list the full filenames of currently loaded non-core modules (spoiler alert: Object.keys(require.cache).
For core modules - modules that come with Node.js itself, such as util - there is no way that I know of that tells you which ones are loaded, but given that they're core, they may all implicitly be loaded, for all we know (and should care).
That said, to test if a given module is a core module, you can use require.resolve(), which returns the input module name as-is (rather than a full filename) in the case of core modules; e.g.:
require.resolve('util') // -> 'util', i.e. NOT A PATH -> core module
Note: When require.resolve() returns a filesystem path, the implication is merely that the module is non-core and can be located; it does not tell you whether or not that module is currently loaded (use require.cache for that).
util is part of the node.js distribution: https://nodejs.org/api/util.html

specify linux kernel module dependency when using jprobe

I am building two linux kernel modules.
The second module (called debugging module hereafter) basically uses jprobe to intercept calling of functions inside the first module (called main module) and prints some states for debugging. They work pretty well. But I got one question on dependency for the debugging module on the main module.
Apparently, the debugging module depends on the main module, as when loading the debugging module without the main module loaded, I got error
"Unknown symbol in module, or unknown parameter"
However, it looks like that modules.dep could not figure it out. By looking at
nm -u <debugging_module.ko>
I did not find any unresolved symbol related to the main module. But jprobe needs function name from main module to intercept, and that is as string assigned for .kp.symbol_name in jprobe structure.
How can we specify dependency in this situation?

do we need both EXPORT_SYMBOL and head file declaration

I am new to the linux kernel. I have searched a little bit of EXPORT_SYMBOL but I still get a little confused. I know it's used to export a variable or function defined in one module to another module. Does that mean by using that, we do not need to include any header file that declares that variable or function? Or are they both needed? If both needed , why do we need to have EXPORT_SYMBOL? Thanks,
Header files are for the compiler. EXPORT_SYMBOL is for the module loader. This allows for proper separation of module code from kernel code.

Insert modules (*.ko) that have circular dependencies

I have three Linux kernel modules (*.ko files). They have circular dependencies like this:
mod1.ko uses functions exported by mod3.ko
mod2.ko uses functions exported by mod1.ko
mod3.ko uses functions exported by mod1.ko and mod2.ko
I cannot load the first mod1.ko file because of "Unknown symbol" error. I also tried two other methods but I got the same error:
load all modules at a time
insmod mod1.ko mod2.ko mod3.ko
Put these files in /lib/modules/kernel_version/my_modules, and run
depmod kernel_version
modprobe mod3
Can anyone help me please! Any suggestions are appreciated. Thanks in advance :)
Well, how about merge those three modules into one?
I encountered the same problem.
Merging the modules was not a good solution in my case.
What I eventaully did was to add use callback function instead of the original function. And to registrate the function in the other module using registration routines (need to use function pointer for that).
This has eliminated the dependency between the modules.
You can then insert the non-dependent module first, and the dependent module afterwards.

Resources