chiark / gitweb /
e13dcf4d9d975a95cfc46a4a86c13bb0f5f932e0
[elogind.git] / src / fsck.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 <stdio.h>
23 #include <stdbool.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <unistd.h>
27
28 #include <libudev.h>
29 #include <dbus/dbus.h>
30
31 #include "util.h"
32 #include "dbus-common.h"
33 #include "special.h"
34
35 static bool arg_skip = false;
36 static bool arg_force = false;
37
38 static void start_target(const char *target, bool isolate) {
39         DBusMessage *m = NULL, *reply = NULL;
40         DBusError error;
41         const char *mode, *base_target = "base.target";
42         DBusConnection *bus = NULL;
43
44         assert(target);
45
46         dbus_error_init(&error);
47
48         if (bus_connect(DBUS_BUS_SYSTEM, &bus, NULL, &error) < 0) {
49                 log_error("Failed to get D-Bus connection: %s", bus_error_message(&error));
50                 goto finish;
51         }
52
53         if (isolate)
54                 mode = "isolate";
55         else
56                 mode = "replace";
57
58         log_debug("Running request %s/start/%s", target, mode);
59
60         if (!(m = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StartUnitReplace"))) {
61                 log_error("Could not allocate message.");
62                 goto finish;
63         }
64
65         /* Start these units only if we can replace base.target with it */
66
67         if (!dbus_message_append_args(m,
68                                       DBUS_TYPE_STRING, &base_target,
69                                       DBUS_TYPE_STRING, &target,
70                                       DBUS_TYPE_STRING, &mode,
71                                       DBUS_TYPE_INVALID)) {
72                 log_error("Could not attach target and flag information to message.");
73                 goto finish;
74         }
75
76         if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
77                 log_error("Failed to start unit: %s", bus_error_message(&error));
78                 goto finish;
79         }
80
81 finish:
82         if (m)
83                 dbus_message_unref(m);
84
85         if (reply)
86                 dbus_message_unref(reply);
87
88         if (bus) {
89                 dbus_connection_flush(bus);
90                 dbus_connection_close(bus);
91                 dbus_connection_unref(bus);
92         }
93
94         dbus_error_free(&error);
95 }
96
97 static int parse_proc_cmdline(void) {
98         char *line, *w, *state;
99         int r;
100         size_t l;
101
102         if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
103                 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
104                 return 0;
105         }
106
107         FOREACH_WORD_QUOTED(w, l, line, state) {
108
109                 if (strneq(w, "fsck.mode=auto", l))
110                         arg_force = arg_skip = false;
111                 else if (strneq(w, "fsck.mode=force", l))
112                         arg_force = true;
113                 else if (strneq(w, "fsck.mode=skip", l))
114                         arg_skip = true;
115                 else if (startswith(w, "fsck.mode"))
116                         log_warning("Invalid fsck.mode= parameter. Ignoring.");
117 #ifdef TARGET_FEDORA
118                 else if (strneq(w, "fastboot", l))
119                         arg_skip = true;
120                 else if (strneq(w, "forcefsck", l))
121                         arg_force = true;
122 #endif
123         }
124
125         free(line);
126         return 0;
127 }
128
129 static void test_files(void) {
130         if (access("/fastboot", F_OK) >= 0)
131                 arg_skip = true;
132
133         if (access("/forcefsck", F_OK) >= 0)
134                 arg_force = true;
135 }
136
137 int main(int argc, char *argv[]) {
138         const char *cmdline[8];
139         int i = 0, r = EXIT_FAILURE, q;
140         pid_t pid;
141         siginfo_t status;
142         struct udev *udev = NULL;
143         struct udev_device *udev_device = NULL;
144         const char *device;
145         bool root_directory;
146
147         if (argc > 2) {
148                 log_error("This program expects one or no arguments.");
149                 return EXIT_FAILURE;
150         }
151
152         log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
153         log_parse_environment();
154         log_open();
155
156         parse_proc_cmdline();
157         test_files();
158
159         if (!arg_force && arg_skip)
160                 return 0;
161
162         if (argc > 1) {
163                 device = argv[1];
164                 root_directory = false;
165         } else {
166                 struct stat st;
167
168                 /* Find root device */
169
170                 if (stat("/", &st) < 0) {
171                         log_error("Failed to stat() the root directory: %m");
172                         goto finish;
173                 }
174
175                 /* Virtual root devices don't need an fsck */
176                 if (major(st.st_dev) == 0)
177                         return 0;
178
179                 if (!(udev = udev_new())) {
180                         log_error("Out of memory");
181                         goto finish;
182                 }
183
184                 if (!(udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev))) {
185                         log_error("Failed to detect root device.");
186                         goto finish;
187                 }
188
189                 if (!(device = udev_device_get_devnode(udev_device))) {
190                         log_error("Failed to detect device node of root directory.");
191                         goto finish;
192                 }
193
194                 root_directory = true;
195         }
196
197         cmdline[i++] = "/sbin/fsck";
198         cmdline[i++] = "-a";
199         cmdline[i++] = "-T";
200         cmdline[i++] = "-C";
201
202         if (!root_directory)
203                 cmdline[i++] = "-M";
204
205         if (arg_force)
206                 cmdline[i++] = "-f";
207
208         cmdline[i++] = device;
209         cmdline[i++] = NULL;
210
211         if ((pid = fork()) < 0) {
212                 log_error("fork(): %m");
213                 goto finish;
214         } else if (pid == 0) {
215                 /* Child */
216                 execv(cmdline[0], (char**) cmdline);
217                 _exit(8); /* Operational error */
218         }
219
220         if ((q = wait_for_terminate(pid, &status)) < 0) {
221                 log_error("waitid(): %s", strerror(-q));
222                 goto finish;
223         }
224
225         if (status.si_code != CLD_EXITED || (status.si_status & ~1)) {
226
227                 if (status.si_code == CLD_KILLED || status.si_code == CLD_DUMPED)
228                         log_error("fsck terminated by signal %s.", signal_to_string(status.si_status));
229                 else if (status.si_code == CLD_EXITED)
230                         log_error("fsck failed with error code %i.", status.si_status);
231                 else
232                         log_error("fsck failed due to unknown reason.");
233
234                 if (status.si_code == CLD_EXITED && status.si_status & 2)
235                         /* System should be rebooted. */
236                         start_target(SPECIAL_REBOOT_TARGET, false);
237                 else
238                         /* Some other problem */
239                         start_target(SPECIAL_EMERGENCY_TARGET, true);
240
241         } else
242                 r = EXIT_SUCCESS;
243
244         if (status.si_code == CLD_EXITED && (status.si_status & 1))
245                 touch("/dev/.systemd/quotacheck");
246
247 finish:
248         if (udev_device)
249                 udev_device_unref(udev_device);
250
251         if (udev)
252                 udev_unref(udev);
253
254         return r;
255 }