systemadm: add a wrappable label and use it for status lines
[systemd_ALT/systemd_imz.git] / src / fsck.c
blobd7d4839f1c9d81f1c3cda869b4c82f7143a28f81
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
3 /***
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
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 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
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>
30 #include <libudev.h>
31 #include <dbus/dbus.h>
33 #include "util.h"
34 #include "dbus-common.h"
35 #include "special.h"
36 #include "bus-errors.h"
38 static bool arg_skip = false;
39 static bool arg_force = false;
40 static bool arg_show_progress = false;
42 static void start_target(const char *target, bool isolate) {
43 DBusMessage *m = NULL, *reply = NULL;
44 DBusError error;
45 const char *mode, *basic_target = "basic.target";
46 DBusConnection *bus = NULL;
48 assert(target);
50 dbus_error_init(&error);
52 if (bus_connect(DBUS_BUS_SYSTEM, &bus, NULL, &error) < 0) {
53 log_error("Failed to get D-Bus connection: %s", bus_error_message(&error));
54 goto finish;
57 if (isolate)
58 mode = "isolate";
59 else
60 mode = "replace";
62 log_info("Running request %s/start/%s", target, mode);
64 if (!(m = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StartUnitReplace"))) {
65 log_error("Could not allocate message.");
66 goto finish;
69 /* Start these units only if we can replace base.target with it */
71 if (!dbus_message_append_args(m,
72 DBUS_TYPE_STRING, &basic_target,
73 DBUS_TYPE_STRING, &target,
74 DBUS_TYPE_STRING, &mode,
75 DBUS_TYPE_INVALID)) {
76 log_error("Could not attach target and flag information to message.");
77 goto finish;
80 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
82 /* Don't print a waring if we aren't called during
83 * startup */
84 if (!dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_JOB))
85 log_error("Failed to start unit: %s", bus_error_message(&error));
87 goto finish;
90 finish:
91 if (m)
92 dbus_message_unref(m);
94 if (reply)
95 dbus_message_unref(reply);
97 if (bus) {
98 dbus_connection_flush(bus);
99 dbus_connection_close(bus);
100 dbus_connection_unref(bus);
103 dbus_error_free(&error);
106 static int parse_proc_cmdline(void) {
107 char *line, *w, *state;
108 int r;
109 size_t l;
111 if (detect_container(NULL) > 0)
112 return 0;
114 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
115 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
116 return 0;
119 FOREACH_WORD_QUOTED(w, l, line, state) {
121 if (strneq(w, "fsck.mode=auto", l))
122 arg_force = arg_skip = false;
123 else if (strneq(w, "fsck.mode=force", l))
124 arg_force = true;
125 else if (strneq(w, "fsck.mode=skip", l))
126 arg_skip = true;
127 else if (startswith(w, "fsck.mode"))
128 log_warning("Invalid fsck.mode= parameter. Ignoring.");
129 #if defined(TARGET_FEDORA) || defined(TARGET_MANDRIVA)
130 else if (strneq(w, "fastboot", l))
131 arg_skip = true;
132 else if (strneq(w, "forcefsck", l))
133 arg_force = true;
134 #endif
137 free(line);
138 return 0;
141 static void test_files(void) {
142 if (access("/fastboot", F_OK) >= 0)
143 arg_skip = true;
145 if (access("/forcefsck", F_OK) >= 0)
146 arg_force = true;
148 if (access("/run/systemd/show-status", F_OK) >= 0 || plymouth_running())
149 arg_show_progress = true;
152 static double percent(int pass, unsigned long cur, unsigned long max) {
153 /* Values stolen from e2fsck */
155 static const int pass_table[] = {
156 0, 70, 90, 92, 95, 100
159 if (pass <= 0)
160 return 0.0;
162 if ((unsigned) pass >= ELEMENTSOF(pass_table) || max == 0)
163 return 100.0;
165 return (double) pass_table[pass-1] +
166 ((double) pass_table[pass] - (double) pass_table[pass-1]) *
167 (double) cur / (double) max;
170 static int process_progress(int fd) {
171 FILE *f, *console;
172 usec_t last = 0;
173 bool locked = false;
174 int clear = 0;
176 f = fdopen(fd, "r");
177 if (!f) {
178 close_nointr_nofail(fd);
179 return -errno;
182 console = fopen("/dev/console", "w");
183 if (!console) {
184 fclose(f);
185 return -ENOMEM;
188 while (!feof(f)) {
189 int pass, m;
190 unsigned long cur, max;
191 char *device;
192 double p;
193 usec_t t;
195 if (fscanf(f, "%i %lu %lu %ms", &pass, &cur, &max, &device) != 4)
196 break;
198 /* Only show one progress counter at max */
199 if (!locked) {
200 if (flock(fileno(console), LOCK_EX|LOCK_NB) < 0) {
201 free(device);
202 continue;
205 locked = true;
208 /* Only update once every 50ms */
209 t = now(CLOCK_MONOTONIC);
210 if (last + 50 * USEC_PER_MSEC > t) {
211 free(device);
212 continue;
215 last = t;
217 p = percent(pass, cur, max);
218 fprintf(console, "\r%s: fsck %3.1f%% complete...\r%n", device, p, &m);
219 fflush(console);
221 free(device);
223 if (m > clear)
224 clear = m;
227 if (clear > 0) {
228 unsigned j;
230 fputc('\r', console);
231 for (j = 0; j < (unsigned) clear; j++)
232 fputc(' ', console);
233 fputc('\r', console);
234 fflush(console);
237 fclose(f);
238 fclose(console);
239 return 0;
242 int main(int argc, char *argv[]) {
243 const char *cmdline[9];
244 int i = 0, r = EXIT_FAILURE, q;
245 pid_t pid;
246 siginfo_t status;
247 struct udev *udev = NULL;
248 struct udev_device *udev_device = NULL;
249 const char *device;
250 bool root_directory;
251 int progress_pipe[2] = { -1, -1 };
252 char dash_c[2+10+1];
254 if (argc > 2) {
255 log_error("This program expects one or no arguments.");
256 return EXIT_FAILURE;
259 log_set_target(LOG_TARGET_AUTO);
260 log_parse_environment();
261 log_open();
263 umask(0022);
265 parse_proc_cmdline();
266 test_files();
268 if (!arg_force && arg_skip)
269 return 0;
271 if (argc > 1) {
272 device = argv[1];
273 root_directory = false;
274 } else {
275 struct stat st;
276 struct timespec times[2];
278 /* Find root device */
280 if (stat("/", &st) < 0) {
281 log_error("Failed to stat() the root directory: %m");
282 goto finish;
285 /* Virtual root devices don't need an fsck */
286 if (major(st.st_dev) == 0)
287 return 0;
289 /* check if we are already writable */
290 times[0] = st.st_atim;
291 times[1] = st.st_mtim;
292 if (utimensat(AT_FDCWD, "/", times, 0) == 0) {
293 log_info("Root directory is writable, skipping check.");
294 return 0;
297 if (!(udev = udev_new())) {
298 log_error("Out of memory");
299 goto finish;
302 if (!(udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev))) {
303 log_error("Failed to detect root device.");
304 goto finish;
307 if (!(device = udev_device_get_devnode(udev_device))) {
308 log_error("Failed to detect device node of root directory.");
309 goto finish;
312 root_directory = true;
315 if (arg_show_progress)
316 if (pipe(progress_pipe) < 0) {
317 log_error("pipe(): %m");
318 goto finish;
321 cmdline[i++] = "/sbin/fsck";
322 cmdline[i++] = "-a";
323 cmdline[i++] = "-T";
324 cmdline[i++] = "-l";
326 if (!root_directory)
327 cmdline[i++] = "-M";
329 if (arg_force)
330 cmdline[i++] = "-f";
332 if (progress_pipe[1] >= 0) {
333 snprintf(dash_c, sizeof(dash_c), "-C%i", progress_pipe[1]);
334 char_array_0(dash_c);
335 cmdline[i++] = dash_c;
338 cmdline[i++] = device;
339 cmdline[i++] = NULL;
341 pid = fork();
342 if (pid < 0) {
343 log_error("fork(): %m");
344 goto finish;
345 } else if (pid == 0) {
346 /* Child */
347 if (progress_pipe[0] >= 0)
348 close_nointr_nofail(progress_pipe[0]);
349 execv(cmdline[0], (char**) cmdline);
350 _exit(8); /* Operational error */
353 if (progress_pipe[1] >= 0) {
354 close_nointr_nofail(progress_pipe[1]);
355 progress_pipe[1] = -1;
358 if (progress_pipe[0] >= 0) {
359 process_progress(progress_pipe[0]);
360 progress_pipe[0] = -1;
363 q = wait_for_terminate(pid, &status);
364 if (q < 0) {
365 log_error("waitid(): %s", strerror(-q));
366 goto finish;
369 if (status.si_code != CLD_EXITED || (status.si_status & ~1)) {
371 if (status.si_code == CLD_KILLED || status.si_code == CLD_DUMPED)
372 log_error("fsck terminated by signal %s.", signal_to_string(status.si_status));
373 else if (status.si_code == CLD_EXITED)
374 log_error("fsck failed with error code %i.", status.si_status);
375 else
376 log_error("fsck failed due to unknown reason.");
378 if (status.si_code == CLD_EXITED && (status.si_status & 2) && root_directory)
379 /* System should be rebooted. */
380 start_target(SPECIAL_REBOOT_TARGET, false);
381 else if (status.si_code == CLD_EXITED && (status.si_status & 6))
382 /* Some other problem */
383 start_target(SPECIAL_EMERGENCY_TARGET, true);
384 else {
385 r = EXIT_SUCCESS;
386 log_warning("Ignoring error.");
389 } else
390 r = EXIT_SUCCESS;
392 if (status.si_code == CLD_EXITED && (status.si_status & 1))
393 touch("/run/systemd/quotacheck");
395 finish:
396 if (udev_device)
397 udev_device_unref(udev_device);
399 if (udev)
400 udev_unref(udev);
402 close_pipe(progress_pipe);
404 return r;