]> git.proxmox.com Git - systemd.git/blame - src/core/kmod-setup.c
Merge tag 'upstream/229'
[systemd.git] / src / core / kmod-setup.c
CommitLineData
663996b3
MS
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
663996b3 20#include <string.h>
db2df898 21#include <unistd.h>
f47781d8
MP
22
23#ifdef HAVE_KMOD
663996b3 24#include <libkmod.h>
f47781d8 25#endif
663996b3 26
e3bff60a 27#include "bus-util.h"
db2df898 28#include "capability-util.h"
663996b3 29#include "kmod-setup.h"
db2df898 30#include "macro.h"
663996b3 31
f47781d8 32#ifdef HAVE_KMOD
663996b3
MS
33static 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 */
60f067b4 42 DISABLE_WARNING_FORMAT_NONLITERAL;
f47781d8 43 log_internalv(LOG_DEBUG, 0, file, line, fn, format, args);
60f067b4 44 REENABLE_WARNING;
663996b3 45}
f47781d8 46#endif
663996b3 47
60f067b4 48int kmod_setup(void) {
f47781d8 49#ifdef HAVE_KMOD
e842803a 50
60f067b4
JS
51 static const struct {
52 const char *module;
53 const char *path;
86f210e9
MP
54 bool warn_if_unavailable:1;
55 bool warn_if_module:1;
60f067b4
JS
56 bool (*condition_fn)(void);
57 } kmod_table[] = {
58 /* auto-loading on use doesn't work before udev is up */
86f210e9 59 { "autofs4", "/sys/class/misc/autofs", true, false, NULL },
60f067b4
JS
60
61 /* early configure of ::1 on the loopback device */
86f210e9 62 { "ipv6", "/sys/module/ipv6", false, true, NULL },
60f067b4
JS
63
64 /* this should never be a module */
86f210e9 65 { "unix", "/proc/net/unix", true, true, NULL },
60f067b4
JS
66
67 /* IPC is needed before we bring up any other services */
86f210e9 68 { "kdbus", "/sys/fs/kdbus", false, false, is_kdbus_wanted },
e3bff60a
MP
69
70#ifdef HAVE_LIBIPTC
71 /* netfilter is needed by networkd, nspawn among others, and cannot be autoloaded */
86f210e9 72 { "ip_tables", "/proc/net/ip_tables_names", false, false, NULL },
e3bff60a 73#endif
60f067b4 74 };
663996b3 75 struct kmod_ctx *ctx = NULL;
60f067b4 76 unsigned int i;
663996b3
MS
77 int r;
78
e842803a
MB
79 if (have_effective_cap(CAP_SYS_MODULE) == 0)
80 return 0;
81
60f067b4 82 for (i = 0; i < ELEMENTSOF(kmod_table); i++) {
663996b3
MS
83 struct kmod_module *mod;
84
60f067b4
JS
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())
663996b3
MS
89 continue;
90
86f210e9 91 if (kmod_table[i].warn_if_module)
60f067b4
JS
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);
663996b3
MS
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
60f067b4 105 r = kmod_module_new_from_name(ctx, kmod_table[i].module, &mod);
663996b3 106 if (r < 0) {
60f067b4 107 log_error("Failed to lookup module '%s'", kmod_table[i].module);
663996b3
MS
108 continue;
109 }
110
111 r = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL);
112 if (r == 0)
13d276d0 113 log_debug("Inserted module '%s'", kmod_module_get_name(mod));
663996b3
MS
114 else if (r == KMOD_PROBE_APPLY_BLACKLIST)
115 log_info("Module '%s' is blacklisted", kmod_module_get_name(mod));
86f210e9 116 else {
fb183854 117 bool print_warning = kmod_table[i].warn_if_unavailable || (r < 0 && r != -ENOENT);
86f210e9
MP
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 }
663996b3
MS
122
123 kmod_module_unref(mod);
124 }
125
126 if (ctx)
127 kmod_unref(ctx);
128
f47781d8 129#endif
663996b3
MS
130 return 0;
131}