From: Stefan Agner <[email protected]>

This adds optional systemd support. When enabled, an utility called
psplash-systemd is built. This tool will connect to systemd using the
system bus to obtain progress information and communicate that to
psplash. Once full boot progress is reported by systemd psplash
psplash is closed using the quit command.

Signed-off-by: Stefan Agner <[email protected]>
---
 Makefile.am       |   7 ++
 configure.ac      |   9 +++
 psplash-systemd.c | 179 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 195 insertions(+)
 create mode 100644 psplash-systemd.c

diff --git a/Makefile.am b/Makefile.am
index a14152a..2374fe3 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -9,6 +9,13 @@ psplash_SOURCES = psplash.c psplash.h psplash-fb.c 
psplash-fb.h \
 
 psplash_write_SOURCES = psplash-write.c psplash.h
 
+if HAVE_SYSTEMD
+bin_PROGRAMS += psplash-systemd
+psplash_systemd_CPPFLAGS = $(SYSTEMD_CFLAGS)
+psplash_systemd_LDFLAGS= $(SYSTEMD_LIBS)
+psplash_systemd_SOURCES = psplash-systemd.c psplash.h
+endif
+
 EXTRA_DIST = make-image-header.sh
  
 MAINTAINERCLEANFILES = aclocal.m4 compile config.guess config.sub configure 
depcomp install-sh ltmain.sh Makefile.in missing
diff --git a/configure.ac b/configure.ac
index 1c4d919..107b182 100644
--- a/configure.ac
+++ b/configure.ac
@@ -12,6 +12,15 @@ if test "x$GCC" = "xyes"; then
         GCC_FLAGS="-g -Wall -Wextra"
 fi
 
