• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* insmod.c - Load a module into the Linux kernel.
2  *
3  * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
4 
5 USE_INSMOD(NEWTOY(insmod, "<1", TOYFLAG_SBIN|TOYFLAG_NEEDROOT))
6 
7 config INSMOD
8   bool "insmod"
9   default y
10   help
11     usage: insmod MODULE [OPTION...]
12 
13     Load the module named MODULE passing options if given.
14 */
15 
16 #include "toys.h"
17 
insmod_main(void)18 void insmod_main(void)
19 {
20   int fd = xopenro(*toys.optargs);
21   int i, rc;
22 
23   i = 1;
24   while (toys.optargs[i] &&
25     strlen(toybuf) + strlen(toys.optargs[i]) + 2 < sizeof(toybuf))
26   {
27     strcat(toybuf, toys.optargs[i++]);
28     strcat(toybuf, " ");
29   }
30 
31   // finit_module doesn't work on stdin, so we fall back to init_module...
32   rc = syscall(SYS_finit_module, fd, toybuf, 0);
33   if (rc && (fd == 0 || errno == ENOSYS)) {
34     off_t len = 0;
35     char *path = !strcmp(*toys.optargs, "-") ? "/dev/stdin" : *toys.optargs;
36     char *buf = readfileat(AT_FDCWD, path, NULL, &len);
37 
38     if (!buf) perror_exit("couldn't read %s", path);
39     rc = syscall(SYS_init_module, buf, len, toybuf);
40     if (CFG_TOYBOX_FREE) free(buf);
41   }
42 
43   if (rc) perror_exit("failed to load %s", toys.optargs[0]);
44 
45   if (CFG_TOYBOX_FREE) close(fd);
46 }
47