From 9efd43080c8fa809d8f14c63c65d6f6e65eed1f2 Mon Sep 17 00:00:00 2001
From: Didier Roche <didrocks@ubuntu.com>
Date: Mon, 26 Jan 2015 15:35:50 +0100
Subject: [PATCH 01/13] fsckd daemon for inter-fsckd communication

* starts systemd-fsckd implementation, accepting multiple systemd-fsck
  instances to connect to it and send progress report. systemd-fsckd then
  compute and write to /dev/console the number of devices currently being
  checked and the minimum fsck progress.
* systemd-fsckd stops on idling when no systemd-fsck is connected.
---
 .gitignore         |   1 +
 Makefile.am        |   9 ++
 src/fsck/fsck.c    |  58 +++++----
 src/fsckd/Makefile |  28 +++++
 src/fsckd/fsckd.c  | 340 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/fsckd/fsckd.h  |  22 ++++
 6 files changed, 428 insertions(+), 30 deletions(-)
 create mode 100644 src/fsckd/Makefile
 create mode 100644 src/fsckd/fsckd.c
 create mode 100644 src/fsckd/fsckd.h

diff --git a/.gitignore b/.gitignore
index ab6d9d1..9400e75 100644
--- a/.gitignore
+++ b/.gitignore
@@ -74,6 +74,7 @@
 /systemd-evcat
 /systemd-firstboot
 /systemd-fsck
+/systemd-fsckd
 /systemd-fstab-generator
 /systemd-getty-generator
 /systemd-gnome-ask-password-agent
diff --git a/Makefile.am b/Makefile.am
index c463f23..4596dfd 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -389,6 +389,7 @@ rootlibexec_PROGRAMS = \
 	systemd-remount-fs \
 	systemd-reply-password \
 	systemd-fsck \
+	systemd-fsckd \
 	systemd-machine-id-commit \
 	systemd-ac-power \
 	systemd-sysctl \
@@ -2355,6 +2356,14 @@ systemd_fsck_LDADD = \
 	libsystemd-shared.la
 
 # ------------------------------------------------------------------------------
+systemd_fsckd_SOURCES = \
+	src/fsckd/fsckd.c
+
+systemd_fsckd_LDADD = \
+	libsystemd-internal.la \
+	libsystemd-shared.la
+
+# ------------------------------------------------------------------------------
 systemd_machine_id_commit_SOURCES = \
 	src/machine-id-commit/machine-id-commit.c \
 	src/core/machine-id-setup.c \
diff --git a/src/fsck/fsck.c b/src/fsck/fsck.c
index 20b7940..8a8d0e2 100644
--- a/src/fsck/fsck.c
+++ b/src/fsck/fsck.c
@@ -39,6 +39,8 @@
 #include "fileio.h"
 #include "udev-util.h"
 #include "path-util.h"
+#include "socket-util.h"
+#include "fsckd/fsckd.h"
 
 static bool arg_skip = false;
 static bool arg_force = false;
