chiark / gitweb /
util: unify SO_PEERCRED/SO_PEERSEC invocations
[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 #include <libkmod.h>
27
28 #include "macro.h"
29 #include "execute.h"
30
31 #include "kmod-setup.h"
32
33 #pragma GCC diagnostic push
34 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
35
36 static void systemd_kmod_log(
37                 void *data,
38                 int priority,
39                 const char *file, int line,
40                 const char *fn,
41                 const char *format,
42                 va_list args) {
43
44         /* library logging is enabled at debug only */
45         log_metav(LOG_DEBUG, file, line, fn, format, args);
46 }
47
48 #pragma GCC diagnostic pop
49
50 static bool cmdline_check_kdbus(void) {
51         _cleanup_free_ char *line = NULL;
52
53         if (proc_cmdline(&line) <= 0)
54                 return false;
55
56         return strstr(line, "kdbus") != NULL;
57 }
58
59 int kmod_setup(void) {
60         static const struct {
61                 const char *module;
62                 const char *path;
63                 bool warn;
64                 bool (*condition_fn)(void);
65         } kmod_table[] = {
66                 /* auto-loading on use doesn't work before udev is up */
67                 { "autofs4", "/sys/class/misc/autofs", true, NULL },
68
69                 /* early configure of ::1 on the loopback device */
70                 { "ipv6",    "/sys/module/ipv6",       true, NULL },
71
72                 /* this should never be a module */
73                 { "unix",    "/proc/net/unix",         true, NULL },
74
75                 /* IPC is needed before we bring up any other services */
76                 { "kdbus",   "/sys/bus/kdbus",         false, cmdline_check_kdbus },
77         };
78         struct kmod_ctx *ctx = NULL;
79         unsigned int i;
80         int r;
81
82         for (i = 0; i < ELEMENTSOF(kmod_table); i++) {
83                 struct kmod_module *mod;
84
85                 if (kmod_table[i].path && access(kmod_table[i].path, F_OK) >= 0)
86                         continue;
87
88                 if (kmod_table[i].condition_fn && !kmod_table[i].condition_fn())
89                         continue;
90
91                 if (kmod_table[i].warn)
92                         log_debug("Your kernel apparently lacks built-in %s support. Might be "
93                                   "a good idea to compile it in. We'll now try to work around "
94                                   "this by loading the module...", kmod_table[i].module);
95
96                 if (!ctx) {
97                         ctx = kmod_new(NULL, NULL);
98                         if (!ctx)
99                                 return log_oom();
100
101                         kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
102                         kmod_load_resources(ctx);
103                 }
104
105                 r = kmod_module_new_from_name(ctx, kmod_table[i].module, &mod);
106                 if (r < 0) {
107                         log_error("Failed to lookup module '%s'", kmod_table[i].module);
108                         continue;
109                 }
110
111                 r = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL);
112                 if (r == 0)
113                         log_info("Inserted module '%s'", kmod_module_get_name(mod));
114                 else if (r == KMOD_PROBE_APPLY_BLACKLIST)
115                         log_info("Module '%s' is blacklisted", kmod_module_get_name(mod));
116                 else if (kmod_table[i].warn)
117                         log_error("Failed to insert module '%s'", kmod_module_get_name(mod));
118
119                 kmod_module_unref(mod);
120         }
121
122         if (ctx)
123                 kmod_unref(ctx);
124
125         return 0;
126 }