codereviewanskquestions
codereviewanskquestions

Reputation: 13998

How to force to exit if init_module() failed?

I am working on a simple kernel module and I am taking arguments from command line. What I want to do is to check those arguments before loading the module.

I checked the argument and returned 1 to indicate the failure of init_module function so that the kernel module won't be loaded if arguments are not valid.

The problem was that the module was still loaded even if it didn't pass the argument check (took the first if statement). I typed sudo -f rmmod kernel_name, it complained the module is busy. How do I make it to load the module if it passes the argument check?

int init_module(){
     //check argument here
     if(failed){ 
          //arguments are not valid. Return 1 to indicate the failure of init_module
          return 1;
     }
     else{
          register hook function here
          return 0;
     }
}

void cleanup_module(){
    unregister hook here
}

Upvotes: 4

Views: 1795

Answers (1)

Eugene
Eugene

Reputation: 6097

I assume you are working on a Linux kernel module.

A positive return value still can be interpreted as success. A common practice is to return -error_code on error, -EINVAL in your case.

Upvotes: 3

Related Questions