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