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) |
| 16 | # define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags) |
| 17 | #endif |
| 18 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 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 | |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 28 | char* FAST_FUNC replace_underscores(char *s) |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 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 | |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 48 | char* FAST_FUNC filename2modname(const char *filename, char *modname) |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 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); |
Denis Vlasenko | 48637e0 | 2009-02-26 12:00:52 +0000 | [diff] [blame] | 58 | 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] | 59 | modname[i] = (from[i] == '-') ? '_' : from[i]; |
Denys Vlasenko | 16bda3b | 2009-05-18 13:08:04 +0200 | [diff] [blame] | 60 | modname[i] = '\0'; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 61 | |
| 62 | return modname; |
| 63 | } |
| 64 | |
Denys Vlasenko | c5830bd | 2011-02-02 00:00:36 +0100 | [diff] [blame] | 65 | char* FAST_FUNC parse_cmdline_module_options(char **argv, int quote_spaces) |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 66 | { |
| 67 | char *options; |
| 68 | int optlen; |
| 69 | |
| 70 | options = xzalloc(1); |
| 71 | optlen = 0; |
| 72 | while (*++argv) { |
Denys Vlasenko | c5830bd | 2011-02-02 00:00:36 +0100 | [diff] [blame] | 73 | const char *fmt; |
| 74 | const char *var; |
| 75 | const char *val; |
| 76 | |
| 77 | var = *argv; |
| 78 | options = xrealloc(options, optlen + 2 + strlen(var) + 2); |
| 79 | fmt = "%.*s%s "; |
| 80 | val = strchrnul(var, '='); |
| 81 | if (quote_spaces) { |
| 82 | /* |
| 83 | * modprobe (module-init-tools version 3.11.1) compat: |
| 84 | * quote only value: |
| 85 | * var="val with spaces", not "var=val with spaces" |
| 86 | * (note: var *name* is not checked for spaces!) |
| 87 | */ |
| 88 | if (*val) { /* has var=val format. skip '=' */ |
| 89 | val++; |
| 90 | if (strchr(val, ' ')) |
| 91 | fmt = "%.*s\"%s\" "; |
| 92 | } |
| 93 | } |
| 94 | optlen += sprintf(options + optlen, fmt, (int)(val - var), var, val); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 95 | } |
Denys Vlasenko | c5830bd | 2011-02-02 00:00:36 +0100 | [diff] [blame] | 96 | /* Remove trailing space. Disabled */ |
| 97 | /* if (optlen != 0) options[optlen-1] = '\0'; */ |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 98 | return options; |
| 99 | } |
| 100 | |
Denys Vlasenko | 77c066e | 2009-10-25 04:35:22 +0100 | [diff] [blame] | 101 | #if ENABLE_FEATURE_INSMOD_TRY_MMAP |
| 102 | void* FAST_FUNC try_to_mmap_module(const char *filename, size_t *image_size_p) |
| 103 | { |
| 104 | /* We have user reports of failure to load 3MB module |
| 105 | * on a 16MB RAM machine. Apparently even a transient |
| 106 | * memory spike to 6MB during module load |
| 107 | * is too big for that system. */ |
| 108 | void *image; |
| 109 | struct stat st; |
| 110 | int fd; |
| 111 | |
| 112 | fd = xopen(filename, O_RDONLY); |
| 113 | fstat(fd, &st); |
| 114 | image = NULL; |
| 115 | /* st.st_size is off_t, we can't just pass it to mmap */ |
| 116 | if (st.st_size <= *image_size_p) { |
| 117 | size_t image_size = st.st_size; |
| 118 | image = mmap(NULL, image_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 119 | if (image == MAP_FAILED) { |
| 120 | image = NULL; |
| 121 | } else if (*(uint32_t*)image != SWAP_BE32(0x7f454C46)) { |
| 122 | /* No ELF signature. Compressed module? */ |
| 123 | munmap(image, image_size); |
| 124 | image = NULL; |
| 125 | } else { |
| 126 | /* Success. Report the size */ |
| 127 | *image_size_p = image_size; |
| 128 | } |
| 129 | } |
| 130 | close(fd); |
| 131 | return image; |
| 132 | } |
| 133 | #endif |
| 134 | |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 135 | /* Return: |
| 136 | * 0 on success, |
| 137 | * -errno on open/read error, |
| 138 | * errno on init_module() error |
| 139 | */ |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 140 | int FAST_FUNC bb_init_module(const char *filename, const char *options) |
| 141 | { |
Denys Vlasenko | 77c066e | 2009-10-25 04:35:22 +0100 | [diff] [blame] | 142 | size_t image_size; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 143 | char *image; |
Denis Vlasenko | 4144504 | 2009-04-13 20:32:31 +0000 | [diff] [blame] | 144 | int rc; |
Denys Vlasenko | 77c066e | 2009-10-25 04:35:22 +0100 | [diff] [blame] | 145 | bool mmaped; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 146 | |
Denis Vlasenko | 3b5c9b8 | 2009-03-13 23:43:26 +0000 | [diff] [blame] | 147 | if (!options) |
| 148 | options = ""; |
| 149 | |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 150 | //TODO: audit bb_init_module_24 to match error code convention |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 151 | #if ENABLE_FEATURE_2_4_MODULES |
| 152 | if (get_linux_version_code() < KERNEL_VERSION(2,6,0)) |
| 153 | return bb_init_module_24(filename, options); |
| 154 | #endif |
| 155 | |
Denys Vlasenko | 77c066e | 2009-10-25 04:35:22 +0100 | [diff] [blame] | 156 | image_size = INT_MAX - 4095; |
| 157 | mmaped = 0; |
| 158 | image = try_to_mmap_module(filename, &image_size); |
| 159 | if (image) { |
| 160 | mmaped = 1; |
| 161 | } else { |
| 162 | errno = ENOMEM; /* may be changed by e.g. open errors below */ |
| 163 | image = xmalloc_open_zipped_read_close(filename, &image_size); |
| 164 | if (!image) |
| 165 | return -errno; |
| 166 | } |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 167 | |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 168 | errno = 0; |
Denys Vlasenko | 77c066e | 2009-10-25 04:35:22 +0100 | [diff] [blame] | 169 | init_module(image, image_size, options); |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 170 | rc = errno; |
Denys Vlasenko | 77c066e | 2009-10-25 04:35:22 +0100 | [diff] [blame] | 171 | if (mmaped) |
| 172 | munmap(image, image_size); |
| 173 | else |
| 174 | free(image); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 175 | return rc; |
| 176 | } |
| 177 | |
| 178 | int FAST_FUNC bb_delete_module(const char *module, unsigned int flags) |
| 179 | { |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 180 | errno = 0; |
| 181 | delete_module(module, flags); |
| 182 | return errno; |
| 183 | } |
| 184 | |
| 185 | const char* FAST_FUNC moderror(int err) |
| 186 | { |
| 187 | switch (err) { |
| 188 | case -1: /* btw: it's -EPERM */ |
| 189 | return "no such module"; |
| 190 | case ENOEXEC: |
| 191 | return "invalid module format"; |
| 192 | case ENOENT: |
| 193 | return "unknown symbol in module, or unknown parameter"; |
| 194 | case ESRCH: |
| 195 | return "module has wrong symbol version"; |
| 196 | case ENOSYS: |
| 197 | return "kernel does not support requested operation"; |
| 198 | } |
| 199 | if (err < 0) /* should always be */ |
| 200 | err = -err; |
| 201 | return strerror(err); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 202 | } |