chiark / gitweb /
tmpfiles: consider TRUNCATE_DIRECTORY as well.
[elogind.git] / src / dbus-unit.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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 #define INVALIDATING_PROPERTIES                 \
32         "LoadState\0"                           \
33         "ActiveState\0"                         \
34         "SubState\0"                            \
35         "InactiveExitTimestamp\0"               \
36         "ActiveEnterTimestamp\0"                \
37         "ActiveExitTimestamp\0"                 \
38         "InactiveEnterTimestamp\0"              \
39         "Job\0"                                 \
40         "NeedDaemonReload\0"                    \
41         "\0"
42
43 int bus_unit_append_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
44         char *t;
45         Iterator j;
46         DBusMessageIter sub;
47         Unit *u = data;
48
49         if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
50                 return -ENOMEM;
51
52         SET_FOREACH(t, u->meta.names, j)
53                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &t))
54                         return -ENOMEM;
55
56         if (!dbus_message_iter_close_container(i, &sub))
57                 return -ENOMEM;
58
59         return 0;
60 }
61
62 int bus_unit_append_following(Manager *m, DBusMessageIter *i, const char *property, void *data) {
63         Unit *u = data, *f;
64         const char *d;
65
66         assert(m);
67         assert(i);
68         assert(property);
69         assert(u);
70
71         f = unit_following(u);
72         d = f ? f->meta.id : "";
73
74         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
75                 return -ENOMEM;
76
77         return 0;
78 }
79
80 int bus_unit_append_dependencies(Manager *m, DBusMessageIter *i, const char *property, void *data) {
81         Unit *u;
82         Iterator j;
83         DBusMessageIter sub;
84         Set *s = data;
85
86         if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
87                 return -ENOMEM;
88
89         SET_FOREACH(u, s, j)
90                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &u->meta.id))
91                         return -ENOMEM;
92
93         if (!dbus_message_iter_close_container(i, &sub))
94                 return -ENOMEM;
95
96         return 0;
97 }
98
99 int bus_unit_append_description(Manager *m, DBusMessageIter *i, const char *property, void *data) {
100         Unit *u = data;
101         const char *d;
102
103         assert(m);
104         assert(i);
105         assert(property);
106         assert(u);
107
108         d = unit_description(u);
109
110         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &d))
111                 return -ENOMEM;
112
113         return 0;
114 }
115
116 DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_unit_append_load_state, unit_load_state, UnitLoadState);
117
118 int bus_unit_append_active_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
119         Unit *u = data;
120         const char *state;
121
122         assert(m);
123         assert(i);
124         assert(property);
125         assert(u);
126
127         state = unit_active_state_to_string(unit_active_state(u));
128
129         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
130                 return -ENOMEM;
131
132         return 0;
133 }
134
135 int bus_unit_append_sub_state(Manager *m, DBusMessageIter *i, const char *property, void *data) {
136         Unit *u = data;
137         const char *state;
138
139         assert(m);
140         assert(i);
141         assert(property);
142         assert(u);
143
144         state = unit_sub_state_to_string(u);
145
146         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &state))
147                 return -ENOMEM;
148
149         return 0;
150 }
151
152 int bus_unit_append_can_start(Manager *m, DBusMessageIter *i, const char *property, void *data) {
153         Unit *u = data;
154         dbus_bool_t b;
155
156         assert(m);
157         assert(i);
158         assert(property);
159         assert(u);
160
161         b = unit_can_start(u) &&
162                 !u->meta.refuse_manual_start;
163
164         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
165                 return -ENOMEM;
166
167         return 0;
168 }
169
170 int bus_unit_append_can_stop(Manager *m, DBusMessageIter *i, const char *property, void *data) {
171         Unit *u = data;
172         dbus_bool_t b;
173
174         assert(m);
175         assert(i);
176         assert(property);
177         assert(u);
178
179         /* On the lower levels we assume that every unit we can start
180          * we can also stop */
181
182         b = unit_can_start(u) &&
183                 !u->meta.refuse_manual_stop;
184
185         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
186                 return -ENOMEM;
187
188         return 0;
189 }
190
191 int bus_unit_append_can_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) {
192         Unit *u = data;
193         dbus_bool_t b;
194
195         assert(m);
196         assert(i);
197         assert(property);
198         assert(u);
199
200         b = unit_can_reload(u);
201
202         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
203                 return -ENOMEM;
204
205         return 0;
206 }
207
208 int bus_unit_append_can_isolate(Manager *m, DBusMessageIter *i, const char *property, void *data) {
209         Unit *u = data;
210         dbus_bool_t b;
211
212         assert(m);
213         assert(i);
214         assert(property);
215         assert(u);
216
217         b = unit_can_isolate(u) &&
218                 !u->meta.refuse_manual_start;
219
220         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
221                 return -ENOMEM;
222
223         return 0;
224 }
225
226 int bus_unit_append_job(Manager *m, DBusMessageIter *i, const char *property, void *data) {
227         Unit *u = data;
228         DBusMessageIter sub;
229         char *p;
230
231         assert(m);
232         assert(i);
233         assert(property);
234         assert(u);
235
236         if (!dbus_message_iter_open_container(i, DBUS_TYPE_STRUCT, NULL, &sub))
237                 return -ENOMEM;
238
239         if (u->meta.job) {
240
241                 if (!(p = job_dbus_path(u->meta.job)))
242                         return -ENOMEM;
243
244                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u->meta.job->id) ||
245                     !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
246                         free(p);
247                         return -ENOMEM;
248                 }
249         } else {
250                 uint32_t id = 0;
251
252                 /* No job, so let's fill in some placeholder
253                  * data. Since we need to fill in a valid path we
254                  * simple point to ourselves. */
255
256                 if (!(p = unit_dbus_path(u)))
257                         return -ENOMEM;
258
259                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &id) ||
260                     !dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &p)) {
261                         free(p);
262                         return -ENOMEM;
263                 }
264         }
265
266         free(p);
267
268         if (!dbus_message_iter_close_container(i, &sub))
269                 return -ENOMEM;
270
271         return 0;
272 }
273
274 int bus_unit_append_default_cgroup(Manager *m, DBusMessageIter *i, const char *property, void *data) {
275         Unit *u = data;
276         char *t;
277         CGroupBonding *cgb;
278         bool success;
279
280         assert(m);
281         assert(i);
282         assert(property);
283         assert(u);
284
285         if ((cgb = unit_get_default_cgroup(u))) {
286                 if (!(t = cgroup_bonding_to_string(cgb)))
287                         return -ENOMEM;
288         } else
289                 t = (char*) "";
290
291         success = dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t);
292
293         if (cgb)
294                 free(t);
295
296         return success ? 0 : -ENOMEM;
297 }
298
299 int bus_unit_append_cgroups(Manager *m, DBusMessageIter *i, const char *property, void *data) {
300         Unit *u = data;
301         CGroupBonding *cgb;
302         DBusMessageIter sub;
303
304         if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
305                 return -ENOMEM;
306
307         LIST_FOREACH(by_unit, cgb, u->meta.cgroup_bondings) {
308                 char *t;
309                 bool success;
310
311                 if (!(t = cgroup_bonding_to_string(cgb)))
312                         return -ENOMEM;
313
314                 success = dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &t);
315                 free(t);
316
317                 if (!success)
318                         return -ENOMEM;
319         }
320
321         if (!dbus_message_iter_close_container(i, &sub))
322                 return -ENOMEM;
323
324         return 0;
325 }
326
327 int bus_unit_append_need_daemon_reload(Manager *m, DBusMessageIter *i, const char *property, void *data) {
328         Unit *u = data;
329         dbus_bool_t b;
330
331         assert(m);
332         assert(i);
333         assert(property);
334         assert(u);
335
336         b = unit_need_daemon_reload(u);
337
338         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &b))
339                 return -ENOMEM;
340
341         return 0;
342 }
343
344 static DBusHandlerResult bus_unit_message_dispatch(Unit *u, DBusConnection *connection, DBusMessage *message) {
345         DBusMessage *reply = NULL;
346         Manager *m = u->meta.manager;
347         DBusError error;
348         JobType job_type = _JOB_TYPE_INVALID;
349         char *path = NULL;
350         bool reload_if_possible = false;
351
352         dbus_error_init(&error);
353
354         if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Start"))
355                 job_type = JOB_START;
356         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Stop"))
357                 job_type = JOB_STOP;
358         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Reload"))
359                 job_type = JOB_RELOAD;
360         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "Restart"))
361                 job_type = JOB_RESTART;
362         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "TryRestart"))
363                 job_type = JOB_TRY_RESTART;
364         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrRestart")) {
365                 reload_if_possible = true;
366                 job_type = JOB_RESTART;
367         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ReloadOrTryRestart")) {
368                 reload_if_possible = true;
369                 job_type = JOB_TRY_RESTART;
370         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Unit", "ResetFailed")) {
371
372                 unit_reset_failed(u);
373
374                 if (!(reply = dbus_message_new_method_return(message)))
375                         goto oom;
376
377         } else if (UNIT_VTABLE(u)->bus_message_handler)
378                 return UNIT_VTABLE(u)->bus_message_handler(u, connection, message);
379         else
380                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
381
382         if (job_type != _JOB_TYPE_INVALID) {
383                 const char *smode;
384                 JobMode mode;
385                 Job *j;
386                 int r;
387
388                 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
389                     (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
390                     ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
391                      (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
392                         dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
393                         return bus_send_error_reply(m, connection, message, &error, -EPERM);
394                 }
395
396                 if (!dbus_message_get_args(
397                                     message,
398                                     &error,
399                                     DBUS_TYPE_STRING, &smode,
400                                     DBUS_TYPE_INVALID))
401                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
402
403                 if (reload_if_possible && unit_can_reload(u)) {
404                         if (job_type == JOB_RESTART)
405                                 job_type = JOB_RELOAD_OR_START;
406                         else if (job_type == JOB_TRY_RESTART)
407                                 job_type = JOB_RELOAD;
408                 }
409
410                 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
411                         dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
412                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
413                 }
414
415                 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
416                         return bus_send_error_reply(m, connection, message, &error, r);
417
418                 if (!(reply = dbus_message_new_method_return(message)))
419                         goto oom;
420
421                 if (!(path = job_dbus_path(j)))
422                         goto oom;
423
424                 if (!dbus_message_append_args(
425                                     reply,
426                                     DBUS_TYPE_OBJECT_PATH, &path,
427                                     DBUS_TYPE_INVALID))
428                         goto oom;
429         }
430
431         free(path);
432
433         if (reply) {
434                 if (!dbus_connection_send(connection, reply, NULL))
435                         goto oom;
436
437                 dbus_message_unref(reply);
438         }
439
440         return DBUS_HANDLER_RESULT_HANDLED;
441
442 oom:
443         free(path);
444
445         if (reply)
446                 dbus_message_unref(reply);
447
448         dbus_error_free(&error);
449
450         return DBUS_HANDLER_RESULT_NEED_MEMORY;
451 }
452
453 static DBusHandlerResult bus_unit_message_handler(DBusConnection *connection, DBusMessage  *message, void *data) {
454         Manager *m = data;
455         Unit *u;
456         int r;
457         DBusMessage *reply;
458
459         assert(connection);
460         assert(message);
461         assert(m);
462
463         if (streq(dbus_message_get_path(message), "/org/freedesktop/systemd1/unit")) {
464                 /* Be nice to gdbus and return introspection data for our mid-level paths */
465
466                 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
467                         char *introspection = NULL;
468                         FILE *f;
469                         Iterator i;
470                         const char *k;
471                         size_t size;
472
473                         if (!(reply = dbus_message_new_method_return(message)))
474                                 goto oom;
475
476                         /* We roll our own introspection code here, instead of
477                          * relying on bus_default_message_handler() because we
478                          * need to generate our introspection string
479                          * dynamically. */
480
481                         if (!(f = open_memstream(&introspection, &size)))
482                                 goto oom;
483
484                         fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
485                               "<node>\n", f);
486
487                         fputs(BUS_INTROSPECTABLE_INTERFACE, f);
488                         fputs(BUS_PEER_INTERFACE, f);
489
490                         HASHMAP_FOREACH_KEY(u, k, m->units, i) {
491                                 char *p;
492
493                                 if (k != u->meta.id)
494                                         continue;
495
496                                 if (!(p = bus_path_escape(k))) {
497                                         fclose(f);
498                                         free(introspection);
499                                         goto oom;
500                                 }
501
502                                 fprintf(f, "<node name=\"%s\"/>", p);
503                                 free(p);
504                         }
505
506                         fputs("</node>\n", f);
507
508                         if (ferror(f)) {
509                                 fclose(f);
510                                 free(introspection);
511                                 goto oom;
512                         }
513
514                         fclose(f);
515
516                         if (!introspection)
517                                 goto oom;
518
519                         if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
520                                 free(introspection);
521                                 goto oom;
522                         }
523
524                         free(introspection);
525
526                         if (!dbus_connection_send(connection, reply, NULL))
527                                 goto oom;
528
529                         dbus_message_unref(reply);
530
531                         return DBUS_HANDLER_RESULT_HANDLED;
532                 }
533
534                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
535         }
536
537         if ((r = manager_get_unit_from_dbus_path(m, dbus_message_get_path(message), &u)) < 0) {
538
539                 if (r == -ENOMEM)
540                         return DBUS_HANDLER_RESULT_NEED_MEMORY;
541
542                 if (r == -ENOENT)
543                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
544
545                 return bus_send_error_reply(m, connection, message, NULL, r);
546         }
547
548         return bus_unit_message_dispatch(u, connection, message);
549
550 oom:
551         if (reply)
552                 dbus_message_unref(reply);
553
554         return DBUS_HANDLER_RESULT_NEED_MEMORY;
555 }
556
557 const DBusObjectPathVTable bus_unit_vtable = {
558         .message_function = bus_unit_message_handler
559 };
560
561 void bus_unit_send_change_signal(Unit *u) {
562         char *p = NULL;
563         DBusMessage *m = NULL;
564
565         assert(u);
566
567         if (u->meta.in_dbus_queue) {
568                 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
569                 u->meta.in_dbus_queue = false;
570         }
571
572         if (!u->meta.id)
573                 return;
574
575         if (!bus_has_subscriber(u->meta.manager)) {
576                 u->meta.sent_dbus_new_signal = true;
577                 return;
578         }
579
580         if (!(p = unit_dbus_path(u)))
581                 goto oom;
582
583         if (u->meta.sent_dbus_new_signal) {
584                 /* Send a properties changed signal. First for the
585                  * specific type, then for the generic unit. The
586                  * clients may rely on this order to get atomic
587                  * behaviour if needed. */
588
589                 if (UNIT_VTABLE(u)->bus_invalidating_properties) {
590
591                         if (!(m = bus_properties_changed_new(p,
592                                                              UNIT_VTABLE(u)->bus_interface,
593                                                              UNIT_VTABLE(u)->bus_invalidating_properties)))
594                                 goto oom;
595
596                         if (bus_broadcast(u->meta.manager, m) < 0)
597                                 goto oom;
598
599                         dbus_message_unref(m);
600                 }
601
602                 if (!(m = bus_properties_changed_new(p, "org.freedesktop.systemd1.Unit", INVALIDATING_PROPERTIES)))
603                         goto oom;
604
605         } else {
606                 /* Send a new signal */
607
608                 if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitNew")))
609                         goto oom;
610
611                 if (!dbus_message_append_args(m,
612                                               DBUS_TYPE_STRING, &u->meta.id,
613                                               DBUS_TYPE_OBJECT_PATH, &p,
614                                               DBUS_TYPE_INVALID))
615                         goto oom;
616         }
617
618         if (bus_broadcast(u->meta.manager, m) < 0)
619                 goto oom;
620
621         free(p);
622         dbus_message_unref(m);
623
624         u->meta.sent_dbus_new_signal = true;
625
626         return;
627
628 oom:
629         free(p);
630
631         if (m)
632                 dbus_message_unref(m);
633
634         log_error("Failed to allocate unit change/new signal.");
635 }
636
637 void bus_unit_send_removed_signal(Unit *u) {
638         char *p = NULL;
639         DBusMessage *m = NULL;
640
641         assert(u);
642
643         if (!bus_has_subscriber(u->meta.manager))
644                 return;
645
646         if (!u->meta.sent_dbus_new_signal)
647                 bus_unit_send_change_signal(u);
648
649         if (!u->meta.id)
650                 return;
651
652         if (!(p = unit_dbus_path(u)))
653                 goto oom;
654
655         if (!(m = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitRemoved")))
656                 goto oom;
657
658         if (!dbus_message_append_args(m,
659                                       DBUS_TYPE_STRING, &u->meta.id,
660                                       DBUS_TYPE_OBJECT_PATH, &p,
661                                       DBUS_TYPE_INVALID))
662                 goto oom;
663
664         if (bus_broadcast(u->meta.manager, m) < 0)
665                 goto oom;
666
667         free(p);
668         dbus_message_unref(m);
669
670         return;
671
672 oom:
673         free(p);
674
675         if (m)
676                 dbus_message_unref(m);
677
678         log_error("Failed to allocate unit remove signal.");
679 }