1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include <errno.h>
4 #include <sys/prctl.h>
5 #include <sys/statvfs.h>
6 #include <unistd.h>
7 
8 #include "alloc-util.h"
9 #include "architecture.h"
10 #include "build.h"
11 #include "bus-common-errors.h"
12 #include "bus-get-properties.h"
13 #include "bus-log-control-api.h"
14 #include "chase-symlinks.h"
15 #include "data-fd-util.h"
16 #include "dbus-cgroup.h"
17 #include "dbus-execute.h"
18 #include "dbus-job.h"
19 #include "dbus-manager.h"
20 #include "dbus-scope.h"
21 #include "dbus-service.h"
22 #include "dbus-unit.h"
23 #include "dbus.h"
24 #include "env-util.h"
25 #include "fd-util.h"
26 #include "fileio.h"
27 #include "format-util.h"
28 #include "install.h"
29 #include "log.h"
30 #include "manager-dump.h"
31 #include "os-util.h"
32 #include "parse-util.h"
33 #include "path-util.h"
34 #include "process-util.h"
35 #include "selinux-access.h"
36 #include "stat-util.h"
37 #include "string-util.h"
38 #include "strv.h"
39 #include "syslog-util.h"
40 #include "user-util.h"
41 #include "virt.h"
42 #include "watchdog.h"
43 
44 /* Require 16MiB free in /run/systemd for reloading/reexecing. After all we need to serialize our state
45  * there, and if we can't we'll fail badly. */
46 #define RELOAD_DISK_SPACE_MIN (UINT64_C(16) * UINT64_C(1024) * UINT64_C(1024))
47 
unit_file_bools_to_flags(bool runtime,bool force)48 static UnitFileFlags unit_file_bools_to_flags(bool runtime, bool force) {
49         return (runtime ? UNIT_FILE_RUNTIME : 0) |
50                (force   ? UNIT_FILE_FORCE   : 0);
51 }
52 
53 BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_oom_policy, oom_policy, OOMPolicy);
54 BUS_DEFINE_PROPERTY_GET_ENUM(bus_property_get_emergency_action, emergency_action, EmergencyAction);
55 
56 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_version, "s", GIT_VERSION);
57 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_features, "s", systemd_features);
58 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_architecture, "s", architecture_to_string(uname_architecture()));
59 static BUS_DEFINE_PROPERTY_GET2(property_get_system_state, "s", Manager, manager_state, manager_state_to_string);
60 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_timer_slack_nsec, "t", (uint64_t) prctl(PR_GET_TIMERSLACK));
61 static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size, "u", Hashmap *, hashmap_size);
62 static BUS_DEFINE_PROPERTY_GET_REF(property_get_set_size, "u", Set *, set_size);
63 static BUS_DEFINE_PROPERTY_GET(property_get_default_timeout_abort_usec, "t", Manager, manager_default_timeout_abort_usec);
64 
property_get_virtualization(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * reply,void * userdata,sd_bus_error * error)65 static int property_get_virtualization(
66                 sd_bus *bus,
67                 const char *path,
68                 const char *interface,
69                 const char *property,
70                 sd_bus_message *reply,
71                 void *userdata,
72                 sd_bus_error *error) {
73 
74         Virtualization v;
75 
76         assert(bus);
77         assert(reply);
78 
79         v = detect_virtualization();
80 
81         /* Make sure to return the empty string when we detect no virtualization, as that is the API.
82          *
83          * https://github.com/systemd/systemd/issues/1423
84          */
85 
86         return sd_bus_message_append(
87                         reply, "s",
88                         v == VIRTUALIZATION_NONE ? NULL : virtualization_to_string(v));
89 }
90 
property_get_tainted(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * reply,void * userdata,sd_bus_error * error)91 static int property_get_tainted(
92                 sd_bus *bus,
93                 const char *path,
94                 const char *interface,
95                 const char *property,
96                 sd_bus_message *reply,
97                 void *userdata,
98                 sd_bus_error *error) {
99 
100         _cleanup_free_ char *s = NULL;
101         Manager *m = userdata;
102 
103         assert(bus);
104         assert(reply);
105         assert(m);
106 
107         s = manager_taint_string(m);
108         if (!s)
109                 return log_oom();
110 
111         return sd_bus_message_append(reply, "s", s);
112 }
113 
property_set_log_target(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * value,void * userdata,sd_bus_error * error)114 static int property_set_log_target(
115                 sd_bus *bus,
116                 const char *path,
117                 const char *interface,
118                 const char *property,
119                 sd_bus_message *value,
120                 void *userdata,
121                 sd_bus_error *error) {
122 
123         Manager *m = userdata;
124         const char *t;
125         int r;
126 
127         assert(bus);
128         assert(value);
129 
130         r = sd_bus_message_read(value, "s", &t);
131         if (r < 0)
132                 return r;
133 
134         if (isempty(t))
135                 manager_restore_original_log_target(m);
136         else {
137                 LogTarget target;
138 
139                 target = log_target_from_string(t);
140                 if (target < 0)
141                         return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid log target '%s'", t);
142 
143                 manager_override_log_target(m, target);
144         }
145 
146         return 0;
147 }
148 
property_set_log_level(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * value,void * userdata,sd_bus_error * error)149 static int property_set_log_level(
150                 sd_bus *bus,
151                 const char *path,
152                 const char *interface,
153                 const char *property,
154                 sd_bus_message *value,
155                 void *userdata,
156                 sd_bus_error *error) {
157 
158         Manager *m = userdata;
159         const char *t;
160         int r;
161 
162         assert(bus);
163         assert(value);
164 
165         r = sd_bus_message_read(value, "s", &t);
166         if (r < 0)
167                 return r;
168 
169         if (isempty(t))
170                 manager_restore_original_log_level(m);
171         else {
172                 int level;
173 
174                 level = log_level_from_string(t);
175                 if (level < 0)
176                         return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid log level '%s'", t);
177 
178                 manager_override_log_level(m, level);
179         }
180 
181         return 0;
182 }
183 
property_get_progress(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * reply,void * userdata,sd_bus_error * error)184 static int property_get_progress(
185                 sd_bus *bus,
186                 const char *path,
187                 const char *interface,
188                 const char *property,
189                 sd_bus_message *reply,
190                 void *userdata,
191                 sd_bus_error *error) {
192 
193         Manager *m = userdata;
194         double d;
195 
196         assert(bus);
197         assert(reply);
198         assert(m);
199 
200         if (MANAGER_IS_FINISHED(m))
201                 d = 1.0;
202         else
203                 d = 1.0 - ((double) hashmap_size(m->jobs) / (double) m->n_installed_jobs);
204 
205         return sd_bus_message_append(reply, "d", d);
206 }
207 
property_get_environment(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * reply,void * userdata,sd_bus_error * error)208 static int property_get_environment(
209                 sd_bus *bus,
210                 const char *path,
211                 const char *interface,
212                 const char *property,
213                 sd_bus_message *reply,
214                 void *userdata,
215                 sd_bus_error *error) {
216 
217         _cleanup_strv_free_ char **l = NULL;
218         Manager *m = userdata;
219         int r;
220 
221         assert(bus);
222         assert(reply);
223         assert(m);
224 
225         r = manager_get_effective_environment(m, &l);
226         if (r < 0)
227                 return r;
228 
229         return sd_bus_message_append_strv(reply, l);
230 }
231 
property_get_show_status(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * reply,void * userdata,sd_bus_error * error)232 static int property_get_show_status(
233                 sd_bus *bus,
234                 const char *path,
235                 const char *interface,
236                 const char *property,
237                 sd_bus_message *reply,
238                 void *userdata,
239                 sd_bus_error *error) {
240 
241         Manager *m = userdata;
242 
243         assert(m);
244         assert(bus);
245         assert(reply);
246 
247         return sd_bus_message_append(reply, "b", manager_get_show_status_on(m));
248 }
249 
property_get_runtime_watchdog(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * reply,void * userdata,sd_bus_error * error)250 static int property_get_runtime_watchdog(
251                 sd_bus *bus,
252                 const char *path,
253                 const char *interface,
254                 const char *property,
255                 sd_bus_message *reply,
256                 void *userdata,
257                 sd_bus_error *error) {
258 
259         Manager *m = userdata;
260 
261         assert(m);
262         assert(bus);
263         assert(reply);
264 
265         return sd_bus_message_append(reply, "t", manager_get_watchdog(m, WATCHDOG_RUNTIME));
266 }
267 
property_get_pretimeout_watchdog(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * reply,void * userdata,sd_bus_error * error)268 static int property_get_pretimeout_watchdog(
269                 sd_bus *bus,
270                 const char *path,
271                 const char *interface,
272                 const char *property,
273                 sd_bus_message *reply,
274                 void *userdata,
275                 sd_bus_error *error) {
276 
277         Manager *m = userdata;
278 
279         assert(m);
280         assert(bus);
281         assert(reply);
282 
283         return sd_bus_message_append(reply, "t", manager_get_watchdog(m, WATCHDOG_PRETIMEOUT));
284 }
285 
property_get_pretimeout_watchdog_governor(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * reply,void * userdata,sd_bus_error * error)286 static int property_get_pretimeout_watchdog_governor(
287                 sd_bus *bus,
288                 const char *path,
289                 const char *interface,
290                 const char *property,
291                 sd_bus_message *reply,
292                 void *userdata,
293                 sd_bus_error *error) {
294 
295         Manager *m = userdata;
296 
297         assert(m);
298         assert(bus);
299         assert(reply);
300 
301         return sd_bus_message_append(reply, "s", m->watchdog_pretimeout_governor);
302 }
303 
property_get_reboot_watchdog(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * reply,void * userdata,sd_bus_error * error)304 static int property_get_reboot_watchdog(
305                 sd_bus *bus,
306                 const char *path,
307                 const char *interface,
308                 const char *property,
309                 sd_bus_message *reply,
310                 void *userdata,
311                 sd_bus_error *error) {
312 
313         Manager *m = userdata;
314 
315         assert(m);
316         assert(bus);
317         assert(reply);
318 
319         return sd_bus_message_append(reply, "t", manager_get_watchdog(m, WATCHDOG_REBOOT));
320 }
321 
property_get_kexec_watchdog(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * reply,void * userdata,sd_bus_error * error)322 static int property_get_kexec_watchdog(
323                 sd_bus *bus,
324                 const char *path,
325                 const char *interface,
326                 const char *property,
327                 sd_bus_message *reply,
328                 void *userdata,
329                 sd_bus_error *error) {
330 
331         Manager *m = userdata;
332 
333         assert(m);
334         assert(bus);
335         assert(reply);
336 
337         return sd_bus_message_append(reply, "t", manager_get_watchdog(m, WATCHDOG_KEXEC));
338 }
339 
property_set_watchdog(Manager * m,WatchdogType type,sd_bus_message * value)340 static int property_set_watchdog(Manager *m, WatchdogType type, sd_bus_message *value) {
341         usec_t timeout;
342         int r;
343 
344         assert(m);
345         assert(value);
346 
347         assert_cc(sizeof(usec_t) == sizeof(uint64_t));
348 
349         r = sd_bus_message_read(value, "t", &timeout);
350         if (r < 0)
351                 return r;
352 
353         manager_override_watchdog(m, type, timeout);
354         return 0;
355 }
356 
property_set_runtime_watchdog(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * value,void * userdata,sd_bus_error * error)357 static int property_set_runtime_watchdog(
358                 sd_bus *bus,
359                 const char *path,
360                 const char *interface,
361                 const char *property,
362                 sd_bus_message *value,
363                 void *userdata,
364                 sd_bus_error *error) {
365 
366         return property_set_watchdog(userdata, WATCHDOG_RUNTIME, value);
367 }
368 
property_set_pretimeout_watchdog(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * value,void * userdata,sd_bus_error * error)369 static int property_set_pretimeout_watchdog(
370                 sd_bus *bus,
371                 const char *path,
372                 const char *interface,
373                 const char *property,
374                 sd_bus_message *value,
375                 void *userdata,
376                 sd_bus_error *error) {
377 
378         return property_set_watchdog(userdata, WATCHDOG_PRETIMEOUT, value);
379 }
380 
property_set_pretimeout_watchdog_governor(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * value,void * userdata,sd_bus_error * error)381 static int property_set_pretimeout_watchdog_governor(
382                 sd_bus *bus,
383                 const char *path,
384                 const char *interface,
385                 const char *property,
386                 sd_bus_message *value,
387                 void *userdata,
388                 sd_bus_error *error) {
389 
390         Manager *m = userdata;
391         char *governor;
392         int r;
393 
394         assert(m);
395 
396         r = sd_bus_message_read(value, "s", &governor);
397         if (r < 0)
398                 return r;
399         if (!string_is_safe(governor))
400                 return -EINVAL;
401 
402         return manager_override_watchdog_pretimeout_governor(m, governor);
403 }
404 
property_set_reboot_watchdog(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * value,void * userdata,sd_bus_error * error)405 static int property_set_reboot_watchdog(
406                 sd_bus *bus,
407                 const char *path,
408                 const char *interface,
409                 const char *property,
410                 sd_bus_message *value,
411                 void *userdata,
412                 sd_bus_error *error) {
413 
414         return property_set_watchdog(userdata, WATCHDOG_REBOOT, value);
415 }
416 
property_set_kexec_watchdog(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * value,void * userdata,sd_bus_error * error)417 static int property_set_kexec_watchdog(
418                 sd_bus *bus,
419                 const char *path,
420                 const char *interface,
421                 const char *property,
422                 sd_bus_message *value,
423                 void *userdata,
424                 sd_bus_error *error) {
425 
426         _unused_ Manager *m = userdata;
427 
428         assert(m);
429         assert(bus);
430         assert(value);
431 
432         return property_set_watchdog(userdata, WATCHDOG_KEXEC, value);
433 }
434 
property_get_oom_score_adjust(sd_bus * bus,const char * path,const char * interface,const char * property,sd_bus_message * reply,void * userdata,sd_bus_error * error)435 static int property_get_oom_score_adjust(
436                 sd_bus *bus,
437                 const char *path,
438                 const char *interface,
439                 const char *property,
440                 sd_bus_message *reply,
441                 void *userdata,
442                 sd_bus_error *error) {
443 
444         Manager *m = userdata;
445         int r, n;
446 
447         assert(m);
448         assert(bus);
449         assert(reply);
450 
451         if (m->default_oom_score_adjust_set)
452                 n = m->default_oom_score_adjust;
453         else {
454                 n = 0;
455                 r = get_oom_score_adjust(&n);
456                 if (r < 0)
457                         log_debug_errno(r, "Failed to read current OOM score adjustment value, ignoring: %m");
458         }
459 
460         return sd_bus_message_append(reply, "i", n);
461 }
462 
bus_get_unit_by_name(Manager * m,sd_bus_message * message,const char * name,Unit ** ret_unit,sd_bus_error * error)463 static int bus_get_unit_by_name(Manager *m, sd_bus_message *message, const char *name, Unit **ret_unit, sd_bus_error *error) {
464         Unit *u;
465         int r;
466 
467         assert(m);
468         assert(message);
469         assert(ret_unit);
470 
471         /* More or less a wrapper around manager_get_unit() that generates nice errors and has one trick up
472          * its sleeve: if the name is specified empty we use the client's unit. */
473 
474         if (isempty(name)) {
475                 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
476                 pid_t pid;
477 
478                 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
479                 if (r < 0)
480                         return r;
481 
482                 r = sd_bus_creds_get_pid(creds, &pid);
483                 if (r < 0)
484                         return r;
485 
486                 u = manager_get_unit_by_pid(m, pid);
487                 if (!u)
488                         return sd_bus_error_set(error, BUS_ERROR_NO_SUCH_UNIT, "Client not member of any unit.");
489         } else {
490                 u = manager_get_unit(m, name);
491                 if (!u)
492                         return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not loaded.", name);
493         }
494 
495         *ret_unit = u;
496         return 0;
497 }
498 
bus_load_unit_by_name(Manager * m,sd_bus_message * message,const char * name,Unit ** ret_unit,sd_bus_error * error)499 static int bus_load_unit_by_name(Manager *m, sd_bus_message *message, const char *name, Unit **ret_unit, sd_bus_error *error) {
500         assert(m);
501         assert(message);
502         assert(ret_unit);
503 
504         /* Pretty much the same as bus_get_unit_by_name(), but we also load the unit if necessary. */
505 
506         if (isempty(name))
507                 return bus_get_unit_by_name(m, message, name, ret_unit, error);
508 
509         return manager_load_unit(m, name, NULL, error, ret_unit);
510 }
511 
reply_unit_path(Unit * u,sd_bus_message * message,sd_bus_error * error)512 static int reply_unit_path(Unit *u, sd_bus_message *message, sd_bus_error *error) {
513         _cleanup_free_ char *path = NULL;
514         int r;
515 
516         assert(u);
517         assert(message);
518 
519         r = mac_selinux_unit_access_check(u, message, "status", error);
520         if (r < 0)
521                 return r;
522 
523         path = unit_dbus_path(u);
524         if (!path)
525                 return log_oom();
526 
527         return sd_bus_reply_method_return(message, "o", path);
528 }
529 
method_get_unit(sd_bus_message * message,void * userdata,sd_bus_error * error)530 static int method_get_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
531         Manager *m = userdata;
532         const char *name;
533         Unit *u;
534         int r;
535 
536         assert(message);
537         assert(m);
538 
539         /* Anyone can call this method */
540 
541         r = sd_bus_message_read(message, "s", &name);
542         if (r < 0)
543                 return r;
544 
545         r = bus_get_unit_by_name(m, message, name, &u, error);
546         if (r < 0)
547                 return r;
548 
549         return reply_unit_path(u, message, error);
550 }
551 
method_get_unit_by_pid(sd_bus_message * message,void * userdata,sd_bus_error * error)552 static int method_get_unit_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
553         Manager *m = userdata;
554         pid_t pid;
555         Unit *u;
556         int r;
557 
558         assert(message);
559         assert(m);
560 
561         assert_cc(sizeof(pid_t) == sizeof(uint32_t));
562 
563         /* Anyone can call this method */
564 
565         r = sd_bus_message_read(message, "u", &pid);
566         if (r < 0)
567                 return r;
568         if (pid < 0)
569                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid PID " PID_FMT, pid);
570 
571         if (pid == 0) {
572                 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
573 
574                 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
575                 if (r < 0)
576                         return r;
577 
578                 r = sd_bus_creds_get_pid(creds, &pid);
579                 if (r < 0)
580                         return r;
581         }
582 
583         u = manager_get_unit_by_pid(m, pid);
584         if (!u)
585                 return sd_bus_error_setf(error, BUS_ERROR_NO_UNIT_FOR_PID, "PID "PID_FMT" does not belong to any loaded unit.", pid);
586 
587         return reply_unit_path(u, message, error);
588 }
589 
method_get_unit_by_invocation_id(sd_bus_message * message,void * userdata,sd_bus_error * error)590 static int method_get_unit_by_invocation_id(sd_bus_message *message, void *userdata, sd_bus_error *error) {
591         _cleanup_free_ char *path = NULL;
592         Manager *m = userdata;
593         sd_id128_t id;
594         const void *a;
595         Unit *u;
596         size_t sz;
597         int r;
598 
599         assert(message);
600         assert(m);
601 
602         /* Anyone can call this method */
603 
604         r = sd_bus_message_read_array(message, 'y', &a, &sz);
605         if (r < 0)
606                 return r;
607         if (sz == 0)
608                 id = SD_ID128_NULL;
609         else if (sz == 16)
610                 memcpy(&id, a, sz);
611         else
612                 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid invocation ID");
613 
614         if (sd_id128_is_null(id)) {
615                 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
616                 pid_t pid;
617 
618                 r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds);
619                 if (r < 0)
620                         return r;
621 
622                 r = sd_bus_creds_get_pid(creds, &pid);
623                 if (r < 0)
624                         return r;
625 
626                 u = manager_get_unit_by_pid(m, pid);
627                 if (!u)
628                         return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT,
629                                                  "Client " PID_FMT " not member of any unit.", pid);
630         } else {
631                 u = hashmap_get(m->units_by_invocation_id, &id);
632                 if (!u)
633                         return sd_bus_error_setf(error, BUS_ERROR_NO_UNIT_FOR_INVOCATION_ID, "No unit with the specified invocation ID " SD_ID128_FORMAT_STR " known.", SD_ID128_FORMAT_VAL(id));
634         }
635 
636         r = mac_selinux_unit_access_check(u, message, "status", error);
637         if (r < 0)
638                 return r;
639 
640         /* So here's a special trick: the bus path we return actually references the unit by its invocation
641          * ID instead of the unit name. This means it stays valid only as long as the invocation ID stays the
642          * same. */
643         path = unit_dbus_path_invocation_id(u);
644         if (!path)
645                 return -ENOMEM;
646 
647         return sd_bus_reply_method_return(message, "o", path);
648 }
649 
method_get_unit_by_control_group(sd_bus_message * message,void * userdata,sd_bus_error * error)650 static int method_get_unit_by_control_group(sd_bus_message *message, void *userdata, sd_bus_error *error) {
651         Manager *m = userdata;
652         const char *cgroup;
653         Unit *u;
654         int r;
655 
656         r = sd_bus_message_read(message, "s", &cgroup);
657         if (r < 0)
658                 return r;
659 
660         u = manager_get_unit_by_cgroup(m, cgroup);
661         if (!u)
662                 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT,
663                                          "Control group '%s' is not valid or not managed by this instance",
664                                          cgroup);
665 
666         return reply_unit_path(u, message, error);
667 }
668 
method_load_unit(sd_bus_message * message,void * userdata,sd_bus_error * error)669 static int method_load_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
670         Manager *m = userdata;
671         const char *name;
672         Unit *u;
673         int r;
674 
675         assert(message);
676         assert(m);
677 
678         /* Anyone can call this method */
679 
680         r = sd_bus_message_read(message, "s", &name);
681         if (r < 0)
682                 return r;
683 
684         r = bus_load_unit_by_name(m, message, name, &u, error);
685         if (r < 0)
686                 return r;
687 
688         return reply_unit_path(u, message, error);
689 }
690 
method_start_unit_generic(sd_bus_message * message,Manager * m,JobType job_type,bool reload_if_possible,sd_bus_error * error)691 static int method_start_unit_generic(sd_bus_message *message, Manager *m, JobType job_type, bool reload_if_possible, sd_bus_error *error) {
692         const char *name;
693         Unit *u;
694         int r;
695 
696         assert(message);
697         assert(m);
698 
699         r = sd_bus_message_read(message, "s", &name);
700         if (r < 0)
701                 return r;
702 
703         r = manager_load_unit(m, name, NULL, error, &u);
704         if (r < 0)
705                 return r;
706 
707         return bus_unit_method_start_generic(message, u, job_type, reload_if_possible, error);
708 }
709 
method_start_unit(sd_bus_message * message,void * userdata,sd_bus_error * error)710 static int method_start_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
711         return method_start_unit_generic(message, userdata, JOB_START, false, error);
712 }
713 
method_stop_unit(sd_bus_message * message,void * userdata,sd_bus_error * error)714 static int method_stop_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
715         return method_start_unit_generic(message, userdata, JOB_STOP, false, error);
716 }
717 
method_reload_unit(sd_bus_message * message,void * userdata,sd_bus_error * error)718 static int method_reload_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
719         return method_start_unit_generic(message, userdata, JOB_RELOAD, false, error);
720 }
721 
method_restart_unit(sd_bus_message * message,void * userdata,sd_bus_error * error)722 static int method_restart_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
723         return method_start_unit_generic(message, userdata, JOB_RESTART, false, error);
724 }
725 
method_try_restart_unit(sd_bus_message * message,void * userdata,sd_bus_error * error)726 static int method_try_restart_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
727         return method_start_unit_generic(message, userdata, JOB_TRY_RESTART, false, error);
728 }
729 
method_reload_or_restart_unit(sd_bus_message * message,void * userdata,sd_bus_error * error)730 static int method_reload_or_restart_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
731         return method_start_unit_generic(message, userdata, JOB_RESTART, true, error);
732 }
733 
method_reload_or_try_restart_unit(sd_bus_message * message,void * userdata,sd_bus_error * error)734 static int method_reload_or_try_restart_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
735         return method_start_unit_generic(message, userdata, JOB_TRY_RESTART, true, error);
736 }
737 
738 typedef enum GenericUnitOperationFlags {
739         GENERIC_UNIT_LOAD            = 1 << 0, /* Load if the unit is not loaded yet */
740         GENERIC_UNIT_VALIDATE_LOADED = 1 << 1, /* Verify unit is properly loaded before forwarding call */
741 } GenericUnitOperationFlags;
742 
method_generic_unit_operation(sd_bus_message * message,Manager * m,sd_bus_error * error,sd_bus_message_handler_t handler,GenericUnitOperationFlags flags)743 static int method_generic_unit_operation(
744                 sd_bus_message *message,
745                 Manager *m,
746                 sd_bus_error *error,
747                 sd_bus_message_handler_t handler,
748                 GenericUnitOperationFlags flags) {
749 
750         const char *name;
751         Unit *u;
752         int r;
753 
754         assert(message);
755         assert(m);
756 
757         /* Read the first argument from the command and pass the operation to the specified per-unit
758          * method. */
759 
760         r = sd_bus_message_read(message, "s", &name);
761         if (r < 0)
762                 return r;
763 
764         if (!isempty(name) && FLAGS_SET(flags, GENERIC_UNIT_LOAD))
765                 r = manager_load_unit(m, name, NULL, error, &u);
766         else
767                 r = bus_get_unit_by_name(m, message, name, &u, error);
768         if (r < 0)
769                 return r;
770 
771         if (FLAGS_SET(flags, GENERIC_UNIT_VALIDATE_LOADED)) {
772                 r = bus_unit_validate_load_state(u, error);
773                 if (r < 0)
774                         return r;
775         }
776 
777         return handler(message, u, error);
778 }
779 
method_enqueue_unit_job(sd_bus_message * message,void * userdata,sd_bus_error * error)780 static int method_enqueue_unit_job(sd_bus_message *message, void *userdata, sd_bus_error *error) {
781         /* We don't bother with GENERIC_UNIT_VALIDATE_LOADED here, as the job logic validates that anyway */
782         return method_generic_unit_operation(message, userdata, error, bus_unit_method_enqueue_job, GENERIC_UNIT_LOAD);
783 }
784 
method_start_unit_replace(sd_bus_message * message,void * userdata,sd_bus_error * error)785 static int method_start_unit_replace(sd_bus_message *message, void *userdata, sd_bus_error *error) {
786         Manager *m = userdata;
787         const char *old_name;
788         Unit *u;
789         int r;
790 
791         assert(message);
792         assert(m);
793 
794         r = sd_bus_message_read(message, "s", &old_name);
795         if (r < 0)
796                 return r;
797 
798         r = bus_get_unit_by_name(m, message, old_name, &u, error);
799         if (r < 0)
800                 return r;
801         if (!u->job || u->job->type != JOB_START)
802                 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_JOB, "No job queued for unit %s", old_name);
803 
804         return method_start_unit_generic(message, m, JOB_START, false, error);
805 }
806 
method_kill_unit(sd_bus_message * message,void * userdata,sd_bus_error * error)807 static int method_kill_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
808         /* We don't bother with GENERIC_UNIT_LOAD nor GENERIC_UNIT_VALIDATE_LOADED here, as it shouldn't
809          * matter whether a unit is loaded for killing any processes possibly in the unit's cgroup. */
810         return method_generic_unit_operation(message, userdata, error, bus_unit_method_kill, 0);
811 }
812 
method_clean_unit(sd_bus_message * message,void * userdata,sd_bus_error * error)813 static int method_clean_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
814         /* Load the unit if necessary, in order to load it, and insist on the unit being loaded to be
815          * cleaned */
816         return method_generic_unit_operation(message, userdata, error, bus_unit_method_clean, GENERIC_UNIT_LOAD|GENERIC_UNIT_VALIDATE_LOADED);
817 }
818 
method_freeze_unit(sd_bus_message * message,void * userdata,sd_bus_error * error)819 static int method_freeze_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
820         return method_generic_unit_operation(message, userdata, error, bus_unit_method_freeze, 0);
821 }
822 
method_thaw_unit(sd_bus_message * message,void * userdata,sd_bus_error * error)823 static int method_thaw_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
824         return method_generic_unit_operation(message, userdata, error, bus_unit_method_thaw, 0);
825 }
826 
method_reset_failed_unit(sd_bus_message * message,void * userdata,sd_bus_error * error)827 static int method_reset_failed_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
828         /* Don't load the unit (because unloaded units can't be in failed state), and don't insist on the
829          * unit to be loaded properly (since a failed unit might have its unit file disappeared) */
830         return method_generic_unit_operation(message, userdata, error, bus_unit_method_reset_failed, 0);
831 }
832 
method_set_unit_properties(sd_bus_message * message,void * userdata,sd_bus_error * error)833 static int method_set_unit_properties(sd_bus_message *message, void *userdata, sd_bus_error *error) {
834         /* Only change properties on fully loaded units, and load them in order to set properties */
835         return method_generic_unit_operation(message, userdata, error, bus_unit_method_set_properties, GENERIC_UNIT_LOAD|GENERIC_UNIT_VALIDATE_LOADED);
836 }
837 
method_bind_mount_unit(sd_bus_message * message,void * userdata,sd_bus_error * error)838 static int method_bind_mount_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
839         /* Only add mounts on fully loaded units */
840         return method_generic_unit_operation(message, userdata, error, bus_service_method_bind_mount, GENERIC_UNIT_VALIDATE_LOADED);
841 }
842 
method_mount_image_unit(sd_bus_message * message,void * userdata,sd_bus_error * error)843 static int method_mount_image_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
844         /* Only add mounts on fully loaded units */
845         return method_generic_unit_operation(message, userdata, error, bus_service_method_mount_image, GENERIC_UNIT_VALIDATE_LOADED);
846 }
847 
method_ref_unit(sd_bus_message * message,void * userdata,sd_bus_error * error)848 static int method_ref_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
849         /* Only allow reffing of fully loaded units, and make sure reffing a unit loads it. */
850         return method_generic_unit_operation(message, userdata, error, bus_unit_method_ref, GENERIC_UNIT_LOAD|GENERIC_UNIT_VALIDATE_LOADED);
851 }
852 
method_unref_unit(sd_bus_message * message,void * userdata,sd_bus_error * error)853 static int method_unref_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
854         /* Dropping a ref OTOH should not require the unit to still be loaded. And since a reffed unit is a
855          * loaded unit there's no need to load the unit for unreffing it. */
856         return method_generic_unit_operation(message, userdata, error, bus_unit_method_unref, 0);
857 }
858 
reply_unit_info(sd_bus_message * reply,Unit * u)859 static int reply_unit_info(sd_bus_message *reply, Unit *u) {
860         _cleanup_free_ char *unit_path = NULL, *job_path = NULL;
861         Unit *following;
862 
863         following = unit_following(u);
864 
865         unit_path = unit_dbus_path(u);
866         if (!unit_path)
867                 return -ENOMEM;
868 
869         if (u->job) {
870                 job_path = job_dbus_path(u->job);
871                 if (!job_path)
872                         return -ENOMEM;
873         }
874 
875         return sd_bus_message_append(
876                         reply, "(ssssssouso)",
877                         u->id,
878                         unit_description(u),
879                         unit_load_state_to_string(u->load_state),
880                         unit_active_state_to_string(unit_active_state(u)),
881                         unit_sub_state_to_string(u),
882                         following ? following->id : "",
883                         unit_path,
884                         u->job ? u->job->id : 0,
885                         u->job ? job_type_to_string(u->job->type) : "",
886                         empty_to_root(job_path));
887 }
888 
method_list_units_by_names(sd_bus_message * message,void * userdata,sd_bus_error * error)889 static int method_list_units_by_names(sd_bus_message *message, void *userdata, sd_bus_error *error) {
890         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
891         Manager *m = userdata;
892         int r;
893         _cleanup_strv_free_ char **units = NULL;
894 
895         assert(message);
896         assert(m);
897 
898         r = sd_bus_message_read_strv(message, &units);
899         if (r < 0)
900                 return r;
901 
902         r = sd_bus_message_new_method_return(message, &reply);
903         if (r < 0)
904                 return r;
905 
906         r = sd_bus_message_open_container(reply, 'a', "(ssssssouso)");
907         if (r < 0)
908                 return r;
909 
910         STRV_FOREACH(unit, units) {
911                 Unit *u;
912 
913                 if (!unit_name_is_valid(*unit, UNIT_NAME_ANY))
914                         continue;
915 
916                 r = bus_load_unit_by_name(m, message, *unit, &u, error);
917                 if (r < 0)
918                         return r;
919 
920                 r = reply_unit_info(reply, u);
921                 if (r < 0)
922                         return r;
923         }
924 
925         r = sd_bus_message_close_container(reply);
926         if (r < 0)
927                 return r;
928 
929         return sd_bus_send(NULL, reply, NULL);
930 }
931 
method_get_unit_processes(sd_bus_message * message,void * userdata,sd_bus_error * error)932 static int method_get_unit_processes(sd_bus_message *message, void *userdata, sd_bus_error *error) {
933         /* Don't load a unit (since it won't have any processes if it's not loaded), but don't insist on the
934          * unit being loaded (because even improperly loaded units might still have processes around */
935         return method_generic_unit_operation(message, userdata, error, bus_unit_method_get_processes, 0);
936 }
937 
method_attach_processes_to_unit(sd_bus_message * message,void * userdata,sd_bus_error * error)938 static int method_attach_processes_to_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
939         /* Don't allow attaching new processes to units that aren't loaded. Don't bother with loading a unit
940          * for this purpose though, as an unloaded unit is a stopped unit, and we don't allow attaching
941          * processes to stopped units anyway. */
942         return method_generic_unit_operation(message, userdata, error, bus_unit_method_attach_processes, GENERIC_UNIT_VALIDATE_LOADED);
943 }
944 
transient_unit_from_message(Manager * m,sd_bus_message * message,const char * name,Unit ** unit,sd_bus_error * error)945 static int transient_unit_from_message(
946                 Manager *m,
947                 sd_bus_message *message,
948                 const char *name,
949                 Unit **unit,
950                 sd_bus_error *error) {
951 
952         UnitType t;
953         Unit *u;
954         int r;
955 
956         assert(m);
957         assert(message);
958         assert(name);
959 
960         t = unit_name_to_type(name);
961         if (t < 0)
962                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
963                                          "Invalid unit name or type.");
964 
965         if (!unit_vtable[t]->can_transient)
966                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
967                                          "Unit type %s does not support transient units.",
968                                          unit_type_to_string(t));
969 
970         r = manager_load_unit(m, name, NULL, error, &u);
971         if (r < 0)
972                 return r;
973 
974         if (!unit_is_pristine(u))
975                 return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS,
976                                          "Unit %s already exists.", name);
977 
978         /* OK, the unit failed to load and is unreferenced, now let's
979          * fill in the transient data instead */
980         r = unit_make_transient(u);
981         if (r < 0)
982                 return r;
983 
984         /* Set our properties */
985         r = bus_unit_set_properties(u, message, UNIT_RUNTIME, false, error);
986         if (r < 0)
987                 return r;
988 
989         /* If the client asked for it, automatically add a reference to this unit. */
990         if (u->bus_track_add) {
991                 r = bus_unit_track_add_sender(u, message);
992                 if (r < 0)
993                         return log_error_errno(r, "Failed to watch sender: %m");
994         }
995 
996         /* Now load the missing bits of the unit we just created */
997         unit_add_to_load_queue(u);
998         manager_dispatch_load_queue(m);
999 
1000         *unit = u;
1001 
1002         return 0;
1003 }
1004 
transient_aux_units_from_message(Manager * m,sd_bus_message * message,sd_bus_error * error)1005 static int transient_aux_units_from_message(
1006                 Manager *m,
1007                 sd_bus_message *message,
1008                 sd_bus_error *error) {
1009 
1010         int r;
1011 
1012         assert(m);
1013         assert(message);
1014 
1015         r = sd_bus_message_enter_container(message, 'a', "(sa(sv))");
1016         if (r < 0)
1017                 return r;
1018 
1019         while ((r = sd_bus_message_enter_container(message, 'r', "sa(sv)")) > 0) {
1020                 const char *name = NULL;
1021                 Unit *u;
1022 
1023                 r = sd_bus_message_read(message, "s", &name);
1024                 if (r < 0)
1025                         return r;
1026 
1027                 r = transient_unit_from_message(m, message, name, &u, error);
1028                 if (r < 0)
1029                         return r;
1030 
1031                 r = sd_bus_message_exit_container(message);
1032                 if (r < 0)
1033                         return r;
1034         }
1035         if (r < 0)
1036                 return r;
1037 
1038         r = sd_bus_message_exit_container(message);
1039         if (r < 0)
1040                 return r;
1041 
1042         return 0;
1043 }
1044 
method_start_transient_unit(sd_bus_message * message,void * userdata,sd_bus_error * error)1045 static int method_start_transient_unit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1046         const char *name, *smode;
1047         Manager *m = userdata;
1048         JobMode mode;
1049         Unit *u;
1050         int r;
1051 
1052         assert(message);
1053         assert(m);
1054 
1055         r = mac_selinux_access_check(message, "start", error);
1056         if (r < 0)
1057                 return r;
1058 
1059         r = sd_bus_message_read(message, "ss", &name, &smode);
1060         if (r < 0)
1061                 return r;
1062 
1063         mode = job_mode_from_string(smode);
1064         if (mode < 0)
1065                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Job mode %s is invalid.", smode);
1066 
1067         r = bus_verify_manage_units_async(m, message, error);
1068         if (r < 0)
1069                 return r;
1070         if (r == 0)
1071                 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1072 
1073         r = transient_unit_from_message(m, message, name, &u, error);
1074         if (r < 0)
1075                 return r;
1076 
1077         r = transient_aux_units_from_message(m, message, error);
1078         if (r < 0)
1079                 return r;
1080 
1081         /* Finally, start it */
1082         return bus_unit_queue_job(message, u, JOB_START, mode, 0, error);
1083 }
1084 
method_get_job(sd_bus_message * message,void * userdata,sd_bus_error * error)1085 static int method_get_job(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1086         _cleanup_free_ char *path = NULL;
1087         Manager *m = userdata;
1088         uint32_t id;
1089         Job *j;
1090         int r;
1091 
1092         assert(message);
1093         assert(m);
1094 
1095         /* Anyone can call this method */
1096 
1097         r = sd_bus_message_read(message, "u", &id);
1098         if (r < 0)
1099                 return r;
1100 
1101         j = manager_get_job(m, id);
1102         if (!j)
1103                 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
1104 
1105         r = mac_selinux_unit_access_check(j->unit, message, "status", error);
1106         if (r < 0)
1107                 return r;
1108 
1109         path = job_dbus_path(j);
1110         if (!path)
1111                 return -ENOMEM;
1112 
1113         return sd_bus_reply_method_return(message, "o", path);
1114 }
1115 
method_cancel_job(sd_bus_message * message,void * userdata,sd_bus_error * error)1116 static int method_cancel_job(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1117         Manager *m = userdata;
1118         uint32_t id;
1119         Job *j;
1120         int r;
1121 
1122         assert(message);
1123         assert(m);
1124 
1125         r = sd_bus_message_read(message, "u", &id);
1126         if (r < 0)
1127                 return r;
1128 
1129         j = manager_get_job(m, id);
1130         if (!j)
1131                 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
1132 
1133         return bus_job_method_cancel(message, j, error);
1134 }
1135 
method_clear_jobs(sd_bus_message * message,void * userdata,sd_bus_error * error)1136 static int method_clear_jobs(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1137         Manager *m = userdata;
1138         int r;
1139 
1140         assert(message);
1141         assert(m);
1142 
1143         r = mac_selinux_access_check(message, "reload", error);
1144         if (r < 0)
1145                 return r;
1146 
1147         r = bus_verify_manage_units_async(m, message, error);
1148         if (r < 0)
1149                 return r;
1150         if (r == 0)
1151                 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1152 
1153         manager_clear_jobs(m);
1154 
1155         return sd_bus_reply_method_return(message, NULL);
1156 }
1157 
method_reset_failed(sd_bus_message * message,void * userdata,sd_bus_error * error)1158 static int method_reset_failed(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1159         Manager *m = userdata;
1160         int r;
1161 
1162         assert(message);
1163         assert(m);
1164 
1165         r = mac_selinux_access_check(message, "reload", error);
1166         if (r < 0)
1167                 return r;
1168 
1169         r = bus_verify_manage_units_async(m, message, error);
1170         if (r < 0)
1171                 return r;
1172         if (r == 0)
1173                 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1174 
1175         manager_reset_failed(m);
1176 
1177         return sd_bus_reply_method_return(message, NULL);
1178 }
1179 
list_units_filtered(sd_bus_message * message,void * userdata,sd_bus_error * error,char ** states,char ** patterns)1180 static int list_units_filtered(sd_bus_message *message, void *userdata, sd_bus_error *error, char **states, char **patterns) {
1181         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1182         Manager *m = userdata;
1183         const char *k;
1184         Unit *u;
1185         int r;
1186 
1187         assert(message);
1188         assert(m);
1189 
1190         /* Anyone can call this method */
1191 
1192         r = mac_selinux_access_check(message, "status", error);
1193         if (r < 0)
1194                 return r;
1195 
1196         r = sd_bus_message_new_method_return(message, &reply);
1197         if (r < 0)
1198                 return r;
1199 
1200         r = sd_bus_message_open_container(reply, 'a', "(ssssssouso)");
1201         if (r < 0)
1202                 return r;
1203 
1204         HASHMAP_FOREACH_KEY(u, k, m->units) {
1205                 if (k != u->id)
1206                         continue;
1207 
1208                 if (!strv_isempty(states) &&
1209                     !strv_contains(states, unit_load_state_to_string(u->load_state)) &&
1210                     !strv_contains(states, unit_active_state_to_string(unit_active_state(u))) &&
1211                     !strv_contains(states, unit_sub_state_to_string(u)))
1212                         continue;
1213 
1214                 if (!strv_isempty(patterns) &&
1215                     !strv_fnmatch_or_empty(patterns, u->id, FNM_NOESCAPE))
1216                         continue;
1217 
1218                 r = reply_unit_info(reply, u);
1219                 if (r < 0)
1220                         return r;
1221         }
1222 
1223         r = sd_bus_message_close_container(reply);
1224         if (r < 0)
1225                 return r;
1226 
1227         return sd_bus_send(NULL, reply, NULL);
1228 }
1229 
method_list_units(sd_bus_message * message,void * userdata,sd_bus_error * error)1230 static int method_list_units(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1231         return list_units_filtered(message, userdata, error, NULL, NULL);
1232 }
1233 
method_list_units_filtered(sd_bus_message * message,void * userdata,sd_bus_error * error)1234 static int method_list_units_filtered(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1235         _cleanup_strv_free_ char **states = NULL;
1236         int r;
1237 
1238         r = sd_bus_message_read_strv(message, &states);
1239         if (r < 0)
1240                 return r;
1241 
1242         return list_units_filtered(message, userdata, error, states, NULL);
1243 }
1244 
method_list_units_by_patterns(sd_bus_message * message,void * userdata,sd_bus_error * error)1245 static int method_list_units_by_patterns(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1246         _cleanup_strv_free_ char **states = NULL;
1247         _cleanup_strv_free_ char **patterns = NULL;
1248         int r;
1249 
1250         r = sd_bus_message_read_strv(message, &states);
1251         if (r < 0)
1252                 return r;
1253 
1254         r = sd_bus_message_read_strv(message, &patterns);
1255         if (r < 0)
1256                 return r;
1257 
1258         return list_units_filtered(message, userdata, error, states, patterns);
1259 }
1260 
method_list_jobs(sd_bus_message * message,void * userdata,sd_bus_error * error)1261 static int method_list_jobs(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1262         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1263         Manager *m = userdata;
1264         Job *j;
1265         int r;
1266 
1267         assert(message);
1268         assert(m);
1269 
1270         /* Anyone can call this method */
1271 
1272         r = mac_selinux_access_check(message, "status", error);
1273         if (r < 0)
1274                 return r;
1275 
1276         r = sd_bus_message_new_method_return(message, &reply);
1277         if (r < 0)
1278                 return r;
1279 
1280         r = sd_bus_message_open_container(reply, 'a', "(usssoo)");
1281         if (r < 0)
1282                 return r;
1283 
1284         HASHMAP_FOREACH(j, m->jobs) {
1285                 _cleanup_free_ char *unit_path = NULL, *job_path = NULL;
1286 
1287                 job_path = job_dbus_path(j);
1288                 if (!job_path)
1289                         return -ENOMEM;
1290 
1291                 unit_path = unit_dbus_path(j->unit);
1292                 if (!unit_path)
1293                         return -ENOMEM;
1294 
1295                 r = sd_bus_message_append(
1296                                 reply, "(usssoo)",
1297                                 j->id,
1298                                 j->unit->id,
1299                                 job_type_to_string(j->type),
1300                                 job_state_to_string(j->state),
1301                                 job_path,
1302                                 unit_path);
1303                 if (r < 0)
1304                         return r;
1305         }
1306 
1307         r = sd_bus_message_close_container(reply);
1308         if (r < 0)
1309                 return r;
1310 
1311         return sd_bus_send(NULL, reply, NULL);
1312 }
1313 
method_subscribe(sd_bus_message * message,void * userdata,sd_bus_error * error)1314 static int method_subscribe(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1315         Manager *m = userdata;
1316         int r;
1317 
1318         assert(message);
1319         assert(m);
1320 
1321         /* Anyone can call this method */
1322 
1323         r = mac_selinux_access_check(message, "status", error);
1324         if (r < 0)
1325                 return r;
1326 
1327         if (sd_bus_message_get_bus(message) == m->api_bus) {
1328 
1329                 /* Note that direct bus connection subscribe by
1330                  * default, we only track peers on the API bus here */
1331 
1332                 if (!m->subscribed) {
1333                         r = sd_bus_track_new(sd_bus_message_get_bus(message), &m->subscribed, NULL, NULL);
1334                         if (r < 0)
1335                                 return r;
1336                 }
1337 
1338                 r = sd_bus_track_add_sender(m->subscribed, message);
1339                 if (r < 0)
1340                         return r;
1341                 if (r == 0)
1342                         return sd_bus_error_set(error, BUS_ERROR_ALREADY_SUBSCRIBED, "Client is already subscribed.");
1343         }
1344 
1345         return sd_bus_reply_method_return(message, NULL);
1346 }
1347 
method_unsubscribe(sd_bus_message * message,void * userdata,sd_bus_error * error)1348 static int method_unsubscribe(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1349         Manager *m = userdata;
1350         int r;
1351 
1352         assert(message);
1353         assert(m);
1354 
1355         /* Anyone can call this method */
1356 
1357         r = mac_selinux_access_check(message, "status", error);
1358         if (r < 0)
1359                 return r;
1360 
1361         if (sd_bus_message_get_bus(message) == m->api_bus) {
1362                 r = sd_bus_track_remove_sender(m->subscribed, message);
1363                 if (r < 0)
1364                         return r;
1365                 if (r == 0)
1366                         return sd_bus_error_set(error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
1367         }
1368 
1369         return sd_bus_reply_method_return(message, NULL);
1370 }
1371 
dump_impl(sd_bus_message * message,void * userdata,sd_bus_error * error,int (* reply)(sd_bus_message *,char *))1372 static int dump_impl(sd_bus_message *message, void *userdata, sd_bus_error *error, int (*reply)(sd_bus_message *, char *)) {
1373         _cleanup_free_ char *dump = NULL;
1374         Manager *m = userdata;
1375         int r;
1376 
1377         assert(message);
1378         assert(m);
1379 
1380         /* Anyone can call this method */
1381 
1382         r = mac_selinux_access_check(message, "status", error);
1383         if (r < 0)
1384                 return r;
1385 
1386         r = manager_get_dump_string(m, &dump);
1387         if (r < 0)
1388                 return r;
1389 
1390         return reply(message, dump);
1391 }
1392 
reply_dump(sd_bus_message * message,char * dump)1393 static int reply_dump(sd_bus_message *message, char *dump) {
1394         return sd_bus_reply_method_return(message, "s", dump);
1395 }
1396 
method_dump(sd_bus_message * message,void * userdata,sd_bus_error * error)1397 static int method_dump(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1398         return dump_impl(message, userdata, error, reply_dump);
1399 }
1400 
reply_dump_by_fd(sd_bus_message * message,char * dump)1401 static int reply_dump_by_fd(sd_bus_message *message, char *dump) {
1402         _cleanup_close_ int fd = -1;
1403 
1404         fd = acquire_data_fd(dump, strlen(dump), 0);
1405         if (fd < 0)
1406                 return fd;
1407 
1408         return sd_bus_reply_method_return(message, "h", fd);
1409 }
1410 
method_dump_by_fd(sd_bus_message * message,void * userdata,sd_bus_error * error)1411 static int method_dump_by_fd(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1412         return dump_impl(message, userdata, error, reply_dump_by_fd);
1413 }
1414 
method_refuse_snapshot(sd_bus_message * message,void * userdata,sd_bus_error * error)1415 static int method_refuse_snapshot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1416         return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "Support for snapshots has been removed.");
1417 }
1418 
verify_run_space(const char * message,sd_bus_error * error)1419 static int verify_run_space(const char *message, sd_bus_error *error) {
1420         struct statvfs svfs;
1421         uint64_t available;
1422 
1423         if (statvfs("/run/systemd", &svfs) < 0)
1424                 return sd_bus_error_set_errnof(error, errno, "Failed to statvfs(/run/systemd): %m");
1425 
1426         available = (uint64_t) svfs.f_bfree * (uint64_t) svfs.f_bsize;
1427 
1428         if (available < RELOAD_DISK_SPACE_MIN)
1429                 return sd_bus_error_setf(error,
1430                                          BUS_ERROR_DISK_FULL,
1431                                          "%s, not enough space available on /run/systemd. "
1432                                          "Currently, %s are free, but a safety buffer of %s is enforced.",
1433                                          message,
1434                                          FORMAT_BYTES(available),
1435                                          FORMAT_BYTES(RELOAD_DISK_SPACE_MIN));
1436 
1437         return 0;
1438 }
1439 
verify_run_space_and_log(const char * message)1440 int verify_run_space_and_log(const char *message) {
1441         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1442         int r;
1443 
1444         r = verify_run_space(message, &error);
1445         if (r < 0)
1446                 return log_error_errno(r, "%s", bus_error_message(&error, r));
1447 
1448         return 0;
1449 }
1450 
method_reload(sd_bus_message * message,void * userdata,sd_bus_error * error)1451 static int method_reload(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1452         Manager *m = userdata;
1453         int r;
1454 
1455         assert(message);
1456         assert(m);
1457 
1458         r = verify_run_space("Refusing to reload", error);
1459         if (r < 0)
1460                 return r;
1461 
1462         r = mac_selinux_access_check(message, "reload", error);
1463         if (r < 0)
1464                 return r;
1465 
1466         r = bus_verify_reload_daemon_async(m, message, error);
1467         if (r < 0)
1468                 return r;
1469         if (r == 0)
1470                 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1471 
1472         /* Instead of sending the reply back right away, we just
1473          * remember that we need to and then send it after the reload
1474          * is finished. That way the caller knows when the reload
1475          * finished. */
1476 
1477         assert(!m->pending_reload_message);
1478         r = sd_bus_message_new_method_return(message, &m->pending_reload_message);
1479         if (r < 0)
1480                 return r;
1481 
1482         m->objective = MANAGER_RELOAD;
1483 
1484         return 1;
1485 }
1486 
method_reexecute(sd_bus_message * message,void * userdata,sd_bus_error * error)1487 static int method_reexecute(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1488         Manager *m = userdata;
1489         int r;
1490 
1491         assert(message);
1492         assert(m);
1493 
1494         r = verify_run_space("Refusing to reexecute", error);
1495         if (r < 0)
1496                 return r;
1497 
1498         r = mac_selinux_access_check(message, "reload", error);
1499         if (r < 0)
1500                 return r;
1501 
1502         r = bus_verify_reload_daemon_async(m, message, error);
1503         if (r < 0)
1504                 return r;
1505         if (r == 0)
1506                 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1507 
1508         /* We don't send a reply back here, the client should
1509          * just wait for us disconnecting. */
1510 
1511         m->objective = MANAGER_REEXECUTE;
1512         return 1;
1513 }
1514 
method_exit(sd_bus_message * message,void * userdata,sd_bus_error * error)1515 static int method_exit(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1516         Manager *m = userdata;
1517         int r;
1518 
1519         assert(message);
1520         assert(m);
1521 
1522         r = mac_selinux_access_check(message, "halt", error);
1523         if (r < 0)
1524                 return r;
1525 
1526         /* Exit() (in contrast to SetExitCode()) is actually allowed even if
1527          * we are running on the host. It will fall back on reboot() in
1528          * systemd-shutdown if it cannot do the exit() because it isn't a
1529          * container. */
1530 
1531         m->objective = MANAGER_EXIT;
1532 
1533         return sd_bus_reply_method_return(message, NULL);
1534 }
1535 
method_reboot(sd_bus_message * message,void * userdata,sd_bus_error * error)1536 static int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1537         Manager *m = userdata;
1538         int r;
1539 
1540         assert(message);
1541         assert(m);
1542 
1543         r = mac_selinux_access_check(message, "reboot", error);
1544         if (r < 0)
1545                 return r;
1546 
1547         if (!MANAGER_IS_SYSTEM(m))
1548                 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED,
1549                                          "Reboot is only supported for system managers.");
1550 
1551         m->objective = MANAGER_REBOOT;
1552 
1553         return sd_bus_reply_method_return(message, NULL);
1554 }
1555 
method_poweroff(sd_bus_message * message,void * userdata,sd_bus_error * error)1556 static int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1557         Manager *m = userdata;
1558         int r;
1559 
1560         assert(message);
1561         assert(m);
1562 
1563         r = mac_selinux_access_check(message, "halt", error);
1564         if (r < 0)
1565                 return r;
1566 
1567         if (!MANAGER_IS_SYSTEM(m))
1568                 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED,
1569                                          "Powering off is only supported for system managers.");
1570 
1571         m->objective = MANAGER_POWEROFF;
1572 
1573         return sd_bus_reply_method_return(message, NULL);
1574 }
1575 
method_halt(sd_bus_message * message,void * userdata,sd_bus_error * error)1576 static int method_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1577         Manager *m = userdata;
1578         int r;
1579 
1580         assert(message);
1581         assert(m);
1582 
1583         r = mac_selinux_access_check(message, "halt", error);
1584         if (r < 0)
1585                 return r;
1586 
1587         if (!MANAGER_IS_SYSTEM(m))
1588                 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED,
1589                                          "Halt is only supported for system managers.");
1590 
1591         m->objective = MANAGER_HALT;
1592 
1593         return sd_bus_reply_method_return(message, NULL);
1594 }
1595 
method_kexec(sd_bus_message * message,void * userdata,sd_bus_error * error)1596 static int method_kexec(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1597         Manager *m = userdata;
1598         int r;
1599 
1600         assert(message);
1601         assert(m);
1602 
1603         r = mac_selinux_access_check(message, "reboot", error);
1604         if (r < 0)
1605                 return r;
1606 
1607         if (!MANAGER_IS_SYSTEM(m))
1608                 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED,
1609                                          "KExec is only supported for system managers.");
1610 
1611         m->objective = MANAGER_KEXEC;
1612 
1613         return sd_bus_reply_method_return(message, NULL);
1614 }
1615 
method_switch_root(sd_bus_message * message,void * userdata,sd_bus_error * error)1616 static int method_switch_root(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1617         _cleanup_free_ char *ri = NULL, *rt = NULL;
1618         const char *root, *init;
1619         Manager *m = userdata;
1620         struct statvfs svfs;
1621         uint64_t available;
1622         int r;
1623 
1624         assert(message);
1625         assert(m);
1626 
1627         if (statvfs("/run/systemd", &svfs) < 0)
1628                 return sd_bus_error_set_errnof(error, errno, "Failed to statvfs(/run/systemd): %m");
1629 
1630         available = (uint64_t) svfs.f_bfree * (uint64_t) svfs.f_bsize;
1631 
1632         if (available < RELOAD_DISK_SPACE_MIN)
1633                 log_warning("Dangerously low amount of free space on /run/systemd, root switching might fail.\n"
1634                             "Currently, %s are free, but %s are suggested. Proceeding anyway.",
1635                             FORMAT_BYTES(available),
1636                             FORMAT_BYTES(RELOAD_DISK_SPACE_MIN));
1637 
1638         r = mac_selinux_access_check(message, "reboot", error);
1639         if (r < 0)
1640                 return r;
1641 
1642         if (!MANAGER_IS_SYSTEM(m))
1643                 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED,
1644                                          "Root switching is only supported by system manager.");
1645 
1646         r = sd_bus_message_read(message, "ss", &root, &init);
1647         if (r < 0)
1648                 return r;
1649 
1650         if (isempty(root))
1651                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
1652                                          "New root directory may not be the empty string.");
1653         if (!path_is_absolute(root))
1654                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
1655                                          "New root path '%s' is not absolute.", root);
1656         if (path_equal(root, "/"))
1657                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
1658                                          "New root directory cannot be the old root directory.");
1659 
1660         /* Safety check */
1661         if (isempty(init)) {
1662                 r = path_is_os_tree(root);
1663                 if (r < 0)
1664                         return sd_bus_error_set_errnof(error, r,
1665                                                        "Failed to determine whether root path '%s' contains an OS tree: %m",
1666                                                        root);
1667                 if (r == 0)
1668                         return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
1669                                                  "Specified switch root path '%s' does not seem to be an OS tree. os-release file is missing.",
1670                                                  root);
1671         } else {
1672                 _cleanup_free_ char *chased = NULL;
1673 
1674                 if (!path_is_absolute(init))
1675                         return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
1676                                                  "Path to init binary '%s' not absolute.", init);
1677 
1678                 r = chase_symlinks(init, root, CHASE_PREFIX_ROOT|CHASE_TRAIL_SLASH, &chased, NULL);
1679                 if (r < 0)
1680                         return sd_bus_error_set_errnof(error, r,
1681                                                        "Could not resolve init executable %s: %m", init);
1682 
1683                 if (laccess(chased, X_OK) < 0) {
1684                         if (errno == EACCES)
1685                                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
1686                                                          "Init binary %s is not executable.", init);
1687 
1688                         return sd_bus_error_set_errnof(error, r,
1689                                                        "Could not check whether init binary %s is executable: %m", init);
1690                 }
1691         }
1692 
1693         rt = strdup(root);
1694         if (!rt)
1695                 return -ENOMEM;
1696 
1697         if (!isempty(init)) {
1698                 ri = strdup(init);
1699                 if (!ri)
1700                         return -ENOMEM;
1701         }
1702 
1703         free_and_replace(m->switch_root, rt);
1704         free_and_replace(m->switch_root_init, ri);
1705 
1706         m->objective = MANAGER_SWITCH_ROOT;
1707 
1708         return sd_bus_reply_method_return(message, NULL);
1709 }
1710 
method_set_environment(sd_bus_message * message,void * userdata,sd_bus_error * error)1711 static int method_set_environment(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1712         _cleanup_strv_free_ char **plus = NULL;
1713         Manager *m = userdata;
1714         int r;
1715 
1716         assert(message);
1717         assert(m);
1718 
1719         r = mac_selinux_access_check(message, "reload", error);
1720         if (r < 0)
1721                 return r;
1722 
1723         r = sd_bus_message_read_strv(message, &plus);
1724         if (r < 0)
1725                 return r;
1726         if (!strv_env_is_valid(plus))
1727                 return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid environment assignments");
1728 
1729         r = bus_verify_set_environment_async(m, message, error);
1730         if (r < 0)
1731                 return r;
1732         if (r == 0)
1733                 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1734 
1735         r = manager_client_environment_modify(m, NULL, plus);
1736         if (r < 0)
1737                 return r;
1738 
1739         return sd_bus_reply_method_return(message, NULL);
1740 }
1741 
method_unset_environment(sd_bus_message * message,void * userdata,sd_bus_error * error)1742 static int method_unset_environment(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1743         _cleanup_strv_free_ char **minus = NULL;
1744         Manager *m = userdata;
1745         int r;
1746 
1747         assert(message);
1748         assert(m);
1749 
1750         r = mac_selinux_access_check(message, "reload", error);
1751         if (r < 0)
1752                 return r;
1753 
1754         r = sd_bus_message_read_strv(message, &minus);
1755         if (r < 0)
1756                 return r;
1757 
1758         if (!strv_env_name_or_assignment_is_valid(minus))
1759                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
1760                                          "Invalid environment variable names or assignments");
1761 
1762         r = bus_verify_set_environment_async(m, message, error);
1763         if (r < 0)
1764                 return r;
1765         if (r == 0)
1766                 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1767 
1768         r = manager_client_environment_modify(m, minus, NULL);
1769         if (r < 0)
1770                 return r;
1771 
1772         return sd_bus_reply_method_return(message, NULL);
1773 }
1774 
method_unset_and_set_environment(sd_bus_message * message,void * userdata,sd_bus_error * error)1775 static int method_unset_and_set_environment(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1776         _cleanup_strv_free_ char **minus = NULL, **plus = NULL;
1777         Manager *m = userdata;
1778         int r;
1779 
1780         assert(message);
1781         assert(m);
1782 
1783         r = mac_selinux_access_check(message, "reload", error);
1784         if (r < 0)
1785                 return r;
1786 
1787         r = sd_bus_message_read_strv(message, &minus);
1788         if (r < 0)
1789                 return r;
1790 
1791         r = sd_bus_message_read_strv(message, &plus);
1792         if (r < 0)
1793                 return r;
1794 
1795         if (!strv_env_name_or_assignment_is_valid(minus))
1796                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
1797                                          "Invalid environment variable names or assignments");
1798         if (!strv_env_is_valid(plus))
1799                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
1800                                          "Invalid environment assignments");
1801 
1802         r = bus_verify_set_environment_async(m, message, error);
1803         if (r < 0)
1804                 return r;
1805         if (r == 0)
1806                 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1807 
1808         r = manager_client_environment_modify(m, minus, plus);
1809         if (r < 0)
1810                 return r;
1811 
1812         return sd_bus_reply_method_return(message, NULL);
1813 }
1814 
method_set_exit_code(sd_bus_message * message,void * userdata,sd_bus_error * error)1815 static int method_set_exit_code(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1816         Manager *m = userdata;
1817         uint8_t code;
1818         int r;
1819 
1820         assert(message);
1821         assert(m);
1822 
1823         r = mac_selinux_access_check(message, "exit", error);
1824         if (r < 0)
1825                 return r;
1826 
1827         r = sd_bus_message_read_basic(message, 'y', &code);
1828         if (r < 0)
1829                 return r;
1830 
1831         if (MANAGER_IS_SYSTEM(m) && detect_container() <= 0)
1832                 return sd_bus_error_set(error, SD_BUS_ERROR_NOT_SUPPORTED, "ExitCode can only be set for user service managers or in containers.");
1833 
1834         m->return_value = code;
1835 
1836         return sd_bus_reply_method_return(message, NULL);
1837 }
1838 
method_lookup_dynamic_user_by_name(sd_bus_message * message,void * userdata,sd_bus_error * error)1839 static int method_lookup_dynamic_user_by_name(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1840         Manager *m = userdata;
1841         const char *name;
1842         uid_t uid;
1843         int r;
1844 
1845         assert(message);
1846         assert(m);
1847 
1848         r = sd_bus_message_read_basic(message, 's', &name);
1849         if (r < 0)
1850                 return r;
1851 
1852         if (!MANAGER_IS_SYSTEM(m))
1853                 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED,
1854                                          "Dynamic users are only supported in the system instance.");
1855         if (!valid_user_group_name(name, VALID_USER_RELAX))
1856                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
1857                                          "User name invalid: %s", name);
1858 
1859         r = dynamic_user_lookup_name(m, name, &uid);
1860         if (r == -ESRCH)
1861                 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_DYNAMIC_USER,
1862                                          "Dynamic user %s does not exist.", name);
1863         if (r < 0)
1864                 return r;
1865 
1866         return sd_bus_reply_method_return(message, "u", (uint32_t) uid);
1867 }
1868 
method_lookup_dynamic_user_by_uid(sd_bus_message * message,void * userdata,sd_bus_error * error)1869 static int method_lookup_dynamic_user_by_uid(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1870         _cleanup_free_ char *name = NULL;
1871         Manager *m = userdata;
1872         uid_t uid;
1873         int r;
1874 
1875         assert(message);
1876         assert(m);
1877 
1878         assert_cc(sizeof(uid_t) == sizeof(uint32_t));
1879         r = sd_bus_message_read_basic(message, 'u', &uid);
1880         if (r < 0)
1881                 return r;
1882 
1883         if (!MANAGER_IS_SYSTEM(m))
1884                 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED,
1885                                          "Dynamic users are only supported in the system instance.");
1886         if (!uid_is_valid(uid))
1887                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
1888                                          "User ID invalid: " UID_FMT, uid);
1889 
1890         r = dynamic_user_lookup_uid(m, uid, &name);
1891         if (r == -ESRCH)
1892                 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_DYNAMIC_USER,
1893                                          "Dynamic user ID " UID_FMT " does not exist.", uid);
1894         if (r < 0)
1895                 return r;
1896 
1897         return sd_bus_reply_method_return(message, "s", name);
1898 }
1899 
method_get_dynamic_users(sd_bus_message * message,void * userdata,sd_bus_error * error)1900 static int method_get_dynamic_users(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1901         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1902         Manager *m = userdata;
1903         DynamicUser *d;
1904         int r;
1905 
1906         assert(message);
1907         assert(m);
1908 
1909         assert_cc(sizeof(uid_t) == sizeof(uint32_t));
1910 
1911         if (!MANAGER_IS_SYSTEM(m))
1912                 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED,
1913                                          "Dynamic users are only supported in the system instance.");
1914 
1915         r = sd_bus_message_new_method_return(message, &reply);
1916         if (r < 0)
1917                 return r;
1918 
1919         r = sd_bus_message_open_container(reply, 'a', "(us)");
1920         if (r < 0)
1921                 return r;
1922 
1923         HASHMAP_FOREACH(d, m->dynamic_users) {
1924                 uid_t uid;
1925 
1926                 r = dynamic_user_current(d, &uid);
1927                 if (r == -EAGAIN) /* not realized yet? */
1928                         continue;
1929                 if (r < 0)
1930                         return sd_bus_error_setf(error, SD_BUS_ERROR_FAILED,
1931                                                  "Failed to look up a dynamic user.");
1932 
1933                 r = sd_bus_message_append(reply, "(us)", uid, d->name);
1934                 if (r < 0)
1935                         return r;
1936         }
1937 
1938         r = sd_bus_message_close_container(reply);
1939         if (r < 0)
1940                 return r;
1941 
1942         return sd_bus_send(NULL, reply, NULL);
1943 }
1944 
method_enqueue_marked_jobs(sd_bus_message * message,void * userdata,sd_bus_error * error)1945 static int method_enqueue_marked_jobs(sd_bus_message *message, void *userdata, sd_bus_error *error) {
1946         Manager *m = userdata;
1947         int r;
1948 
1949         assert(message);
1950         assert(m);
1951 
1952         r = mac_selinux_access_check(message, "start", error);
1953         if (r < 0)
1954                 return r;
1955 
1956         r = bus_verify_manage_units_async(m, message, error);
1957         if (r < 0)
1958                 return r;
1959         if (r == 0)
1960                 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
1961 
1962         log_info("Queuing reload/restart jobs for marked units…");
1963 
1964         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1965         r = sd_bus_message_new_method_return(message, &reply);
1966         if (r < 0)
1967                 return r;
1968 
1969         r = sd_bus_message_open_container(reply, 'a', "o");
1970         if (r < 0)
1971                 return r;
1972 
1973         Unit *u;
1974         char *k;
1975         int ret = 0;
1976         HASHMAP_FOREACH_KEY(u, k, m->units) {
1977                 /* ignore aliases */
1978                 if (u->id != k)
1979                         continue;
1980 
1981                 BusUnitQueueFlags flags;
1982                 if (FLAGS_SET(u->markers, 1u << UNIT_MARKER_NEEDS_RESTART))
1983                         flags = 0;
1984                 else if (FLAGS_SET(u->markers, 1u << UNIT_MARKER_NEEDS_RELOAD))
1985                         flags = BUS_UNIT_QUEUE_RELOAD_IF_POSSIBLE;
1986                 else
1987                         continue;
1988 
1989                 r = mac_selinux_unit_access_check(u, message, "start", error);
1990                 if (r >= 0)
1991                         r = bus_unit_queue_job_one(message, u,
1992                                                    JOB_TRY_RESTART, JOB_FAIL, flags,
1993                                                    reply, error);
1994                 if (r < 0) {
1995                         if (ERRNO_IS_RESOURCE(r))
1996                                 return r;
1997                         if (ret >= 0)
1998                                 ret = r;
1999                         sd_bus_error_free(error);
2000                 }
2001         }
2002 
2003         if (ret < 0)
2004                 return sd_bus_error_set_errnof(error, ret,
2005                                                "Failed to enqueue some jobs, see logs for details: %m");
2006 
2007         r = sd_bus_message_close_container(reply);
2008         if (r < 0)
2009                 return r;
2010 
2011         return sd_bus_send(NULL, reply, NULL);
2012 }
2013 
list_unit_files_by_patterns(sd_bus_message * message,void * userdata,sd_bus_error * error,char ** states,char ** patterns)2014 static int list_unit_files_by_patterns(sd_bus_message *message, void *userdata, sd_bus_error *error, char **states, char **patterns) {
2015         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
2016         Manager *m = userdata;
2017         UnitFileList *item;
2018         Hashmap *h;
2019         int r;
2020 
2021         assert(message);
2022         assert(m);
2023 
2024         /* Anyone can call this method */
2025 
2026         r = mac_selinux_access_check(message, "status", error);
2027         if (r < 0)
2028                 return r;
2029 
2030         r = sd_bus_message_new_method_return(message, &reply);
2031         if (r < 0)
2032                 return r;
2033 
2034         h = hashmap_new(&string_hash_ops);
2035         if (!h)
2036                 return -ENOMEM;
2037 
2038         r = unit_file_get_list(m->unit_file_scope, NULL, h, states, patterns);
2039         if (r < 0)
2040                 goto fail;
2041 
2042         r = sd_bus_message_open_container(reply, 'a', "(ss)");
2043         if (r < 0)
2044                 goto fail;
2045 
2046         HASHMAP_FOREACH(item, h) {
2047 
2048                 r = sd_bus_message_append(reply, "(ss)", item->path, unit_file_state_to_string(item->state));
2049                 if (r < 0)
2050                         goto fail;
2051         }
2052 
2053         unit_file_list_free(h);
2054 
2055         r = sd_bus_message_close_container(reply);
2056         if (r < 0)
2057                 return r;
2058 
2059         return sd_bus_send(NULL, reply, NULL);
2060 
2061 fail:
2062         unit_file_list_free(h);
2063         return r;
2064 }
2065 
method_list_unit_files(sd_bus_message * message,void * userdata,sd_bus_error * error)2066 static int method_list_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2067         return list_unit_files_by_patterns(message, userdata, error, NULL, NULL);
2068 }
2069 
method_list_unit_files_by_patterns(sd_bus_message * message,void * userdata,sd_bus_error * error)2070 static int method_list_unit_files_by_patterns(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2071         _cleanup_strv_free_ char **states = NULL;
2072         _cleanup_strv_free_ char **patterns = NULL;
2073         int r;
2074 
2075         r = sd_bus_message_read_strv(message, &states);
2076         if (r < 0)
2077                 return r;
2078 
2079         r = sd_bus_message_read_strv(message, &patterns);
2080         if (r < 0)
2081                 return r;
2082 
2083         return list_unit_files_by_patterns(message, userdata, error, states, patterns);
2084 }
2085 
method_get_unit_file_state(sd_bus_message * message,void * userdata,sd_bus_error * error)2086 static int method_get_unit_file_state(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2087         Manager *m = userdata;
2088         const char *name;
2089         UnitFileState state;
2090         int r;
2091 
2092         assert(message);
2093         assert(m);
2094 
2095         /* Anyone can call this method */
2096 
2097         r = mac_selinux_access_check(message, "status", error);
2098         if (r < 0)
2099                 return r;
2100 
2101         r = sd_bus_message_read(message, "s", &name);
2102         if (r < 0)
2103                 return r;
2104 
2105         r = unit_file_get_state(m->unit_file_scope, NULL, name, &state);
2106         if (r < 0)
2107                 return r;
2108 
2109         return sd_bus_reply_method_return(message, "s", unit_file_state_to_string(state));
2110 }
2111 
method_get_default_target(sd_bus_message * message,void * userdata,sd_bus_error * error)2112 static int method_get_default_target(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2113         _cleanup_free_ char *default_target = NULL;
2114         Manager *m = userdata;
2115         int r;
2116 
2117         assert(message);
2118         assert(m);
2119 
2120         /* Anyone can call this method */
2121 
2122         r = mac_selinux_access_check(message, "status", error);
2123         if (r < 0)
2124                 return r;
2125 
2126         r = unit_file_get_default(m->unit_file_scope, NULL, &default_target);
2127         if (r < 0)
2128                 return r;
2129 
2130         return sd_bus_reply_method_return(message, "s", default_target);
2131 }
2132 
send_unit_files_changed(sd_bus * bus,void * userdata)2133 static int send_unit_files_changed(sd_bus *bus, void *userdata) {
2134         _cleanup_(sd_bus_message_unrefp) sd_bus_message *message = NULL;
2135         int r;
2136 
2137         assert(bus);
2138 
2139         r = sd_bus_message_new_signal(bus, &message,
2140                                       "/org/freedesktop/systemd1",
2141                                       "org.freedesktop.systemd1.Manager",
2142                                       "UnitFilesChanged");
2143         if (r < 0)
2144                 return r;
2145 
2146         return sd_bus_send(bus, message, NULL);
2147 }
2148 
2149 /* Create an error reply, using the error information from changes[]
2150  * if possible, and fall back to generating an error from error code c.
2151  * The error message only describes the first error.
2152  *
2153  * Coordinate with unit_file_dump_changes() in install.c.
2154  */
install_error(sd_bus_error * error,int c,UnitFileChange * changes,size_t n_changes)2155 static int install_error(
2156                 sd_bus_error *error,
2157                 int c,
2158                 UnitFileChange *changes,
2159                 size_t n_changes) {
2160 
2161         int r;
2162 
2163         for (size_t i = 0; i < n_changes; i++)
2164 
2165                 switch (changes[i].type_or_errno) {
2166 
2167                 case 0 ... _UNIT_FILE_CHANGE_TYPE_MAX: /* not errors */
2168                         continue;
2169 
2170                 case -EEXIST:
2171                         if (changes[i].source)
2172                                 r = sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS,
2173                                                       "File %s already exists and is a symlink to %s.",
2174                                                       changes[i].path, changes[i].source);
2175                         else
2176                                 r = sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS,
2177                                                       "File %s already exists.",
2178                                                       changes[i].path);
2179                         goto found;
2180 
2181                 case -ERFKILL:
2182                         r = sd_bus_error_setf(error, BUS_ERROR_UNIT_MASKED,
2183                                               "Unit file %s is masked.", changes[i].path);
2184                         goto found;
2185 
2186                 case -EADDRNOTAVAIL:
2187                         r = sd_bus_error_setf(error, BUS_ERROR_UNIT_GENERATED,
2188                                               "Unit %s is transient or generated.", changes[i].path);
2189                         goto found;
2190 
2191                 case -EUCLEAN:
2192                         r = sd_bus_error_setf(error, BUS_ERROR_BAD_UNIT_SETTING,
2193                                               "\"%s\" is not a valid unit name.",
2194                                               changes[i].path);
2195                         goto found;
2196 
2197                 case -ELOOP:
2198                         r = sd_bus_error_setf(error, BUS_ERROR_UNIT_LINKED,
2199                                               "Refusing to operate on alias name or linked unit file: %s",
2200                                               changes[i].path);
2201                         goto found;
2202 
2203                 case -ENOENT:
2204                         r = sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT,
2205                                               "Unit file %s does not exist.", changes[i].path);
2206                         goto found;
2207 
2208                 default:
2209                         assert(changes[i].type_or_errno < 0); /* other errors */
2210                         r = sd_bus_error_set_errnof(error, changes[i].type_or_errno, "File %s: %m", changes[i].path);
2211                         goto found;
2212                 }
2213 
2214         r = c < 0 ? c : -EINVAL;
2215 
2216  found:
2217         unit_file_changes_free(changes, n_changes);
2218         return r;
2219 }
2220 
reply_unit_file_changes_and_free(Manager * m,sd_bus_message * message,int carries_install_info,UnitFileChange * changes,size_t n_changes,sd_bus_error * error)2221 static int reply_unit_file_changes_and_free(
2222                 Manager *m,
2223                 sd_bus_message *message,
2224                 int carries_install_info,
2225                 UnitFileChange *changes,
2226                 size_t n_changes,
2227                 sd_bus_error *error) {
2228 
2229         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
2230         bool bad = false, good = false;
2231         int r;
2232 
2233         if (unit_file_changes_have_modification(changes, n_changes)) {
2234                 r = bus_foreach_bus(m, NULL, send_unit_files_changed, NULL);
2235                 if (r < 0)
2236                         log_debug_errno(r, "Failed to send UnitFilesChanged signal: %m");
2237         }
2238 
2239         r = sd_bus_message_new_method_return(message, &reply);
2240         if (r < 0)
2241                 goto fail;
2242 
2243         if (carries_install_info >= 0) {
2244                 r = sd_bus_message_append(reply, "b", carries_install_info);
2245                 if (r < 0)
2246                         goto fail;
2247         }
2248 
2249         r = sd_bus_message_open_container(reply, 'a', "(sss)");
2250         if (r < 0)
2251                 goto fail;
2252 
2253         for (size_t i = 0; i < n_changes; i++) {
2254 
2255                 if (changes[i].type_or_errno < 0) {
2256                         bad = true;
2257                         continue;
2258                 }
2259 
2260                 r = sd_bus_message_append(
2261                                 reply, "(sss)",
2262                                 unit_file_change_type_to_string(changes[i].type_or_errno),
2263                                 changes[i].path,
2264                                 changes[i].source);
2265                 if (r < 0)
2266                         goto fail;
2267 
2268                 good = true;
2269         }
2270 
2271         /* If there was a failed change, and no successful change, then return the first failure as proper
2272          * method call error. */
2273         if (bad && !good)
2274                 return install_error(error, 0, changes, n_changes);
2275 
2276         r = sd_bus_message_close_container(reply);
2277         if (r < 0)
2278                 goto fail;
2279 
2280         unit_file_changes_free(changes, n_changes);
2281         return sd_bus_send(NULL, reply, NULL);
2282 
2283 fail:
2284         unit_file_changes_free(changes, n_changes);
2285         return r;
2286 }
2287 
method_enable_unit_files_generic(sd_bus_message * message,Manager * m,int (* call)(LookupScope scope,UnitFileFlags flags,const char * root_dir,char * files[],UnitFileChange ** changes,size_t * n_changes),bool carries_install_info,sd_bus_error * error)2288 static int method_enable_unit_files_generic(
2289                 sd_bus_message *message,
2290                 Manager *m,
2291                 int (*call)(LookupScope scope, UnitFileFlags flags, const char *root_dir, char *files[], UnitFileChange **changes, size_t *n_changes),
2292                 bool carries_install_info,
2293                 sd_bus_error *error) {
2294 
2295         _cleanup_strv_free_ char **l = NULL;
2296         UnitFileChange *changes = NULL;
2297         size_t n_changes = 0;
2298         UnitFileFlags flags;
2299         int r;
2300 
2301         assert(message);
2302         assert(m);
2303 
2304         r = sd_bus_message_read_strv(message, &l);
2305         if (r < 0)
2306                 return r;
2307 
2308         if (sd_bus_message_is_method_call(message, NULL, "EnableUnitFilesWithFlags")) {
2309                 uint64_t raw_flags;
2310 
2311                 r = sd_bus_message_read(message, "t", &raw_flags);
2312                 if (r < 0)
2313                         return r;
2314                 if ((raw_flags & ~_UNIT_FILE_FLAGS_MASK_PUBLIC) != 0)
2315                         return -EINVAL;
2316                 flags = raw_flags;
2317         } else {
2318                 int runtime, force;
2319 
2320                 r = sd_bus_message_read(message, "bb", &runtime, &force);
2321                 if (r < 0)
2322                         return r;
2323                 flags = unit_file_bools_to_flags(runtime, force);
2324         }
2325 
2326         r = bus_verify_manage_unit_files_async(m, message, error);
2327         if (r < 0)
2328                 return r;
2329         if (r == 0)
2330                 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2331 
2332         r = call(m->unit_file_scope, flags, NULL, l, &changes, &n_changes);
2333         if (r < 0)
2334                 return install_error(error, r, changes, n_changes);
2335 
2336         return reply_unit_file_changes_and_free(m, message, carries_install_info ? r : -1, changes, n_changes, error);
2337 }
2338 
method_enable_unit_files_with_flags(sd_bus_message * message,void * userdata,sd_bus_error * error)2339 static int method_enable_unit_files_with_flags(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2340         return method_enable_unit_files_generic(message, userdata, unit_file_enable, true, error);
2341 }
2342 
method_enable_unit_files(sd_bus_message * message,void * userdata,sd_bus_error * error)2343 static int method_enable_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2344         return method_enable_unit_files_generic(message, userdata, unit_file_enable, true, error);
2345 }
2346 
method_reenable_unit_files(sd_bus_message * message,void * userdata,sd_bus_error * error)2347 static int method_reenable_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2348         return method_enable_unit_files_generic(message, userdata, unit_file_reenable, true, error);
2349 }
2350 
method_link_unit_files(sd_bus_message * message,void * userdata,sd_bus_error * error)2351 static int method_link_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2352         return method_enable_unit_files_generic(message, userdata, unit_file_link, false, error);
2353 }
2354 
unit_file_preset_without_mode(LookupScope scope,UnitFileFlags flags,const char * root_dir,char ** files,UnitFileChange ** changes,size_t * n_changes)2355 static int unit_file_preset_without_mode(LookupScope scope, UnitFileFlags flags, const char *root_dir, char **files, UnitFileChange **changes, size_t *n_changes) {
2356         return unit_file_preset(scope, flags, root_dir, files, UNIT_FILE_PRESET_FULL, changes, n_changes);
2357 }
2358 
method_preset_unit_files(sd_bus_message * message,void * userdata,sd_bus_error * error)2359 static int method_preset_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2360         return method_enable_unit_files_generic(message, userdata, unit_file_preset_without_mode, true, error);
2361 }
2362 
method_mask_unit_files(sd_bus_message * message,void * userdata,sd_bus_error * error)2363 static int method_mask_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2364         return method_enable_unit_files_generic(message, userdata, unit_file_mask, false, error);
2365 }
2366 
method_preset_unit_files_with_mode(sd_bus_message * message,void * userdata,sd_bus_error * error)2367 static int method_preset_unit_files_with_mode(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2368 
2369         _cleanup_strv_free_ char **l = NULL;
2370         UnitFileChange *changes = NULL;
2371         size_t n_changes = 0;
2372         Manager *m = userdata;
2373         UnitFilePresetMode preset_mode;
2374         int runtime, force, r;
2375         UnitFileFlags flags;
2376         const char *mode;
2377 
2378         assert(message);
2379         assert(m);
2380 
2381         r = sd_bus_message_read_strv(message, &l);
2382         if (r < 0)
2383                 return r;
2384 
2385         r = sd_bus_message_read(message, "sbb", &mode, &runtime, &force);
2386         if (r < 0)
2387                 return r;
2388 
2389         flags = unit_file_bools_to_flags(runtime, force);
2390 
2391         if (isempty(mode))
2392                 preset_mode = UNIT_FILE_PRESET_FULL;
2393         else {
2394                 preset_mode = unit_file_preset_mode_from_string(mode);
2395                 if (preset_mode < 0)
2396                         return -EINVAL;
2397         }
2398 
2399         r = bus_verify_manage_unit_files_async(m, message, error);
2400         if (r < 0)
2401                 return r;
2402         if (r == 0)
2403                 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2404 
2405         r = unit_file_preset(m->unit_file_scope, flags, NULL, l, preset_mode, &changes, &n_changes);
2406         if (r < 0)
2407                 return install_error(error, r, changes, n_changes);
2408 
2409         return reply_unit_file_changes_and_free(m, message, r, changes, n_changes, error);
2410 }
2411 
method_disable_unit_files_generic(sd_bus_message * message,Manager * m,int (* call)(LookupScope scope,UnitFileFlags flags,const char * root_dir,char * files[],UnitFileChange ** changes,size_t * n_changes),sd_bus_error * error)2412 static int method_disable_unit_files_generic(
2413                 sd_bus_message *message,
2414                 Manager *m,
2415                 int (*call)(LookupScope scope, UnitFileFlags flags, const char *root_dir, char *files[], UnitFileChange **changes, size_t *n_changes),
2416                 sd_bus_error *error) {
2417 
2418         _cleanup_strv_free_ char **l = NULL;
2419         UnitFileChange *changes = NULL;
2420         UnitFileFlags flags;
2421         size_t n_changes = 0;
2422         int r;
2423 
2424         assert(message);
2425         assert(m);
2426 
2427         r = sd_bus_message_read_strv(message, &l);
2428         if (r < 0)
2429                 return r;
2430 
2431         if (sd_bus_message_is_method_call(message, NULL, "DisableUnitFilesWithFlags")) {
2432                 uint64_t raw_flags;
2433 
2434                 r = sd_bus_message_read(message, "t", &raw_flags);
2435                 if (r < 0)
2436                         return r;
2437                 if ((raw_flags & ~_UNIT_FILE_FLAGS_MASK_PUBLIC) != 0 ||
2438                                 FLAGS_SET(raw_flags, UNIT_FILE_FORCE))
2439                         return -EINVAL;
2440                 flags = raw_flags;
2441         } else {
2442                 int runtime;
2443 
2444                 r = sd_bus_message_read(message, "b", &runtime);
2445                 if (r < 0)
2446                         return r;
2447                 flags = unit_file_bools_to_flags(runtime, false);
2448         }
2449 
2450         r = bus_verify_manage_unit_files_async(m, message, error);
2451         if (r < 0)
2452                 return r;
2453         if (r == 0)
2454                 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2455 
2456         r = call(m->unit_file_scope, flags, NULL, l, &changes, &n_changes);
2457         if (r < 0)
2458                 return install_error(error, r, changes, n_changes);
2459 
2460         return reply_unit_file_changes_and_free(m, message, -1, changes, n_changes, error);
2461 }
2462 
method_disable_unit_files_with_flags(sd_bus_message * message,void * userdata,sd_bus_error * error)2463 static int method_disable_unit_files_with_flags(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2464         return method_disable_unit_files_generic(message, userdata, unit_file_disable, error);
2465 }
2466 
method_disable_unit_files(sd_bus_message * message,void * userdata,sd_bus_error * error)2467 static int method_disable_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2468         return method_disable_unit_files_generic(message, userdata, unit_file_disable, error);
2469 }
2470 
method_unmask_unit_files(sd_bus_message * message,void * userdata,sd_bus_error * error)2471 static int method_unmask_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2472         return method_disable_unit_files_generic(message, userdata, unit_file_unmask, error);
2473 }
2474 
method_revert_unit_files(sd_bus_message * message,void * userdata,sd_bus_error * error)2475 static int method_revert_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2476         _cleanup_strv_free_ char **l = NULL;
2477         UnitFileChange *changes = NULL;
2478         size_t n_changes = 0;
2479         Manager *m = userdata;
2480         int r;
2481 
2482         assert(message);
2483         assert(m);
2484 
2485         r = sd_bus_message_read_strv(message, &l);
2486         if (r < 0)
2487                 return r;
2488 
2489         r = bus_verify_manage_unit_files_async(m, message, error);
2490         if (r < 0)
2491                 return r;
2492         if (r == 0)
2493                 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2494 
2495         r = unit_file_revert(m->unit_file_scope, NULL, l, &changes, &n_changes);
2496         if (r < 0)
2497                 return install_error(error, r, changes, n_changes);
2498 
2499         return reply_unit_file_changes_and_free(m, message, -1, changes, n_changes, error);
2500 }
2501 
method_set_default_target(sd_bus_message * message,void * userdata,sd_bus_error * error)2502 static int method_set_default_target(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2503         UnitFileChange *changes = NULL;
2504         size_t n_changes = 0;
2505         Manager *m = userdata;
2506         const char *name;
2507         int force, r;
2508 
2509         assert(message);
2510         assert(m);
2511 
2512         r = mac_selinux_access_check(message, "enable", error);
2513         if (r < 0)
2514                 return r;
2515 
2516         r = sd_bus_message_read(message, "sb", &name, &force);
2517         if (r < 0)
2518                 return r;
2519 
2520         r = bus_verify_manage_unit_files_async(m, message, error);
2521         if (r < 0)
2522                 return r;
2523         if (r == 0)
2524                 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2525 
2526         r = unit_file_set_default(m->unit_file_scope, force ? UNIT_FILE_FORCE : 0, NULL, name, &changes, &n_changes);
2527         if (r < 0)
2528                 return install_error(error, r, changes, n_changes);
2529 
2530         return reply_unit_file_changes_and_free(m, message, -1, changes, n_changes, error);
2531 }
2532 
method_preset_all_unit_files(sd_bus_message * message,void * userdata,sd_bus_error * error)2533 static int method_preset_all_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2534         UnitFileChange *changes = NULL;
2535         size_t n_changes = 0;
2536         Manager *m = userdata;
2537         UnitFilePresetMode preset_mode;
2538         const char *mode;
2539         UnitFileFlags flags;
2540         int force, runtime, r;
2541 
2542         assert(message);
2543         assert(m);
2544 
2545         r = mac_selinux_access_check(message, "enable", error);
2546         if (r < 0)
2547                 return r;
2548 
2549         r = sd_bus_message_read(message, "sbb", &mode, &runtime, &force);
2550         if (r < 0)
2551                 return r;
2552 
2553         flags = unit_file_bools_to_flags(runtime, force);
2554 
2555         if (isempty(mode))
2556                 preset_mode = UNIT_FILE_PRESET_FULL;
2557         else {
2558                 preset_mode = unit_file_preset_mode_from_string(mode);
2559                 if (preset_mode < 0)
2560                         return -EINVAL;
2561         }
2562 
2563         r = bus_verify_manage_unit_files_async(m, message, error);
2564         if (r < 0)
2565                 return r;
2566         if (r == 0)
2567                 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2568 
2569         r = unit_file_preset_all(m->unit_file_scope, flags, NULL, preset_mode, &changes, &n_changes);
2570         if (r < 0)
2571                 return install_error(error, r, changes, n_changes);
2572 
2573         return reply_unit_file_changes_and_free(m, message, -1, changes, n_changes, error);
2574 }
2575 
method_add_dependency_unit_files(sd_bus_message * message,void * userdata,sd_bus_error * error)2576 static int method_add_dependency_unit_files(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2577         _cleanup_strv_free_ char **l = NULL;
2578         Manager *m = userdata;
2579         UnitFileChange *changes = NULL;
2580         size_t n_changes = 0;
2581         int runtime, force, r;
2582         char *target, *type;
2583         UnitDependency dep;
2584         UnitFileFlags flags;
2585 
2586         assert(message);
2587         assert(m);
2588 
2589         r = bus_verify_manage_unit_files_async(m, message, error);
2590         if (r < 0)
2591                 return r;
2592         if (r == 0)
2593                 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2594 
2595         r = sd_bus_message_read_strv(message, &l);
2596         if (r < 0)
2597                 return r;
2598 
2599         r = sd_bus_message_read(message, "ssbb", &target, &type, &runtime, &force);
2600         if (r < 0)
2601                 return r;
2602 
2603         flags = unit_file_bools_to_flags(runtime, force);
2604 
2605         dep = unit_dependency_from_string(type);
2606         if (dep < 0)
2607                 return -EINVAL;
2608 
2609         r = unit_file_add_dependency(m->unit_file_scope, flags, NULL, l, target, dep, &changes, &n_changes);
2610         if (r < 0)
2611                 return install_error(error, r, changes, n_changes);
2612 
2613         return reply_unit_file_changes_and_free(m, message, -1, changes, n_changes, error);
2614 }
2615 
method_get_unit_file_links(sd_bus_message * message,void * userdata,sd_bus_error * error)2616 static int method_get_unit_file_links(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2617         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
2618         UnitFileChange *changes = NULL;
2619         size_t n_changes = 0, i;
2620         UnitFileFlags flags;
2621         const char *name;
2622         char **p;
2623         int runtime, r;
2624 
2625         r = sd_bus_message_read(message, "sb", &name, &runtime);
2626         if (r < 0)
2627                 return r;
2628 
2629         r = sd_bus_message_new_method_return(message, &reply);
2630         if (r < 0)
2631                 return r;
2632 
2633         r = sd_bus_message_open_container(reply, SD_BUS_TYPE_ARRAY, "s");
2634         if (r < 0)
2635                 return r;
2636 
2637         p = STRV_MAKE(name);
2638         flags = UNIT_FILE_DRY_RUN |
2639                 (runtime ? UNIT_FILE_RUNTIME : 0);
2640 
2641         r = unit_file_disable(LOOKUP_SCOPE_SYSTEM, flags, NULL, p, &changes, &n_changes);
2642         if (r < 0)
2643                 return log_error_errno(r, "Failed to get file links for %s: %m", name);
2644 
2645         for (i = 0; i < n_changes; i++)
2646                 if (changes[i].type_or_errno == UNIT_FILE_UNLINK) {
2647                         r = sd_bus_message_append(reply, "s", changes[i].path);
2648                         if (r < 0)
2649                                 return r;
2650                 }
2651 
2652         r = sd_bus_message_close_container(reply);
2653         if (r < 0)
2654                 return r;
2655 
2656         return sd_bus_send(NULL, reply, NULL);
2657 }
2658 
method_get_job_waiting(sd_bus_message * message,void * userdata,sd_bus_error * error)2659 static int method_get_job_waiting(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2660         Manager *m = userdata;
2661         uint32_t id;
2662         Job *j;
2663         int r;
2664 
2665         assert(message);
2666         assert(m);
2667 
2668         r = sd_bus_message_read(message, "u", &id);
2669         if (r < 0)
2670                 return r;
2671 
2672         j = manager_get_job(m, id);
2673         if (!j)
2674                 return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
2675 
2676         return bus_job_method_get_waiting_jobs(message, j, error);
2677 }
2678 
method_abandon_scope(sd_bus_message * message,void * userdata,sd_bus_error * error)2679 static int method_abandon_scope(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2680         Manager *m = userdata;
2681         const char *name;
2682         Unit *u;
2683         int r;
2684 
2685         assert(message);
2686         assert(m);
2687 
2688         r = sd_bus_message_read(message, "s", &name);
2689         if (r < 0)
2690                 return r;
2691 
2692         r = bus_get_unit_by_name(m, message, name, &u, error);
2693         if (r < 0)
2694                 return r;
2695 
2696         if (u->type != UNIT_SCOPE)
2697                 return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
2698                                          "Unit '%s' is not a scope unit, refusing.", name);
2699 
2700         return bus_scope_method_abandon(message, u, error);
2701 }
2702 
method_set_show_status(sd_bus_message * message,void * userdata,sd_bus_error * error)2703 static int method_set_show_status(sd_bus_message *message, void *userdata, sd_bus_error *error) {
2704         Manager *m = userdata;
2705         ShowStatus mode = _SHOW_STATUS_INVALID;
2706         const char *t;
2707         int r;
2708 
2709         assert(m);
2710         assert(message);
2711 
2712         r = mac_selinux_access_check(message, "reload", error);
2713         if (r < 0)
2714                 return r;
2715 
2716         r = bus_verify_set_environment_async(m, message, error);
2717         if (r < 0)
2718                 return r;
2719         if (r == 0)
2720                 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
2721 
2722         r = sd_bus_message_read(message, "s", &t);
2723         if (r < 0)
2724                 return r;
2725 
2726         if (!isempty(t)) {
2727                 mode = show_status_from_string(t);
2728                 if (mode < 0)
2729                         return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
2730                                                  "Invalid show status '%s'", t);
2731         }
2732 
2733         manager_override_show_status(m, mode, "bus");
2734 
2735         return sd_bus_reply_method_return(message, NULL);
2736 }
2737 
2738 const sd_bus_vtable bus_manager_vtable[] = {
2739         SD_BUS_VTABLE_START(0),
2740 
2741         SD_BUS_PROPERTY("Version", "s", property_get_version, 0, SD_BUS_VTABLE_PROPERTY_CONST),
2742         SD_BUS_PROPERTY("Features", "s", property_get_features, 0, SD_BUS_VTABLE_PROPERTY_CONST),
2743         SD_BUS_PROPERTY("Virtualization", "s", property_get_virtualization, 0, SD_BUS_VTABLE_PROPERTY_CONST),
2744         SD_BUS_PROPERTY("Architecture", "s", property_get_architecture, 0, SD_BUS_VTABLE_PROPERTY_CONST),
2745         SD_BUS_PROPERTY("Tainted", "s", property_get_tainted, 0, SD_BUS_VTABLE_PROPERTY_CONST),
2746         BUS_PROPERTY_DUAL_TIMESTAMP("FirmwareTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_FIRMWARE]), SD_BUS_VTABLE_PROPERTY_CONST),
2747         BUS_PROPERTY_DUAL_TIMESTAMP("LoaderTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_LOADER]), SD_BUS_VTABLE_PROPERTY_CONST),
2748         BUS_PROPERTY_DUAL_TIMESTAMP("KernelTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_KERNEL]), SD_BUS_VTABLE_PROPERTY_CONST),
2749         BUS_PROPERTY_DUAL_TIMESTAMP("InitRDTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_INITRD]), SD_BUS_VTABLE_PROPERTY_CONST),
2750         BUS_PROPERTY_DUAL_TIMESTAMP("UserspaceTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_USERSPACE]), SD_BUS_VTABLE_PROPERTY_CONST),
2751         BUS_PROPERTY_DUAL_TIMESTAMP("FinishTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_FINISH]), SD_BUS_VTABLE_PROPERTY_CONST),
2752         BUS_PROPERTY_DUAL_TIMESTAMP("SecurityStartTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_SECURITY_START]), SD_BUS_VTABLE_PROPERTY_CONST),
2753         BUS_PROPERTY_DUAL_TIMESTAMP("SecurityFinishTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_SECURITY_FINISH]), SD_BUS_VTABLE_PROPERTY_CONST),
2754         BUS_PROPERTY_DUAL_TIMESTAMP("GeneratorsStartTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_GENERATORS_START]), SD_BUS_VTABLE_PROPERTY_CONST),
2755         BUS_PROPERTY_DUAL_TIMESTAMP("GeneratorsFinishTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_GENERATORS_FINISH]), SD_BUS_VTABLE_PROPERTY_CONST),
2756         BUS_PROPERTY_DUAL_TIMESTAMP("UnitsLoadStartTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_UNITS_LOAD_START]), SD_BUS_VTABLE_PROPERTY_CONST),
2757         BUS_PROPERTY_DUAL_TIMESTAMP("UnitsLoadFinishTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_UNITS_LOAD_FINISH]), SD_BUS_VTABLE_PROPERTY_CONST),
2758         BUS_PROPERTY_DUAL_TIMESTAMP("UnitsLoadTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_UNITS_LOAD]), SD_BUS_VTABLE_PROPERTY_CONST),
2759         BUS_PROPERTY_DUAL_TIMESTAMP("InitRDSecurityStartTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_INITRD_SECURITY_START]), SD_BUS_VTABLE_PROPERTY_CONST),
2760         BUS_PROPERTY_DUAL_TIMESTAMP("InitRDSecurityFinishTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_INITRD_SECURITY_FINISH]), SD_BUS_VTABLE_PROPERTY_CONST),
2761         BUS_PROPERTY_DUAL_TIMESTAMP("InitRDGeneratorsStartTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_INITRD_GENERATORS_START]), SD_BUS_VTABLE_PROPERTY_CONST),
2762         BUS_PROPERTY_DUAL_TIMESTAMP("InitRDGeneratorsFinishTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_INITRD_GENERATORS_FINISH]), SD_BUS_VTABLE_PROPERTY_CONST),
2763         BUS_PROPERTY_DUAL_TIMESTAMP("InitRDUnitsLoadStartTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_INITRD_UNITS_LOAD_START]), SD_BUS_VTABLE_PROPERTY_CONST),
2764         BUS_PROPERTY_DUAL_TIMESTAMP("InitRDUnitsLoadFinishTimestamp", offsetof(Manager, timestamps[MANAGER_TIMESTAMP_INITRD_UNITS_LOAD_FINISH]), SD_BUS_VTABLE_PROPERTY_CONST),
2765         SD_BUS_WRITABLE_PROPERTY("LogLevel", "s", bus_property_get_log_level, property_set_log_level, 0, 0),
2766         SD_BUS_WRITABLE_PROPERTY("LogTarget", "s", bus_property_get_log_target, property_set_log_target, 0, 0),
2767         SD_BUS_PROPERTY("NNames", "u", property_get_hashmap_size, offsetof(Manager, units), 0),
2768         SD_BUS_PROPERTY("NFailedUnits", "u", property_get_set_size, offsetof(Manager, failed_units), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
2769         SD_BUS_PROPERTY("NJobs", "u", property_get_hashmap_size, offsetof(Manager, jobs), 0),
2770         SD_BUS_PROPERTY("NInstalledJobs", "u", bus_property_get_unsigned, offsetof(Manager, n_installed_jobs), 0),
2771         SD_BUS_PROPERTY("NFailedJobs", "u", bus_property_get_unsigned, offsetof(Manager, n_failed_jobs), 0),
2772         SD_BUS_PROPERTY("Progress", "d", property_get_progress, 0, 0),
2773         SD_BUS_PROPERTY("Environment", "as", property_get_environment, 0, 0),
2774         SD_BUS_PROPERTY("ConfirmSpawn", "b", bus_property_get_bool, offsetof(Manager, confirm_spawn), SD_BUS_VTABLE_PROPERTY_CONST),
2775         SD_BUS_PROPERTY("ShowStatus", "b", property_get_show_status, 0, 0),
2776         SD_BUS_PROPERTY("UnitPath", "as", NULL, offsetof(Manager, lookup_paths.search_path), SD_BUS_VTABLE_PROPERTY_CONST),
2777         SD_BUS_PROPERTY("DefaultStandardOutput", "s", bus_property_get_exec_output, offsetof(Manager, default_std_output), SD_BUS_VTABLE_PROPERTY_CONST),
2778         SD_BUS_PROPERTY("DefaultStandardError", "s", bus_property_get_exec_output, offsetof(Manager, default_std_error), SD_BUS_VTABLE_PROPERTY_CONST),
2779         SD_BUS_WRITABLE_PROPERTY("RuntimeWatchdogUSec", "t", property_get_runtime_watchdog, property_set_runtime_watchdog, 0, 0),
2780         SD_BUS_WRITABLE_PROPERTY("RuntimeWatchdogPreUSec", "t", property_get_pretimeout_watchdog, property_set_pretimeout_watchdog, 0, 0),
2781         SD_BUS_WRITABLE_PROPERTY("RuntimeWatchdogPreGovernor", "s", property_get_pretimeout_watchdog_governor, property_set_pretimeout_watchdog_governor, 0, 0),
2782         SD_BUS_WRITABLE_PROPERTY("RebootWatchdogUSec", "t", property_get_reboot_watchdog, property_set_reboot_watchdog, 0, 0),
2783         /* The following item is an obsolete alias */
2784         SD_BUS_WRITABLE_PROPERTY("ShutdownWatchdogUSec", "t", property_get_reboot_watchdog, property_set_reboot_watchdog, 0, SD_BUS_VTABLE_HIDDEN),
2785         SD_BUS_WRITABLE_PROPERTY("KExecWatchdogUSec", "t", property_get_kexec_watchdog, property_set_kexec_watchdog, 0, 0),
2786         SD_BUS_WRITABLE_PROPERTY("ServiceWatchdogs", "b", bus_property_get_bool, bus_property_set_bool, offsetof(Manager, service_watchdogs), 0),
2787         SD_BUS_PROPERTY("ControlGroup", "s", NULL, offsetof(Manager, cgroup_root), 0),
2788         SD_BUS_PROPERTY("SystemState", "s", property_get_system_state, 0, 0),
2789         SD_BUS_PROPERTY("ExitCode", "y", bus_property_get_unsigned, offsetof(Manager, return_value), 0),
2790         SD_BUS_PROPERTY("DefaultTimerAccuracyUSec", "t", bus_property_get_usec, offsetof(Manager, default_timer_accuracy_usec), SD_BUS_VTABLE_PROPERTY_CONST),
2791         SD_BUS_PROPERTY("DefaultTimeoutStartUSec", "t", bus_property_get_usec, offsetof(Manager, default_timeout_start_usec), SD_BUS_VTABLE_PROPERTY_CONST),
2792         SD_BUS_PROPERTY("DefaultTimeoutStopUSec", "t", bus_property_get_usec, offsetof(Manager, default_timeout_stop_usec), SD_BUS_VTABLE_PROPERTY_CONST),
2793         SD_BUS_PROPERTY("DefaultTimeoutAbortUSec", "t", property_get_default_timeout_abort_usec, 0, 0),
2794         SD_BUS_PROPERTY("DefaultRestartUSec", "t", bus_property_get_usec, offsetof(Manager, default_restart_usec), SD_BUS_VTABLE_PROPERTY_CONST),
2795         SD_BUS_PROPERTY("DefaultStartLimitIntervalUSec", "t", bus_property_get_usec, offsetof(Manager, default_start_limit_interval), SD_BUS_VTABLE_PROPERTY_CONST),
2796         /* The following two items are obsolete alias */
2797         SD_BUS_PROPERTY("DefaultStartLimitIntervalSec", "t", bus_property_get_usec, offsetof(Manager, default_start_limit_interval), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
2798         SD_BUS_PROPERTY("DefaultStartLimitInterval", "t", bus_property_get_usec, offsetof(Manager, default_start_limit_interval), SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN),
2799         SD_BUS_PROPERTY("DefaultStartLimitBurst", "u", bus_property_get_unsigned, offsetof(Manager, default_start_limit_burst), SD_BUS_VTABLE_PROPERTY_CONST),
2800         SD_BUS_PROPERTY("DefaultCPUAccounting", "b", bus_property_get_bool, offsetof(Manager, default_cpu_accounting), SD_BUS_VTABLE_PROPERTY_CONST),
2801         SD_BUS_PROPERTY("DefaultBlockIOAccounting", "b", bus_property_get_bool, offsetof(Manager, default_blockio_accounting), SD_BUS_VTABLE_PROPERTY_CONST),
2802         SD_BUS_PROPERTY("DefaultMemoryAccounting", "b", bus_property_get_bool, offsetof(Manager, default_memory_accounting), SD_BUS_VTABLE_PROPERTY_CONST),
2803         SD_BUS_PROPERTY("DefaultTasksAccounting", "b", bus_property_get_bool, offsetof(Manager, default_tasks_accounting), SD_BUS_VTABLE_PROPERTY_CONST),
2804         SD_BUS_PROPERTY("DefaultLimitCPU", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_CPU]), SD_BUS_VTABLE_PROPERTY_CONST),
2805         SD_BUS_PROPERTY("DefaultLimitCPUSoft", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_CPU]), SD_BUS_VTABLE_PROPERTY_CONST),
2806         SD_BUS_PROPERTY("DefaultLimitFSIZE", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_FSIZE]), SD_BUS_VTABLE_PROPERTY_CONST),
2807         SD_BUS_PROPERTY("DefaultLimitFSIZESoft", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_FSIZE]), SD_BUS_VTABLE_PROPERTY_CONST),
2808         SD_BUS_PROPERTY("DefaultLimitDATA", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_DATA]), SD_BUS_VTABLE_PROPERTY_CONST),
2809         SD_BUS_PROPERTY("DefaultLimitDATASoft", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_DATA]), SD_BUS_VTABLE_PROPERTY_CONST),
2810         SD_BUS_PROPERTY("DefaultLimitSTACK", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_STACK]), SD_BUS_VTABLE_PROPERTY_CONST),
2811         SD_BUS_PROPERTY("DefaultLimitSTACKSoft", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_STACK]), SD_BUS_VTABLE_PROPERTY_CONST),
2812         SD_BUS_PROPERTY("DefaultLimitCORE", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_CORE]), SD_BUS_VTABLE_PROPERTY_CONST),
2813         SD_BUS_PROPERTY("DefaultLimitCORESoft", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_CORE]), SD_BUS_VTABLE_PROPERTY_CONST),
2814         SD_BUS_PROPERTY("DefaultLimitRSS", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_RSS]), SD_BUS_VTABLE_PROPERTY_CONST),
2815         SD_BUS_PROPERTY("DefaultLimitRSSSoft", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_RSS]), SD_BUS_VTABLE_PROPERTY_CONST),
2816         SD_BUS_PROPERTY("DefaultLimitNOFILE", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_NOFILE]), SD_BUS_VTABLE_PROPERTY_CONST),
2817         SD_BUS_PROPERTY("DefaultLimitNOFILESoft", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_NOFILE]), SD_BUS_VTABLE_PROPERTY_CONST),
2818         SD_BUS_PROPERTY("DefaultLimitAS", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_AS]), SD_BUS_VTABLE_PROPERTY_CONST),
2819         SD_BUS_PROPERTY("DefaultLimitASSoft", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_AS]), SD_BUS_VTABLE_PROPERTY_CONST),
2820         SD_BUS_PROPERTY("DefaultLimitNPROC", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_NPROC]), SD_BUS_VTABLE_PROPERTY_CONST),
2821         SD_BUS_PROPERTY("DefaultLimitNPROCSoft", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_NPROC]), SD_BUS_VTABLE_PROPERTY_CONST),
2822         SD_BUS_PROPERTY("DefaultLimitMEMLOCK", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_MEMLOCK]), SD_BUS_VTABLE_PROPERTY_CONST),
2823         SD_BUS_PROPERTY("DefaultLimitMEMLOCKSoft", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_MEMLOCK]), SD_BUS_VTABLE_PROPERTY_CONST),
2824         SD_BUS_PROPERTY("DefaultLimitLOCKS", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_LOCKS]), SD_BUS_VTABLE_PROPERTY_CONST),
2825         SD_BUS_PROPERTY("DefaultLimitLOCKSSoft", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_LOCKS]), SD_BUS_VTABLE_PROPERTY_CONST),
2826         SD_BUS_PROPERTY("DefaultLimitSIGPENDING", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_SIGPENDING]), SD_BUS_VTABLE_PROPERTY_CONST),
2827         SD_BUS_PROPERTY("DefaultLimitSIGPENDINGSoft", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_SIGPENDING]), SD_BUS_VTABLE_PROPERTY_CONST),
2828         SD_BUS_PROPERTY("DefaultLimitMSGQUEUE", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_MSGQUEUE]), SD_BUS_VTABLE_PROPERTY_CONST),
2829         SD_BUS_PROPERTY("DefaultLimitMSGQUEUESoft", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_MSGQUEUE]), SD_BUS_VTABLE_PROPERTY_CONST),
2830         SD_BUS_PROPERTY("DefaultLimitNICE", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_NICE]), SD_BUS_VTABLE_PROPERTY_CONST),
2831         SD_BUS_PROPERTY("DefaultLimitNICESoft", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_NICE]), SD_BUS_VTABLE_PROPERTY_CONST),
2832         SD_BUS_PROPERTY("DefaultLimitRTPRIO", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_RTPRIO]), SD_BUS_VTABLE_PROPERTY_CONST),
2833         SD_BUS_PROPERTY("DefaultLimitRTPRIOSoft", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_RTPRIO]), SD_BUS_VTABLE_PROPERTY_CONST),
2834         SD_BUS_PROPERTY("DefaultLimitRTTIME", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_RTTIME]), SD_BUS_VTABLE_PROPERTY_CONST),
2835         SD_BUS_PROPERTY("DefaultLimitRTTIMESoft", "t", bus_property_get_rlimit, offsetof(Manager, rlimit[RLIMIT_RTTIME]), SD_BUS_VTABLE_PROPERTY_CONST),
2836         SD_BUS_PROPERTY("DefaultTasksMax", "t", bus_property_get_tasks_max, offsetof(Manager, default_tasks_max), 0),
2837         SD_BUS_PROPERTY("TimerSlackNSec", "t", property_get_timer_slack_nsec, 0, SD_BUS_VTABLE_PROPERTY_CONST),
2838         SD_BUS_PROPERTY("DefaultOOMPolicy", "s", bus_property_get_oom_policy, offsetof(Manager, default_oom_policy), SD_BUS_VTABLE_PROPERTY_CONST),
2839         SD_BUS_PROPERTY("DefaultOOMScoreAdjust", "i", property_get_oom_score_adjust, 0, SD_BUS_VTABLE_PROPERTY_CONST),
2840         SD_BUS_PROPERTY("CtrlAltDelBurstAction", "s", bus_property_get_emergency_action, offsetof(Manager, cad_burst_action), SD_BUS_VTABLE_PROPERTY_CONST),
2841 
2842         SD_BUS_METHOD_WITH_ARGS("GetUnit",
2843                                 SD_BUS_ARGS("s", name),
2844                                 SD_BUS_RESULT("o", unit),
2845                                 method_get_unit,
2846                                 SD_BUS_VTABLE_UNPRIVILEGED),
2847         SD_BUS_METHOD_WITH_ARGS("GetUnitByPID",
2848                                 SD_BUS_ARGS("u", pid),
2849                                 SD_BUS_RESULT("o", unit),
2850                                 method_get_unit_by_pid,
2851                                 SD_BUS_VTABLE_UNPRIVILEGED),
2852         SD_BUS_METHOD_WITH_ARGS("GetUnitByInvocationID",
2853                                 SD_BUS_ARGS("ay", invocation_id),
2854                                 SD_BUS_RESULT("o", unit),
2855                                 method_get_unit_by_invocation_id,
2856                                 SD_BUS_VTABLE_UNPRIVILEGED),
2857         SD_BUS_METHOD_WITH_ARGS("GetUnitByControlGroup",
2858                                 SD_BUS_ARGS("s", cgroup),
2859                                 SD_BUS_RESULT("o", unit),
2860                                 method_get_unit_by_control_group,
2861                                 SD_BUS_VTABLE_UNPRIVILEGED),
2862         SD_BUS_METHOD_WITH_ARGS("LoadUnit",
2863                                 SD_BUS_ARGS("s", name),
2864                                 SD_BUS_RESULT("o", unit),
2865                                 method_load_unit,
2866                                 SD_BUS_VTABLE_UNPRIVILEGED),
2867         SD_BUS_METHOD_WITH_ARGS("StartUnit",
2868                                 SD_BUS_ARGS("s", name, "s", mode),
2869                                 SD_BUS_RESULT("o", job),
2870                                 method_start_unit,
2871                                 SD_BUS_VTABLE_UNPRIVILEGED),
2872         SD_BUS_METHOD_WITH_ARGS("StartUnitWithFlags",
2873                                 SD_BUS_ARGS("s", name, "s", mode, "t", flags),
2874                                 SD_BUS_RESULT("o", job),
2875                                 method_start_unit,
2876                                 SD_BUS_VTABLE_UNPRIVILEGED),
2877         SD_BUS_METHOD_WITH_ARGS("StartUnitReplace",
2878                                 SD_BUS_ARGS("s", old_unit, "s", new_unit, "s", mode),
2879                                 SD_BUS_RESULT("o", job),
2880                                 method_start_unit_replace,
2881                                 SD_BUS_VTABLE_UNPRIVILEGED),
2882         SD_BUS_METHOD_WITH_ARGS("StopUnit",
2883                                 SD_BUS_ARGS("s", name, "s", mode),
2884                                 SD_BUS_RESULT("o", job),
2885                                 method_stop_unit,
2886                                 SD_BUS_VTABLE_UNPRIVILEGED),
2887         SD_BUS_METHOD_WITH_ARGS("ReloadUnit",
2888                                 SD_BUS_ARGS("s", name, "s", mode),
2889                                 SD_BUS_RESULT("o", job),
2890                                 method_reload_unit,
2891                                 SD_BUS_VTABLE_UNPRIVILEGED),
2892         SD_BUS_METHOD_WITH_ARGS("RestartUnit",
2893                                 SD_BUS_ARGS("s", name, "s", mode),
2894                                 SD_BUS_RESULT("o", job),
2895                                 method_restart_unit,
2896                                 SD_BUS_VTABLE_UNPRIVILEGED),
2897         SD_BUS_METHOD_WITH_ARGS("TryRestartUnit",
2898                                 SD_BUS_ARGS("s", name, "s", mode),
2899                                 SD_BUS_RESULT("o", job),
2900                                 method_try_restart_unit,
2901                                 SD_BUS_VTABLE_UNPRIVILEGED),
2902         SD_BUS_METHOD_WITH_ARGS("ReloadOrRestartUnit",
2903                                 SD_BUS_ARGS("s", name, "s", mode),
2904                                 SD_BUS_RESULT("o", job),
2905                                 method_reload_or_restart_unit,
2906                                 SD_BUS_VTABLE_UNPRIVILEGED),
2907         SD_BUS_METHOD_WITH_ARGS("ReloadOrTryRestartUnit",
2908                                 SD_BUS_ARGS("s", name, "s", mode),
2909                                 SD_BUS_RESULT("o", job),
2910                                 method_reload_or_try_restart_unit,
2911                                 SD_BUS_VTABLE_UNPRIVILEGED),
2912         SD_BUS_METHOD_WITH_ARGS("EnqueueUnitJob",
2913                                 SD_BUS_ARGS("s", name, "s", job_type, "s", job_mode),
2914                                 SD_BUS_RESULT("u", job_id, "o", job_path, "s", unit_id, "o", unit_path, "s", job_type, "a(uosos)", affected_jobs),
2915                                 method_enqueue_unit_job,
2916                                 SD_BUS_VTABLE_UNPRIVILEGED),
2917         SD_BUS_METHOD_WITH_ARGS("KillUnit",
2918                                 SD_BUS_ARGS("s", name, "s", whom, "i", signal),
2919                                 SD_BUS_NO_RESULT,
2920                                 method_kill_unit,
2921                                 SD_BUS_VTABLE_UNPRIVILEGED),
2922         SD_BUS_METHOD_WITH_ARGS("CleanUnit",
2923                                 SD_BUS_ARGS("s", name, "as", mask),
2924                                 SD_BUS_NO_RESULT,
2925                                 method_clean_unit,
2926                                 SD_BUS_VTABLE_UNPRIVILEGED),
2927         SD_BUS_METHOD_WITH_ARGS("FreezeUnit",
2928                                 SD_BUS_ARGS("s", name),
2929                                 SD_BUS_NO_RESULT,
2930                                 method_freeze_unit,
2931                                 SD_BUS_VTABLE_UNPRIVILEGED),
2932         SD_BUS_METHOD_WITH_ARGS("ThawUnit",
2933                                 SD_BUS_ARGS("s", name),
2934                                 SD_BUS_NO_RESULT,
2935                                 method_thaw_unit,
2936                                 SD_BUS_VTABLE_UNPRIVILEGED),
2937         SD_BUS_METHOD_WITH_ARGS("ResetFailedUnit",
2938                                 SD_BUS_ARGS("s", name),
2939                                 SD_BUS_NO_RESULT,
2940                                 method_reset_failed_unit,
2941                                 SD_BUS_VTABLE_UNPRIVILEGED),
2942         SD_BUS_METHOD_WITH_ARGS("SetUnitProperties",
2943                                 SD_BUS_ARGS("s", name, "b", runtime, "a(sv)", properties),
2944                                 SD_BUS_NO_RESULT,
2945                                 method_set_unit_properties,
2946                                 SD_BUS_VTABLE_UNPRIVILEGED),
2947         SD_BUS_METHOD_WITH_ARGS("BindMountUnit",
2948                                 SD_BUS_ARGS("s", name, "s", source, "s", destination, "b", read_only, "b", mkdir),
2949                                 SD_BUS_NO_RESULT,
2950                                 method_bind_mount_unit,
2951                                 SD_BUS_VTABLE_UNPRIVILEGED),
2952         SD_BUS_METHOD_WITH_ARGS("MountImageUnit",
2953                                 SD_BUS_ARGS("s", name, "s", source, "s", destination, "b", read_only, "b", mkdir, "a(ss)", options),
2954                                 SD_BUS_NO_RESULT,
2955                                 method_mount_image_unit,
2956                                 SD_BUS_VTABLE_UNPRIVILEGED),
2957         SD_BUS_METHOD_WITH_ARGS("RefUnit",
2958                                 SD_BUS_ARGS("s", name),
2959                                 SD_BUS_NO_RESULT,
2960                                 method_ref_unit,
2961                                 SD_BUS_VTABLE_UNPRIVILEGED),
2962         SD_BUS_METHOD_WITH_ARGS("UnrefUnit",
2963                                 SD_BUS_ARGS("s", name),
2964                                 SD_BUS_NO_RESULT,
2965                                 method_unref_unit,
2966                                 SD_BUS_VTABLE_UNPRIVILEGED),
2967         SD_BUS_METHOD_WITH_ARGS("StartTransientUnit",
2968                                 SD_BUS_ARGS("s", name, "s", mode, "a(sv)", properties, "a(sa(sv))", aux),
2969                                 SD_BUS_RESULT("o", job),
2970                                 method_start_transient_unit,
2971                                 SD_BUS_VTABLE_UNPRIVILEGED),
2972         SD_BUS_METHOD_WITH_ARGS("GetUnitProcesses",
2973                                 SD_BUS_ARGS("s", name),
2974                                 SD_BUS_RESULT("a(sus)", processes),
2975                                 method_get_unit_processes,
2976                                 SD_BUS_VTABLE_UNPRIVILEGED),
2977         SD_BUS_METHOD_WITH_ARGS("AttachProcessesToUnit",
2978                                 SD_BUS_ARGS("s", unit_name, "s", subcgroup, "au", pids),
2979                                 SD_BUS_NO_RESULT,
2980                                 method_attach_processes_to_unit,
2981                                 SD_BUS_VTABLE_UNPRIVILEGED),
2982         SD_BUS_METHOD_WITH_ARGS("AbandonScope",
2983                                 SD_BUS_ARGS("s", name),
2984                                 SD_BUS_NO_RESULT,
2985                                 method_abandon_scope,
2986                                 SD_BUS_VTABLE_UNPRIVILEGED),
2987         SD_BUS_METHOD_WITH_ARGS("GetJob",
2988                                 SD_BUS_ARGS("u", id),
2989                                 SD_BUS_RESULT("o", job),
2990                                 method_get_job,
2991                                 SD_BUS_VTABLE_UNPRIVILEGED),
2992         SD_BUS_METHOD_WITH_ARGS("GetJobAfter",
2993                                 SD_BUS_ARGS("u", id),
2994                                 SD_BUS_RESULT("a(usssoo)", jobs),
2995                                 method_get_job_waiting,
2996                                 SD_BUS_VTABLE_UNPRIVILEGED),
2997         SD_BUS_METHOD_WITH_ARGS("GetJobBefore",
2998                                 SD_BUS_ARGS("u", id),
2999                                 SD_BUS_RESULT("a(usssoo)", jobs),
3000                                 method_get_job_waiting,
3001                                 SD_BUS_VTABLE_UNPRIVILEGED),
3002         SD_BUS_METHOD_WITH_ARGS("CancelJob",
3003                                 SD_BUS_ARGS("u", id),
3004                                 SD_BUS_NO_RESULT,
3005                                 method_cancel_job,
3006                                 SD_BUS_VTABLE_UNPRIVILEGED),
3007         SD_BUS_METHOD("ClearJobs",
3008                       NULL,
3009                       NULL,
3010                       method_clear_jobs,
3011                       SD_BUS_VTABLE_UNPRIVILEGED),
3012         SD_BUS_METHOD("ResetFailed",
3013                       NULL,
3014                       NULL,
3015                       method_reset_failed,
3016                       SD_BUS_VTABLE_UNPRIVILEGED),
3017         SD_BUS_METHOD_WITH_ARGS("SetShowStatus",
3018                                 SD_BUS_ARGS("s", mode),
3019                                 SD_BUS_NO_RESULT,
3020                                 method_set_show_status,
3021                                 SD_BUS_VTABLE_UNPRIVILEGED),
3022         SD_BUS_METHOD_WITH_ARGS("ListUnits",
3023                                 SD_BUS_NO_ARGS,
3024                                 SD_BUS_RESULT("a(ssssssouso)", units),
3025                                 method_list_units,
3026                                 SD_BUS_VTABLE_UNPRIVILEGED),
3027         SD_BUS_METHOD_WITH_ARGS("ListUnitsFiltered",
3028                                 SD_BUS_ARGS("as", states),
3029                                 SD_BUS_RESULT("a(ssssssouso)", units),
3030                                 method_list_units_filtered,
3031                                 SD_BUS_VTABLE_UNPRIVILEGED),
3032         SD_BUS_METHOD_WITH_ARGS("ListUnitsByPatterns",
3033                                 SD_BUS_ARGS("as", states, "as", patterns),
3034                                 SD_BUS_RESULT("a(ssssssouso)", units),
3035                                 method_list_units_by_patterns,
3036                                 SD_BUS_VTABLE_UNPRIVILEGED),
3037         SD_BUS_METHOD_WITH_ARGS("ListUnitsByNames",
3038                                 SD_BUS_ARGS("as", names),
3039                                 SD_BUS_RESULT("a(ssssssouso)", units),
3040                                 method_list_units_by_names,
3041                                 SD_BUS_VTABLE_UNPRIVILEGED),
3042         SD_BUS_METHOD_WITH_ARGS("ListJobs",
3043                                 SD_BUS_NO_ARGS,
3044                                 SD_BUS_RESULT("a(usssoo)", jobs),
3045                                 method_list_jobs,
3046                                 SD_BUS_VTABLE_UNPRIVILEGED),
3047         SD_BUS_METHOD("Subscribe",
3048                       NULL,
3049                       NULL,
3050                       method_subscribe,
3051                       SD_BUS_VTABLE_UNPRIVILEGED),
3052         SD_BUS_METHOD("Unsubscribe",
3053                       NULL,
3054                       NULL,
3055                       method_unsubscribe,
3056                       SD_BUS_VTABLE_UNPRIVILEGED),
3057         SD_BUS_METHOD_WITH_ARGS("Dump",
3058                                 SD_BUS_NO_ARGS,
3059                                 SD_BUS_RESULT("s", output),
3060                                 method_dump,
3061                                 SD_BUS_VTABLE_UNPRIVILEGED),
3062         SD_BUS_METHOD_WITH_ARGS("DumpByFileDescriptor",
3063                                 SD_BUS_NO_ARGS,
3064                                 SD_BUS_RESULT("h", fd),
3065                                 method_dump_by_fd,
3066                                 SD_BUS_VTABLE_UNPRIVILEGED),
3067         SD_BUS_METHOD_WITH_ARGS("CreateSnapshot",
3068                                 SD_BUS_ARGS("s", name, "b", cleanup),
3069                                 SD_BUS_RESULT("o", unit),
3070                                 method_refuse_snapshot,
3071                                 SD_BUS_VTABLE_UNPRIVILEGED|SD_BUS_VTABLE_HIDDEN),
3072         SD_BUS_METHOD_WITH_ARGS("RemoveSnapshot",
3073                                 SD_BUS_ARGS("s", name),
3074                                 SD_BUS_NO_RESULT,
3075                                 method_refuse_snapshot,
3076                                 SD_BUS_VTABLE_UNPRIVILEGED|SD_BUS_VTABLE_HIDDEN),
3077         SD_BUS_METHOD("Reload",
3078                       NULL,
3079                       NULL,
3080                       method_reload,
3081                       SD_BUS_VTABLE_UNPRIVILEGED),
3082         SD_BUS_METHOD("Reexecute",
3083                       NULL,
3084                       NULL,
3085                       method_reexecute,
3086                       SD_BUS_VTABLE_UNPRIVILEGED|SD_BUS_VTABLE_METHOD_NO_REPLY),
3087         SD_BUS_METHOD("Exit",
3088                       NULL,
3089                       NULL,
3090                       method_exit,
3091                       0),
3092         SD_BUS_METHOD("Reboot",
3093                       NULL,
3094                       NULL,
3095                       method_reboot,
3096                       SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)),
3097         SD_BUS_METHOD("PowerOff",
3098                       NULL,
3099                       NULL,
3100                       method_poweroff,
3101                       SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)),
3102         SD_BUS_METHOD("Halt",
3103                       NULL,
3104                       NULL,
3105                       method_halt,
3106                       SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)),
3107         SD_BUS_METHOD("KExec",
3108                       NULL,
3109                       NULL,
3110                       method_kexec,
3111                       SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)),
3112         SD_BUS_METHOD_WITH_ARGS("SwitchRoot",
3113                                 SD_BUS_ARGS("s", new_root, "s", init),
3114                                 SD_BUS_NO_RESULT,
3115                                 method_switch_root,
3116                                 SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)),
3117         SD_BUS_METHOD_WITH_ARGS("SetEnvironment",
3118                                 SD_BUS_ARGS("as", assignments),
3119                                 SD_BUS_NO_RESULT,
3120                                 method_set_environment,
3121                                 SD_BUS_VTABLE_UNPRIVILEGED),
3122         SD_BUS_METHOD_WITH_ARGS("UnsetEnvironment",
3123                                 SD_BUS_ARGS("as", names),
3124                                 SD_BUS_NO_RESULT,
3125                                 method_unset_environment,
3126                                 SD_BUS_VTABLE_UNPRIVILEGED),
3127         SD_BUS_METHOD_WITH_ARGS("UnsetAndSetEnvironment",
3128                                 SD_BUS_ARGS("as", names, "as", assignments),
3129                                 SD_BUS_NO_RESULT,
3130                                 method_unset_and_set_environment,
3131                                 SD_BUS_VTABLE_UNPRIVILEGED),
3132         SD_BUS_METHOD_WITH_ARGS("EnqueueMarkedJobs",
3133                                 SD_BUS_NO_ARGS,
3134                                 SD_BUS_RESULT("ao", jobs),
3135                                 method_enqueue_marked_jobs,
3136                                 SD_BUS_VTABLE_UNPRIVILEGED),
3137         SD_BUS_METHOD_WITH_ARGS("ListUnitFiles",
3138                                 SD_BUS_NO_ARGS,
3139                                 SD_BUS_RESULT("a(ss)", unit_files),
3140                                 method_list_unit_files,
3141                                 SD_BUS_VTABLE_UNPRIVILEGED),
3142         SD_BUS_METHOD_WITH_ARGS("ListUnitFilesByPatterns",
3143                                 SD_BUS_ARGS("as", states, "as", patterns),
3144                                 SD_BUS_RESULT("a(ss)", unit_files),
3145                                 method_list_unit_files_by_patterns,
3146                                 SD_BUS_VTABLE_UNPRIVILEGED),
3147         SD_BUS_METHOD_WITH_ARGS("GetUnitFileState",
3148                                 SD_BUS_ARGS("s", file),
3149                                 SD_BUS_RESULT("s", state),
3150                                 method_get_unit_file_state,
3151                                 SD_BUS_VTABLE_UNPRIVILEGED),
3152         SD_BUS_METHOD_WITH_ARGS("EnableUnitFiles",
3153                                 SD_BUS_ARGS("as", files, "b", runtime, "b", force),
3154                                 SD_BUS_RESULT("b", carries_install_info, "a(sss)", changes),
3155                                 method_enable_unit_files,
3156                                 SD_BUS_VTABLE_UNPRIVILEGED),
3157         SD_BUS_METHOD_WITH_ARGS("DisableUnitFiles",
3158                                 SD_BUS_ARGS("as", files, "b", runtime),
3159                                 SD_BUS_RESULT("a(sss)", changes),
3160                                 method_disable_unit_files,
3161                                 SD_BUS_VTABLE_UNPRIVILEGED),
3162         SD_BUS_METHOD_WITH_ARGS("EnableUnitFilesWithFlags",
3163                                 SD_BUS_ARGS("as", files, "t", flags),
3164                                 SD_BUS_RESULT("b", carries_install_info, "a(sss)", changes),
3165                                 method_enable_unit_files_with_flags,
3166                                 SD_BUS_VTABLE_UNPRIVILEGED),
3167         SD_BUS_METHOD_WITH_ARGS("DisableUnitFilesWithFlags",
3168                                 SD_BUS_ARGS("as", files, "t", flags),
3169                                 SD_BUS_RESULT("a(sss)", changes),
3170                                 method_disable_unit_files_with_flags,
3171                                 SD_BUS_VTABLE_UNPRIVILEGED),
3172         SD_BUS_METHOD_WITH_ARGS("ReenableUnitFiles",
3173                                 SD_BUS_ARGS("as", files, "b", runtime, "b", force),
3174                                 SD_BUS_RESULT("b", carries_install_info, "a(sss)", changes),
3175                                 method_reenable_unit_files,
3176                                 SD_BUS_VTABLE_UNPRIVILEGED),
3177         SD_BUS_METHOD_WITH_ARGS("LinkUnitFiles",
3178                                 SD_BUS_ARGS("as", files, "b", runtime, "b", force),
3179                                 SD_BUS_RESULT("a(sss)", changes),
3180                                 method_link_unit_files,
3181                                 SD_BUS_VTABLE_UNPRIVILEGED),
3182         SD_BUS_METHOD_WITH_ARGS("PresetUnitFiles",
3183                                 SD_BUS_ARGS("as", files, "b", runtime, "b", force),
3184                                 SD_BUS_RESULT("b", carries_install_info, "a(sss)", changes),
3185                                 method_preset_unit_files,
3186                                 SD_BUS_VTABLE_UNPRIVILEGED),
3187         SD_BUS_METHOD_WITH_ARGS("PresetUnitFilesWithMode",
3188                                 SD_BUS_ARGS("as", files, "s", mode, "b", runtime, "b", force),
3189                                 SD_BUS_RESULT("b", carries_install_info, "a(sss)", changes),
3190                                 method_preset_unit_files_with_mode,
3191                                 SD_BUS_VTABLE_UNPRIVILEGED),
3192         SD_BUS_METHOD_WITH_ARGS("MaskUnitFiles",
3193                                 SD_BUS_ARGS("as", files, "b", runtime, "b", force),
3194                                 SD_BUS_RESULT("a(sss)", changes),
3195                                 method_mask_unit_files,
3196                                 SD_BUS_VTABLE_UNPRIVILEGED),
3197         SD_BUS_METHOD_WITH_ARGS("UnmaskUnitFiles",
3198                                 SD_BUS_ARGS("as", files, "b", runtime),
3199                                 SD_BUS_RESULT("a(sss)", changes),
3200                                 method_unmask_unit_files,
3201                                 SD_BUS_VTABLE_UNPRIVILEGED),
3202         SD_BUS_METHOD_WITH_ARGS("RevertUnitFiles",
3203                                 SD_BUS_ARGS("as", files),
3204                                 SD_BUS_RESULT("a(sss)", changes),
3205                                 method_revert_unit_files,
3206                                 SD_BUS_VTABLE_UNPRIVILEGED),
3207         SD_BUS_METHOD_WITH_ARGS("SetDefaultTarget",
3208                                 SD_BUS_ARGS("s", name, "b", force),
3209                                 SD_BUS_RESULT("a(sss)", changes),
3210                                 method_set_default_target,
3211                                 SD_BUS_VTABLE_UNPRIVILEGED),
3212         SD_BUS_METHOD_WITH_ARGS("GetDefaultTarget",
3213                                 SD_BUS_NO_ARGS,
3214                                 SD_BUS_RESULT("s", name),
3215                                 method_get_default_target,
3216                                 SD_BUS_VTABLE_UNPRIVILEGED),
3217         SD_BUS_METHOD_WITH_ARGS("PresetAllUnitFiles",
3218                                 SD_BUS_ARGS("s", mode, "b", runtime, "b", force),
3219                                 SD_BUS_RESULT("a(sss)", changes),
3220                                 method_preset_all_unit_files,
3221                                 SD_BUS_VTABLE_UNPRIVILEGED),
3222         SD_BUS_METHOD_WITH_ARGS("AddDependencyUnitFiles",
3223                                 SD_BUS_ARGS("as", files, "s", target, "s", type, "b", runtime, "b", force),
3224                                 SD_BUS_RESULT("a(sss)", changes),
3225                                 method_add_dependency_unit_files,
3226                                 SD_BUS_VTABLE_UNPRIVILEGED),
3227         SD_BUS_METHOD_WITH_ARGS("GetUnitFileLinks",
3228                                 SD_BUS_ARGS("s", name, "b", runtime),
3229                                 SD_BUS_RESULT("as", links),
3230                                 method_get_unit_file_links,
3231                                 SD_BUS_VTABLE_UNPRIVILEGED),
3232         SD_BUS_METHOD_WITH_ARGS("SetExitCode",
3233                                 SD_BUS_ARGS("y", number),
3234                                 SD_BUS_NO_RESULT,
3235                                 method_set_exit_code,
3236                                 SD_BUS_VTABLE_UNPRIVILEGED),
3237         SD_BUS_METHOD_WITH_ARGS("LookupDynamicUserByName",
3238                                 SD_BUS_ARGS("s", name),
3239                                 SD_BUS_RESULT("u", uid),
3240                                 method_lookup_dynamic_user_by_name,
3241                                 SD_BUS_VTABLE_UNPRIVILEGED),
3242         SD_BUS_METHOD_WITH_ARGS("LookupDynamicUserByUID",
3243                                 SD_BUS_ARGS("u", uid),
3244                                 SD_BUS_RESULT("s", name),
3245                                 method_lookup_dynamic_user_by_uid,
3246                                 SD_BUS_VTABLE_UNPRIVILEGED),
3247         SD_BUS_METHOD_WITH_ARGS("GetDynamicUsers",
3248                                 SD_BUS_NO_ARGS,
3249                                 SD_BUS_RESULT("a(us)", users),
3250                                 method_get_dynamic_users,
3251                                 SD_BUS_VTABLE_UNPRIVILEGED),
3252 
3253         SD_BUS_SIGNAL_WITH_ARGS("UnitNew",
3254                                 SD_BUS_ARGS("s", id, "o", unit),
3255                                 0),
3256         SD_BUS_SIGNAL_WITH_ARGS("UnitRemoved",
3257                                 SD_BUS_ARGS("s", id, "o", unit),
3258                                 0),
3259         SD_BUS_SIGNAL_WITH_ARGS("JobNew",
3260                                 SD_BUS_ARGS("u", id, "o", job, "s", unit),
3261                                 0),
3262         SD_BUS_SIGNAL_WITH_ARGS("JobRemoved",
3263                                 SD_BUS_ARGS("u", id, "o", job, "s", unit, "s", result),
3264                                 0),
3265         SD_BUS_SIGNAL_WITH_ARGS("StartupFinished",
3266                                 SD_BUS_ARGS("t", firmware, "t", loader, "t", kernel, "t", initrd, "t", userspace, "t", total),
3267                                 0),
3268         SD_BUS_SIGNAL("UnitFilesChanged", NULL, 0),
3269         SD_BUS_SIGNAL_WITH_ARGS("Reloading",
3270                                 SD_BUS_ARGS("b", active),
3271                                 0),
3272 
3273         SD_BUS_VTABLE_END
3274 };
3275 
3276 const sd_bus_vtable bus_manager_log_control_vtable[] = {
3277         SD_BUS_VTABLE_START(0),
3278 
3279         /* We define a private version of this interface here, since we want slightly different
3280          * implementations for the setters. We'll still use the generic getters however, and we share the
3281          * setters with the implementations for the Manager interface above (which pre-dates the generic
3282          * service API interface). */
3283 
3284         SD_BUS_WRITABLE_PROPERTY("LogLevel", "s", bus_property_get_log_level, property_set_log_level, 0, 0),
3285         SD_BUS_WRITABLE_PROPERTY("LogTarget", "s", bus_property_get_log_target, property_set_log_target, 0, 0),
3286         SD_BUS_PROPERTY("SyslogIdentifier", "s", bus_property_get_syslog_identifier, 0, 0),
3287 
3288         SD_BUS_VTABLE_END,
3289 };
3290 
send_finished(sd_bus * bus,void * userdata)3291 static int send_finished(sd_bus *bus, void *userdata) {
3292         _cleanup_(sd_bus_message_unrefp) sd_bus_message *message = NULL;
3293         usec_t *times = userdata;
3294         int r;
3295 
3296         assert(bus);
3297         assert(times);
3298 
3299         r = sd_bus_message_new_signal(bus,
3300                                       &message,
3301                                       "/org/freedesktop/systemd1",
3302                                       "org.freedesktop.systemd1.Manager",
3303                                       "StartupFinished");
3304         if (r < 0)
3305                 return r;
3306 
3307         r = sd_bus_message_append(message, "tttttt", times[0], times[1], times[2], times[3], times[4], times[5]);
3308         if (r < 0)
3309                 return r;
3310 
3311         return sd_bus_send(bus, message, NULL);
3312 }
3313 
bus_manager_send_finished(Manager * m,usec_t firmware_usec,usec_t loader_usec,usec_t kernel_usec,usec_t initrd_usec,usec_t userspace_usec,usec_t total_usec)3314 void bus_manager_send_finished(
3315                 Manager *m,
3316                 usec_t firmware_usec,
3317                 usec_t loader_usec,
3318                 usec_t kernel_usec,
3319                 usec_t initrd_usec,
3320                 usec_t userspace_usec,
3321                 usec_t total_usec) {
3322 
3323         int r;
3324 
3325         assert(m);
3326 
3327         r = bus_foreach_bus(
3328                         m,
3329                         NULL,
3330                         send_finished,
3331                         (usec_t[6]) {
3332                                 firmware_usec,
3333                                 loader_usec,
3334                                 kernel_usec,
3335                                 initrd_usec,
3336                                 userspace_usec,
3337                                 total_usec
3338                         });
3339         if (r < 0)
3340                 log_debug_errno(r, "Failed to send finished signal: %m");
3341 }
3342 
send_reloading(sd_bus * bus,void * userdata)3343 static int send_reloading(sd_bus *bus, void *userdata) {
3344         _cleanup_(sd_bus_message_unrefp) sd_bus_message *message = NULL;
3345         int r;
3346 
3347         assert(bus);
3348 
3349         r = sd_bus_message_new_signal(bus, &message, "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "Reloading");
3350         if (r < 0)
3351                 return r;
3352 
3353         r = sd_bus_message_append(message, "b", PTR_TO_INT(userdata));
3354         if (r < 0)
3355                 return r;
3356 
3357         return sd_bus_send(bus, message, NULL);
3358 }
3359 
bus_manager_send_reloading(Manager * m,bool active)3360 void bus_manager_send_reloading(Manager *m, bool active) {
3361         int r;
3362 
3363         assert(m);
3364 
3365         r = bus_foreach_bus(m, NULL, send_reloading, INT_TO_PTR(active));
3366         if (r < 0)
3367                 log_debug_errno(r, "Failed to send reloading signal: %m");
3368 }
3369 
send_changed_signal(sd_bus * bus,void * userdata)3370 static int send_changed_signal(sd_bus *bus, void *userdata) {
3371         assert(bus);
3372 
3373         return sd_bus_emit_properties_changed_strv(bus,
3374                                                    "/org/freedesktop/systemd1",
3375                                                    "org.freedesktop.systemd1.Manager",
3376                                                    NULL);
3377 }
3378 
bus_manager_send_change_signal(Manager * m)3379 void bus_manager_send_change_signal(Manager *m) {
3380         int r;
3381 
3382         assert(m);
3383 
3384         r = bus_foreach_bus(m, NULL, send_changed_signal, NULL);
3385         if (r < 0)
3386                 log_debug_errno(r, "Failed to send manager change signal: %m");
3387 }
3388