Find the answer to your Linux question:
Results 1 to 8 of 8
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...........
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2012
    Posts
    42

    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
    Location
    Hyderabad, India
    Posts
    11
    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;
    }
    }
    close(fd);
    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) {
    perror("init_module");
    }

    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
    Just Joined!
    Join Date
    Feb 2012
    Posts
    42

    Smile

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

  4. #4
    Just Joined!
    Join Date
    Feb 2012
    Location
    Hyderabad, India
    Posts
    11
    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.

  5. #5
    Just Joined!
    Join Date
    Feb 2012
    Posts
    42
    thank u.............

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

  7. #7
    Just Joined!
    Join Date
    Feb 2012
    Location
    Hyderabad, India
    Posts
    11
    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)

  8. #8
    Just Joined!
    Join Date
    Feb 2012
    Posts
    42
    hi,

    thank u...but how to know which modules are necessary & whichever unnecessary in that kernel....how to remove it?..& how to add some modules whenever we want?...how to know the modules interconnected with another one?...how 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
  •