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