1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
34 /* This reads all module names listed in /etc/modules-load.d/?*.conf and
35 * loads them into the kernel. This follows roughly Debian's way to
36 * handle modules, but uses a directory of fragments instead of a
37 * single /etc/modules file. */
39 static int scandir_filter(const struct dirent
*d
) {
42 if (ignore_file(d
->d_name
))
45 if (d
->d_type
!= DT_REG
&&
46 d
->d_type
!= DT_LNK
&&
47 d
->d_type
!= DT_UNKNOWN
)
50 return endswith(d
->d_name
, ".conf");
53 int main(int argc
, char *argv
[]) {
54 struct dirent
**de
= NULL
;
55 int r
= EXIT_FAILURE
, n
, i
;
56 char **arguments
= NULL
;
57 unsigned n_arguments
= 0, n_allocated
= 0;
60 log_error("This program takes no argument.");
64 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG
);
65 log_parse_environment();
68 if (!(arguments
= strv_new("/sbin/modprobe", "-sab", "--", NULL
))) {
69 log_error("Failed to allocate string array");
73 n_arguments
= n_allocated
= 3;
75 if ((n
= scandir("/etc/modules-load.d/", &de
, scandir_filter
, alphasort
)) < 0) {
80 log_error("Failed to enumerate /etc/modules-load.d/ files: %m");
87 for (i
= 0; i
< n
; i
++) {
92 k
= asprintf(&fn
, "/etc/modules-load.d/%s", de
[i
]->d_name
);
96 log_error("Failed to allocate file name.");
104 if (errno
== ENOENT
) {
109 log_error("Failed to open %s: %m", fn
);
118 char line
[LINE_MAX
], *l
, *t
;
120 if (!(fgets(line
, sizeof(line
), f
)))
124 if (*l
== '#' || *l
== 0)
127 if (!(t
= strdup(l
))) {
128 log_error("Failed to allocate module name.");
132 if (n_arguments
>= n_allocated
) {
136 m
= MAX(16U, n_arguments
*2);
138 if (!(a
= realloc(arguments
, sizeof(char*) * (m
+1)))) {
139 log_error("Failed to increase module array size.");
149 arguments
[n_arguments
++] = t
;
154 log_error("Failed to read from file: %m");
164 if (n_arguments
> 3) {
165 arguments
[n_arguments
] = NULL
;
166 execv("/sbin/modprobe", arguments
);
168 log_error("Failed to execute /sbin/modprobe: %m");
172 strv_free(arguments
);