内核启动用户态的程序

内核启动用户态的应用程序是通过call_usermodehelper来调用的,比较常见的调用,比如modprobe. 

Java代码   收藏代码
  1. static inline int  
  2. call_usermodehelper(char *path, char **argv, char **envp, enum umh_wait wait)  


参数说明; 

path : 应用程序的位置 

argv : 传递给用户态应用程序的参数 

envp: 传递给应用程序的环境变量 

wait : 调用用户态应用程序的内核程序是否等到用户态程序退出后才退出。 

使用的例子: 

我们来看kmod.c的源码,它通过调用用户态的modprobe来加载所需要的驱动模块。 

Java代码   收藏代码
  1. int request_module(const char *fmt, ...)  
  2. {  
  3.     va_list args;  
  4.     char module_name[MODULE_NAME_LEN];  
  5.     unsigned int max_modprobes;  
  6.     int ret;  
  7.   
  8. ///构造传递给modeprobe的参数  
  9.     char *argv[] = { modprobe_path, "-q""--", module_name, NULL };  
  10.     static char *envp[] = { "HOME=/",  
  11.                 "TERM=linux",  
  12.                 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",  
  13.                 NULL };  
  14.     static atomic_t kmod_concurrent = ATOMIC_INIT(0);  
  15. #define MAX_KMOD_CONCURRENT 50  /* Completely arbitrary value - KAO */  
  16.     static int kmod_loop_msg;  
  17.   
  18.     va_start(args, fmt);  
  19.     ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);  
  20.     va_end(args);  
  21.     if (ret >= MODULE_NAME_LEN)  
  22.         return -ENAMETOOLONG;  
  23.   
  24.     /* If modprobe needs a service that is in a module, we get a recursive 
  25.      * loop.  Limit the number of running kmod threads to max_threads/2 or 
  26.      * MAX_KMOD_CONCURRENT, whichever is the smaller.  A cleaner method 
  27.      * would be to run the parents of this process, counting how many times 
  28.      * kmod was invoked.  That would mean accessing the internals of the 
  29.      * process tables to get the command line, proc_pid_cmdline is static 
  30.      * and it is not worth changing the proc code just to handle this case.  
  31.      * KAO. 
  32.      * 
  33.      * "trace the ppid" is simple, but will fail if someone's 
  34.      * parent exits.  I think this is as good as it gets. --RR 
  35.      */  
  36.     max_modprobes = min(max_threads/2, MAX_KMOD_CONCURRENT);  
  37.     atomic_inc(&kmod_concurrent);  
  38.     if (atomic_read(&kmod_concurrent) > max_modprobes) {  
  39.         /* We may be blaming an innocent here, but unlikely */  
  40.         if (kmod_loop_msg++ < 5)  
  41.             printk(KERN_ERR  
  42.                    "request_module: runaway loop modprobe %s\n",  
  43.                    module_name);  
  44.         atomic_dec(&kmod_concurrent);  
  45.         return -ENOMEM;  
  46.     }  
  47. ///调用modeprobe.  
  48.     ret = call_usermodehelper(modprobe_path, argv, envp, 1);  
  49.     atomic_dec(&kmod_concurrent);  
  50.     return ret;  
  51. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值