chiark / gitweb /
ask-password: don't show wall message on ttys we are already running a tty agent on
[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                 return 0;
48
49         return endswith(d->d_name, ".conf");
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-load.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-load.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-load.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                         if (errno == ENOENT)
105                                 continue;
106
107                         log_error("Failed to open %s: %m", fn);
108                         r = EXIT_FAILURE;
109                         continue;
110                 }
111
112                 for (;;) {
113                         char line[LINE_MAX], *l, *t;
114
115                         if (!(fgets(line, sizeof(line), f)))
116                                 break;
117
118                         l = strstrip(line);
119                         if (*l == '#' || *l == 0)
120                                 continue;
121
122                         if (!(t = strdup(l))) {
123                                 log_error("Failed to allocate module name.");
124                                 continue;
125                         }
126
127                         if (n_arguments >= n_allocated) {
128                                 char **a;
129                                 unsigned m;
130
131                                 m = MAX(16U, n_arguments*2);
132
133                                 if (!(a = realloc(arguments, sizeof(char*) * (m+1)))) {
134                                         log_error("Failed to increase module array size.");
135                                         free(t);
136                                         r = EXIT_FAILURE;
137                                         continue;
138                                 }
139
140                                 arguments = a;
141                                 n_allocated = m;
142                         }
143
144                         arguments[n_arguments++] = t;
145                 }
146
147                 if (ferror(f)) {
148                         r = EXIT_FAILURE;
149                         log_error("Failed to read from file: %m");
150                 }
151
152                 fclose(f);
153         }
154
155         free(de);
156
157 finish:
158
159         if (n_arguments > 3) {
160                 arguments[n_arguments] = NULL;
161                 execv("/sbin/modprobe", arguments);
162
163                 log_error("Failed to execute /sbin/modprobe: %m");
164                 r = EXIT_FAILURE;
165         }
166
167         strv_free(arguments);
168
169         return r;
170 }