chiark / gitweb /
modules-load: use libkmod rather than modprobe
[elogind.git] / src / modules-load.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 General Public License as published by
10   the Free Software Foundation; either version 2 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   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <limits.h>
28 #include <dirent.h>
29 #include <libkmod.h>
30
31 #include "log.h"
32 #include "util.h"
33 #include "strv.h"
34
35 static void systemd_kmod_log(void *data, int priority, const char *file, int line,
36                              const char *fn, const char *format, va_list args)
37 {
38         log_meta(priority, file, line, fn, format, args);
39 }
40
41 int main(int argc, char *argv[]) {
42         int r = EXIT_FAILURE;
43         char **files, **fn;
44         struct kmod_ctx *ctx;
45         struct kmod_module *mod;
46
47         if (argc > 1) {
48                 log_error("This program takes no argument.");
49                 return EXIT_FAILURE;
50         }
51
52         log_set_target(LOG_TARGET_AUTO);
53         log_parse_environment();
54         log_open();
55
56         umask(0022);
57
58         if (!(ctx = kmod_new(NULL, NULL))) {
59                 log_error("Failed to allocate memory for kmod.");
60                 goto finish;
61         }
62
63         kmod_load_resources(ctx);
64
65         kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
66
67         if (conf_files_list(&files, ".conf",
68                             "/run/modules-load.d",
69                             "/etc/modules-load.d",
70                             "/usr/local/lib/modules-load.d",
71                             "/usr/lib/modules-load.d",
72                             "/lib/modules-load.d",
73                             NULL) < 0) {
74                 log_error("Failed to enumerate modules-load.d files: %s", strerror(-r));
75                 goto finish;
76         }
77
78         r = EXIT_SUCCESS;
79
80         STRV_FOREACH(fn, files) {
81                 FILE *f;
82
83                 f = fopen(*fn, "re");
84                 if (!f) {
85                         if (errno == ENOENT)
86                                 continue;
87
88                         log_error("Failed to open %s: %m", *fn);
89                         r = EXIT_FAILURE;
90                         continue;
91                 }
92
93                 log_debug("apply: %s\n", *fn);
94                 for (;;) {
95                         char line[LINE_MAX], *l;
96                         int err;
97
98                         if (!(fgets(line, sizeof(line), f)))
99                                 break;
100
101                         l = strstrip(line);
102                         if (*l == '#' || *l == 0)
103                                 continue;
104
105                         err = kmod_module_new_from_name(ctx, l, &mod);
106                         if (err < 0) {
107                                 log_error("Failed to load module '%s'", l);
108                                 r = EXIT_FAILURE;
109                                 continue;
110                         }
111
112                         err = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST,
113                                                               NULL, NULL, NULL, NULL);
114                         if (err == 0)
115                                 log_info("Inserted module '%s'", kmod_module_get_name(mod));
116                         else if (err == KMOD_PROBE_APPLY_BLACKLIST)
117                                 log_info("Module '%s' is blacklisted", kmod_module_get_name(mod));
118                         else {
119                                 log_error("Failed to insert '%s'", kmod_module_get_name(mod));
120                                 r = EXIT_FAILURE;
121                         }
122
123                         kmod_module_unref(mod);
124                 }
125
126                 if (ferror(f)) {
127                         log_error("Failed to read from file: %m");
128                         r = EXIT_FAILURE;
129                 }
130
131                 fclose(f);
132         }
133
134 finish:
135         strv_free(files);
136         kmod_unref(ctx);
137
138         return r;
139 }