annotate toys/insmod.c @ 562:4d802d438983

Match uint64_t with PRIu64 to avoid warnings on 64 bit builds.
author Rob Landley <rob@landley.net>
date Sat, 14 Apr 2012 21:27:00 -0500
parents d473dff476e2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
489
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
1 /* vi: set sw=4 ts=4:
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
2 *
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
3 * insmod.c - Load a module into the Linux kernel.
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
4 *
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
5 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
6 *
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
7 * Not in SUSv4.
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
8
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
9 USE_INSMOD(NEWTOY(insmod, "<1", TOYFLAG_BIN|TOYFLAG_NEEDROOT))
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
10
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
11 config INSMOD
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
12 bool "insmod"
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
13 default y
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
14 help
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
15 usage: insmod MODULE [MODULE_OPTIONS]
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
16
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
17 Load the module named MODULE passing options if given.
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
18 */
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
19
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
20 #include "toys.h"
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
21
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
22 #include <sys/syscall.h>
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
23 #define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
24
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
25 void insmod_main(void)
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
26 {
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
27 char * buf = NULL;
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
28 int len, res, i;
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
29 int fd = xopen(toys.optargs[0], O_RDONLY);
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
30
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
31 len = fdlength(fd);
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
32 buf = xmalloc(len);
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
33 xreadall(fd, buf, len);
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
34
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
35 i = 1;
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
36 while(toys.optargs[i] &&
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
37 strlen(toybuf) + strlen(toys.optargs[i]) + 2 < sizeof(toybuf)) {
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
38 strcat(toybuf, toys.optargs[i++]);
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
39 strcat(toybuf, " ");
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
40 }
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
41
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
42 res = init_module(buf, len, toybuf);
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
43 if (CFG_TOYBOX_FREE && buf != toybuf) free(buf);
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
44
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
45 if (res)
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
46 perror_exit("failed to load %s", toys.optargs[0]);
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
47 }