chiark / gitweb /
core/load-fragment: safe_close() protects errno
[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 <unistd.h>
23 #include <string.h>
24
25 #ifdef HAVE_KMOD
26 #include <libkmod.h>
27 #endif
28
29 #include "macro.h"
30 #include "capability.h"
31 #include "kmod-setup.h"
32
33 #ifdef HAVE_KMOD
34 static 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
42         /* library logging is enabled at debug only */
43         DISABLE_WARNING_FORMAT_NONLITERAL;
44         log_internalv(LOG_DEBUG, 0, file, line, fn, format, args);
45         REENABLE_WARNING;
46 }
47
48 static bool cmdline_check_kdbus(void) {
49         return get_proc_cmdline_key("kdbus", NULL) > 0;
50 }
51 #endif
52
53 int kmod_setup(void) {
54 #ifdef HAVE_KMOD
55
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 */
63                 { "autofs4", "/sys/class/misc/autofs", true, NULL                 },
64
65                 /* early configure of ::1 on the loopback device */
66                 { "ipv6",    "/sys/module/ipv6",       true, NULL                 },
67
68                 /* this should never be a module */
69                 { "unix",    "/proc/net/unix",         true, NULL                 },
70
71                 /* IPC is needed before we bring up any other services */
72                 { "kdbus",   "/sys/fs/kdbus",          false, cmdline_check_kdbus },
73         };
74         struct kmod_ctx *ctx = NULL;
75         unsigned int i;
76         int r;
77
78         if (have_effective_cap(CAP_SYS_MODULE) == 0)
79                 return 0;
80
81         for (i = 0; i < ELEMENTSOF(kmod_table); i++) {
82                 struct kmod_module *mod;
83
84                 if (kmod_table[i].path && access(kmod_table[i].path, F_OK) >= 0)
85                         continue;
86
87                 if (kmod_table[i].condition_fn && !kmod_table[i].condition_fn())
88                         continue;
89
90                 if (kmod_table[i].warn)
91                         log_debug("Your kernel apparently lacks built-in %s support. Might be "
92                                   "a good idea to compile it in. We'll now try to work around "
93                                   "this by loading the module...", kmod_table[i].module);
94
95                 if (!ctx) {
96                         ctx = kmod_new(NULL, NULL);
97                         if (!ctx)
98                                 return log_oom();
99
100                         kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
101                         kmod_load_resources(ctx);
102                 }
103
104                 r = kmod_module_new_from_name(ctx, kmod_table[i].module, &mod);
105                 if (r < 0) {
106                         log_error("Failed to lookup module '%s'", kmod_table[i].module);
107                         continue;
108                 }
109
110                 r = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL);
111                 if (r == 0)
112                         log_info("Inserted module '%s'", kmod_module_get_name(mod));
113                 else if (r == KMOD_PROBE_APPLY_BLACKLIST)
114                         log_info("Module '%s' is blacklisted", kmod_module_get_name(mod));
115                 else if (kmod_table[i].warn)
116                         log_error("Failed to insert module '%s'", kmod_module_get_name(mod));
117
118                 kmod_module_unref(mod);
119         }
120
121         if (ctx)
122                 kmod_unref(ctx);
123
124 #endif
125         return 0;
126 }