modutils.c 6.5 KB

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