fsck.c revision b26fa1a2fbcfee7d03b0c8fd15ec3aa64ae70b9f
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
Copyright 2014 Holger Hans Peter Freyther
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include "sd-bus.h"
#include "sd-device.h"
#include "alloc-util.h"
#include "bus-common-errors.h"
#include "bus-error.h"
#include "bus-util.h"
#include "device-util.h"
#include "fd-util.h"
#include "fs-util.h"
#include "parse-util.h"
#include "path-util.h"
#include "proc-cmdline.h"
#include "process-util.h"
#include "signal-util.h"
#include "socket-util.h"
#include "special.h"
#include "stdio-util.h"
#include "util.h"
/* exit codes as defined in fsck(8) */
enum {
FSCK_SUCCESS = 0,
FSCK_ERROR_CORRECTED = 1,
FSCK_USER_CANCELLED = 32,
FSCK_SHARED_LIB_ERROR = 128,
};
static bool arg_skip = false;
static bool arg_force = false;
static bool arg_show_progress = false;
static const char *arg_repair = "-a";
int r;
r = bus_connect_system_systemd(&bus);
if (r < 0) {
log_error_errno(r, "Failed to get D-Bus connection: %m");
return;
}
/* Start these units only if we can replace base.target with it */
r = sd_bus_call_method(bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"StartUnitReplace",
&error,
NULL,
/* Don't print a warning if we aren't called during startup */
}
int r;
arg_force = true;
arg_skip = true;
else
arg_repair = "-a";
else {
r = parse_boolean(value);
if (r > 0)
arg_repair = "-y";
else if (r == 0)
arg_repair = "-n";
else
}
}
#ifdef HAVE_SYSV_COMPAT
log_warning("Please pass 'fsck.mode=skip' rather than 'fastboot' on the kernel command line.");
arg_skip = true;
log_warning("Please pass 'fsck.mode=force' rather than 'forcefsck' on the kernel command line.");
arg_force = true;
}
#endif
return 0;
}
static void test_files(void) {
#ifdef HAVE_SYSV_COMPAT
log_error("Please pass 'fsck.mode=skip' on the kernel command line rather than creating /fastboot on the root file system.");
arg_skip = true;
}
log_error("Please pass 'fsck.mode=force' on the kernel command line rather than creating /forcefsck on the root file system.");
arg_force = true;
}
#endif
}
/* Values stolen from e2fsck */
static const int pass_table[] = {
0, 70, 90, 92, 95, 100
};
if (pass <= 0)
return 0.0;
return 100.0;
}
static int process_progress(int fd) {
bool locked = false;
int clear = 0, r;
/* No progress pipe to process? Then we are a NOP. */
if (fd < 0)
return 0;
if (!f) {
safe_close(fd);
return -errno;
}
if (!console)
return -ENOMEM;
for (;;) {
int pass, m;
double p;
usec_t t;
if (ferror(f))
else if (feof(f))
r = 0;
else {
log_warning("Failed to parse progress pipe data");
r = -EBADMSG;
}
break;
}
/* Only show one progress counter at max */
if (!locked) {
continue;
locked = true;
}
/* Only update once every 50ms */
t = now(CLOCK_MONOTONIC);
continue;
last = t;
if (m > clear)
clear = m;
}
if (clear > 0) {
unsigned j;
for (j = 0; j < (unsigned) clear; j++)
}
return r;
}
static int fsck_progress_socket(void) {
static const union sockaddr_union sa = {
};
int fd, r;
if (fd < 0)
safe_close(fd);
return r;
}
return fd;
}
bool root_directory;
int r;
if (argc > 2) {
log_error("This program expects one or no arguments.");
return EXIT_FAILURE;
}
log_open();
umask(0022);
if (r < 0)
log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
test_files();
r = 0;
goto finish;
}
if (argc > 1) {
goto finish;
}
r = -EINVAL;
goto finish;
}
if (r < 0) {
goto finish;
}
root_directory = false;
} else {
/* Find root device */
goto finish;
}
/* Virtual root devices don't need an fsck */
log_debug("Root directory is virtual or btrfs, skipping check.");
r = 0;
goto finish;
}
/* check if we are already writable */
log_info("Root directory is writable, skipping check.");
r = 0;
goto finish;
}
if (r < 0) {
log_error_errno(r, "Failed to detect root device: %m");
goto finish;
}
if (r < 0) {
log_error_errno(r, "Failed to detect device node of root directory: %m");
goto finish;
}
root_directory = true;
}
if (r >= 0) {
r = fsck_exists(type);
if (r < 0)
log_warning_errno(r, "Couldn't detect if fsck.%s may be used for %s, proceeding: %m", type, device);
else if (r == 0) {
goto finish;
}
}
if (arg_show_progress) {
if (pipe(progress_pipe) < 0) {
goto finish;
}
}
if (pid < 0) {
goto finish;
}
if (pid == 0) {
int progress_socket = -1;
const char *cmdline[9];
int i = 0;
/* Child */
(void) reset_all_signal_handlers();
(void) reset_signal_mask();
/* Close the reading side of the progress pipe */
/* Try to connect to a progress management daemon, if there is one */
if (progress_socket >= 0) {
/* If this worked we close the progress pipe early, and just use the socket */
} else if (progress_pipe[1] >= 0) {
/* Otherwise if we have the progress pipe to our own local handle, we use it */
} else
dash_c[0] = 0;
cmdline[i++] = arg_repair;
cmdline[i++] = "-T";
/*
* The previous versions use flock for the device and conflict with
* udevd, see https://bugs.freedesktop.org/show_bug.cgi?id=79576#c5
*/
cmdline[i++] = "-l";
if (!root_directory)
cmdline[i++] = "-M";
if (arg_force)
cmdline[i++] = "-f";
}
(void) process_progress(progress_pipe[0]);
progress_pipe[0] = -1;
if (r < 0) {
log_error_errno(r, "waitid(): %m");
goto finish;
}
else
log_error("fsck failed due to unknown reason.");
r = -EINVAL;
if (status.si_code == CLD_EXITED && (status.si_status & FSCK_SYSTEM_SHOULD_REBOOT) && root_directory)
/* System should be rebooted. */
else if (status.si_code == CLD_EXITED && (status.si_status & (FSCK_SYSTEM_SHOULD_REBOOT | FSCK_ERRORS_LEFT_UNCORRECTED)))
/* Some other problem */
else {
log_warning("Ignoring error.");
r = 0;
}
} else
r = 0;
(void) touch("/run/systemd/quotacheck");
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}