执行/调用用户空间程序,并从内核模块获取其pid

sda*_*aau 7 c linux linux-kernel

我查看了内核API,第1部分:从内核调用用户空间应用程序,以及从内核空间执行用户空间函数 - Stack Overflow - 这里是一个小内核模块callmodule.c,演示了:

// http://people.ee.ethz.ch/~arkeller/linux/code/usermodehelper.c

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <asm/uaccess.h>

static int __init callmodule_init(void)
{
    int ret = 0;
    char userprog[] = "/path/to/mytest";
    char *argv[] = {userprog, "2", NULL };
    char *envp[] = {"HOME=/", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };

    printk("callmodule: init %s\n", userprog);
    /* last parameter: 1 -> wait until execution has finished, 0 go ahead without waiting*/
    /* returns 0 if usermode process was started successfully, errorvalue otherwise*/
    /* no possiblity to get return value of usermode process*/
    ret = call_usermodehelper(userprog, argv, envp, UMH_WAIT_EXEC);
    if (ret != 0)
        printk("error in call to usermodehelper: %i\n", ret);
    else
        printk("everything all right\n");
        return 0;
}

static void __exit callmodule_exit(void)
{
    printk("callmodule: exit\n");
}

module_init(callmodule_init);
module_exit(callmodule_exit);
MODULE_LICENSE("GPL");
Run Code Online (Sandbox Code Playgroud)

......用Makefile:

obj-m += callmodule.o

all:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Run Code Online (Sandbox Code Playgroud)

当我通过这个sudo insmod ./callmodule.ko && sudo rmmod callmodule,我进入/var/log/syslog:

Feb 10 00:42:45 mypc kernel: [71455.260355] callmodule: init /path/to/mytest
Feb 10 00:42:45 mypc kernel: [71455.261218] everything all right
Feb 10 00:42:45 mypc kernel: [71455.286131] callmodule: exit
Run Code Online (Sandbox Code Playgroud)

......这显然意味着一切顺利.(使用Linux 2.6.38-16-通用#67-Ubuntu SMP)

我的问题是 - 如何从内核模块中获取进程的PID?除了之外是否有类似的进程call_usermodehelper,这将允许我在内核空间中实例化用户空间进程,并获取其pid?


请注意,可能无法使用call_usermodehelper并获取实例化的进程PID:

Re:call_usermodehelper的pid? - Linux Kernel Newbies

我想在内核模块中创建一个用户空间进程,并能够杀死它,向它发送信号等等......

我可以知道它的pid吗?

不,你不能.但是因为在实现中pid是已知的,使得它可用的补丁不会太难(注意,内核中的错误总是负的,并且pid是正的,限制为2**16).您必须修改所有期望0成功的调用者.

我稍微戳了一下这些来源,似乎最终有一个调用链:call_usermodehelper- > call_usermodehelper_setup- > __call_usermodehelper,它看起来像:

static void __call_usermodehelper(struct work_struct *work)
{
    struct subprocess_info *sub_info =
        container_of(work, struct subprocess_info, work);
    // ...
    if (wait == UMH_WAIT_PROC)
        pid = kernel_thread(wait_for_helper, sub_info,
                    CLONE_FS | CLONE_FILES | SIGCHLD);
    else
        pid = kernel_thread(____call_usermodehelper, sub_info,
                    CLONE_VFORK | SIGCHLD);
...
Run Code Online (Sandbox Code Playgroud)

...所以使用内核线程的PID,但它没有保存; 也没有,work_struct也subprocess_info没有一个pid领域(task_struct确实,但似乎没有任何东西task_struct).记录这个pid需要更改内核源代码 - 而且我想避免这种情况,这就是为什么我也对call_usermodehelper... 以外的方法感兴趣的原因

Chr*_*ier 4

根据我对kmod.c中实现的理解的初步答案。

如果您查看 的代码,call_usermodehelper您会看到它调用call_usermodehelper_setup然后call_usermodehelper_exec。

call_usermodehelper_setup将 init 函数作为参数,该函数将在do_execve. current我相信init 函数执行时的值会给你带来task_struct将为您提供用户进程的值。

因此,要获取 pid,您需要:

  1. call_usermodehelper复制代码中的实现。
  2. 定义一个 init 函数,将其作为参数传递给call_usermodehelper_setup.
  3. 在 init 函数中检索当前的 PID task_struct,然后检索 PID。


归档时间:

查看次数:

4434 次

最近记录:

12 年,9 月 前