chiark / gitweb /
core: no need to pass bus object to selinux access check calls anymore
[elogind.git] / src / core / dbus-manager.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 Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 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   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <unistd.h>
24
25 #include "log.h"
26 #include "strv.h"
27 #include "build.h"
28 #include "install.h"
29 #include "selinux-access.h"
30 #include "watchdog.h"
31 #include "hwclock.h"
32 #include "path-util.h"
33 #include "virt.h"
34 #include "architecture.h"
35 #include "env-util.h"
36 #include "dbus.h"
37 #include "dbus-manager.h"
38 #include "dbus-unit.h"
39 #include "dbus-snapshot.h"
40 #include "dbus-execute.h"
41 #include "bus-errors.h"
42
43 static int property_get_version(
44                 sd_bus *bus,
45                 const char *path,
46                 const char *interface,
47                 const char *property,
48                 sd_bus_message *reply,
49                 void *userdata,
50                 sd_bus_error *error) {
51
52         assert(bus);
53         assert(reply);
54
55         return sd_bus_message_append(reply, "s", PACKAGE_VERSION);
56 }
57
58 static int property_get_features(
59                 sd_bus *bus,
60                 const char *path,
61                 const char *interface,
62                 const char *property,
63                 sd_bus_message *reply,
64                 void *userdata,
65                 sd_bus_error *error) {
66
67         assert(bus);
68         assert(reply);
69
70         return sd_bus_message_append(reply, "s", SYSTEMD_FEATURES);
71 }
72
73 static int property_get_virtualization(
74                 sd_bus *bus,
75                 const char *path,
76                 const char *interface,
77                 const char *property,
78                 sd_bus_message *reply,
79                 void *userdata,
80                 sd_bus_error *error) {
81
82         const char *id = NULL;
83
84         assert(bus);
85         assert(reply);
86
87         detect_virtualization(&id);
88
89         return sd_bus_message_append(reply, "s", id);
90 }
91
92 static int property_get_architecture(
93                 sd_bus *bus,
94                 const char *path,
95                 const char *interface,
96                 const char *property,
97                 sd_bus_message *reply,
98                 void *userdata,
99                 sd_bus_error *error) {
100
101         assert(bus);
102         assert(reply);
103
104         return sd_bus_message_append(reply, "s", architecture_to_string(uname_architecture()));
105 }
106
107 static int property_get_tainted(
108                 sd_bus *bus,
109                 const char *path,
110                 const char *interface,
111                 const char *property,
112                 sd_bus_message *reply,
113                 void *userdata,
114                 sd_bus_error *error) {
115
116         char buf[sizeof("split-usr:mtab-not-symlink:cgroups-missing:local-hwclock:")] = "", *e = buf;
117         _cleanup_free_ char *p = NULL;
118         Manager *m = userdata;
119
120         assert(bus);
121         assert(reply);
122         assert(m);
123
124         if (m->taint_usr)
125                 e = stpcpy(e, "split-usr:");
126
127         if (readlink_malloc("/etc/mtab", &p) < 0)
128                 e = stpcpy(e, "mtab-not-symlink:");
129
130         if (access("/proc/cgroups", F_OK) < 0)
131                 e = stpcpy(e, "cgroups-missing:");
132
133         if (hwclock_is_localtime() > 0)
134                 e = stpcpy(e, "local-hwclock:");
135
136         /* remove the last ':' */
137         if (e != buf)
138                 e[-1] = 0;
139
140         return sd_bus_message_append(reply, "s", buf);
141 }
142
143 static int property_get_log_target(
144                 sd_bus *bus,
145                 const char *path,
146                 const char *interface,
147                 const char *property,
148                 sd_bus_message *reply,
149                 void *userdata,
150                 sd_bus_error *error) {
151
152         assert(bus);
153         assert(reply);
154
155         return sd_bus_message_append(reply, "s", log_target_to_string(log_get_target()));
156 }
157
158 static int property_set_log_target(
159                 sd_bus *bus,
160                 const char *path,
161                 const char *interface,
162                 const char *property,
163                 sd_bus_message *value,
164                 void *userdata,
165                 sd_bus_error *error) {
166
167         const char *t;
168         int r;
169
170         assert(bus);
171         assert(value);
172
173         r = sd_bus_message_read(value, "s", &t);
174         if (r < 0)
175                 return r;
176
177         return log_set_target_from_string(t);
178 }
179
180 static int property_get_log_level(
181                 sd_bus *bus,
182                 const char *path,
183                 const char *interface,
184                 const char *property,
185                 sd_bus_message *reply,
186                 void *userdata,
187                 sd_bus_error *error) {
188
189         _cleanup_free_ char *t = NULL;
190         int r;
191
192         assert(bus);
193         assert(reply);
194
195         r = log_level_to_string_alloc(log_get_max_level(), &t);
196         if (r < 0)
197                 return r;
198
199         return sd_bus_message_append(reply, "s", t);
200 }
201
202 static int property_set_log_level(
203                 sd_bus *bus,
204                 const char *path,
205                 const char *interface,
206                 const char *property,
207                 sd_bus_message *value,
208                 void *userdata,
209                 sd_bus_error *error) {
210
211         const char *t;
212         int r;
213
214         assert(bus);
215         assert(value);
216
217         r = sd_bus_message_read(value, "s", &t);
218         if (r < 0)
219                 return r;
220
221         return log_set_max_level_from_string(t);
222 }
223
224 static int property_get_n_names(
225                 sd_bus *bus,
226                 const char *path,
227                 const char *interface,
228                 const char *property,
229                 sd_bus_message *reply,
230                 void *userdata,
231                 sd_bus_error *error) {
232
233         Manager *m = userdata;
234
235         assert(bus);
236         assert(reply);
237         assert(m);
238
239         return sd_bus_message_append(reply, "u", (uint32_t) hashmap_size(m->units));
240 }
241
242 static int property_get_n_failed_units(
243                 sd_bus *bus,
244                 const char *path,
245                 const char *interface,
246                 const char *property,
247                 sd_bus_message *reply,
248                 void *userdata,
249                 sd_bus_error *error) {
250
251         Manager *m = userdata;
252
253         assert(bus);
254         assert(reply);
255         assert(m);
256
257         return sd_bus_message_append(reply, "u", (uint32_t) set_size(m->failed_units));
258 }
259
260 static int property_get_n_jobs(
261                 sd_bus *bus,
262                 const char *path,
263                 const char *interface,
264                 const char *property,
265                 sd_bus_message *reply,
266                 void *userdata,
267                 sd_bus_error *error) {
268
269         Manager *m = userdata;
270
271         assert(bus);
272         assert(reply);
273         assert(m);
274
275         return sd_bus_message_append(reply, "u", (uint32_t) hashmap_size(m->jobs));
276 }
277
278 static int property_get_progress(
279                 sd_bus *bus,
280                 const char *path,
281                 const char *interface,
282                 const char *property,
283                 sd_bus_message *reply,
284                 void *userdata,
285                 sd_bus_error *error) {
286
287         Manager *m = userdata;
288         double d;
289
290         assert(bus);
291         assert(reply);
292         assert(m);
293
294         if (dual_timestamp_is_set(&m->finish_timestamp))
295                 d = 1.0;
296         else
297                 d = 1.0 - ((double) hashmap_size(m->jobs) / (double) m->n_installed_jobs);
298
299         return sd_bus_message_append(reply, "d", d);
300 }
301
302 static int property_get_system_state(
303                 sd_bus *bus,
304                 const char *path,
305                 const char *interface,
306                 const char *property,
307                 sd_bus_message *reply,
308                 void *userdata,
309                 sd_bus_error *error) {
310
311         Manager *m = userdata;
312
313         assert(bus);
314         assert(reply);
315         assert(m);
316
317         return sd_bus_message_append(reply, "s", manager_state_to_string(manager_state(m)));
318 }
319
320 static int property_set_runtime_watchdog(
321                 sd_bus *bus,
322                 const char *path,
323                 const char *interface,
324                 const char *property,
325                 sd_bus_message *value,
326                 void *userdata,
327                 sd_bus_error *error) {
328
329         usec_t *t = userdata;
330         int r;
331
332         assert(bus);
333         assert(value);
334
335         assert_cc(sizeof(usec_t) == sizeof(uint64_t));
336
337         r = sd_bus_message_read(value, "t", t);
338         if (r < 0)
339                 return r;
340
341         return watchdog_set_timeout(t);
342 }
343
344 static int method_get_unit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
345         _cleanup_free_ char *path = NULL;
346         Manager *m = userdata;
347         const char *name;
348         Unit *u;
349         int r;
350
351         assert(bus);
352         assert(message);
353         assert(m);
354
355         r = sd_bus_message_read(message, "s", &name);
356         if (r < 0)
357                 return r;
358
359         u = manager_get_unit(m, name);
360         if (!u)
361                 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not loaded.", name);
362
363         r = selinux_unit_access_check(u, message, "status", error);
364         if (r < 0)
365                 return r;
366
367         path = unit_dbus_path(u);
368         if (!path)
369                 return -ENOMEM;
370
371         return sd_bus_reply_method_return(message, "o", path);
372 }
373
374 static int method_get_unit_by_pid(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
375         _cleanup_free_ char *path = NULL;
376         Manager *m = userdata;
377         pid_t pid;
378         Unit *u;
379         int r;
380
381         assert(bus);
382         assert(message);
383         assert(m);
384
385         assert_cc(sizeof(pid_t) == sizeof(uint32_t));
386
387         r = sd_bus_message_read(message, "u", &pid);
388         if (r < 0)
389                 return r;
390
391         if (pid == 0) {
392                 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
393
394                 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
395                 if (r < 0)
396                         return r;
397
398                 r = sd_bus_creds_get_pid(creds, &pid);
399                 if (r < 0)
400                         return r;
401         }
402
403         u = manager_get_unit_by_pid(m, pid);
404         if (!u)
405                 return sd_bus_error_setf(error, BUS_ERROR_NO_UNIT_FOR_PID, "PID %u does not belong to any loaded unit.", pid);
406
407         r = selinux_unit_access_check(u, message, "status", error);
408         if (r < 0)
409                 return r;
410
411         path = unit_dbus_path(u);
412         if (!path)
413                 return -ENOMEM;
414
415         return sd_bus_reply_method_return(message, "o", path);
416 }
417
418 static int method_load_unit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
419         _cleanup_free_ char *path = NULL;
420         Manager *m = userdata;
421         const char *name;
422         Unit *u;
423         int r;
424
425         assert(bus);
426         assert(message);
427         assert(m);
428
429         r = sd_bus_message_read(message, "s", &name);
430         if (r < 0)
431                 return r;
432
433         r = manager_load_unit(m, name, NULL, error, &u);
434         if (r < 0)
435                 return r;
436
437         r = selinux_unit_access_check(u, message, "status", error);
438         if (r < 0)
439                 return r;
440
441         path = unit_dbus_path(u);
442         if (!path)
443                 return -ENOMEM;
444
445         return sd_bus_reply_method_return(message, "o", path);
446 }
447
448 static int method_start_unit_generic(sd_bus *bus, sd_bus_message *message, Manager *m, JobType job_type, bool reload_if_possible, sd_bus_error *error) {
449         const char *name;
450         Unit *u;
451         int r;
452
453         assert(bus);
454         assert(message);
455         assert(m);
456
457         r = sd_bus_message_read(message, "s", &name);
458         if (r < 0)
459                 return r;
460
461         r = manager_load_unit(m, name, NULL, error, &u);
462         if (r < 0)
463                 return r;
464
465         return bus_unit_method_start_generic(bus, message, u, job_type, reload_if_possible, error);
466 }
467
468 static int method_start_unit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
469         return method_start_unit_generic(bus, message, userdata, JOB_START, false, error);
470 }
471
472 static int method_stop_unit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
473         return method_start_unit_generic(bus, message, userdata, JOB_STOP, false, error);
474 }
475
476 static int method_reload_unit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
477         return method_start_unit_generic(bus, message, userdata, JOB_RELOAD, false, error);
478 }
479
480 static int method_restart_unit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
481         return method_start_unit_generic(bus, message, userdata, JOB_RESTART, false, error);
482 }
483
484 static int method_try_restart_unit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
485         return method_start_unit_generic(bus, message, userdata, JOB_TRY_RESTART, false, error);
486 }
487
488 static int method_reload_or_restart_unit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
489         return method_start_unit_generic(bus, message, userdata, JOB_RESTART, true, error);
490 }
491
492 static int method_reload_or_try_restart_unit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
493         return method_start_unit_generic(bus, message, userdata, JOB_TRY_RESTART, true, error);
494 }
495
496 static int method_start_unit_replace(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
497         Manager *m = userdata;
498         const char *old_name;
499         Unit *u;
500         int r;
501
502         assert(bus);
503         assert(message);
504         assert(m);
505
506         r = sd_bus_message_read(message, "s", &old_name);
507         if (r < 0)
508                 return r;
509
510         u = manager_get_unit(m, old_name);
511         if (!u || !u->job || u->job->type != JOB_START)
512                 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_JOB, "No job queued for unit %s", old_name);
513
514         return method_start_unit_generic(bus, message, m, JOB_START, false, error);
515 }
516
517 static int method_kill_unit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
518         Manager *m = userdata;
519         const char *name;
520         Unit *u;
521         int r;
522
523         assert(bus);
524         assert(message);
525         assert(m);
526
527         r = sd_bus_message_read(message, "s", &name);
528         if (r < 0)
529                 return r;
530
531         u = manager_get_unit(m, name);
532         if (!u)
533                 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
534
535         return bus_unit_method_kill(bus, message, u, error);
536 }
537
538 static int method_reset_failed_unit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
539         Manager *m = userdata;
540         const char *name;
541         Unit *u;
542         int r;
543
544         assert(bus);
545         assert(message);
546         assert(m);
547
548         r = sd_bus_message_read(message, "s", &name);
549         if (r < 0)
550                 return r;
551
552         u = manager_get_unit(m, name);
553         if (!u)
554                 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
555
556         return bus_unit_method_reset_failed(bus, message, u, error);
557 }
558
559 static int method_set_unit_properties(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
560         Manager *m = userdata;
561         const char *name;
562         Unit *u;
563         int r;
564
565         assert(bus);
566         assert(message);
567         assert(m);
568
569         r = sd_bus_message_read(message, "s", &name);
570         if (r < 0)
571                 return r;
572
573         u = manager_get_unit(m, name);
574         if (!u)
575                 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
576
577         return bus_unit_method_set_properties(bus, message, u, error);
578 }
579
580 static int method_start_transient_unit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
581         const char *name, *smode;
582         Manager *m = userdata;
583         JobMode mode;
584         UnitType t;
585         Unit *u;
586         int r;
587
588         assert(bus);
589         assert(message);
590         assert(m);
591
592         r = sd_bus_message_read(message, "ss", &name, &smode);
593         if (r < 0)
594                 return r;
595
596         t = unit_name_to_type(name);
597         if (t < 0)
598                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid unit type.");
599
600         if (!unit_vtable[t]->can_transient)
601                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unit type %s does not support transient units.", unit_type_to_string(t));
602
603         mode = job_mode_from_string(smode);
604         if (mode < 0)
605                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Job mode %s is invalid.", smode);
606
607         r = selinux_access_check(message, "start", error);
608         if (r < 0)
609                 return r;
610
611         r = manager_load_unit(m, name, NULL, error, &u);
612         if (r < 0)
613                 return r;
614
615         if (u->load_state != UNIT_NOT_FOUND || set_size(u->dependencies[UNIT_REFERENCED_BY]) > 0)
616                 return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS, "Unit %s already exists.", name);
617
618         /* OK, the unit failed to load and is unreferenced, now let's
619          * fill in the transient data instead */
620         r = unit_make_transient(u);
621         if (r < 0)
622                 return r;
623
624         /* Set our properties */
625         r = bus_unit_set_properties(u, message, UNIT_RUNTIME, false, error);
626         if (r < 0)
627                 return r;
628
629         /* And load this stub fully */
630         r = unit_load(u);
631         if (r < 0)
632                 return r;
633
634         manager_dispatch_load_queue(m);
635
636         /* Finally, start it */
637         return bus_unit_queue_job(bus, message, u, JOB_START, mode, false, error);
638 }
639
640 static int method_get_job(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
641         _cleanup_free_ char *path = NULL;
642         Manager *m = userdata;
643         uint32_t id;
644         Job *j;
645         int r;
646
647         assert(bus);
648         assert(message);
649         assert(m);
650
651         r = sd_bus_message_read(message, "u", &id);
652         if (r < 0)
653                 return r;
654
655         j = manager_get_job(m, id);
656         if (!j)
657                 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
658
659         r = selinux_unit_access_check(j->unit, message, "status", error);
660         if (r < 0)
661                 return r;
662
663         path = job_dbus_path(j);
664         if (!path)
665                 return -ENOMEM;
666
667         return sd_bus_reply_method_return(message, "o", path);
668 }
669
670 static int method_cancel_job(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
671         Manager *m = userdata;
672         uint32_t id;
673         Job *j;
674         int r;
675
676         assert(bus);
677         assert(message);
678         assert(m);
679
680         r = sd_bus_message_read(message, "u", &id);
681         if (r < 0)
682                 return r;
683
684         j = manager_get_job(m, id);
685         if (!j)
686                 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
687
688         r = selinux_unit_access_check(j->unit, message, "stop", error);
689         if (r < 0)
690                 return r;
691
692         job_finish_and_invalidate(j, JOB_CANCELED, true);
693
694         return sd_bus_reply_method_return(message, NULL);
695 }
696
697 static int method_clear_jobs(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
698         Manager *m = userdata;
699         int r;
700
701         assert(bus);
702         assert(message);
703         assert(m);
704
705         r = selinux_access_check(message, "reboot", error);
706         if (r < 0)
707                 return r;
708
709         manager_clear_jobs(m);
710
711         return sd_bus_reply_method_return(message, NULL);
712 }
713
714 static int method_reset_failed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
715         Manager *m = userdata;
716         int r;
717
718         assert(bus);
719         assert(message);
720         assert(m);
721
722         r = selinux_access_check(message, "reload", error);
723         if (r < 0)
724                 return r;
725
726         manager_reset_failed(m);
727
728         return sd_bus_reply_method_return(message, NULL);
729 }
730
731 static int method_list_units(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
732         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
733         Manager *m = userdata;
734         const char *k;
735         Iterator i;
736         Unit *u;
737         int r;
738
739         assert(bus);
740         assert(message);
741         assert(m);
742
743         r = selinux_access_check(message, "status", error);
744         if (r < 0)
745                 return r;
746
747         r = sd_bus_message_new_method_return(message, &reply);
748         if (r < 0)
749                 return r;
750
751         r = sd_bus_message_open_container(reply, 'a', "(ssssssouso)");
752         if (r < 0)
753                 return r;
754
755         HASHMAP_FOREACH_KEY(u, k, m->units, i) {
756                 _cleanup_free_ char *unit_path = NULL, *job_path = NULL;
757                 Unit *following;
758
759                 if (k != u->id)
760                         continue;
761
762                 following = unit_following(u);
763
764                 unit_path = unit_dbus_path(u);
765                 if (!unit_path)
766                         return -ENOMEM;
767
768                 if (u->job) {
769                         job_path = job_dbus_path(u->job);
770                         if (!job_path)
771                                 return -ENOMEM;
772                 }
773
774                 r = sd_bus_message_append(
775                                 reply, "(ssssssouso)",
776                                 u->id,
777                                 unit_description(u),
778                                 unit_load_state_to_string(u->load_state),
779                                 unit_active_state_to_string(unit_active_state(u)),
780                                 unit_sub_state_to_string(u),
781                                 following ? following->id : "",
782                                 unit_path,
783                                 u->job ? u->job->id : 0,
784                                 u->job ? job_type_to_string(u->job->type) : "",
785                                 job_path ? job_path : "/");
786                 if (r < 0)
787                         return r;
788         }
789
790         r = sd_bus_message_close_container(reply);
791         if (r < 0)
792                 return r;
793
794         return sd_bus_send(bus, reply, NULL);
795 }
796
797 static int method_list_jobs(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
798         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
799         Manager *m = userdata;
800         Iterator i;
801         Job *j;
802         int r;
803
804         assert(bus);
805         assert(message);
806         assert(m);
807
808         r = selinux_access_check(message, "status", error);
809         if (r < 0)
810                 return r;
811
812         r = sd_bus_message_new_method_return(message, &reply);
813         if (r < 0)
814                 return r;
815
816         r = sd_bus_message_open_container(reply, 'a', "(usssoo)");
817         if (r < 0)
818                 return r;
819
820         HASHMAP_FOREACH(j, m->jobs, i) {
821                 _cleanup_free_ char *unit_path = NULL, *job_path = NULL;
822
823                 job_path = job_dbus_path(j);
824                 if (!job_path)
825                         return -ENOMEM;
826
827                 unit_path = unit_dbus_path(j->unit);
828                 if (!unit_path)
829                         return -ENOMEM;
830
831                 r = sd_bus_message_append(
832                                 reply, "(usssoo)",
833                                 j->id,
834                                 j->unit->id,
835                                 job_type_to_string(j->type),
836                                 job_state_to_string(j->state),
837                                 job_path,
838                                 unit_path);
839                 if (r < 0)
840                         return r;
841         }
842
843         r = sd_bus_message_close_container(reply);
844         if (r < 0)
845                 return r;
846
847         return sd_bus_send(bus, reply, NULL);
848 }
849
850 static int method_subscribe(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
851         Manager *m = userdata;
852         int r;
853
854         assert(bus);
855         assert(message);
856         assert(m);
857
858         r = selinux_access_check(message, "status", error);
859         if (r < 0)
860                 return r;
861
862         if (bus == m->api_bus) {
863
864                 /* Note that direct bus connection subscribe by
865                  * default, we only track peers on the API bus here */
866
867                 if (!m->subscribed) {
868                         r = sd_bus_track_new(bus, &m->subscribed, NULL, NULL);
869                         if (r < 0)
870                                 return r;
871                 }
872
873                 r = sd_bus_track_add_sender(m->subscribed, message);
874                 if (r < 0)
875                         return r;
876                 if (r == 0)
877                         return sd_bus_error_setf(error, BUS_ERROR_ALREADY_SUBSCRIBED, "Client is already subscribed.");
878         }
879
880         return sd_bus_reply_method_return(message, NULL);
881 }
882
883 static int method_unsubscribe(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
884         Manager *m = userdata;
885         int r;
886
887         assert(bus);
888         assert(message);
889         assert(m);
890
891         r = selinux_access_check(message, "status", error);
892         if (r < 0)
893                 return r;
894
895         if (bus == m->api_bus) {
896                 r = sd_bus_track_remove_sender(m->subscribed, message);
897                 if (r < 0)
898                         return r;
899                 if (r == 0)
900                         return sd_bus_error_setf(error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
901         }
902
903         return sd_bus_reply_method_return(message, NULL);
904 }
905
906 static int method_dump(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
907         _cleanup_free_ char *dump = NULL;
908         _cleanup_fclose_ FILE *f = NULL;
909         Manager *m = userdata;
910         size_t size;
911         int r;
912
913         assert(bus);
914         assert(message);
915         assert(m);
916
917         r = selinux_access_check(message, "status", error);
918         if (r < 0)
919                 return r;
920
921         f = open_memstream(&dump, &size);
922         if (!f)
923                 return -ENOMEM;
924
925         manager_dump_units(m, f, NULL);
926         manager_dump_jobs(m, f, NULL);
927
928         fflush(f);
929
930         if (ferror(f))
931                 return -ENOMEM;
932
933         return sd_bus_reply_method_return(message, "s", dump);
934 }
935
936 static int method_create_snapshot(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
937         _cleanup_free_ char *path = NULL;
938         Manager *m = userdata;
939         const char *name;
940         int cleanup;
941         Snapshot *s = NULL;
942         int r;
943
944         assert(bus);
945         assert(message);
946         assert(m);
947
948         r = selinux_access_check(message, "start", error);
949         if (r < 0)
950                 return r;
951
952         r = sd_bus_message_read(message, "sb", &name, &cleanup);
953         if (r < 0)
954                 return r;
955
956         if (isempty(name))
957                 name = NULL;
958
959         r = snapshot_create(m, name, cleanup, error, &s);
960         if (r < 0)
961                 return r;
962
963         path = unit_dbus_path(UNIT(s));
964         if (!path)
965                 return -ENOMEM;
966
967         return sd_bus_reply_method_return(message, "o", path);
968 }
969
970 static int method_remove_snapshot(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
971         Manager *m = userdata;
972         const char *name;
973         Unit *u;
974         int r;
975
976         assert(bus);
977         assert(message);
978         assert(m);
979
980         r = selinux_access_check(message, "stop", error);
981         if (r < 0)
982                 return r;
983
984         r = sd_bus_message_read(message, "s", &name);
985         if (r < 0)
986                 return r;
987
988         u = manager_get_unit(m, name);
989         if (!u)
990                 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s does not exist.", name);
991
992         if (u->type != UNIT_SNAPSHOT)
993                 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not a snapshot", name);
994
995         return bus_snapshot_method_remove(bus, message, u, error);
996 }
997
998 static int method_reload(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
999         Manager *m = userdata;
1000         int r;
1001
1002         assert(bus);
1003         assert(message);
1004         assert(m);
1005
1006         r = selinux_access_check(message, "reload", error);
1007         if (r < 0)
1008                 return r;
1009
1010         /* Instead of sending the reply back right away, we just
1011          * remember that we need to and then send it after the reload
1012          * is finished. That way the caller knows when the reload
1013          * finished. */
1014
1015         assert(!m->queued_message);
1016         r = sd_bus_message_new_method_return(message, &m->queued_message);
1017         if (r < 0)
1018                 return r;
1019
1020         m->queued_message_bus = sd_bus_ref(bus);
1021         m->exit_code = MANAGER_RELOAD;
1022
1023         return 1;
1024 }
1025
1026 static int method_reexecute(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1027         Manager *m = userdata;
1028         int r;
1029
1030         assert(bus);
1031         assert(message);
1032         assert(m);
1033
1034         r = selinux_access_check(message, "reload", error);
1035         if (r < 0)
1036                 return r;
1037
1038         /* We don't send a reply back here, the client should
1039          * just wait for us disconnecting. */
1040
1041         m->exit_code = MANAGER_REEXECUTE;
1042         return 1;
1043 }
1044
1045 static int method_exit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1046         Manager *m = userdata;
1047         int r;
1048
1049         assert(bus);
1050         assert(message);
1051         assert(m);
1052
1053         r = selinux_access_check(message, "halt", error);
1054         if (r < 0)
1055                 return r;
1056
1057         if (m->running_as == SYSTEMD_SYSTEM)
1058                 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for user service managers.");
1059
1060         m->exit_code = MANAGER_EXIT;
1061
1062         return sd_bus_reply_method_return(message, NULL);
1063 }
1064
1065 static int method_reboot(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1066         Manager *m = userdata;
1067         int r;
1068
1069         assert(bus);
1070         assert(message);
1071         assert(m);
1072
1073         r = selinux_access_check(message, "reboot", error);
1074         if (r < 0)
1075                 return r;
1076
1077         if (m->running_as != SYSTEMD_SYSTEM)
1078                 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Reboot is only supported for system managers.");
1079
1080         m->exit_code = MANAGER_REBOOT;
1081
1082         return sd_bus_reply_method_return(message, NULL);
1083 }
1084
1085
1086 static int method_poweroff(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1087         Manager *m = userdata;
1088         int r;
1089
1090         assert(bus);
1091         assert(message);
1092         assert(m);
1093
1094         r = selinux_access_check(message, "halt", error);
1095         if (r < 0)
1096                 return r;
1097
1098         if (m->running_as != SYSTEMD_SYSTEM)
1099                 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Powering off is only supported for system managers.");
1100
1101         m->exit_code = MANAGER_POWEROFF;
1102
1103         return sd_bus_reply_method_return(message, NULL);
1104 }
1105
1106 static int method_halt(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1107         Manager *m = userdata;
1108         int r;
1109
1110         assert(bus);
1111         assert(message);
1112         assert(m);
1113
1114         r = selinux_access_check(message, "halt", error);
1115         if (r < 0)
1116                 return r;
1117
1118         if (m->running_as != SYSTEMD_SYSTEM)
1119                 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Halt is only supported for system managers.");
1120
1121         m->exit_code = MANAGER_HALT;
1122
1123         return sd_bus_reply_method_return(message, NULL);
1124 }
1125
1126 static int method_kexec(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1127         Manager *m = userdata;
1128         int r;
1129
1130         assert(bus);
1131         assert(message);
1132         assert(m);
1133
1134         r = selinux_access_check(message, "reboot", error);
1135         if (r < 0)
1136                 return r;
1137
1138         if (m->running_as != SYSTEMD_SYSTEM)
1139                 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "KExec is only supported for system managers.");
1140
1141         m->exit_code = MANAGER_KEXEC;
1142
1143         return sd_bus_reply_method_return(message, NULL);
1144 }
1145
1146 static int method_switch_root(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1147         char *ri = NULL, *rt = NULL;
1148         const char *root, *init;
1149         Manager *m = userdata;
1150         int r;
1151
1152         assert(bus);
1153         assert(message);
1154         assert(m);
1155
1156         r = selinux_access_check(message, "reboot", error);
1157         if (r < 0)
1158                 return r;
1159
1160         if (m->running_as != SYSTEMD_SYSTEM)
1161                 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "KExec is only supported for system managers.");
1162
1163         r = sd_bus_message_read(message, "ss", &root, &init);
1164         if (r < 0)
1165                 return r;
1166
1167         if (path_equal(root, "/") || !path_is_absolute(root))
1168                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid switch root path %s", root);
1169
1170         /* Safety check */
1171         if (isempty(init)) {
1172                 if (! path_is_os_tree(root))
1173                         return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified switch root path %s does not seem to be an OS tree. /etc/os-release is missing.", root);
1174         } else {
1175                 _cleanup_free_ char *p = NULL;
1176
1177                 if (!path_is_absolute(init))
1178                         return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid init path %s", init);
1179
1180                 p = strappend(root, init);
1181                 if (!p)
1182                         return -ENOMEM;
1183
1184                 if (access(p, X_OK) < 0)
1185                         return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified init binary %s does not exist.", p);
1186         }
1187
1188         rt = strdup(root);
1189         if (!rt)
1190                 return -ENOMEM;
1191
1192         if (!isempty(init)) {
1193                 ri = strdup(init);
1194                 if (!ri) {
1195                         free(rt);
1196                         return -ENOMEM;
1197                 }
1198         }
1199
1200         free(m->switch_root);
1201         m->switch_root = rt;
1202
1203         free(m->switch_root_init);
1204         m->switch_root_init = ri;
1205
1206         m->exit_code = MANAGER_SWITCH_ROOT;
1207
1208         return sd_bus_reply_method_return(message, NULL);
1209 }
1210
1211 static int method_set_environment(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1212         _cleanup_strv_free_ char **plus = NULL;
1213         Manager *m = userdata;
1214         int r;
1215
1216         assert(bus);
1217         assert(message);
1218         assert(m);
1219
1220         r = selinux_access_check(message, "reload", error);
1221         if (r < 0)
1222                 return r;
1223
1224         r = sd_bus_message_read_strv(message, &plus);
1225         if (r < 0)
1226                 return r;
1227         if (!strv_env_is_valid(plus))
1228                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid environment assignments");
1229
1230         r = manager_environment_add(m, NULL, plus);
1231         if (r < 0)
1232                 return r;
1233
1234         return sd_bus_reply_method_return(message, NULL);
1235 }
1236
1237 static int method_unset_environment(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1238         _cleanup_strv_free_ char **minus = NULL;
1239         Manager *m = userdata;
1240         int r;
1241
1242         assert(bus);
1243         assert(message);
1244         assert(m);
1245
1246         r = selinux_access_check(message, "reload", error);
1247         if (r < 0)
1248                 return r;
1249
1250         r = sd_bus_message_read_strv(message, &minus);
1251         if (r < 0)
1252                 return r;
1253
1254         if (!strv_env_name_or_assignment_is_valid(minus))
1255                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid environment variable names or assignments");
1256
1257         r = manager_environment_add(m, minus, NULL);
1258         if (r < 0)
1259                 return r;
1260
1261         return sd_bus_reply_method_return(message, NULL);
1262 }
1263
1264 static int method_unset_and_set_environment(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1265         _cleanup_strv_free_ char **minus = NULL, **plus = NULL;
1266         Manager *m = userdata;
1267         int r;
1268
1269         assert(bus);
1270         assert(message);
1271         assert(m);
1272
1273         r = selinux_access_check(message, "reload", error);
1274         if (r < 0)
1275                 return r;
1276
1277         r = sd_bus_message_read_strv(message, &plus);
1278         if (r < 0)
1279                 return r;
1280
1281         r = sd_bus_message_read_strv(message, &minus);
1282         if (r < 0)
1283                 return r;
1284
1285         if (!strv_env_is_valid(plus))
1286                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid environment assignments");
1287         if (!strv_env_name_or_assignment_is_valid(minus))
1288                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid environment variable names or assignments");
1289
1290         r = manager_environment_add(m, minus, plus);
1291         if (r < 0)
1292                 return r;
1293
1294         return sd_bus_reply_method_return(message, NULL);
1295 }
1296
1297 static int method_list_unit_files(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1298         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1299         Manager *m = userdata;
1300         UnitFileList *item;
1301         Hashmap *h;
1302         Iterator i;
1303         int r;
1304
1305         assert(bus);
1306         assert(message);
1307         assert(m);
1308
1309         r = selinux_access_check(message, "status", error);
1310         if (r < 0)
1311                 return r;
1312
1313         r = sd_bus_message_new_method_return(message, &reply);
1314         if (r < 0)
1315                 return r;
1316
1317         h = hashmap_new(string_hash_func, string_compare_func);
1318         if (!h)
1319                 return -ENOMEM;
1320
1321         r = unit_file_get_list(m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER, NULL, h);
1322         if (r < 0)
1323                 goto fail;
1324
1325         r = sd_bus_message_open_container(reply, 'a', "(ss)");
1326         if (r < 0)
1327                 goto fail;
1328
1329         HASHMAP_FOREACH(item, h, i) {
1330
1331                 r = sd_bus_message_append(reply, "(ss)", item->path, unit_file_state_to_string(item->state));
1332                 if (r < 0)
1333                         goto fail;
1334         }
1335
1336         unit_file_list_free(h);
1337
1338         r = sd_bus_message_close_container(reply);
1339         if (r < 0)
1340                 return r;
1341
1342         return sd_bus_send(bus, reply, NULL);
1343
1344 fail:
1345         unit_file_list_free(h);
1346         return r;
1347 }
1348
1349 static int method_get_unit_file_state(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1350         Manager *m = userdata;
1351         const char *name;
1352         UnitFileState state;
1353         UnitFileScope scope;
1354         int r;
1355
1356         assert(bus);
1357         assert(message);
1358         assert(m);
1359
1360         r = selinux_access_check(message, "status", error);
1361         if (r < 0)
1362                 return r;
1363
1364         r = sd_bus_message_read(message, "s", &name);
1365         if (r < 0)
1366                 return r;
1367
1368         scope = m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
1369
1370         state = unit_file_get_state(scope, NULL, name);
1371         if (state < 0)
1372                 return state;
1373
1374         return sd_bus_reply_method_return(message, "s", unit_file_state_to_string(state));
1375 }
1376
1377 static int method_get_default_target(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1378         _cleanup_free_ char *default_target = NULL;
1379         Manager *m = userdata;
1380         UnitFileScope scope;
1381         int r;
1382
1383         assert(bus);
1384         assert(message);
1385         assert(m);
1386
1387         r = selinux_access_check(message, "status", error);
1388         if (r < 0)
1389                 return r;
1390
1391         scope = m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
1392
1393         r = unit_file_get_default(scope, NULL, &default_target);
1394         if (r < 0)
1395                 return r;
1396
1397         return sd_bus_reply_method_return(message, "s", default_target);
1398 }
1399
1400 static int send_unit_files_changed(sd_bus *bus, void *userdata) {
1401         _cleanup_bus_message_unref_ sd_bus_message *message = NULL;
1402         int r;
1403
1404         assert(bus);
1405
1406         r = sd_bus_message_new_signal(bus, &message, "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitFilesChanged");
1407         if (r < 0)
1408                 return r;
1409
1410         return sd_bus_send(bus, message, NULL);
1411 }
1412
1413 static int reply_unit_file_changes_and_free(
1414                 Manager *m,
1415                 sd_bus *bus,
1416                 sd_bus_message *message,
1417                 int carries_install_info,
1418                 UnitFileChange *changes,
1419                 unsigned n_changes) {
1420
1421         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
1422         unsigned i;
1423         int r;
1424
1425         if (n_changes > 0) {
1426                 r = bus_foreach_bus(m, NULL, send_unit_files_changed, NULL);
1427                 if (r < 0)
1428                         log_debug("Failed to send UnitFilesChanged signal: %s", strerror(-r));
1429         }
1430
1431         r = sd_bus_message_new_method_return(message, &reply);
1432         if (r < 0)
1433                 goto fail;
1434
1435         if (carries_install_info >= 0) {
1436                 r = sd_bus_message_append(reply, "b", carries_install_info);
1437                 if (r < 0)
1438                         goto fail;
1439         }
1440
1441         r = sd_bus_message_open_container(reply, 'a', "(sss)");
1442         if (r < 0)
1443                 goto fail;
1444
1445         for (i = 0; i < n_changes; i++) {
1446                 r = sd_bus_message_append(
1447                                 reply, "(sss)",
1448                                 unit_file_change_type_to_string(changes[i].type),
1449                                 changes[i].path,
1450                                 changes[i].source);
1451                 if (r < 0)
1452                         goto fail;
1453         }
1454
1455         r = sd_bus_message_close_container(reply);
1456         if (r < 0)
1457                 goto fail;
1458
1459         return sd_bus_send(bus, reply, NULL);
1460
1461 fail:
1462         unit_file_changes_free(changes, n_changes);
1463         return r;
1464 }
1465
1466 static int method_enable_unit_files_generic(
1467                 sd_bus *bus,
1468                 sd_bus_message *message,
1469                 Manager *m, const
1470                 char *verb,
1471                 int (*call)(UnitFileScope scope, bool runtime, const char *root_dir, char *files[], bool force, UnitFileChange **changes, unsigned *n_changes),
1472                 bool carries_install_info,
1473                 sd_bus_error *error) {
1474
1475         _cleanup_strv_free_ char **l = NULL;
1476 #ifdef HAVE_SELINUX
1477         char **i;
1478 #endif
1479         UnitFileChange *changes = NULL;
1480         unsigned n_changes = 0;
1481         UnitFileScope scope;
1482         int runtime, force, r;
1483
1484         assert(bus);
1485         assert(message);
1486         assert(m);
1487
1488         r = sd_bus_message_read_strv(message, &l);
1489         if (r < 0)
1490                 return r;
1491
1492 #ifdef HAVE_SELINUX
1493         STRV_FOREACH(i, l) {
1494                 Unit *u;
1495
1496                 u = manager_get_unit(m, *i);
1497                 if (u) {
1498                         r = selinux_unit_access_check(u, message, verb, error);
1499                         if (r < 0)
1500                                 return r;
1501                 }
1502         }
1503 #endif
1504
1505         r = sd_bus_message_read(message, "bb", &runtime, &force);
1506         if (r < 0)
1507                 return r;
1508
1509         scope = m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
1510
1511         r = call(scope, runtime, NULL, l, force, &changes, &n_changes);
1512         if (r < 0)
1513                 return r;
1514
1515         return reply_unit_file_changes_and_free(m, bus, message, carries_install_info ? r : -1, changes, n_changes);
1516 }
1517
1518 static int method_enable_unit_files(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1519         return method_enable_unit_files_generic(bus, message, userdata, "enable", unit_file_enable, true, error);
1520 }
1521
1522 static int method_reenable_unit_files(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1523         return method_enable_unit_files_generic(bus, message, userdata, "enable", unit_file_reenable, true, error);
1524 }
1525
1526 static int method_link_unit_files(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1527         return method_enable_unit_files_generic(bus, message, userdata, "enable", unit_file_link, false, error);
1528 }
1529
1530 static int method_preset_unit_files(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1531         return method_enable_unit_files_generic(bus, message, userdata, "enable", unit_file_preset, true, error);
1532 }
1533
1534 static int method_mask_unit_files(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1535         return method_enable_unit_files_generic(bus, message, userdata, "disable", unit_file_mask, false, error);
1536 }
1537
1538 static int method_disable_unit_files_generic(
1539                 sd_bus *bus,
1540                 sd_bus_message *message,
1541                 Manager *m, const
1542                 char *verb,
1543                 int (*call)(UnitFileScope scope, bool runtime, const char *root_dir, char *files[], UnitFileChange **changes, unsigned *n_changes),
1544                 sd_bus_error *error) {
1545
1546         _cleanup_strv_free_ char **l = NULL;
1547         UnitFileChange *changes = NULL;
1548         unsigned n_changes = 0;
1549         UnitFileScope scope;
1550         int r, runtime;
1551
1552         assert(bus);
1553         assert(message);
1554         assert(m);
1555
1556         r = selinux_access_check(message, verb, error);
1557         if (r < 0)
1558                 return r;
1559
1560         r = sd_bus_message_read_strv(message, &l);
1561         if (r < 0)
1562                 return r;
1563
1564         r = sd_bus_message_read(message, "b", &runtime);
1565         if (r < 0)
1566                 return r;
1567
1568         scope = m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
1569
1570         r = call(scope, runtime, NULL, l, &changes, &n_changes);
1571         if (r < 0)
1572                 return r;
1573
1574         return reply_unit_file_changes_and_free(m, bus, message, -1, changes, n_changes);
1575 }
1576
1577 static int method_disable_unit_files(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1578         return method_disable_unit_files_generic(bus, message, userdata, "disable", unit_file_disable, error);
1579 }
1580
1581 static int method_unmask_unit_files(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1582         return method_disable_unit_files_generic(bus, message, userdata, "enable", unit_file_unmask, error);
1583 }
1584
1585 static int method_set_default_target(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
1586         UnitFileChange *changes = NULL;
1587         unsigned n_changes = 0;
1588         Manager *m = userdata;
1589         UnitFileScope scope;
1590         const char *name;
1591         int force, r;
1592
1593         assert(bus);
1594         assert(message);
1595         assert(m);
1596
1597         r = selinux_access_check(message, "enable", error);
1598         if (r < 0)
1599                 return r;
1600
1601         r = sd_bus_message_read(message, "sb", &name, &force);
1602         if (r < 0)
1603                 return r;
1604
1605         scope = m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
1606
1607         r = unit_file_set_default(scope, NULL, name, force, &changes, &n_changes);
1608         if (r < 0)
1609                 return r;
1610
1611         return reply_unit_file_changes_and_free(m, bus, message, -1, changes, n_changes);
1612 }
1613
1614 const sd_bus_vtable bus_manager_vtable[] = {
1615         SD_BUS_VTABLE_START(0),
1616
1617         SD_BUS_PROPERTY("Version", "s", property_get_version, 0, SD_BUS_VTABLE_PROPERTY_CONST),
1618         SD_BUS_PROPERTY("Features", "s", property_get_features, 0, SD_BUS_VTABLE_PROPERTY_CONST),
1619         SD_BUS_PROPERTY("Virtualization", "s", property_get_virtualization, 0, SD_BUS_VTABLE_PROPERTY_CONST),
1620         SD_BUS_PROPERTY("Architecture", "s", property_get_architecture, 0, SD_BUS_VTABLE_PROPERTY_CONST),
1621         SD_BUS_PROPERTY("Tainted", "s", property_get_tainted, 0, SD_BUS_VTABLE_PROPERTY_CONST),
1622         BUS_PROPERTY_DUAL_TIMESTAMP("FirmwareTimestamp", offsetof(Manager, firmware_timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
1623         BUS_PROPERTY_DUAL_TIMESTAMP("LoaderTimestamp", offsetof(Manager, loader_timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
1624         BUS_PROPERTY_DUAL_TIMESTAMP("KernelTimestamp", offsetof(Manager, kernel_timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
1625         BUS_PROPERTY_DUAL_TIMESTAMP("InitRDTimestamp", offsetof(Manager, initrd_timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
1626         BUS_PROPERTY_DUAL_TIMESTAMP("UserspaceTimestamp", offsetof(Manager, userspace_timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
1627         BUS_PROPERTY_DUAL_TIMESTAMP("FinishTimestamp", offsetof(Manager, finish_timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
1628         BUS_PROPERTY_DUAL_TIMESTAMP("SecurityStartTimestamp", offsetof(Manager, security_start_timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
1629         BUS_PROPERTY_DUAL_TIMESTAMP("SecurityFinishTimestamp", offsetof(Manager, security_finish_timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
1630         BUS_PROPERTY_DUAL_TIMESTAMP("GeneratorsStartTimestamp", offsetof(Manager, generators_start_timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
1631         BUS_PROPERTY_DUAL_TIMESTAMP("GeneratorsFinishTimestamp", offsetof(Manager, generators_finish_timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
1632         BUS_PROPERTY_DUAL_TIMESTAMP("UnitsLoadStartTimestamp", offsetof(Manager, units_load_start_timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
1633         BUS_PROPERTY_DUAL_TIMESTAMP("UnitsLoadFinishTimestamp", offsetof(Manager, units_load_finish_timestamp), SD_BUS_VTABLE_PROPERTY_CONST),
1634         SD_BUS_WRITABLE_PROPERTY("LogLevel", "s", property_get_log_level, property_set_log_level, 0, 0),
1635         SD_BUS_WRITABLE_PROPERTY("LogTarget", "s", property_get_log_target, property_set_log_target, 0, 0),
1636         SD_BUS_PROPERTY("NNames", "u", property_get_n_names, 0, 0),
1637         SD_BUS_PROPERTY("NFailedUnits", "u", property_get_n_failed_units, 0, 0),
1638         SD_BUS_PROPERTY("NJobs", "u", property_get_n_jobs, 0, 0),
1639         SD_BUS_PROPERTY("NInstalledJobs", "u", bus_property_get_unsigned, offsetof(Manager, n_installed_jobs), 0),
1640         SD_BUS_PROPERTY("NFailedJobs", "u", bus_property_get_unsigned, offsetof(Manager, n_failed_jobs), 0),
1641         SD_BUS_PROPERTY("Progress", "d", property_get_progress, 0, 0),
1642         SD_BUS_PROPERTY("Environment", "as", NULL, offsetof(Manager, environment), 0),
1643         SD_BUS_PROPERTY("ConfirmSpawn", "b", bus_property_get_bool, offsetof(Manager, confirm_spawn), SD_BUS_VTABLE_PROPERTY_CONST),
1644         SD_BUS_PROPERTY("ShowStatus", "b", bus_property_get_bool, offsetof(Manager, show_status), SD_BUS_VTABLE_PROPERTY_CONST),
1645         SD_BUS_PROPERTY("UnitPath", "as", NULL, offsetof(Manager, lookup_paths.unit_path), SD_BUS_VTABLE_PROPERTY_CONST),
1646         SD_BUS_PROPERTY("DefaultStandardOutput", "s", bus_property_get_exec_output, offsetof(Manager, default_std_output), SD_BUS_VTABLE_PROPERTY_CONST),
1647         SD_BUS_PROPERTY("DefaultStandardError", "s", bus_property_get_exec_output, offsetof(Manager, default_std_output), SD_BUS_VTABLE_PROPERTY_CONST),
1648         SD_BUS_WRITABLE_PROPERTY("RuntimeWatchdogUSec", "t", bus_property_get_usec, property_set_runtime_watchdog, offsetof(Manager, runtime_watchdog), SD_BUS_VTABLE_PROPERTY_CONST),
1649         SD_BUS_WRITABLE_PROPERTY("ShutdownWatchdogUSec", "t", bus_property_get_usec, bus_property_set_usec, offsetof(Manager, shutdown_watchdog), SD_BUS_VTABLE_PROPERTY_CONST),
1650         SD_BUS_PROPERTY("ControlGroup", "s", NULL, offsetof(Manager, cgroup_root), 0),
1651         SD_BUS_PROPERTY("SystemState", "s", property_get_system_state, 0, 0),
1652
1653         SD_BUS_METHOD("GetUnit", "s", "o", method_get_unit, SD_BUS_VTABLE_UNPRIVILEGED),
1654         SD_BUS_METHOD("GetUnitByPID", "u", "o", method_get_unit_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
1655         SD_BUS_METHOD("LoadUnit", "s", "o", method_load_unit, SD_BUS_VTABLE_UNPRIVILEGED),
1656         SD_BUS_METHOD("StartUnit", "ss", "o", method_start_unit, 0),
1657         SD_BUS_METHOD("StartUnitReplace", "sss", "o", method_start_unit_replace, 0),
1658         SD_BUS_METHOD("StopUnit", "ss", "o", method_stop_unit, 0),
1659         SD_BUS_METHOD("ReloadUnit", "ss", "o", method_reload_unit, 0),
1660         SD_BUS_METHOD("RestartUnit", "ss", "o", method_restart_unit, 0),
1661         SD_BUS_METHOD("TryRestartUnit", "ss", "o", method_try_restart_unit, 0),
1662         SD_BUS_METHOD("ReloadOrRestartUnit", "ss", "o", method_reload_or_restart_unit, 0),
1663         SD_BUS_METHOD("ReloadOrTryRestartUnit", "ss", "o", method_reload_or_try_restart_unit, 0),
1664         SD_BUS_METHOD("KillUnit", "ssi", NULL, method_kill_unit, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)),
1665         SD_BUS_METHOD("ResetFailedUnit", "s", NULL, method_reset_failed_unit, 0),
1666         SD_BUS_METHOD("SetUnitProperties", "sba(sv)", NULL, method_set_unit_properties, 0),
1667         SD_BUS_METHOD("StartTransientUnit", "ssa(sv)a(sa(sv))", "o", method_start_transient_unit, 0),
1668         SD_BUS_METHOD("GetJob", "u", "o", method_get_job, SD_BUS_VTABLE_UNPRIVILEGED),
1669         SD_BUS_METHOD("CancelJob", "u", NULL, method_cancel_job, 0),
1670         SD_BUS_METHOD("ClearJobs", NULL, NULL, method_clear_jobs, 0),
1671         SD_BUS_METHOD("ResetFailed", NULL, NULL, method_reset_failed, 0),
1672         SD_BUS_METHOD("ListUnits", NULL, "a(ssssssouso)", method_list_units, SD_BUS_VTABLE_UNPRIVILEGED),
1673         SD_BUS_METHOD("ListJobs", NULL, "a(usssoo)", method_list_jobs, SD_BUS_VTABLE_UNPRIVILEGED),
1674         SD_BUS_METHOD("Subscribe", NULL, NULL, method_subscribe, SD_BUS_VTABLE_UNPRIVILEGED),
1675         SD_BUS_METHOD("Unsubscribe", NULL, NULL, method_unsubscribe, SD_BUS_VTABLE_UNPRIVILEGED),
1676         SD_BUS_METHOD("Dump", NULL, "s", method_dump, SD_BUS_VTABLE_UNPRIVILEGED),
1677         SD_BUS_METHOD("CreateSnapshot", "sb", "o", method_create_snapshot, 0),
1678         SD_BUS_METHOD("RemoveSnapshot", "s", NULL, method_remove_snapshot, 0),
1679         SD_BUS_METHOD("Reload", NULL, NULL, method_reload, 0),
1680         SD_BUS_METHOD("Reexecute", NULL, NULL, method_reexecute, 0),
1681         SD_BUS_METHOD("Exit", NULL, NULL, method_exit, 0),
1682         SD_BUS_METHOD("Reboot", NULL, NULL, method_reboot, SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)),
1683         SD_BUS_METHOD("PowerOff", NULL, NULL, method_poweroff, SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)),
1684         SD_BUS_METHOD("Halt", NULL, NULL, method_halt, SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)),
1685         SD_BUS_METHOD("KExec", NULL, NULL, method_kexec, SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)),
1686         SD_BUS_METHOD("SwitchRoot", "ss", NULL, method_switch_root, SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)),
1687         SD_BUS_METHOD("SetEnvironment", "as", NULL, method_set_environment, 0),
1688         SD_BUS_METHOD("UnsetEnvironment", "as", NULL, method_unset_environment, 0),
1689         SD_BUS_METHOD("UnsetAndSetEnvironment", "asas", NULL, method_unset_and_set_environment, 0),
1690         SD_BUS_METHOD("ListUnitFiles", NULL, "a(ss)", method_list_unit_files, SD_BUS_VTABLE_UNPRIVILEGED),
1691         SD_BUS_METHOD("GetUnitFileState", "s", "s", method_get_unit_file_state, SD_BUS_VTABLE_UNPRIVILEGED),
1692         SD_BUS_METHOD("EnableUnitFiles", "asbb", "ba(sss)", method_enable_unit_files, 0),
1693         SD_BUS_METHOD("DisableUnitFiles", "asb", "a(sss)", method_disable_unit_files, 0),
1694         SD_BUS_METHOD("ReenableUnitFiles", "asbb", "ba(sss)", method_reenable_unit_files, 0),
1695         SD_BUS_METHOD("LinkUnitFiles", "asbb", "a(sss)", method_link_unit_files, 0),
1696         SD_BUS_METHOD("PresetUnitFiles", "asbb", "ba(sss)", method_preset_unit_files, 0),
1697         SD_BUS_METHOD("MaskUnitFiles", "asbb", "a(sss)", method_mask_unit_files, 0),
1698         SD_BUS_METHOD("UnmaskUnitFiles", "asb", "a(sss)", method_unmask_unit_files, 0),
1699         SD_BUS_METHOD("SetDefaultTarget", "sb", "a(sss)", method_set_default_target, 0),
1700         SD_BUS_METHOD("GetDefaultTarget", NULL, "s", method_get_default_target, SD_BUS_VTABLE_UNPRIVILEGED),
1701
1702         SD_BUS_SIGNAL("UnitNew", "so", 0),
1703         SD_BUS_SIGNAL("UnitRemoved", "so", 0),
1704         SD_BUS_SIGNAL("JobNew", "uos", 0),
1705         SD_BUS_SIGNAL("JobRemoved", "uoss", 0),
1706         SD_BUS_SIGNAL("StartupFinished", "tttttt", 0),
1707         SD_BUS_SIGNAL("UnitFilesChanged", NULL, 0),
1708         SD_BUS_SIGNAL("Reloading", "b", 0),
1709
1710         SD_BUS_VTABLE_END
1711 };
1712
1713 static int send_finished(sd_bus *bus, void *userdata) {
1714         _cleanup_bus_message_unref_ sd_bus_message *message = NULL;
1715         usec_t *times = userdata;
1716         int r;
1717
1718         assert(bus);
1719         assert(times);
1720
1721         r = sd_bus_message_new_signal(bus, &message, "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StartupFinished");
1722         if (r < 0)
1723                 return r;
1724
1725         r = sd_bus_message_append(message, "tttttt", times[0], times[1], times[2], times[3], times[4], times[5]);
1726         if (r < 0)
1727                 return r;
1728
1729         return sd_bus_send(bus, message, NULL);
1730 }
1731
1732 void bus_manager_send_finished(
1733                 Manager *m,
1734                 usec_t firmware_usec,
1735                 usec_t loader_usec,
1736                 usec_t kernel_usec,
1737                 usec_t initrd_usec,
1738                 usec_t userspace_usec,
1739                 usec_t total_usec) {
1740
1741         int r;
1742
1743         assert(m);
1744
1745         r = bus_foreach_bus(
1746                         m,
1747                         NULL,
1748                         send_finished,
1749                         (usec_t[6]) {
1750                                 firmware_usec,
1751                                 loader_usec,
1752                                 kernel_usec,
1753                                 initrd_usec,
1754                                 userspace_usec,
1755                                 total_usec
1756                         });
1757         if (r < 0)
1758                 log_debug("Failed to send finished signal: %s", strerror(-r));
1759 }
1760
1761 static int send_reloading(sd_bus *bus, void *userdata) {
1762         _cleanup_bus_message_unref_ sd_bus_message *message = NULL;
1763         int r;
1764
1765         assert(bus);
1766
1767         r = sd_bus_message_new_signal(bus, &message, "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "Reloading");
1768         if (r < 0)
1769                 return r;
1770
1771         r = sd_bus_message_append(message, "b", PTR_TO_INT(userdata));
1772         if (r < 0)
1773                 return r;
1774
1775         return sd_bus_send(bus, message, NULL);
1776 }
1777
1778 void bus_manager_send_reloading(Manager *m, bool active) {
1779         int r;
1780
1781         assert(m);
1782
1783         r = bus_foreach_bus(m, NULL, send_reloading, INT_TO_PTR(active));
1784         if (r < 0)
1785                 log_debug("Failed to send reloading signal: %s", strerror(-r));
1786
1787 }