I want to use system()
function of stdlib.h
in my c code. I am actually working on kernel programming.
Whenever i want to use system()
in it, it gives error to stdlib.h
saying no such file found.
I want to use system()
function of stdlib.h
in my c code. I am actually working on kernel programming.
Whenever i want to use system()
in it, it gives error to stdlib.h
saying no such file found.
It's simple!
#include <linux/kmod.h>
char * envp[] = { "HOME=/", NULL };
char * argv[] = { "/bin/ls", NULL };
call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
What you probably want is executing a userspace function. That SE answer includes a link to an IBM article with an example userspace process invoked from the kernel. The search terms you should use are "usermodehelper" and "usermode helper".
In the kernel, see:
yba@tavas:~/linux-2.6/linux-2.6$ find . -type f | xargs grep "usermode.helper"
./kernel/cgroup.c: /* Drop the lock while we invoke the usermode helper,
./kernel/kmod.c: /* CLONE_VFORK: wait until the usermode helper has execve'd
./kernel/kmod.c: * call_usermodehelper_setup - prepare to call a usermode helper
./drivers/block/drbd/drbd_int.h:extern char usermode_helper[];
./drivers/block/drbd/drbd_nl.c: char *argv[] = {usermode_helper, cmd, mb, NULL };
./drivers/block/drbd/drbd_nl.c: dev_info(DEV, "helper command: %s %s %s\n", usermode_helper, cmd, mb);
./drivers/block/drbd/drbd_nl.c: ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
./drivers/block/drbd/drbd_nl.c: usermode_helper, cmd, mb,
./drivers/block/drbd/drbd_nl.c: usermode_helper, cmd, mb,
./drivers/block/drbd/drbd_nl.c: char *argv[] = {usermode_helper, cmd, tconn->name, NULL };
./drivers/block/drbd/drbd_nl.c: conn_info(tconn, "helper command: %s %s %s\n", usermode_helper, cmd, tconn->name);
./drivers/block/drbd/drbd_nl.c: ret = call_usermodehelper(usermode_helper, argv, envp, UMH_WAIT_PROC);
./drivers/block/drbd/drbd_nl.c: usermode_helper, cmd, tconn->name,
./drivers/block/drbd/drbd_nl.c: usermode_helper, cmd, tconn->name,
./drivers/block/drbd/drbd_main.c:char usermode_helper[80] = "/sbin/drbdadm";
./drivers/block/drbd/drbd_main.c:module_param_string(usermode_helper, usermode_helper, sizeof(usermode_helper), 0644);
./drivers/block/drbd/drbd_main.c: * currently blocked waiting for that usermode helper to
./security/keys/request_key.c: * Initialise a usermode helper that is going to have a specific session
./security/keys/request_key.c: * Clean up a usermode helper with session keyring.
./security/keys/request_key.c: * Call a usermode helper with a specific session keyring.
No easy way, because system() is a user-level C library function, which involve many system calls, including:
sys_fork()
sys_execve()
You could acchive system() effect using the following ways ( I guess):
create a kernel thread.
let the kernel thread execute sys_execve( your command name)
Not sure this could work, but you can try.
You can't use system() from the kernel. End of story. There is no application level code to execute from the kernel space.