]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/fsck/fsck.c
tree-wide: beautify remaining copyright statements
[thirdparty/systemd.git] / src / fsck / fsck.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
3d20ed6d 2/***
96b2fb93 3 Copyright © 2014 Holger Hans Peter Freyther
3d20ed6d
LP
4***/
5
3d20ed6d 6#include <errno.h>
a84f5192 7#include <fcntl.h>
cf0fbc49
TA
8#include <stdbool.h>
9#include <stdio.h>
27d340c7 10#include <sys/file.h>
96d9117a 11#include <sys/prctl.h>
cf0fbc49
TA
12#include <sys/stat.h>
13#include <unistd.h>
3d20ed6d 14
0c842e0a 15#include "sd-bus.h"
9102fdc5 16#include "sd-device.h"
3d20ed6d 17
b5efdb8a 18#include "alloc-util.h"
96aad8d1 19#include "bus-common-errors.h"
3ffd4af2
LP
20#include "bus-error.h"
21#include "bus-util.h"
9102fdc5 22#include "device-util.h"
3ffd4af2 23#include "fd-util.h"
f4f15635 24#include "fs-util.h"
6bedfcbb 25#include "parse-util.h"
eb66db55 26#include "path-util.h"
4e731273 27#include "proc-cmdline.h"
3ffd4af2
LP
28#include "process-util.h"
29#include "signal-util.h"
ac6e2f0d 30#include "socket-util.h"
3ffd4af2 31#include "special.h"
15a5e950 32#include "stdio-util.h"
3ffd4af2 33#include "util.h"
3d20ed6d 34
91077af6
TA
35/* exit codes as defined in fsck(8) */
36enum {
37 FSCK_SUCCESS = 0,
38 FSCK_ERROR_CORRECTED = 1,
39 FSCK_SYSTEM_SHOULD_REBOOT = 2,
40 FSCK_ERRORS_LEFT_UNCORRECTED = 4,
41 FSCK_OPERATIONAL_ERROR = 8,
42 FSCK_USAGE_OR_SYNTAX_ERROR = 16,
43 FSCK_USER_CANCELLED = 32,
44 FSCK_SHARED_LIB_ERROR = 128,
45};
46
3d20ed6d
LP
47static bool arg_skip = false;
48static bool arg_force = false;
96d9117a 49static bool arg_show_progress = false;
f1f0198c 50static const char *arg_repair = "-a";
3d20ed6d 51
48db40b3 52static void start_target(const char *target, const char *mode) {
4afd3348
LP
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;
0c842e0a 55 int r;
3d20ed6d
LP
56
57 assert(target);
58
266f3e26 59 r = bus_connect_system_systemd(&bus);
0c842e0a 60 if (r < 0) {
da927ba9 61 log_error_errno(r, "Failed to get D-Bus connection: %m");
0c842e0a 62 return;
3d20ed6d
LP
63 }
64
0c842e0a 65 log_info("Running request %s/start/replace", target);
3d20ed6d 66
90bb85e1 67 /* Start these units only if we can replace base.target with it */
0c842e0a
TG
68 r = sd_bus_call_method(bus,
69 "org.freedesktop.systemd1",
70 "/org/freedesktop/systemd1",
71 "org.freedesktop.systemd1.Manager",
72 "StartUnitReplace",
73 &error,
74 NULL,
48db40b3 75 "sss", "basic.target", target, mode);
3d20ed6d 76
5220a6f3
LP
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))
24b52437 79 log_error("Failed to start unit: %s", bus_error_message(&error, r));
3d20ed6d
LP
80}
81
96287a49 82static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
24b52437
LP
83 int r;
84
85 assert(key);
059cb385 86
1d84ad94
LP
87 if (streq(key, "fsck.mode")) {
88
89 if (proc_cmdline_value_missing(key, value))
90 return 0;
059cb385
LP
91
92 if (streq(value, "auto"))
93 arg_force = arg_skip = false;
94 else if (streq(value, "force"))
95 arg_force = true;
96 else if (streq(value, "skip"))
97 arg_skip = true;
98 else
85013844
LP
99 log_warning("Invalid fsck.mode= parameter '%s'. Ignoring.", value);
100
1d84ad94
LP
101 } else if (streq(key, "fsck.repair")) {
102
103 if (proc_cmdline_value_missing(key, value))
104 return 0;
f1f0198c
HHPF
105
106 if (streq(value, "preen"))
107 arg_repair = "-a";
24b52437
LP
108 else {
109 r = parse_boolean(value);
110 if (r > 0)
111 arg_repair = "-y";
112 else if (r == 0)
113 arg_repair = "-n";
114 else
115 log_warning("Invalid fsck.repair= parameter '%s'. Ignoring.", value);
116 }
85013844
LP
117 }
118
349cc4a5 119#if HAVE_SYSV_COMPAT
059cb385
LP
120 else if (streq(key, "fastboot") && !value) {
121 log_warning("Please pass 'fsck.mode=skip' rather than 'fastboot' on the kernel command line.");
141a79f4 122 arg_skip = true;
85013844 123
059cb385
LP
124 } else if (streq(key, "forcefsck") && !value) {
125 log_warning("Please pass 'fsck.mode=force' rather than 'forcefsck' on the kernel command line.");
141a79f4 126 arg_force = true;
3d20ed6d 127 }
141a79f4 128#endif
3d20ed6d 129
3d20ed6d
LP
130 return 0;
131}
132
133static void test_files(void) {
85013844 134
349cc4a5 135#if HAVE_SYSV_COMPAT
32f992a5
LP
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.");
3d20ed6d 138 arg_skip = true;
32f992a5 139 }
3d20ed6d 140
32f992a5
LP
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.");
3d20ed6d 143 arg_force = true;
32f992a5
LP
144 }
145#endif
27d340c7 146
96d9117a 147 arg_show_progress = access("/run/systemd/show-status", F_OK) >= 0;
27d340c7
LP
148}
149
96d9117a
LP
150static double percent(int pass, unsigned long cur, unsigned long max) {
151 /* Values stolen from e2fsck */
152
153 static const int pass_table[] = {
154 0, 70, 90, 92, 95, 100
27d340c7
LP
155 };
156
96d9117a
LP
157 if (pass <= 0)
158 return 0.0;
159
160 if ((unsigned) pass >= ELEMENTSOF(pass_table) || max == 0)
161 return 100.0;
162
163 return (double) pass_table[pass-1] +
164 ((double) pass_table[pass] - (double) pass_table[pass-1]) *
165 (double) cur / (double) max;
166}
167
168static int process_progress(int fd) {
169 _cleanup_fclose_ FILE *console = NULL, *f = NULL;
170 usec_t last = 0;
171 bool locked = false;
172 int clear = 0, r;
173
174 /* No progress pipe to process? Then we are a NOP. */
175 if (fd < 0)
176 return 0;
177
178 f = fdopen(fd, "re");
179 if (!f) {
180 safe_close(fd);
181 return -errno;
182 }
183
184 console = fopen("/dev/console", "we");
185 if (!console)
186 return -ENOMEM;
187
188 for (;;) {
189 int pass, m;
190 unsigned long cur, max;
ac6e2f0d 191 _cleanup_free_ char *device = NULL;
96d9117a
LP
192 double p;
193 usec_t t;
27d340c7 194
96d9117a
LP
195 if (fscanf(f, "%i %lu %lu %ms", &pass, &cur, &max, &device) != 4) {
196
197 if (ferror(f))
198 r = log_warning_errno(errno, "Failed to read from progress pipe: %m");
199 else if (feof(f))
200 r = 0;
201 else {
202 log_warning("Failed to parse progress pipe data");
203 r = -EBADMSG;
204 }
27d340c7 205 break;
96d9117a
LP
206 }
207
208 /* Only show one progress counter at max */
209 if (!locked) {
210 if (flock(fileno(console), LOCK_EX|LOCK_NB) < 0)
211 continue;
212
213 locked = true;
214 }
27d340c7 215
27d340c7
LP
216 /* Only update once every 50ms */
217 t = now(CLOCK_MONOTONIC);
e375825d 218 if (last + 50 * USEC_PER_MSEC > t)
27d340c7 219 continue;
27d340c7
LP
220
221 last = t;
222
96d9117a
LP
223 p = percent(pass, cur, max);
224 fprintf(console, "\r%s: fsck %3.1f%% complete...\r%n", device, p, &m);
225 fflush(console);
226
227 if (m > clear)
228 clear = m;
27d340c7
LP
229 }
230
96d9117a
LP
231 if (clear > 0) {
232 unsigned j;
233
234 fputc('\r', console);
235 for (j = 0; j < (unsigned) clear; j++)
236 fputc(' ', console);
237 fputc('\r', console);
238 fflush(console);
239 }
240
241 return r;
242}
243
244static 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",
248 };
249
250 int fd, r;
251
252 fd = socket(AF_UNIX, SOCK_STREAM, 0);
253 if (fd < 0)
254 return log_warning_errno(errno, "socket(): %m");
255
fc2fffe7 256 if (connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) {
3742095b 257 r = log_full_errno(IN_SET(errno, ECONNREFUSED, ENOENT) ? LOG_DEBUG : LOG_WARNING,
96d9117a
LP
258 errno, "Failed to connect to progress socket %s, ignoring: %m", sa.un.sun_path);
259 safe_close(fd);
260 return r;
261 }
262
263 return fd;
3d20ed6d
LP
264}
265
266int main(int argc, char *argv[]) {
96d9117a 267 _cleanup_close_pair_ int progress_pipe[2] = { -1, -1 };
4afd3348 268 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
94192cda 269 const char *device, *type;
dc8e15c2 270 bool root_directory;
94192cda 271 struct stat st;
2e87a1fd 272 int r, exit_status;
96d9117a 273 pid_t pid;
3d20ed6d 274
a9e1f5ec
LP
275 if (argc > 2) {
276 log_error("This program expects one or no arguments.");
3d20ed6d
LP
277 return EXIT_FAILURE;
278 }
279
27d340c7 280 log_set_target(LOG_TARGET_AUTO);
3d20ed6d
LP
281 log_parse_environment();
282 log_open();
283
4c12626c
LP
284 umask(0022);
285
1d84ad94 286 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
e7a3aa3d
LP
287 if (r < 0)
288 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
b5884878 289
3d20ed6d
LP
290 test_files();
291
566690fd
LP
292 if (!arg_force && arg_skip) {
293 r = 0;
294 goto finish;
295 }
a9e1f5ec 296
dc8e15c2 297 if (argc > 1) {
a9e1f5ec 298 device = argv[1];
94192cda
ZJS
299
300 if (stat(device, &st) < 0) {
e7a3aa3d
LP
301 r = log_error_errno(errno, "Failed to stat %s: %m", device);
302 goto finish;
303 }
304
305 if (!S_ISBLK(st.st_mode)) {
306 log_error("%s is not a block device.", device);
307 r = -EINVAL;
566690fd 308 goto finish;
94192cda
ZJS
309 }
310
9102fdc5
TG
311 r = sd_device_new_from_devnum(&dev, 'b', st.st_rdev);
312 if (r < 0) {
313 log_error_errno(r, "Failed to detect device %s: %m", device);
566690fd 314 goto finish;
94192cda 315 }
e7a3aa3d
LP
316
317 root_directory = false;
dc8e15c2 318 } else {
a84f5192 319 struct timespec times[2];
3d20ed6d 320
a9e1f5ec
LP
321 /* Find root device */
322
323 if (stat("/", &st) < 0) {
566690fd
LP
324 r = log_error_errno(errno, "Failed to stat() the root directory: %m");
325 goto finish;
a9e1f5ec
LP
326 }
327
328 /* Virtual root devices don't need an fsck */
566690fd 329 if (major(st.st_dev) == 0) {
e7a3aa3d 330 log_debug("Root directory is virtual or btrfs, skipping check.");
566690fd
LP
331 r = 0;
332 goto finish;
333 }
a9e1f5ec 334
a84f5192
KS
335 /* check if we are already writable */
336 times[0] = st.st_atim;
337 times[1] = st.st_mtim;
e7a3aa3d 338
a84f5192 339 if (utimensat(AT_FDCWD, "/", times, 0) == 0) {
cf1a1055 340 log_info("Root directory is writable, skipping check.");
566690fd
LP
341 r = 0;
342 goto finish;
a84f5192
KS
343 }
344
9102fdc5
TG
345 r = sd_device_new_from_devnum(&dev, 'b', st.st_dev);
346 if (r < 0) {
347 log_error_errno(r, "Failed to detect root device: %m");
566690fd 348 goto finish;
a9e1f5ec
LP
349 }
350
9102fdc5
TG
351 r = sd_device_get_devname(dev, &device);
352 if (r < 0) {
353 log_error_errno(r, "Failed to detect device node of root directory: %m");
566690fd 354 goto finish;
3e33a44a 355 }
dc8e15c2
LP
356
357 root_directory = true;
3d20ed6d
LP
358 }
359
9102fdc5
TG
360 r = sd_device_get_property_value(dev, "ID_FS_TYPE", &type);
361 if (r >= 0) {
eb66db55 362 r = fsck_exists(type);
85eca92e
LP
363 if (r < 0)
364 log_warning_errno(r, "Couldn't detect if fsck.%s may be used for %s, proceeding: %m", type, device);
365 else if (r == 0) {
366 log_info("fsck.%s doesn't exist, not checking file system on %s.", type, device);
566690fd 367 goto finish;
85eca92e 368 }
94192cda
ZJS
369 }
370
96d9117a
LP
371 if (arg_show_progress) {
372 if (pipe(progress_pipe) < 0) {
373 r = log_error_errno(errno, "pipe(): %m");
374 goto finish;
375 }
19e887e7 376 }
27d340c7 377
b6e1fff1
LP
378 r = safe_fork("(fsck)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pid);
379 if (r < 0)
3d20ed6d 380 goto finish;
4c253ed1 381 if (r == 0) {
fbd0b64f 382 char dash_c[STRLEN("-C") + DECIMAL_STR_MAX(int) + 1];
96d9117a
LP
383 int progress_socket = -1;
384 const char *cmdline[9];
385 int i = 0;
386
3d20ed6d 387 /* Child */
96d9117a 388
96d9117a 389 /* Close the reading side of the progress pipe */
1952708a 390 progress_pipe[0] = safe_close(progress_pipe[0]);
96d9117a
LP
391
392 /* Try to connect to a progress management daemon, if there is one */
393 progress_socket = fsck_progress_socket();
394 if (progress_socket >= 0) {
395 /* If this worked we close the progress pipe early, and just use the socket */
396 progress_pipe[1] = safe_close(progress_pipe[1]);
397 xsprintf(dash_c, "-C%i", progress_socket);
398 } else if (progress_pipe[1] >= 0) {
399 /* Otherwise if we have the progress pipe to our own local handle, we use it */
400 xsprintf(dash_c, "-C%i", progress_pipe[1]);
401 } else
402 dash_c[0] = 0;
403
404 cmdline[i++] = "/sbin/fsck";
405 cmdline[i++] = arg_repair;
406 cmdline[i++] = "-T";
407
408 /*
409 * Since util-linux v2.25 fsck uses /run/fsck/<diskname>.lock files.
410 * The previous versions use flock for the device and conflict with
411 * udevd, see https://bugs.freedesktop.org/show_bug.cgi?id=79576#c5
412 */
413 cmdline[i++] = "-l";
414
415 if (!root_directory)
416 cmdline[i++] = "-M";
417
418 if (arg_force)
419 cmdline[i++] = "-f";
420
421 if (!isempty(dash_c))
422 cmdline[i++] = dash_c;
423
424 cmdline[i++] = device;
425 cmdline[i++] = NULL;
426
3d20ed6d 427 execv(cmdline[0], (char**) cmdline);
91077af6 428 _exit(FSCK_OPERATIONAL_ERROR);
3d20ed6d
LP
429 }
430
03e334a1 431 progress_pipe[1] = safe_close(progress_pipe[1]);
96d9117a 432 (void) process_progress(progress_pipe[0]);
576a13ea 433 progress_pipe[0] = -1;
27d340c7 434
2e87a1fd
LP
435 exit_status = wait_for_terminate_and_check("fsck", pid, WAIT_LOG_ABNORMAL);
436 if (exit_status < 0) {
437 r = exit_status;
3d20ed6d
LP
438 goto finish;
439 }
2e87a1fd
LP
440 if (exit_status & ~1) {
441 log_error("fsck failed with exit status %i.", exit_status);
3d20ed6d 442
2e87a1fd 443 if ((exit_status & FSCK_SYSTEM_SHOULD_REBOOT) && root_directory) {
3d20ed6d 444 /* System should be rebooted. */
48db40b3 445 start_target(SPECIAL_REBOOT_TARGET, "replace-irreversibly");
2e87a1fd
LP
446 r = -EINVAL;
447 } else if (exit_status & (FSCK_SYSTEM_SHOULD_REBOOT | FSCK_ERRORS_LEFT_UNCORRECTED)) {
3d20ed6d 448 /* Some other problem */
48db40b3 449 start_target(SPECIAL_EMERGENCY_TARGET, "replace");
2e87a1fd
LP
450 r = -EINVAL;
451 } else {
96d9117a 452 log_warning("Ignoring error.");
566690fd 453 r = 0;
a4c24ff7 454 }
3d20ed6d 455 } else
566690fd 456 r = 0;
3d20ed6d 457
2e87a1fd 458 if (exit_status & FSCK_ERROR_CORRECTED)
e7a3aa3d 459 (void) touch("/run/systemd/quotacheck");
3d20ed6d
LP
460
461finish:
566690fd 462 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
3d20ed6d 463}