tizen 2.0
[external/module-init-tools.git] / insmod.c
1 /* insmod.c: insert a module into the kernel.
2     Copyright (C) 2001  Rusty Russell.
3     Copyright (C) 2002  Rusty Russell, IBM Corporation.
4
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.
9
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.
14
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
18 */
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>
30
31 #include "util.h"
32 #include "testing.h"
33
34 extern long init_module(void *, unsigned long, const char *);
35
36 static void print_usage(const char *progname)
37 {
38         fprintf(stderr, "Usage: %s filename [args]\n", progname);
39         exit(1);
40 }
41
42 /* We use error numbers in a loose translation... */
43 static const char *moderror(int err)
44 {
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);
56         }
57 }
58
59 static void *grab_file(const char *filename, unsigned long *size)
60 {
61         unsigned int max = 16384;
62         int ret, fd, err_save;
63         void *buffer;
64
65         if (streq(filename, "-"))
66                 fd = dup(STDIN_FILENO);
67         else
68                 fd = open(filename, O_RDONLY, 0);
69
70         if (fd < 0)
71                 return NULL;
72
73         buffer = malloc(max);
74         if (!buffer)
75                 goto out_error;
76
77         *size = 0;
78         while ((ret = read(fd, buffer + *size, max - *size)) > 0) {
79                 *size += ret;
80                 if (*size == max) {
81                         void *p;
82
83                         p = realloc(buffer, max *= 2);
84                         if (!p)
85                                 goto out_error;
86                         buffer = p;
87                 }
88         }
89         if (ret < 0)
90                 goto out_error;
91
92         close(fd);
93         return buffer;
94
95 out_error:
96         err_save = errno;
97         free(buffer);
98         close(fd);
99         errno = err_save;
100         return NULL;
101 }
102
103 int main(int argc, char *argv[])
104 {
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];
111
112         if (!options) {
113                 fprintf(stderr,
114                         "insmod: can't allocate memory: %s\n",
115                         strerror(errno));
116                 exit(1);
117         }
118
119         p = my_basename(argv[0]);
120
121         if (argv[1] && (streq(argv[1], "--version") || streq(argv[1], "-V"))) {
122                 puts(PACKAGE " version " VERSION);
123                 exit(0);
124         }
125
126         /* Ignore old options, for backwards compat. */
127         while (argv[1] && (streq(argv[1], "-p")
128                            || streq(argv[1], "-s")
129                            || streq(argv[1], "-f"))) {
130                 argv++;
131                 argc--;
132         }
133
134         filename = argv[1];
135         if (!filename)
136                 print_usage(progname);
137
138         /* Rest is options */
139         for (i = 2; i < argc; i++) {
140                 options = realloc(options,
141                                   strlen(options) + 1 + strlen(argv[i]) + 1);
142                 if (!options) {
143                         fprintf(stderr,
144                                 "insmod: can't allocate memory: %s\n",
145                                 strerror(errno));
146                         exit(1);
147                 }
148                 strcat(options, argv[i]);
149                 strcat(options, " ");
150         }
151
152         file = grab_file(filename, &len);
153         if (!file) {
154                 fprintf(stderr, "insmod: can't read '%s': %s\n",
155                         filename, strerror(errno));
156                 exit(1);
157         }
158
159         ret = init_module(file, len, options);
160         if (ret != 0) {
161                 fprintf(stderr, "insmod: error inserting '%s': %li %s\n",
162                         filename, ret, moderror(errno));
163         }
164         free(file);
165
166         if (ret != 0)
167                 exit(1);
168         exit(0);
169 }