]>
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 | ||
4139c1b2 | 131 | b = unit_can_start(u); |
38131695 LP |
132 | |
133 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b)) | |
134 | return -ENOMEM; | |
135 | ||
136 | return 0; | |
137 | } | |
138 | ||
4139c1b2 | 139 | int bus_unit_append_can_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) { |
38131695 LP |
140 | Unit *u = data; |
141 | dbus_bool_t b; | |
142 | ||
143 | assert(m); | |
144 | assert(i); | |
145 | assert(property); | |
146 | assert(u); | |
147 | ||
4139c1b2 | 148 | b = unit_can_reload(u); |
38131695 LP |
149 | |
150 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b)) | |
151 | return -ENOMEM; | |
152 | ||
153 | return 0; | |
154 | } | |
155 | ||
4139c1b2 | 156 | int bus_unit_append_job(Manager *m, DBusMessageIter *i, const char *property, void *data) { |
38131695 LP |
157 | Unit *u = data; |
158 | DBusMessageIter sub; | |
159 | char *p; | |
160 | ||
161 | assert(m); | |
162 | assert(i); | |
163 | assert(property); | |
164 | assert(u); | |
165 | ||
166 | if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub)) | |
167 | return -ENOMEM; | |
168 | ||
169 | if (u->meta.job) { | |
170 | ||
171 | if (!(p = job_dbus_path(u->meta.job))) | |
172 | return -ENOMEM; | |
173 | ||
174 | if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u->meta.job->id) || | |
86ad3bc1 | 175 | !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) { |
38131695 LP |
176 | free(p); |
177 | return -ENOMEM; | |
178 | } | |
179 | } else { | |
180 | uint32_t id = 0; | |
181 | ||
182 | /* No job, so let's fill in some placeholder | |
183 | * data. Since we need to fill in a valid path we | |
184 | * simple point to ourselves. */ | |
185 | ||
186 | if (!(p = unit_dbus_path(u))) | |
187 | return -ENOMEM; | |
188 | ||
189 | if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &id) || | |
86ad3bc1 | 190 | !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) { |
38131695 LP |
191 | free(p); |
192 | return -ENOMEM; | |
193 | } | |
194 | } | |
195 | ||
196 | free(p); | |
197 | ||
198 | if (!dbus_message_iter_close_container(i, &sub)) | |
199 | return -ENOMEM; | |
200 | ||
201 | return 0; | |
202 | } | |
203 | ||
4139c1b2 LP |
204 | int bus_unit_append_default_cgroup(Manager *m, DBusMessageIter *i, const char *property, void *data) { |
205 | Unit *u = data; | |
206 | char *t; | |
207 | CGroupBonding *cgb; | |
208 | bool success; | |
ea430986 | 209 | |
4139c1b2 LP |
210 | assert(m); |
211 | assert(i); | |
212 | assert(property); | |
213 | assert(u); | |
214 | ||
215 | if ((cgb = unit_get_default_cgroup(u))) { | |
216 | if (!(t = cgroup_bonding_to_string(cgb))) | |
217 | return -ENOMEM; | |
218 | } else | |
219 | t = (char*) ""; | |
ea430986 | 220 | |
4139c1b2 LP |
221 | success = dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t); |
222 | ||
223 | if (cgb) | |
224 | free(t); | |
225 | ||
226 | return success ? 0 : -ENOMEM; | |
227 | } | |
228 | ||
229 | int bus_unit_append_cgroups(Manager *m, DBusMessageIter *i, const char *property, void *data) { | |
230 | Unit *u = data; | |
231 | CGroupBonding *cgb; | |
232 | DBusMessageIter sub; | |
233 | ||
234 | if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub)) | |
235 | return -ENOMEM; | |
236 | ||
237 | LIST_FOREACH(by_unit, cgb, u->meta.cgroup_bondings) { | |
238 | char *t; | |
239 | bool success; | |
240 | ||
241 | if (!(t = cgroup_bonding_to_string(cgb))) | |
242 | return -ENOMEM; | |
243 | ||
244 | success = dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &t); | |
245 | free(t); | |
246 | ||
247 | if (!success) | |
248 | return -ENOMEM; | |
249 | } | |
250 | ||
251 | if (!dbus_message_iter_close_container(i, &sub)) | |
252 | return -ENOMEM; | |
253 | ||
254 | return 0; | |
255 | } | |
256 | ||
5e8d1c9a | 257 | static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *connection, DBusMessage *message) { |
b548631a LP |
258 | DBusMessage *reply = NULL; |
259 | Manager *m = u->meta.manager; | |
260 | DBusError error; | |
261 | JobType job_type = _JOB_TYPE_INVALID; | |
c87eba54 | 262 | char *path = NULL; |
b548631a LP |
263 | |
264 | dbus_error_init(&error); | |
265 | ||
266 | if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Start")) | |
267 | job_type = JOB_START; | |
268 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Stop")) | |
269 | job_type = JOB_STOP; | |
270 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Reload")) | |
271 | job_type = JOB_RELOAD; | |
272 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Restart")) | |
273 | job_type = JOB_RESTART; | |
9a1ac7b9 LP |
274 | else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "TryRestart")) |
275 | job_type = JOB_TRY_RESTART; | |
4139c1b2 | 276 | else if (UNIT_VTABLE(u)->bus_message_handler) |
5e8d1c9a | 277 | return UNIT_VTABLE(u)->bus_message_handler(u, connection, message); |
b548631a | 278 | else |
4139c1b2 | 279 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
b548631a LP |
280 | |
281 | if (job_type != _JOB_TYPE_INVALID) { | |
282 | const char *smode; | |
283 | JobMode mode; | |
284 | Job *j; | |
285 | int r; | |
b548631a | 286 | |
398ef8ba LP |
287 | if (job_type == JOB_START && u->meta.only_by_dependency) { |
288 | dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Unit may be activated by dependency only."); | |
289 | return bus_send_error_reply(m, connection, message, &error, -EPERM); | |
290 | } | |
bc0f8771 | 291 | |
b548631a LP |
292 | if (!dbus_message_get_args( |
293 | message, | |
294 | &error, | |
295 | DBUS_TYPE_STRING, &smode, | |
296 | DBUS_TYPE_INVALID)) | |
5e8d1c9a | 297 | return bus_send_error_reply(m, connection, message, &error, -EINVAL); |
b548631a | 298 | |
398ef8ba LP |
299 | if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) { |
300 | dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode); | |
301 | return bus_send_error_reply(m, connection, message, &error, -EINVAL); | |
302 | } | |
b548631a | 303 | |
398ef8ba LP |
304 | if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0) |
305 | return bus_send_error_reply(m, connection, message, &error, r); | |
b548631a LP |
306 | |
307 | if (!(reply = dbus_message_new_method_return(message))) | |
308 | goto oom; | |
309 | ||
310 | if (!(path = job_dbus_path(j))) | |
311 | goto oom; | |
312 | ||
313 | if (!dbus_message_append_args( | |
314 | reply, | |
315 | DBUS_TYPE_OBJECT_PATH, &path, | |
316 | DBUS_TYPE_INVALID)) | |
317 | goto oom; | |
318 | } | |
319 | ||
c87eba54 LP |
320 | free(path); |
321 | ||
b548631a | 322 | if (reply) { |
5e8d1c9a | 323 | if (!dbus_connection_send(connection, reply, NULL)) |
b548631a LP |
324 | goto oom; |
325 | ||
326 | dbus_message_unref(reply); | |
327 | } | |
328 | ||
329 | return DBUS_HANDLER_RESULT_HANDLED; | |
330 | ||
331 | oom: | |
c87eba54 LP |
332 | free(path); |
333 | ||
b548631a LP |
334 | if (reply) |
335 | dbus_message_unref(reply); | |
336 | ||
337 | dbus_error_free(&error); | |
338 | ||
339 | return DBUS_HANDLER_RESULT_NEED_MEMORY; | |
ea430986 LP |
340 | } |
341 | ||
5e8d1c9a | 342 | static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage *message, void *data) { |
ea430986 LP |
343 | Manager *m = data; |
344 | Unit *u; | |
345 | int r; | |
346 | ||
347 | assert(connection); | |
348 | assert(message); | |
349 | assert(m); | |
350 | ||
ea430986 LP |
351 | if ((r = manager_get_unit_from_dbus_path(m, dbus_message_get_path(message), &u)) < 0) { |
352 | ||
353 | if (r == -ENOMEM) | |
354 | return DBUS_HANDLER_RESULT_NEED_MEMORY; | |
355 | ||
356 | if (r == -ENOENT) | |
357 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | |
358 | ||
5e8d1c9a | 359 | return bus_send_error_reply(m, connection, message, NULL, r); |
ea430986 LP |
360 | } |
361 | ||
5e8d1c9a | 362 | return bus_unit_message_dispatch(u, connection, message); |
ea430986 LP |
363 | } |
364 | ||
365 | const DBusObjectPathVTable bus_unit_vtable = { | |
366 | .message_function = bus_unit_message_handler | |
367 | }; | |
c1e1601e LP |
368 | |
369 | void bus_unit_send_change_signal(Unit *u) { | |
370 | char *p = NULL; | |
371 | DBusMessage *m = NULL; | |
372 | ||
373 | assert(u); | |
c1e1601e | 374 | |
c0bd0cf7 LP |
375 | if (u->meta.in_dbus_queue) { |
376 | LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta); | |
377 | u->meta.in_dbus_queue = false; | |
378 | } | |
c1e1601e | 379 | |
a567261a | 380 | if (!bus_has_subscriber(u->meta.manager)) { |
94b6dfa2 | 381 | u->meta.sent_dbus_new_signal = true; |
c1e1601e | 382 | return; |
94b6dfa2 | 383 | } |
c1e1601e LP |
384 | |
385 | if (!(p = unit_dbus_path(u))) | |
386 | goto oom; | |
387 | ||
388 | if (u->meta.sent_dbus_new_signal) { | |
389 | /* Send a change signal */ | |
390 | ||
391 | if (!(m = dbus_message_new_signal(p, "org.freedesktop.systemd1.Unit", "Changed"))) | |
392 | goto oom; | |
393 | } else { | |
c1e1601e LP |
394 | /* Send a new signal */ |
395 | ||
701cc384 | 396 | if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitNew"))) |
c1e1601e LP |
397 | goto oom; |
398 | ||
c1e1601e | 399 | if (!dbus_message_append_args(m, |
9e2f7c11 | 400 | DBUS_TYPE_STRING, &u->meta.id, |
c1e1601e LP |
401 | DBUS_TYPE_OBJECT_PATH, &p, |
402 | DBUS_TYPE_INVALID)) | |
403 | goto oom; | |
404 | } | |
405 | ||
5e8d1c9a | 406 | if (bus_broadcast(u->meta.manager, m) < 0) |
c1e1601e LP |
407 | goto oom; |
408 | ||
409 | free(p); | |
410 | dbus_message_unref(m); | |
411 | ||
412 | u->meta.sent_dbus_new_signal = true; | |
413 | ||
414 | return; | |
415 | ||
416 | oom: | |
417 | free(p); | |
418 | ||
419 | if (m) | |
420 | dbus_message_unref(m); | |
421 | ||
422 | log_error("Failed to allocate unit change/new signal."); | |
423 | } | |
424 | ||
425 | void bus_unit_send_removed_signal(Unit *u) { | |
426 | char *p = NULL; | |
427 | DBusMessage *m = NULL; | |
c1e1601e LP |
428 | |
429 | assert(u); | |
430 | ||
a567261a | 431 | if (!bus_has_subscriber(u->meta.manager)) |
c1e1601e LP |
432 | return; |
433 | ||
7535cc78 LP |
434 | if (!u->meta.sent_dbus_new_signal) |
435 | bus_unit_send_change_signal(u); | |
436 | ||
c1e1601e LP |
437 | if (!(p = unit_dbus_path(u))) |
438 | goto oom; | |
439 | ||
701cc384 | 440 | if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitRemoved"))) |
c1e1601e LP |
441 | goto oom; |
442 | ||
c1e1601e | 443 | if (!dbus_message_append_args(m, |
9e2f7c11 | 444 | DBUS_TYPE_STRING, &u->meta.id, |
c1e1601e LP |
445 | DBUS_TYPE_OBJECT_PATH, &p, |
446 | DBUS_TYPE_INVALID)) | |
447 | goto oom; | |
448 | ||
5e8d1c9a | 449 | if (bus_broadcast(u->meta.manager, m) < 0) |
c1e1601e LP |
450 | goto oom; |
451 | ||
452 | free(p); | |
453 | dbus_message_unref(m); | |
454 | ||
455 | return; | |
456 | ||
457 | oom: | |
458 | free(p); | |
459 | ||
460 | if (m) | |
461 | dbus_message_unref(m); | |
462 | ||
463 | log_error("Failed to allocate unit remove signal."); | |
464 | } |