chiark / gitweb /
units: add missing target files
[elogind.git] / src / dbus-unit.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
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
22 #include <errno.h>
23
24 #include "dbus.h"
25 #include "log.h"
26 #include "dbus-unit.h"
27 #include "bus-errors.h"
28
29 const char bus_unit_interface[] = BUS_UNIT_INTERFACE;
30
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
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
69 int bus_unit_append_description(Manager *m, DBusMessageIter *i, const char *property, void *data) {
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
86 DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_unit_append_load_state, unit_load_state, UnitLoadState);
87
88 int bus_unit_append_active_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
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
105 int bus_unit_append_sub_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
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
122 int bus_unit_append_can_start(Manager *m, DBusMessageIter *i, const char *property, void *data) {
123         Unit *u = data;
124         dbus_bool_t b;
125
126         assert(m);
127         assert(i);
128         assert(property);
129         assert(u);
130
131         b = unit_can_start(u);
132
133         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
134                 return -ENOMEM;
135
136         return 0;
137 }
138
139 int bus_unit_append_can_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) {
140         Unit *u = data;
141         dbus_bool_t b;
142
143         assert(m);
144         assert(i);
145         assert(property);
146         assert(u);
147
148         b = unit_can_reload(u);
149
150         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
151                 return -ENOMEM;
152
153         return 0;
154 }
155
156 int bus_unit_append_job(Manager *m, DBusMessageIter *i, const char *property, void *data) {
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) ||
175                     !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
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) ||
190                     !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
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
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;
209
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*) "";
220
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
257 static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *connection, DBusMessage *message) {
258         DBusMessage *reply = NULL;
259         Manager *m = u->meta.manager;
260         DBusError error;
261         JobType job_type = _JOB_TYPE_INVALID;
262         char *path = NULL;
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;
274         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "TryRestart"))
275                 job_type = JOB_TRY_RESTART;
276         else if (UNIT_VTABLE(u)->bus_message_handler)
277                 return UNIT_VTABLE(u)->bus_message_handler(u, connection, message);
278         else
279                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
280
281         if (job_type != _JOB_TYPE_INVALID) {
282                 const char *smode;
283                 JobMode mode;
284                 Job *j;
285                 int r;
286
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                 }
291
292                 if (!dbus_message_get_args(
293                                     message,
294                                     &error,
295                                     DBUS_TYPE_STRING, &smode,
296                                     DBUS_TYPE_INVALID))
297                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
298
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                 }
303
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);
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
320         free(path);
321
322         if (reply) {
323                 if (!dbus_connection_send(connection, reply, NULL))
324                         goto oom;
325
326                 dbus_message_unref(reply);
327         }
328
329         return DBUS_HANDLER_RESULT_HANDLED;
330
331 oom:
332         free(path);
333
334         if (reply)
335                 dbus_message_unref(reply);
336
337         dbus_error_free(&error);
338
339         return DBUS_HANDLER_RESULT_NEED_MEMORY;
340 }
341
342 static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage  *message, void *data) {
343         Manager *m = data;
344         Unit *u;
345         int r;
346
347         assert(connection);
348         assert(message);
349         assert(m);
350
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
359                 return bus_send_error_reply(m, connection, message, NULL, r);
360         }
361
362         return bus_unit_message_dispatch(u, connection, message);
363 }
364
365 const DBusObjectPathVTable bus_unit_vtable = {
366         .message_function = bus_unit_message_handler
367 };
368
369 void bus_unit_send_change_signal(Unit *u) {
370         char *p = NULL;
371         DBusMessage *m = NULL;
372
373         assert(u);
374
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         }
379
380         if (!bus_has_subscriber(u->meta.manager)) {
381                 u->meta.sent_dbus_new_signal = true;
382                 return;
383         }
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 {
394                 /* Send a new signal */
395
396                 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitNew")))
397                         goto oom;
398
399                 if (!dbus_message_append_args(m,
400                                               DBUS_TYPE_STRING, &u->meta.id,
401                                               DBUS_TYPE_OBJECT_PATH, &p,
402                                               DBUS_TYPE_INVALID))
403                         goto oom;
404         }
405
406         if (bus_broadcast(u->meta.manager, m) < 0)
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;
428
429         assert(u);
430
431         if (!bus_has_subscriber(u->meta.manager))
432                 return;
433
434         if (!u->meta.sent_dbus_new_signal)
435                 bus_unit_send_change_signal(u);
436
437         if (!(p = unit_dbus_path(u)))
438                 goto oom;
439
440         if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitRemoved")))
441                 goto oom;
442
443         if (!dbus_message_append_args(m,
444                                       DBUS_TYPE_STRING, &u->meta.id,
445                                       DBUS_TYPE_OBJECT_PATH, &p,
446                                       DBUS_TYPE_INVALID))
447                 goto oom;
448
449         if (bus_broadcast(u->meta.manager, m) < 0)
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 }