4

我正在开发一个简单的内核模块,并且正在从命令行获取参数。我想要做的是在加载模块之前检查这些参数。

我检查了参数并返回 1 表示 init_module 函数失败,因此如果参数无效,则不会加载内核模块。

问题是模块仍然被加载,即使它没有通过参数检查(使用第一个 if 语句)。我输入sudo -f rmmod kernel_name了,它抱怨模块很忙。如果它通过参数检查,我如何让它加载模块?

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
}
4

1 回答 1

2

我假设您正在使用 Linux 内核模块。

一个正的返回值仍然可以解释为成功。在您的情况下,一种常见的做法是返回-error_code错误。-EINVAL

于 2012-09-26T04:36:23.970 回答