1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2015 Canonical
9 Didier Roche <didrocks@ubuntu.com>
11 systemd is free software; you can redistribute it and/or modify it
12 under the terms of the GNU Lesser General Public License as published by
13 the Free Software Foundation; either version 2.1 of the License, or
14 (at your option) any later version.
16 systemd is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public License
22 along with systemd; If not, see <http://www.gnu.org/licenses/>.
32 #include <sys/socket.h>
33 #include <sys/types.h>
37 #include "sd-daemon.h"
40 #include "event-util.h"
44 #include "socket-util.h"
48 #define IDLE_TIME_SECONDS 30
49 #define PLYMOUTH_REQUEST_KEY "K\2\2\3"
50 #define CLIENTS_MAX 128
54 typedef struct Client {
55 struct Manager *manager;
68 sd_event_source *event_source;
70 LIST_FIELDS(struct Client, clients);
73 typedef struct Manager {
76 LIST_HEAD(Client, clients);
82 sd_event_source *connection_event_source;
84 bool show_status_console;
90 sd_event_source *plymouth_event_source;
91 bool plymouth_cancel_sent;
93 bool cancel_requested;
96 static void client_free(Client *c);
97 static void manager_free(Manager *m);
99 DEFINE_TRIVIAL_CLEANUP_FUNC(Client*, client_free);
100 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
102 static int manager_write_console(Manager *m, const char *message) {
103 _cleanup_fclose_ FILE *console = NULL;
109 if (!m->show_status_console)
112 /* Reduce the SAK window by opening and closing console on every request */
113 console = fopen("/dev/console", "we");
118 fprintf(console, "\r%s\r%n", message, &l);
119 if (m->clear < (size_t)l)
120 m->clear = (size_t)l;
122 fputc('\r', console);
123 for (j = 0; j < m->clear; j++)
125 fputc('\r', console);
132 static double compute_percent(int pass, size_t cur, size_t max) {
133 /* Values stolen from e2fsck */
135 static const double pass_table[] = {
136 0, 70, 90, 92, 95, 100
142 if ((unsigned) pass >= ELEMENTSOF(pass_table) || max == 0)
145 return pass_table[pass-1] +
146 (pass_table[pass] - pass_table[pass-1]) *
150 static int client_request_cancel(Client *c) {
151 FsckdMessage cancel_msg = {
162 n = send(c->fd, &cancel_msg, sizeof(FsckdMessage), 0);
164 return log_warning_errno(errno, "Cannot send cancel to fsck on (%u:%u): %m", major(c->devnum), minor(c->devnum));
165 if ((size_t) n < sizeof(FsckdMessage)) {
166 log_warning("Short send when sending cancel to fsck on (%u:%u).", major(c->devnum), minor(c->devnum));
174 static void client_free(Client *c) {
178 LIST_REMOVE(clients, c->manager->clients, c);
179 c->manager->n_clients--;
182 sd_event_source_unref(c->event_source);
188 static void manager_disconnect_plymouth(Manager *m) {
191 m->plymouth_event_source = sd_event_source_unref(m->plymouth_event_source);
192 m->plymouth_fd = safe_close(m->plymouth_fd);
193 m->plymouth_cancel_sent = false;
196 static int manager_plymouth_feedback_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
197 Manager *m = userdata;
204 l = read(m->plymouth_fd, buffer, sizeof(buffer));
206 log_warning_errno(errno, "Got error while reading from plymouth: %m");
207 manager_disconnect_plymouth(m);
211 manager_disconnect_plymouth(m);
215 if (l > 1 && buffer[0] == '\15')
216 log_error("Message update to plymouth wasn't delivered successfully");
218 /* the only answer support type we requested is a key interruption */
219 if (l > 2 && buffer[0] == '\2' && buffer[5] == '\3') {
220 m->cancel_requested = true;
222 /* cancel all connected clients */
223 LIST_FOREACH(clients, current, m->clients)
224 client_request_cancel(current);
230 static int manager_connect_plymouth(Manager *m) {
231 union sockaddr_union sa = PLYMOUTH_SOCKET;
234 /* try to connect or reconnect if sending a message */
235 if (m->plymouth_fd >= 0)
238 m->plymouth_fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
239 if (m->plymouth_fd < 0)
240 return log_warning_errno(errno, "Connection to plymouth socket failed: %m");
242 if (connect(m->plymouth_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) {
243 r = log_warning_errno(errno, "Couldn't connect to plymouth: %m");
247 r = sd_event_add_io(m->event, &m->plymouth_event_source, m->plymouth_fd, EPOLLIN, manager_plymouth_feedback_handler, m);
249 log_warning_errno(r, "Can't listen to plymouth socket: %m");
256 manager_disconnect_plymouth(m);
260 static int plymouth_send_message(int plymouth_fd, const char *message, bool update) {
261 _cleanup_free_ char *packet = NULL;
268 if (asprintf(&packet, "%c\002%c%s%n", mode, (int) (strlen(message) + 1), message, &n) < 0)
271 return loop_write(plymouth_fd, packet, n + 1, true);
274 static int manager_send_plymouth_message(Manager *m, const char *message) {
275 const char *plymouth_cancel_message = NULL, *l10n_cancel_message = NULL;
278 r = manager_connect_plymouth(m);
282 if (!m->plymouth_cancel_sent) {
284 /* Indicate to plymouth that we listen to Ctrl+C */
285 r = loop_write(m->plymouth_fd, PLYMOUTH_REQUEST_KEY, sizeof(PLYMOUTH_REQUEST_KEY), true);
287 return log_warning_errno(r, "Can't send to plymouth cancel key: %m");
289 m->plymouth_cancel_sent = true;
291 l10n_cancel_message = _("Press Ctrl+C to cancel all filesystem checks in progress");
292 plymouth_cancel_message = strjoina("fsckd-cancel-msg:", l10n_cancel_message);
294 r = plymouth_send_message(m->plymouth_fd, plymouth_cancel_message, false);
296 log_warning_errno(r, "Can't send filesystem cancel message to plymouth: %m");
298 } else if (m->numdevices == 0) {
300 m->plymouth_cancel_sent = false;
302 r = plymouth_send_message(m->plymouth_fd, "", false);
304 log_warning_errno(r, "Can't clear plymouth filesystem cancel message: %m");
307 r = plymouth_send_message(m->plymouth_fd, message, true);
309 return log_warning_errno(r, "Couldn't send \"%s\" to plymouth: %m", message);
314 static int manager_update_global_progress(Manager *m) {
315 Client *current = NULL;
316 _cleanup_free_ char *console_message = NULL;
317 _cleanup_free_ char *fsck_message = NULL;
318 int current_numdevices = 0, r;
319 double current_percent = 100;
321 /* get the overall percentage */
322 LIST_FOREACH(clients, current, m->clients) {
323 current_numdevices++;
325 /* right now, we only keep the minimum % of all fsckd processes. We could in the future trying to be
326 linear, but max changes and corresponds to the pass. We have all the informations into fsckd
327 already if we can treat that in a smarter way. */
328 current_percent = MIN(current_percent, current->percent);
331 /* update if there is anything user-visible to update */
332 if (fabs(current_percent - m->percent) > 0.001 || current_numdevices != m->numdevices) {
333 m->numdevices = current_numdevices;
334 m->percent = current_percent;
336 if (asprintf(&console_message,
337 ngettext("Checking in progress on %d disk (%3.1f%% complete)",
338 "Checking in progress on %d disks (%3.1f%% complete)", m->numdevices),
339 m->numdevices, m->percent) < 0)
342 if (asprintf(&fsck_message, "fsckd:%d:%3.1f:%s", m->numdevices, m->percent, console_message) < 0)
345 r = manager_write_console(m, console_message);
349 /* try to connect to plymouth and send message */
350 r = manager_send_plymouth_message(m, fsck_message);
357 static int client_progress_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
358 Client *client = userdata;
359 FsckProgress fsck_data;
368 /* check first if we need to cancel this client */
369 if (m->cancel_requested)
370 client_request_cancel(client);
372 /* ensure we have enough data to read */
373 r = ioctl(fd, FIONREAD, &buflen);
374 if (r == 0 && buflen != 0 && (size_t) buflen < sizeof(FsckProgress)) {
375 if (client->buflen != buflen)
376 client->buflen = buflen;
377 /* we got twice the same size from a bad behaving client, kick it off the list */
379 log_warning("Closing bad behaving fsck client connection at fd %d", client->fd);
381 manager_update_global_progress(m);
386 /* read actual data */
387 r = recv(fd, &fsck_data, sizeof(FsckProgress), 0);
389 log_debug("Fsck client connected to fd %d disconnected", client->fd);
391 } else if (r > 0 && r != sizeof(FsckProgress))
392 log_warning("Unexpected data structure sent to fsckd socket from fd: %d. Ignoring", client->fd);
393 else if (r > 0 && r == sizeof(FsckProgress)) {
394 client->devnum = fsck_data.devnum;
395 client->cur = fsck_data.cur;
396 client->max = fsck_data.max;
397 client->pass = fsck_data.pass;
398 client->percent = compute_percent(client->pass, client->cur, client->max);
399 log_debug("Getting progress for %u:%u (%lu, %lu, %d) : %3.1f%%",
400 major(client->devnum), minor(client->devnum),
401 client->cur, client->max, client->pass, client->percent);
403 log_error_errno(r, "Unknown error while trying to read fsck data: %m");
405 manager_update_global_progress(m);
410 static int manager_new_connection_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
411 _cleanup_(client_freep) Client *c = NULL;
412 _cleanup_close_ int new_client_fd = -1;
413 Manager *m = userdata;
418 /* Initialize and list new clients */
419 new_client_fd = accept4(m->connection_fd, NULL, NULL, SOCK_CLOEXEC);
420 if (new_client_fd < 0)
421 return log_error_errno(errno, "Couldn't accept a new connection: %m");
423 if (m->n_clients >= CLIENTS_MAX) {
424 log_error("Too many clients, refusing connection.");
428 log_debug("New fsck client connected to fd: %d", new_client_fd);
436 c->fd = new_client_fd;
439 r = sd_event_add_io(m->event, &c->event_source, c->fd, EPOLLIN, client_progress_handler, c);
445 LIST_PREPEND(clients, m->clients, c);
449 /* only request the client to cancel now in case the request is dropped by the client (chance to recancel) */
450 if (m->cancel_requested)
451 client_request_cancel(c);
457 static void manager_free(Manager *m) {
461 /* clear last line */
462 manager_write_console(m, NULL);
464 sd_event_source_unref(m->connection_event_source);
465 safe_close(m->connection_fd);
468 client_free(m->clients);
470 manager_disconnect_plymouth(m);
472 sd_event_unref(m->event);
477 static int manager_new(Manager **ret, int fd) {
478 _cleanup_(manager_freep) Manager *m = NULL;
483 m = new0(Manager, 1);
488 m->connection_fd = fd;
491 r = sd_event_default(&m->event);
495 if (access("/run/systemd/show-status", F_OK) >= 0)
496 m->show_status_console = true;
498 r = sd_event_add_io(m->event, &m->connection_event_source, fd, EPOLLIN, manager_new_connection_handler, m);
508 static int run_event_loop_with_timeout(sd_event *e, usec_t timeout) {
514 r = sd_event_get_state(e);
517 if (r == SD_EVENT_FINISHED)
520 r = sd_event_run(e, timeout);
524 /* timeout reached */
531 r = sd_event_get_exit_code(e, &code);
538 static void help(void) {
539 printf("%s [OPTIONS...]\n\n"
540 "Capture fsck progress and forward one stream to plymouth\n\n"
541 " -h --help Show this help\n"
542 " --version Show package version\n",
543 program_invocation_short_name);
546 static int parse_argv(int argc, char *argv[]) {
553 static const struct option options[] = {
554 { "help", no_argument, NULL, 'h' },
555 { "version", no_argument, NULL, ARG_VERSION },
564 while ((c = getopt_long(argc, argv, "hv", options, NULL)) >= 0)
572 puts(PACKAGE_STRING);
573 puts(SYSTEMD_FEATURES);
580 assert_not_reached("Unhandled option");
584 log_error("Extraneous arguments");
591 int main(int argc, char *argv[]) {
592 _cleanup_(manager_freep) Manager *m = NULL;
596 log_set_target(LOG_TARGET_AUTO);
597 log_parse_environment();
601 r = parse_argv(argc, argv);
605 n = sd_listen_fds(0);
607 log_error("Too many file descriptors received.");
611 fd = SD_LISTEN_FDS_START + 0;
613 fd = make_socket_fd(LOG_DEBUG, FSCKD_SOCKET_PATH, SOCK_STREAM | SOCK_CLOEXEC);
615 r = log_error_errno(fd, "Couldn't create listening socket fd on %s: %m", FSCKD_SOCKET_PATH);
620 r = manager_new(&m, fd);
622 log_error_errno(r, "Failed to allocate manager: %m");
626 r = run_event_loop_with_timeout(m->event, IDLE_TIME_SECONDS * USEC_PER_SEC);
628 log_error_errno(r, "Failed to run event loop: %m");
632 sd_event_get_exit_code(m->event, &r);
635 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;