]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/modules-load.c
hostnamed: introduce systemd-hostnamed
[thirdparty/systemd.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
103 if (!f) {
104 if (errno == ENOENT) {
105 free(fn);
106 continue;
107 }
108
109 log_error("Failed to open %s: %m", fn);
110 free(fn);
111 r = EXIT_FAILURE;
112 continue;
113 }
114
115 free(fn);
116
117 for (;;) {
118 char line[LINE_MAX], *l, *t;
119
120 if (!(fgets(line, sizeof(line), f)))
121 break;
122
123 l = strstrip(line);
124 if (*l == '#' || *l == 0)
125 continue;
126
127 if (!(t = strdup(l))) {
128 log_error("Failed to allocate module name.");
129 continue;
130 }
131
132 if (n_arguments >= n_allocated) {
133 char **a;
134 unsigned m;
135
136 m = MAX(16U, n_arguments*2);
137
138 if (!(a = realloc(arguments, sizeof(char*) * (m+1)))) {
139 log_error("Failed to increase module array size.");
140 free(t);
141 r = EXIT_FAILURE;
142 continue;
143 }
144
145 arguments = a;
146 n_allocated = m;
147 }
148
149 arguments[n_arguments++] = t;
150 }
151
152 if (ferror(f)) {
153 r = EXIT_FAILURE;
154 log_error("Failed to read from file: %m");
155 }
156
157 fclose(f);
158 }
159
160 free(de);
161
162 finish:
163
164 if (n_arguments > 3) {
165 arguments[n_arguments] = NULL;
166 execv("/sbin/modprobe", arguments);
167
168 log_error("Failed to execute /sbin/modprobe: %m");
169 r = EXIT_FAILURE;
170 }
171
172 strv_free(arguments);
173
174 return r;
175 }