]>
Commit | Line | Data |
---|---|---|
ea430986 LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | ||
a7334b09 LP |
3 | /*** |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
ea430986 LP |
22 | #include <errno.h> |
23 | ||
24 | #include "dbus.h" | |
25 | #include "log.h" | |
26 | ||
27 | static const char introspection[] = | |
28 | DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE | |
29 | "<node>" | |
ea430986 | 30 | " <interface name=\"org.freedesktop.systemd1.Unit\">" |
b548631a LP |
31 | " <method name=\"Start\">" |
32 | " <arg name=\"mode\" type=\"s\" direction=\"in\"/>" | |
33 | " <arg name=\"job\" type=\"o\" direction=\"out\"/>" | |
34 | " </method>" | |
35 | " <method name=\"Stop\">" | |
36 | " <arg name=\"mode\" type=\"s\" direction=\"in\"/>" | |
37 | " <arg name=\"job\" type=\"o\" direction=\"out\"/>" | |
38 | " </method>" | |
39 | " <method name=\"Restart\">" | |
40 | " <arg name=\"mode\" type=\"s\" direction=\"in\"/>" | |
41 | " <arg name=\"job\" type=\"o\" direction=\"out\"/>" | |
42 | " </method>" | |
43 | " <method name=\"Reload\">" | |
44 | " <arg name=\"mode\" type=\"s\" direction=\"in\"/>" | |
45 | " <arg name=\"job\" type=\"o\" direction=\"out\"/>" | |
46 | " </method>" | |
c1e1601e | 47 | " <signal name=\"Changed\"/>" |
ea430986 LP |
48 | " <property name=\"Id\" type=\"s\" access=\"read\"/>" |
49 | " <property name=\"Description\" type=\"s\" access=\"read\"/>" | |
50 | " <property name=\"LoadState\" type=\"s\" access=\"read\"/>" | |
51 | " <property name=\"ActiveState\" type=\"s\" access=\"read\"/>" | |
10a94420 | 52 | " <property name=\"SubState\" type=\"s\" access=\"read\"/>" |
6be1e7d5 | 53 | " <property name=\"FragmentPath\" type=\"s\" access=\"read\"/>" |
38131695 LP |
54 | " <property name=\"ActiveEnterTimestamp\" type=\"t\" access=\"read\"/>" |
55 | " <property name=\"ActiveExitTimestamp\" type=\"t\" access=\"read\"/>" | |
56 | " <property name=\"CanReload\" type=\"b\" access=\"read\"/>" | |
57 | " <property name=\"CanStart\" type=\"b\" access=\"read\"/>" | |
58 | " <property name=\"Job\" type=\"(uo)\" access=\"read\"/>" | |
ea430986 LP |
59 | " </interface>" |
60 | BUS_PROPERTIES_INTERFACE | |
61 | BUS_INTROSPECTABLE_INTERFACE | |
62 | "</node>"; | |
63 | ||
ea430986 LP |
64 | static int bus_unit_append_description(Manager *m, DBusMessageIter *i, const char *property, void *data) { |
65 | Unit *u = data; | |
66 | const char *d; | |
67 | ||
68 | assert(m); | |
69 | assert(i); | |
70 | assert(property); | |
71 | assert(u); | |
72 | ||
73 | d = unit_description(u); | |
74 | ||
75 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d)) | |
76 | return -ENOMEM; | |
77 | ||
78 | return 0; | |
79 | } | |
80 | ||
6f4706b7 | 81 | DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_unit_append_load_state, unit_load_state, UnitLoadState); |
ea430986 LP |
82 | |
83 | static int bus_unit_append_active_state(Manager *m, DBusMessageIter *i, const char *property, void *data) { | |
84 | Unit *u = data; | |
85 | const char *state; | |
86 | ||
87 | assert(m); | |
88 | assert(i); | |
89 | assert(property); | |
90 | assert(u); | |
91 | ||
92 | state = unit_active_state_to_string(unit_active_state(u)); | |
93 | ||
94 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state)) | |
95 | return -ENOMEM; | |
96 | ||
97 | return 0; | |
98 | } | |
99 | ||
10a94420 LP |
100 | static int bus_unit_append_sub_state(Manager *m, DBusMessageIter *i, const char *property, void *data) { |
101 | Unit *u = data; | |
102 | const char *state; | |
103 | ||
104 | assert(m); | |
105 | assert(i); | |
106 | assert(property); | |
107 | assert(u); | |
108 | ||
109 | state = unit_sub_state_to_string(u); | |
110 | ||
111 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state)) | |
112 | return -ENOMEM; | |
113 | ||
114 | return 0; | |
115 | } | |
116 | ||
38131695 LP |
117 | static int bus_unit_append_can_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) { |
118 | Unit *u = data; | |
119 | dbus_bool_t b; | |
120 | ||
121 | assert(m); | |
122 | assert(i); | |
123 | assert(property); | |
124 | assert(u); | |
125 | ||
126 | b = unit_can_reload(u); | |
127 | ||
128 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b)) | |
129 | return -ENOMEM; | |
130 | ||
131 | return 0; | |
132 | } | |
133 | ||
134 | static int bus_unit_append_can_start(Manager *m, DBusMessageIter *i, const char *property, void *data) { | |
135 | Unit *u = data; | |
136 | dbus_bool_t b; | |
137 | ||
138 | assert(m); | |
139 | assert(i); | |
140 | assert(property); | |
141 | assert(u); | |
142 | ||
143 | b = unit_can_start(u); | |
144 | ||
145 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b)) | |
146 | return -ENOMEM; | |
147 | ||
148 | return 0; | |
149 | } | |
150 | ||
151 | static int bus_unit_append_job(Manager *m, DBusMessageIter *i, const char *property, void *data) { | |
152 | Unit *u = data; | |
153 | DBusMessageIter sub; | |
154 | char *p; | |
155 | ||
156 | assert(m); | |
157 | assert(i); | |
158 | assert(property); | |
159 | assert(u); | |
160 | ||
161 | if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub)) | |
162 | return -ENOMEM; | |
163 | ||
164 | if (u->meta.job) { | |
165 | ||
166 | if (!(p = job_dbus_path(u->meta.job))) | |
167 | return -ENOMEM; | |
168 | ||
169 | if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u->meta.job->id) || | |
86ad3bc1 | 170 | !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) { |
38131695 LP |
171 | free(p); |
172 | return -ENOMEM; | |
173 | } | |
174 | } else { | |
175 | uint32_t id = 0; | |
176 | ||
177 | /* No job, so let's fill in some placeholder | |
178 | * data. Since we need to fill in a valid path we | |
179 | * simple point to ourselves. */ | |
180 | ||
181 | if (!(p = unit_dbus_path(u))) | |
182 | return -ENOMEM; | |
183 | ||
184 | if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &id) || | |
86ad3bc1 | 185 | !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) { |
38131695 LP |
186 | free(p); |
187 | return -ENOMEM; | |
188 | } | |
189 | } | |
190 | ||
191 | free(p); | |
192 | ||
193 | if (!dbus_message_iter_close_container(i, &sub)) | |
194 | return -ENOMEM; | |
195 | ||
196 | return 0; | |
197 | } | |
198 | ||
ea430986 LP |
199 | static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusMessage *message) { |
200 | ||
201 | const BusProperty properties[] = { | |
9e2f7c11 | 202 | { "org.freedesktop.systemd1.Unit", "Id", bus_property_append_string, "s", u->meta.id }, |
38131695 | 203 | { "org.freedesktop.systemd1.Unit", "Description", bus_unit_append_description, "s", u }, |
6f4706b7 | 204 | { "org.freedesktop.systemd1.Unit", "LoadState", bus_unit_append_load_state, "s", &u->meta.load_state }, |
38131695 | 205 | { "org.freedesktop.systemd1.Unit", "ActiveState", bus_unit_append_active_state, "s", u }, |
10a94420 | 206 | { "org.freedesktop.systemd1.Unit", "SubState", bus_unit_append_sub_state, "s", u }, |
6be1e7d5 | 207 | { "org.freedesktop.systemd1.Unit", "FragmentPath", bus_property_append_string, "s", u->meta.fragment_path }, |
38131695 LP |
208 | { "org.freedesktop.systemd1.Unit", "ActiveEnterTimestamp", bus_property_append_uint64, "t", &u->meta.active_enter_timestamp }, |
209 | { "org.freedesktop.systemd1.Unit", "ActiveExitTimestamp", bus_property_append_uint64, "t", &u->meta.active_exit_timestamp }, | |
210 | { "org.freedesktop.systemd1.Unit", "CanReload", bus_unit_append_can_reload, "b", u }, | |
211 | { "org.freedesktop.systemd1.Unit", "CanStart", bus_unit_append_can_start, "b", u }, | |
86ad3bc1 | 212 | { "org.freedesktop.systemd1.Unit", "Job", bus_unit_append_job, "(uo)", u }, |
ea430986 LP |
213 | { NULL, NULL, NULL, NULL, NULL } |
214 | }; | |
215 | ||
b548631a LP |
216 | DBusMessage *reply = NULL; |
217 | Manager *m = u->meta.manager; | |
218 | DBusError error; | |
219 | JobType job_type = _JOB_TYPE_INVALID; | |
220 | ||
221 | dbus_error_init(&error); | |
222 | ||
223 | if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Start")) | |
224 | job_type = JOB_START; | |
225 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Stop")) | |
226 | job_type = JOB_STOP; | |
227 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Reload")) | |
228 | job_type = JOB_RELOAD; | |
229 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Restart")) | |
230 | job_type = JOB_RESTART; | |
231 | else | |
232 | return bus_default_message_handler(u->meta.manager, message, introspection, properties); | |
233 | ||
234 | if (job_type != _JOB_TYPE_INVALID) { | |
235 | const char *smode; | |
236 | JobMode mode; | |
237 | Job *j; | |
238 | int r; | |
239 | char *path; | |
240 | ||
241 | if (!dbus_message_get_args( | |
242 | message, | |
243 | &error, | |
244 | DBUS_TYPE_STRING, &smode, | |
245 | DBUS_TYPE_INVALID)) | |
246 | return bus_send_error_reply(m, message, &error, -EINVAL); | |
247 | ||
248 | if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) | |
249 | return bus_send_error_reply(m, message, NULL, -EINVAL); | |
250 | ||
251 | if ((r = manager_add_job(m, job_type, u, mode, true, &j)) < 0) | |
252 | return bus_send_error_reply(m, message, NULL, r); | |
253 | ||
254 | if (!(reply = dbus_message_new_method_return(message))) | |
255 | goto oom; | |
256 | ||
257 | if (!(path = job_dbus_path(j))) | |
258 | goto oom; | |
259 | ||
260 | if (!dbus_message_append_args( | |
261 | reply, | |
262 | DBUS_TYPE_OBJECT_PATH, &path, | |
263 | DBUS_TYPE_INVALID)) | |
264 | goto oom; | |
265 | } | |
266 | ||
267 | if (reply) { | |
f278026d | 268 | if (!dbus_connection_send(m->api_bus, reply, NULL)) |
b548631a LP |
269 | goto oom; |
270 | ||
271 | dbus_message_unref(reply); | |
272 | } | |
273 | ||
274 | return DBUS_HANDLER_RESULT_HANDLED; | |
275 | ||
276 | oom: | |
277 | if (reply) | |
278 | dbus_message_unref(reply); | |
279 | ||
280 | dbus_error_free(&error); | |
281 | ||
282 | return DBUS_HANDLER_RESULT_NEED_MEMORY; | |
ea430986 LP |
283 | } |
284 | ||
285 | static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage *message, void *data) { | |
286 | Manager *m = data; | |
287 | Unit *u; | |
288 | int r; | |
289 | ||
290 | assert(connection); | |
291 | assert(message); | |
292 | assert(m); | |
293 | ||
294 | log_debug("Got D-Bus request: %s.%s() on %s", | |
295 | dbus_message_get_interface(message), | |
296 | dbus_message_get_member(message), | |
297 | dbus_message_get_path(message)); | |
298 | ||
299 | if ((r = manager_get_unit_from_dbus_path(m, dbus_message_get_path(message), &u)) < 0) { | |
300 | ||
301 | if (r == -ENOMEM) | |
302 | return DBUS_HANDLER_RESULT_NEED_MEMORY; | |
303 | ||
304 | if (r == -ENOENT) | |
305 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | |
306 | ||
307 | return bus_send_error_reply(m, message, NULL, r); | |
308 | } | |
309 | ||
310 | return bus_unit_message_dispatch(u, message); | |
311 | } | |
312 | ||
313 | const DBusObjectPathVTable bus_unit_vtable = { | |
314 | .message_function = bus_unit_message_handler | |
315 | }; | |
c1e1601e LP |
316 | |
317 | void bus_unit_send_change_signal(Unit *u) { | |
318 | char *p = NULL; | |
319 | DBusMessage *m = NULL; | |
320 | ||
321 | assert(u); | |
322 | assert(u->meta.in_dbus_queue); | |
323 | ||
324 | LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta); | |
325 | u->meta.in_dbus_queue = false; | |
326 | ||
327 | if (set_isempty(u->meta.manager->subscribed)) | |
328 | return; | |
329 | ||
330 | if (!(p = unit_dbus_path(u))) | |
331 | goto oom; | |
332 | ||
333 | if (u->meta.sent_dbus_new_signal) { | |
334 | /* Send a change signal */ | |
335 | ||
336 | if (!(m = dbus_message_new_signal(p, "org.freedesktop.systemd1.Unit", "Changed"))) | |
337 | goto oom; | |
338 | } else { | |
c1e1601e LP |
339 | /* Send a new signal */ |
340 | ||
341 | if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1", "UnitNew"))) | |
342 | goto oom; | |
343 | ||
c1e1601e | 344 | if (!dbus_message_append_args(m, |
9e2f7c11 | 345 | DBUS_TYPE_STRING, &u->meta.id, |
c1e1601e LP |
346 | DBUS_TYPE_OBJECT_PATH, &p, |
347 | DBUS_TYPE_INVALID)) | |
348 | goto oom; | |
349 | } | |
350 | ||
f278026d | 351 | if (!dbus_connection_send(u->meta.manager->api_bus, m, NULL)) |
c1e1601e LP |
352 | goto oom; |
353 | ||
354 | free(p); | |
355 | dbus_message_unref(m); | |
356 | ||
357 | u->meta.sent_dbus_new_signal = true; | |
358 | ||
359 | return; | |
360 | ||
361 | oom: | |
362 | free(p); | |
363 | ||
364 | if (m) | |
365 | dbus_message_unref(m); | |
366 | ||
367 | log_error("Failed to allocate unit change/new signal."); | |
368 | } | |
369 | ||
370 | void bus_unit_send_removed_signal(Unit *u) { | |
371 | char *p = NULL; | |
372 | DBusMessage *m = NULL; | |
c1e1601e LP |
373 | |
374 | assert(u); | |
375 | ||
376 | if (set_isempty(u->meta.manager->subscribed) || !u->meta.sent_dbus_new_signal) | |
377 | return; | |
378 | ||
379 | if (!(p = unit_dbus_path(u))) | |
380 | goto oom; | |
381 | ||
382 | if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1", "UnitRemoved"))) | |
383 | goto oom; | |
384 | ||
c1e1601e | 385 | if (!dbus_message_append_args(m, |
9e2f7c11 | 386 | DBUS_TYPE_STRING, &u->meta.id, |
c1e1601e LP |
387 | DBUS_TYPE_OBJECT_PATH, &p, |
388 | DBUS_TYPE_INVALID)) | |
389 | goto oom; | |
390 | ||
f278026d | 391 | if (!dbus_connection_send(u->meta.manager->api_bus, m, NULL)) |
c1e1601e LP |
392 | goto oom; |
393 | ||
394 | free(p); | |
395 | dbus_message_unref(m); | |
396 | ||
397 | return; | |
398 | ||
399 | oom: | |
400 | free(p); | |
401 | ||
402 | if (m) | |
403 | dbus_message_unref(m); | |
404 | ||
405 | log_error("Failed to allocate unit remove signal."); | |
406 | } |