chiark / gitweb /
util: use waitid() instead of waitpid() everywhere to avoid confusion due to SIGSTOP
[elogind.git] / src / kmod-setup.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 <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         "unix",    "/proc/net/unix"
36 };
37
38 int kmod_setup(void) {
39         unsigned i, n = 0;
40         const char * cmdline[3 + ELEMENTSOF(kmod_table) + 1];
41         ExecCommand command;
42         ExecContext context;
43         pid_t pid;
44         int r;
45         siginfo_t status;
46
47         for (i = 0; i < ELEMENTSOF(kmod_table); i += 2) {
48
49                 if (access(kmod_table[i+1], F_OK) >= 0)
50                         continue;
51
52                 log_debug("Your kernel apparently lacks built-in %s support. Might be a good idea to compile it in. "
53                           "We'll now try to work around this by calling '/sbin/modprobe %s'...",
54                           kmod_table[i], kmod_table[i]);
55
56                 cmdline[3 + n++] = kmod_table[i];
57         }
58
59         if (n <= 0)
60                 return 0;
61
62         cmdline[0] = "/sbin/modprobe";
63         cmdline[1] = "-qab";
64         cmdline[2] = "--";
65         cmdline[3 + n] = NULL;
66
67         zero(command);
68         zero(context);
69
70         command.path = (char*) cmdline[0];
71         command.argv = (char**) cmdline;
72
73         exec_context_init(&context);
74         r = exec_spawn(&command, NULL, &context, NULL, 0, NULL, false, false, false, false, NULL, &pid);
75         exec_context_done(&context);
76
77         if (r < 0)
78                 return r;
79
80         if ((r = wait_for_terminate(pid, &status)) < 0)
81                 return -errno;
82
83         if (status.si_code == CLD_EXITED) {
84                 if (status.si_status != 0) {
85                         log_warning("/sbin/modprobe failed with error code %i.", status.si_status);
86                         return -EPROTO;
87                 }
88
89                 log_debug("/sbin/modprobe succeeded.");
90                 return 0;
91
92         } else if (status.si_code == CLD_KILLED ||
93                    status.si_code == CLD_DUMPED) {
94
95                 log_warning("/sbin/modprobe terminated by signal %s.", signal_to_string(status.si_status));
96                 return -EPROTO;
97         }
98
99         log_warning("/sbin/modprobe failed due to unknown reason.");
100         return -EPROTO;
101 }