chiark / gitweb /
path: after installing inotify watches, recheck file again to fix race
[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-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. */
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             d->d_type != DT_UNKNOWN)
48                 return 0;
49
50         return endswith(d->d_name, ".conf");
51 }
52
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;
58
59         if (argc > 1) {
60                 log_error("This program takes no argument.");
61                 return EXIT_FAILURE;
62         }
63
64         log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
65         log_parse_environment();
66         log_open();
67
68         if (!(arguments = strv_new("/sbin/modprobe", "-sab", "--", NULL))) {
69                 log_error("Failed to allocate string array");
70                 goto finish;
71         }
72
73         n_arguments = n_allocated = 3;
74
75         if ((n = scandir("/etc/modules-load.d/", &de, scandir_filter, alphasort)) < 0) {
76
77                 if (errno == ENOENT)
78                         r = EXIT_SUCCESS;
79                 else
80                         log_error("Failed to enumerate /etc/modules-load.d/ files: %m");
81
82                 goto finish;
83         }
84
85         r = EXIT_SUCCESS;
86
87         for (i = 0; i < n; i++) {
88                 int k;
89                 char *fn;
90                 FILE *f;
91
92                 k = asprintf(&fn, "/etc/modules-load.d/%s", de[i]->d_name);
93                 free(de[i]);
94
95                 if (k < 0) {
96                         log_error("Failed to allocate file name.");
97                         r = EXIT_FAILURE;
98                         continue;
99                 }
100
101                 f = fopen(fn, "re");
102                 free(fn);
103
104                 if (!f) {
105                         if (errno == ENOENT)
106                                 continue;
107
108                         log_error("Failed to open %s: %m", fn);
109                         r = EXIT_FAILURE;
110                         continue;
111                 }
112
113                 for (;;) {
114                         char line[LINE_MAX], *l, *t;
115
116                         if (!(fgets(line, sizeof(line), f)))
117                                 break;
118
119                         l = strstrip(line);
120                         if (*l == '#' || *l == 0)
121                                 continue;
122
123                         if (!(t = strdup(l))) {
124                                 log_error("Failed to allocate module name.");
125                                 continue;
126                         }
127
128                         if (n_arguments >= n_allocated) {
129                                 char **a;
130                                 unsigned m;
131
132                                 m = MAX(16U, n_arguments*2);
133
134                                 if (!(a = realloc(arguments, sizeof(char*) * (m+1)))) {
135                                         log_error("Failed to increase module array size.");
136                                         free(t);
137                                         r = EXIT_FAILURE;
138                                         continue;
139                                 }
140
141                                 arguments = a;
142                                 n_allocated = m;
143                         }
144
145                         arguments[n_arguments++] = t;
146                 }
147
148                 if (ferror(f)) {
149                         r = EXIT_FAILURE;
150                         log_error("Failed to read from file: %m");
151                 }
152
153                 fclose(f);
154         }
155
156         free(de);
157
158 finish:
159
160         if (n_arguments > 3) {
161                 arguments[n_arguments] = NULL;
162                 execv("/sbin/modprobe", arguments);
163
164                 log_error("Failed to execute /sbin/modprobe: %m");
165                 r = EXIT_FAILURE;
166         }
167
168         strv_free(arguments);
169
170         return r;
171 }