fsckd.c revision 1952708abb10a47960d9160205e511343894896d
/*-*- 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 <libintl.h>
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "sd-daemon.h"
#include "build.h"
#include "def.h"
#include "event-util.h"
#include "log.h"
#include "list.h"
#include "macro.h"
#include "socket-util.h"
#include "util.h"
#include "fsckd.h"
#define IDLE_TIME_SECONDS 30
#define PLYMOUTH_REQUEST_KEY "K\2\2\3"
#define CLIENTS_MAX 128
struct Manager;
typedef struct Client {
int fd;
int pass;
double percent;
bool cancelled;
} Client;
typedef struct Manager {
unsigned n_clients;
int clear;
int connection_fd;
double percent;
int numdevices;
int plymouth_fd;
bool plymouth_cancel_sent;
bool cancel_requested;
} Manager;
static void client_free(Client *c);
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;
}
static int client_request_cancel(Client *c) {
.cancel = 1,
};
ssize_t n;
assert(c);
if (c->cancelled)
return 0;
if (n < 0)
return log_warning_errno(errno, "Cannot send cancel to fsck on (%u:%u): %m", major(c->devnum), minor(c->devnum));
if ((size_t) n < sizeof(FsckdMessage)) {
log_warning("Short send when sending cancel to fsck on (%u:%u).", major(c->devnum), minor(c->devnum));
return -EIO;
}
c->cancelled = true;
return 1;
}
static void client_free(Client *c) {
assert(c);
if (c->manager) {
}
safe_close(c->fd);
free(c);
}
static void manager_disconnect_plymouth(Manager *m) {
assert(m);
m->plymouth_cancel_sent = false;
}
static int manager_plymouth_feedback_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
char buffer[6];
ssize_t l;
assert(m);
if (l < 0) {
return -errno;
}
if (l == 0) {
return 0;
}
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;
}
static int manager_connect_plymouth(Manager *m) {
int r;
/* try to connect or reconnect if sending a message */
if (m->plymouth_fd >= 0)
return 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) {
goto fail;
}
r = sd_event_add_io(m->event, &m->plymouth_event_source, m->plymouth_fd, EPOLLIN, manager_plymouth_feedback_handler, m);
if (r < 0) {
log_warning_errno(r, "Can't listen to plymouth socket: %m");
goto fail;
}
return 1;
fail:
return r;
}
int n;
char mode = 'M';
if (update)
mode = 'U';
return log_oom();
}
const char *plymouth_cancel_message = NULL;
int r;
r = manager_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)
return log_warning_errno(r, "Can't send to plymouth cancel key: %m");
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 manager_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;
if (asprintf(&console_message,
ngettext("Checking in progress on %d disk (%3.1f%% complete)",
"Checking in progress on %d disks (%3.1f%% complete)", m->numdevices),
m->numdevices, m->percent) < 0)
return -ENOMEM;
return -ENOMEM;
/* write to console */
if (m->console) {
}
/* try to connect to plymouth and send message */
if (r < 0)
log_debug("Couldn't send message to plymouth");
if (l > m->clear)
m->clear = l;
}
return 0;
}
Manager *m;
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 = manager_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 = manager_update_global_progress(m);
if (r < 0)
log_warning_errno(r, "Couldn't update global progress: %m");
return 0;
}
static int manager_new_connection_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
int r;
assert(m);
/* Initialize and list new clients */
if (new_client_fd < 0)
if (m->n_clients >= CLIENTS_MAX) {
log_error("Too many clients, refusing connection.");
return 0;
}
if (!c) {
log_oom();
return 0;
}
c->fd = new_client_fd;
new_client_fd = -1;
if (r < 0) {
log_oom();
return 0;
}
m->n_clients++;
c->manager = m;
/* only request the client to cancel now in case the request is dropped by the client (chance to recancel) */
if (m->cancel_requested)
c = NULL;
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);
while (m->clients)
client_free(m->clients);
if (m->console)
sd_event_unref(m->event);
free(m);
}
int r;
if (!m)
return -ENOMEM;
m->plymouth_fd = -1;
m->connection_fd = fd;
m->percent = 100;
r = sd_event_default(&m->event);
if (r < 0)
return r;
if (!m->console)
return -errno;
}
r = sd_event_add_io(m->event, &m->connection_event_source, fd, EPOLLIN, manager_new_connection_handler, m);
if (r < 0)
return r;
*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();
init_gettext();
if (r <= 0)
goto finish;
n = sd_listen_fds(0);
if (n > 1) {
log_error("Too many file descriptors received.");
r = -EINVAL;
goto finish;
} else if (n == 1)
fd = SD_LISTEN_FDS_START + 0;
else {
if (fd < 0) {
goto finish;
}
}
r = manager_new(&m, fd);
if (r < 0) {
log_error_errno(r, "Failed to allocate manager: %m");
goto finish;
}
if (r < 0) {
log_error_errno(r, "Failed to run event loop: %m");
goto finish;
}
sd_event_get_exit_code(m->event, &r);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}