]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/kmod-setup.c
kmod-setup: use libkmod rather than modprobe
[thirdparty/systemd.git] / src / kmod-setup.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <sys/wait.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <libkmod.h>
27
28 #include "macro.h"
29 #include "execute.h"
30
31 #include "kmod-setup.h"
32
33 static const char * const kmod_table[] = {
34 "autofs4", "/sys/class/misc/autofs",
35 "ipv6", "/sys/module/ipv6",
36 "unix", "/proc/net/unix"
37 };
38
39 static void systemd_kmod_log(void *data, int priority, const char *file, int line,
40 const char *fn, const char *format, va_list args)
41 {
42 log_meta(priority, file, line, fn, format, args);
43 }
44
45
46 int kmod_setup(void) {
47 unsigned i;
48 struct kmod_ctx *ctx = NULL;
49 struct kmod_module *mod;
50 int err;
51
52 for (i = 0; i < ELEMENTSOF(kmod_table); i += 2) {
53
54 if (access(kmod_table[i+1], F_OK) >= 0)
55 continue;
56
57 log_debug("Your kernel apparently lacks built-in %s support. Might be a good idea to compile it in. "
58 "We'll now try to work around this by loading the module...",
59 kmod_table[i]);
60
61 if (!ctx) {
62 ctx = kmod_new(NULL, NULL);
63 if (!ctx) {
64 log_error("Failed to allocate memory for kmod");
65 return -ENOMEM;
66 }
67
68 kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
69
70 kmod_load_resources(ctx);
71 }
72
73 err = kmod_module_new_from_name(ctx, kmod_table[i], &mod);
74 if (err < 0) {
75 log_error("Failed to load module '%s'", kmod_table[i]);
76 continue;
77 }
78
79 err = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL);
80 if (err == 0)
81 log_info("Inserted module '%s'", kmod_module_get_name(mod));
82 else if (err == KMOD_PROBE_APPLY_BLACKLIST)
83 log_info("Module '%s' is blacklisted", kmod_module_get_name(mod));
84 else
85 log_error("Failed to insert '%s'", kmod_module_get_name(mod));
86
87 kmod_module_unref(mod);
88 }
89
90 if (ctx)
91 ctx = kmod_unref(ctx);
92
93 return 0;
94 }