chiark / gitweb /
fsck: make fsck idempotent
[elogind.git] / src / 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 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.
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   General Public License for more details.
17
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 ***/
21
22 #include <stdio.h>
23 #include <stdbool.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <unistd.h>
27
28 #include <libudev.h>
29 #include <dbus/dbus.h>
30
31 #include "util.h"
32 #include "dbus-common.h"
33 #include "special.h"
34
35 static bool arg_skip = false;
36 static bool arg_force = false;
37
38 static void start_target(const char *target, bool isolate) {
39         DBusMessage *m = NULL, *reply = NULL;
40         DBusError error;
41         const char *mode, *base_target = "base.target";
42         DBusConnection *bus = NULL;
43
44         assert(target);
45
46         dbus_error_init(&error);
47
48         if (bus_connect(DBUS_BUS_SYSTEM, &bus, NULL, &error) < 0) {
49                 log_error("Failed to get D-Bus connection: %s", bus_error_message(&error));
50                 goto finish;
51         }
52
53         if (isolate)
54                 mode = "isolate";
55         else
56                 mode = "replace";
57
58         log_debug("Running request %s/start/%s", target, mode);
59
60         if (!(m = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StartUnitReplace"))) {
61                 log_error("Could not allocate message.");
62                 goto finish;
63         }
64
65         /* Start these units only if we can replace base.target with it */
66
67         if (!dbus_message_append_args(m,
68                                       DBUS_TYPE_STRING, &base_target,
69                                       DBUS_TYPE_STRING, &target,
70                                       DBUS_TYPE_STRING, &mode,
71                                       DBUS_TYPE_INVALID)) {
72                 log_error("Could not attach target and flag information to message.");
73                 goto finish;
74         }
75
76         if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
77                 log_error("Failed to start unit: %s", bus_error_message(&error));
78                 goto finish;
79         }
80
81 finish:
82         if (m)
83                 dbus_message_unref(m);
84
85         if (reply)
86                 dbus_message_unref(reply);
87
88         if (bus) {
89                 dbus_connection_flush(bus);
90                 dbus_connection_close(bus);
91                 dbus_connection_unref(bus);
92         }
93
94         dbus_error_free(&error);
95 }
96
97 static int parse_proc_cmdline(void) {
98         char *line, *w, *state;
99         int r;
100         size_t l;
101
102         if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
103                 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
104                 return 0;
105         }
106
107         FOREACH_WORD_QUOTED(w, l, line, state) {
108
109                 if (strneq(w, "fsck.mode=auto", l))
110                         arg_force = arg_skip = false;
111                 else if (strneq(w, "fsck.mode=force", l))
112                         arg_force = true;
113                 else if (strneq(w, "fsck.mode=skip", l))
114                         arg_skip = true;
115                 else if (startswith(w, "fsck.mode"))
116                         log_warning("Invalid fsck.mode= parameter. Ignoring.");
117 #ifdef TARGET_FEDORA
118                 else if (strneq(w, "fastboot", l))
119                         arg_skip = true;
120                 else if (strneq(w, "forcefsck", l))
121                         arg_force = true;
122 #endif
123         }
124
125         free(line);
126         return 0;
127 }
128
129 static void test_files(void) {
130         if (access("/fastboot", F_OK) >= 0)
131                 arg_skip = true;
132
133         if (access("/forcefsck", F_OK) >= 0)
134                 arg_force = true;
135 }
136
137 int main(int argc, char *argv[]) {
138         const char *cmdline[8];
139         int i = 0, r = EXIT_FAILURE, q;
140         pid_t pid;
141         siginfo_t status;
142         struct udev *udev = NULL;
143         struct udev_device *udev_device = NULL;
144         const char *device;
145
146         if (argc > 2) {
147                 log_error("This program expects one or no arguments.");
148                 return EXIT_FAILURE;
149         }
150
151         log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
152         log_parse_environment();
153         log_open();
154
155         parse_proc_cmdline();
156         test_files();
157
158         if (!arg_force && arg_skip)
159                 return 0;
160
161         if (argc > 1)
162                 device = argv[1];
163         else {
164                 struct stat st;
165
166                 /* Find root device */
167
168                 if (stat("/", &st) < 0) {
169                         log_error("Failed to stat() the root directory: %m");
170                         goto finish;
171                 }
172
173                 /* Virtual root devices don't need an fsck */
174                 if (major(st.st_dev) == 0)
175                         return 0;
176
177                 if (!(udev = udev_new())) {
178                         log_error("Out of memory");
179                         goto finish;
180                 }
181
182                 if (!(udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev))) {
183                         log_error("Failed to detect root device.");
184                         goto finish;
185                 }
186
187                 if (!(device = udev_device_get_devnode(udev_device))) {
188                         log_error("Failed to detect device node of root directory.");
189                         goto finish;
190                 }
191         }
192
193         cmdline[i++] = "/sbin/fsck";
194         cmdline[i++] = "-a";
195         cmdline[i++] = "-T";
196         cmdline[i++] = "-C";
197         cmdline[i++] = "-M";
198
199         if (arg_force)
200                 cmdline[i++] = "-f";
201
202         cmdline[i++] = device;
203         cmdline[i++] = NULL;
204
205         if ((pid = fork()) < 0) {
206                 log_error("fork(): %m");
207                 goto finish;
208         } else if (pid == 0) {
209                 /* Child */
210                 execv(cmdline[0], (char**) cmdline);
211                 _exit(8); /* Operational error */
212         }
213
214         if ((q = wait_for_terminate(pid, &status)) < 0) {
215                 log_error("waitid(): %s", strerror(-q));
216                 goto finish;
217         }
218
219         if (status.si_code != CLD_EXITED || (status.si_status & ~1)) {
220
221                 if (status.si_code == CLD_KILLED || status.si_code == CLD_DUMPED)
222                         log_error("fsck terminated by signal %s.", signal_to_string(status.si_status));
223                 else if (status.si_code == CLD_EXITED)
224                         log_error("fsck failed with error code %i.", status.si_status);
225                 else
226                         log_error("fsck failed due to unknown reason.");
227
228                 if (status.si_code == CLD_EXITED && status.si_status & 2)
229                         /* System should be rebooted. */
230                         start_target(SPECIAL_REBOOT_TARGET, false);
231                 else
232                         /* Some other problem */
233                         start_target(SPECIAL_EMERGENCY_TARGET, true);
234
235         } else
236                 r = EXIT_SUCCESS;
237
238         if (status.si_code == CLD_EXITED && (status.si_status & 1))
239                 touch("/dev/.systemd/quotacheck");
240
241 finish:
242         if (udev_device)
243                 udev_device_unref(udev_device);
244
245         if (udev)
246                 udev_unref(udev);
247
248         return r;
249 }