chiark / gitweb /
702f22c95defa5d37b21833e2f779aad002033f5
[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 <dbus/dbus.h>
29
30 #include "util.h"
31 #include "ac-power.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;
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", "StartUnit"))) {
61                 log_error("Could not allocate message.");
62                 goto finish;
63         }
64
65         if (!dbus_message_append_args(m,
66                                       DBUS_TYPE_STRING, &target,
67                                       DBUS_TYPE_STRING, &mode,
68                                       DBUS_TYPE_INVALID)) {
69                 log_error("Could not attach target and flag information to message.");
70                 goto finish;
71         }
72
73         if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
74                 log_error("Failed to start unit: %s", bus_error_message(&error));
75                 goto finish;
76         }
77
78 finish:
79         if (m)
80                 dbus_message_unref(m);
81
82         if (reply)
83                 dbus_message_unref(reply);
84
85         if (bus) {
86                 dbus_connection_flush(bus);
87                 dbus_connection_close(bus);
88                 dbus_connection_unref(bus);
89         }
90
91         dbus_error_free(&error);
92 }
93
94 static int parse_proc_cmdline(void) {
95         char *line, *w, *state;
96         int r;
97         size_t l;
98
99         if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
100                 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
101                 return 0;
102         }
103
104         FOREACH_WORD_QUOTED(w, l, line, state) {
105
106                 if (strneq(w, "fsck.mode=auto", l))
107                         arg_force = arg_skip = false;
108                 else if (strneq(w, "fsck.mode=force", l))
109                         arg_force = true;
110                 else if (strneq(w, "fsck.mode=skip", l))
111                         arg_skip = true;
112                 else if (startswith(w, "fsck.mode"))
113                         log_warning("Invalid fsck.mode= parameter. Ignoring.");
114 #ifdef TARGET_FEDORA
115                 else if (strneq(w, "fastboot", l))
116                         arg_skip = true;
117                 else if (strneq(w, "forcefsck", l))
118                         arg_force = true;
119 #endif
120         }
121
122         free(line);
123         return 0;
124 }
125
126 static void test_files(void) {
127         if (access("/fastboot", F_OK) >= 0)
128                 arg_skip = true;
129
130         if (access("/forcefsck", F_OK) >= 0)
131                 arg_force = true;
132 }
133
134 int main(int argc, char *argv[]) {
135         static const char * cmdline[7];
136         int i = 0, r = EXIT_FAILURE, q;
137         pid_t pid;
138         siginfo_t status;
139
140         if (argc != 2) {
141                 log_error("This program expects exactly one argument.");
142                 return EXIT_FAILURE;
143         }
144
145         log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
146         log_parse_environment();
147         log_open();
148
149         parse_proc_cmdline();
150         test_files();
151
152         if (!arg_force) {
153                 if (arg_skip)
154                         return 0;
155
156                 /* FIXME: only execute necessary fsck's if no AC power present */
157                 if (on_ac_power() == 0) {
158                         log_info("Running on battery power, skipping file system check.");
159                         return 0;
160                 }
161         }
162
163         cmdline[i++] = "/sbin/fsck";
164         cmdline[i++] = "-a";
165         cmdline[i++] = "-T";
166         cmdline[i++] = "-C";
167
168         if (arg_force)
169                 cmdline[i++] = "-f";
170
171         cmdline[i++] = argv[1];
172         cmdline[i++] = NULL;
173
174         if ((pid = fork()) < 0) {
175                 log_error("fork(): %m");
176                 goto finish;
177         } else if (pid == 0) {
178                 /* Child */
179                 execv(cmdline[0], (char**) cmdline);
180                 _exit(8); /* Operational error */
181         }
182
183         if ((q = wait_for_terminate(pid, &status)) < 0) {
184                 log_error("waitid(): %s", strerror(-q));
185                 goto finish;
186         }
187
188         if (status.si_code == CLD_KILLED ||
189             status.si_code == CLD_DUMPED) {
190                 log_error("fsck terminated by signal %s.", signal_to_string(status.si_status));
191                 goto finish;
192
193         } else if (status.si_code != CLD_EXITED) {
194                 log_error("fsck failed due to unknown reason.");
195                 goto finish;
196         }
197
198         if (status.si_status & ~1) {
199
200                 if (access("/dev/.systemd/late-fsck", F_OK) >= 0) {
201                         log_error("fsck failed with error code %i.", status.si_status);
202                         goto finish;
203                 }
204
205                 if (status.si_status & 2)
206                         /* System should be rebooted. */
207                         start_target(SPECIAL_REBOOT_TARGET, false);
208                 else
209                         /* Some other problem */
210                         start_target(SPECIAL_EMERGENCY_TARGET, true);
211
212         } else
213                 r = EXIT_SUCCESS;
214
215         if (status.si_status & 1)
216                 touch("/dev/.systemd/quotacheck");
217
218 finish:
219         return r;
220 }