blob: d252433bf1194f1273a486bd18218357f4c5f200 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001#include <stdio.h>
2#include <string.h>
3#include <fcntl.h>
4#include <unistd.h>
5#include <malloc.h>
6#include <errno.h>
Elliott Hughesccecf142014-01-16 10:53:11 -08007#include <sys/param.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08008#include <sys/types.h>
9#include <sys/stat.h>
10#include <unistd.h>
11
12extern int init_module(void *, unsigned long, const char *);
13
14static void *read_file(const char *filename, ssize_t *_size)
15{
16 int ret, fd;
17 struct stat sb;
18 ssize_t size;
19 void *buffer = NULL;
20
21 /* open the file */
22 fd = open(filename, O_RDONLY);
23 if (fd < 0)
24 return NULL;
25
26 /* find out how big it is */
27 if (fstat(fd, &sb) < 0)
28 goto bail;
29 size = sb.st_size;
30
31 /* allocate memory for it to be read into */
32 buffer = malloc(size);
33 if (!buffer)
34 goto bail;
35
36 /* slurp it into our buffer */
37 ret = read(fd, buffer, size);
38 if (ret != size)
39 goto bail;
40
41 /* let the caller know how big it is */
42 *_size = size;
43
44bail:
45 close(fd);
46 return buffer;
47}
48
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049int insmod_main(int argc, char **argv)
50{
51 void *file;
52 ssize_t size = 0;
53 char opts[1024];
54 int ret;
55
56 /* make sure we've got an argument */
57 if (argc < 2) {
58 fprintf(stderr, "usage: insmod <module.o>\n");
59 return -1;
60 }
61
62 /* read the file into memory */
63 file = read_file(argv[1], &size);
64 if (!file) {
65 fprintf(stderr, "insmod: can't open '%s'\n", argv[1]);
66 return -1;
67 }
68
69 opts[0] = '\0';
70 if (argc > 2) {
71 int i, len;
72 char *end = opts + sizeof(opts) - 1;
73 char *ptr = opts;
74
75 for (i = 2; (i < argc) && (ptr < end); i++) {
Greg Hackmanneeed45f2014-05-21 10:39:44 -070076 len = MIN(strlen(argv[i]), (size_t)(end - ptr));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077 memcpy(ptr, argv[i], len);
78 ptr += len;
79 *ptr++ = ' ';
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080 }
81 *(ptr - 1) = '\0';
82 }
83
84 /* pass it to the kernel */
85 ret = init_module(file, size, opts);
86 if (ret != 0) {
87 fprintf(stderr,
88 "insmod: init_module '%s' failed (%s)\n",
89 argv[1], strerror(errno));
90 }
91
92 /* free the file buffer */
93 free(file);
94
95 return ret;
96}
97