aboutsummaryrefslogtreecommitdiff
path: root/insmod.c
diff options
context:
space:
mode:
Diffstat (limited to 'insmod.c')
-rw-r--r--insmod.c240
1 files changed, 122 insertions, 118 deletions
diff --git a/insmod.c b/insmod.c
index 31cb11261..7cbbed441 100644
--- a/insmod.c
+++ b/insmod.c
@@ -1,3 +1,4 @@
1/* vi: set sw=4 ts=4: */
1/* 2/*
2 * Mini insmod implementation for busybox 3 * Mini insmod implementation for busybox
3 * 4 *
@@ -41,8 +42,7 @@
41 42
42/* Some firendly syscalls to cheer everyone's day... */ 43/* Some firendly syscalls to cheer everyone's day... */
43_syscall2(int, init_module, const char *, name, 44_syscall2(int, init_module, const char *, name,
44 const struct module *, info) 45 const struct module *, info)
45
46#ifndef BB_RMMOD 46#ifndef BB_RMMOD
47_syscall1(int, delete_module, const char *, name) 47_syscall1(int, delete_module, const char *, name)
48#else 48#else
@@ -52,155 +52,159 @@ extern int delete_module(const char *);
52#if defined(__i386__) || defined(__m68k__) || defined(__arm__) 52#if defined(__i386__) || defined(__m68k__) || defined(__arm__)
53/* Jump through hoops to fixup error return codes */ 53/* Jump through hoops to fixup error return codes */
54#define __NR__create_module __NR_create_module 54#define __NR__create_module __NR_create_module
55static inline _syscall2(long, _create_module, const char *, name, size_t, size) 55static inline _syscall2(long, _create_module, const char *, name, size_t,
56 size)
56unsigned long create_module(const char *name, size_t size) 57unsigned long create_module(const char *name, size_t size)
57{ 58{
58 long ret = _create_module(name, size); 59 long ret = _create_module(name, size);
59 if (ret == -1 && errno > 125) { 60
60 ret = -errno; 61 if (ret == -1 && errno > 125) {
61 errno = 0; 62 ret = -errno;
62 } 63 errno = 0;
63 return ret; 64 }
65 return ret;
64} 66}
65#else 67#else
66_syscall2(unsigned long, create_module, const char *, name, size_t, size) 68_syscall2(unsigned long, create_module, const char *, name, size_t, size)
67#endif 69#endif
68
69
70static char m_filename[PATH_MAX + 1] = "\0"; 70static char m_filename[PATH_MAX + 1] = "\0";
71static char m_fullName[PATH_MAX + 1] = "\0"; 71static char m_fullName[PATH_MAX + 1] = "\0";
72static const char insmod_usage[] = 72static const char insmod_usage[] =
73 "insmod [OPTION]... MODULE [symbol=value]...\n\n" 73 "insmod [OPTION]... MODULE [symbol=value]...\n\n"
74 "Loads the specified kernel modules into the kernel.\n\n" 74 "Loads the specified kernel modules into the kernel.\n\n"
75 "Options:\n" 75 "Options:\n"
76 "\t-f\tForce module to load into the wrong kernel version.\n" 76
77 "\t-k\tMake module autoclean-able.\n"; 77 "\t-f\tForce module to load into the wrong kernel version.\n"
78 "\t-k\tMake module autoclean-able.\n";
78 79
79 80
80static int findNamedModule(const char *fileName, struct stat* statbuf) 81static int findNamedModule(const char *fileName, struct stat *statbuf)
81{ 82{
82 if (m_fullName[0]=='\0') 83 if (m_fullName[0] == '\0')
83 return( FALSE); 84 return (FALSE);
84 else { 85 else {
85 char* tmp = strrchr( fileName, '/'); 86 char *tmp = strrchr(fileName, '/');
86 if (tmp == NULL) 87
87 tmp = (char*)fileName; 88 if (tmp == NULL)
88 else 89 tmp = (char *) fileName;
89 tmp++; 90 else
90 if (check_wildcard_match(tmp, m_fullName) == TRUE) { 91 tmp++;
91 /* Stop searching if we find a match */ 92 if (check_wildcard_match(tmp, m_fullName) == TRUE) {
92 memcpy(m_filename, fileName, strlen(fileName)); 93 /* Stop searching if we find a match */
93 return( FALSE); 94 memcpy(m_filename, fileName, strlen(fileName));
95 return (FALSE);
96 }
94 } 97 }
95 } 98 return (TRUE);
96 return( TRUE);
97} 99}
98 100
99 101
100extern int insmod_main(int argc, char **argv) 102extern int insmod_main(int argc, char **argv)
101{ 103{
102 int len; 104 int len;
103 char *tmp; 105 char *tmp;
104 char m_name[PATH_MAX + 1] ="\0"; 106 char m_name[PATH_MAX + 1] = "\0";
105 FILE *fp; 107 FILE *fp;
106 108
107 if (argc<=1) { 109 if (argc <= 1) {
108 usage( insmod_usage);
109 }
110
111 /* Parse any options */
112 while (--argc > 0 && **(++argv) == '-') {
113 while (*(++(*argv))) {
114 switch (**argv) {
115 case 'f':
116 break;
117 case 'k':
118 break;
119 default:
120 usage(insmod_usage); 110 usage(insmod_usage);
121 }
122 } 111 }
123 } 112
124 113 /* Parse any options */
125 if (argc <= 0 ) 114 while (--argc > 0 && **(++argv) == '-') {
126 usage(insmod_usage); 115 while (*(++(*argv))) {
127 116 switch (**argv) {
128 /* Grab the module name */ 117 case 'f':
129 if ((tmp = strrchr(*argv, '/')) != NULL) 118 break;
130 tmp++; 119 case 'k':
131 else 120 break;
132 tmp = *argv; 121 default:
133 len = strlen(tmp); 122 usage(insmod_usage);
134 123 }
135 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o') 124 }
136 len -= 2;
137 memcpy(m_name, tmp, len);
138 strcpy(m_fullName, m_name);
139 strcat(m_fullName, ".o");
140
141 /* Get a filedesc for the module */
142 if ((fp = fopen(*argv, "r")) == NULL) {
143 /* Hmpf. Could not open it. Search through _PATH_MODULES to find a module named m_name */
144 if (recursiveAction(_PATH_MODULES, TRUE, FALSE, FALSE,
145 findNamedModule, findNamedModule) == FALSE) {
146 if ( m_filename[0] == '\0' || ((fp = fopen(m_filename, "r")) == NULL)) {
147 perror("No module by that name found in " _PATH_MODULES "\n");
148 exit( FALSE);
149 }
150 } 125 }
151 } else
152 memcpy(m_filename, *argv, strlen(*argv));
153 126
127 if (argc <= 0)
128 usage(insmod_usage);
154 129
155 fprintf(stderr, "m_filename='%s'\n", m_filename); 130 /* Grab the module name */
156 fprintf(stderr, "m_name='%s'\n", m_name); 131 if ((tmp = strrchr(*argv, '/')) != NULL)
132 tmp++;
133 else
134 tmp = *argv;
135 len = strlen(tmp);
136
137 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o')
138 len -= 2;
139 memcpy(m_name, tmp, len);
140 strcpy(m_fullName, m_name);
141 strcat(m_fullName, ".o");
142
143 /* Get a filedesc for the module */
144 if ((fp = fopen(*argv, "r")) == NULL) {
145 /* Hmpf. Could not open it. Search through _PATH_MODULES to find a module named m_name */
146 if (recursiveAction(_PATH_MODULES, TRUE, FALSE, FALSE,
147 findNamedModule, findNamedModule) == FALSE) {
148 if (m_filename[0] == '\0'
149 || ((fp = fopen(m_filename, "r")) == NULL)) {
150 perror("No module by that name found in " _PATH_MODULES
151 "\n");
152 exit(FALSE);
153 }
154 }
155 } else
156 memcpy(m_filename, *argv, strlen(*argv));
157
158
159 fprintf(stderr, "m_filename='%s'\n", m_filename);
160 fprintf(stderr, "m_name='%s'\n", m_name);
161
162
163 /* TODO: do something roughtly like this... */
164#if 0
157 165
166 if ((f = obj_load(fp)) == NULL) {
167 perror("Could not load the module\n");
168 exit(FALSE);
169 }
158 170
159 /* TODO: do something roughtly like this... */ 171 /* Let the module know about the kernel symbols. */
160#if 0 172 add_kernel_symbols(f);
161 173
162 if ((f = obj_load(fp)) == NULL) { 174 if (!create_this_module(f, m_name)) {
163 perror("Could not load the module\n"); 175 perror("Could not create the module\n");
164 exit( FALSE); 176 exit(FALSE);
165 }
166
167 /* Let the module know about the kernel symbols. */
168 add_kernel_symbols(f);
169
170 if (!create_this_module(f, m_name)) {
171 perror("Could not create the module\n");
172 exit( FALSE);
173 }
174
175 if (!obj_check_undefineds(f, quiet)) {
176 perror("Undefined symbols in the module\n");
177 exit( FALSE);
178 }
179 obj_allocate_commons(f);
180
181 /* Perse the module's arguments */
182 while (argc-- >0 && *(argv++) != '\0') {
183 if (!process_module_arguments(f, argc - optind, argv + optind)) {
184 perror("Undefined symbols in the module\n");
185 exit( FALSE);
186 } 177 }
187 }
188 178
189 /* Find current size of the module */ 179 if (!obj_check_undefineds(f, quiet)) {
190 m_size = obj_load_size(f); 180 perror("Undefined symbols in the module\n");
181 exit(FALSE);
182 }
183 obj_allocate_commons(f);
184
185 /* Perse the module's arguments */
186 while (argc-- > 0 && *(argv++) != '\0') {
187 if (!process_module_arguments(f, argc - optind, argv + optind)) {
188 perror("Undefined symbols in the module\n");
189 exit(FALSE);
190 }
191 }
192
193 /* Find current size of the module */
194 m_size = obj_load_size(f);
191 195
192 196
193 errno = 0; 197 errno = 0;
194 m_addr = create_module(m_name, m_size); 198 m_addr = create_module(m_name, m_size);
195 switch (errno) { 199 switch (errno) {
196 /* yada yada */ 200 /* yada yada */
197 default: 201 default:
198 perror("create_module: %m"); 202 perror("create_module: %m");
199 203
200 } 204 }
201 205
202#endif 206#endif
203 207
204 fclose( fp); 208 fclose(fp);
205 exit( TRUE); 209 exit(TRUE);
206} 210}