chiark / gitweb /
manager: measure startup times
[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
30 #include "log.h"
31 #include "util.h"
32 #include "strv.h"
33
34 /* This reads all module names listed in /etc/modules.d/?*.modules 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. */
38
39 static int scandir_filter(const struct dirent *d) {
40         assert(d);
41
42         if (ignore_file(d->d_name))
43                 return 0;
44
45         if (d->d_type != DT_REG &&
46             d->d_type != DT_LNK)
47                 return 0;
48
49         return endswith(d->d_name, ".modules");
50 }
51
52 int main(int argc, char *argv[]) {
53         struct dirent **de = NULL;
54         int r = EXIT_FAILURE, n, i;
55         char **arguments = NULL;
56         unsigned n_arguments = 0, n_allocated = 0;
57
58         if (argc > 1) {
59                 log_error("This program takes no argument.");
60                 return EXIT_FAILURE;
61         }
62
63         log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
64         log_parse_environment();
65         log_open();
66
67         if (!(arguments = strv_new("/sbin/modprobe", "-sab", "--", NULL))) {
68                 log_error("Failed to allocate string array");
69                 goto finish;
70         }
71
72         n_arguments = n_allocated = 3;
73
74         if ((n = scandir("/etc/modules.d/", &de, scandir_filter, alphasort)) < 0) {
75
76                 if (errno == ENOENT)
77                         r = EXIT_SUCCESS;
78                 else
79                         log_error("Failed to enumerate /etc/modules.d/ files: %m");
80
81                 goto finish;
82         }
83
84         r = EXIT_SUCCESS;
85
86         for (i = 0; i < n; i++) {
87                 int k;
88                 char *fn;
89                 FILE *f;
90
91                 k = asprintf(&fn, "/etc/modules.d/%s", de[i]->d_name);
92                 free(de[i]);
93
94                 if (k < 0) {
95                         log_error("Failed to allocate file name.");
96                         r = EXIT_FAILURE;
97                         continue;
98                 }
99
100                 f = fopen(fn, "re");
101                 free(fn);
102
103                 if (!f) {
104                         log_error("Failed to open %s: %m", fn);
105                         r = EXIT_FAILURE;
106                         continue;
107                 }
108
109                 for (;;) {
110                         char line[LINE_MAX], *l, *t;
111
112                         if (!(fgets(line, sizeof(line), f)))
113                                 break;
114
115                         l = strstrip(line);
116                         if (*l == '#' || *l == 0)
117                                 continue;
118
119                         if (!(t = strdup(l))) {
120                                 log_error("Failed to allocate module name.");
121                                 continue;
122                         }
123
124                         if (n_arguments >= n_allocated) {
125                                 char **a;
126                                 unsigned m;
127
128                                 m = MAX(16U, n_arguments*2);
129
130                                 if (!(a = realloc(arguments, sizeof(char*) * (m+1)))) {
131                                         log_error("Failed to increase module array size.");
132                                         free(t);
133                                         r = EXIT_FAILURE;
134                                         continue;
135                                 }
136
137                                 arguments = a;
138                                 n_allocated = m;
139                         }
140
141                         arguments[n_arguments++] = t;
142                 }
143
144                 if (ferror(f)) {
145                         r = EXIT_FAILURE;
146                         log_error("Failed to read from file: %m");
147                 }
148
149                 fclose(f);
150         }
151
152         free(de);
153
154 finish:
155
156         if (n_arguments > 3) {
157                 arguments[n_arguments] = NULL;
158                 execv("/sbin/modprobe", arguments);
159
160                 log_error("Failed to execute /sbin/modprobe: %m");
161                 r = EXIT_FAILURE;
162         }
163
164         strv_free(arguments);
165
166         return r;
167 }