blob: f437a9829c66b1cd95f64712871b155c031e629b [file] [log] [blame]
Denis Vlasenkoba1315d2008-09-13 14:59:38 +00001/*
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 Vlasenkoba1315d2008-09-13 14:59:38 +00008#include "modutils.h"
9
10#ifdef __UCLIBC__
11extern int init_module(void *module, unsigned long len, const char *options);
12extern 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 Vlasenkoba1315d2008-09-13 14:59:38 +000019void 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
28char * FAST_FUNC replace_underscores(char *s)
29{
30 replace(s, '-', '_');
31 return s;
32}
33
34int 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
48char * FAST_FUNC filename2modname(const char *filename, char *modname)
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 Vlasenko48637e02009-02-26 12:00:52 +000058 for (i = 0; i < (MODULE_NAME_LEN-1) && from[i] != '\0' && from[i] != '.'; i++)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000059 modname[i] = (from[i] == '-') ? '_' : from[i];
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020060 modname[i] = '\0';
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000061
62 return modname;
63}
64
65const char * FAST_FUNC moderror(int err)
66{
67 switch (err) {
68 case -1:
69 return "no such module";
70 case ENOEXEC:
71 return "invalid module format";
72 case ENOENT:
73 return "unknown symbol in module, or unknown parameter";
74 case ESRCH:
75 return "module has wrong symbol version";
76 case ENOSYS:
77 return "kernel does not support requested operation";
78 default:
79 return strerror(err);
80 }
81}
82
83char * FAST_FUNC parse_cmdline_module_options(char **argv)
84{
85 char *options;
86 int optlen;
87
88 options = xzalloc(1);
89 optlen = 0;
90 while (*++argv) {
91 options = xrealloc(options, optlen + 2 + strlen(*argv) + 2);
92 /* Spaces handled by "" pairs, but no way of escaping quotes */
Denis Vlasenkobb26db42008-10-31 02:04:28 +000093 optlen += sprintf(options + optlen, (strchr(*argv, ' ') ? "\"%s\" " : "%s "), *argv);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000094 }
95 return options;
96}
97
98int FAST_FUNC bb_init_module(const char *filename, const char *options)
99{
Denis Vlasenko41445042009-04-13 20:32:31 +0000100 size_t len;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000101 char *image;
Denis Vlasenko41445042009-04-13 20:32:31 +0000102 int rc;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000103
Denis Vlasenko3b5c9b82009-03-13 23:43:26 +0000104 if (!options)
105 options = "";
106
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000107#if ENABLE_FEATURE_2_4_MODULES
108 if (get_linux_version_code() < KERNEL_VERSION(2,6,0))
109 return bb_init_module_24(filename, options);
110#endif
111
112 /* Use the 2.6 way */
Denis Vlasenko41445042009-04-13 20:32:31 +0000113 len = INT_MAX - 4095;
114 rc = ENOENT;
Denis Vlasenkofc668922008-11-06 02:32:31 +0000115 image = xmalloc_open_zipped_read_close(filename, &len);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000116 if (image) {
Denis Vlasenko3b5c9b82009-03-13 23:43:26 +0000117 rc = 0;
Denis Vlasenko41445042009-04-13 20:32:31 +0000118 if (init_module(image, len, options) != 0)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000119 rc = errno;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000120 free(image);
121 }
122
123 return rc;
124}
125
126int FAST_FUNC bb_delete_module(const char *module, unsigned int flags)
127{
128 return delete_module(module, flags);
129}