1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2014 Holger Hans Peter Freyther
11 #include <sys/prctl.h>
16 #include "sd-device.h"
18 #include "alloc-util.h"
19 #include "bus-common-errors.h"
20 #include "bus-error.h"
22 #include "device-util.h"
25 #include "parse-util.h"
26 #include "path-util.h"
27 #include "proc-cmdline.h"
28 #include "process-util.h"
29 #include "signal-util.h"
30 #include "socket-util.h"
32 #include "stdio-util.h"
35 /* exit codes as defined in fsck(8) */
38 FSCK_ERROR_CORRECTED
= 1 << 0,
39 FSCK_SYSTEM_SHOULD_REBOOT
= 1 << 1,
40 FSCK_ERRORS_LEFT_UNCORRECTED
= 1 << 2,
41 FSCK_OPERATIONAL_ERROR
= 1 << 3,
42 FSCK_USAGE_OR_SYNTAX_ERROR
= 1 << 4,
43 FSCK_USER_CANCELLED
= 1 << 5,
44 FSCK_SHARED_LIB_ERROR
= 1 << 7,
47 static bool arg_skip
= false;
48 static bool arg_force
= false;
49 static bool arg_show_progress
= false;
50 static const char *arg_repair
= "-a";
52 static void start_target(const char *target
, const char *mode
) {
53 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
54 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
59 r
= bus_connect_system_systemd(&bus
);
61 log_error_errno(r
, "Failed to get D-Bus connection: %m");
65 log_info("Running request %s/start/replace", target
);
67 /* Start these units only if we can replace base.target with it */
68 r
= sd_bus_call_method(bus
,
69 "org.freedesktop.systemd1",
70 "/org/freedesktop/systemd1",
71 "org.freedesktop.systemd1.Manager",
75 "sss", "basic.target", target
, mode
);
77 /* Don't print a warning if we aren't called during startup */
78 if (r
< 0 && !sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_JOB
))
79 log_error("Failed to start unit: %s", bus_error_message(&error
, r
));
82 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
87 if (streq(key
, "fsck.mode")) {
89 if (proc_cmdline_value_missing(key
, value
))
92 if (streq(value
, "auto"))
93 arg_force
= arg_skip
= false;
94 else if (streq(value
, "force"))
96 else if (streq(value
, "skip"))
99 log_warning("Invalid fsck.mode= parameter '%s'. Ignoring.", value
);
101 } else if (streq(key
, "fsck.repair")) {
103 if (proc_cmdline_value_missing(key
, value
))
106 if (streq(value
, "preen"))
109 r
= parse_boolean(value
);
115 log_warning("Invalid fsck.repair= parameter '%s'. Ignoring.", value
);
120 else if (streq(key
, "fastboot") && !value
) {
121 log_warning("Please pass 'fsck.mode=skip' rather than 'fastboot' on the kernel command line.");
124 } else if (streq(key
, "forcefsck") && !value
) {
125 log_warning("Please pass 'fsck.mode=force' rather than 'forcefsck' on the kernel command line.");
133 static void test_files(void) {
136 if (access("/fastboot", F_OK
) >= 0) {
137 log_error("Please pass 'fsck.mode=skip' on the kernel command line rather than creating /fastboot on the root file system.");
141 if (access("/forcefsck", F_OK
) >= 0) {
142 log_error("Please pass 'fsck.mode=force' on the kernel command line rather than creating /forcefsck on the root file system.");
147 arg_show_progress
= access("/run/systemd/show-status", F_OK
) >= 0;
150 static double percent(int pass
, unsigned long cur
, unsigned long max
) {
151 /* Values stolen from e2fsck */
153 static const int pass_table
[] = {
154 0, 70, 90, 92, 95, 100
160 if ((unsigned) pass
>= ELEMENTSOF(pass_table
) || max
== 0)
163 return (double) pass_table
[pass
-1] +
164 ((double) pass_table
[pass
] - (double) pass_table
[pass
-1]) *
165 (double) cur
/ (double) max
;
168 static int process_progress(int fd
) {
169 _cleanup_fclose_
FILE *console
= NULL
, *f
= NULL
;
174 /* No progress pipe to process? Then we are a NOP. */
178 f
= fdopen(fd
, "re");
184 console
= fopen("/dev/console", "we");
190 unsigned long cur
, max
;
191 _cleanup_free_
char *device
= NULL
;
195 if (fscanf(f
, "%i %lu %lu %ms", &pass
, &cur
, &max
, &device
) != 4) {
198 r
= log_warning_errno(errno
, "Failed to read from progress pipe: %m");
202 log_warning("Failed to parse progress pipe data");
208 /* Only show one progress counter at max */
210 if (flock(fileno(console
), LOCK_EX
|LOCK_NB
) < 0)
216 /* Only update once every 50ms */
217 t
= now(CLOCK_MONOTONIC
);
218 if (last
+ 50 * USEC_PER_MSEC
> t
)
223 p
= percent(pass
, cur
, max
);
224 fprintf(console
, "\r%s: fsck %3.1f%% complete...\r%n", device
, p
, &m
);
234 fputc('\r', console
);
235 for (j
= 0; j
< (unsigned) clear
; j
++)
237 fputc('\r', console
);
244 static int fsck_progress_socket(void) {
245 static const union sockaddr_union sa
= {
246 .un
.sun_family
= AF_UNIX
,
247 .un
.sun_path
= "/run/systemd/fsck.progress",
252 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
254 return log_warning_errno(errno
, "socket(): %m");
256 if (connect(fd
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
)) < 0) {
257 r
= log_full_errno(IN_SET(errno
, ECONNREFUSED
, ENOENT
) ? LOG_DEBUG
: LOG_WARNING
,
258 errno
, "Failed to connect to progress socket %s, ignoring: %m", sa
.un
.sun_path
);
266 int main(int argc
, char *argv
[]) {
267 _cleanup_close_pair_
int progress_pipe
[2] = { -1, -1 };
268 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
269 const char *device
, *type
;
276 log_error("This program expects one or no arguments.");
280 log_set_target(LOG_TARGET_AUTO
);
281 log_parse_environment();
286 r
= proc_cmdline_parse(parse_proc_cmdline_item
, NULL
, PROC_CMDLINE_STRIP_RD_PREFIX
);
288 log_warning_errno(r
, "Failed to parse kernel command line, ignoring: %m");
292 if (!arg_force
&& arg_skip
) {
300 if (stat(device
, &st
) < 0) {
301 r
= log_error_errno(errno
, "Failed to stat %s: %m", device
);
305 if (!S_ISBLK(st
.st_mode
)) {
306 log_error("%s is not a block device.", device
);
311 r
= sd_device_new_from_devnum(&dev
, 'b', st
.st_rdev
);
313 log_error_errno(r
, "Failed to detect device %s: %m", device
);
317 root_directory
= false;
319 struct timespec times
[2];
321 /* Find root device */
323 if (stat("/", &st
) < 0) {
324 r
= log_error_errno(errno
, "Failed to stat() the root directory: %m");
328 /* Virtual root devices don't need an fsck */
329 if (major(st
.st_dev
) == 0) {
330 log_debug("Root directory is virtual or btrfs, skipping check.");
335 /* check if we are already writable */
336 times
[0] = st
.st_atim
;
337 times
[1] = st
.st_mtim
;
339 if (utimensat(AT_FDCWD
, "/", times
, 0) == 0) {
340 log_info("Root directory is writable, skipping check.");
345 r
= sd_device_new_from_devnum(&dev
, 'b', st
.st_dev
);
347 log_error_errno(r
, "Failed to detect root device: %m");
351 r
= sd_device_get_devname(dev
, &device
);
353 log_error_errno(r
, "Failed to detect device node of root directory: %m");
357 root_directory
= true;
360 if (sd_device_get_property_value(dev
, "ID_FS_TYPE", &type
) >= 0) {
361 r
= fsck_exists(type
);
363 log_warning_errno(r
, "Couldn't detect if fsck.%s may be used for %s, proceeding: %m", type
, device
);
365 log_info("fsck.%s doesn't exist, not checking file system on %s.", type
, device
);
370 if (arg_show_progress
) {
371 if (pipe(progress_pipe
) < 0) {
372 r
= log_error_errno(errno
, "pipe(): %m");
377 r
= safe_fork("(fsck)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &pid
);
381 char dash_c
[STRLEN("-C") + DECIMAL_STR_MAX(int) + 1];
382 int progress_socket
= -1;
383 const char *cmdline
[9];
388 /* Close the reading side of the progress pipe */
389 progress_pipe
[0] = safe_close(progress_pipe
[0]);
391 /* Try to connect to a progress management daemon, if there is one */
392 progress_socket
= fsck_progress_socket();
393 if (progress_socket
>= 0) {
394 /* If this worked we close the progress pipe early, and just use the socket */
395 progress_pipe
[1] = safe_close(progress_pipe
[1]);
396 xsprintf(dash_c
, "-C%i", progress_socket
);
397 } else if (progress_pipe
[1] >= 0) {
398 /* Otherwise if we have the progress pipe to our own local handle, we use it */
399 xsprintf(dash_c
, "-C%i", progress_pipe
[1]);
403 cmdline
[i
++] = "/sbin/fsck";
404 cmdline
[i
++] = arg_repair
;
408 * Since util-linux v2.25 fsck uses /run/fsck/<diskname>.lock files.
409 * The previous versions use flock for the device and conflict with
410 * udevd, see https://bugs.freedesktop.org/show_bug.cgi?id=79576#c5
420 if (!isempty(dash_c
))
421 cmdline
[i
++] = dash_c
;
423 cmdline
[i
++] = device
;
426 execv(cmdline
[0], (char**) cmdline
);
427 _exit(FSCK_OPERATIONAL_ERROR
);
430 progress_pipe
[1] = safe_close(progress_pipe
[1]);
431 (void) process_progress(progress_pipe
[0]);
432 progress_pipe
[0] = -1;
434 exit_status
= wait_for_terminate_and_check("fsck", pid
, WAIT_LOG_ABNORMAL
);
435 if (exit_status
< 0) {
439 if (exit_status
& ~1) {
440 log_error("fsck failed with exit status %i.", exit_status
);
442 if ((exit_status
& FSCK_SYSTEM_SHOULD_REBOOT
) && root_directory
) {
443 /* System should be rebooted. */
444 start_target(SPECIAL_REBOOT_TARGET
, "replace-irreversibly");
446 } else if (exit_status
& (FSCK_SYSTEM_SHOULD_REBOOT
| FSCK_ERRORS_LEFT_UNCORRECTED
)) {
447 /* Some other problem */
448 start_target(SPECIAL_EMERGENCY_TARGET
, "replace");
451 log_warning("Ignoring error.");
457 if (exit_status
& FSCK_ERROR_CORRECTED
)
458 (void) touch("/run/systemd/quotacheck");
461 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;