3
0

modutils.c 6.6 KB

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