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