Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kernel Module Init and Exit functions being called in wrong order

I'm making a very simple hello world kernel module and getting some crazy behavior. This worked until I upgraded to kernel 3.3.8 and now it... Well, it's calling the init function on exit, and the exit function on initialize. I've made sure my names are correct

// Needed for module definitions
#include <linux/module.h>
// Needed for initilization modules
#include <linux/init.h>

// Must declare some license
MODULE_LICENSE("Dual BSD/GPL");

// Function to be called on insmod
// Returns 0 on success
static int __init mymod_init(void)
{
        // Prints kernel alert.  Check /var/log/syslog
        printk(KERN_ALERT "Module was loaded, this is the printk.");

        return 0;
}

// Function to be called on rmmod
static void __exit mymod_exit(void)
{
        // Prints kernel alert.  Check /var/log/syslog
        printk(KERN_ALERT "Module was unloaded, this is the printk");
}

// Register these functions
module_init(mymod_init);
module_exit(mymod_exit);

Sample output:

root@cop4610:/home/cop4610/Downloads/linux-3.3.8/mymodule# insmod mymodule.ko root@cop4610:/home/cop4610/Downloads/linux-3.3.8/mymodule# tail /var/log/syslog Oct 12 10:08:20 cop4610 kernel: [ 633.567832] Module was unloaded, this is the printk

The following is a video of this happening live: http://www.youtube.com/watch?v=8aJNSpCd7as&feature=youtu.be

like image 628
Ben Avatar asked Oct 12 '12 14:10

Ben


People also ask

What is init and exit functions of a driver?

__init and __exit attributesThe kernel will run the init function of the driver for the first time during its boot sequence. Since the driver cannot be unloaded, its init function will not be called again until the next reboot. There is no need to keep references on its init function anymore. ...

What do the functions module_init and Module_exit do?

module_init() will either be called during do_initcalls() (if builtin) or at module insertion time (if a module). There can only be one per module. module_exit() will wrap the driver clean-up code with cleanup_module() when used with rmmod when the driver is a module.

What does __ init mean in the Linux kernel code?

__init is a macro defined in ./include/linux/init. h which expands to __attribute__ ((__section__(". init. text"))) . It instructs the compiler to mark this function in a special way.

Which macro defines the function that needs to be called during module insertion?

The module_init() macro defines which function is to be called at module insertion time (if the file is compiled as a module), or at boot time: if the file is not compiled as a module the module_init() macro becomes equivalent to __initcall() , which through linker magic ensures that the function is called on boot.


2 Answers

It needed a newline!!!!!! Arrggg!!!

 printk(KERN_ALERT "Module was unloaded, this is the printk\n");

and

 printk(KERN_ALERT "Module was loaded, this is the printk\n");

It seems it wasn't really doing them out of order, it just appeared to, because the first one was not showing up until the second one was issued as the buffer was not flushed.

like image 175
Ben Avatar answered Oct 24 '22 08:10

Ben


This is my basic example:

#include <linux/module.h>
#include <linux/kernel.h>

#define MODULE_NAME "hello_md"

MODULE_LICENSE("GPL");
MODULE_AUTHOR("B3h3m0th");
MODULE_DESCRIPTION("Basic LKM; hello world module");
MODULE_VERSION("0.0");

static int __init insert_mod(void)
{
   printk(KERN_ALERT "[%s] Init: \"Hello World\"\n", MODULE_NAME);
   return 0;
}


static void __exit remove_mod(void)
{
   printk(KERN_ALERT "[%s] Exit\n", MODULE_NAME);
}

module_init(insert_mod);
module_exit(remove_mod);

My basic Makefile:

obj-m += basic_module.o 

KERNELVERSION = $(shell uname -r)

all:
    $(MAKE) -C /lib/modules/$(KERNELVERSION)/build M=$(PWD) modules
clean:
    $(MAKE) -C /lib/modules/$(KERNELVERSION)/build M=$(PWD) clean
like image 31
b3h3m0th Avatar answered Oct 24 '22 08:10

b3h3m0th