kernel space 执行user space程序

本文介绍了一个简单的Linux内核模块,该模块通过call_usermodehelper函数在内核空间中调用用户空间的程序,并展示了如何编译和加载此类模块。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值