1 /* insmod.c: insert a module into the kernel.
2 Copyright (C) 2001 Rusty Russell.
3 Copyright (C) 2002 Rusty Russell, IBM Corporation.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <sys/types.h>
29 #include <asm/unistd.h>
34 extern long init_module(void *, unsigned long, const char *);
36 static void print_usage(const char *progname
)
38 fprintf(stderr
, "Usage: %s filename [args]\n", progname
);
42 /* We use error numbers in a loose translation... */
43 static const char *moderror(int err
)
47 return "Invalid module format";
49 return "Unknown symbol in module";
51 return "Module has wrong symbol version";
53 return "Invalid parameters";
59 static void *grab_file(const char *filename
, unsigned long *size
)
61 unsigned int max
= 16384;
62 int ret
, fd
, err_save
;
63 void *buffer
= malloc(max
);
67 if (streq(filename
, "-"))
68 fd
= dup(STDIN_FILENO
);
70 fd
= open(filename
, O_RDONLY
, 0);
76 while ((ret
= read(fd
, buffer
+ *size
, max
- *size
)) > 0) {
81 p
= realloc(buffer
, max
*= 2);
101 int main(int argc
, char *argv
[])
107 char *filename
, *options
= strdup("");
108 char *p
, *progname
= argv
[0];
112 "insmod: can't allocate memory: %s\n",
117 p
= my_basename(argv
[0]);
119 if (argv
[1] && (streq(argv
[1], "--version") || streq(argv
[1], "-V"))) {
120 puts(PACKAGE
" version " VERSION
);
124 /* Ignore old options, for backwards compat. */
125 while (argv
[1] && (streq(argv
[1], "-p")
126 || streq(argv
[1], "-s")
127 || streq(argv
[1], "-f"))) {
134 print_usage(progname
);
136 /* Rest is options */
137 for (i
= 2; i
< argc
; i
++) {
138 options
= realloc(options
,
139 strlen(options
) + 1 + strlen(argv
[i
]) + 1);
142 "insmod: can't allocate memory: %s\n",
146 strcat(options
, argv
[i
]);
147 strcat(options
, " ");
150 file
= grab_file(filename
, &len
);
152 fprintf(stderr
, "insmod: can't read '%s': %s\n",
153 filename
, strerror(errno
));
157 ret
= init_module(file
, len
, options
);
159 fprintf(stderr
, "insmod: error inserting '%s': %li %s\n",
160 filename
, ret
, moderror(errno
));