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