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 | * |
| 6 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 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 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 65 | char * FAST_FUNC parse_cmdline_module_options(char **argv) |
| 66 | { |
| 67 | char *options; |
| 68 | int optlen; |
| 69 | |
| 70 | options = xzalloc(1); |
| 71 | optlen = 0; |
| 72 | while (*++argv) { |
| 73 | options = xrealloc(options, optlen + 2 + strlen(*argv) + 2); |
| 74 | /* Spaces handled by "" pairs, but no way of escaping quotes */ |
Denis Vlasenko | bb26db4 | 2008-10-31 02:04:28 +0000 | [diff] [blame] | 75 | optlen += sprintf(options + optlen, (strchr(*argv, ' ') ? "\"%s\" " : "%s "), *argv); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 76 | } |
| 77 | return options; |
| 78 | } |
| 79 | |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 80 | /* Return: |
| 81 | * 0 on success, |
| 82 | * -errno on open/read error, |
| 83 | * errno on init_module() error |
| 84 | */ |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 85 | int FAST_FUNC bb_init_module(const char *filename, const char *options) |
| 86 | { |
Denis Vlasenko | 4144504 | 2009-04-13 20:32:31 +0000 | [diff] [blame] | 87 | size_t len; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 88 | char *image; |
Denis Vlasenko | 4144504 | 2009-04-13 20:32:31 +0000 | [diff] [blame] | 89 | int rc; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 90 | |
Denis Vlasenko | 3b5c9b8 | 2009-03-13 23:43:26 +0000 | [diff] [blame] | 91 | if (!options) |
| 92 | options = ""; |
| 93 | |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 94 | //TODO: audit bb_init_module_24 to match error code convention |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 95 | #if ENABLE_FEATURE_2_4_MODULES |
| 96 | if (get_linux_version_code() < KERNEL_VERSION(2,6,0)) |
| 97 | return bb_init_module_24(filename, options); |
| 98 | #endif |
| 99 | |
| 100 | /* Use the 2.6 way */ |
Denis Vlasenko | 4144504 | 2009-04-13 20:32:31 +0000 | [diff] [blame] | 101 | len = INT_MAX - 4095; |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 102 | errno = ENOMEM; /* may be changed by e.g. open errors below */ |
Denis Vlasenko | fc66892 | 2008-11-06 02:32:31 +0000 | [diff] [blame] | 103 | image = xmalloc_open_zipped_read_close(filename, &len); |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 104 | if (!image) |
| 105 | return -errno; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 106 | |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 107 | errno = 0; |
| 108 | init_module(image, len, options); |
| 109 | rc = errno; |
| 110 | free(image); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 111 | return rc; |
| 112 | } |
| 113 | |
| 114 | int FAST_FUNC bb_delete_module(const char *module, unsigned int flags) |
| 115 | { |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 116 | errno = 0; |
| 117 | delete_module(module, flags); |
| 118 | return errno; |
| 119 | } |
| 120 | |
| 121 | const char* FAST_FUNC moderror(int err) |
| 122 | { |
| 123 | switch (err) { |
| 124 | case -1: /* btw: it's -EPERM */ |
| 125 | return "no such module"; |
| 126 | case ENOEXEC: |
| 127 | return "invalid module format"; |
| 128 | case ENOENT: |
| 129 | return "unknown symbol in module, or unknown parameter"; |
| 130 | case ESRCH: |
| 131 | return "module has wrong symbol version"; |
| 132 | case ENOSYS: |
| 133 | return "kernel does not support requested operation"; |
| 134 | } |
| 135 | if (err < 0) /* should always be */ |
| 136 | err = -err; |
| 137 | return strerror(err); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 138 | } |