fsckd.c revision ac6e2f0dfc2e800f01c79ecea2b811914110a7b9
/*-*- 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 "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
struct Manager;
typedef struct Client {
int fd;
int pass;
double percent;
} Client;
typedef struct Manager {
int clear;
int connection_fd;
double percent;
int numdevices;
} Manager;
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 update_global_progress(Manager *m) {
int current_numdevices = 0, l = 0;
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;
/* write to console */
if (m->console) {
}
if (l > m->clear)
m->clear = l;
}
return 0;
}
int r;
/* 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;
}
} 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);
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;
*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;
}