]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/kmod-setup.c
fd426840343a65a35cf1057d43832f4fd0db5210
[thirdparty/systemd.git] / src / core / kmod-setup.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <ftw.h>
4 #include <unistd.h>
5
6 #include "alloc-util.h"
7 #include "bus-util.h"
8 #include "capability-util.h"
9 #include "fileio.h"
10 #include "kmod-setup.h"
11 #include "macro.h"
12 #include "string-util.h"
13
14 #if HAVE_KMOD
15 #include <libkmod.h>
16 #include "module-util.h"
17
18 static void systemd_kmod_log(
19 void *data,
20 int priority,
21 const char *file, int line,
22 const char *fn,
23 const char *format,
24 va_list args) {
25
26 /* library logging is enabled at debug only */
27 DISABLE_WARNING_FORMAT_NONLITERAL;
28 log_internalv(LOG_DEBUG, 0, file, line, fn, format, args);
29 REENABLE_WARNING;
30 }
31
32 static int has_virtio_rng_nftw_cb(
33 const char *fpath,
34 const struct stat *sb,
35 int tflag,
36 struct FTW *ftwbuf) {
37
38 _cleanup_free_ char *alias = NULL;
39 int r;
40
41 if ((FTW_D == tflag) && (ftwbuf->level > 2))
42 return FTW_SKIP_SUBTREE;
43
44 if (FTW_F != tflag)
45 return FTW_CONTINUE;
46
47 if (!endswith(fpath, "/modalias"))
48 return FTW_CONTINUE;
49
50 r = read_one_line_file(fpath, &alias);
51 if (r < 0)
52 return FTW_SKIP_SIBLINGS;
53
54 if (startswith(alias, "pci:v00001AF4d00001005"))
55 return FTW_STOP;
56
57 if (startswith(alias, "pci:v00001AF4d00001044"))
58 return FTW_STOP;
59
60 return FTW_SKIP_SIBLINGS;
61 }
62
63 static bool has_virtio_rng(void) {
64 return (nftw("/sys/devices/pci0000:00", has_virtio_rng_nftw_cb, 64, FTW_MOUNT|FTW_PHYS|FTW_ACTIONRETVAL) == FTW_STOP);
65 }
66 #endif
67
68 int kmod_setup(void) {
69 #if HAVE_KMOD
70
71 static const struct {
72 const char *module;
73 const char *path;
74 bool warn_if_unavailable:1;
75 bool warn_if_module:1;
76 bool (*condition_fn)(void);
77 } kmod_table[] = {
78 /* This one we need to load explicitly, since auto-loading on use doesn't work
79 * before udev created the ghost device nodes, and we need it earlier than that. */
80 { "autofs4", "/sys/class/misc/autofs", true, false, NULL },
81
82 /* This one we need to load explicitly, since auto-loading of IPv6 is not done when
83 * we try to configure ::1 on the loopback device. */
84 { "ipv6", "/sys/module/ipv6", false, true, NULL },
85
86 /* This should never be a module */
87 { "unix", "/proc/net/unix", true, true, NULL },
88
89 #if HAVE_LIBIPTC
90 /* netfilter is needed by networkd, nspawn among others, and cannot be autoloaded */
91 { "ip_tables", "/proc/net/ip_tables_names", false, false, NULL },
92 #endif
93 /* virtio_rng would be loaded by udev later, but real entropy might be needed very early */
94 { "virtio_rng", NULL, false, false, has_virtio_rng },
95 };
96 _cleanup_(kmod_unrefp) struct kmod_ctx *ctx = NULL;
97 unsigned i;
98
99 if (have_effective_cap(CAP_SYS_MODULE) == 0)
100 return 0;
101
102 for (i = 0; i < ELEMENTSOF(kmod_table); i++) {
103 if (kmod_table[i].path && access(kmod_table[i].path, F_OK) >= 0)
104 continue;
105
106 if (kmod_table[i].condition_fn && !kmod_table[i].condition_fn())
107 continue;
108
109 if (kmod_table[i].warn_if_module)
110 log_debug("Your kernel apparently lacks built-in %s support. Might be "
111 "a good idea to compile it in. We'll now try to work around "
112 "this by loading the module...", kmod_table[i].module);
113
114 if (!ctx) {
115 ctx = kmod_new(NULL, NULL);
116 if (!ctx)
117 return log_oom();
118
119 kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
120 kmod_load_resources(ctx);
121 }
122
123 (void) module_load_and_warn(ctx, kmod_table[i].module, kmod_table[i].warn_if_unavailable);
124 }
125
126 #endif
127 return 0;
128 }