]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/kmod-setup.c
Merge pull request #2495 from heftig/master
[thirdparty/systemd.git] / src / core / kmod-setup.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <string.h>
21 #include <unistd.h>
22
23 #ifdef HAVE_KMOD
24 #include <libkmod.h>
25 #endif
26
27 #include "bus-util.h"
28 #include "capability-util.h"
29 #include "kmod-setup.h"
30 #include "macro.h"
31
32 #ifdef HAVE_KMOD
33 static void systemd_kmod_log(
34 void *data,
35 int priority,
36 const char *file, int line,
37 const char *fn,
38 const char *format,
39 va_list args) {
40
41 /* library logging is enabled at debug only */
42 DISABLE_WARNING_FORMAT_NONLITERAL;
43 log_internalv(LOG_DEBUG, 0, file, line, fn, format, args);
44 REENABLE_WARNING;
45 }
46 #endif
47
48 int kmod_setup(void) {
49 #ifdef HAVE_KMOD
50
51 static const struct {
52 const char *module;
53 const char *path;
54 bool warn_if_unavailable:1;
55 bool warn_if_module:1;
56 bool (*condition_fn)(void);
57 } kmod_table[] = {
58 /* auto-loading on use doesn't work before udev is up */
59 { "autofs4", "/sys/class/misc/autofs", true, false, NULL },
60
61 /* early configure of ::1 on the loopback device */
62 { "ipv6", "/sys/module/ipv6", false, true, NULL },
63
64 /* this should never be a module */
65 { "unix", "/proc/net/unix", true, true, NULL },
66
67 /* IPC is needed before we bring up any other services */
68 { "kdbus", "/sys/fs/kdbus", false, false, is_kdbus_wanted },
69
70 #ifdef HAVE_LIBIPTC
71 /* netfilter is needed by networkd, nspawn among others, and cannot be autoloaded */
72 { "ip_tables", "/proc/net/ip_tables_names", false, false, NULL },
73 #endif
74 };
75 struct kmod_ctx *ctx = NULL;
76 unsigned int i;
77 int r;
78
79 if (have_effective_cap(CAP_SYS_MODULE) == 0)
80 return 0;
81
82 for (i = 0; i < ELEMENTSOF(kmod_table); i++) {
83 struct kmod_module *mod;
84
85 if (kmod_table[i].path && access(kmod_table[i].path, F_OK) >= 0)
86 continue;
87
88 if (kmod_table[i].condition_fn && !kmod_table[i].condition_fn())
89 continue;
90
91 if (kmod_table[i].warn_if_module)
92 log_debug("Your kernel apparently lacks built-in %s support. Might be "
93 "a good idea to compile it in. We'll now try to work around "
94 "this by loading the module...", kmod_table[i].module);
95
96 if (!ctx) {
97 ctx = kmod_new(NULL, NULL);
98 if (!ctx)
99 return log_oom();
100
101 kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
102 kmod_load_resources(ctx);
103 }
104
105 r = kmod_module_new_from_name(ctx, kmod_table[i].module, &mod);
106 if (r < 0) {
107 log_error("Failed to lookup module '%s'", kmod_table[i].module);
108 continue;
109 }
110
111 r = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL);
112 if (r == 0)
113 log_debug("Inserted module '%s'", kmod_module_get_name(mod));
114 else if (r == KMOD_PROBE_APPLY_BLACKLIST)
115 log_info("Module '%s' is blacklisted", kmod_module_get_name(mod));
116 else {
117 bool print_warning = kmod_table[i].warn_if_unavailable || (r < 0 && r != -ENOENT);
118
119 log_full_errno(print_warning ? LOG_WARNING : LOG_DEBUG, r,
120 "Failed to insert module '%s': %m", kmod_module_get_name(mod));
121 }
122
123 kmod_module_unref(mod);
124 }
125
126 if (ctx)
127 kmod_unref(ctx);
128
129 #endif
130 return 0;
131 }