+AC_ARG_WITH([systemd], AS_HELP_STRING([--with-systemd], [Build with systemd
+            support]))
+
+AS_IF([test "x$with_systemd" = "xyes"], [
+   PKG_CHECK_MODULES(SYSTEMD, libsystemd >= 221)
+])
+
+AM_CONDITIONAL([HAVE_SYSTEMD], [test "x$with_systemd" = "xyes"])
+
 AC_SUBST(GCC_FLAGS)
 
 AC_OUTPUT([
diff --git a/psplash-systemd.c b/psplash-systemd.c
new file mode 100644
index 0000000..840bd4e
--- /dev/null
+++ b/psplash-systemd.c
@@ -0,0 +1,179 @@
+/*
+ * pslash-systemd - systemd integration for psplash
+ *
+ * Copyright (c) 2020 Toradex
+ *
+ * Author: Stefan Agner <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <systemd/sd-bus.h>
+#include <systemd/sd-event.h>
+#include "psplash.h"
+
+#define PSPLASH_UPDATE_USEC 1000000
+
+typedef uint64_t usec_t;
+
+static int pipe_fd;
+
+int get_progress(void)
+{
+       sd_bus_error error = SD_BUS_ERROR_NULL;
+       static double current_progress = 0;
+       double progress = 0;
+       sd_bus *bus = NULL;
+       int r;
+       char buffer[20];
+       int len;
+
+        /* Connect to the system bus */
+       r = sd_bus_new(&bus);
+       if (r < 0)
+               goto finish;
+
+       r = sd_bus_set_address(bus, "unix:path=/run/systemd/private");
+       if (r < 0)
+               goto finish;
+
+       r = sd_bus_start(bus);
+       if (r < 0) {
+               fprintf(stderr, "Failed to connect to systemd private bus: 
%s\n", strerror(-r));
+               goto finish;
+        }
+
+        /* Issue the method call and store the respons message in m */
+       r = sd_bus_get_property_trivial(bus,
+               "org.freedesktop.systemd1",           /* service to contact */
+               "/org/freedesktop/systemd1",          /* object path */
+               "org.freedesktop.systemd1.Manager",   /* interface name */
+               "Progress",                           /* method name */
+               &error,                               /* object to return error 
in */
+               'd',                                  /* return message on 
success */
+               &progress);                           /* value */
+       if (r < 0) {
+               fprintf(stderr, "Failed to get progress: %s\n", error.message);
+               goto finish;
+       }
+
+       /*
+        * Systemd's progress seems go backwards at times. Prevent that
+        * progress bar on psplash goes backward by just communicating the
+        * highest observed progress so far.
+        */
+       if (current_progress < progress)
+               current_progress = progress;
+
+       len = snprintf(buffer, 20, "PROGRESS %d", (int)(current_progress * 
100));
+       write(pipe_fd, buffer, len + 1);
+
+       if (progress == 1.0) {
+               printf("Systemd reported progress of 1.0, quit psplash.\n");
+               write(pipe_fd, "QUIT", 5);
+               r = -1;
+       }
+
+finish:
+       sd_bus_error_free(&error);
+       sd_bus_unref(bus);
+
+       return r;
+}
+
+int psplash_handler(sd_event_source *s,
+                       uint64_t usec,
+                       void *userdata)
+{
+       sd_event *event = userdata;
+       int r;
+
+       r = get_progress();
+       if (r < 0)
+               goto err;
+
+       r = sd_event_source_set_time(s, usec + PSPLASH_UPDATE_USEC);
+       if (r < 0)
+               goto err;
+
+       return 0;
+err:
+       sd_event_exit(event, EXIT_FAILURE);
+
+       return r;
+}
+
+int main()
+{
+       sd_event *event;
+       sd_event_source *event_source = NULL;
+       int r;
+       sigset_t ss;
+       usec_t time_now;
+       char *rundir;
+
+       /* Open pipe for psplash */
+       rundir = getenv("PSPLASH_FIFO_DIR");
+
+       if (!rundir)
+               rundir = "/run";
+
+       chdir(rundir);
+
+       if ((pipe_fd = open (PSPLASH_FIFO,O_WRONLY|O_NONBLOCK)) == -1) {
+               fprintf(stderr, "Error unable to open fifo");
+               exit(EXIT_FAILURE);
+       }
+
+       r = sd_event_default(&event);
+       if (r < 0)
+               goto finish;
+
+       if (sigemptyset(&ss) < 0 ||
+           sigaddset(&ss, SIGTERM) < 0 ||
+           sigaddset(&ss, SIGINT) < 0) {
+               r = -errno;
+               goto finish;
+       }
+
+       /* Block SIGTERM first, so that the event loop can handle it */
+       if (sigprocmask(SIG_BLOCK, &ss, NULL) < 0) {
+               r = -errno;
+               goto finish;
+       }
+
+       /* Let's make use of the default handler and "floating" reference
+        * features of sd_event_add_signal() */
+       r = sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
+       if (r < 0)
+               goto finish;
+
+       r = sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
+       if (r < 0)
+               goto finish;
+
+       r = sd_event_now(event, CLOCK_MONOTONIC, &time_now);
+       if (r < 0)
+               goto finish;
+
+       r = sd_event_add_time(event, &event_source, CLOCK_MONOTONIC,
+                             time_now, 0, psplash_handler, event);
+       if (r < 0)
+               goto finish;
+
+       r = sd_event_source_set_enabled(event_source, SD_EVENT_ON);
+       if (r < 0)
+               goto finish;
+
+       r = sd_event_loop(event);
+finish:
+       event = sd_event_unref(event);
+       close(pipe_fd);
+
+       return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+}
-- 
2.24.1

-=-=-=-=-=-=-=-=-=-=-=-
Links: You receive all messages sent to this group.

View/Reply Online (#48061): https://lists.yoctoproject.org/g/yocto/message/48061
Mute This Topic: https://lists.yoctoproject.org/mt/69953266/21656
Group Owner: [email protected]
Unsubscribe: https://lists.yoctoproject.org/g/yocto/unsub  
[[email protected]]
-=-=-=-=-=-=-=-=-=-=-=-

Reply via email to