]>
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" | |
4139c1b2 | 26 | #include "dbus-unit.h" |
398ef8ba | 27 | #include "bus-errors.h" |
ea430986 | 28 | |
4288f619 LP |
29 | const char bus_unit_interface[] = BUS_UNIT_INTERFACE; |
30 | ||
4139c1b2 LP |
31 | int bus_unit_append_names(Manager *m, DBusMessageIter *i, const char *property, void *data) { |
32 | char *t; | |
33 | Iterator j; | |
34 | DBusMessageIter sub; | |
35 | Unit *u = data; | |
36 | ||
37 | if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub)) | |
38 | return -ENOMEM; | |
39 | ||
40 | SET_FOREACH(t, u->meta.names, j) | |
41 | if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &t)) | |
42 | return -ENOMEM; | |
43 | ||
44 | if (!dbus_message_iter_close_container(i, &sub)) | |
45 | return -ENOMEM; | |
46 | ||
47 | return 0; | |
48 | } | |
49 | ||
5301be81 LP |
50 | int bus_unit_append_dependencies(Manager *m, DBusMessageIter *i, const char *property, void *data) { |
51 | Unit *u; | |
52 | Iterator j; | |
53 | DBusMessageIter sub; | |
54 | Set *s = data; | |
55 | ||
56 | if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub)) | |
57 | return -ENOMEM; | |
58 | ||
59 | SET_FOREACH(u, s, j) | |
60 | if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &u->meta.id)) | |
61 | return -ENOMEM; | |
62 | ||
63 | if (!dbus_message_iter_close_container(i, &sub)) | |
64 | return -ENOMEM; | |
65 | ||
66 | return 0; | |
67 | } | |
68 | ||
4139c1b2 | 69 | int bus_unit_append_description(Manager *m, DBusMessageIter *i, const char *property, void *data) { |
ea430986 LP |
70 | Unit *u = data; |
71 | const char *d; | |
72 | ||
73 | assert(m); | |
74 | assert(i); | |
75 | assert(property); | |
76 | assert(u); | |
77 | ||
78 | d = unit_description(u); | |
79 | ||
80 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d)) | |
81 | return -ENOMEM; | |
82 | ||
83 | return 0; | |
84 | } | |
85 | ||
6f4706b7 | 86 | DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_unit_append_load_state, unit_load_state, UnitLoadState); |
ea430986 | 87 | |
4139c1b2 | 88 | int bus_unit_append_active_state(Manager *m, DBusMessageIter *i, const char *property, void *data) { |
ea430986 LP |
89 | Unit *u = data; |
90 | const char *state; | |
91 | ||
92 | assert(m); | |
93 | assert(i); | |
94 | assert(property); | |
95 | assert(u); | |
96 | ||
97 | state = unit_active_state_to_string(unit_active_state(u)); | |
98 | ||
99 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state)) | |
100 | return -ENOMEM; | |
101 | ||
102 | return 0; | |
103 | } | |
104 | ||
4139c1b2 | 105 | int bus_unit_append_sub_state(Manager *m, DBusMessageIter *i, const char *property, void *data) { |
10a94420 LP |
106 | Unit *u = data; |
107 | const char *state; | |
108 | ||
109 | assert(m); | |
110 | assert(i); | |
111 | assert(property); | |
112 | assert(u); | |
113 | ||
114 | state = unit_sub_state_to_string(u); | |
115 | ||
116 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state)) | |
117 | return -ENOMEM; | |
118 | ||
119 | return 0; | |
120 | } | |
121 | ||
4139c1b2 | 122 | int bus_unit_append_can_start(Manager *m, DBusMessageIter *i, const char *property, void *data) { |
38131695 LP |
123 | Unit *u = data; |
124 | dbus_bool_t b; | |
125 | ||
126 | assert(m); | |
127 | assert(i); | |
128 | assert(property); | |
129 | assert(u); | |
130 | ||
064f51fa LP |
131 | b = unit_can_start(u) && |
132 | !u->meta.only_by_dependency; | |
38131695 LP |
133 | |
134 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b)) | |
135 | return -ENOMEM; | |
136 | ||
137 | return 0; | |
138 | } | |
139 | ||
4139c1b2 | 140 | int bus_unit_append_can_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) { |
38131695 LP |
141 | Unit *u = data; |
142 | dbus_bool_t b; | |
143 | ||
144 | assert(m); | |
145 | assert(i); | |
146 | assert(property); | |
147 | assert(u); | |
148 | ||
4139c1b2 | 149 | b = unit_can_reload(u); |
38131695 LP |
150 | |
151 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b)) | |
152 | return -ENOMEM; | |
153 | ||
154 | return 0; | |
155 | } | |
156 | ||
4139c1b2 | 157 | int bus_unit_append_job(Manager *m, DBusMessageIter *i, const char *property, void *data) { |
38131695 LP |
158 | Unit *u = data; |
159 | DBusMessageIter sub; | |
160 | char *p; | |
161 | ||
162 | assert(m); | |
163 | assert(i); | |
164 | assert(property); | |
165 | assert(u); | |
166 | ||
167 | if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub)) | |
168 | return -ENOMEM; | |
169 | ||
170 | if (u->meta.job) { | |
171 | ||
172 | if (!(p = job_dbus_path(u->meta.job))) | |
173 | return -ENOMEM; | |
174 | ||
175 | if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u->meta.job->id) || | |
86ad3bc1 | 176 | !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) { |
38131695 LP |
177 | free(p); |
178 | return -ENOMEM; | |
179 | } | |
180 | } else { | |
181 | uint32_t id = 0; | |
182 | ||
183 | /* No job, so let's fill in some placeholder | |
184 | * data. Since we need to fill in a valid path we | |
185 | * simple point to ourselves. */ | |
186 | ||
187 | if (!(p = unit_dbus_path(u))) | |
188 | return -ENOMEM; | |
189 | ||
190 | if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &id) || | |
86ad3bc1 | 191 | !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) { |
38131695 LP |
192 | free(p); |
193 | return -ENOMEM; | |
194 | } | |
195 | } | |
196 | ||
197 | free(p); | |
198 | ||
199 | if (!dbus_message_iter_close_container(i, &sub)) | |
200 | return -ENOMEM; | |
201 | ||
202 | return 0; | |
203 | } | |
204 | ||
4139c1b2 LP |
205 | int bus_unit_append_default_cgroup(Manager *m, DBusMessageIter *i, const char *property, void *data) { |
206 | Unit *u = data; | |
207 | char *t; | |
208 | CGroupBonding *cgb; | |
209 | bool success; | |
ea430986 | 210 | |
4139c1b2 LP |
211 | assert(m); |
212 | assert(i); | |
213 | assert(property); | |
214 | assert(u); | |
215 | ||
216 | if ((cgb = unit_get_default_cgroup(u))) { | |
217 | if (!(t = cgroup_bonding_to_string(cgb))) | |
218 | return -ENOMEM; | |
219 | } else | |
220 | t = (char*) ""; | |
ea430986 | 221 | |
4139c1b2 LP |
222 | success = dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t); |
223 | ||
224 | if (cgb) | |
225 | free(t); | |
226 | ||
227 | return success ? 0 : -ENOMEM; | |
228 | } | |
229 | ||
230 | int bus_unit_append_cgroups(Manager *m, DBusMessageIter *i, const char *property, void *data) { | |
231 | Unit *u = data; | |
232 | CGroupBonding *cgb; | |
233 | DBusMessageIter sub; | |
234 | ||
235 | if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub)) | |
236 | return -ENOMEM; | |
237 | ||
238 | LIST_FOREACH(by_unit, cgb, u->meta.cgroup_bondings) { | |
239 | char *t; | |
240 | bool success; | |
241 | ||
242 | if (!(t = cgroup_bonding_to_string(cgb))) | |
243 | return -ENOMEM; | |
244 | ||
245 | success = dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &t); | |
246 | free(t); | |
247 | ||
248 | if (!success) | |
249 | return -ENOMEM; | |
250 | } | |
251 | ||
252 | if (!dbus_message_iter_close_container(i, &sub)) | |
253 | return -ENOMEM; | |
254 | ||
255 | return 0; | |
256 | } | |
257 | ||
45fb0699 LP |
258 | int bus_unit_append_need_daemon_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) { |
259 | Unit *u = data; | |
260 | dbus_bool_t b; | |
261 | ||
262 | assert(m); | |
263 | assert(i); | |
264 | assert(property); | |
265 | assert(u); | |
266 | ||
267 | b = unit_need_daemon_reload(u); | |
268 | ||
269 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b)) | |
270 | return -ENOMEM; | |
271 | ||
272 | return 0; | |
273 | } | |
274 | ||
5e8d1c9a | 275 | static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *connection, DBusMessage *message) { |
b548631a LP |
276 | DBusMessage *reply = NULL; |
277 | Manager *m = u->meta.manager; | |
278 | DBusError error; | |
279 | JobType job_type = _JOB_TYPE_INVALID; | |
c87eba54 | 280 | char *path = NULL; |
6f28c033 | 281 | bool reload_if_possible = false; |
b548631a LP |
282 | |
283 | dbus_error_init(&error); | |
284 | ||
285 | if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Start")) | |
286 | job_type = JOB_START; | |
287 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Stop")) | |
288 | job_type = JOB_STOP; | |
289 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Reload")) | |
290 | job_type = JOB_RELOAD; | |
291 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Restart")) | |
292 | job_type = JOB_RESTART; | |
9a1ac7b9 LP |
293 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "TryRestart")) |
294 | job_type = JOB_TRY_RESTART; | |
6f28c033 LP |
295 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrRestart")) { |
296 | reload_if_possible = true; | |
297 | job_type = JOB_RESTART; | |
298 | } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrTryRestart")) { | |
299 | reload_if_possible = true; | |
300 | job_type = JOB_TRY_RESTART; | |
301 | } else if (UNIT_VTABLE(u)->bus_message_handler) | |
5e8d1c9a | 302 | return UNIT_VTABLE(u)->bus_message_handler(u, connection, message); |
b548631a | 303 | else |
4139c1b2 | 304 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
b548631a LP |
305 | |
306 | if (job_type != _JOB_TYPE_INVALID) { | |
307 | const char *smode; | |
308 | JobMode mode; | |
309 | Job *j; | |
310 | int r; | |
b548631a | 311 | |
398ef8ba LP |
312 | if (job_type == JOB_START && u->meta.only_by_dependency) { |
313 | dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Unit may be activated by dependency only."); | |
314 | return bus_send_error_reply(m, connection, message, &error, -EPERM); | |
315 | } | |
bc0f8771 | 316 | |
b548631a LP |
317 | if (!dbus_message_get_args( |
318 | message, | |
319 | &error, | |
320 | DBUS_TYPE_STRING, &smode, | |
321 | DBUS_TYPE_INVALID)) | |
5e8d1c9a | 322 | return bus_send_error_reply(m, connection, message, &error, -EINVAL); |
b548631a | 323 | |
6f28c033 LP |
324 | if (reload_if_possible && unit_can_reload(u)) { |
325 | if (job_type == JOB_RESTART) | |
326 | job_type = JOB_RELOAD_OR_START; | |
327 | else if (job_type == JOB_TRY_RESTART) | |
328 | job_type = JOB_RELOAD; | |
329 | } | |
330 | ||
398ef8ba LP |
331 | if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) { |
332 | dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode); | |
333 | return bus_send_error_reply(m, connection, message, &error, -EINVAL); | |
334 | } | |
b548631a | 335 | |
398ef8ba LP |
336 | if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0) |
337 | return bus_send_error_reply(m, connection, message, &error, r); | |
b548631a LP |
338 | |
339 | if (!(reply = dbus_message_new_method_return(message))) | |
340 | goto oom; | |
341 | ||
342 | if (!(path = job_dbus_path(j))) | |
343 | goto oom; | |
344 | ||
345 | if (!dbus_message_append_args( | |
346 | reply, | |
347 | DBUS_TYPE_OBJECT_PATH, &path, | |
348 | DBUS_TYPE_INVALID)) | |
349 | goto oom; | |
350 | } | |
351 | ||
c87eba54 LP |
352 | free(path); |
353 | ||
b548631a | 354 | if (reply) { |
5e8d1c9a | 355 | if (!dbus_connection_send(connection, reply, NULL)) |
b548631a LP |
356 | goto oom; |
357 | ||
358 | dbus_message_unref(reply); | |
359 | } | |
360 | ||
361 | return DBUS_HANDLER_RESULT_HANDLED; | |
362 | ||
363 | oom: | |
c87eba54 LP |
364 | free(path); |
365 | ||
b548631a LP |
366 | if (reply) |
367 | dbus_message_unref(reply); | |
368 | ||
369 | dbus_error_free(&error); | |
370 | ||
371 | return DBUS_HANDLER_RESULT_NEED_MEMORY; | |
ea430986 LP |
372 | } |
373 | ||
5e8d1c9a | 374 | static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage *message, void *data) { |
ea430986 LP |
375 | Manager *m = data; |
376 | Unit *u; | |
377 | int r; | |
378 | ||
379 | assert(connection); | |
380 | assert(message); | |
381 | assert(m); | |
382 | ||
ea430986 LP |
383 | if ((r = manager_get_unit_from_dbus_path(m, dbus_message_get_path(message), &u)) < 0) { |
384 | ||
385 | if (r == -ENOMEM) | |
386 | return DBUS_HANDLER_RESULT_NEED_MEMORY; | |
387 | ||
388 | if (r == -ENOENT) | |
389 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | |
390 | ||
5e8d1c9a | 391 | return bus_send_error_reply(m, connection, message, NULL, r); |
ea430986 LP |
392 | } |
393 | ||
5e8d1c9a | 394 | return bus_unit_message_dispatch(u, connection, message); |
ea430986 LP |
395 | } |
396 | ||
397 | const DBusObjectPathVTable bus_unit_vtable = { | |
398 | .message_function = bus_unit_message_handler | |
399 | }; | |
c1e1601e LP |
400 | |
401 | void bus_unit_send_change_signal(Unit *u) { | |
402 | char *p = NULL; | |
403 | DBusMessage *m = NULL; | |
404 | ||
405 | assert(u); | |
c1e1601e | 406 | |
c0bd0cf7 LP |
407 | if (u->meta.in_dbus_queue) { |
408 | LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta); | |
409 | u->meta.in_dbus_queue = false; | |
410 | } | |
c1e1601e | 411 | |
a567261a | 412 | if (!bus_has_subscriber(u->meta.manager)) { |
94b6dfa2 | 413 | u->meta.sent_dbus_new_signal = true; |
c1e1601e | 414 | return; |
94b6dfa2 | 415 | } |
c1e1601e LP |
416 | |
417 | if (!(p = unit_dbus_path(u))) | |
418 | goto oom; | |
419 | ||
420 | if (u->meta.sent_dbus_new_signal) { | |
421 | /* Send a change signal */ | |
422 | ||
423 | if (!(m = dbus_message_new_signal(p, "org.freedesktop.systemd1.Unit", "Changed"))) | |
424 | goto oom; | |
425 | } else { | |
c1e1601e LP |
426 | /* Send a new signal */ |
427 | ||
701cc384 | 428 | if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitNew"))) |
c1e1601e LP |
429 | goto oom; |
430 | ||
c1e1601e | 431 | if (!dbus_message_append_args(m, |
9e2f7c11 | 432 | DBUS_TYPE_STRING, &u->meta.id, |
c1e1601e LP |
433 | DBUS_TYPE_OBJECT_PATH, &p, |
434 | DBUS_TYPE_INVALID)) | |
435 | goto oom; | |
436 | } | |
437 | ||
5e8d1c9a | 438 | if (bus_broadcast(u->meta.manager, m) < 0) |
c1e1601e LP |
439 | goto oom; |
440 | ||
441 | free(p); | |
442 | dbus_message_unref(m); | |
443 | ||
444 | u->meta.sent_dbus_new_signal = true; | |
445 | ||
446 | return; | |
447 | ||
448 | oom: | |
449 | free(p); | |
450 | ||
451 | if (m) | |
452 | dbus_message_unref(m); | |
453 | ||
454 | log_error("Failed to allocate unit change/new signal."); | |
455 | } | |
456 | ||
457 | void bus_unit_send_removed_signal(Unit *u) { | |
458 | char *p = NULL; | |
459 | DBusMessage *m = NULL; | |
c1e1601e LP |
460 | |
461 | assert(u); | |
462 | ||
a567261a | 463 | if (!bus_has_subscriber(u->meta.manager)) |
c1e1601e LP |
464 | return; |
465 | ||
7535cc78 LP |
466 | if (!u->meta.sent_dbus_new_signal) |
467 | bus_unit_send_change_signal(u); | |
468 | ||
c1e1601e LP |
469 | if (!(p = unit_dbus_path(u))) |
470 | goto oom; | |
471 | ||
701cc384 | 472 | if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitRemoved"))) |
c1e1601e LP |
473 | goto oom; |
474 | ||
c1e1601e | 475 | if (!dbus_message_append_args(m, |
9e2f7c11 | 476 | DBUS_TYPE_STRING, &u->meta.id, |
c1e1601e LP |
477 | DBUS_TYPE_OBJECT_PATH, &p, |
478 | DBUS_TYPE_INVALID)) | |
479 | goto oom; | |
480 | ||
5e8d1c9a | 481 | if (bus_broadcast(u->meta.manager, m) < 0) |
c1e1601e LP |
482 | goto oom; |
483 | ||
484 | free(p); | |
485 | dbus_message_unref(m); | |
486 | ||
487 | return; | |
488 | ||
489 | oom: | |
490 | free(p); | |
491 | ||
492 | if (m) | |
493 | dbus_message_unref(m); | |
494 | ||
495 | log_error("Failed to allocate unit remove signal."); | |
496 | } |