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