[PATCH] module-init-tools : insmod_correct_argv0_computation
[mit.git] / insmod.c
blob22270eddc0e53e9f703c5872eb7e2b2c5d822441
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 "backwards_compat.c"
33 #define streq(a,b) (strcmp((a),(b)) == 0)
35 extern long init_module(void *, unsigned long, const char *);
37 static void print_usage(const char *progname)
39 fprintf(stderr, "Usage: %s filename [args]\n", progname);
40 exit(1);
43 /* We use error numbers in a loose translation... */
44 static const char *moderror(int err)
46 switch (err) {
47 case ENOEXEC:
48 return "Invalid module format";
49 case ENOENT:
50 return "Unknown symbol in module";
51 case ESRCH:
52 return "Module has wrong symbol version";
53 case EINVAL:
54 return "Invalid parameters";
55 default:
56 return strerror(err);
60 static void *grab_file(const char *filename, unsigned long *size)
62 unsigned int max = 16384;
63 int ret, fd, err_save;
64 void *buffer = malloc(max);
65 if (!buffer)
66 return NULL;
68 if (streq(filename, "-"))
69 fd = dup(STDIN_FILENO);
70 else
71 fd = open(filename, O_RDONLY, 0);
73 if (fd < 0)
74 return NULL;
76 *size = 0;
77 while ((ret = read(fd, buffer + *size, max - *size)) > 0) {
78 *size += ret;
79 if (*size == max) {
80 void *p;
82 p = realloc(buffer, max *= 2);
83 if (!p)
84 goto out_error;
85 buffer = p;
88 if (ret < 0)
89 goto out_error;
91 close(fd);
92 return buffer;
94 out_error:
95 err_save = errno;
96 free(buffer);
97 close(fd);
98 errno = err_save;
99 return NULL;
102 int main(int argc, char *argv[])
104 unsigned int i;
105 long int ret;
106 unsigned long len;
107 void *file;
108 char *filename, *options = strdup("");
109 char *p, *progname = argv[0];
111 if (!options) {
112 fprintf(stderr,
113 "insmod: can't allocate memory: %s\n",
114 strerror(errno));
115 exit(1);
118 p = strrchr(argv[0], '/');
119 if (p)
120 p++;
121 else
122 p=argv[0];
123 if (strstr(p, "insmod.static"))
124 try_old_version("insmod.static", argv);
125 else
126 try_old_version("insmod", argv);
128 if (argv[1] && (streq(argv[1], "--version") || streq(argv[1], "-V"))) {
129 puts(PACKAGE " version " VERSION);
130 exit(0);
133 /* Ignore old options, for backwards compat. */
134 while (argv[1] && (streq(argv[1], "-p")
135 || streq(argv[1], "-s")
136 || streq(argv[1], "-f"))) {
137 argv++;
138 argc--;
141 filename = argv[1];
142 if (!filename)
143 print_usage(progname);
145 /* Rest is options */
146 for (i = 2; i < argc; i++) {
147 options = realloc(options,
148 strlen(options) + 1 + strlen(argv[i]) + 1);
149 if (!options) {
150 fprintf(stderr,
151 "insmod: can't allocate memory: %s\n",
152 strerror(errno));
153 exit(1);
155 strcat(options, argv[i]);
156 strcat(options, " ");
159 file = grab_file(filename, &len);
160 if (!file) {
161 fprintf(stderr, "insmod: can't read '%s': %s\n",
162 filename, strerror(errno));
163 exit(1);
166 ret = init_module(file, len, options);
167 if (ret != 0) {
168 fprintf(stderr, "insmod: error inserting '%s': %li %s\n",
169 filename, ret, moderror(errno));
170 exit(1);
172 exit(0);