blob: 9cb635f40385d7a77a61bc9a077640175bed9997 [file] [log] [blame]
Erik Andersen02104321999-12-17 18:57:34 +00001/*
2 * Mini insmod implementation for busybox
3 *
4 * Copyright (C) 1999 by Lineo, inc.
5 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#include "internal.h"
24#include <stdlib.h>
25#include <stdio.h>
26#include <errno.h>
27#include <unistd.h>
28#include <dirent.h>
29#include <sys/syscall.h>
30#include <linux/module.h>
31
Erik Andersend387d011999-12-21 02:55:11 +000032#define _PATH_MODULES "/lib/modules"
33
34#warning "Danger Will Robinson, Danger!!!"
35#warning " "
36#warning "insmod is still under construction. Don't use it."
37#warning " "
38#warning " You have been warned!"
39#warning " "
40
41
Erik Andersen02104321999-12-17 18:57:34 +000042/* Some firendly syscalls to cheer everyone's day... */
43_syscall2(int, init_module, const char *, name,
44 const struct module *, info)
45
46#ifndef BB_RMMOD
47_syscall1(int, delete_module, const char *, name)
48#else
49extern int delete_module(const char *);
50#endif
51
52#if defined(__i386__) || defined(__m68k__) || defined(__arm__)
53/* Jump through hoops to fixup error return codes */
54#define __NR__create_module __NR_create_module
55static inline _syscall2(long, _create_module, const char *, name, size_t, size)
56unsigned long create_module(const char *name, size_t size)
57{
58 long ret = _create_module(name, size);
59 if (ret == -1 && errno > 125) {
60 ret = -errno;
61 errno = 0;
62 }
63 return ret;
64}
65#else
66_syscall2(unsigned long, create_module, const char *, name, size_t, size)
67#endif
68
69
Erik Andersend387d011999-12-21 02:55:11 +000070static char m_filename[PATH_MAX] = "\0";
71static char m_fullName[PATH_MAX] ="\0";
Erik Andersen02104321999-12-17 18:57:34 +000072static const char insmod_usage[] =
73 "insmod [OPTION]... MODULE [symbol=value]...\n\n"
74 "Loads the specified kernel modules into the kernel.\n\n"
75 "Options:\n"
76 "\t-f\tForce module to load into the wrong kernel version.\n"
77 "\t-k\tMake module autoclean-able.\n";
78
79
Erik Andersend387d011999-12-21 02:55:11 +000080static int findNamedModule(const char *fileName, struct stat* statbuf)
81{
82 if (m_fullName[0]=='\0')
83 return( FALSE);
84 else {
85 char* tmp = strrchr( fileName, '/');
86 if (tmp == NULL)
87 tmp = (char*)fileName;
88 else
89 tmp++;
90 if (check_wildcard_match(tmp, m_fullName) == TRUE) {
91 /* Stop searching if we find a match */
92 memcpy(m_filename, fileName, strlen(fileName));
93 return( FALSE);
94 }
95 }
96 return( TRUE);
97}
98
Erik Andersen02104321999-12-17 18:57:34 +000099
100extern int insmod_main(int argc, char **argv)
101{
102 int len;
Erik Andersend387d011999-12-21 02:55:11 +0000103 char *tmp;
104 char m_name[PATH_MAX] ="\0";
105 FILE *fp;
Erik Andersen02104321999-12-17 18:57:34 +0000106
107 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);
121 }
122 }
123 }
124
125 if (argc <= 0 )
126 usage(insmod_usage);
127
128 /* Grab the module name */
129 if ((tmp = strrchr(*argv, '/')) != NULL)
130 tmp++;
131 else
132 tmp = *argv;
133 len = strlen(tmp);
134
135 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o')
136 len -= 2;
137 memcpy(m_name, tmp, len);
Erik Andersend387d011999-12-21 02:55:11 +0000138 strcpy(m_fullName, m_name);
139 strcat(m_fullName, ".o");
Erik Andersen02104321999-12-17 18:57:34 +0000140
Erik Andersend387d011999-12-21 02:55:11 +0000141 /* 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 }
151 } else
152 memcpy(m_filename, *argv, strlen(*argv));
153
154
155 fprintf(stderr, "m_filename='%s'\n", m_filename);
Erik Andersen02104321999-12-17 18:57:34 +0000156 fprintf(stderr, "m_name='%s'\n", m_name);
157
Erik Andersend387d011999-12-21 02:55:11 +0000158
159 /* TODO: do something roughtly like this... */
160#if 0
161
162 if ((f = obj_load(fp)) == NULL) {
163 perror("Could not load the module\n");
164 exit( FALSE);
165 }
Erik Andersen02104321999-12-17 18:57:34 +0000166
Erik Andersend387d011999-12-21 02:55:11 +0000167 /* 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 }
187 }
188
189 /* Find current size of the module */
190 m_size = obj_load_size(f);
191
192
193 errno = 0;
194 m_addr = create_module(m_name, m_size);
195 switch (errno) {
196 /* yada yada */
197 default:
198 perror("create_module: %m");
199
200 }
201
202#endif
203
204 fclose( fp);
Erik Andersen02104321999-12-17 18:57:34 +0000205 exit( TRUE);
206}