Find the answer to your Linux question:
Results 1 to 8 of 8

Thread: kerne module

Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    kerne module

    hi all,
    i want to know how can i add or remove kernel module in application and also send me how to program.........pls help me........

  2. #2
    Just Joined!
    Join Date
    Feb 2012
    Hyderabad, India
    If i understand correctly, you want to know how to write an application which inserts and removes a module. I mean instead of using insmod and rmmod tools, you want to do that in the application. If my understanding is correct, you can try the following program.

    #include <sys/mman.h>
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <limits.h>
    #include <unistd.h>
    #include <sys/syscall.h>
    #include <string.h>

    #define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
    #define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags)

    void* try_to_mmap_module(const char *filename, size_t *image_size_p)
    void *image;
    struct stat st;
    int fd;

    fd = open(filename, O_RDONLY);
    if (fd < 0)
    return NULL;

    fstat(fd, &st);
    image = NULL;
    /* st.st_size is off_t, we can't just pass it to mmap */
    if (st.st_size <= *image_size_p) {
    size_t image_size = st.st_size;
    image = mmap(NULL, image_size, PROT_READ, MAP_PRIVATE, fd, 0);
    if (image == MAP_FAILED) {
    image = NULL;
    } else {
    /* Success. Report the size */
    *image_size_p = image_size;
    return image;

    int main(void)
    char *module = "module.ko";
    size_t image_size;
    char *image;
    int rc;
    char *options = "";
    char mod[100]; // max module name

    image_size = INT_MAX - 4095;
    image = try_to_mmap_module(module, &image_size);
    if (!image) {
    printf("\nCannot mmap the module\n");
    return 0;

    rc = init_module(image, image_size, options);
    if (rc < 0) {

    munmap(image, image_size);

    * To remove the module, uncomment the following code
    // strncpy(mod, module, strlen(module) - 3);

    // rc = delete_module(mod, 0);
    // if (rc)
    // perror("delete_module");

  3. #3


    hi vedasolutions,
    thank you for ur reply...i will check it ...........

  4. $spacer_open
  5. #4
    Just Joined!
    Join Date
    Feb 2012
    Hyderabad, India
    The programs mmaps the given module (module.ko). mmap will copy the module into the memory and returns its pointer. I am invoking a system call init_module using the syscall() function. For the system call I am giving the address of the module where it has been loaded and some other flags. delete_module is to invoke delete_module system call to remove the module. delete_module() takes the module name (without .ko) and removes the module.

  6. #5
    thank u.............

  7. #6
    Blackfooted Penguin daark.child's Avatar
    Join Date
    Apr 2006
    West Yorks
    This article may help you with some of your questions.

  8. #7
    Just Joined!
    Join Date
    Feb 2012
    Hyderabad, India
    Use modprobe command. First copy your module into /lib/modules/$(shell uname -r)/kernel and type depmod command, like this:

    $cp module.ko /lib/modules/$(shell uname -r)/kernel

    $sudo depmod
    $modprobe module (note no .ko here)

  9. #8

    thank u...but how to know which modules are necessary & whichever unnecessary in that to remove it?..& how to add some modules whenever we want? to know the modules interconnected with another one? it is automatically removed?..pls help me.......

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts