aboutsummaryrefslogtreecommitdiffstats
path: root/psplash-systemd.c
blob: 840bd4e0eb855b4eced7ec2f45f17fc18966dbf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * pslash-systemd - systemd integration for psplash
 *
 * Copyright (c) 2020 Toradex
 *
 * Author: Stefan Agner <stefan.agner@toradex.com>
 *
 * 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;
}