chiark / gitweb /
23df1fdf36d12569a291e24aa0950a1785c40333
[elogind.git] / src / core / kmod-setup.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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
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
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
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
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>
26
27 #ifdef HAVE_KMOD
28 #include <libkmod.h>
29 #endif
30
31 #include "macro.h"
32 #include "execute.h"
33 #include "capability.h"
34 #include "kmod-setup.h"
35
36 #ifdef HAVE_KMOD
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
45         /* library logging is enabled at debug only */
46         DISABLE_WARNING_FORMAT_NONLITERAL;
47         log_metav(LOG_DEBUG, file, line, fn, format, args);
48         REENABLE_WARNING;
49 }
50
51 static bool cmdline_check_kdbus(void) {
52         _cleanup_free_ char *line = NULL;
53
54         if (proc_cmdline(&line) < 0)
55                 return false;
56
57         return strstr(line, "kdbus") != NULL;
58 }
59 #endif
60
61 int kmod_setup(void) {
62 #ifdef HAVE_KMOD
63
64         static const struct {
65                 const char *module;
66                 const char *path;
67                 bool warn;
68                 bool (*condition_fn)(void);
69         } kmod_table[] = {
70                 /* auto-loading on use doesn't work before udev is up */
71                 { "autofs4", "/sys/class/misc/autofs", true, NULL },
72
73                 /* early configure of ::1 on the loopback device */
74                 { "ipv6",    "/sys/module/ipv6",       true, NULL },
75
76                 /* this should never be a module */
77                 { "unix",    "/proc/net/unix",         true, NULL },
78
79                 /* IPC is needed before we bring up any other services */
80                 { "kdbus",   "/sys/fs/kdbus",          false, cmdline_check_kdbus },
81         };
82         struct kmod_ctx *ctx = NULL;
83         unsigned int i;
84         int r;
85
86         if (have_effective_cap(CAP_SYS_MODULE) == 0)
87                 return 0;
88
89         for (i = 0; i < ELEMENTSOF(kmod_table); i++) {
90                 struct kmod_module *mod;
91
92                 if (kmod_table[i].path && access(kmod_table[i].path, F_OK) >= 0)
93                         continue;
94
95                 if (kmod_table[i].condition_fn && !kmod_table[i].condition_fn())
96                         continue;
97
98                 if (kmod_table[i].warn)
99                         log_debug("Your kernel apparently lacks built-in %s support. Might be "
100                                   "a good idea to compile it in. We'll now try to work around "
101                                   "this by loading the module...", kmod_table[i].module);
102
103                 if (!ctx) {
104                         ctx = kmod_new(NULL, NULL);
105                         if (!ctx)
106                                 return log_oom();
107
108                         kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
109                         kmod_load_resources(ctx);
110                 }
111
112                 r = kmod_module_new_from_name(ctx, kmod_table[i].module, &mod);
113                 if (r < 0) {
114                         log_error("Failed to lookup module '%s'", kmod_table[i].module);
115                         continue;
116                 }
117
118                 r = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL);
119                 if (r == 0)
120                         log_info("Inserted module '%s'", kmod_module_get_name(mod));
121                 else if (r == KMOD_PROBE_APPLY_BLACKLIST)
122                         log_info("Module '%s' is blacklisted", kmod_module_get_name(mod));
123                 else if (kmod_table[i].warn)
124                         log_error("Failed to insert module '%s'", kmod_module_get_name(mod));
125
126                 kmod_module_unref(mod);
127         }
128
129         if (ctx)
130                 kmod_unref(ctx);
131
132 #endif
133         return 0;
134 }