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