3

I wrote a kernel module that creates a entry in /proc/ and does some other tasks. I want to modify an existing kernel module to check if my module is running and execute some sentences depending on it (or execute others in case it is not running)

Any advice on how to do this?

jeanc
  • 4,563
  • 4
  • 22
  • 27

1 Answers1

4

kernel/module.c provides a function that will probably do what you need; you first need to lock module_mutex and then call find_module() with the name of your module. The result will be a pointer to a struct module that describes the named module -- or NULL if the module is not loaded:

/* Search for module by name: must hold module_mutex. */
struct module *find_module(const char *name)
{
        struct module *mod;

        list_for_each_entry(mod, &modules, list) {
                if (strcmp(mod->name, name) == 0) 
                        return mod;
        }
        return NULL;
}
EXPORT_SYMBOL_GPL(find_module);
sarnold
  • 102,305
  • 22
  • 181
  • 238
  • 1
    I have a similar doubt. What @sarnold says works. But suppose we are talking about a custom kernel, and the program is configured to be built-in instead of as a module, how could the other module know? Could be there a generic solution? A way to ask "IF (xxx is built-in or is running as a module) THEN DO this ELSE DO this"? – marcocamejo May 17 '12 at 22:15
  • 1
    @marcocamejo: if you mean that you selected `Y` rather than `M` in `make config`, I would really hope that the `find_module()` interface _still_ works. Perhaps you'd be better suited with `#if CONFIG_FOO` for whatever `CONFIG_FOO` variable you use to decide if the built-in is built-in or not... – sarnold May 17 '12 at 23:08
  • Yes, I mean selecting Y rather than M in make config. I will try find_module() in case it was selected Y, hope it works. I didn't get the part of #if CONFIG_FOO, could you explain me a bit more? thanks – marcocamejo May 18 '12 at 01:06
  • 1
    Just to take one example, `drivers/infiniband/core/addr.c` has the following line: `#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)` -- if you're modifying the kernel source to add your built-in code, you could do the same sorts of checks. – sarnold May 18 '12 at 01:34
  • Oh thanks for commenting @marcocamejo and for your answer @sarnold. Using `#if defined()` could cover both cases (configured as Y or as M)? – jeanc May 18 '12 at 13:29
  • I am working a problem similar to this now, and your solution doesn't work when the functions that you are going to use in your modified module are defined in the other module. When compiling the kernel, if you selected your program as module, the compiler will not be able to solve the references to your external functions. I you can, take a look at my post here: http://stackoverflow.com/questions/10627738/check-if-linux-kernel-module-is-running – marcocamejo Jul 13 '12 at 01:08
  • For the curious, @marcocamejo intended to paste this URL: http://stackoverflow.com/questions/11463184/how-to-use-exported-symbols-in-the-linux-kernel – sarnold Jul 16 '12 at 22:32