2 * Mini insmod implementation for busybox
4 * Copyright (C) 1999 by Lineo, inc.
5 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
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.
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.
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
29 #include <sys/syscall.h>
30 #include <linux/module.h>
32 #define _PATH_MODULES "/lib/modules"
34 #warning "Danger Will Robinson, Danger!!!"
36 #warning "insmod is still under construction. Don't use it."
38 #warning " You have been warned!"
42 /* Some firendly syscalls to cheer everyone's day... */
43 _syscall2(int, init_module, const char *, name,
44 const struct module *, info)
47 _syscall1(int, delete_module, const char *, name)
49 extern int delete_module(const char *);
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
55 static inline _syscall2(long, _create_module, const char *, name, size_t, size)
56 unsigned long create_module(const char *name, size_t size)
58 long ret = _create_module(name, size);
59 if (ret == -1 && errno > 125) {
66 _syscall2(unsigned long, create_module, const char *, name, size_t, size)
70 static char m_filename[PATH_MAX] = "\0";
71 static char m_fullName[PATH_MAX] ="\0";
72 static const char insmod_usage[] =
73 "insmod [OPTION]... MODULE [symbol=value]...\n\n"
74 "Loads the specified kernel modules into the kernel.\n\n"
76 "\t-f\tForce module to load into the wrong kernel version.\n"
77 "\t-k\tMake module autoclean-able.\n";
80 static int findNamedModule(const char *fileName, struct stat* statbuf)
82 if (m_fullName[0]=='\0')
85 char* tmp = strrchr( fileName, '/');
87 tmp = (char*)fileName;
90 if (check_wildcard_match(tmp, m_fullName) == TRUE) {
91 /* Stop searching if we find a match */
92 memcpy(m_filename, fileName, strlen(fileName));
100 extern int insmod_main(int argc, char **argv)
104 char m_name[PATH_MAX] ="\0";
108 usage( insmod_usage);
111 /* Parse any options */
112 while (--argc > 0 && **(++argv) == '-') {
113 while (*(++(*argv))) {
128 /* Grab the module name */
129 if ((tmp = strrchr(*argv, '/')) != NULL)
135 if (len > 2 && tmp[len - 2] == '.' && tmp[len - 1] == 'o')
137 memcpy(m_name, tmp, len);
138 strcpy(m_fullName, m_name);
139 strcat(m_fullName, ".o");
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");
152 memcpy(m_filename, *argv, strlen(*argv));
155 fprintf(stderr, "m_filename='%s'\n", m_filename);
156 fprintf(stderr, "m_name='%s'\n", m_name);
159 /* TODO: do something roughtly like this... */
162 if ((f = obj_load(fp)) == NULL) {
163 perror("Could not load the module\n");
167 /* Let the module know about the kernel symbols. */
168 add_kernel_symbols(f);
170 if (!create_this_module(f, m_name)) {
171 perror("Could not create the module\n");
175 if (!obj_check_undefineds(f, quiet)) {
176 perror("Undefined symbols in the module\n");
179 obj_allocate_commons(f);
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");
189 /* Find current size of the module */
190 m_size = obj_load_size(f);
194 m_addr = create_module(m_name, m_size);
198 perror("create_module: %m");