]>
Commit | Line | Data |
---|---|---|
d6c9574f | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
11c3a4ee LP |
2 | |
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
9 | under the terms of the GNU Lesser General Public License as published by |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11c3a4ee LP |
11 | (at your option) any later version. |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 16 | Lesser General Public License for more details. |
11c3a4ee | 17 | |
5430f7f2 | 18 | You should have received a copy of the GNU Lesser General Public License |
11c3a4ee LP |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
20 | ***/ | |
21 | ||
22 | #include <sys/wait.h> | |
23 | #include <unistd.h> | |
24 | #include <string.h> | |
25 | #include <errno.h> | |
728beb28 | 26 | #include <libkmod.h> |
11c3a4ee LP |
27 | |
28 | #include "macro.h" | |
29 | #include "execute.h" | |
c47fc1f0 | 30 | #include "capability.h" |
11c3a4ee LP |
31 | #include "kmod-setup.h" |
32 | ||
b4b87964 LP |
33 | static void systemd_kmod_log( |
34 | void *data, | |
35 | int priority, | |
36 | const char *file, int line, | |
37 | const char *fn, | |
38 | const char *format, | |
39 | va_list args) { | |
40 | ||
10223732 | 41 | /* library logging is enabled at debug only */ |
bcfce235 | 42 | DISABLE_WARNING_FORMAT_NONLITERAL; |
10223732 | 43 | log_metav(LOG_DEBUG, file, line, fn, format, args); |
bcfce235 | 44 | REENABLE_WARNING; |
728beb28 | 45 | } |
b4b87964 | 46 | |
b43b8f7a | 47 | static bool cmdline_check_kdbus(void) { |
7491e6e7 | 48 | _cleanup_free_ char *line = NULL; |
b4b87964 | 49 | |
b43b8f7a | 50 | if (proc_cmdline(&line) <= 0) |
7491e6e7 | 51 | return false; |
b4b87964 | 52 | |
b43b8f7a | 53 | return strstr(line, "kdbus") != NULL; |
7491e6e7 | 54 | } |
b4b87964 | 55 | |
7491e6e7 | 56 | int kmod_setup(void) { |
c47fc1f0 | 57 | |
7491e6e7 KS |
58 | static const struct { |
59 | const char *module; | |
60 | const char *path; | |
61 | bool warn; | |
62 | bool (*condition_fn)(void); | |
63 | } kmod_table[] = { | |
64 | /* auto-loading on use doesn't work before udev is up */ | |
65 | { "autofs4", "/sys/class/misc/autofs", true, NULL }, | |
66 | ||
67 | /* early configure of ::1 on the loopback device */ | |
68 | { "ipv6", "/sys/module/ipv6", true, NULL }, | |
69 | ||
70 | /* this should never be a module */ | |
71 | { "unix", "/proc/net/unix", true, NULL }, | |
72 | ||
73 | /* IPC is needed before we bring up any other services */ | |
b43b8f7a | 74 | { "kdbus", "/sys/bus/kdbus", false, cmdline_check_kdbus }, |
7491e6e7 | 75 | }; |
728beb28 | 76 | struct kmod_ctx *ctx = NULL; |
7491e6e7 | 77 | unsigned int i; |
b4b87964 | 78 | int r; |
11c3a4ee | 79 | |
c47fc1f0 LP |
80 | if (have_effective_cap(CAP_SYS_MODULE) == 0) |
81 | return 0; | |
82 | ||
7491e6e7 | 83 | for (i = 0; i < ELEMENTSOF(kmod_table); i++) { |
b4b87964 | 84 | struct kmod_module *mod; |
11c3a4ee | 85 | |
b43b8f7a | 86 | if (kmod_table[i].path && access(kmod_table[i].path, F_OK) >= 0) |
7491e6e7 KS |
87 | continue; |
88 | ||
b43b8f7a | 89 | if (kmod_table[i].condition_fn && !kmod_table[i].condition_fn()) |
11c3a4ee LP |
90 | continue; |
91 | ||
7491e6e7 KS |
92 | if (kmod_table[i].warn) |
93 | log_debug("Your kernel apparently lacks built-in %s support. Might be " | |
94 | "a good idea to compile it in. We'll now try to work around " | |
95 | "this by loading the module...", kmod_table[i].module); | |
11c3a4ee | 96 | |
728beb28 TG |
97 | if (!ctx) { |
98 | ctx = kmod_new(NULL, NULL); | |
b4b87964 LP |
99 | if (!ctx) |
100 | return log_oom(); | |
11c3a4ee | 101 | |
728beb28 | 102 | kmod_set_log_fn(ctx, systemd_kmod_log, NULL); |
728beb28 TG |
103 | kmod_load_resources(ctx); |
104 | } | |
11c3a4ee | 105 | |
7491e6e7 | 106 | r = kmod_module_new_from_name(ctx, kmod_table[i].module, &mod); |
b4b87964 | 107 | if (r < 0) { |
7491e6e7 | 108 | log_error("Failed to lookup module '%s'", kmod_table[i].module); |
728beb28 TG |
109 | continue; |
110 | } | |
11c3a4ee | 111 | |
b4b87964 LP |
112 | r = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL); |
113 | if (r == 0) | |
728beb28 | 114 | log_info("Inserted module '%s'", kmod_module_get_name(mod)); |
b4b87964 | 115 | else if (r == KMOD_PROBE_APPLY_BLACKLIST) |
728beb28 | 116 | log_info("Module '%s' is blacklisted", kmod_module_get_name(mod)); |
b43b8f7a | 117 | else if (kmod_table[i].warn) |
10223732 | 118 | log_error("Failed to insert module '%s'", kmod_module_get_name(mod)); |
11c3a4ee | 119 | |
728beb28 | 120 | kmod_module_unref(mod); |
11c3a4ee LP |
121 | } |
122 | ||
728beb28 | 123 | if (ctx) |
34a35ece | 124 | kmod_unref(ctx); |
728beb28 TG |
125 | |
126 | return 0; | |
11c3a4ee | 127 | } |