1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 Copyright © 2014 Holger Hans Peter Freyther
10 #include <sys/prctl.h>
15 #include "sd-device.h"
17 #include "alloc-util.h"
18 #include "bus-common-errors.h"
19 #include "bus-error.h"
21 #include "device-util.h"
24 #include "fsck-util.h"
25 #include "main-func.h"
26 #include "parse-util.h"
27 #include "path-util.h"
28 #include "proc-cmdline.h"
29 #include "process-util.h"
30 #include "rlimit-util.h"
31 #include "signal-util.h"
32 #include "socket-util.h"
34 #include "stdio-util.h"
36 static bool arg_skip
= false;
37 static bool arg_force
= false;
38 static bool arg_show_progress
= false;
39 static const char *arg_repair
= "-a";
41 static void start_target(const char *target
, const char *mode
) {
42 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
43 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
48 r
= bus_connect_system_systemd(&bus
);
50 log_error_errno(r
, "Failed to get D-Bus connection: %m");
54 log_info("Requesting %s/start/%s", target
, mode
);
56 /* Start this unit only if we can replace basic.target with it */
57 r
= sd_bus_call_method(bus
,
58 "org.freedesktop.systemd1",
59 "/org/freedesktop/systemd1",
60 "org.freedesktop.systemd1.Manager",
64 "sss", "basic.target", target
, mode
);
66 /* Don't print a warning if we aren't called during startup */
67 if (r
< 0 && !sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_JOB
))
68 log_error("Failed to start unit: %s", bus_error_message(&error
, r
));
71 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
76 if (streq(key
, "fsck.mode")) {
78 if (proc_cmdline_value_missing(key
, value
))
81 if (streq(value
, "auto"))
82 arg_force
= arg_skip
= false;
83 else if (streq(value
, "force"))
85 else if (streq(value
, "skip"))
88 log_warning("Invalid fsck.mode= parameter '%s'. Ignoring.", value
);
90 } else if (streq(key
, "fsck.repair")) {
92 if (proc_cmdline_value_missing(key
, value
))
95 if (streq(value
, "preen"))
98 r
= parse_boolean(value
);
104 log_warning("Invalid fsck.repair= parameter '%s'. Ignoring.", value
);
109 else if (streq(key
, "fastboot") && !value
) {
110 log_warning("Please pass 'fsck.mode=skip' rather than 'fastboot' on the kernel command line.");
113 } else if (streq(key
, "forcefsck") && !value
) {
114 log_warning("Please pass 'fsck.mode=force' rather than 'forcefsck' on the kernel command line.");
122 static void test_files(void) {
125 if (access("/fastboot", F_OK
) >= 0) {
126 log_error("Please pass 'fsck.mode=skip' on the kernel command line rather than creating /fastboot on the root file system.");
130 if (access("/forcefsck", F_OK
) >= 0) {
131 log_error("Please pass 'fsck.mode=force' on the kernel command line rather than creating /forcefsck on the root file system.");
136 arg_show_progress
= access("/run/systemd/show-status", F_OK
) >= 0;
139 static double percent(int pass
, unsigned long cur
, unsigned long max
) {
140 /* Values stolen from e2fsck */
142 static const int pass_table
[] = {
143 0, 70, 90, 92, 95, 100
149 if ((unsigned) pass
>= ELEMENTSOF(pass_table
) || max
== 0)
152 return (double) pass_table
[pass
-1] +
153 ((double) pass_table
[pass
] - (double) pass_table
[pass
-1]) *
154 (double) cur
/ (double) max
;
157 static int process_progress(int fd
, FILE* console
) {
158 _cleanup_fclose_
FILE *f
= NULL
;
163 /* No progress pipe to process? Then we are a NOP. */
170 return log_debug_errno(errno
, "Failed to use pipe: %m");
175 unsigned long cur
, max
;
176 _cleanup_free_
char *device
= NULL
;
180 if (fscanf(f
, "%i %lu %lu %ms", &pass
, &cur
, &max
, &device
) != 4) {
183 r
= log_warning_errno(errno
, "Failed to read from progress pipe: %m");
187 r
= log_warning_errno(SYNTHETIC_ERRNO(errno
), "Failed to parse progress pipe data");
192 /* Only show one progress counter at max */
194 if (flock(fileno(console
), LOCK_EX
|LOCK_NB
) < 0)
200 /* Only update once every 50ms */
201 t
= now(CLOCK_MONOTONIC
);
202 if (last
+ 50 * USEC_PER_MSEC
> t
)
207 p
= percent(pass
, cur
, max
);
208 r
= fprintf(console
, "\r%s: fsck %3.1f%% complete...\r", device
, p
);
210 return -EIO
; /* No point in continuing if something happened to our output stream */
213 clear
= MAX(clear
, r
);
217 fputc('\r', console
);
218 for (int j
= 0; j
< clear
; j
++)
220 fputc('\r', console
);
227 static int fsck_progress_socket(void) {
228 _cleanup_close_
int fd
= -EBADF
;
231 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
233 return log_warning_errno(errno
, "socket(): %m");
235 r
= connect_unix_path(fd
, AT_FDCWD
, "/run/systemd/fsck.progress");
237 return log_full_errno(IN_SET(r
, -ECONNREFUSED
, -ENOENT
) ? LOG_DEBUG
: LOG_WARNING
,
238 r
, "Failed to connect to progress socket, ignoring: %m");
243 static int run(int argc
, char *argv
[]) {
244 _cleanup_close_pair_
int progress_pipe
[2] = { -EBADF
, -EBADF
};
245 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
246 _cleanup_free_
char *dpath
= NULL
;
247 _cleanup_fclose_
FILE *console
= NULL
;
248 const char *device
, *type
;
257 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
258 "This program expects one or no arguments.");
262 r
= proc_cmdline_parse(parse_proc_cmdline_item
, NULL
, PROC_CMDLINE_STRIP_RD_PREFIX
);
264 log_warning_errno(r
, "Failed to parse kernel command line, ignoring: %m");
268 if (!arg_force
&& arg_skip
)
272 dpath
= strdup(argv
[1]);
278 if (stat(device
, &st
) < 0)
279 return log_error_errno(errno
, "Failed to stat %s: %m", device
);
281 if (!S_ISBLK(st
.st_mode
))
282 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
283 "%s is not a block device.",
286 r
= sd_device_new_from_stat_rdev(&dev
, &st
);
288 return log_error_errno(r
, "Failed to detect device %s: %m", device
);
290 root_directory
= false;
292 struct timespec times
[2];
294 /* Find root device */
296 if (stat("/", &st
) < 0)
297 return log_error_errno(errno
, "Failed to stat() the root directory: %m");
299 /* Virtual root devices don't need an fsck */
300 if (major(st
.st_dev
) == 0) {
301 log_debug("Root directory is virtual or btrfs, skipping check.");
305 /* check if we are already writable */
306 times
[0] = st
.st_atim
;
307 times
[1] = st
.st_mtim
;
309 if (utimensat(AT_FDCWD
, "/", times
, 0) == 0) {
310 log_info("Root directory is writable, skipping check.");
314 r
= sd_device_new_from_devnum(&dev
, 'b', st
.st_dev
);
316 return log_error_errno(r
, "Failed to detect root device: %m");
318 r
= sd_device_get_devname(dev
, &device
);
320 return log_device_error_errno(dev
, r
, "Failed to detect device node of root directory: %m");
322 root_directory
= true;
325 if (sd_device_get_property_value(dev
, "ID_FS_TYPE", &type
) >= 0) {
326 r
= fsck_exists_for_fstype(type
);
328 log_device_warning_errno(dev
, r
, "Couldn't detect if fsck.%s may be used, proceeding: %m", type
);
330 log_device_info(dev
, "fsck.%s doesn't exist, not checking file system.", type
);
336 log_device_warning_errno(dev
, r
, "Couldn't detect if the fsck command may be used, proceeding: %m");
338 log_device_info(dev
, "The fsck command does not exist, not checking file system.");
343 console
= fopen("/dev/console", "we");
346 pipe(progress_pipe
) < 0)
347 return log_error_errno(errno
, "pipe(): %m");
349 r
= safe_fork("(fsck)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
|FORK_LOG
, &pid
);
353 char dash_c
[STRLEN("-C") + DECIMAL_STR_MAX(int) + 1];
354 int progress_socket
= -1;
355 const char *cmdline
[9];
360 /* Close the reading side of the progress pipe */
361 progress_pipe
[0] = safe_close(progress_pipe
[0]);
363 /* Try to connect to a progress management daemon, if there is one */
364 progress_socket
= fsck_progress_socket();
365 if (progress_socket
>= 0) {
366 /* If this worked we close the progress pipe early, and just use the socket */
367 progress_pipe
[1] = safe_close(progress_pipe
[1]);
368 xsprintf(dash_c
, "-C%i", progress_socket
);
369 } else if (progress_pipe
[1] >= 0) {
370 /* Otherwise if we have the progress pipe to our own local handle, we use it */
371 xsprintf(dash_c
, "-C%i", progress_pipe
[1]);
375 cmdline
[i
++] = "/sbin/fsck";
376 cmdline
[i
++] = arg_repair
;
380 * Since util-linux v2.25 fsck uses /run/fsck/<diskname>.lock files.
381 * The previous versions use flock for the device and conflict with
382 * udevd, see https://bugs.freedesktop.org/show_bug.cgi?id=79576#c5
392 if (!isempty(dash_c
))
393 cmdline
[i
++] = dash_c
;
395 cmdline
[i
++] = device
;
398 (void) rlimit_nofile_safe();
400 execv(cmdline
[0], (char**) cmdline
);
401 _exit(FSCK_OPERATIONAL_ERROR
);
405 progress_pipe
[1] = safe_close(progress_pipe
[1]);
406 (void) process_progress(TAKE_FD(progress_pipe
[0]), console
);
409 exit_status
= wait_for_terminate_and_check("fsck", pid
, WAIT_LOG_ABNORMAL
);
412 if ((exit_status
& ~FSCK_ERROR_CORRECTED
) != FSCK_SUCCESS
) {
413 log_error("fsck failed with exit status %i.", exit_status
);
415 if ((exit_status
& FSCK_SYSTEM_SHOULD_REBOOT
) && root_directory
) {
416 /* System should be rebooted. */
417 start_target(SPECIAL_REBOOT_TARGET
, "replace-irreversibly");
419 } else if (!(exit_status
& (FSCK_SYSTEM_SHOULD_REBOOT
| FSCK_ERRORS_LEFT_UNCORRECTED
)))
420 log_warning("Ignoring error.");
423 if (exit_status
& FSCK_ERROR_CORRECTED
)
424 (void) touch("/run/systemd/quotacheck");
426 return !!(exit_status
& (FSCK_SYSTEM_SHOULD_REBOOT
| FSCK_ERRORS_LEFT_UNCORRECTED
));
429 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);