chiark / gitweb /
build-sys: move .pc files next to the matching sources
[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 int main(int argc, char *argv[]) {
35         int r = EXIT_FAILURE;
36         char **arguments = NULL;
37         unsigned n_arguments = 0, n_allocated = 0;
38         char **files, **fn;
39
40         if (argc > 1) {
41                 log_error("This program takes no argument.");
42                 return EXIT_FAILURE;
43         }
44
45         log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
46         log_parse_environment();
47         log_open();
48
49         umask(0022);
50
51         if (!(arguments = strv_new("/sbin/modprobe", "-sab", "--", NULL))) {
52                 log_error("Failed to allocate string array");
53                 goto finish;
54         }
55
56         n_arguments = n_allocated = 3;
57
58         if (conf_files_list(&files, ".conf",
59                             "/run/modules-load.d",
60                             "/etc/modules-load.d",
61                             "/usr/local/lib/modules-load.d",
62                             "/usr/lib/modules-load.d",
63                             "/lib/modules-load.d",
64                             NULL) < 0) {
65                 log_error("Failed to enumerate modules-load.d files: %s", strerror(-r));
66                 goto finish;
67         }
68
69         r = EXIT_SUCCESS;
70
71         STRV_FOREACH(fn, files) {
72                 FILE *f;
73
74                 f = fopen(*fn, "re");
75                 if (!f) {
76                         if (errno == ENOENT)
77                                 continue;
78
79                         log_error("Failed to open %s: %m", *fn);
80                         r = EXIT_FAILURE;
81                         continue;
82                 }
83
84                 log_debug("apply: %s\n", *fn);
85                 for (;;) {
86                         char line[LINE_MAX], *l, *t;
87
88                         if (!(fgets(line, sizeof(line), f)))
89                                 break;
90
91                         l = strstrip(line);
92                         if (*l == '#' || *l == 0)
93                                 continue;
94
95                         if (!(t = strdup(l))) {
96                                 log_error("Failed to allocate module name.");
97                                 continue;
98                         }
99
100                         if (n_arguments >= n_allocated) {
101                                 char **a;
102                                 unsigned m;
103
104                                 m = MAX(16U, n_arguments*2);
105
106                                 if (!(a = realloc(arguments, sizeof(char*) * (m+1)))) {
107                                         log_error("Failed to increase module array size.");
108                                         free(t);
109                                         r = EXIT_FAILURE;
110                                         continue;
111                                 }
112
113                                 arguments = a;
114                                 n_allocated = m;
115                         }
116
117                         arguments[n_arguments++] = t;
118                 }
119
120                 if (ferror(f)) {
121                         r = EXIT_FAILURE;
122                         log_error("Failed to read from file: %m");
123                 }
124
125                 fclose(f);
126         }
127
128         strv_free(files);
129 finish:
130
131         if (n_arguments > 3) {
132                 arguments[n_arguments] = NULL;
133                 strv_uniq(arguments);
134                 execv("/sbin/modprobe", arguments);
135
136                 log_error("Failed to execute /sbin/modprobe: %m");
137                 r = EXIT_FAILURE;
138         }
139
140         strv_free(arguments);
141
142         return r;
143 }