chiark / gitweb /
build-sys: add make upload target
[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 DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_unit_append_kill_mode, kill_mode, KillMode);
258
259 static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *connection, DBusMessage *message) {
260         DBusMessage *reply = NULL;
261         Manager *m = u->meta.manager;
262         DBusError error;
263         JobType job_type = _JOB_TYPE_INVALID;
264         char *path = NULL;
265
266         dbus_error_init(&error);
267
268         if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Start"))
269                 job_type = JOB_START;
270         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Stop"))
271                 job_type = JOB_STOP;
272         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Reload"))
273                 job_type = JOB_RELOAD;
274         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Restart"))
275                 job_type = JOB_RESTART;
276         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "TryRestart"))
277                 job_type = JOB_TRY_RESTART;
278         else if (UNIT_VTABLE(u)->bus_message_handler)
279                 return UNIT_VTABLE(u)->bus_message_handler(u, connection, message);
280         else
281                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
282
283         if (job_type != _JOB_TYPE_INVALID) {
284                 const char *smode;
285                 JobMode mode;
286                 Job *j;
287                 int r;
288
289                 if (job_type == JOB_START && u->meta.only_by_dependency) {
290                         dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Unit may be activated by dependency only.");
291                         return bus_send_error_reply(m, connection, message, &error, -EPERM);
292                 }
293
294                 if (!dbus_message_get_args(
295                                     message,
296                                     &error,
297                                     DBUS_TYPE_STRING, &smode,
298                                     DBUS_TYPE_INVALID))
299                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
300
301                 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
302                         dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
303                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
304                 }
305
306                 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
307                         return bus_send_error_reply(m, connection, message, &error, r);
308
309                 if (!(reply = dbus_message_new_method_return(message)))
310                         goto oom;
311
312                 if (!(path = job_dbus_path(j)))
313                         goto oom;
314
315                 if (!dbus_message_append_args(
316                                     reply,
317                                     DBUS_TYPE_OBJECT_PATH, &path,
318                                     DBUS_TYPE_INVALID))
319                         goto oom;
320         }
321
322         free(path);
323
324         if (reply) {
325                 if (!dbus_connection_send(connection, reply, NULL))
326                         goto oom;
327
328                 dbus_message_unref(reply);
329         }
330
331         return DBUS_HANDLER_RESULT_HANDLED;
332
333 oom:
334         free(path);
335
336         if (reply)
337                 dbus_message_unref(reply);
338
339         dbus_error_free(&error);
340
341         return DBUS_HANDLER_RESULT_NEED_MEMORY;
342 }
343
344 static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage  *message, void *data) {
345         Manager *m = data;
346         Unit *u;
347         int r;
348
349         assert(connection);
350         assert(message);
351         assert(m);
352
353         if ((r = manager_get_unit_from_dbus_path(m, dbus_message_get_path(message), &u)) < 0) {
354
355                 if (r == -ENOMEM)
356                         return DBUS_HANDLER_RESULT_NEED_MEMORY;
357
358                 if (r == -ENOENT)
359                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
360
361                 return bus_send_error_reply(m, connection, message, NULL, r);
362         }
363
364         return bus_unit_message_dispatch(u, connection, message);
365 }
366
367 const DBusObjectPathVTable bus_unit_vtable = {
368         .message_function = bus_unit_message_handler
369 };
370
371 void bus_unit_send_change_signal(Unit *u) {
372         char *p = NULL;
373         DBusMessage *m = NULL;
374
375         assert(u);
376         assert(u->meta.in_dbus_queue);
377
378         LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
379         u->meta.in_dbus_queue = false;
380
381         if (!bus_has_subscriber(u->meta.manager)) {
382                 u->meta.sent_dbus_new_signal = true;
383                 return;
384         }
385
386         if (!(p = unit_dbus_path(u)))
387                 goto oom;
388
389         if (u->meta.sent_dbus_new_signal) {
390                 /* Send a change signal */
391
392                 if (!(m = dbus_message_new_signal(p, "org.freedesktop.systemd1.Unit", "Changed")))
393                         goto oom;
394         } else {
395                 /* Send a new signal */
396
397                 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitNew")))
398                         goto oom;
399
400                 if (!dbus_message_append_args(m,
401                                               DBUS_TYPE_STRING, &u->meta.id,
402                                               DBUS_TYPE_OBJECT_PATH, &p,
403                                               DBUS_TYPE_INVALID))
404                         goto oom;
405         }
406
407         if (bus_broadcast(u->meta.manager, m) < 0)
408                 goto oom;
409
410         free(p);
411         dbus_message_unref(m);
412
413         u->meta.sent_dbus_new_signal = true;
414
415         return;
416
417 oom:
418         free(p);
419
420         if (m)
421                 dbus_message_unref(m);
422
423         log_error("Failed to allocate unit change/new signal.");
424 }
425
426 void bus_unit_send_removed_signal(Unit *u) {
427         char *p = NULL;
428         DBusMessage *m = NULL;
429
430         assert(u);
431
432         if (!bus_has_subscriber(u->meta.manager))
433                 return;
434
435         if (!u->meta.sent_dbus_new_signal)
436                 bus_unit_send_change_signal(u);
437
438         if (!(p = unit_dbus_path(u)))
439                 goto oom;
440
441         if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitRemoved")))
442                 goto oom;
443
444         if (!dbus_message_append_args(m,
445                                       DBUS_TYPE_STRING, &u->meta.id,
446                                       DBUS_TYPE_OBJECT_PATH, &p,
447                                       DBUS_TYPE_INVALID))
448                 goto oom;
449
450         if (bus_broadcast(u->meta.manager, m) < 0)
451                 goto oom;
452
453         free(p);
454         dbus_message_unref(m);
455
456         return;
457
458 oom:
459         free(p);
460
461         if (m)
462                 dbus_message_unref(m);
463
464         log_error("Failed to allocate unit remove signal.");
465 }