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