1 | /*
|
---|
2 | * Common modutils related functions for busybox
|
---|
3 | *
|
---|
4 | * Copyright (C) 2008 by Timo Teras <timo.teras@iki.fi>
|
---|
5 | *
|
---|
6 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
7 | */
|
---|
8 | #include "modutils.h"
|
---|
9 |
|
---|
10 | #ifdef __UCLIBC__
|
---|
11 | extern int init_module(void *module, unsigned long len, const char *options);
|
---|
12 | extern int delete_module(const char *module, unsigned int flags);
|
---|
13 | #else
|
---|
14 | # include <sys/syscall.h>
|
---|
15 | # define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts)
|
---|
16 | # define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags)
|
---|
17 | #endif
|
---|
18 |
|
---|
19 | void FAST_FUNC replace(char *s, char what, char with)
|
---|
20 | {
|
---|
21 | while (*s) {
|
---|
22 | if (what == *s)
|
---|
23 | *s = with;
|
---|
24 | ++s;
|
---|
25 | }
|
---|
26 | }
|
---|
27 |
|
---|
28 | char* FAST_FUNC replace_underscores(char *s)
|
---|
29 | {
|
---|
30 | replace(s, '-', '_');
|
---|
31 | return s;
|
---|
32 | }
|
---|
33 |
|
---|
34 | int FAST_FUNC string_to_llist(char *string, llist_t **llist, const char *delim)
|
---|
35 | {
|
---|
36 | char *tok;
|
---|
37 | int len = 0;
|
---|
38 |
|
---|
39 | while ((tok = strsep(&string, delim)) != NULL) {
|
---|
40 | if (tok[0] == '\0')
|
---|
41 | continue;
|
---|
42 | llist_add_to_end(llist, xstrdup(tok));
|
---|
43 | len += strlen(tok);
|
---|
44 | }
|
---|
45 | return len;
|
---|
46 | }
|
---|
47 |
|
---|
48 | char* FAST_FUNC filename2modname(const char *filename, char *modname)
|
---|
49 | {
|
---|
50 | int i;
|
---|
51 | char *from;
|
---|
52 |
|
---|
53 | if (filename == NULL)
|
---|
54 | return NULL;
|
---|
55 | if (modname == NULL)
|
---|
56 | modname = xmalloc(MODULE_NAME_LEN);
|
---|
57 | from = bb_get_last_path_component_nostrip(filename);
|
---|
58 | for (i = 0; i < (MODULE_NAME_LEN-1) && from[i] != '\0' && from[i] != '.'; i++)
|
---|
59 | modname[i] = (from[i] == '-') ? '_' : from[i];
|
---|
60 | modname[i] = '\0';
|
---|
61 |
|
---|
62 | return modname;
|
---|
63 | }
|
---|
64 |
|
---|
65 | char* FAST_FUNC parse_cmdline_module_options(char **argv)
|
---|
66 | {
|
---|
67 | char *options;
|
---|
68 | int optlen;
|
---|
69 |
|
---|
70 | options = xzalloc(1);
|
---|
71 | optlen = 0;
|
---|
72 | while (*++argv) {
|
---|
73 | options = xrealloc(options, optlen + 2 + strlen(*argv) + 2);
|
---|
74 | /* Spaces handled by "" pairs, but no way of escaping quotes */
|
---|
75 | //TODO: module-init-tools version 3.11.1 quotes only value:
|
---|
76 | //it generates var="val with spaces", not "var=val with spaces"
|
---|
77 | //(and it won't quote var *name* even if it has spaces)
|
---|
78 | optlen += sprintf(options + optlen, (strchr(*argv, ' ') ? "\"%s\" " : "%s "), *argv);
|
---|
79 | }
|
---|
80 | return options;
|
---|
81 | }
|
---|
82 |
|
---|
83 | #if ENABLE_FEATURE_INSMOD_TRY_MMAP
|
---|
84 | void* FAST_FUNC try_to_mmap_module(const char *filename, size_t *image_size_p)
|
---|
85 | {
|
---|
86 | /* We have user reports of failure to load 3MB module
|
---|
87 | * on a 16MB RAM machine. Apparently even a transient
|
---|
88 | * memory spike to 6MB during module load
|
---|
89 | * is too big for that system. */
|
---|
90 | void *image;
|
---|
91 | struct stat st;
|
---|
92 | int fd;
|
---|
93 |
|
---|
94 | fd = xopen(filename, O_RDONLY);
|
---|
95 | fstat(fd, &st);
|
---|
96 | image = NULL;
|
---|
97 | /* st.st_size is off_t, we can't just pass it to mmap */
|
---|
98 | if (st.st_size <= *image_size_p) {
|
---|
99 | size_t image_size = st.st_size;
|
---|
100 | image = mmap(NULL, image_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
---|
101 | if (image == MAP_FAILED) {
|
---|
102 | image = NULL;
|
---|
103 | } else if (*(uint32_t*)image != SWAP_BE32(0x7f454C46)) {
|
---|
104 | /* No ELF signature. Compressed module? */
|
---|
105 | munmap(image, image_size);
|
---|
106 | image = NULL;
|
---|
107 | } else {
|
---|
108 | /* Success. Report the size */
|
---|
109 | *image_size_p = image_size;
|
---|
110 | }
|
---|
111 | }
|
---|
112 | close(fd);
|
---|
113 | return image;
|
---|
114 | }
|
---|
115 | #endif
|
---|
116 |
|
---|
117 | /* Return:
|
---|
118 | * 0 on success,
|
---|
119 | * -errno on open/read error,
|
---|
120 | * errno on init_module() error
|
---|
121 | */
|
---|
122 | int FAST_FUNC bb_init_module(const char *filename, const char *options)
|
---|
123 | {
|
---|
124 | size_t image_size;
|
---|
125 | char *image;
|
---|
126 | int rc;
|
---|
127 | bool mmaped;
|
---|
128 |
|
---|
129 | if (!options)
|
---|
130 | options = "";
|
---|
131 |
|
---|
132 | //TODO: audit bb_init_module_24 to match error code convention
|
---|
133 | #if ENABLE_FEATURE_2_4_MODULES
|
---|
134 | if (get_linux_version_code() < KERNEL_VERSION(2,6,0))
|
---|
135 | return bb_init_module_24(filename, options);
|
---|
136 | #endif
|
---|
137 |
|
---|
138 | image_size = INT_MAX - 4095;
|
---|
139 | mmaped = 0;
|
---|
140 | image = try_to_mmap_module(filename, &image_size);
|
---|
141 | if (image) {
|
---|
142 | mmaped = 1;
|
---|
143 | } else {
|
---|
144 | errno = ENOMEM; /* may be changed by e.g. open errors below */
|
---|
145 | image = xmalloc_open_zipped_read_close(filename, &image_size);
|
---|
146 | if (!image)
|
---|
147 | return -errno;
|
---|
148 | }
|
---|
149 |
|
---|
150 | errno = 0;
|
---|
151 | init_module(image, image_size, options);
|
---|
152 | rc = errno;
|
---|
153 | if (mmaped)
|
---|
154 | munmap(image, image_size);
|
---|
155 | else
|
---|
156 | free(image);
|
---|
157 | return rc;
|
---|
158 | }
|
---|
159 |
|
---|
160 | int FAST_FUNC bb_delete_module(const char *module, unsigned int flags)
|
---|
161 | {
|
---|
162 | errno = 0;
|
---|
163 | delete_module(module, flags);
|
---|
164 | return errno;
|
---|
165 | }
|
---|
166 |
|
---|
167 | const char* FAST_FUNC moderror(int err)
|
---|
168 | {
|
---|
169 | switch (err) {
|
---|
170 | case -1: /* btw: it's -EPERM */
|
---|
171 | return "no such module";
|
---|
172 | case ENOEXEC:
|
---|
173 | return "invalid module format";
|
---|
174 | case ENOENT:
|
---|
175 | return "unknown symbol in module, or unknown parameter";
|
---|
176 | case ESRCH:
|
---|
177 | return "module has wrong symbol version";
|
---|
178 | case ENOSYS:
|
---|
179 | return "kernel does not support requested operation";
|
---|
180 | }
|
---|
181 | if (err < 0) /* should always be */
|
---|
182 | err = -err;
|
---|
183 | return strerror(err);
|
---|
184 | }
|
---|