chiark / gitweb /
unit: avoid assert on daemon reload
[elogind.git] / src / kmod-setup.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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 <sys/wait.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <errno.h>
26
27 #include "macro.h"
28 #include "execute.h"
29
30 #include "kmod-setup.h"
31
32 static const char * const kmod_table[] = {
33         "autofs4", "/sys/class/misc/autofs",
34         "ipv6",    "/sys/module/ipv6"
35 };
36
37 int kmod_setup(void) {
38         unsigned i, n = 0;
39         const char * cmdline[3 + ELEMENTSOF(kmod_table) + 1];
40         ExecCommand command;
41         ExecContext context;
42         pid_t pid;
43         int status, r;
44
45         for (i = 0; i < ELEMENTSOF(kmod_table); i += 2) {
46
47                 if (access(kmod_table[i+1], F_OK) >= 0)
48                         continue;
49
50                 log_info("Your kernel apparently lacks built-in %s support. Please fix that. "
51                          "We'll now try to work around this by calling '/sbin/modprobe %s'...",
52                          kmod_table[i], kmod_table[i]);
53
54                 cmdline[3 + n++] = kmod_table[i];
55         }
56
57         if (n <= 0)
58                 return 0;
59
60         cmdline[0] = "/sbin/modprobe";
61         cmdline[1] = "-qab";
62         cmdline[2] = "--";
63         cmdline[3 + n] = NULL;
64
65         zero(command);
66         zero(context);
67
68         command.path = (char*) cmdline[0];
69         command.argv = (char**) cmdline;
70
71         exec_context_init(&context);
72         r = exec_spawn(&command, NULL, &context, NULL, 0, NULL, false, false, false, NULL, &pid);
73         exec_context_done(&context);
74
75         if (r < 0)
76                 return r;
77
78         for (;;) {
79                 if (waitpid(pid, &status, 0) < 0) {
80
81                         if (errno == EINTR)
82                                 continue;
83
84                         return -errno;
85                 }
86
87                 break;
88         }
89
90         if (WIFEXITED(status)) {
91                 if (WEXITSTATUS(status) != 0) {
92                         log_warning("/sbin/modprobe failed with error code %i.", WEXITSTATUS(status));
93                         return -EPROTO;
94                 }
95
96                 log_debug("/sbin/modprobe succeeded.");
97                 return 0;
98         }
99
100         if (WIFSIGNALED(status)) {
101                 log_warning("/sbin/modprobe terminated by signal %s.", strsignal(WTERMSIG(status)));
102                 return -EPROTO;
103         }
104
105         log_warning("/sbin/modprobe failed due to unknown reason.");
106         return -EPROTO;
107 }