summaryrefslogtreecommitdiff
path: root/shared/systemd.c
blob: 4edc055fee3ab6f5d08ededfec228379e84bbcff (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
/* SPDX-License-Identifier: GPL-2.0 */
#include <string.h>
#include <stdlib.h>
#include <systemd/sd-bus.h>
#include <errno.h>

#include "utils.h"
#include "config-parser.h"
#include "systemd.h"

static sd_bus *bus = NULL;
static bool bus_failed = false;

void systemd_delete()
{
	assert_return_silent(bus);

	sd_bus_unref(bus);
	bus = NULL;
	bus_failed = false;
}

static sd_bus *get_bus()
{
	int r;

	if (bus_failed)
		return NULL;

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

	return bus;
}

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

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

again:
	r = sd_bus_get_property_string(bus, SYSTEMD_DBUS_SERVICE,
				       scfg->systemd_obj,
				       SYSTEMD_DBUS_INTERFACE, "ActiveState",
				       &sderror, &status);
	if (r < 0) {
		if (sd_bus_error_get_errno(&sderror) == EINTR)
			goto again;

		debug(DBG_SYSD, "failed to get status for server %s (%s): %s",
		      scfg->systemd_service, scfg->systemd_obj, sderror.message);
		*error = "failed to get service status";

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

	} else {
		debug(DBG_SYSD, "systemd service %s (%s) is not active (%s)",
		      scfg->systemd_service, scfg->systemd_obj, status);
		*error = "service not active";
	}

	free(status);
	sd_bus_error_free(&sderror);
	return running;
}

static bool systemd_service_action(struct server_config *scfg,
				   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(bus && scfg && scfg->systemd_service &&
		      scfg->systemd_obj && action, false);

again:
	r = sd_bus_call_method(bus, SYSTEMD_DBUS_SERVICE, scfg->systemd_obj,
			       SYSTEMD_DBUS_INTERFACE, action, &error, &m, "s",
			       "fail");
	if (r < 0) {
		if (sd_bus_error_get_errno(&error) == EINTR)
			goto again;

		error("failed to perform action %s on systemd service %s: %s",
		      action, scfg->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,
		scfg->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 mc@world1):
 *   gdbus call --session
 *	--dest org.freedesktop.systemd1
 *	--object-path /org/freedesktop/systemd1/unit/mc_40world1_2eservice
 *	--method org.freedesktop.systemd1.Unit.Stop "fail"
 */
bool systemd_service_stop(struct server_config *scfg)
{
	assert_return(scfg, false);

	return systemd_service_action(scfg, "Stop");
}

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

	return systemd_service_action(scfg, "Start");
}