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