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