Pages

2010-08-18

Linux: Calling user-land program from kernel-land in Linux

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kmod.h>

MODULE_LICENSE( "GPL" );
static int __init my_module_init( void )
{
  return call_user_land_program();
}


static void __exit my_module_exit( void )
{
  return;
}

module_init( my_module_init );
module_exit( my_moduel_exit );
static int call_user_land_program( void )
{
  struct subprocess_info *sub_info;
  char *argv[] = { "/usr/bin/myprogram", "--help", NULL };
  static char *envp[] = {
        "HOME=/",
        "TERM=linux",
        "PATH=/sbin:/bin:/usr/sbin:/usr/bin", NULL };
  sub_info = call_usermodehelper_setup( argv[0], argv, envp, GFP_ATOMIC );
  if (sub_info == NULL) return -ENOMEM;
return call_usermodehelper_exec( sub_info, UMH_WAIT_PROC ); // UMH_NO_WAIT, UMH_WAIT_EXEC
}
Second Solutions (Preferred solution):
 
static int call_user_land_program( void )
{
  
   char *argv[] = { "/usr/bin/myprogram", "--help", NULL }; 
static char *envp[] = {
        "HOME=/",
        "TERM=linux",
        "PATH=/sbin:/bin:/usr/sbin:/usr/bin", NULL };
  return call_usermodehelper( argv[0], argv, envp, UMH_WAIT_PROC );
}
 
  

No comments:

Post a Comment