I checked the kernel APIs, part 1: Calling user space applications from the kernel and Executing a user space function from kernel space - Stack overflow - and here is a small kernel module callmodule.c , demonstrating that:
// 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");
... with the 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
When I ran this via sudo insmod ./callmodule.ko && sudo rmmod callmodule , I ended up in /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
... which obviously means that everything went well. (Using Linux 2.6.38-16-generiC # 67-Ubuntu SMP)
My question is: how can I get the PID of a process created from a kernel module? Is there a similar process different from call_usermodehelper that will allow me to instantiate a user space process in kernel space and get its pid?
Note that it is not possible to use call_usermodehelper and get the created PID process:
Re: call_usermodehelper pid? - Newcomers to the Linux kernel kernel
I want to create a custom space process from inside the kernel module and be able to kill it, send signals to it, etc.
Can I recognize his pid?
No, you canβt. But since pid is known inside the implementation, the patch that makes it available will not be too complicated (note that errors are always negative in the kernel, and pids are positive, limited to 2 ** 16). You will need to change all callers who expect 0 success.
I was looking a bit for sources, and it seems that the call chain ends up: call_usermodehelper - call_usermodehelper call_usermodehelper_setup __call_usermodehelper , which looks like this:
static void __call_usermodehelper(struct work_struct *work) { struct subprocess_info *sub_info = container_of(work, struct subprocess_info, work);
... therefore the PID of the kernel thread is used, but it is not stored anywhere in addition, neither work_struct nor subprocess_info have a pid field ( task_struct , but nothing here seems to use task_struct ). Writing this pid will require changing the kernel source - and, as I would like, this is the reason why I am also interested in approaches other than call_usermodehelper ...