]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/kmod-setup.c
Merge pull request #6704 from andir/fix_vrf_table_identifier
[thirdparty/systemd.git] / src / core / kmod-setup.c
CommitLineData
11c3a4ee
LP
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
5430f7f2
LP
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
11c3a4ee
LP
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
5430f7f2 14 Lesser General Public License for more details.
11c3a4ee 15
5430f7f2 16 You should have received a copy of the GNU Lesser General Public License
11c3a4ee
LP
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
11c3a4ee 20#include <string.h>
cf0fbc49 21#include <unistd.h>
f84f9974
LP
22
23#ifdef HAVE_KMOD
728beb28 24#include <libkmod.h>
f84f9974 25#endif
11c3a4ee 26
d79acc30 27#include "bus-util.h"
cf0fbc49 28#include "capability-util.h"
11c3a4ee 29#include "kmod-setup.h"
cf0fbc49 30#include "macro.h"
11c3a4ee 31
f84f9974 32#ifdef HAVE_KMOD
b4b87964
LP
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
10223732 41 /* library logging is enabled at debug only */
bcfce235 42 DISABLE_WARNING_FORMAT_NONLITERAL;
79008bdd 43 log_internalv(LOG_DEBUG, 0, file, line, fn, format, args);
bcfce235 44 REENABLE_WARNING;
728beb28 45}
f84f9974 46#endif
b4b87964 47
7491e6e7 48int kmod_setup(void) {
f84f9974 49#ifdef HAVE_KMOD
c47fc1f0 50
7491e6e7
KS
51 static const struct {
52 const char *module;
53 const char *path;
85c67553
DM
54 bool warn_if_unavailable:1;
55 bool warn_if_module:1;
7491e6e7
KS
56 bool (*condition_fn)(void);
57 } kmod_table[] = {
58 /* auto-loading on use doesn't work before udev is up */
85c67553 59 { "autofs4", "/sys/class/misc/autofs", true, false, NULL },
7491e6e7
KS
60
61 /* early configure of ::1 on the loopback device */
85c67553 62 { "ipv6", "/sys/module/ipv6", false, true, NULL },
7491e6e7
KS
63
64 /* this should never be a module */
85c67553 65 { "unix", "/proc/net/unix", true, true, NULL },
7491e6e7 66
a363680f 67#ifdef HAVE_LIBIPTC
1d308797 68 /* netfilter is needed by networkd, nspawn among others, and cannot be autoloaded */
85c67553 69 { "ip_tables", "/proc/net/ip_tables_names", false, false, NULL },
a363680f 70#endif
7491e6e7 71 };
728beb28 72 struct kmod_ctx *ctx = NULL;
7491e6e7 73 unsigned int i;
b4b87964 74 int r;
11c3a4ee 75
c47fc1f0
LP
76 if (have_effective_cap(CAP_SYS_MODULE) == 0)
77 return 0;
78
7491e6e7 79 for (i = 0; i < ELEMENTSOF(kmod_table); i++) {
b4b87964 80 struct kmod_module *mod;
11c3a4ee 81
b43b8f7a 82 if (kmod_table[i].path && access(kmod_table[i].path, F_OK) >= 0)
7491e6e7
KS
83 continue;
84
b43b8f7a 85 if (kmod_table[i].condition_fn && !kmod_table[i].condition_fn())
11c3a4ee
LP
86 continue;
87
85c67553 88 if (kmod_table[i].warn_if_module)
7491e6e7
KS
89 log_debug("Your kernel apparently lacks built-in %s support. Might be "
90 "a good idea to compile it in. We'll now try to work around "
91 "this by loading the module...", kmod_table[i].module);
11c3a4ee 92
728beb28
TG
93 if (!ctx) {
94 ctx = kmod_new(NULL, NULL);
b4b87964
LP
95 if (!ctx)
96 return log_oom();
11c3a4ee 97
728beb28 98 kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
728beb28
TG
99 kmod_load_resources(ctx);
100 }
11c3a4ee 101
7491e6e7 102 r = kmod_module_new_from_name(ctx, kmod_table[i].module, &mod);
b4b87964 103 if (r < 0) {
7491e6e7 104 log_error("Failed to lookup module '%s'", kmod_table[i].module);
728beb28
TG
105 continue;
106 }
11c3a4ee 107
b4b87964
LP
108 r = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL);
109 if (r == 0)
149730fc 110 log_debug("Inserted module '%s'", kmod_module_get_name(mod));
b4b87964 111 else if (r == KMOD_PROBE_APPLY_BLACKLIST)
728beb28 112 log_info("Module '%s' is blacklisted", kmod_module_get_name(mod));
78d298bb 113 else {
e0465827 114 bool print_warning = kmod_table[i].warn_if_unavailable || (r < 0 && r != -ENOENT);
78d298bb
DM
115
116 log_full_errno(print_warning ? LOG_WARNING : LOG_DEBUG, r,
d814f990 117 "Failed to insert module '%s': %m", kmod_module_get_name(mod));
78d298bb 118 }
11c3a4ee 119
728beb28 120 kmod_module_unref(mod);
11c3a4ee
LP
121 }
122
728beb28 123 if (ctx)
34a35ece 124 kmod_unref(ctx);
728beb28 125
f84f9974 126#endif
728beb28 127 return 0;
11c3a4ee 128}