blob: ff79d3fac9c80f17fe9abf8255e37852c651eb63 [file] [log] [blame]
Denis Vlasenkoba1315d2008-09-13 14:59:38 +00001/*
2 * Common modutils related functions for busybox
3 *
4 * Copyright (C) 2008 by Timo Teras <timo.teras@iki.fi>
5 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02006 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoba1315d2008-09-13 14:59:38 +00007 */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +00008#include "modutils.h"
9
10#ifdef __UCLIBC__
11extern int init_module(void *module, unsigned long len, const char *options);
12extern 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
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000019void 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
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +020028char* FAST_FUNC replace_underscores(char *s)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000029{
30 replace(s, '-', '_');
31 return s;
32}
33
34int 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
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +020048char* FAST_FUNC filename2modname(const char *filename, char *modname)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000049{
50 int i;
Denys Vlasenko78854522015-01-01 19:02:40 +010051 const char *from;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000052
53 if (filename == NULL)
54 return NULL;
55 if (modname == NULL)
56 modname = xmalloc(MODULE_NAME_LEN);
Denys Vlasenko78854522015-01-01 19:02:40 +010057 // Disabled since otherwise "modprobe dir/name" would work
58 // as if it is "modprobe name". It is unclear why
59 // 'basenamization' was here in the first place.
60 //from = bb_get_last_path_component_nostrip(filename);
61 from = filename;
Denis Vlasenko48637e02009-02-26 12:00:52 +000062 for (i = 0; i < (MODULE_NAME_LEN-1) && from[i] != '\0' && from[i] != '.'; i++)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000063 modname[i] = (from[i] == '-') ? '_' : from[i];
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020064 modname[i] = '\0';
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000065
66 return modname;
67}
68
Denys Vlasenkoc5830bd2011-02-02 00:00:36 +010069char* FAST_FUNC parse_cmdline_module_options(char **argv, int quote_spaces)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000070{
71 char *options;
72 int optlen;
73
74 options = xzalloc(1);
75 optlen = 0;
76 while (*++argv) {
Denys Vlasenkoc5830bd2011-02-02 00:00:36 +010077 const char *fmt;
78 const char *var;
79 const char *val;
80
81 var = *argv;
82 options = xrealloc(options, optlen + 2 + strlen(var) + 2);
83 fmt = "%.*s%s ";
84 val = strchrnul(var, '=');
85 if (quote_spaces) {
86 /*
87 * modprobe (module-init-tools version 3.11.1) compat:
88 * quote only value:
89 * var="val with spaces", not "var=val with spaces"
90 * (note: var *name* is not checked for spaces!)
91 */
92 if (*val) { /* has var=val format. skip '=' */
93 val++;
94 if (strchr(val, ' '))
95 fmt = "%.*s\"%s\" ";
96 }
97 }
98 optlen += sprintf(options + optlen, fmt, (int)(val - var), var, val);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000099 }
Denys Vlasenkoc5830bd2011-02-02 00:00:36 +0100100 /* Remove trailing space. Disabled */
101 /* if (optlen != 0) options[optlen-1] = '\0'; */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000102 return options;
103}
104
Denys Vlasenko77c066e2009-10-25 04:35:22 +0100105#if ENABLE_FEATURE_INSMOD_TRY_MMAP
106void* FAST_FUNC try_to_mmap_module(const char *filename, size_t *image_size_p)
107{
108 /* We have user reports of failure to load 3MB module
109 * on a 16MB RAM machine. Apparently even a transient
110 * memory spike to 6MB during module load
111 * is too big for that system. */
112 void *image;
113 struct stat st;
114 int fd;
115
116 fd = xopen(filename, O_RDONLY);
117 fstat(fd, &st);
118 image = NULL;
119 /* st.st_size is off_t, we can't just pass it to mmap */
120 if (st.st_size <= *image_size_p) {
121 size_t image_size = st.st_size;
122 image = mmap(NULL, image_size, PROT_READ, MAP_PRIVATE, fd, 0);
123 if (image == MAP_FAILED) {
124 image = NULL;
125 } else if (*(uint32_t*)image != SWAP_BE32(0x7f454C46)) {
126 /* No ELF signature. Compressed module? */
127 munmap(image, image_size);
128 image = NULL;
129 } else {
130 /* Success. Report the size */
131 *image_size_p = image_size;
132 }
133 }
134 close(fd);
135 return image;
136}
137#endif
138
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200139/* Return:
140 * 0 on success,
141 * -errno on open/read error,
142 * errno on init_module() error
143 */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000144int FAST_FUNC bb_init_module(const char *filename, const char *options)
145{
Denys Vlasenko77c066e2009-10-25 04:35:22 +0100146 size_t image_size;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000147 char *image;
Denis Vlasenko41445042009-04-13 20:32:31 +0000148 int rc;
Denys Vlasenko77c066e2009-10-25 04:35:22 +0100149 bool mmaped;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000150
Denis Vlasenko3b5c9b82009-03-13 23:43:26 +0000151 if (!options)
152 options = "";
153
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200154//TODO: audit bb_init_module_24 to match error code convention
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000155#if ENABLE_FEATURE_2_4_MODULES
156 if (get_linux_version_code() < KERNEL_VERSION(2,6,0))
157 return bb_init_module_24(filename, options);
158#endif
159
Denys Vlasenko77c066e2009-10-25 04:35:22 +0100160 image_size = INT_MAX - 4095;
161 mmaped = 0;
162 image = try_to_mmap_module(filename, &image_size);
163 if (image) {
164 mmaped = 1;
165 } else {
166 errno = ENOMEM; /* may be changed by e.g. open errors below */
167 image = xmalloc_open_zipped_read_close(filename, &image_size);
168 if (!image)
169 return -errno;
170 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000171
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200172 errno = 0;
Denys Vlasenko77c066e2009-10-25 04:35:22 +0100173 init_module(image, image_size, options);
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200174 rc = errno;
Denys Vlasenko77c066e2009-10-25 04:35:22 +0100175 if (mmaped)
176 munmap(image, image_size);
177 else
178 free(image);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000179 return rc;
180}
181
182int FAST_FUNC bb_delete_module(const char *module, unsigned int flags)
183{
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200184 errno = 0;
185 delete_module(module, flags);
186 return errno;
187}
188
189const char* FAST_FUNC moderror(int err)
190{
191 switch (err) {
192 case -1: /* btw: it's -EPERM */
193 return "no such module";
194 case ENOEXEC:
195 return "invalid module format";
196 case ENOENT:
197 return "unknown symbol in module, or unknown parameter";
198 case ESRCH:
199 return "module has wrong symbol version";
200 case ENOSYS:
201 return "kernel does not support requested operation";
202 }
203 if (err < 0) /* should always be */
204 err = -err;
205 return strerror(err);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000206}