summaryrefslogtreecommitdiff
path: root/src/udev/udev-builtin-kmod.c
blob: f39b01a452696559d2f9027db5192f73eb4d08d4 (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
/*
 * load kernel modules
 *
 * Copyright (C) 2011-2012 Kay Sievers <kay@vrfy.org>
 * Copyright (C) 2011 ProFUSION embedded systems
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>

#ifdef HAVE_LIBKMOD
#include <libkmod.h>
#endif

#include "udev.h"

static struct kmod_ctx *ctx = NULL;

static int load_module(struct udev *udev, const char *alias) {
        int err;
#ifdef HAVE_LIBKMOD
        struct kmod_list *list = NULL;
        struct kmod_list *l;

        err = kmod_module_new_from_lookup(ctx, alias, &list);
        if (err < 0)
                return err;

        if (list == NULL)
                log_debug("No module matches '%s'", alias);

        kmod_list_foreach(l, list) {
                struct kmod_module *mod = kmod_module_get_module(l);

                err = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL);
                if (err == KMOD_PROBE_APPLY_BLACKLIST)
                        log_debug("Module '%s' is blacklisted", kmod_module_get_name(mod));
                else if (err == 0)
                        log_debug("Inserted '%s'", kmod_module_get_name(mod));
                else
                        log_debug("Failed to insert '%s'", kmod_module_get_name(mod));

                kmod_module_unref(mod);
        }

        kmod_module_unref_list(list);
#else

        /* 
           These 3 temporaries are needed because argv (below) is a const pointer, not pointer to const
        */
        char *tmp_alias = strdup(alias);
        char *tmp_modprobe = strdup(MODPROBE);
        char *tmp_bq = strdup("-bq");
        char *const argv[] = { tmp_modprobe, tmp_bq, tmp_alias, 0 };

        err = execute_command(MODPROBE, argv);

        free(tmp_alias);
        free(tmp_modprobe);
        free(tmp_bq);
#endif
        /*  both 'kmod_module_new_from_lookup' and 'execute_command' return <0 on error
		    so it is ok to assign both to 'err' */
        return err;
}

_printf_(6,0) static void udev_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);
}

static int builtin_kmod(struct udev_device *dev, int argc, char *argv[], bool test) {
        struct udev *udev = udev_device_get_udev(dev);
        int i;

#ifdef HAVE_LIBKMOD
        if (!ctx)
                return 0;
#endif

        if (argc < 3 || !streq(argv[1], "load")) {
                log_error("expect: %s load <module>", argv[0]);
                return EXIT_FAILURE;
        }

        for (i = 2; argv[i]; i++) {
                log_debug("Execute '%s' '%s'", argv[1], argv[i]);
                load_module(udev, argv[i]);
        }

        return EXIT_SUCCESS;
}

/* called at udev startup and reload */
static int builtin_kmod_init(struct udev *udev) {
#ifdef HAVE_LIBKMOD
        if (ctx)
                return 0;

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

        log_debug("Load module index");
        kmod_set_log_fn(ctx, udev_kmod_log, udev);
        kmod_load_resources(ctx);
#endif
        return 0;
}

/* called on udev shutdown and reload request */
static void builtin_kmod_exit(struct udev *udev) {
#ifdef HAVE_LIBKMOD
        log_debug("Unload module index");
        ctx = kmod_unref(ctx);
#endif
}

#ifdef HAVE_LIBKMOD
/* called every couple of seconds during event activity; 'true' if config has changed */
static bool builtin_kmod_validate(struct udev *udev) {
        log_debug("Validate module index");
        if (!ctx)
                return false;
        return (kmod_validate_resources(ctx) != KMOD_RESOURCES_OK);
}
#endif

const struct udev_builtin udev_builtin_kmod = {
        .name = "kmod",
        .cmd = builtin_kmod,
        .init = builtin_kmod_init,
        .exit = builtin_kmod_exit,
#ifdef HAVE_LIBKMOD
        .validate = builtin_kmod_validate,
#endif
        .help = "Kernel module loader",
        .run_once = false,
};