2 * kmod-insmod - insert modules into linux kernel using libkmod.
4 * Copyright (C) 2011-2013 ProFUSION embedded systems
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include <shared/util.h>
28 #include <libkmod/libkmod.h>
32 static const char cmdopts_s
[] = "psfVh";
33 static const struct option cmdopts
[] = {
34 {"version", no_argument
, 0, 'V'},
35 {"help", no_argument
, 0, 'h'},
39 static void help(void)
42 "\t%s [options] filename [args]\n"
44 "\t-V, --version show version\n"
45 "\t-h, --help show this help\n",
46 program_invocation_short_name
);
49 static const char *mod_strerror(int err
)
53 return "Invalid module format";
55 return "Unknown symbol in module";
57 return "Module has wrong symbol version";
59 return "Invalid parameters";
65 static int do_insmod(int argc
, char *argv
[])
68 struct kmod_module
*mod
;
73 const char *null_config
= NULL
;
74 unsigned int flags
= 0;
78 c
= getopt_long(argc
, argv
, cmdopts_s
, cmdopts
, &idx
);
84 /* ignored, for compatibility only */
87 flags
|= KMOD_PROBE_FORCE_MODVERSION
;
88 flags
|= KMOD_PROBE_FORCE_VERMAGIC
;
94 puts(PACKAGE
" version " VERSION
);
100 ERR("unexpected getopt_long() value '%c'.\n",
106 if (optind
>= argc
) {
107 ERR("missing filename.\n");
111 filename
= argv
[optind
];
112 if (streq(filename
, "-")) {
113 ERR("this tool does not support loading from stdin!\n");
117 for (i
= optind
+ 1; i
< argc
; i
++) {
118 size_t len
= strlen(argv
[i
]);
119 void *tmp
= realloc(opts
, optslen
+ len
+ 2);
121 ERR("out of memory\n");
130 memcpy(opts
+ optslen
, argv
[i
], len
);
132 opts
[optslen
] = '\0';
135 ctx
= kmod_new(NULL
, &null_config
);
137 ERR("kmod_new() failed!\n");
142 err
= kmod_module_new_from_path(ctx
, filename
, &mod
);
144 ERR("could not load module %s: %s\n", filename
,
149 err
= kmod_module_insert_module(mod
, flags
, opts
);
151 ERR("could not insert module %s: %s\n", filename
,
154 kmod_module_unref(mod
);
159 return err
>= 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
162 const struct kmod_cmd kmod_cmd_compat_insmod
= {
165 .help
= "compat insmod command",