Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Common modutils related functions for busybox |
| 3 | * |
| 4 | * Copyright (C) 2008 by Timo Teras <timo.teras@iki.fi> |
| 5 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 6 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 7 | */ |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 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) |
Mike Frysinger | 3a45b87 | 2016-09-15 12:16:33 +0200 | [diff] [blame] | 16 | # if defined(__NR_finit_module) |
| 17 | # define finit_module(fd, uargs, flags) syscall(__NR_finit_module, fd, uargs, flags) |
| 18 | # endif |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 19 | # define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags) |
| 20 | #endif |
| 21 | |
Timo Teräs | 48dc80b | 2015-11-05 18:54:55 +0100 | [diff] [blame] | 22 | static module_entry *helper_get_module(module_db *db, const char *module, int create) |
| 23 | { |
| 24 | char modname[MODULE_NAME_LEN]; |
| 25 | struct module_entry *e; |
| 26 | unsigned i, hash; |
| 27 | |
| 28 | filename2modname(module, modname); |
| 29 | |
| 30 | hash = 0; |
| 31 | for (i = 0; modname[i]; i++) |
| 32 | hash = ((hash << 5) + hash) + modname[i]; |
| 33 | hash %= MODULE_HASH_SIZE; |
| 34 | |
| 35 | for (e = db->buckets[hash]; e; e = e->next) |
| 36 | if (strcmp(e->modname, modname) == 0) |
| 37 | return e; |
| 38 | if (!create) |
| 39 | return NULL; |
| 40 | |
| 41 | e = xzalloc(sizeof(*e)); |
| 42 | e->modname = xstrdup(modname); |
| 43 | e->next = db->buckets[hash]; |
| 44 | db->buckets[hash] = e; |
Denys Vlasenko | 196e400 | 2015-11-06 15:50:28 +0100 | [diff] [blame] | 45 | IF_DEPMOD(e->dnext = e->dprev = e;) |
Timo Teräs | 48dc80b | 2015-11-05 18:54:55 +0100 | [diff] [blame] | 46 | |
| 47 | return e; |
| 48 | } |
| 49 | module_entry* FAST_FUNC moddb_get(module_db *db, const char *module) |
| 50 | { |
| 51 | return helper_get_module(db, module, 0); |
| 52 | } |
| 53 | module_entry* FAST_FUNC moddb_get_or_create(module_db *db, const char *module) |
| 54 | { |
| 55 | return helper_get_module(db, module, 1); |
| 56 | } |
| 57 | |
| 58 | void FAST_FUNC moddb_free(module_db *db) |
| 59 | { |
| 60 | module_entry *e, *n; |
| 61 | unsigned i; |
| 62 | |
| 63 | for (i = 0; i < MODULE_HASH_SIZE; i++) { |
| 64 | for (e = db->buckets[i]; e; e = n) { |
| 65 | n = e->next; |
| 66 | free(e->name); |
| 67 | free(e->modname); |
| 68 | free(e); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 73 | void FAST_FUNC replace(char *s, char what, char with) |
| 74 | { |
| 75 | while (*s) { |
| 76 | if (what == *s) |
| 77 | *s = with; |
| 78 | ++s; |
| 79 | } |
| 80 | } |
| 81 | |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 82 | char* FAST_FUNC replace_underscores(char *s) |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 83 | { |
| 84 | replace(s, '-', '_'); |
| 85 | return s; |
| 86 | } |
| 87 | |
| 88 | int FAST_FUNC string_to_llist(char *string, llist_t **llist, const char *delim) |
| 89 | { |
| 90 | char *tok; |
| 91 | int len = 0; |
| 92 | |
| 93 | while ((tok = strsep(&string, delim)) != NULL) { |
| 94 | if (tok[0] == '\0') |
| 95 | continue; |
| 96 | llist_add_to_end(llist, xstrdup(tok)); |
| 97 | len += strlen(tok); |
| 98 | } |
| 99 | return len; |
| 100 | } |
| 101 | |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 102 | char* FAST_FUNC filename2modname(const char *filename, char *modname) |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 103 | { |
Denys Vlasenko | cc70b6f | 2015-01-24 22:30:30 +0100 | [diff] [blame] | 104 | char local_modname[MODULE_NAME_LEN]; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 105 | int i; |
Denys Vlasenko | 7885452 | 2015-01-01 19:02:40 +0100 | [diff] [blame] | 106 | const char *from; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 107 | |
| 108 | if (filename == NULL) |
| 109 | return NULL; |
| 110 | if (modname == NULL) |
Denys Vlasenko | cc70b6f | 2015-01-24 22:30:30 +0100 | [diff] [blame] | 111 | modname = local_modname; |
Denys Vlasenko | 7885452 | 2015-01-01 19:02:40 +0100 | [diff] [blame] | 112 | // Disabled since otherwise "modprobe dir/name" would work |
| 113 | // as if it is "modprobe name". It is unclear why |
| 114 | // 'basenamization' was here in the first place. |
| 115 | //from = bb_get_last_path_component_nostrip(filename); |
| 116 | from = filename; |
Denis Vlasenko | 48637e0 | 2009-02-26 12:00:52 +0000 | [diff] [blame] | 117 | for (i = 0; i < (MODULE_NAME_LEN-1) && from[i] != '\0' && from[i] != '.'; i++) |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 118 | modname[i] = (from[i] == '-') ? '_' : from[i]; |
Denys Vlasenko | 16bda3b | 2009-05-18 13:08:04 +0200 | [diff] [blame] | 119 | modname[i] = '\0'; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 120 | |
Denys Vlasenko | cc70b6f | 2015-01-24 22:30:30 +0100 | [diff] [blame] | 121 | if (modname == local_modname) |
| 122 | return xstrdup(modname); |
| 123 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 124 | return modname; |
| 125 | } |
| 126 | |
Denys Vlasenko | c5830bd | 2011-02-02 00:00:36 +0100 | [diff] [blame] | 127 | char* FAST_FUNC parse_cmdline_module_options(char **argv, int quote_spaces) |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 128 | { |
| 129 | char *options; |
| 130 | int optlen; |
| 131 | |
| 132 | options = xzalloc(1); |
| 133 | optlen = 0; |
| 134 | while (*++argv) { |
Denys Vlasenko | c5830bd | 2011-02-02 00:00:36 +0100 | [diff] [blame] | 135 | const char *fmt; |
| 136 | const char *var; |
| 137 | const char *val; |
| 138 | |
| 139 | var = *argv; |
| 140 | options = xrealloc(options, optlen + 2 + strlen(var) + 2); |
| 141 | fmt = "%.*s%s "; |
| 142 | val = strchrnul(var, '='); |
| 143 | if (quote_spaces) { |
| 144 | /* |
| 145 | * modprobe (module-init-tools version 3.11.1) compat: |
| 146 | * quote only value: |
| 147 | * var="val with spaces", not "var=val with spaces" |
| 148 | * (note: var *name* is not checked for spaces!) |
| 149 | */ |
| 150 | if (*val) { /* has var=val format. skip '=' */ |
| 151 | val++; |
| 152 | if (strchr(val, ' ')) |
| 153 | fmt = "%.*s\"%s\" "; |
| 154 | } |
| 155 | } |
| 156 | optlen += sprintf(options + optlen, fmt, (int)(val - var), var, val); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 157 | } |
Denys Vlasenko | c5830bd | 2011-02-02 00:00:36 +0100 | [diff] [blame] | 158 | /* Remove trailing space. Disabled */ |
| 159 | /* if (optlen != 0) options[optlen-1] = '\0'; */ |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 160 | return options; |
| 161 | } |
| 162 | |
Denys Vlasenko | 77c066e | 2009-10-25 04:35:22 +0100 | [diff] [blame] | 163 | #if ENABLE_FEATURE_INSMOD_TRY_MMAP |
| 164 | void* FAST_FUNC try_to_mmap_module(const char *filename, size_t *image_size_p) |
| 165 | { |
| 166 | /* We have user reports of failure to load 3MB module |
| 167 | * on a 16MB RAM machine. Apparently even a transient |
| 168 | * memory spike to 6MB during module load |
| 169 | * is too big for that system. */ |
| 170 | void *image; |
| 171 | struct stat st; |
| 172 | int fd; |
| 173 | |
| 174 | fd = xopen(filename, O_RDONLY); |
| 175 | fstat(fd, &st); |
| 176 | image = NULL; |
| 177 | /* st.st_size is off_t, we can't just pass it to mmap */ |
| 178 | if (st.st_size <= *image_size_p) { |
| 179 | size_t image_size = st.st_size; |
| 180 | image = mmap(NULL, image_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 181 | if (image == MAP_FAILED) { |
| 182 | image = NULL; |
| 183 | } else if (*(uint32_t*)image != SWAP_BE32(0x7f454C46)) { |
| 184 | /* No ELF signature. Compressed module? */ |
| 185 | munmap(image, image_size); |
| 186 | image = NULL; |
| 187 | } else { |
| 188 | /* Success. Report the size */ |
| 189 | *image_size_p = image_size; |
| 190 | } |
| 191 | } |
| 192 | close(fd); |
| 193 | return image; |
| 194 | } |
| 195 | #endif |
| 196 | |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 197 | /* Return: |
| 198 | * 0 on success, |
| 199 | * -errno on open/read error, |
| 200 | * errno on init_module() error |
| 201 | */ |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 202 | int FAST_FUNC bb_init_module(const char *filename, const char *options) |
| 203 | { |
Denys Vlasenko | 77c066e | 2009-10-25 04:35:22 +0100 | [diff] [blame] | 204 | size_t image_size; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 205 | char *image; |
Denis Vlasenko | 4144504 | 2009-04-13 20:32:31 +0000 | [diff] [blame] | 206 | int rc; |
Denys Vlasenko | 77c066e | 2009-10-25 04:35:22 +0100 | [diff] [blame] | 207 | bool mmaped; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 208 | |
Denis Vlasenko | 3b5c9b8 | 2009-03-13 23:43:26 +0000 | [diff] [blame] | 209 | if (!options) |
| 210 | options = ""; |
| 211 | |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 212 | //TODO: audit bb_init_module_24 to match error code convention |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 213 | #if ENABLE_FEATURE_2_4_MODULES |
| 214 | if (get_linux_version_code() < KERNEL_VERSION(2,6,0)) |
| 215 | return bb_init_module_24(filename, options); |
| 216 | #endif |
| 217 | |
Mike Frysinger | 3a45b87 | 2016-09-15 12:16:33 +0200 | [diff] [blame] | 218 | /* |
| 219 | * First we try finit_module if available. Some kernels are configured |
| 220 | * to only allow loading of modules off of secure storage (like a read- |
| 221 | * only rootfs) which needs the finit_module call. If it fails, we fall |
| 222 | * back to normal module loading to support compressed modules. |
| 223 | */ |
| 224 | # ifdef __NR_finit_module |
| 225 | { |
| 226 | int fd = open(filename, O_RDONLY | O_CLOEXEC); |
| 227 | if (fd >= 0) { |
| 228 | rc = finit_module(fd, options, 0) != 0; |
| 229 | close(fd); |
| 230 | if (rc == 0) |
| 231 | return rc; |
| 232 | } |
| 233 | } |
| 234 | # endif |
| 235 | |
Denys Vlasenko | 77c066e | 2009-10-25 04:35:22 +0100 | [diff] [blame] | 236 | image_size = INT_MAX - 4095; |
| 237 | mmaped = 0; |
| 238 | image = try_to_mmap_module(filename, &image_size); |
| 239 | if (image) { |
| 240 | mmaped = 1; |
| 241 | } else { |
| 242 | errno = ENOMEM; /* may be changed by e.g. open errors below */ |
| 243 | image = xmalloc_open_zipped_read_close(filename, &image_size); |
| 244 | if (!image) |
| 245 | return -errno; |
| 246 | } |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 247 | |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 248 | errno = 0; |
Denys Vlasenko | 77c066e | 2009-10-25 04:35:22 +0100 | [diff] [blame] | 249 | init_module(image, image_size, options); |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 250 | rc = errno; |
Denys Vlasenko | 77c066e | 2009-10-25 04:35:22 +0100 | [diff] [blame] | 251 | if (mmaped) |
| 252 | munmap(image, image_size); |
| 253 | else |
| 254 | free(image); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 255 | return rc; |
| 256 | } |
| 257 | |
| 258 | int FAST_FUNC bb_delete_module(const char *module, unsigned int flags) |
| 259 | { |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 260 | errno = 0; |
| 261 | delete_module(module, flags); |
| 262 | return errno; |
| 263 | } |
| 264 | |
Denys Vlasenko | cd13974 | 2015-10-24 04:17:04 +0200 | [diff] [blame] | 265 | /* Note: not suitable for delete_module() errnos. |
| 266 | * For them, probably only EWOULDBLOCK needs explaining: |
| 267 | * "Other modules depend on us". So far we don't do such |
| 268 | * translation and don't use moderror() for removal errors. |
| 269 | */ |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 270 | const char* FAST_FUNC moderror(int err) |
| 271 | { |
| 272 | switch (err) { |
| 273 | case -1: /* btw: it's -EPERM */ |
| 274 | return "no such module"; |
| 275 | case ENOEXEC: |
| 276 | return "invalid module format"; |
| 277 | case ENOENT: |
| 278 | return "unknown symbol in module, or unknown parameter"; |
| 279 | case ESRCH: |
| 280 | return "module has wrong symbol version"; |
| 281 | case ENOSYS: |
| 282 | return "kernel does not support requested operation"; |
| 283 | } |
| 284 | if (err < 0) /* should always be */ |
| 285 | err = -err; |
| 286 | return strerror(err); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 287 | } |