+static int flush_to_var(void) {
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ _cleanup_bus_close_unref_ sd_bus *bus = NULL;
+ _cleanup_close_ int watch_fd = -1;
+ int r;
+
+ /* Quick exit */
+ if (access("/run/systemd/journal/flushed", F_OK) >= 0)
+ return 0;
+
+ /* OK, let's actually do the full logic, send SIGUSR1 to the
+ * daemon and set up inotify to wait for the flushed file to appear */
+ r = bus_open_system_systemd(&bus);
+ if (r < 0) {
+ log_error_errno(r, "Failed to get D-Bus connection: %m");
+ return r;
+ }
+
+ r = sd_bus_call_method(
+ bus,
+ "org.freedesktop.systemd1",
+ "/org/freedesktop/systemd1",
+ "org.freedesktop.systemd1.Manager",
+ "KillUnit",
+ &error,
+ NULL,
+ "ssi", "systemd-journald.service", "main", SIGUSR1);
+ if (r < 0) {
+ log_error("Failed to kill journal service: %s", bus_error_message(&error, r));
+ return r;
+ }
+
+ mkdir_p("/run/systemd/journal", 0755);
+
+ watch_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
+ if (watch_fd < 0) {
+ log_error("Failed to create inotify watch: %m");
+ return -errno;
+ }
+
+ r = inotify_add_watch(watch_fd, "/run/systemd/journal", IN_CREATE|IN_DONT_FOLLOW|IN_ONLYDIR);
+ if (r < 0) {
+ log_error("Failed to watch journal directory: %m");
+ return -errno;
+ }
+
+ for (;;) {
+ if (access("/run/systemd/journal/flushed", F_OK) >= 0)
+ break;
+
+ if (errno != ENOENT) {
+ log_error("Failed to check for existance of /run/systemd/journal/flushed: %m");
+ return -errno;
+ }
+
+ r = fd_wait_for_event(watch_fd, POLLIN, USEC_INFINITY);
+ if (r < 0) {
+ log_error_errno(r, "Failed to wait for event: %m");
+ return r;
+ }
+
+ r = flush_fd(watch_fd);
+ if (r < 0) {
+ log_error_errno(r, "Failed to flush inotify events: %m");
+ return r;
+ }
+ }
+
+ return 0;
+}
+