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