Move more logging to common files
[mit.git] / insmod.c
blob0ce1d995a796eb03795ce9970ed72fd21d224367
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
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <sys/mman.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <asm/unistd.h>
31 #include "testing.h"
32 #include "backwards_compat.c"
34 #define streq(a,b) (strcmp((a),(b)) == 0)
36 extern long init_module(void *, unsigned long, const char *);
38 static void print_usage(const char *progname)
40 fprintf(stderr, "Usage: %s filename [args]\n", progname);
41 exit(1);
44 /* We use error numbers in a loose translation... */
45 static const char *moderror(int err)
47 switch (err) {
48 case ENOEXEC:
49 return "Invalid module format";
50 case ENOENT:
51 return "Unknown symbol in module";
52 case ESRCH:
53 return "Module has wrong symbol version";
54 case EINVAL:
55 return "Invalid parameters";
56 default:
57 return strerror(err);
61 static void *grab_file(const char *filename, unsigned long *size)
63 unsigned int max = 16384;
64 int ret, fd, err_save;
65 void *buffer = malloc(max);
66 if (!buffer)
67 return NULL;
69 if (streq(filename, "-"))
70 fd = dup(STDIN_FILENO);
71 else
72 fd = open(filename, O_RDONLY, 0);
74 if (fd < 0)
75 return NULL;
77 *size = 0;
78 while ((ret = read(fd, buffer + *size, max - *size)) > 0) {
79 *size += ret;
80 if (*size == max) {
81 void *p;
83 p = realloc(buffer, max *= 2);
84 if (!p)
85 goto out_error;
86 buffer = p;
89 if (ret < 0)
90 goto out_error;
92 close(fd);
93 return buffer;
95 out_error:
96 err_save = errno;
97 free(buffer);
98 close(fd);
99 errno = err_save;
100 return NULL;
103 int main(int argc, char *argv[])
105 unsigned int i;
106 long int ret;
107 unsigned long len;
108 void *file;
109 char *filename, *options = strdup("");
110 char *p, *progname = argv[0];
112 if (!options) {
113 fprintf(stderr,
114 "insmod: can't allocate memory: %s\n",
115 strerror(errno));
116 exit(1);
119 p = strrchr(argv[0], '/');
120 if (p)
121 p++;
122 else
123 p=argv[0];
124 if (strstr(p, "insmod.static"))
125 try_old_version("insmod.static", argv);
126 else
127 try_old_version("insmod", argv);
129 if (argv[1] && (streq(argv[1], "--version") || streq(argv[1], "-V"))) {
130 puts(PACKAGE " version " VERSION);
131 exit(0);
134 /* Ignore old options, for backwards compat. */
135 while (argv[1] && (streq(argv[1], "-p")
136 || streq(argv[1], "-s")
137 || streq(argv[1], "-f"))) {
138 argv++;
139 argc--;
142 filename = argv[1];
143 if (!filename)
144 print_usage(progname);
146 /* Rest is options */
147 for (i = 2; i < argc; i++) {
148 options = realloc(options,
149 strlen(options) + 1 + strlen(argv[i]) + 1);
150 if (!options) {
151 fprintf(stderr,
152 "insmod: can't allocate memory: %s\n",
153 strerror(errno));
154 exit(1);
156 strcat(options, argv[i]);
157 strcat(options, " ");
160 file = grab_file(filename, &len);
161 if (!file) {
162 fprintf(stderr, "insmod: can't read '%s': %s\n",
163 filename, strerror(errno));
164 exit(1);
167 ret = init_module(file, len, options);
168 if (ret != 0) {
169 fprintf(stderr, "insmod: error inserting '%s': %li %s\n",
170 filename, ret, moderror(errno));
171 exit(1);
173 exit(0);