Loading kernel module from C user space application

978 Views Asked by At

I'm trying to load a kernel module from C using kmod, but it's not working at all. Here's what I have:

#include <stdio.h>
#include <stddef.h>
#include <unistd.h>
#include <stdlib.h>

#include <libkmod.h>

int main(int argc, char **argv){

    struct kmod_ctx *ctx;
    struct kmod_module *mod;
    const char *null_config = NULL;
    int err;

    ctx = kmod_new(NULL, &null_config);
    if(ctx == NULL)
        exit(EXIT_FAILURE);

    err = kmod_module_new_from_path(ctx, "./my_module.ko", &mod); // <-- module is in same dir as this binary
    if(err != 0)
        printf("Error creating module from path\n");

    err = kmod_module_insert_module(mod, 0, NULL);
    if(err != 0)
        printf("Error inserting module\n");
    kmod_unref(ctx);

}

I'm getting both printfs, so kmod is failing at creating the module from the path, but why?

PS: I'm running the binary as root, from the same dir as the module.

0

There are 0 best solutions below