]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
11c3a4ee LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2010 Lennart Poettering | |
11c3a4ee LP |
6 | ***/ |
7 | ||
6c1f72f6 | 8 | #include <ftw.h> |
11c3a4ee | 9 | #include <string.h> |
cf0fbc49 | 10 | #include <unistd.h> |
f84f9974 | 11 | |
6c1f72f6 | 12 | #include "alloc-util.h" |
d79acc30 | 13 | #include "bus-util.h" |
cf0fbc49 | 14 | #include "capability-util.h" |
6c1f72f6 | 15 | #include "fileio.h" |
11c3a4ee | 16 | #include "kmod-setup.h" |
cf0fbc49 | 17 | #include "macro.h" |
6c1f72f6 | 18 | #include "string-util.h" |
11c3a4ee | 19 | |
349cc4a5 | 20 | #if HAVE_KMOD |
8a8a4b6e ZJS |
21 | #include <libkmod.h> |
22 | #include "module-util.h" | |
23 | ||
b4b87964 LP |
24 | static void systemd_kmod_log( |
25 | void *data, | |
26 | int priority, | |
27 | const char *file, int line, | |
28 | const char *fn, | |
29 | const char *format, | |
30 | va_list args) { | |
31 | ||
10223732 | 32 | /* library logging is enabled at debug only */ |
bcfce235 | 33 | DISABLE_WARNING_FORMAT_NONLITERAL; |
79008bdd | 34 | log_internalv(LOG_DEBUG, 0, file, line, fn, format, args); |
bcfce235 | 35 | REENABLE_WARNING; |
728beb28 | 36 | } |
b4b87964 | 37 | |
6c1f72f6 HH |
38 | static int has_virtio_rng_nftw_cb( |
39 | const char *fpath, | |
40 | const struct stat *sb, | |
41 | int tflag, | |
42 | struct FTW *ftwbuf) { | |
43 | ||
44 | _cleanup_free_ char *alias = NULL; | |
45 | int r; | |
46 | ||
47 | if ((FTW_D == tflag) && (ftwbuf->level > 2)) | |
48 | return FTW_SKIP_SUBTREE; | |
49 | ||
50 | if (FTW_F != tflag) | |
51 | return FTW_CONTINUE; | |
52 | ||
53 | if (!endswith(fpath, "/modalias")) | |
54 | return FTW_CONTINUE; | |
55 | ||
56 | r = read_one_line_file(fpath, &alias); | |
57 | if (r < 0) | |
58 | return FTW_SKIP_SIBLINGS; | |
59 | ||
60 | if (startswith(alias, "pci:v00001AF4d00001005")) | |
61 | return FTW_STOP; | |
62 | ||
63 | if (startswith(alias, "pci:v00001AF4d00001044")) | |
64 | return FTW_STOP; | |
65 | ||
66 | return FTW_SKIP_SIBLINGS; | |
67 | } | |
68 | ||
69 | static bool has_virtio_rng(void) { | |
70 | return (nftw("/sys/devices/pci0000:00", has_virtio_rng_nftw_cb, 64, FTW_MOUNT|FTW_PHYS|FTW_ACTIONRETVAL) == FTW_STOP); | |
71 | } | |
95441cf2 | 72 | #endif |
6c1f72f6 | 73 | |
7491e6e7 | 74 | int kmod_setup(void) { |
349cc4a5 | 75 | #if HAVE_KMOD |
c47fc1f0 | 76 | |
7491e6e7 KS |
77 | static const struct { |
78 | const char *module; | |
79 | const char *path; | |
85c67553 DM |
80 | bool warn_if_unavailable:1; |
81 | bool warn_if_module:1; | |
7491e6e7 KS |
82 | bool (*condition_fn)(void); |
83 | } kmod_table[] = { | |
84 | /* auto-loading on use doesn't work before udev is up */ | |
85c67553 | 85 | { "autofs4", "/sys/class/misc/autofs", true, false, NULL }, |
7491e6e7 KS |
86 | |
87 | /* early configure of ::1 on the loopback device */ | |
85c67553 | 88 | { "ipv6", "/sys/module/ipv6", false, true, NULL }, |
7491e6e7 KS |
89 | |
90 | /* this should never be a module */ | |
85c67553 | 91 | { "unix", "/proc/net/unix", true, true, NULL }, |
7491e6e7 | 92 | |
349cc4a5 | 93 | #if HAVE_LIBIPTC |
1d308797 | 94 | /* netfilter is needed by networkd, nspawn among others, and cannot be autoloaded */ |
85c67553 | 95 | { "ip_tables", "/proc/net/ip_tables_names", false, false, NULL }, |
a363680f | 96 | #endif |
6c1f72f6 HH |
97 | /* virtio_rng would be loaded by udev later, but real entropy might be needed very early */ |
98 | { "virtio_rng", NULL, false, false, has_virtio_rng }, | |
7491e6e7 | 99 | }; |
232ac0d6 | 100 | _cleanup_(kmod_unrefp) struct kmod_ctx *ctx = NULL; |
7491e6e7 | 101 | unsigned int i; |
b4b87964 | 102 | int r; |
11c3a4ee | 103 | |
c47fc1f0 LP |
104 | if (have_effective_cap(CAP_SYS_MODULE) == 0) |
105 | return 0; | |
106 | ||
7491e6e7 | 107 | for (i = 0; i < ELEMENTSOF(kmod_table); i++) { |
232ac0d6 | 108 | _cleanup_(kmod_module_unrefp) struct kmod_module *mod = NULL; |
11c3a4ee | 109 | |
b43b8f7a | 110 | if (kmod_table[i].path && access(kmod_table[i].path, F_OK) >= 0) |
7491e6e7 KS |
111 | continue; |
112 | ||
b43b8f7a | 113 | if (kmod_table[i].condition_fn && !kmod_table[i].condition_fn()) |
11c3a4ee LP |
114 | continue; |
115 | ||
85c67553 | 116 | if (kmod_table[i].warn_if_module) |
7491e6e7 KS |
117 | log_debug("Your kernel apparently lacks built-in %s support. Might be " |
118 | "a good idea to compile it in. We'll now try to work around " | |
119 | "this by loading the module...", kmod_table[i].module); | |
11c3a4ee | 120 | |
728beb28 TG |
121 | if (!ctx) { |
122 | ctx = kmod_new(NULL, NULL); | |
b4b87964 LP |
123 | if (!ctx) |
124 | return log_oom(); | |
11c3a4ee | 125 | |
728beb28 | 126 | kmod_set_log_fn(ctx, systemd_kmod_log, NULL); |
728beb28 TG |
127 | kmod_load_resources(ctx); |
128 | } | |
11c3a4ee | 129 | |
7491e6e7 | 130 | r = kmod_module_new_from_name(ctx, kmod_table[i].module, &mod); |
b4b87964 | 131 | if (r < 0) { |
7491e6e7 | 132 | log_error("Failed to lookup module '%s'", kmod_table[i].module); |
728beb28 TG |
133 | continue; |
134 | } | |
11c3a4ee | 135 | |
b4b87964 LP |
136 | r = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL); |
137 | if (r == 0) | |
149730fc | 138 | log_debug("Inserted module '%s'", kmod_module_get_name(mod)); |
b4b87964 | 139 | else if (r == KMOD_PROBE_APPLY_BLACKLIST) |
728beb28 | 140 | log_info("Module '%s' is blacklisted", kmod_module_get_name(mod)); |
78d298bb | 141 | else { |
e0465827 | 142 | bool print_warning = kmod_table[i].warn_if_unavailable || (r < 0 && r != -ENOENT); |
78d298bb DM |
143 | |
144 | log_full_errno(print_warning ? LOG_WARNING : LOG_DEBUG, r, | |
d814f990 | 145 | "Failed to insert module '%s': %m", kmod_module_get_name(mod)); |
78d298bb | 146 | } |
11c3a4ee LP |
147 | } |
148 | ||
f84f9974 | 149 | #endif |
728beb28 | 150 | return 0; |
11c3a4ee | 151 | } |