+static int client_request_cancel(Client *c) {
+ FsckdMessage cancel_msg = {
+ .cancel = 1,
+ };
+
+ ssize_t n;
+
+ assert(c);
+
+ if (c->cancelled)
+ return 0;
+
+ n = send(c->fd, &cancel_msg, sizeof(FsckdMessage), 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) {
+ LIST_REMOVE(clients, c->manager->clients, c);
+ c->manager->n_clients--;
+ }
+
+ sd_event_source_unref(c->event_source);
+
+ safe_close(c->fd);
+ free(c);
+}
+
+static void manager_disconnect_plymouth(Manager *m) {
+ assert(m);
+
+ m->plymouth_event_source = sd_event_source_unref(m->plymouth_event_source);
+ m->plymouth_fd = safe_close(m->plymouth_fd);
+ m->plymouth_cancel_sent = false;
+}
+
+static int manager_plymouth_feedback_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
+ Manager *m = userdata;
+ Client *current;
+ char buffer[6];
+ ssize_t l;
+
+ assert(m);
+
+ l = read(m->plymouth_fd, buffer, sizeof(buffer));
+ if (l < 0) {
+ log_warning_errno(errno, "Got error while reading from plymouth: %m");
+ manager_disconnect_plymouth(m);
+ return -errno;
+ }
+ if (l == 0) {
+ manager_disconnect_plymouth(m);
+ return 0;
+ }
+
+ if (l > 1 && buffer[0] == '\15')
+ log_error("Message update to plymouth wasn't delivered successfully");
+
+ /* the only answer support type we requested is a key interruption */
+ if (l > 2 && buffer[0] == '\2' && buffer[5] == '\3') {
+ m->cancel_requested = true;
+
+ /* cancel all connected clients */
+ LIST_FOREACH(clients, current, m->clients)
+ client_request_cancel(current);
+ }
+
+ return 0;
+}
+
+static int manager_connect_plymouth(Manager *m) {
+ union sockaddr_union sa = PLYMOUTH_SOCKET;
+ int r;
+
+ /* try to connect or reconnect if sending a message */
+ if (m->plymouth_fd >= 0)
+ return 0;
+
+ m->plymouth_fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
+ if (m->plymouth_fd < 0)
+ return log_warning_errno(errno, "Connection to plymouth socket failed: %m");
+
+ if (connect(m->plymouth_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) {
+ r = log_warning_errno(errno, "Couldn't connect to plymouth: %m");
+ 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:
+ manager_disconnect_plymouth(m);
+ return r;
+}
+
+static int plymouth_send_message(int plymouth_fd, const char *message, bool update) {
+ _cleanup_free_ char *packet = NULL;
+ int n;
+ char mode = 'M';
+
+ if (update)
+ mode = 'U';
+
+ if (asprintf(&packet, "%c\002%c%s%n", mode, (int) (strlen(message) + 1), message, &n) < 0)
+ return log_oom();