summaryrefslogtreecommitdiff
path: root/systemd.c
blob: c5c62d30dc8c6ccba5edfe11d15dc4ba1dea1608 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <string.h>
#include <stdlib.h>
#include <systemd/sd-bus.h>

#include "main.h"
#include "server.h"
#include "systemd.h"

#define SYSTEMD_DBUS_SERVICE		"org.freedesktop.systemd1"
#define SYSTEMD_DBUS_INTERFACE		"org.freedesktop.systemd1.Unit"
#define SYSTEMD_DBUS_PATH_PREFIX	"/org/freedesktop/systemd1/unit/"

static inline char
tohex(uint8_t val)
{
	static const char hex[] = "0123456789abcdef";

	return hex[val & 0x0f];
}

/*
 * Creates an escaped D-Bus object path for a given systemd service
 *
 * Escaping rules are documented here:
 * https://dbus.freedesktop.org/doc/dbus-specification.html
 *
 * Essentially, everyting but a-z, A-Z, 0-9 is replaced by _xx where xx is
 * the hexadecimal value of the character.
 *
 * Example: minecraft@world1.service -> minecraft_40world1_2eservice
 */
char *
systemd_object_path(const char *service)
{
	char *r;
	char *d;
	const char *s;

	assert_return(service && !empty_str(service), NULL);

	r = zmalloc(strlen(SYSTEMD_DBUS_PATH_PREFIX) + strlen(service) * 3 + 1);
	if (!r)
		return NULL;

	memcpy(r, SYSTEMD_DBUS_PATH_PREFIX, strlen(SYSTEMD_DBUS_PATH_PREFIX));
	d = r + strlen(SYSTEMD_DBUS_PATH_PREFIX);

	for (s = service; *s; s++) {
		if ((*s >= 'a' && *s <= 'z') ||
		    (*s >= 'A' && *s <= 'Z') ||
		    (*s >= '0' && *s <= '9')) {
			*(d++) = *s;
			continue;
		}

		*(d++) = '_';
		*(d++) = tohex(*s >> 4);
		*(d++) = tohex(*s);
	}

	*d = '\0';
	return r;
}

void
systemd_delete()
{
       	assert_return_silent(cfg->sd_bus);

        sd_bus_unref(cfg->sd_bus);
	cfg->sd_bus = NULL;
}

static sd_bus *
get_bus()
{
	int r;

	if (cfg->sd_bus_failed)
		return NULL;

	if (!cfg->sd_bus) {
		r = sd_bus_open_user(&cfg->sd_bus);
		if (r < 0) {
			error("failed to connect to user system bus: %s", strerror(-r));
			cfg->sd_bus_failed = true;
			return NULL;
		}
	}

	return cfg->sd_bus;
}

/*
 * Check if a systemd service is running.
 *
 * This is equivalent to (assuming service minecraft@world1):
 *   gdbus call --session 
 *	--dest org.freedesktop.systemd1 
 *	--object-path /org/freedesktop/systemd1/unit/minecraft_40world1_2eservice
 *	--method org.freedesktop.DBus.Properties.Get
 *		"org.freedesktop.systemd1.Unit" 
 *		"ActiveState"
 */
bool
systemd_service_running(struct server *server)
{
	sd_bus *bus = get_bus();
        sd_bus_error error = SD_BUS_ERROR_NULL;
	char *status = NULL;
	bool running = false;
	int r;

	assert_return(server && bus && server->systemd_service && server->systemd_obj, false);

	r = sd_bus_get_property_string(bus,
				       SYSTEMD_DBUS_SERVICE,
				       server->systemd_obj,
				       SYSTEMD_DBUS_INTERFACE,
				       "ActiveState",
				       &error,
				       &status);
        if (r < 0) {
                error("failed to get status for service %s (%s): %s",
		      server->systemd_service, server->systemd_obj, error.message);
		goto out;
        }

	if (streq(status, "active")) {
		running = true;
		debug(DBG_SYSD, "systemd service %s (%s) is active",
		      server->systemd_service, server->systemd_obj);
	} else
		debug(DBG_SYSD, "systemd service %s (%s) is not active",
		      server->systemd_service, server->systemd_obj);

out:
	xfree(status);
        sd_bus_error_free(&error);
	return running;
}

static bool
systemd_service_action(struct server *server, const char *action)
{
        sd_bus_error error = SD_BUS_ERROR_NULL;
        sd_bus_message *m = NULL;
	sd_bus *bus = get_bus();
	const char *path;
	bool performed = false;
	int r;

	assert_return(server && bus && server->systemd_service && server->systemd_obj && action, false);

        r = sd_bus_call_method(bus,
			       SYSTEMD_DBUS_SERVICE,
			       server->systemd_obj,
			       SYSTEMD_DBUS_INTERFACE,
			       action,
			       &error,
			       &m,
			       "s",
			       "fail");
        if (r < 0) {
                error("failed to perform action %s on systemd service %s: %s",
		      action, server->systemd_service, error.message);
		goto out;
        }

        r = sd_bus_message_read(m, "o", &path);
        if (r < 0) {
                error("failed to parse response message: %s", strerror(-r));
		goto out;
        }

        verbose("action %s queued for service %s",
		action, server->systemd_service);
	performed = true;

out:
        sd_bus_error_free(&error);
        sd_bus_message_unref(m);
	return performed;
}

/*
 * Stop systemd service.
 *
 * This is equivalent to (assuming service minecraft@world1):
 *   gdbus call --session 
 *	--dest org.freedesktop.systemd1 
 *	--object-path /org/freedesktop/systemd1/unit/minecraft_40world1_2eservice
 *	--method org.freedesktop.systemd1.Unit.Stop "fail"
 */
bool
systemd_service_stop(struct server *server)
{
	assert_return(server, false);

	return systemd_service_action(server, "Stop");
}

/*
 * Start systemd service.
 *
 * This is equivalent to (assuming service minecraft@world1):
 *   gdbus call --session 
 *	--dest org.freedesktop.systemd1 
 *	--object-path /org/freedesktop/systemd1/unit/minecraft_40world1_2eservice
 *	--method org.freedesktop.systemd1.Unit.Start "fail"
 */
bool
systemd_service_start(struct server *server)
{
	assert_return(server, false);

	return systemd_service_action(server, "Start");
}