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 [MODULE_OPTIONS]
12
13 Load the module named MODULE passing options if given.
14 */
15
16 #include "toys.h"
17
18 #include <sys/syscall.h>
19 #define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
20
insmod_main(void)21 void insmod_main(void)
22 {
23 char * buf = NULL;
24 int len, res, i;
25 int fd = xopen(*toys.optargs, O_RDONLY);
26
27 len = fdlength(fd);
28 buf = xmalloc(len);
29 xreadall(fd, buf, len);
30
31 i = 1;
32 while(toys.optargs[i] &&
33 strlen(toybuf) + strlen(toys.optargs[i]) + 2 < sizeof(toybuf))
34 {
35 strcat(toybuf, toys.optargs[i++]);
36 strcat(toybuf, " ");
37 }
38
39 res = init_module(buf, len, toybuf);
40 if (CFG_TOYBOX_FREE) {
41 if (buf != toybuf) free(buf);
42 close(fd);
43 }
44
45 if (res) perror_exit("failed to load %s", toys.optargs[0]);
46 }
47