1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
/*
* load kernel modules
*
* Copyright (C) 2011 Kay Sievers <kay.sievers@vrfy.org>
* Copyright (C) 2011 ProFUSION embedded systems
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <libkmod.h>
#include "udev.h"
static struct kmod_ctx *ctx;
static int command_do(struct kmod_module *module, const char *type, const char *command, const char *cmdline_opts)
{
const char *modname = kmod_module_get_name(module);
char *p, *cmd = NULL;
size_t cmdlen, cmdline_opts_len, varlen;
int ret = 0;
if (cmdline_opts == NULL)
cmdline_opts = "";
cmdline_opts_len = strlen(cmdline_opts);
cmd = strdup(command);
if (cmd == NULL)
return -ENOMEM;
cmdlen = strlen(cmd);
varlen = sizeof("$CMDLINE_OPTS") - 1;
while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
size_t prefixlen = p - cmd;
size_t suffixlen = cmdlen - prefixlen - varlen;
size_t slen = cmdlen - varlen + cmdline_opts_len;
char *suffix = p + varlen;
char *s = malloc(slen + 1);
if (s == NULL) {
free(cmd);
return -ENOMEM;
}
memcpy(s, cmd, p - cmd);
memcpy(s + prefixlen, cmdline_opts, cmdline_opts_len);
memcpy(s + prefixlen + cmdline_opts_len, suffix, suffixlen);
s[slen] = '\0';
free(cmd);
cmd = s;
cmdlen = slen;
}
setenv("MODPROBE_MODULE", modname, 1);
ret = system(cmd);
unsetenv("MODPROBE_MODULE");
if (ret == -1 || WEXITSTATUS(ret)) {
//LOG("Error running %s command for %s\n", type, modname);
if (ret != -1)
ret = -WEXITSTATUS(ret);
}
end:
free(cmd);
return ret;
}
static int insmod_do_dependencies(struct kmod_module *parent);
static int insmod_do_soft_dependencies(struct kmod_module *mod, struct kmod_list *deps);
static int insmod_do_deps_list(struct kmod_module *parent, struct kmod_list *deps, unsigned stop_on_errors)
{
struct kmod_list *d;
int err = 0;
kmod_list_foreach(d, deps) {
struct kmod_module *dm = kmod_module_get_module(d);
struct kmod_list *pre = NULL, *post = NULL;
const char *cmd, *opts, *dmname = kmod_module_get_name(dm);
int state;
int r;
r = insmod_do_dependencies(dm);
if (r < 0) {
//WRN("could not insert dependencies of '%s': %s\n", dmname, strerror(-r));
goto dep_error;
}
r = kmod_module_get_softdeps(dm, &pre, &post);
if (r < 0) {
//WRN("could not get softdeps of '%s': %s\n", dmname, strerror(-r));
goto dep_done;
}
r = insmod_do_soft_dependencies(dm, pre);
if (r < 0) {
//WRN("could not insert pre softdeps of '%s': %s\n", dmname, strerror(-r));
goto dep_error;
}
state = kmod_module_get_initstate(dm);
if (state == KMOD_MODULE_LIVE ||
state == KMOD_MODULE_COMING ||
state == KMOD_MODULE_BUILTIN)
goto dep_done;
cmd = kmod_module_get_install_commands(dm);
if (cmd) {
r = command_do(dm, "install", cmd, NULL);
if (r < 0) {
//WRN("failed to execute install command of '%s':" " %s\n", dmname, strerror(-r));
goto dep_error;
} else
goto dep_done;
}
opts = kmod_module_get_options(dm);
r = kmod_module_insert_module(dm, 0, opts);
if (r < 0) {
//WRN("could not insert '%s': %s\n", dmname, strerror(-r));
goto dep_error;
}
dep_done:
r = insmod_do_soft_dependencies(dm, post);
if (r < 0) {
//WRN("could not insert post softdeps of '%s': %s\n", dmname, strerror(-r));
goto dep_error;
}
kmod_module_unref_list(pre);
kmod_module_unref_list(post);
kmod_module_unref(dm);
continue;
dep_error:
err = r;
kmod_module_unref_list(pre);
kmod_module_unref_list(post);
kmod_module_unref(dm);
if (stop_on_errors)
break;
else
continue;
}
return err;
}
static int insmod_do_soft_dependencies(struct kmod_module *mod, struct kmod_list *deps)
{
return insmod_do_deps_list(mod, deps, 0);
}
static int insmod_do_dependencies(struct kmod_module *parent)
{
struct kmod_list *deps = kmod_module_get_dependencies(parent);
int err = insmod_do_deps_list(parent, deps, 1);
kmod_module_unref_list(deps);
return err;
}
static int insmod_do(struct kmod_module *mod, const char *extra_opts)
{
const char *modname = kmod_module_get_name(mod);
const char *conf_opts = kmod_module_get_options(mod);
struct kmod_list *pre = NULL, *post = NULL;
char *opts = NULL;
const char *cmd;
int state;
int err;
err = kmod_module_get_softdeps(mod, &pre, &post);
if (err < 0) {
//WRN("could not get softdeps of '%s': %s\n", modname, strerror(-err));
return err;
}
err = insmod_do_soft_dependencies(mod, pre);
if (err < 0) {
//WRN("could not insert pre softdeps of '%s': %s\n", modname, strerror(-err));
goto error;
}
cmd = kmod_module_get_install_commands(mod);
if (cmd != NULL) {
err = command_do(mod, "install", cmd, extra_opts);
goto done;
}
state = kmod_module_get_initstate(mod);
if (state == KMOD_MODULE_BUILTIN || state == KMOD_MODULE_LIVE)
return 0;
/*
* At this point it's not possible to be a install/remove command
* anymore. So if we can't get module's path, it's because it was
* really intended to be a module and it doesn't exist
*/
if (kmod_module_get_path(mod) == NULL) {
//LOG("Module %s not found.\n", modname);
return -ENOENT;
}
err = insmod_do_dependencies(mod);
if (err < 0)
return err;
if (conf_opts || extra_opts) {
if (conf_opts == NULL)
opts = strdup(extra_opts);
else if (extra_opts == NULL)
opts = strdup(conf_opts);
else if (asprintf(&opts, "%s %s", conf_opts, extra_opts) < 0)
opts = NULL;
if (opts == NULL) {
err = -ENOMEM;
goto error;
}
}
err = kmod_module_insert_module(mod, 0, opts);
if (err == -EEXIST)
err = 0;
done:
err = insmod_do_soft_dependencies(mod, post);
if (err < 0) {
//WRN("could not insert post softdeps of '%s': %s\n", modname, strerror(-err));
goto error;
}
error:
kmod_module_unref_list(pre);
kmod_module_unref_list(post);
free(opts);
return err;
}
static int insmod_path(struct kmod_ctx *ctx, const char *path, const char *extra_options)
{
struct kmod_module *mod;
int err;
err = kmod_module_new_from_path(ctx, path, &mod);
if (err < 0) {
//LOG("Module %s not found.\n", path);
return err;
}
err = insmod_do(mod, extra_options);
kmod_module_unref(mod);
return err;
}
static int insmod_alias(struct kmod_ctx *ctx, const char *alias, const char *extra_options)
{
struct kmod_list *l, *list = NULL;
struct kmod_list *filtered = NULL;
int err;
err = kmod_module_new_from_lookup(ctx, alias, &list);
if (err < 0)
return err;
if (list == NULL) {
//LOG("Module %s not found.\n", alias);
return err;
}
err = kmod_module_get_filtered_blacklist(ctx, list, &filtered);
kmod_module_unref_list(list);
if (err < 0) {
//LOG("Could not filter alias list!\n");
return err;
}
list = filtered;
kmod_list_foreach(l, list) {
struct kmod_module *mod = kmod_module_get_module(l);
err = insmod_do(mod, extra_options);
kmod_module_unref(mod);
if (err < 0)
break;
}
kmod_module_unref_list(list);
return err;
}
static int insmod(struct kmod_ctx *ctx, const char *name, const char *extra_options)
{
struct stat st;
if (stat(name, &st) == 0)
return insmod_path(ctx, name, extra_options);
else
return insmod_alias(ctx, name, extra_options);
}
static int builtin_kmod(struct udev_device *dev, int argc, char *argv[], bool test)
{
struct udev *udev = udev_device_get_udev(dev);
int i;
if (!ctx)
return EXIT_FAILURE;
if (argc < 3) {
err(udev, "missing command + argument\n");
return EXIT_FAILURE;
}
for (i = 2; argv[i]; i++) {
info(udev, "%s '%s'\n", argv[1], argv[i]);
insmod(ctx, argv[i], NULL);
}
return EXIT_SUCCESS;
}
static int builtin_kmod_load(struct udev *udev)
{
kmod_unref(ctx);
ctx = kmod_new(NULL, NULL);
if (!ctx)
return -ENOMEM;
info(udev, "load module index\n");
return 0;
}
static int builtin_kmod_unload(struct udev *udev)
{
ctx = kmod_unref(ctx);
info(udev, "unload module index\n");
return 0;
}
const struct udev_builtin udev_builtin_kmod = {
.name = "kmod",
.cmd = builtin_kmod,
.load = builtin_kmod_load,
.unload = builtin_kmod_unload,
.help = "kernel module loader",
.run_once = false,
};
|