安装kernel module 流程简析(一)

Older kernel version

//source/kernel/module.c
/* This is where the real work happens */
SYSCALL_DEFINE3(init_module, void __user *, umod,
		unsigned long, len, const char __user *, uargs)

{

       ......

    load_module(umod, len, uargs);

       ........

    do_one_initcall(mod->init);

}

 

 

V3.8.0

SYSCALL_DEFINE3(finit_module, int, fd, const char __user *, uargs, int, flags)
{
    ......
    copy_module_from_fd();
    ......
    load_module();

}
SYSCALL_DEFINE3(init_module, void __user *, umod,
		unsigned long, len, const char __user *, uargs)
{
    ......
    copy_module_from_user(umod, len, &info);
    ......
    return load_module(&info, uargs, 0);
}

 

 

 

toybox/toys/other/insmod.c

/* insmod.c - Load a module into the Linux kernel.
 *
 * Copyright 2012 Elie De Brauwer 
USE_INSMOD(NEWTOY(insmod, "<1", TOYFLAG_SBIN|TOYFLAG_NEEDROOT))
config INSMOD
  bool "insmod"
  default y
  help
    usage: insmod MODULE [MODULE_OPTIONS]
    Load the module named MODULE passing options if given.
*/

#include "toys.h"

#include 
#ifdef SYS_finit_module
#define finit_module(fd, opts, flags) syscall(SYS_finit_module, fd, opts, flags)
#else
#define finit_module(a, b, c) (errno = ENOSYS)
#endif
#define init_module(mod, len, opts) syscall(SYS_init_module, mod, len, opts)

void insmod_main(void)
{
  int fd = xopenro(*toys.optargs);
  int i, rc;

  i = 1;
  while (toys.optargs[i] &&
    strlen(toybuf) + strlen(toys.optargs[i]) + 2 < sizeof(toybuf))
  {
    strcat(toybuf, toys.optargs[i++]);
    strcat(toybuf, " ");
  }

  // finit_module was new in Linux 3.8, and doesn't work on stdin,
  // so we fall back to init_module if necessary.
  rc = finit_module(fd, toybuf, 0);
  if (rc && (fd == 0 || errno == ENOSYS)) {
    off_t len = 0;
    char *path = !strcmp(*toys.optargs, "-") ? "/dev/stdin" : *toys.optargs;
    char *buf = readfileat(AT_FDCWD, path, NULL, &len);

    rc = init_module(buf, len, toybuf);
    if (CFG_TOYBOX_FREE) free(buf);
  }

  if (rc) perror_exit("failed to load %s", toys.optargs[0]);

  if (CFG_TOYBOX_FREE) close(fd);
}

 

你可能感兴趣的:(Linux,kernel,driver)