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