如何在linux上监视进程创建和退出以及模块加载?

时间:2015-07-16 11:28:42

标签: linux linux-kernel

在Windows上,这些工作可以通过使用PsSetCreateProcessNotifyRoutine和PsSetLoadImageNotifyRoutine来完成。但我想在linux上实现它。任何人都可以给出一些建议或想法?提前感谢!

1 个答案:

答案 0 :(得分:1)

对于catch模块的加载事件,您需要使用register_module_notifier()函数。它的struct notifier_block参数应该在调用之前初始化:

int notifier_callback(struct notifier_block *nb,
     unsigned long action, void *data)
{
    struct module* m = data; // Module which state is changed.
    switch(action)
    {
    case MODULE_STATE_COMING:
        // Module is just loaded
    break;
    case MODULE_STATE_LIVING:
        // Module's init function has been executed
    break;
    case MODULE_STATE_GOING:
        // Module's exit function has been executed
    break;
    }

    return 0;
}

struct notifier_block n =
{
    .notifier_call = &notifier_callback,
    .priority = <any integer value, e.g. 0>
};

...

int my_module_init(void)
{
      ...
      register_module_notifier(&n);
}

void my_module_exit(void)
{
      unregister_module_notifier(&n);
      ...
}

我不知道监控过程状态的任何方式。