]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/kmod-setup.c
timedated: remove unnecessary goto
[thirdparty/systemd.git] / src / core / kmod-setup.c
CommitLineData
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
11c3a4ee
LP
22#include <unistd.h>
23#include <string.h>
f84f9974
LP
24
25#ifdef HAVE_KMOD
728beb28 26#include <libkmod.h>
f84f9974 27#endif
11c3a4ee
LP
28
29#include "macro.h"
c47fc1f0 30#include "capability.h"
11c3a4ee
LP
31#include "kmod-setup.h"
32
f84f9974 33#ifdef HAVE_KMOD
b4b87964
LP
34static void systemd_kmod_log(
35 void *data,
36 int priority,
37 const char *file, int line,
38 const char *fn,
39 const char *format,
40 va_list args) {
41
10223732 42 /* library logging is enabled at debug only */
bcfce235 43 DISABLE_WARNING_FORMAT_NONLITERAL;
79008bdd 44 log_internalv(LOG_DEBUG, 0, file, line, fn, format, args);
bcfce235 45 REENABLE_WARNING;
728beb28 46}
b4b87964 47
b43b8f7a 48static bool cmdline_check_kdbus(void) {
3da44ef5 49 return get_proc_cmdline_key("kdbus", NULL) > 0;
7491e6e7 50}
f84f9974 51#endif
b4b87964 52
7491e6e7 53int kmod_setup(void) {
f84f9974 54#ifdef HAVE_KMOD
c47fc1f0 55
7491e6e7
KS
56 static const struct {
57 const char *module;
58 const char *path;
59 bool warn;
60 bool (*condition_fn)(void);
61 } kmod_table[] = {
62 /* auto-loading on use doesn't work before udev is up */
1d308797 63 { "autofs4", "/sys/class/misc/autofs", true, NULL },
7491e6e7
KS
64
65 /* early configure of ::1 on the loopback device */
1d308797 66 { "ipv6", "/sys/module/ipv6", true, NULL },
7491e6e7
KS
67
68 /* this should never be a module */
1d308797 69 { "unix", "/proc/net/unix", true, NULL },
7491e6e7
KS
70
71 /* IPC is needed before we bring up any other services */
1d308797
LP
72 { "kdbus", "/sys/fs/kdbus", false, cmdline_check_kdbus },
73
74 /* netfilter is needed by networkd, nspawn among others, and cannot be autoloaded */
75 { "ip_tables", "/proc/net/ip_tables_names", false, NULL },
7491e6e7 76 };
728beb28 77 struct kmod_ctx *ctx = NULL;
7491e6e7 78 unsigned int i;
b4b87964 79 int r;
11c3a4ee 80
c47fc1f0
LP
81 if (have_effective_cap(CAP_SYS_MODULE) == 0)
82 return 0;
83
7491e6e7 84 for (i = 0; i < ELEMENTSOF(kmod_table); i++) {
b4b87964 85 struct kmod_module *mod;
11c3a4ee 86
b43b8f7a 87 if (kmod_table[i].path && access(kmod_table[i].path, F_OK) >= 0)
7491e6e7
KS
88 continue;
89
b43b8f7a 90 if (kmod_table[i].condition_fn && !kmod_table[i].condition_fn())
11c3a4ee
LP
91 continue;
92
7491e6e7
KS
93 if (kmod_table[i].warn)
94 log_debug("Your kernel apparently lacks built-in %s support. Might be "
95 "a good idea to compile it in. We'll now try to work around "
96 "this by loading the module...", kmod_table[i].module);
11c3a4ee 97
728beb28
TG
98 if (!ctx) {
99 ctx = kmod_new(NULL, NULL);
b4b87964
LP
100 if (!ctx)
101 return log_oom();
11c3a4ee 102
728beb28 103 kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
728beb28
TG
104 kmod_load_resources(ctx);
105 }
11c3a4ee 106
7491e6e7 107 r = kmod_module_new_from_name(ctx, kmod_table[i].module, &mod);
b4b87964 108 if (r < 0) {
7491e6e7 109 log_error("Failed to lookup module '%s'", kmod_table[i].module);
728beb28
TG
110 continue;
111 }
11c3a4ee 112
b4b87964
LP
113 r = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL);
114 if (r == 0)
728beb28 115 log_info("Inserted module '%s'", kmod_module_get_name(mod));
b4b87964 116 else if (r == KMOD_PROBE_APPLY_BLACKLIST)
728beb28 117 log_info("Module '%s' is blacklisted", kmod_module_get_name(mod));
b43b8f7a 118 else if (kmod_table[i].warn)
10223732 119 log_error("Failed to insert module '%s'", kmod_module_get_name(mod));
11c3a4ee 120
728beb28 121 kmod_module_unref(mod);
11c3a4ee
LP
122 }
123
728beb28 124 if (ctx)
34a35ece 125 kmod_unref(ctx);
728beb28 126
f84f9974 127#endif
728beb28 128 return 0;
11c3a4ee 129}