test code:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/sched.h>
MODULE_LICENSE("GPL");
static __init int hello_init(void)
{
int result = 0;
char cmd_path[] = "/config/riu_w"; //可执行程序
//可执行程序参数,每个参数都要独立占一个数组值
char *cmd_argv[] = {cmd_path, "0xe", "0x2e", "0x79", NULL};
char *cmd_envp[] = {"HOME=/", "PATH=/sbin:/bin:/user/bin", NULL};
result = call_usermodehelper(cmd_path, cmd_argv, cmd_envp, UMH_WAIT_PROC);
printk(KERN_DEBUG"THe result of call_usermodehelper is %d\n", result);
return result;
}
static __exit void hello_exit(void)
{
int result = 0;
char cmd_path[] = "/bin/sh";
char *cmd_argv[] = {cmd_path, "/tmp/test.sh", NULL};
char *cmd_envp[] = {"HOME=/", "PATH=/sbin:/bin:/user/bin", NULL};
result = call_usermodehelper(cmd_path, cmd_argv, cmd_envp,
UMH_WAIT_PROC);
printk(KERN_DEBUG"THe result of call_usermodehelper is %d\n", result);
}
module_init(hello_init);
module_exit(hello_exit);
外部编译ko makefile
# Rules for making the NTFS-3G driver.
EXTRA_CFLAGS = -DNTFS3G_VERSION=\"0.0.1\"
obj-m += usermodehelper.o
#增加编译c文件
usermodehelper-objs += call_usermodehelper.o
#KDIR = /home/alan.yu/PERFORCE/THEALE/RedLion/2.6.32.15/kernel
#KDIR = /home/alan.yu/PERFORCE/DAILEO/temp_branch/Supernova_His_J2/component_src/kernel/2.6.32.15/kernel
KDIR = /home/aaron.feng/Code/CarDv/linux-3.18
ifneq ($(KDIR), $(wildcard $(KDIR) ))
$(info KDIR not exist,you should appoint proper KDIR value in Makefile.)
endif
all:
make -C $(KDIR)/ SUBDIRS=$(PWD) modules
clean:
make -C $(KDIR)/ SUBDIRS=$(PWD) clean