]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/dbus-manager.c
virt: detect LXC+libvirt containers
[thirdparty/systemd.git] / src / dbus-manager.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <unistd.h>
24
25 #include "dbus.h"
26 #include "log.h"
27 #include "dbus-manager.h"
28 #include "strv.h"
29 #include "bus-errors.h"
30 #include "build.h"
31 #include "dbus-common.h"
32 #include "install.h"
33
34 #define BUS_MANAGER_INTERFACE_BEGIN \
35 " <interface name=\"org.freedesktop.systemd1.Manager\">\n"
36
37 #define BUS_MANAGER_INTERFACE_METHODS \
38 " <method name=\"GetUnit\">\n" \
39 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
40 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
41 " </method>\n" \
42 " <method name=\"GetUnitByPID\">\n" \
43 " <arg name=\"pid\" type=\"u\" direction=\"in\"/>\n" \
44 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
45 " </method>\n" \
46 " <method name=\"LoadUnit\">\n" \
47 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
48 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
49 " </method>\n" \
50 " <method name=\"StartUnit\">\n" \
51 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
52 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
53 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
54 " </method>\n" \
55 " <method name=\"StartUnitReplace\">\n" \
56 " <arg name=\"old_unit\" type=\"s\" direction=\"in\"/>\n" \
57 " <arg name=\"new_unit\" type=\"s\" direction=\"in\"/>\n" \
58 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
59 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
60 " </method>\n" \
61 " <method name=\"StopUnit\">\n" \
62 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
63 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
64 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
65 " </method>\n" \
66 " <method name=\"ReloadUnit\">\n" \
67 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
68 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
69 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
70 " </method>\n" \
71 " <method name=\"RestartUnit\">\n" \
72 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
73 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
74 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
75 " </method>\n" \
76 " <method name=\"TryRestartUnit\">\n" \
77 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
78 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
79 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
80 " </method>\n" \
81 " <method name=\"ReloadOrRestartUnit\">\n" \
82 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
83 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
84 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
85 " </method>\n" \
86 " <method name=\"ReloadOrTryRestartUnit\">\n" \
87 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
88 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
89 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
90 " </method>\n" \
91 " <method name=\"KillUnit\">\n" \
92 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
93 " <arg name=\"who\" type=\"s\" direction=\"in\"/>\n" \
94 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
95 " <arg name=\"signal\" type=\"i\" direction=\"in\"/>\n" \
96 " </method>\n" \
97 " <method name=\"ResetFailedUnit\">\n" \
98 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
99 " </method>\n" \
100 " <method name=\"GetJob\">\n" \
101 " <arg name=\"id\" type=\"u\" direction=\"in\"/>\n" \
102 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
103 " </method>\n" \
104 " <method name=\"ClearJobs\"/>\n" \
105 " <method name=\"ResetFailed\"/>\n" \
106 " <method name=\"ListUnits\">\n" \
107 " <arg name=\"units\" type=\"a(ssssssouso)\" direction=\"out\"/>\n" \
108 " </method>\n" \
109 " <method name=\"ListJobs\">\n" \
110 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \
111 " </method>\n" \
112 " <method name=\"Subscribe\"/>\n" \
113 " <method name=\"Unsubscribe\"/>\n" \
114 " <method name=\"Dump\"/>\n" \
115 " <method name=\"CreateSnapshot\">\n" \
116 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
117 " <arg name=\"cleanup\" type=\"b\" direction=\"in\"/>\n" \
118 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
119 " </method>\n" \
120 " <method name=\"Reload\"/>\n" \
121 " <method name=\"Reexecute\"/>\n" \
122 " <method name=\"Exit\"/>\n" \
123 " <method name=\"Reboot\"/>\n" \
124 " <method name=\"PowerOff\"/>\n" \
125 " <method name=\"Halt\"/>\n" \
126 " <method name=\"KExec\"/>\n" \
127 " <method name=\"SetEnvironment\">\n" \
128 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
129 " </method>\n" \
130 " <method name=\"UnsetEnvironment\">\n" \
131 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
132 " </method>\n" \
133 " <method name=\"UnsetAndSetEnvironment\">\n" \
134 " <arg name=\"unset\" type=\"as\" direction=\"in\"/>\n" \
135 " <arg name=\"set\" type=\"as\" direction=\"in\"/>\n" \
136 " </method>\n" \
137 " <method name=\"ListUnitFiles\">\n" \
138 " <arg name=\"changes\" type=\"a(ss)\" direction=\"out\"/>\n" \
139 " </method>\n" \
140 " <method name=\"GetUnitFileState\">\n" \
141 " <arg name=\"file\" type=\"s\" direction=\"in\"/>\n" \
142 " <arg name=\"state\" type=\"s\" direction=\"out\"/>\n" \
143 " </method>\n" \
144 " <method name=\"EnableUnitFiles\">\n" \
145 " <arg name=\"files\" type=\"as\" direction=\"in\"/>\n" \
146 " <arg name=\"runtime\" type=\"b\" direction=\"in\"/>\n" \
147 " <arg name=\"force\" type=\"b\" direction=\"in\"/>\n" \
148 " <arg name=\"carries_install_info\" type=\"b\" direction=\"out\"/>\n" \
149 " <arg name=\"changes\" type=\"a(sss)\" direction=\"out\"/>\n" \
150 " </method>\n" \
151 " <method name=\"DisableUnitFiles\">\n" \
152 " <arg name=\"files\" type=\"as\" direction=\"in\"/>\n" \
153 " <arg name=\"runtime\" type=\"b\" direction=\"in\"/>\n" \
154 " <arg name=\"changes\" type=\"a(sss)\" direction=\"out\"/>\n" \
155 " </method>\n" \
156 " <method name=\"ReenableUnitFiles\">\n" \
157 " <arg name=\"files\" type=\"as\" direction=\"in\"/>\n" \
158 " <arg name=\"runtime\" type=\"b\" direction=\"in\"/>\n" \
159 " <arg name=\"force\" type=\"b\" direction=\"in\"/>\n" \
160 " <arg name=\"carries_install_info\" type=\"b\" direction=\"out\"/>\n" \
161 " <arg name=\"changes\" type=\"a(sss)\" direction=\"out\"/>\n" \
162 " </method>\n" \
163 " <method name=\"LinkUnitFiles\">\n" \
164 " <arg name=\"files\" type=\"as\" direction=\"in\"/>\n" \
165 " <arg name=\"runtime\" type=\"b\" direction=\"in\"/>\n" \
166 " <arg name=\"force\" type=\"b\" direction=\"in\"/>\n" \
167 " <arg name=\"changes\" type=\"a(sss)\" direction=\"out\"/>\n" \
168 " </method>\n" \
169 " <method name=\"PresetUnitFiles\">\n" \
170 " <arg name=\"files\" type=\"as\" direction=\"in\"/>\n" \
171 " <arg name=\"runtime\" type=\"b\" direction=\"in\"/>\n" \
172 " <arg name=\"force\" type=\"b\" direction=\"in\"/>\n" \
173 " <arg name=\"carries_install_info\" type=\"b\" direction=\"out\"/>\n" \
174 " <arg name=\"changes\" type=\"a(sss)\" direction=\"out\"/>\n" \
175 " </method>\n" \
176 " <method name=\"MaskUnitFiles\">\n" \
177 " <arg name=\"files\" type=\"as\" direction=\"in\"/>\n" \
178 " <arg name=\"runtime\" type=\"b\" direction=\"in\"/>\n" \
179 " <arg name=\"force\" type=\"b\" direction=\"in\"/>\n" \
180 " <arg name=\"changes\" type=\"a(sss)\" direction=\"out\"/>\n" \
181 " </method>\n" \
182 " <method name=\"UnmaskUnitFiles\">\n" \
183 " <arg name=\"files\" type=\"as\" direction=\"in\"/>\n" \
184 " <arg name=\"runtime\" type=\"b\" direction=\"in\"/>\n" \
185 " <arg name=\"changes\" type=\"a(sss)\" direction=\"out\"/>\n" \
186 " </method>\n"
187
188 #define BUS_MANAGER_INTERFACE_SIGNALS \
189 " <signal name=\"UnitNew\">\n" \
190 " <arg name=\"id\" type=\"s\"/>\n" \
191 " <arg name=\"unit\" type=\"o\"/>\n" \
192 " </signal>\n" \
193 " <signal name=\"UnitRemoved\">\n" \
194 " <arg name=\"id\" type=\"s\"/>\n" \
195 " <arg name=\"unit\" type=\"o\"/>\n" \
196 " </signal>\n" \
197 " <signal name=\"JobNew\">\n" \
198 " <arg name=\"id\" type=\"u\"/>\n" \
199 " <arg name=\"job\" type=\"o\"/>\n" \
200 " </signal>\n" \
201 " <signal name=\"JobRemoved\">\n" \
202 " <arg name=\"id\" type=\"u\"/>\n" \
203 " <arg name=\"job\" type=\"o\"/>\n" \
204 " <arg name=\"result\" type=\"s\"/>\n" \
205 " </signal>" \
206 " <signal name=\"StartupFinished\">\n" \
207 " <arg name=\"kernel\" type=\"t\"/>\n" \
208 " <arg name=\"initrd\" type=\"t\"/>\n" \
209 " <arg name=\"userspace\" type=\"t\"/>\n" \
210 " <arg name=\"total\" type=\"t\"/>\n" \
211 " </signal>" \
212 " <signal name=\"UnitFilesChanged\"/>\n"
213
214 #define BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
215 " <property name=\"Version\" type=\"s\" access=\"read\"/>\n" \
216 " <property name=\"Distribution\" type=\"s\" access=\"read\"/>\n" \
217 " <property name=\"Features\" type=\"s\" access=\"read\"/>\n" \
218 " <property name=\"Tainted\" type=\"s\" access=\"read\"/>\n" \
219 " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \
220 " <property name=\"InitRDTimestamp\" type=\"t\" access=\"read\"/>\n" \
221 " <property name=\"InitRDTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
222 " <property name=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \
223 " <property name=\"StartupTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
224 " <property name=\"FinishTimestamp\" type=\"t\" access=\"read\"/>\n" \
225 " <property name=\"FinishTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
226 " <property name=\"LogLevel\" type=\"s\" access=\"readwrite\"/>\n" \
227 " <property name=\"LogTarget\" type=\"s\" access=\"readwrite\"/>\n" \
228 " <property name=\"NNames\" type=\"u\" access=\"read\"/>\n" \
229 " <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n" \
230 " <property name=\"NInstalledJobs\" type=\"u\" access=\"read\"/>\n" \
231 " <property name=\"NFailedJobs\" type=\"u\" access=\"read\"/>\n" \
232 " <property name=\"Progress\" type=\"d\" access=\"read\"/>\n" \
233 " <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
234 " <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \
235 " <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \
236 " <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
237 " <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
238 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
239 " <property name=\"MountAuto\" type=\"b\" access=\"read\"/>\n" \
240 " <property name=\"SwapAuto\" type=\"b\" access=\"read\"/>\n" \
241 " <property name=\"DefaultControllers\" type=\"as\" access=\"read\"/>\n" \
242 " <property name=\"DefaultStandardOutput\" type=\"s\" access=\"read\"/>\n" \
243 " <property name=\"DefaultStandardError\" type=\"s\" access=\"read\"/>\n"
244
245 #ifdef HAVE_SYSV_COMPAT
246 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
247 " <property name=\"SysVConsole\" type=\"b\" access=\"read\"/>\n" \
248 " <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
249 " <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n"
250 #else
251 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV
252 #endif
253
254 #define BUS_MANAGER_INTERFACE_END \
255 " </interface>\n"
256
257 #define BUS_MANAGER_INTERFACE \
258 BUS_MANAGER_INTERFACE_BEGIN \
259 BUS_MANAGER_INTERFACE_METHODS \
260 BUS_MANAGER_INTERFACE_SIGNALS \
261 BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
262 BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
263 BUS_MANAGER_INTERFACE_END
264
265 #define INTROSPECTION_BEGIN \
266 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
267 "<node>\n" \
268 BUS_MANAGER_INTERFACE \
269 BUS_PROPERTIES_INTERFACE \
270 BUS_PEER_INTERFACE \
271 BUS_INTROSPECTABLE_INTERFACE
272
273 #define INTROSPECTION_END \
274 "</node>\n"
275
276 #define INTERFACES_LIST \
277 BUS_GENERIC_INTERFACES_LIST \
278 "org.freedesktop.systemd1.Manager\0"
279
280 const char bus_manager_interface[] _introspect_("Manager") = BUS_MANAGER_INTERFACE;
281
282 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
283 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_exec_output, exec_output, ExecOutput);
284
285 static int bus_manager_append_tainted(DBusMessageIter *i, const char *property, void *data) {
286 const char *t;
287 Manager *m = data;
288 char buf[LINE_MAX] = "", *e = buf, *p = NULL;
289
290 assert(i);
291 assert(property);
292 assert(m);
293
294 if (m->taint_usr)
295 e = stpcpy(e, "usr-separate-fs ");
296
297 if (readlink_malloc("/etc/mtab", &p) < 0)
298 e = stpcpy(e, "etc-mtab-not-symlink ");
299 else
300 free(p);
301
302 if (access("/proc/cgroups", F_OK) < 0)
303 stpcpy(e, "cgroups-missing ");
304
305 t = strstrip(buf);
306
307 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
308 return -ENOMEM;
309
310 return 0;
311 }
312
313 static int bus_manager_append_log_target(DBusMessageIter *i, const char *property, void *data) {
314 const char *t;
315
316 assert(i);
317 assert(property);
318
319 t = log_target_to_string(log_get_target());
320
321 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
322 return -ENOMEM;
323
324 return 0;
325 }
326
327 static int bus_manager_set_log_target(DBusMessageIter *i, const char *property) {
328 const char *t;
329
330 assert(i);
331 assert(property);
332
333 dbus_message_iter_get_basic(i, &t);
334
335 return log_set_target_from_string(t);
336 }
337
338 static int bus_manager_append_log_level(DBusMessageIter *i, const char *property, void *data) {
339 const char *t;
340
341 assert(i);
342 assert(property);
343
344 t = log_level_to_string(log_get_max_level());
345
346 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
347 return -ENOMEM;
348
349 return 0;
350 }
351
352 static int bus_manager_set_log_level(DBusMessageIter *i, const char *property) {
353 const char *t;
354
355 assert(i);
356 assert(property);
357
358 dbus_message_iter_get_basic(i, &t);
359
360 return log_set_max_level_from_string(t);
361 }
362
363 static int bus_manager_append_n_names(DBusMessageIter *i, const char *property, void *data) {
364 Manager *m = data;
365 uint32_t u;
366
367 assert(i);
368 assert(property);
369 assert(m);
370
371 u = hashmap_size(m->units);
372
373 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
374 return -ENOMEM;
375
376 return 0;
377 }
378
379 static int bus_manager_append_n_jobs(DBusMessageIter *i, const char *property, void *data) {
380 Manager *m = data;
381 uint32_t u;
382
383 assert(i);
384 assert(property);
385 assert(m);
386
387 u = hashmap_size(m->jobs);
388
389 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
390 return -ENOMEM;
391
392 return 0;
393 }
394
395 static int bus_manager_append_progress(DBusMessageIter *i, const char *property, void *data) {
396 double d;
397 Manager *m = data;
398
399 assert(i);
400 assert(property);
401 assert(m);
402
403 if (dual_timestamp_is_set(&m->finish_timestamp))
404 d = 1.0;
405 else
406 d = 1.0 - ((double) hashmap_size(m->jobs) / (double) m->n_installed_jobs);
407
408 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_DOUBLE, &d))
409 return -ENOMEM;
410
411 return 0;
412 }
413
414 static const char *message_get_sender_with_fallback(DBusMessage *m) {
415 const char *s;
416
417 assert(m);
418
419 if ((s = dbus_message_get_sender(m)))
420 return s;
421
422 /* When the message came in from a direct connection the
423 * message will have no sender. We fix that here. */
424
425 return ":no-sender";
426 }
427
428 static DBusMessage *message_from_file_changes(
429 DBusMessage *m,
430 UnitFileChange *changes,
431 unsigned n_changes,
432 int carries_install_info) {
433
434 DBusMessageIter iter, sub, sub2;
435 DBusMessage *reply;
436 unsigned i;
437
438 reply = dbus_message_new_method_return(m);
439 if (!reply)
440 return NULL;
441
442 dbus_message_iter_init_append(reply, &iter);
443
444 if (carries_install_info >= 0) {
445 dbus_bool_t b;
446
447 b = !!carries_install_info;
448 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &b))
449 goto oom;
450 }
451
452 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(sss)", &sub))
453 goto oom;
454
455 for (i = 0; i < n_changes; i++) {
456 const char *type, *path, *source;
457
458 type = unit_file_change_type_to_string(changes[i].type);
459 path = strempty(changes[i].path);
460 source = strempty(changes[i].source);
461
462 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) ||
463 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
464 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &path) ||
465 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &source) ||
466 !dbus_message_iter_close_container(&sub, &sub2))
467 goto oom;
468 }
469
470 if (!dbus_message_iter_close_container(&iter, &sub))
471 goto oom;
472
473 return reply;
474
475 oom:
476 dbus_message_unref(reply);
477 return NULL;
478 }
479
480 static int bus_manager_send_unit_files_changed(Manager *m) {
481 DBusMessage *s;
482 int r;
483
484 s = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "UnitFilesChanged");
485 if (!s)
486 return -ENOMEM;
487
488 r = bus_broadcast(m, s);
489 dbus_message_unref(s);
490
491 return r;
492 }
493
494 static const char systemd_property_string[] = PACKAGE_STRING "\0" DISTRIBUTION "\0" SYSTEMD_FEATURES;
495
496 static const BusProperty bus_systemd_properties[] = {
497 { "Version", bus_property_append_string, "s", 0 },
498 { "Distribution", bus_property_append_string, "s", sizeof(PACKAGE_STRING) },
499 { "Features", bus_property_append_string, "s", sizeof(PACKAGE_STRING) + sizeof(DISTRIBUTION) },
500 { NULL, }
501 };
502
503 static const BusProperty bus_manager_properties[] = {
504 { "RunningAs", bus_manager_append_running_as, "s", offsetof(Manager, running_as) },
505 { "Tainted", bus_manager_append_tainted, "s", 0 },
506 { "InitRDTimestamp", bus_property_append_uint64, "t", offsetof(Manager, initrd_timestamp.realtime) },
507 { "InitRDTimestampMonotonic", bus_property_append_uint64, "t", offsetof(Manager, initrd_timestamp.monotonic) },
508 { "StartupTimestamp", bus_property_append_uint64, "t", offsetof(Manager, startup_timestamp.realtime) },
509 { "StartupTimestampMonotonic", bus_property_append_uint64, "t", offsetof(Manager, startup_timestamp.monotonic) },
510 { "FinishTimestamp", bus_property_append_uint64, "t", offsetof(Manager, finish_timestamp.realtime) },
511 { "FinishTimestampMonotonic", bus_property_append_uint64, "t", offsetof(Manager, finish_timestamp.monotonic) },
512 { "LogLevel", bus_manager_append_log_level, "s", 0, 0, bus_manager_set_log_level },
513 { "LogTarget", bus_manager_append_log_target, "s", 0, 0, bus_manager_set_log_target },
514 { "NNames", bus_manager_append_n_names, "u", 0 },
515 { "NJobs", bus_manager_append_n_jobs, "u", 0 },
516 { "NInstalledJobs",bus_property_append_uint32, "u", offsetof(Manager, n_installed_jobs) },
517 { "NFailedJobs", bus_property_append_uint32, "u", offsetof(Manager, n_failed_jobs) },
518 { "Progress", bus_manager_append_progress, "d", 0 },
519 { "Environment", bus_property_append_strv, "as", offsetof(Manager, environment), true },
520 { "ConfirmSpawn", bus_property_append_bool, "b", offsetof(Manager, confirm_spawn) },
521 { "ShowStatus", bus_property_append_bool, "b", offsetof(Manager, show_status) },
522 { "UnitPath", bus_property_append_strv, "as", offsetof(Manager, lookup_paths.unit_path), true },
523 { "NotifySocket", bus_property_append_string, "s", offsetof(Manager, notify_socket), true },
524 { "ControlGroupHierarchy", bus_property_append_string, "s", offsetof(Manager, cgroup_hierarchy), true },
525 { "MountAuto", bus_property_append_bool, "b", offsetof(Manager, mount_auto) },
526 { "SwapAuto", bus_property_append_bool, "b", offsetof(Manager, swap_auto) },
527 { "DefaultControllers", bus_property_append_strv, "as", offsetof(Manager, default_controllers), true },
528 { "DefaultStandardOutput", bus_manager_append_exec_output, "s", offsetof(Manager, default_std_output) },
529 { "DefaultStandardError", bus_manager_append_exec_output, "s", offsetof(Manager, default_std_error) },
530 #ifdef HAVE_SYSV_COMPAT
531 { "SysVConsole", bus_property_append_bool, "b", offsetof(Manager, sysv_console) },
532 { "SysVInitPath", bus_property_append_strv, "as", offsetof(Manager, lookup_paths.sysvinit_path), true },
533 { "SysVRcndPath", bus_property_append_strv, "as", offsetof(Manager, lookup_paths.sysvrcnd_path), true },
534 #endif
535 { NULL, }
536 };
537
538 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
539 Manager *m = data;
540
541 int r;
542 DBusError error;
543 DBusMessage *reply = NULL;
544 char * path = NULL;
545 JobType job_type = _JOB_TYPE_INVALID;
546 bool reload_if_possible = false;
547 const char *member;
548
549 assert(connection);
550 assert(message);
551 assert(m);
552
553 dbus_error_init(&error);
554
555 member = dbus_message_get_member(message);
556
557 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
558 const char *name;
559 Unit *u;
560
561 if (!dbus_message_get_args(
562 message,
563 &error,
564 DBUS_TYPE_STRING, &name,
565 DBUS_TYPE_INVALID))
566 return bus_send_error_reply(connection, message, &error, -EINVAL);
567
568 if (!(u = manager_get_unit(m, name))) {
569 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
570 return bus_send_error_reply(connection, message, &error, -ENOENT);
571 }
572
573 if (!(reply = dbus_message_new_method_return(message)))
574 goto oom;
575
576 if (!(path = unit_dbus_path(u)))
577 goto oom;
578
579 if (!dbus_message_append_args(
580 reply,
581 DBUS_TYPE_OBJECT_PATH, &path,
582 DBUS_TYPE_INVALID))
583 goto oom;
584 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) {
585 Unit *u;
586 uint32_t pid;
587
588 if (!dbus_message_get_args(
589 message,
590 &error,
591 DBUS_TYPE_UINT32, &pid,
592 DBUS_TYPE_INVALID))
593 return bus_send_error_reply(connection, message, &error, -EINVAL);
594
595 if (!(u = cgroup_unit_by_pid(m, (pid_t) pid))) {
596 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "No unit for PID %lu is loaded.", (unsigned long) pid);
597 return bus_send_error_reply(connection, message, &error, -ENOENT);
598 }
599
600 if (!(reply = dbus_message_new_method_return(message)))
601 goto oom;
602
603 if (!(path = unit_dbus_path(u)))
604 goto oom;
605
606 if (!dbus_message_append_args(
607 reply,
608 DBUS_TYPE_OBJECT_PATH, &path,
609 DBUS_TYPE_INVALID))
610 goto oom;
611 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
612 const char *name;
613 Unit *u;
614
615 if (!dbus_message_get_args(
616 message,
617 &error,
618 DBUS_TYPE_STRING, &name,
619 DBUS_TYPE_INVALID))
620 return bus_send_error_reply(connection, message, &error, -EINVAL);
621
622 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
623 return bus_send_error_reply(connection, message, &error, r);
624
625 if (!(reply = dbus_message_new_method_return(message)))
626 goto oom;
627
628 if (!(path = unit_dbus_path(u)))
629 goto oom;
630
631 if (!dbus_message_append_args(
632 reply,
633 DBUS_TYPE_OBJECT_PATH, &path,
634 DBUS_TYPE_INVALID))
635 goto oom;
636
637 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
638 job_type = JOB_START;
639 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
640 job_type = JOB_START;
641 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
642 job_type = JOB_STOP;
643 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
644 job_type = JOB_RELOAD;
645 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
646 job_type = JOB_RESTART;
647 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
648 job_type = JOB_TRY_RESTART;
649 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) {
650 reload_if_possible = true;
651 job_type = JOB_RESTART;
652 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) {
653 reload_if_possible = true;
654 job_type = JOB_TRY_RESTART;
655 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KillUnit")) {
656 const char *name, *swho, *smode;
657 int32_t signo;
658 Unit *u;
659 KillMode mode;
660 KillWho who;
661
662 if (!dbus_message_get_args(
663 message,
664 &error,
665 DBUS_TYPE_STRING, &name,
666 DBUS_TYPE_STRING, &swho,
667 DBUS_TYPE_STRING, &smode,
668 DBUS_TYPE_INT32, &signo,
669 DBUS_TYPE_INVALID))
670 return bus_send_error_reply(connection, message, &error, -EINVAL);
671
672 if (isempty(swho))
673 who = KILL_ALL;
674 else {
675 who = kill_who_from_string(swho);
676 if (who < 0)
677 return bus_send_error_reply(connection, message, &error, -EINVAL);
678 }
679
680 if (isempty(smode))
681 mode = KILL_CONTROL_GROUP;
682 else {
683 mode = kill_mode_from_string(smode);
684 if (mode < 0)
685 return bus_send_error_reply(connection, message, &error, -EINVAL);
686 }
687
688 if (signo <= 0 || signo >= _NSIG)
689 return bus_send_error_reply(connection, message, &error, -EINVAL);
690
691 if (!(u = manager_get_unit(m, name))) {
692 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
693 return bus_send_error_reply(connection, message, &error, -ENOENT);
694 }
695
696 if ((r = unit_kill(u, who, mode, signo, &error)) < 0)
697 return bus_send_error_reply(connection, message, &error, r);
698
699 if (!(reply = dbus_message_new_method_return(message)))
700 goto oom;
701
702 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
703 uint32_t id;
704 Job *j;
705
706 if (!dbus_message_get_args(
707 message,
708 &error,
709 DBUS_TYPE_UINT32, &id,
710 DBUS_TYPE_INVALID))
711 return bus_send_error_reply(connection, message, &error, -EINVAL);
712
713 if (!(j = manager_get_job(m, id))) {
714 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
715 return bus_send_error_reply(connection, message, &error, -ENOENT);
716 }
717
718 if (!(reply = dbus_message_new_method_return(message)))
719 goto oom;
720
721 if (!(path = job_dbus_path(j)))
722 goto oom;
723
724 if (!dbus_message_append_args(
725 reply,
726 DBUS_TYPE_OBJECT_PATH, &path,
727 DBUS_TYPE_INVALID))
728 goto oom;
729
730 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
731
732 manager_clear_jobs(m);
733
734 if (!(reply = dbus_message_new_method_return(message)))
735 goto oom;
736
737 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailed")) {
738
739 manager_reset_failed(m);
740
741 if (!(reply = dbus_message_new_method_return(message)))
742 goto oom;
743
744 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailedUnit")) {
745 const char *name;
746 Unit *u;
747
748 if (!dbus_message_get_args(
749 message,
750 &error,
751 DBUS_TYPE_STRING, &name,
752 DBUS_TYPE_INVALID))
753 return bus_send_error_reply(connection, message, &error, -EINVAL);
754
755 if (!(u = manager_get_unit(m, name))) {
756 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
757 return bus_send_error_reply(connection, message, &error, -ENOENT);
758 }
759
760 unit_reset_failed(u);
761
762 if (!(reply = dbus_message_new_method_return(message)))
763 goto oom;
764
765 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
766 DBusMessageIter iter, sub;
767 Iterator i;
768 Unit *u;
769 const char *k;
770
771 if (!(reply = dbus_message_new_method_return(message)))
772 goto oom;
773
774 dbus_message_iter_init_append(reply, &iter);
775
776 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub))
777 goto oom;
778
779 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
780 char *u_path, *j_path;
781 const char *description, *load_state, *active_state, *sub_state, *sjob_type, *following;
782 DBusMessageIter sub2;
783 uint32_t job_id;
784 Unit *f;
785
786 if (k != u->id)
787 continue;
788
789 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
790 goto oom;
791
792 description = unit_description(u);
793 load_state = unit_load_state_to_string(u->load_state);
794 active_state = unit_active_state_to_string(unit_active_state(u));
795 sub_state = unit_sub_state_to_string(u);
796
797 f = unit_following(u);
798 following = f ? f->id : "";
799
800 if (!(u_path = unit_dbus_path(u)))
801 goto oom;
802
803 if (u->job) {
804 job_id = (uint32_t) u->job->id;
805
806 if (!(j_path = job_dbus_path(u->job))) {
807 free(u_path);
808 goto oom;
809 }
810
811 sjob_type = job_type_to_string(u->job->type);
812 } else {
813 job_id = 0;
814 j_path = u_path;
815 sjob_type = "";
816 }
817
818 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->id) ||
819 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
820 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
821 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
822 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
823 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &following) ||
824 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
825 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
826 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
827 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
828 free(u_path);
829 if (u->job)
830 free(j_path);
831 goto oom;
832 }
833
834 free(u_path);
835 if (u->job)
836 free(j_path);
837
838 if (!dbus_message_iter_close_container(&sub, &sub2))
839 goto oom;
840 }
841
842 if (!dbus_message_iter_close_container(&iter, &sub))
843 goto oom;
844
845 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
846 DBusMessageIter iter, sub;
847 Iterator i;
848 Job *j;
849
850 if (!(reply = dbus_message_new_method_return(message)))
851 goto oom;
852
853 dbus_message_iter_init_append(reply, &iter);
854
855 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
856 goto oom;
857
858 HASHMAP_FOREACH(j, m->jobs, i) {
859 char *u_path, *j_path;
860 const char *state, *type;
861 uint32_t id;
862 DBusMessageIter sub2;
863
864 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
865 goto oom;
866
867 id = (uint32_t) j->id;
868 state = job_state_to_string(j->state);
869 type = job_type_to_string(j->type);
870
871 if (!(j_path = job_dbus_path(j)))
872 goto oom;
873
874 if (!(u_path = unit_dbus_path(j->unit))) {
875 free(j_path);
876 goto oom;
877 }
878
879 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
880 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->id) ||
881 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
882 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
883 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
884 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
885 free(j_path);
886 free(u_path);
887 goto oom;
888 }
889
890 free(j_path);
891 free(u_path);
892
893 if (!dbus_message_iter_close_container(&sub, &sub2))
894 goto oom;
895 }
896
897 if (!dbus_message_iter_close_container(&iter, &sub))
898 goto oom;
899
900 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
901 char *client;
902 Set *s;
903
904 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
905 if (!(s = set_new(string_hash_func, string_compare_func)))
906 goto oom;
907
908 if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
909 set_free(s);
910 goto oom;
911 }
912 }
913
914 if (!(client = strdup(message_get_sender_with_fallback(message))))
915 goto oom;
916
917 if ((r = set_put(s, client)) < 0) {
918 free(client);
919 return bus_send_error_reply(connection, message, NULL, r);
920 }
921
922 if (!(reply = dbus_message_new_method_return(message)))
923 goto oom;
924
925 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
926 char *client;
927
928 if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) {
929 dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
930 return bus_send_error_reply(connection, message, &error, -ENOENT);
931 }
932
933 free(client);
934
935 if (!(reply = dbus_message_new_method_return(message)))
936 goto oom;
937
938 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
939 FILE *f;
940 char *dump = NULL;
941 size_t size;
942
943 if (!(reply = dbus_message_new_method_return(message)))
944 goto oom;
945
946 if (!(f = open_memstream(&dump, &size)))
947 goto oom;
948
949 manager_dump_units(m, f, NULL);
950 manager_dump_jobs(m, f, NULL);
951
952 if (ferror(f)) {
953 fclose(f);
954 free(dump);
955 goto oom;
956 }
957
958 fclose(f);
959
960 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
961 free(dump);
962 goto oom;
963 }
964
965 free(dump);
966 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
967 const char *name;
968 dbus_bool_t cleanup;
969 Snapshot *s;
970
971 if (!dbus_message_get_args(
972 message,
973 &error,
974 DBUS_TYPE_STRING, &name,
975 DBUS_TYPE_BOOLEAN, &cleanup,
976 DBUS_TYPE_INVALID))
977 return bus_send_error_reply(connection, message, &error, -EINVAL);
978
979 if (name && name[0] == 0)
980 name = NULL;
981
982 if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0)
983 return bus_send_error_reply(connection, message, &error, r);
984
985 if (!(reply = dbus_message_new_method_return(message)))
986 goto oom;
987
988 if (!(path = unit_dbus_path(UNIT(s))))
989 goto oom;
990
991 if (!dbus_message_append_args(
992 reply,
993 DBUS_TYPE_OBJECT_PATH, &path,
994 DBUS_TYPE_INVALID))
995 goto oom;
996
997 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
998 char *introspection = NULL;
999 FILE *f;
1000 Iterator i;
1001 Unit *u;
1002 Job *j;
1003 const char *k;
1004 size_t size;
1005
1006 if (!(reply = dbus_message_new_method_return(message)))
1007 goto oom;
1008
1009 /* We roll our own introspection code here, instead of
1010 * relying on bus_default_message_handler() because we
1011 * need to generate our introspection string
1012 * dynamically. */
1013
1014 if (!(f = open_memstream(&introspection, &size)))
1015 goto oom;
1016
1017 fputs(INTROSPECTION_BEGIN, f);
1018
1019 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
1020 char *p;
1021
1022 if (k != u->id)
1023 continue;
1024
1025 if (!(p = bus_path_escape(k))) {
1026 fclose(f);
1027 free(introspection);
1028 goto oom;
1029 }
1030
1031 fprintf(f, "<node name=\"unit/%s\"/>", p);
1032 free(p);
1033 }
1034
1035 HASHMAP_FOREACH(j, m->jobs, i)
1036 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
1037
1038 fputs(INTROSPECTION_END, f);
1039
1040 if (ferror(f)) {
1041 fclose(f);
1042 free(introspection);
1043 goto oom;
1044 }
1045
1046 fclose(f);
1047
1048 if (!introspection)
1049 goto oom;
1050
1051 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
1052 free(introspection);
1053 goto oom;
1054 }
1055
1056 free(introspection);
1057
1058 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
1059
1060 assert(!m->queued_message);
1061
1062 /* Instead of sending the reply back right away, we
1063 * just remember that we need to and then send it
1064 * after the reload is finished. That way the caller
1065 * knows when the reload finished. */
1066
1067 if (!(m->queued_message = dbus_message_new_method_return(message)))
1068 goto oom;
1069
1070 m->queued_message_connection = connection;
1071 m->exit_code = MANAGER_RELOAD;
1072
1073 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
1074
1075 /* We don't send a reply back here, the client should
1076 * just wait for us disconnecting. */
1077
1078 m->exit_code = MANAGER_REEXECUTE;
1079
1080 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
1081
1082 if (m->running_as == MANAGER_SYSTEM) {
1083 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for user service managers.");
1084 return bus_send_error_reply(connection, message, &error, -ENOTSUP);
1085 }
1086
1087 if (!(reply = dbus_message_new_method_return(message)))
1088 goto oom;
1089
1090 m->exit_code = MANAGER_EXIT;
1091
1092 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reboot")) {
1093
1094 if (m->running_as != MANAGER_SYSTEM) {
1095 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Reboot is only supported for system managers.");
1096 return bus_send_error_reply(connection, message, &error, -ENOTSUP);
1097 }
1098
1099 if (!(reply = dbus_message_new_method_return(message)))
1100 goto oom;
1101
1102 m->exit_code = MANAGER_REBOOT;
1103
1104 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "PowerOff")) {
1105
1106 if (m->running_as != MANAGER_SYSTEM) {
1107 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Powering off is only supported for system managers.");
1108 return bus_send_error_reply(connection, message, &error, -ENOTSUP);
1109 }
1110
1111 if (!(reply = dbus_message_new_method_return(message)))
1112 goto oom;
1113
1114 m->exit_code = MANAGER_POWEROFF;
1115
1116 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Halt")) {
1117
1118 if (m->running_as != MANAGER_SYSTEM) {
1119 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Halting is only supported for system managers.");
1120 return bus_send_error_reply(connection, message, &error, -ENOTSUP);
1121 }
1122
1123 if (!(reply = dbus_message_new_method_return(message)))
1124 goto oom;
1125
1126 m->exit_code = MANAGER_HALT;
1127
1128 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KExec")) {
1129
1130 if (m->running_as != MANAGER_SYSTEM) {
1131 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "kexec is only supported for system managers.");
1132 return bus_send_error_reply(connection, message, &error, -ENOTSUP);
1133 }
1134
1135 if (!(reply = dbus_message_new_method_return(message)))
1136 goto oom;
1137
1138 m->exit_code = MANAGER_KEXEC;
1139
1140 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
1141 char **l = NULL, **e = NULL;
1142
1143 if ((r = bus_parse_strv(message, &l)) < 0) {
1144 if (r == -ENOMEM)
1145 goto oom;
1146
1147 return bus_send_error_reply(connection, message, NULL, r);
1148 }
1149
1150 e = strv_env_merge(2, m->environment, l);
1151 strv_free(l);
1152
1153 if (!e)
1154 goto oom;
1155
1156 if (!(reply = dbus_message_new_method_return(message))) {
1157 strv_free(e);
1158 goto oom;
1159 }
1160
1161 strv_free(m->environment);
1162 m->environment = e;
1163
1164 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
1165 char **l = NULL, **e = NULL;
1166
1167 if ((r = bus_parse_strv(message, &l)) < 0) {
1168 if (r == -ENOMEM)
1169 goto oom;
1170
1171 return bus_send_error_reply(connection, message, NULL, r);
1172 }
1173
1174 e = strv_env_delete(m->environment, 1, l);
1175 strv_free(l);
1176
1177 if (!e)
1178 goto oom;
1179
1180 if (!(reply = dbus_message_new_method_return(message))) {
1181 strv_free(e);
1182 goto oom;
1183 }
1184
1185 strv_free(m->environment);
1186 m->environment = e;
1187
1188 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetAndSetEnvironment")) {
1189 char **l_set = NULL, **l_unset = NULL, **e = NULL, **f = NULL;
1190 DBusMessageIter iter;
1191
1192 if (!dbus_message_iter_init(message, &iter))
1193 goto oom;
1194
1195 r = bus_parse_strv_iter(&iter, &l_unset);
1196 if (r < 0) {
1197 if (r == -ENOMEM)
1198 goto oom;
1199
1200 return bus_send_error_reply(connection, message, NULL, r);
1201 }
1202
1203 if (!dbus_message_iter_next(&iter)) {
1204 strv_free(l_unset);
1205 return bus_send_error_reply(connection, message, NULL, -EINVAL);
1206 }
1207
1208 r = bus_parse_strv_iter(&iter, &l_set);
1209 if (r < 0) {
1210 strv_free(l_unset);
1211 if (r == -ENOMEM)
1212 goto oom;
1213
1214 return bus_send_error_reply(connection, message, NULL, r);
1215 }
1216
1217 e = strv_env_delete(m->environment, 1, l_unset);
1218 strv_free(l_unset);
1219
1220 if (!e) {
1221 strv_free(l_set);
1222 goto oom;
1223 }
1224
1225 f = strv_env_merge(2, e, l_set);
1226 strv_free(l_set);
1227 strv_free(e);
1228
1229 if (!f)
1230 goto oom;
1231
1232 if (!(reply = dbus_message_new_method_return(message))) {
1233 strv_free(f);
1234 goto oom;
1235 }
1236
1237 strv_free(m->environment);
1238 m->environment = f;
1239 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnitFiles")) {
1240 DBusMessageIter iter, sub, sub2;
1241 Hashmap *h;
1242 Iterator i;
1243 UnitFileList *item;
1244
1245 reply = dbus_message_new_method_return(message);
1246 if (!reply)
1247 goto oom;
1248
1249 h = hashmap_new(string_hash_func, string_compare_func);
1250 if (!h)
1251 goto oom;
1252
1253 r = unit_file_get_list(m->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER, NULL, h);
1254 if (r < 0) {
1255 unit_file_list_free(h);
1256 dbus_message_unref(reply);
1257 return bus_send_error_reply(connection, message, NULL, r);
1258 }
1259
1260 dbus_message_iter_init_append(reply, &iter);
1261
1262 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ss)", &sub)) {
1263 unit_file_list_free(h);
1264 goto oom;
1265 }
1266
1267 HASHMAP_FOREACH(item, h, i) {
1268 const char *state;
1269
1270 state = unit_file_state_to_string(item->state);
1271 assert(state);
1272
1273 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2) ||
1274 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &item->path) ||
1275 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
1276 !dbus_message_iter_close_container(&sub, &sub2)) {
1277 unit_file_list_free(h);
1278 goto oom;
1279 }
1280 }
1281
1282 unit_file_list_free(h);
1283
1284 if (!dbus_message_iter_close_container(&iter, &sub))
1285 goto oom;
1286
1287 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitFileState")) {
1288 const char *name;
1289 UnitFileState state;
1290 const char *s;
1291
1292 if (!dbus_message_get_args(
1293 message,
1294 &error,
1295 DBUS_TYPE_STRING, &name,
1296 DBUS_TYPE_INVALID))
1297 return bus_send_error_reply(connection, message, &error, -EINVAL);
1298
1299 state = unit_file_get_state(m->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER, NULL, name);
1300 if (state < 0)
1301 return bus_send_error_reply(connection, message, NULL, state);
1302
1303 s = unit_file_state_to_string(state);
1304 assert(s);
1305
1306 reply = dbus_message_new_method_return(message);
1307 if (!reply)
1308 goto oom;
1309
1310 if (!dbus_message_append_args(
1311 reply,
1312 DBUS_TYPE_STRING, &s,
1313 DBUS_TYPE_INVALID))
1314 goto oom;
1315 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "EnableUnitFiles") ||
1316 dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReenableUnitFiles") ||
1317 dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LinkUnitFiles") ||
1318 dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "PresetUnitFiles") ||
1319 dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "MaskUnitFiles")) {
1320
1321 char **l = NULL;
1322 DBusMessageIter iter;
1323 UnitFileScope scope = m->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
1324 UnitFileChange *changes = NULL;
1325 unsigned n_changes = 0;
1326 dbus_bool_t runtime, force;
1327 int carries_install_info = -1;
1328
1329 if (!dbus_message_iter_init(message, &iter))
1330 goto oom;
1331
1332 r = bus_parse_strv_iter(&iter, &l);
1333 if (r < 0) {
1334 if (r == -ENOMEM)
1335 goto oom;
1336
1337 return bus_send_error_reply(connection, message, NULL, r);
1338 }
1339
1340 if (!dbus_message_iter_next(&iter) ||
1341 bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &runtime, true) < 0 ||
1342 bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &force, false) < 0) {
1343 strv_free(l);
1344 return bus_send_error_reply(connection, message, NULL, -EIO);
1345 }
1346
1347 if (streq(member, "EnableUnitFiles")) {
1348 r = unit_file_enable(scope, runtime, NULL, l, force, &changes, &n_changes);
1349 carries_install_info = r;
1350 } else if (streq(member, "ReenableUnitFiles")) {
1351 r = unit_file_reenable(scope, runtime, NULL, l, force, &changes, &n_changes);
1352 carries_install_info = r;
1353 } else if (streq(member, "LinkUnitFiles"))
1354 r = unit_file_link(scope, runtime, NULL, l, force, &changes, &n_changes);
1355 else if (streq(member, "PresetUnitFiles")) {
1356 r = unit_file_preset(scope, runtime, NULL, l, force, &changes, &n_changes);
1357 carries_install_info = r;
1358 } else if (streq(member, "MaskUnitFiles"))
1359 r = unit_file_mask(scope, runtime, NULL, l, force, &changes, &n_changes);
1360 else
1361 assert_not_reached("Uh? Wrong method");
1362
1363 strv_free(l);
1364 bus_manager_send_unit_files_changed(m);
1365
1366 if (r < 0) {
1367 unit_file_changes_free(changes, n_changes);
1368 return bus_send_error_reply(connection, message, NULL, r);
1369 }
1370
1371 reply = message_from_file_changes(message, changes, n_changes, carries_install_info);
1372 unit_file_changes_free(changes, n_changes);
1373
1374 if (!reply)
1375 goto oom;
1376
1377 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "DisableUnitFiles") ||
1378 dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnmaskUnitFiles")) {
1379
1380 char **l = NULL;
1381 DBusMessageIter iter;
1382 UnitFileScope scope = m->running_as == MANAGER_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
1383 UnitFileChange *changes = NULL;
1384 unsigned n_changes = 0;
1385 dbus_bool_t runtime;
1386
1387 if (!dbus_message_iter_init(message, &iter))
1388 goto oom;
1389
1390 r = bus_parse_strv_iter(&iter, &l);
1391 if (r < 0) {
1392 if (r == -ENOMEM)
1393 goto oom;
1394
1395 return bus_send_error_reply(connection, message, NULL, r);
1396 }
1397
1398 if (!dbus_message_iter_next(&iter) ||
1399 bus_iter_get_basic_and_next(&iter, DBUS_TYPE_BOOLEAN, &runtime, false) < 0) {
1400 strv_free(l);
1401 return bus_send_error_reply(connection, message, NULL, -EIO);
1402 }
1403
1404 if (streq(member, "DisableUnitFiles"))
1405 r = unit_file_disable(scope, runtime, NULL, l, &changes, &n_changes);
1406 else if (streq(member, "UnmaskUnitFiles"))
1407 r = unit_file_unmask(scope, runtime, NULL, l, &changes, &n_changes);
1408 else
1409 assert_not_reached("Uh? Wrong method");
1410
1411 strv_free(l);
1412 bus_manager_send_unit_files_changed(m);
1413
1414 if (r < 0) {
1415 unit_file_changes_free(changes, n_changes);
1416 return bus_send_error_reply(connection, message, NULL, r);
1417 }
1418
1419 reply = message_from_file_changes(message, changes, n_changes, -1);
1420 unit_file_changes_free(changes, n_changes);
1421
1422 if (!reply)
1423 goto oom;
1424
1425 } else {
1426 const BusBoundProperties bps[] = {
1427 { "org.freedesktop.systemd1.Manager", bus_systemd_properties, systemd_property_string },
1428 { "org.freedesktop.systemd1.Manager", bus_manager_properties, m },
1429 { NULL, }
1430 };
1431 return bus_default_message_handler(connection, message, NULL, INTERFACES_LIST, bps);
1432 }
1433
1434 if (job_type != _JOB_TYPE_INVALID) {
1435 const char *name, *smode, *old_name = NULL;
1436 JobMode mode;
1437 Job *j;
1438 Unit *u;
1439 bool b;
1440
1441 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
1442 b = dbus_message_get_args(
1443 message,
1444 &error,
1445 DBUS_TYPE_STRING, &old_name,
1446 DBUS_TYPE_STRING, &name,
1447 DBUS_TYPE_STRING, &smode,
1448 DBUS_TYPE_INVALID);
1449 else
1450 b = dbus_message_get_args(
1451 message,
1452 &error,
1453 DBUS_TYPE_STRING, &name,
1454 DBUS_TYPE_STRING, &smode,
1455 DBUS_TYPE_INVALID);
1456
1457 if (!b)
1458 return bus_send_error_reply(connection, message, &error, -EINVAL);
1459
1460 if (old_name)
1461 if (!(u = manager_get_unit(m, old_name)) ||
1462 !u->job ||
1463 u->job->type != JOB_START) {
1464 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "No job queued for unit %s", old_name);
1465 return bus_send_error_reply(connection, message, &error, -ENOENT);
1466 }
1467
1468
1469 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
1470 dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
1471 return bus_send_error_reply(connection, message, &error, -EINVAL);
1472 }
1473
1474 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
1475 return bus_send_error_reply(connection, message, &error, r);
1476
1477 if (reload_if_possible && unit_can_reload(u)) {
1478 if (job_type == JOB_RESTART)
1479 job_type = JOB_RELOAD_OR_START;
1480 else if (job_type == JOB_TRY_RESTART)
1481 job_type = JOB_RELOAD;
1482 }
1483
1484 if ((job_type == JOB_START && u->refuse_manual_start) ||
1485 (job_type == JOB_STOP && u->refuse_manual_stop) ||
1486 ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
1487 (u->refuse_manual_start || u->refuse_manual_stop))) {
1488 dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
1489 return bus_send_error_reply(connection, message, &error, -EPERM);
1490 }
1491
1492 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
1493 return bus_send_error_reply(connection, message, &error, r);
1494
1495 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
1496 goto oom;
1497
1498 j->bus = connection;
1499
1500 if (!(reply = dbus_message_new_method_return(message)))
1501 goto oom;
1502
1503 if (!(path = job_dbus_path(j)))
1504 goto oom;
1505
1506 if (!dbus_message_append_args(
1507 reply,
1508 DBUS_TYPE_OBJECT_PATH, &path,
1509 DBUS_TYPE_INVALID))
1510 goto oom;
1511 }
1512
1513 if (reply) {
1514 if (!dbus_connection_send(connection, reply, NULL))
1515 goto oom;
1516
1517 dbus_message_unref(reply);
1518 }
1519
1520 free(path);
1521
1522 return DBUS_HANDLER_RESULT_HANDLED;
1523
1524 oom:
1525 free(path);
1526
1527 if (reply)
1528 dbus_message_unref(reply);
1529
1530 dbus_error_free(&error);
1531
1532 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1533 }
1534
1535 const DBusObjectPathVTable bus_manager_vtable = {
1536 .message_function = bus_manager_message_handler
1537 };