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 Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
36 #include "bus-error.h"
37 #include "bus-errors.h"
39 #include "udev-util.h"
41 static bool arg_skip = false;
42 static bool arg_force = false;
43 static bool arg_show_progress = false;
45 static void start_target(const char *target) {
46 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
47 _cleanup_bus_unref_ sd_bus *bus = NULL;
52 r = bus_open_system_systemd(&bus);
54 log_error("Failed to get D-Bus connection: %s", strerror(-r));
58 log_info("Running request %s/start/replace", target);
60 /* Start these units only if we can replace base.target with it */
61 r = sd_bus_call_method(bus,
62 "org.freedesktop.systemd1",
63 "/org/freedesktop/systemd1",
64 "org.freedesktop.systemd1.Manager",
68 "sss", "basic.target", target, "replace");
70 /* Don't print a warning if we aren't called during startup */
71 if (r < 0 && !sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_JOB))
72 log_error("Failed to start unit: %s", bus_error_message(&error, -r));
75 static int parse_proc_cmdline(void) {
76 char *line, *w, *state;
80 r = proc_cmdline(&line);
82 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
86 FOREACH_WORD_QUOTED(w, l, line, state) {
88 if (strneq(w, "fsck.mode=auto", l))
89 arg_force = arg_skip = false;
90 else if (strneq(w, "fsck.mode=force", l))
92 else if (strneq(w, "fsck.mode=skip", l))
94 else if (startswith(w, "fsck"))
95 log_warning("Invalid fsck parameter. Ignoring.");
96 #ifdef HAVE_SYSV_COMPAT
97 else if (strneq(w, "fastboot", l)) {
98 log_error("Please pass 'fsck.mode=skip' rather than 'fastboot' on the kernel command line.");
100 } else if (strneq(w, "forcefsck", l)) {
101 log_error("Please pass 'fsck.mode=force' rather than 'forcefsck' on the kernel command line.");
111 static void test_files(void) {
112 #ifdef HAVE_SYSV_COMPAT
113 if (access("/fastboot", F_OK) >= 0) {
114 log_error("Please pass 'fsck.mode=skip' on the kernel command line rather than creating /fastboot on the root file system.");
118 if (access("/forcefsck", F_OK) >= 0) {
119 log_error("Please pass 'fsck.mode=force' on the kernel command line rather than creating /forcefsck on the root file system.");
124 if (access("/run/systemd/show-status", F_OK) >= 0 || plymouth_running())
125 arg_show_progress = true;
128 static double percent(int pass, unsigned long cur, unsigned long max) {
129 /* Values stolen from e2fsck */
131 static const int pass_table[] = {
132 0, 70, 90, 92, 95, 100
138 if ((unsigned) pass >= ELEMENTSOF(pass_table) || max == 0)
141 return (double) pass_table[pass-1] +
142 ((double) pass_table[pass] - (double) pass_table[pass-1]) *
143 (double) cur / (double) max;
146 static int process_progress(int fd) {
147 _cleanup_fclose_ FILE *console = NULL, *f = NULL;
154 close_nointr_nofail(fd);
158 console = fopen("/dev/console", "w");
164 unsigned long cur, max;
165 _cleanup_free_ char *device = NULL;
169 if (fscanf(f, "%i %lu %lu %ms", &pass, &cur, &max, &device) != 4)
172 /* Only show one progress counter at max */
174 if (flock(fileno(console), LOCK_EX|LOCK_NB) < 0)
180 /* Only update once every 50ms */
181 t = now(CLOCK_MONOTONIC);
182 if (last + 50 * USEC_PER_MSEC > t)
187 p = percent(pass, cur, max);
188 fprintf(console, "\r%s: fsck %3.1f%% complete...\r%n", device, p, &m);
198 fputc('\r', console);
199 for (j = 0; j < (unsigned) clear; j++)
201 fputc('\r', console);
208 int main(int argc, char *argv[]) {
209 const char *cmdline[9];
210 int i = 0, r = EXIT_FAILURE, q;
213 _cleanup_udev_unref_ struct udev *udev = NULL;
214 _cleanup_udev_device_unref_ struct udev_device *udev_device = NULL;
215 const char *device, *type;
217 int progress_pipe[2] = { -1, -1 };
222 log_error("This program expects one or no arguments.");
226 log_set_target(LOG_TARGET_AUTO);
227 log_parse_environment();
232 parse_proc_cmdline();
235 if (!arg_force && arg_skip)
246 root_directory = false;
248 if (stat(device, &st) < 0) {
249 log_error("Failed to stat '%s': %m", device);
253 udev_device = udev_device_new_from_devnum(udev, 'b', st.st_rdev);
255 log_error("Failed to detect device %s", device);
259 struct timespec times[2];
261 /* Find root device */
263 if (stat("/", &st) < 0) {
264 log_error("Failed to stat() the root directory: %m");
268 /* Virtual root devices don't need an fsck */
269 if (major(st.st_dev) == 0)
272 /* check if we are already writable */
273 times[0] = st.st_atim;
274 times[1] = st.st_mtim;
275 if (utimensat(AT_FDCWD, "/", times, 0) == 0) {
276 log_info("Root directory is writable, skipping check.");
280 udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev);
282 log_error("Failed to detect root device.");
286 device = udev_device_get_devnode(udev_device);
288 log_error("Failed to detect device node of root directory.");
292 root_directory = true;
295 type = udev_device_get_property_value(udev_device, "ID_FS_TYPE");
297 const char *checker = strappenda("/sbin/fsck.", type);
298 r = access(checker, X_OK);
300 if (errno == ENOENT) {
301 log_info("%s doesn't exist, not checking file system.", checker);
304 log_warning("%s cannot be used: %m", checker);
308 if (arg_show_progress)
309 if (pipe(progress_pipe) < 0) {
310 log_error("pipe(): %m");
314 cmdline[i++] = "/sbin/fsck";
325 if (progress_pipe[1] >= 0) {
326 snprintf(dash_c, sizeof(dash_c), "-C%i", progress_pipe[1]);
327 char_array_0(dash_c);
328 cmdline[i++] = dash_c;
331 cmdline[i++] = device;
336 log_error("fork(): %m");
338 } else if (pid == 0) {
340 if (progress_pipe[0] >= 0)
341 close_nointr_nofail(progress_pipe[0]);
342 execv(cmdline[0], (char**) cmdline);
343 _exit(8); /* Operational error */
346 if (progress_pipe[1] >= 0) {
347 close_nointr_nofail(progress_pipe[1]);
348 progress_pipe[1] = -1;
351 if (progress_pipe[0] >= 0) {
352 process_progress(progress_pipe[0]);
353 progress_pipe[0] = -1;
356 q = wait_for_terminate(pid, &status);
358 log_error("waitid(): %s", strerror(-q));
362 if (status.si_code != CLD_EXITED || (status.si_status & ~1)) {
364 if (status.si_code == CLD_KILLED || status.si_code == CLD_DUMPED)
365 log_error("fsck terminated by signal %s.", signal_to_string(status.si_status));
366 else if (status.si_code == CLD_EXITED)
367 log_error("fsck failed with error code %i.", status.si_status);
369 log_error("fsck failed due to unknown reason.");
371 if (status.si_code == CLD_EXITED && (status.si_status & 2) && root_directory)
372 /* System should be rebooted. */
373 start_target(SPECIAL_REBOOT_TARGET);
374 else if (status.si_code == CLD_EXITED && (status.si_status & 6))
375 /* Some other problem */
376 start_target(SPECIAL_EMERGENCY_TARGET);
379 log_warning("Ignoring error.");
385 if (status.si_code == CLD_EXITED && (status.si_status & 1))
386 touch("/run/systemd/quotacheck");
389 close_pipe(progress_pipe);