@@ -151,39 +153,42 @@ static double percent(int pass, unsigned long cur, unsigned long max) {
 }
 
 static int process_progress(int fd) {
-        _cleanup_fclose_ FILE *console = NULL, *f = NULL;
+        _cleanup_fclose_ FILE *f = NULL;
         usec_t last = 0;
-        bool locked = false;
-        int clear = 0;
+        _cleanup_close_ int fsckd_fd;
+        static const union sockaddr_union sa = {
+                .un.sun_family = AF_UNIX,
+                .un.sun_path = FSCKD_SOCKET_PATH,
+        };
+
+        fsckd_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
+        if (fsckd_fd < 0) {
+                log_warning_errno(fsckd_fd, "Cannot open fsckd socket, we won't report fsck progress: %m");
+                return -fsckd_fd;
+        }
+        if (connect(fsckd_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
+                log_warning_errno(errno, "Cannot connect to fsckd socket, we won't report fsck progress: %m");
+                return -errno;
+        }
 
         f = fdopen(fd, "r");
         if (!f) {
-                safe_close(fd);
+                log_warning_errno(errno, "Cannot connect to fsck, we won't report fsck progress: %m");
                 return -errno;
         }
 
-        console = fopen("/dev/console", "we");
-        if (!console)
-                return -ENOMEM;
-
         while (!feof(f)) {
-                int pass, m;
+                int pass;
                 unsigned long cur, max;
                 _cleanup_free_ char *device = NULL;
                 double p;
+                ssize_t n;
+                size_t l;
                 usec_t t;
 
                 if (fscanf(f, "%i %lu %lu %ms", &pass, &cur, &max, &device) != 4)
                         break;
 
-                /* Only show one progress counter at max */
-                if (!locked) {
-                        if (flock(fileno(console), LOCK_EX|LOCK_NB) < 0)
-                                continue;
-
-                        locked = true;
-                }
-
                 /* Only update once every 50ms */
                 t = now(CLOCK_MONOTONIC);
                 if (last + 50 * USEC_PER_MSEC > t)
@@ -192,21 +197,14 @@ static int process_progress(int fd) {
                 last = t;
 
                 p = percent(pass, cur, max);
-                fprintf(console, "\r%s: fsck %3.1f%% complete...\r%n", device, p, &m);
-                fflush(console);
-
-                if (m > clear)
-                        clear = m;
-        }
 
-        if (clear > 0) {
-                unsigned j;
+                /* send progress to fsckd */
+                asprintf(&progress_message, "%s:%3.1f\n", device, p);
+                l = strlen(progress_message);
+                n = write(fsckd_fd, progress_message, l);
+                if (n < 0 || (size_t) n < l)
+                        log_warning_errno(n, "Cannot communicate %s to fsckd: %m", progress_message);
 
-                fputc('\r', console);
-                for (j = 0; j < (unsigned) clear; j++)
-                        fputc(' ', console);
-                fputc('\r', console);
-                fflush(console);
         }
 
         return 0;
diff --git a/src/fsckd/Makefile b/src/fsckd/Makefile
new file mode 100644
index 0000000..9d07505
--- /dev/null
+++ b/src/fsckd/Makefile
@@ -0,0 +1,28 @@
+#  This file is part of systemd.
+#
+#  Copyright 2010 Lennart Poettering
+#
+#  systemd is free software; you can redistribute it and/or modify it
+#  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/>.
+
+# This file is a dirty trick to simplify compilation from within
+# emacs. This file is not intended to be distributed. So, don't touch
+# it, even better ignore it!
+
+all:
+	$(MAKE) -C ..
+
+clean:
+	$(MAKE) -C .. clean
+
+.PHONY: all clean
diff --git a/src/fsckd/fsckd.c b/src/fsckd/fsckd.c
new file mode 100644
index 0000000..3fc1053
--- /dev/null
+++ b/src/fsckd/fsckd.c
@@ -0,0 +1,340 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright 2015 Canonical
+
+  systemd is free software; you can redistribute it and/or modify it
+  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 <stdlib.h>
+#include <stdio.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/un.h>
+#include <unistd.h>
+
+#include "build.h"
+#include "fsckd.h"
+#include "log.h"
+#include "list.h"
+#include "macro.h"
+#include "sd-daemon.h"
+#include "time-util.h"
+#include "util.h"
+
+#define INITIAL_BUFFER_SIZE 20
+#define IDLE_TIME_MINUTES 1
+#define ERROR_FORMAT_MSG "We expected that the sent format would be of form: \"/dev/<device>:(double progress)\\n\". Received: %s"
+
+typedef struct Clients {
+        int fd;
+        double percentage;
+        char *device;
+
+        LIST_FIELDS(struct Clients, clients);
+} Clients;
+
+/* this can eventually be put in fileio.c */
+static int read_full_fd(int fd, char **buf) {
+        // message format is /dev/<device>:%\n
+        static size_t n = INITIAL_BUFFER_SIZE;
+        int l = 0;
+        for (;;) {
+                char *t;
+                int k;
+
+                t = realloc(*buf, n);
+                if (!t)
+                        return -ENOMEM;
+
+                *buf = t;
+                k = read(fd, *buf + l, n - l);
+                if (k <= 0) {
+                        // some data from previous loops to treat
+                        if (l != 0)
+                                break;
+                        return k;
+                }
+
+                l += k;
+
+                /* we resize if l == n as we are unsure to have read the entire available content */
+                if (l < (int) n)
+                    continue;
+                n *= 2;
+                log_debug("Resizing socket content buffer to %d\n", (int)n);
+
+                /* Safety check */
+                if (n > 4*1024*1024)
+                        return -E2BIG;
+        }
+        (*buf)[l] = '\0';
+        return l;
+}
+
+static int treat_requests(int socket_fd) {
+        Clients *first = NULL;
+        usec_t last_activity = 0;
+        int nbdevices = 0, clear = 0;
+        double minpercent = 100;
+        _cleanup_free_ char *buf = NULL;
+        _cleanup_fclose_ FILE *console = NULL;
+
+        console = fopen("/dev/console", "we");
+        if (!console) {
+                log_error("Can't connect to /dev/console");
+                return -ENOMEM;
+        }
+
+        buf = malloc(INITIAL_BUFFER_SIZE*sizeof(char));
+        last_activity = now(CLOCK_MONOTONIC);
+
+        for (;;) {
+                int new_client_fd = 0;
+                bool remove_previous = false;
+                Clients *current, *to_remove;
+                _cleanup_free_ char *console_message = NULL;
+                int current_nbdevices = 0, m = 0;
+                double current_minpercent = 100;
+                usec_t t;
+                int rc;
+                char *last_entry, *sep;  // pointers to substring, no need for freeing
+
+
+                /* Initialize and list new clients */
+                new_client_fd = accept4(socket_fd, NULL, NULL, SOCK_NONBLOCK);
+                if (new_client_fd > 0) {
+                        log_debug("new fsck client connected to fd: %d", new_client_fd);
+                        current = alloca0(sizeof(Clients));
+                        current->fd = new_client_fd;
+                        if (!first)
+                                LIST_INIT(clients, current);
+                        else
+                                LIST_PREPEND(clients, first, current);
+                        first = current;
+                        current->device = NULL;
+                        current = NULL;
+                }
+
+                LIST_FOREACH(clients, current, first) {
+                        if (remove_previous)
+                                LIST_REMOVE(clients, first, current->clients_prev);
+
+                        remove_previous = false;
+
+                        rc = read_full_fd(current->fd, &buf);
+                        if (rc == 0) {
+                                log_debug("fsck client connected to fd %d disconnected", current->fd);
+                                remove_previous = true;
+                        }
+                        else if ((rc < 0) && (errno != EWOULDBLOCK)) {
+                                log_error_errno(errno, "Socket read error, disconnecting fd %d: %m", current->fd);
+                                remove_previous = true;
+                        }
+                        else if (rc > 0) {
+                                /* check format */
+                                if (buf[rc-1] != '\n') {
+                                        log_error(ERROR_FORMAT_MSG, buf);
+                                        continue;
+                                }
+                                buf[rc-1] = '\0';
+
+                                /* we treat only last message per device (other are invalid) */
+                                last_entry = strrchr(buf, '\n');
+                                if (!last_entry)
+                                        last_entry = buf;
+                                else
+                                        last_entry++;
+
+                                /* extract values */
+                                sep = strrchr(last_entry, ':');
+                                if (!sep) {
+                                        log_error(ERROR_FORMAT_MSG, buf);
+                                        continue;
+                                }
+                                if (!(current->device))
+                                        current->device = strndupa(buf, (sep - last_entry));
+                                rc = safe_atod(sep+1, &(current->percentage));
+                                if (rc < 0) {
+                                        log_error(ERROR_FORMAT_MSG, buf);
+                                        continue;
+                                }
+
+                        }
+
+                        if(remove_previous)
+                                close(current->fd);
+                        else {
+                                current_nbdevices++;
+                                current_minpercent = MIN(current_minpercent, current->percentage);
+                        }
+                }
+
+                /* remove last item if should be deleted */
+                if (remove_previous) {
+                        LIST_FIND_TAIL(clients, first, to_remove);
+                        LIST_REMOVE(clients, first, to_remove);
+                }
+
+                if ((fabs(current_minpercent - minpercent) > 0.000001) || (current_nbdevices != nbdevices)) {
+                        minpercent = current_minpercent;
+                        nbdevices = current_nbdevices;
+
+                        asprintf(&console_message, "Checking in progress on %d disks (%3.1f%% complete)",
+                                 nbdevices, minpercent);
+
+                        /* write to console */
+                        fprintf(console, "\r%s\r%n", console_message, &m);
+                        fflush(console);
+
+                        if (m > clear)
+                                clear = m;
+                }
+
+                /* idle out after IDLE_TIME_MINUTES minutes with no connected device */
+                t = now(CLOCK_MONOTONIC);
+                if (nbdevices == 0) {
+                        if (t > last_activity + IDLE_TIME_MINUTES * USEC_PER_MINUTE) {
+                            log_debug("No fsck in progress for the last %d minutes, shutting down.", IDLE_TIME_MINUTES);
+                            break;
+                        }
+                } else
+                    last_activity = t;
+        }
+
+        /* clear last line */
+        if (clear > 0) {
+                unsigned j;
+
+                fputc('\r', console);
+                for (j = 0; j < (unsigned) clear; j++)
+                        fputc(' ', console);
+                fputc('\r', console);
+                fflush(console);
+        }
+
+        return 0;
+}
+
+static int create_socket(void) {
+        /* Create the socket manually for testing */
+        union {
+                struct sockaddr sa;
+                struct sockaddr_un un;
+        } sa;
+        int fd;
+
+        fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
+        if (fd < 0)
+                return log_error_errno(errno, "socket() failed: %m");
+
+        memset(&sa, 0, sizeof(sa));
+        sa.un.sun_family = AF_UNIX;
+        strncpy(sa.un.sun_path, FSCKD_SOCKET_PATH, sizeof(sa.un.sun_path));
+        unlink(FSCKD_SOCKET_PATH);
+        if (bind(fd, &sa.sa, sizeof(sa)) < 0)
+                return log_error_errno(errno, "binding %s failed: %m", FSCKD_SOCKET_PATH);
+
+        if (listen(fd, 5) < 0)
+                return log_error_errno(errno, "listening on %s failed: %m", FSCKD_SOCKET_PATH);
+
+        return fd;
+}
+
+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",
+               program_invocation_short_name);
+}
+
+static int parse_argv(int argc, char *argv[]) {
+
+        enum {
+                ARG_VERSION = 0x100,
+                ARG_ROOT,
+        };
+
+        static const struct option options[] = {
+                { "help",      no_argument,       NULL, 'h'           },
+                { "version",   no_argument,       NULL, ARG_VERSION   },
+                {}
+        };
+
+        int c;
+
+        assert(argc >= 0);
+        assert(argv);
+
+        while ((c = getopt_long(argc, argv, "hv", options, NULL)) >= 0)
+                switch (c) {
+
+                case 'h':
+                        help();
+                        return 0;
+
+                case ARG_VERSION:
+                        puts(PACKAGE_STRING);
+                        puts(SYSTEMD_FEATURES);
+                        return 0;
+
+                case '?':
+                        return -EINVAL;
+
+                default:
+                        assert_not_reached("Unhandled option");
+                }
+
+        if (optind < argc) {
+                log_error("Extraneous arguments");
+                return -EINVAL;
+        }
+
+        return 1;
+}
+
+int main(int argc, char *argv[]) {
+        int r;
+        int fd, n;
+        int flags;
+
+        log_set_target(LOG_TARGET_AUTO);
+        log_parse_environment();
+        log_open();
+
+        r = parse_argv(argc, argv);
+        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;
+                flags = fcntl(fd, F_GETFL, 0);
+                fcntl(fd, F_SETFL, flags | O_NONBLOCK);
+        } else {
+                fd = create_socket();
+                if (fd <= 0)
+                        return EXIT_FAILURE;
+        }
+        return treat_requests(fd) < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+}
diff --git a/src/fsckd/fsckd.h b/src/fsckd/fsckd.h
new file mode 100644
index 0000000..efe1994
--- /dev/null
+++ b/src/fsckd/fsckd.h
@@ -0,0 +1,22 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright 2015 Canonical
+
+  systemd is free software; you can redistribute it and/or modify it
+  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/>.
+***/
+
+#define FSCKD_SOCKET_PATH "/run/systemd/fsckd"
-- 
2.1.4

