编程方式检查一个Linux内核模块是否存在在运行时 [英] Programmatically check whether a linux kernel module exists or not at runtime

查看:312
本文介绍了编程方式检查一个Linux内核模块是否存在在运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个C守护进程,它取​​决于两个内核模块的存在,以完成其工作。程序不直接使用这些(或任何其他)的模块。它只需要他们存在。
因此,我想以编程方式检查是否这些模块都已经加载与否,以提醒用户在运行时。

I am writing a C daemon, which depends on the existence of two kernel modules in order to do its job. The program does not directly use these (or any other) modules. It only needs them to exist. Therefore, I would like to programmatically check whether these modules are already loaded or not, in order to warn the user at runtime.

在我开始做的事情一样解析的/ proc /模块 lsmod的输出,做了效用函数已经存在的地方?
类似 is_module_loaded(为const char *名称);

Before I start to do things like parsing /proc/modules or lsmod output, does a utility function already exist somewhere? Something like is_module_loaded(const char* name);

我是pretty相信这已经问过。
不过,我觉得我缺少正确的术语来搜索这个。

I am pretty sure this has been asked before. However, I think I am missing the correct terms to search for this.

推荐答案

您可以使用的popen lsmod的| grep的招:

  FILE *fd = popen("lsmod | grep module_name", "r");

  char buf[16];
  if (fread (buf, 1, sizeof (buf), fd) > 0) // if there is some result the module must be loaded
    printf ("module is loaded\n");
  else
    printf ("module is not loaded\n");

这篇关于编程方式检查一个Linux内核模块是否存在在运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