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