]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/kmod-setup.c
relicense to LGPLv2.1 (with exceptions)
[thirdparty/systemd.git] / src / core / kmod-setup.c
CommitLineData
d6c9574f 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
11c3a4ee
LP
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
5430f7f2
LP
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
11c3a4ee
LP
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
5430f7f2 16 Lesser General Public License for more details.
11c3a4ee 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
11c3a4ee
LP
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>
728beb28 26#include <libkmod.h>
11c3a4ee
LP
27
28#include "macro.h"
29#include "execute.h"
30
31#include "kmod-setup.h"
32
33static const char * const kmod_table[] = {
34 "autofs4", "/sys/class/misc/autofs",
17586c16
LP
35 "ipv6", "/sys/module/ipv6",
36 "unix", "/proc/net/unix"
11c3a4ee
LP
37};
38
cdb454f2
LP
39#pragma GCC diagnostic push
40#pragma GCC diagnostic ignored "-Wformat-nonliteral"
728beb28
TG
41static void systemd_kmod_log(void *data, int priority, const char *file, int line,
42 const char *fn, const char *format, va_list args)
43{
44 log_meta(priority, file, line, fn, format, args);
45}
cdb454f2 46#pragma GCC diagnostic pop
728beb28 47
11c3a4ee 48int kmod_setup(void) {
728beb28
TG
49 unsigned i;
50 struct kmod_ctx *ctx = NULL;
51 struct kmod_module *mod;
52 int err;
11c3a4ee
LP
53
54 for (i = 0; i < ELEMENTSOF(kmod_table); i += 2) {
55
56 if (access(kmod_table[i+1], F_OK) >= 0)
57 continue;
58
be11c12e 59 log_debug("Your kernel apparently lacks built-in %s support. Might be a good idea to compile it in. "
728beb28
TG
60 "We'll now try to work around this by loading the module...",
61 kmod_table[i]);
11c3a4ee 62
728beb28
TG
63 if (!ctx) {
64 ctx = kmod_new(NULL, NULL);
65 if (!ctx) {
66 log_error("Failed to allocate memory for kmod");
67 return -ENOMEM;
68 }
11c3a4ee 69
728beb28 70 kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
11c3a4ee 71
728beb28
TG
72 kmod_load_resources(ctx);
73 }
11c3a4ee 74
728beb28
TG
75 err = kmod_module_new_from_name(ctx, kmod_table[i], &mod);
76 if (err < 0) {
77 log_error("Failed to load module '%s'", kmod_table[i]);
78 continue;
79 }
11c3a4ee 80
728beb28
TG
81 err = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL);
82 if (err == 0)
83 log_info("Inserted module '%s'", kmod_module_get_name(mod));
84 else if (err == KMOD_PROBE_APPLY_BLACKLIST)
85 log_info("Module '%s' is blacklisted", kmod_module_get_name(mod));
86 else
87 log_error("Failed to insert '%s'", kmod_module_get_name(mod));
11c3a4ee 88
728beb28 89 kmod_module_unref(mod);
11c3a4ee
LP
90 }
91
728beb28 92 if (ctx)
34a35ece 93 kmod_unref(ctx);
728beb28
TG
94
95 return 0;
11c3a4ee 96}