chiark / gitweb /
kmod-setup: add conditional module loading callback
[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 typedef struct Kmodule {
34         const char *name;
35         const char *directory;
36         bool (*condition_fn)(void);
37 } KModule;
38
39 static const KModule kmod_table[] = {
40         { "autofs4",  "/sys/class/misc/autofs",    NULL } ,
41         { "ipv6",     "/sys/module/ipv6",          NULL },
42         { "efivarfs", "/sys/firmware/efi/efivars", NULL },
43         { "unix",     "/proc/net/unix",            NULL } ,
44 };
45
46 #pragma GCC diagnostic push
47 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
48 static void systemd_kmod_log(void *data, int priority, const char *file, int line,
49                              const char *fn, const char *format, va_list args)
50 {
51         /* library logging is enabled at debug only */
52         log_metav(LOG_DEBUG, file, line, fn, format, args);
53 }
54 #pragma GCC diagnostic pop
55
56 int kmod_setup(void) {
57         unsigned i;
58         struct kmod_ctx *ctx = NULL;
59         struct kmod_module *mod;
60         int err;
61
62         for (i = 0; i < ELEMENTSOF(kmod_table); i += 2) {
63                 if (kmod_table[i].condition_fn && !kmod_table[i].condition_fn())
64                         continue;
65
66                 if (access(kmod_table[i].directory, F_OK) >= 0)
67                         continue;
68
69                 log_debug("Your kernel apparently lacks built-in %s support. Might be a good idea to compile it in. "
70                           "We'll now try to work around this by loading the module...",
71                           kmod_table[i].name);
72
73                 if (!ctx) {
74                         ctx = kmod_new(NULL, NULL);
75                         if (!ctx) {
76                                 log_error("Failed to allocate memory for kmod");
77                                 return -ENOMEM;
78                         }
79
80                         kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
81                         kmod_load_resources(ctx);
82                 }
83
84                 err = kmod_module_new_from_name(ctx, kmod_table[i].name, &mod);
85                 if (err < 0) {
86                         log_error("Failed to lookup module '%s'", kmod_table[i].name);
87                         continue;
88                 }
89
90                 err = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL);
91                 if (err == 0)
92                         log_info("Inserted module '%s'", kmod_module_get_name(mod));
93                 else if (err == KMOD_PROBE_APPLY_BLACKLIST)
94                         log_info("Module '%s' is blacklisted", kmod_module_get_name(mod));
95                 else
96                         log_error("Failed to insert module '%s'", kmod_module_get_name(mod));
97
98                 kmod_module_unref(mod);
99         }
100
101         if (ctx)
102                 kmod_unref(ctx);
103
104         return 0;
105 }