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