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/>.
28 #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;
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", "StartUnit"))) {
61 log_error("Could not allocate message.");
65 if (!dbus_message_append_args(m,
66 DBUS_TYPE_STRING, &target,
67 DBUS_TYPE_STRING, &mode,
69 log_error("Could not attach target and flag information to message.");
73 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
74 log_error("Failed to start unit: %s", bus_error_message(&error));
80 dbus_message_unref(m);
83 dbus_message_unref(reply);
86 dbus_connection_flush(bus);
87 dbus_connection_close(bus);
88 dbus_connection_unref(bus);
91 dbus_error_free(&error);
94 static int parse_proc_cmdline(void) {
95 char *line, *w, *state;
99 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
100 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
104 FOREACH_WORD_QUOTED(w, l, line, state) {
106 if (strneq(w, "fsck.mode=auto", l))
107 arg_force = arg_skip = false;
108 else if (strneq(w, "fsck.mode=force", l))
110 else if (strneq(w, "fsck.mode=skip", l))
112 else if (startswith(w, "fsck.mode"))
113 log_warning("Invalid fsck.mode= parameter. Ignoring.");
115 else if (strneq(w, "fastboot", l))
117 else if (strneq(w, "forcefsck", l))
126 static void test_files(void) {
127 if (access("/fastboot", F_OK) >= 0)
130 if (access("/forcefsck", F_OK) >= 0)
134 int main(int argc, char *argv[]) {
135 static const char * cmdline[7];
136 int i = 0, r = EXIT_FAILURE, q;
141 log_error("This program expects exactly one argument.");
145 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
146 log_parse_environment();
149 parse_proc_cmdline();
156 /* FIXME: only execute necessary fsck's if no AC power present */
157 if (on_ac_power() == 0) {
158 log_info("Running on battery power, skipping file system check.");
163 cmdline[i++] = "/sbin/fsck";
171 cmdline[i++] = argv[1];
174 if ((pid = fork()) < 0) {
175 log_error("fork(): %m");
177 } else if (pid == 0) {
179 execv(cmdline[0], (char**) cmdline);
180 _exit(8); /* Operational error */
183 if ((q = wait_for_terminate(pid, &status)) < 0) {
184 log_error("waitid(): %s", strerror(-q));
188 if (status.si_code == CLD_KILLED ||
189 status.si_code == CLD_DUMPED) {
190 log_error("fsck terminated by signal %s.", signal_to_string(status.si_status));
193 } else if (status.si_code != CLD_EXITED) {
194 log_error("fsck failed due to unknown reason.");
198 if (status.si_status & ~1) {
200 if (access("/dev/.systemd/late-fsck", F_OK) >= 0) {
201 log_error("fsck failed with error code %i.", status.si_status);
205 if (status.si_status & 2)
206 /* System should be rebooted. */
207 start_target(SPECIAL_REBOOT_TARGET, false);
209 /* Some other problem */
210 start_target(SPECIAL_EMERGENCY_TARGET, true);
215 if (status.si_status & 1)
216 touch("/dev/.systemd/quotacheck");