summaryrefslogtreecommitdiffstats
path: root/src/shared/module-util.c
blob: 56e93e87323b0395038cf233c665fa7d50a251ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include <errno.h>

#include "module-util.h"
#include "proc-cmdline.h"
#include "strv.h"

#if HAVE_KMOD

static void *libkmod_dl = NULL;

DLSYM_PROTOTYPE(kmod_list_next) = NULL;
DLSYM_PROTOTYPE(kmod_load_resources) = NULL;
DLSYM_PROTOTYPE(kmod_module_get_initstate) = NULL;
DLSYM_PROTOTYPE(kmod_module_get_module) = NULL;
DLSYM_PROTOTYPE(kmod_module_get_name) = NULL;
DLSYM_PROTOTYPE(kmod_module_new_from_lookup) = NULL;
DLSYM_PROTOTYPE(kmod_module_probe_insert_module) = NULL;
DLSYM_PROTOTYPE(kmod_module_unref) = NULL;
DLSYM_PROTOTYPE(kmod_module_unref_list) = NULL;
DLSYM_PROTOTYPE(kmod_new) = NULL;
DLSYM_PROTOTYPE(kmod_set_log_fn) = NULL;
DLSYM_PROTOTYPE(kmod_unref) = NULL;
DLSYM_PROTOTYPE(kmod_validate_resources) = NULL;

int dlopen_libkmod(void) {
        ELF_NOTE_DLOPEN("kmod",
                        "Support for loading kernel modules",
                        ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED,
                        "libkmod.so.2");

        return dlopen_many_sym_or_warn(
                        &libkmod_dl,
                        "libkmod.so.2",
                        LOG_DEBUG,
                        DLSYM_ARG(kmod_list_next),
                        DLSYM_ARG(kmod_load_resources),
                        DLSYM_ARG(kmod_module_get_initstate),
                        DLSYM_ARG(kmod_module_get_module),
                        DLSYM_ARG(kmod_module_get_name),
                        DLSYM_ARG(kmod_module_new_from_lookup),
                        DLSYM_ARG(kmod_module_probe_insert_module),
                        DLSYM_ARG(kmod_module_unref),
                        DLSYM_ARG(kmod_module_unref_list),
                        DLSYM_ARG(kmod_new),
                        DLSYM_ARG(kmod_set_log_fn),
                        DLSYM_ARG(kmod_unref),
                        DLSYM_ARG(kmod_validate_resources));
}

static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
        char ***denylist = ASSERT_PTR(data);
        int r;

        if (proc_cmdline_key_streq(key, "module_blacklist")) {

                if (proc_cmdline_value_missing(key, value))
                        return 0;

                r = strv_split_and_extend(denylist, value, ",", /* filter_duplicates = */ true);
                if (r < 0)
                        return r;
        }

        return 0;
}

int module_load_and_warn(struct kmod_ctx *ctx, const char *module, bool verbose) {
        _cleanup_(sym_kmod_module_unref_listp) struct kmod_list *modlist = NULL;
        _cleanup_strv_free_ char **denylist = NULL;
        bool denylist_parsed = false;
        struct kmod_list *itr;
        int r;

        assert(ctx);
        assert(module);

        /* verbose==true means we should log at non-debug level if we
         * fail to find or load the module. */

        log_debug("Loading module: %s", module);

        r = sym_kmod_module_new_from_lookup(ctx, module, &modlist);
        if (r < 0)
                return log_full_errno(verbose ? LOG_ERR : LOG_DEBUG, r,
                                      "Failed to look up module alias '%s': %m", module);

        if (!modlist)
                return log_full_errno(verbose ? LOG_ERR : LOG_DEBUG,
                                      SYNTHETIC_ERRNO(ENOENT),
                                      "Failed to find module '%s'", module);

        sym_kmod_list_foreach(itr, modlist) {
                _cleanup_(sym_kmod_module_unrefp) struct kmod_module *mod = NULL;
                int state, err;

                mod = sym_kmod_module_get_module(itr);
                state = sym_kmod_module_get_initstate(mod);

                switch (state) {
                case KMOD_MODULE_BUILTIN:
                        log_full(verbose ? LOG_INFO : LOG_DEBUG,
                                 "Module '%s' is built in", sym_kmod_module_get_name(mod));
                        break;

                case KMOD_MODULE_LIVE:
                        log_debug("Module '%s' is already loaded", sym_kmod_module_get_name(mod));
                        break;

                default:
                        err = sym_kmod_module_probe_insert_module(
                                        mod,
                                        KMOD_PROBE_APPLY_BLACKLIST,
                                        /* extra_options= */ NULL,
                                        /* run_install= */ NULL,
                                        /* data= */ NULL,
                                        /* print_action= */ NULL);
                        if (err == 0)
                                log_full(verbose ? LOG_INFO : LOG_DEBUG,
                                         "Inserted module '%s'", sym_kmod_module_get_name(mod));
                        else if (err == KMOD_PROBE_APPLY_BLACKLIST)
                                log_full(verbose ? LOG_INFO : LOG_DEBUG,
                                         "Module '%s' is deny-listed (by kmod)", sym_kmod_module_get_name(mod));
                        else {
                                assert(err < 0);

                                if (err == -EPERM) {
                                        if (!denylist_parsed) {
                                                r = proc_cmdline_parse(parse_proc_cmdline_item, &denylist, 0);
                                                if (r < 0)
                                                        log_full_errno(!verbose ? LOG_DEBUG : LOG_WARNING,
                                                                       r,
                                                                       "Failed to parse kernel command line, ignoring: %m");

                                                denylist_parsed = true;
                                        }
                                        if (strv_contains(denylist, sym_kmod_module_get_name(mod))) {
                                                log_full(verbose ? LOG_INFO : LOG_DEBUG,
                                                         "Module '%s' is deny-listed (by kernel)", sym_kmod_module_get_name(mod));
                                                continue;
                                        }
                                }

                                log_full_errno(!verbose ? LOG_DEBUG :
                                               err == -ENODEV ? LOG_NOTICE :
                                               err == -ENOENT ? LOG_WARNING :
                                                                LOG_ERR,
                                               err,
                                               "Failed to insert module '%s': %m",
                                               sym_kmod_module_get_name(mod));
                                if (!IN_SET(err, -ENODEV, -ENOENT))
                                        r = err;
                        }
                }
        }

        return r;
}

_printf_(6,0) static void systemd_kmod_log(
                void *data,
                int priority,
                const char *file,
                int line,
                const char *fn,
                const char *format,
                va_list args) {

        log_internalv(priority, 0, file, line, fn, format, args);
}

int module_setup_context(struct kmod_ctx **ret) {
        _cleanup_(sym_kmod_unrefp) struct kmod_ctx *ctx = NULL;
        int r;

        assert(ret);

        r = dlopen_libkmod();
        if (r < 0)
                return r;

        ctx = sym_kmod_new(NULL, NULL);
        if (!ctx)
                return -ENOMEM;

        (void) sym_kmod_load_resources(ctx);
        sym_kmod_set_log_fn(ctx, systemd_kmod_log, NULL);

        *ret = TAKE_PTR(ctx);
        return 0;
}

#endif