depmod: use real regex matching on depmod "overrides" entries
[mit.git] / insmod.c
blob3a2a910a29dffdad11b16cdefadc836bbc61fa1a
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 "util.h"
32 #include "testing.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);
39 exit(1);
42 /* We use error numbers in a loose translation... */
43 static const char *moderror(int err)
45 switch (err) {
46 case ENOEXEC:
47 return "Invalid module format";
48 case ENOENT:
49 return "Unknown symbol in module";
50 case ESRCH:
51 return "Module has wrong symbol version";
52 case EINVAL:
53 return "Invalid parameters";
54 default:
55 return strerror(err);
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);
64 if (!buffer)
65 return NULL;
67 if (streq(filename, "-"))
68 fd = dup(STDIN_FILENO);
69 else
70 fd = open(filename, O_RDONLY, 0);
72 if (fd < 0)
73 return NULL;
75 *size = 0;
76 while ((ret = read(fd, buffer + *size, max - *size)) > 0) {
77 *size += ret;
78 if (*size == max) {
79 void *p;
81 p = realloc(buffer, max *= 2);
82 if (!p)
83 goto out_error;
84 buffer = p;
87 if (ret < 0)
88 goto out_error;
90 close(fd);
91 return buffer;
93 out_error:
94 err_save = errno;
95 free(buffer);
96 close(fd);
97 errno = err_save;
98 return NULL;
101 int main(int argc, char *argv[])
103 unsigned int i;
104 long int ret;
105 unsigned long len;
106 void *file;
107 char *filename, *options = strdup("");
108 char *p, *progname = argv[0];
110 if (!options) {
111 fprintf(stderr,
112 "insmod: can't allocate memory: %s\n",
113 strerror(errno));
114 exit(1);
117 p = my_basename(argv[0]);
119 if (argv[1] && (streq(argv[1], "--version") || streq(argv[1], "-V"))) {
120 puts(PACKAGE " version " VERSION);
121 exit(0);
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"))) {
128 argv++;
129 argc--;
132 filename = argv[1];
133 if (!filename)
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);
140 if (!options) {
141 fprintf(stderr,
142 "insmod: can't allocate memory: %s\n",
143 strerror(errno));
144 exit(1);
146 strcat(options, argv[i]);
147 strcat(options, " ");
150 file = grab_file(filename, &len);
151 if (!file) {
152 fprintf(stderr, "insmod: can't read '%s': %s\n",
153 filename, strerror(errno));
154 exit(1);
157 ret = init_module(file, len, options);
158 if (ret != 0) {
159 fprintf(stderr, "insmod: error inserting '%s': %li %s\n",
160 filename, ret, moderror(errno));
161 exit(1);
163 exit(0);