]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/fsck/fsck.c
general: various cleanups
[thirdparty/systemd.git] / src / fsck / fsck.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <stdio.h>
23 #include <stdbool.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <sys/file.h>
29
30 #include "sd-bus.h"
31 #include "libudev.h"
32
33 #include "util.h"
34 #include "special.h"
35 #include "bus-util.h"
36 #include "bus-error.h"
37 #include "bus-errors.h"
38 #include "virt.h"
39 #include "fileio.h"
40 #include "udev-util.h"
41
42 static bool arg_skip = false;
43 static bool arg_force = false;
44 static bool arg_show_progress = false;
45
46 static void start_target(const char *target) {
47 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
48 _cleanup_bus_unref_ sd_bus *bus = NULL;
49 int r;
50
51 assert(target);
52
53 r = bus_open_system_systemd(&bus);
54 if (r < 0) {
55 log_error("Failed to get D-Bus connection: %s", strerror(-r));
56 return;
57 }
58
59 log_info("Running request %s/start/replace", target);
60
61 /* Start these units only if we can replace base.target with it */
62 r = sd_bus_call_method(bus,
63 "org.freedesktop.systemd1",
64 "/org/freedesktop/systemd1",
65 "org.freedesktop.systemd1.Manager",
66 "StartUnitReplace",
67 &error,
68 NULL,
69 "sss", "basic.target", target, "replace");
70
71 /* Don't print a warning if we aren't called during startup */
72 if (r < 0 && !sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_JOB))
73 log_error("Failed to start unit: %s", bus_error_message(&error, -r));
74 }
75
76 static int parse_proc_cmdline(void) {
77 char *line, *w, *state;
78 int r;
79 size_t l;
80
81 if (detect_container(NULL) > 0)
82 return 0;
83
84 r = read_one_line_file("/proc/cmdline", &line);
85 if (r < 0) {
86 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
87 return 0;
88 }
89
90 FOREACH_WORD_QUOTED(w, l, line, state) {
91
92 if (strneq(w, "fsck.mode=auto", l))
93 arg_force = arg_skip = false;
94 else if (strneq(w, "fsck.mode=force", l))
95 arg_force = true;
96 else if (strneq(w, "fsck.mode=skip", l))
97 arg_skip = true;
98 else if (startswith(w, "fsck"))
99 log_warning("Invalid fsck parameter. Ignoring.");
100 #ifdef HAVE_SYSV_COMPAT
101 else if (strneq(w, "fastboot", l)) {
102 log_error("Please pass 'fsck.mode=skip' rather than 'fastboot' on the kernel command line.");
103 arg_skip = true;
104 } else if (strneq(w, "forcefsck", l)) {
105 log_error("Please pass 'fsck.mode=force' rather than 'forcefsck' on the kernel command line.");
106 arg_force = true;
107 }
108 #endif
109 }
110
111 free(line);
112 return 0;
113 }
114
115 static void test_files(void) {
116 #ifdef HAVE_SYSV_COMPAT
117 if (access("/fastboot", F_OK) >= 0) {
118 log_error("Please pass 'fsck.mode=skip' on the kernel command line rather than creating /fastboot on the root file system.");
119 arg_skip = true;
120 }
121
122 if (access("/forcefsck", F_OK) >= 0) {
123 log_error("Please pass 'fsck.mode=force' on the kernel command line rather than creating /forcefsck on the root file system.");
124 arg_force = true;
125 }
126 #endif
127
128 if (access("/run/systemd/show-status", F_OK) >= 0 || plymouth_running())
129 arg_show_progress = true;
130 }
131
132 static double percent(int pass, unsigned long cur, unsigned long max) {
133 /* Values stolen from e2fsck */
134
135 static const int pass_table[] = {
136 0, 70, 90, 92, 95, 100
137 };
138
139 if (pass <= 0)
140 return 0.0;
141
142 if ((unsigned) pass >= ELEMENTSOF(pass_table) || max == 0)
143 return 100.0;
144
145 return (double) pass_table[pass-1] +
146 ((double) pass_table[pass] - (double) pass_table[pass-1]) *
147 (double) cur / (double) max;
148 }
149
150 static int process_progress(int fd) {
151 FILE *f, *console;
152 usec_t last = 0;
153 bool locked = false;
154 int clear = 0;
155
156 f = fdopen(fd, "r");
157 if (!f) {
158 close_nointr_nofail(fd);
159 return -errno;
160 }
161
162 console = fopen("/dev/console", "w");
163 if (!console) {
164 fclose(f);
165 return -ENOMEM;
166 }
167
168 while (!feof(f)) {
169 int pass, m;
170 unsigned long cur, max;
171 char *device;
172 double p;
173 usec_t t;
174
175 if (fscanf(f, "%i %lu %lu %ms", &pass, &cur, &max, &device) != 4)
176 break;
177
178 /* Only show one progress counter at max */
179 if (!locked) {
180 if (flock(fileno(console), LOCK_EX|LOCK_NB) < 0) {
181 free(device);
182 continue;
183 }
184
185 locked = true;
186 }
187
188 /* Only update once every 50ms */
189 t = now(CLOCK_MONOTONIC);
190 if (last + 50 * USEC_PER_MSEC > t) {
191 free(device);
192 continue;
193 }
194
195 last = t;
196
197 p = percent(pass, cur, max);
198 fprintf(console, "\r%s: fsck %3.1f%% complete...\r%n", device, p, &m);
199 fflush(console);
200
201 free(device);
202
203 if (m > clear)
204 clear = m;
205 }
206
207 if (clear > 0) {
208 unsigned j;
209
210 fputc('\r', console);
211 for (j = 0; j < (unsigned) clear; j++)
212 fputc(' ', console);
213 fputc('\r', console);
214 fflush(console);
215 }
216
217 fclose(f);
218 fclose(console);
219 return 0;
220 }
221
222 int main(int argc, char *argv[]) {
223 const char *cmdline[9];
224 int i = 0, r = EXIT_FAILURE, q;
225 pid_t pid;
226 siginfo_t status;
227 _cleanup_udev_unref_ struct udev *udev = NULL;
228 _cleanup_udev_device_unref_ struct udev_device *udev_device = NULL;
229 const char *device;
230 bool root_directory;
231 int progress_pipe[2] = { -1, -1 };
232 char dash_c[2+10+1];
233
234 if (argc > 2) {
235 log_error("This program expects one or no arguments.");
236 return EXIT_FAILURE;
237 }
238
239 log_set_target(LOG_TARGET_AUTO);
240 log_parse_environment();
241 log_open();
242
243 umask(0022);
244
245 parse_proc_cmdline();
246 test_files();
247
248 if (!arg_force && arg_skip)
249 return 0;
250
251 if (argc > 1) {
252 device = argv[1];
253 root_directory = false;
254 } else {
255 struct stat st;
256 struct timespec times[2];
257
258 /* Find root device */
259
260 if (stat("/", &st) < 0) {
261 log_error("Failed to stat() the root directory: %m");
262 goto finish;
263 }
264
265 /* Virtual root devices don't need an fsck */
266 if (major(st.st_dev) == 0)
267 return 0;
268
269 /* check if we are already writable */
270 times[0] = st.st_atim;
271 times[1] = st.st_mtim;
272 if (utimensat(AT_FDCWD, "/", times, 0) == 0) {
273 log_info("Root directory is writable, skipping check.");
274 return 0;
275 }
276
277 if (!(udev = udev_new())) {
278 log_oom();
279 goto finish;
280 }
281
282 if (!(udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev))) {
283 log_error("Failed to detect root device.");
284 goto finish;
285 }
286
287 if (!(device = udev_device_get_devnode(udev_device))) {
288 log_error("Failed to detect device node of root directory.");
289 goto finish;
290 }
291
292 root_directory = true;
293 }
294
295 if (arg_show_progress)
296 if (pipe(progress_pipe) < 0) {
297 log_error("pipe(): %m");
298 goto finish;
299 }
300
301 cmdline[i++] = "/sbin/fsck";
302 cmdline[i++] = "-a";
303 cmdline[i++] = "-T";
304 cmdline[i++] = "-l";
305
306 if (!root_directory)
307 cmdline[i++] = "-M";
308
309 if (arg_force)
310 cmdline[i++] = "-f";
311
312 if (progress_pipe[1] >= 0) {
313 snprintf(dash_c, sizeof(dash_c), "-C%i", progress_pipe[1]);
314 char_array_0(dash_c);
315 cmdline[i++] = dash_c;
316 }
317
318 cmdline[i++] = device;
319 cmdline[i++] = NULL;
320
321 pid = fork();
322 if (pid < 0) {
323 log_error("fork(): %m");
324 goto finish;
325 } else if (pid == 0) {
326 /* Child */
327 if (progress_pipe[0] >= 0)
328 close_nointr_nofail(progress_pipe[0]);
329 execv(cmdline[0], (char**) cmdline);
330 _exit(8); /* Operational error */
331 }
332
333 if (progress_pipe[1] >= 0) {
334 close_nointr_nofail(progress_pipe[1]);
335 progress_pipe[1] = -1;
336 }
337
338 if (progress_pipe[0] >= 0) {
339 process_progress(progress_pipe[0]);
340 progress_pipe[0] = -1;
341 }
342
343 q = wait_for_terminate(pid, &status);
344 if (q < 0) {
345 log_error("waitid(): %s", strerror(-q));
346 goto finish;
347 }
348
349 if (status.si_code != CLD_EXITED || (status.si_status & ~1)) {
350
351 if (status.si_code == CLD_KILLED || status.si_code == CLD_DUMPED)
352 log_error("fsck terminated by signal %s.", signal_to_string(status.si_status));
353 else if (status.si_code == CLD_EXITED)
354 log_error("fsck failed with error code %i.", status.si_status);
355 else
356 log_error("fsck failed due to unknown reason.");
357
358 if (status.si_code == CLD_EXITED && (status.si_status & 2) && root_directory)
359 /* System should be rebooted. */
360 start_target(SPECIAL_REBOOT_TARGET);
361 else if (status.si_code == CLD_EXITED && (status.si_status & 6))
362 /* Some other problem */
363 start_target(SPECIAL_EMERGENCY_TARGET);
364 else {
365 r = EXIT_SUCCESS;
366 log_warning("Ignoring error.");
367 }
368
369 } else
370 r = EXIT_SUCCESS;
371
372 if (status.si_code == CLD_EXITED && (status.si_status & 1))
373 touch("/run/systemd/quotacheck");
374
375 finish:
376 close_pipe(progress_pipe);
377
378 return r;
379 }