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/>.
30 #include <dbus/dbus.h>
33 #include "dbus-common.h"
35 #include "bus-errors.h"
37 static bool arg_skip
= false;
38 static bool arg_force
= false;
40 static void start_target(const char *target
, bool isolate
) {
41 DBusMessage
*m
= NULL
, *reply
= NULL
;
43 const char *mode
, *basic_target
= "basic.target";
44 DBusConnection
*bus
= NULL
;
48 dbus_error_init(&error
);
50 if (bus_connect(DBUS_BUS_SYSTEM
, &bus
, NULL
, &error
) < 0) {
51 log_error("Failed to get D-Bus connection: %s", bus_error_message(&error
));
60 log_info("Running request %s/start/%s", target
, mode
);
62 if (!(m
= dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StartUnitReplace"))) {
63 log_error("Could not allocate message.");
67 /* Start these units only if we can replace base.target with it */
69 if (!dbus_message_append_args(m
,
70 DBUS_TYPE_STRING
, &basic_target
,
71 DBUS_TYPE_STRING
, &target
,
72 DBUS_TYPE_STRING
, &mode
,
74 log_error("Could not attach target and flag information to message.");
78 if (!(reply
= dbus_connection_send_with_reply_and_block(bus
, m
, -1, &error
))) {
80 /* Don't print a waring if we aren't called during
82 if (!dbus_error_has_name(&error
, BUS_ERROR_NO_SUCH_JOB
))
83 log_error("Failed to start unit: %s", bus_error_message(&error
));
90 dbus_message_unref(m
);
93 dbus_message_unref(reply
);
96 dbus_connection_flush(bus
);
97 dbus_connection_close(bus
);
98 dbus_connection_unref(bus
);
101 dbus_error_free(&error
);
104 static int parse_proc_cmdline(void) {
105 char *line
, *w
, *state
;
109 if (detect_container(NULL
) > 0)
112 if ((r
= read_one_line_file("/proc/cmdline", &line
)) < 0) {
113 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r
));
117 FOREACH_WORD_QUOTED(w
, l
, line
, state
) {
119 if (strneq(w
, "fsck.mode=auto", l
))
120 arg_force
= arg_skip
= false;
121 else if (strneq(w
, "fsck.mode=force", l
))
123 else if (strneq(w
, "fsck.mode=skip", l
))
125 else if (startswith(w
, "fsck.mode"))
126 log_warning("Invalid fsck.mode= parameter. Ignoring.");
127 #if defined(TARGET_FEDORA) || defined(TARGET_MANDRIVA)
128 else if (strneq(w
, "fastboot", l
))
130 else if (strneq(w
, "forcefsck", l
))
139 static void test_files(void) {
140 if (access("/fastboot", F_OK
) >= 0)
143 if (access("/forcefsck", F_OK
) >= 0)
147 int main(int argc
, char *argv
[]) {
148 const char *cmdline
[8];
149 int i
= 0, r
= EXIT_FAILURE
, q
;
152 struct udev
*udev
= NULL
;
153 struct udev_device
*udev_device
= NULL
;
158 log_error("This program expects one or no arguments.");
162 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG
);
163 log_parse_environment();
168 parse_proc_cmdline();
171 if (!arg_force
&& arg_skip
)
176 root_directory
= false;
179 struct timespec times
[2];
181 /* Find root device */
183 if (stat("/", &st
) < 0) {
184 log_error("Failed to stat() the root directory: %m");
188 /* Virtual root devices don't need an fsck */
189 if (major(st
.st_dev
) == 0)
192 /* check if we are already writable */
193 times
[0] = st
.st_atim
;
194 times
[1] = st
.st_mtim
;
195 if (utimensat(AT_FDCWD
, "/", times
, 0) == 0) {
196 log_info("Root directory is writable, skipping check.");
200 if (!(udev
= udev_new())) {
201 log_error("Out of memory");
205 if (!(udev_device
= udev_device_new_from_devnum(udev
, 'b', st
.st_dev
))) {
206 log_error("Failed to detect root device.");
210 if (!(device
= udev_device_get_devnode(udev_device
))) {
211 log_error("Failed to detect device node of root directory.");
215 root_directory
= true;
218 cmdline
[i
++] = "/sbin/fsck";
229 cmdline
[i
++] = device
;
232 if ((pid
= fork()) < 0) {
233 log_error("fork(): %m");
235 } else if (pid
== 0) {
237 execv(cmdline
[0], (char**) cmdline
);
238 _exit(8); /* Operational error */
241 if ((q
= wait_for_terminate(pid
, &status
)) < 0) {
242 log_error("waitid(): %s", strerror(-q
));
246 if (status
.si_code
!= CLD_EXITED
|| (status
.si_status
& ~1)) {
248 if (status
.si_code
== CLD_KILLED
|| status
.si_code
== CLD_DUMPED
)
249 log_error("fsck terminated by signal %s.", signal_to_string(status
.si_status
));
250 else if (status
.si_code
== CLD_EXITED
)
251 log_error("fsck failed with error code %i.", status
.si_status
);
253 log_error("fsck failed due to unknown reason.");
255 if (status
.si_code
== CLD_EXITED
&& (status
.si_status
& 2) && root_directory
)
256 /* System should be rebooted. */
257 start_target(SPECIAL_REBOOT_TARGET
, false);
258 else if (status
.si_code
== CLD_EXITED
&& (status
.si_status
& 6))
259 /* Some other problem */
260 start_target(SPECIAL_EMERGENCY_TARGET
, true);
263 log_warning("Ignoring error.");
269 if (status
.si_code
== CLD_EXITED
&& (status
.si_status
& 1))
270 touch("/run/systemd/quotacheck");
274 udev_device_unref(udev_device
);