1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
29 #include <dbus/dbus.h>
32 #include "dbus-common.h"
35 static bool arg_skip = false;
36 static bool arg_force = false;
38 static void start_target(const char *target, bool isolate) {
39 DBusMessage *m = NULL, *reply = NULL;
41 const char *mode, *base_target = "base.target";
42 DBusConnection *bus = NULL;
46 dbus_error_init(&error);
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));
58 log_debug("Running request %s/start/%s", target, mode);
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.");
65 /* Start these units only if we can replace base.target with it */
67 if (!dbus_message_append_args(m,
68 DBUS_TYPE_STRING, &base_target,
69 DBUS_TYPE_STRING, &target,
70 DBUS_TYPE_STRING, &mode,
72 log_error("Could not attach target and flag information to message.");
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));
83 dbus_message_unref(m);
86 dbus_message_unref(reply);
89 dbus_connection_flush(bus);
90 dbus_connection_close(bus);
91 dbus_connection_unref(bus);
94 dbus_error_free(&error);
97 static int parse_proc_cmdline(void) {
98 char *line, *w, *state;
102 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
103 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
107 FOREACH_WORD_QUOTED(w, l, line, state) {
109 if (strneq(w, "fsck.mode=auto", l))
110 arg_force = arg_skip = false;
111 else if (strneq(w, "fsck.mode=force", l))
113 else if (strneq(w, "fsck.mode=skip", l))
115 else if (startswith(w, "fsck.mode"))
116 log_warning("Invalid fsck.mode= parameter. Ignoring.");
118 else if (strneq(w, "fastboot", l))
120 else if (strneq(w, "forcefsck", l))
129 static void test_files(void) {
130 if (access("/fastboot", F_OK) >= 0)
133 if (access("/forcefsck", F_OK) >= 0)
137 int main(int argc, char *argv[]) {
138 const char *cmdline[8];
139 int i = 0, r = EXIT_FAILURE, q;
142 struct udev *udev = NULL;
143 struct udev_device *udev_device = NULL;
148 log_error("This program expects one or no arguments.");
152 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
153 log_parse_environment();
156 parse_proc_cmdline();
159 if (!arg_force && arg_skip)
164 root_directory = false;
168 /* Find root device */
170 if (stat("/", &st) < 0) {
171 log_error("Failed to stat() the root directory: %m");
175 /* Virtual root devices don't need an fsck */
176 if (major(st.st_dev) == 0)
179 if (!(udev = udev_new())) {
180 log_error("Out of memory");
184 if (!(udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev))) {
185 log_error("Failed to detect root device.");
189 if (!(device = udev_device_get_devnode(udev_device))) {
190 log_error("Failed to detect device node of root directory.");
194 root_directory = true;
197 cmdline[i++] = "/sbin/fsck";
208 cmdline[i++] = device;
211 if ((pid = fork()) < 0) {
212 log_error("fork(): %m");
214 } else if (pid == 0) {
216 execv(cmdline[0], (char**) cmdline);
217 _exit(8); /* Operational error */
220 if ((q = wait_for_terminate(pid, &status)) < 0) {
221 log_error("waitid(): %s", strerror(-q));
225 if (status.si_code != CLD_EXITED || (status.si_status & ~1)) {
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);
232 log_error("fsck failed due to unknown reason.");
234 if (status.si_code == CLD_EXITED && status.si_status & 2)
235 /* System should be rebooted. */
236 start_target(SPECIAL_REBOOT_TARGET, false);
238 /* Some other problem */
239 start_target(SPECIAL_EMERGENCY_TARGET, true);
244 if (status.si_code == CLD_EXITED && (status.si_status & 1))
245 touch("/dev/.systemd/quotacheck");
249 udev_device_unref(udev_device);