fsckd.c revision 07f9a21b6d2a8a7cfd8928a329b1ce9c479e3972
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2015 Canonical
Author:
Didier Roche <didrocks@ubuntu.com>
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <getopt.h>
#include <errno.h>
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "build.h"
#include "def.h"
#include "event-util.h"
#include "fsckd.h"
#include "log.h"
#include "list.h"
#include "macro.h"
#include "sd-daemon.h"
#include "socket-util.h"
#include "util.h"
#define IDLE_TIME_SECONDS 30
#define PLYMOUTH_REQUEST_KEY "K\2\2\3"
struct Manager;
typedef struct Client {
int fd;
int pass;
double percent;
bool cancelled;
} Client;
typedef struct Manager {
int clear;
int connection_fd;
double percent;
int numdevices;
int plymouth_fd;
bool plymouth_cancel_sent;
bool cancel_requested;
} Manager;
static int connect_plymouth(Manager *m);
static int update_global_progress(Manager *m);
static void manager_free(Manager *m);
/* Values stolen from e2fsck */
static const double pass_table[] = {
0, 70, 90, 92, 95, 100
};
if (pass <= 0)
return 0.0;
return 100.0;
}
ssize_t n;
if (n < 0 || (size_t) n < sizeof(FsckdMessage))
return log_warning_errno(n, "Cannot send cancel to fsck on (%u, %u): %m",
else
return 0;
}
}
static void on_plymouth_disconnect(Manager *m) {
safe_close(m->plymouth_fd);
m->plymouth_fd = -1;
m->plymouth_cancel_sent = false;
}
static int plymouth_feedback_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
char buffer[6];
int r;
assert(m);
if (r <= 0)
else {
if (buffer[0] == '\15')
log_error("Message update to plymouth wasn't delivered successfully");
/* the only answer support type we requested is a key interruption */
m->cancel_requested = true;
/* cancel all connected clients */
}
}
return 0;
}
int r, n;
char mode = 'M';
if (update)
mode = 'U';
return log_oom();
return r;
}
int r;
const char *plymouth_cancel_message = NULL;
r = connect_plymouth(m);
if (r < 0)
return r;
if (!m->plymouth_cancel_sent) {
/* indicate to plymouth that we listen to Ctrl+C */
if (r < 0)
m->plymouth_cancel_sent = true;
plymouth_cancel_message = strjoina("fsckd-cancel-msg:", "Press Ctrl+C to cancel all filesystem checks in progress");
if (r < 0)
log_warning_errno(r, "Can't send filesystem cancel message to plymouth: %m");
} else if (m->numdevices == 0) {
m->plymouth_cancel_sent = false;
if (r < 0)
log_warning_errno(r, "Can't clear plymouth filesystem cancel message: %m");
}
if (r < 0)
return 0;
}
static int update_global_progress(Manager *m) {
int current_numdevices = 0, l = 0, r;
double current_percent = 100;
/* get the overall percentage */
/* right now, we only keep the minimum % of all fsckd processes. We could in the future trying to be
linear, but max changes and corresponds to the pass. We have all the informations into fsckd
already if we can treat that in a smarter way. */
}
/* update if there is anything user-visible to update */
m->numdevices = current_numdevices;
m->percent = current_percent;
m->numdevices, m->percent) < 0)
return -ENOMEM;
return -ENOMEM;
/* write to console */
if (m->console) {
}
/* try to connect to plymouth and send message */
r = send_message_plymouth(m, fsck_message);
if (r < 0)
log_debug("Couldn't send message to plymouth");
if (l > m->clear)
m->clear = l;
}
return 0;
}
static int connect_plymouth(Manager *m) {
int r;
/* try to connect or reconnect if sending a message */
if (m->plymouth_fd <= 0) {
if (m->plymouth_fd < 0) {
}
if (connect(m->plymouth_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) {
}
if (r < 0) {
return log_warning_errno(r, "Can't listen to plymouth socket: %m");
}
}
return 0;
}
int r;
/* check first if we need to cancel this client */
if (m->cancel_requested) {
}
/* ensure we have enough data to read */
/* we got twice the same size from a bad behaving client, kick it off the list */
else {
r = update_global_progress(m);
if (r < 0)
log_warning_errno(r, "Couldn't update global progress: %m");
}
return 0;
}
/* read actual data */
if (r == 0) {
} else if (r > 0 && r != sizeof(FsckProgress))
else if (r > 0 && r == sizeof(FsckProgress)) {
log_debug("Getting progress for %u:%u (%lu, %lu, %d) : %3.1f%%",
} else
log_error_errno(r, "Unknown error while trying to read fsck data: %m");
r = update_global_progress(m);
if (r < 0)
log_warning_errno(r, "Couldn't update global progress: %m");
return 0;
}
int new_client_fd, r;
assert(m);
/* Initialize and list new clients */
if (new_client_fd > 0) {
if (!client)
return log_oom();
if (r < 0) {
return r;
}
/* only request the client to cancel now in case the request is dropped by the client (chance to recancel) */
if (m->cancel_requested)
} else
return 0;
}
static void manager_free(Manager *m) {
if (!m)
return;
/* clear last line */
unsigned j;
for (j = 0; j < (unsigned) m->clear; j++)
}
safe_close(m->connection_fd);
safe_close(m->plymouth_fd);
if (m->console)
sd_event_unref(m->event);
free(m);
}
int r;
if (!m)
return -ENOMEM;
r = sd_event_default(&m->event);
if (r < 0)
return r;
m->connection_fd = fd;
if (!m->console)
}
m->percent = 100;
m->plymouth_fd = -1;
*ret = m;
m = NULL;
return 0;
}
int r, code;
assert(e);
for (;;) {
r = sd_event_get_state(e);
if (r < 0)
return r;
if (r == SD_EVENT_FINISHED)
break;
r = sd_event_run(e, timeout);
if (r < 0)
return r;
/* timeout reached */
if (r == 0) {
sd_event_exit(e, 0);
break;
}
}
r = sd_event_get_exit_code(e, &code);
if (r < 0)
return r;
return code;
}
static void help(void) {
printf("%s [OPTIONS...]\n\n"
"Capture fsck progress and forward one stream to plymouth\n\n"
" -h --help Show this help\n"
" --version Show package version\n",
}
enum {
ARG_VERSION = 0x100,
};
{}
};
int c;
switch (c) {
case 'h':
help();
return 0;
case ARG_VERSION:
return 0;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
log_error("Extraneous arguments");
return -EINVAL;
}
return 1;
}
int fd = -1;
int r, n;
log_open();
if (r <= 0)
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
n = sd_listen_fds(0);
if (n > 1) {
log_error("Too many file descriptors received.");
return EXIT_FAILURE;
} else if (n == 1) {
fd = SD_LISTEN_FDS_START + 0;
} else {
if (fd < 0) {
return EXIT_FAILURE;
}
}
r = manager_new(&m, fd);
if (r < 0) {
log_error_errno(r, "Failed to allocate manager: %m");
return EXIT_FAILURE;
}
if (r < 0) {
log_error_errno(r, "Can't listen to connection socket: %m");
return EXIT_FAILURE;
}
if (r < 0) {
log_error_errno(r, "Failed to run event loop: %m");
return EXIT_FAILURE;
}
sd_event_get_exit_code(m->event, &r);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}