summaryrefslogtreecommitdiff
path: root/hw/systemd.c
blob: 97d32ce20a57778691f14f192b7a40917c68fdd0 (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
/*
 * libdevice-node
 *
 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <hw/systemd.h>
#include <systemd/sd-bus.h>

#define SYSTEMD_DBUS_SERVICE "org.freedesktop.systemd1"
#define SYSTEMD_DBUS_PATH "/org/freedesktop/systemd1"
#define SYSTEMD_DBUS_MANAGER_IFACE "org.freedesktop.systemd1.Manager"

#define SYSTEMD_SOCKET_SUFFIX ".socket"
#define MAX_SOCKET_NAME 1024

#define SYSTEMD_SERVICE_SUFFIX ".service"
#define MAX_SERVICE_NAME 1024

#ifndef EXPORT
#define EXPORT	__attribute__ ((visibility("default")))
#endif

struct bus_ctx {
	const char *unit;
	sd_event *loop;
};

static int socket_started(sd_bus_message *m, void *userdata,
			  sd_bus_error *ret_error)
{
	struct bus_ctx *ctx = userdata;
	char *signal_unit;
	int ret;

	ret = sd_bus_message_read(m, "uoss", NULL, NULL, &signal_unit, NULL);
	if (ret < 0) {
		sd_event_exit(ctx->loop, ret);
		return 0;
	}

	if (!strcmp(signal_unit, ctx->unit))
		sd_event_exit(ctx->loop, 0);

	return 0;
}

static int systemd_unit_interface_sync(const char *method, const char *unit,
				       bool wait)
{
	sd_bus *bus = NULL;
	sd_event *loop = NULL;
	struct bus_ctx ctx;
	int ret;

	ret = sd_bus_open_system(&bus);
	if (ret < 0)
		return ret;

	if (wait) {
		ret = sd_event_new(&loop);
		if (ret < 0)
			goto unref_bus;

		ctx.loop = loop;
		ctx.unit = unit;

		ret = sd_bus_attach_event(bus, loop, SD_EVENT_PRIORITY_NORMAL);
		if (ret < 0)
			goto unref_loop;

		ret = sd_bus_add_match(bus, NULL,
				       "type='signal',"
				       "sender='" SYSTEMD_DBUS_SERVICE "',"
				       "interface='" SYSTEMD_DBUS_MANAGER_IFACE "',"
				       "member='JobRemoved',"
				       "path_namespace='" SYSTEMD_DBUS_PATH "'",
				       socket_started,
				       &ctx);
		if (ret < 0)
			goto unref_loop;
	}


	ret = sd_bus_call_method(bus,
			SYSTEMD_DBUS_SERVICE,
			SYSTEMD_DBUS_PATH,
			SYSTEMD_DBUS_MANAGER_IFACE,
			method,
			NULL,
			NULL,
			"ss",
			unit,
			"replace");
	if (ret < 0)
		goto unref_loop;

	if (wait)
		ret = sd_event_loop(loop);

unref_loop:
	if (wait)
		sd_event_unref(loop);
unref_bus:
	sd_bus_unref(bus);
	return ret;
}

EXPORT int systemd_start_service(const char *service_name)
{
	char unit[MAX_SERVICE_NAME];
	int ret;

	ret = snprintf(unit, sizeof(unit), "%s" SYSTEMD_SERVICE_SUFFIX,
		       service_name);
	if (ret < 0 || ret >= sizeof(unit))
		return -ENAMETOOLONG;

	return systemd_unit_interface_sync("StartUnit", unit, true);
}

EXPORT int systemd_stop_service(const char *service_name)
{
	char unit[MAX_SERVICE_NAME];
	int ret;

	ret = snprintf(unit, sizeof(unit), "%s" SYSTEMD_SERVICE_SUFFIX,
		       service_name);
	if (ret < 0 || ret >= sizeof(unit))
		return -ENAMETOOLONG;

	return systemd_unit_interface_sync("StopUnit", unit, false);
}

EXPORT int systemd_start_socket(const char *socket_name)
{
	char unit[MAX_SOCKET_NAME];
	int ret;

	ret = snprintf(unit, sizeof(unit), "%s" SYSTEMD_SOCKET_SUFFIX,
		       socket_name);
	if (ret < 0 || ret >= sizeof(unit))
		return -ENAMETOOLONG;


	return systemd_unit_interface_sync("StartUnit", unit, true);
}

EXPORT int systemd_stop_socket(const char *socket_name)
{
	char unit[MAX_SOCKET_NAME];
	int ret;

	ret = snprintf(unit, sizeof(unit), "%s" SYSTEMD_SOCKET_SUFFIX,
		       socket_name);
	if (ret < 0 || ret >= sizeof(unit))
		return -ENAMETOOLONG;

	return systemd_unit_interface_sync("StopUnit", unit, false);
}