]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/dbus-manager.c
src: our lord is coverity
[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
32 #define BUS_MANAGER_INTERFACE_BEGIN \
33 " <interface name=\"org.freedesktop.systemd1.Manager\">\n"
34
35 #define BUS_MANAGER_INTERFACE_METHODS \
36 " <method name=\"GetUnit\">\n" \
37 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
38 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
39 " </method>\n" \
40 " <method name=\"GetUnitByPID\">\n" \
41 " <arg name=\"pid\" type=\"u\" direction=\"in\"/>\n" \
42 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
43 " </method>\n" \
44 " <method name=\"LoadUnit\">\n" \
45 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
46 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
47 " </method>\n" \
48 " <method name=\"StartUnit\">\n" \
49 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
50 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
51 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
52 " </method>\n" \
53 " <method name=\"StartUnitReplace\">\n" \
54 " <arg name=\"old_unit\" type=\"s\" direction=\"in\"/>\n" \
55 " <arg name=\"new_unit\" type=\"s\" direction=\"in\"/>\n" \
56 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
57 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
58 " </method>\n" \
59 " <method name=\"StopUnit\">\n" \
60 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
61 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
62 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
63 " </method>\n" \
64 " <method name=\"ReloadUnit\">\n" \
65 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
66 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
67 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
68 " </method>\n" \
69 " <method name=\"RestartUnit\">\n" \
70 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
71 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
72 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
73 " </method>\n" \
74 " <method name=\"TryRestartUnit\">\n" \
75 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
76 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
77 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
78 " </method>\n" \
79 " <method name=\"ReloadOrRestartUnit\">\n" \
80 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
81 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
82 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
83 " </method>\n" \
84 " <method name=\"ReloadOrTryRestartUnit\">\n" \
85 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
86 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
87 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
88 " </method>\n" \
89 " <method name=\"KillUnit\">\n" \
90 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
91 " <arg name=\"who\" type=\"s\" direction=\"in\"/>\n" \
92 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
93 " <arg name=\"signal\" type=\"i\" direction=\"in\"/>\n" \
94 " </method>\n" \
95 " <method name=\"ResetFailedUnit\">\n" \
96 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
97 " </method>\n" \
98 " <method name=\"GetJob\">\n" \
99 " <arg name=\"id\" type=\"u\" direction=\"in\"/>\n" \
100 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
101 " </method>\n" \
102 " <method name=\"ClearJobs\"/>\n" \
103 " <method name=\"ResetFailed\"/>\n" \
104 " <method name=\"ListUnits\">\n" \
105 " <arg name=\"units\" type=\"a(ssssssouso)\" direction=\"out\"/>\n" \
106 " </method>\n" \
107 " <method name=\"ListJobs\">\n" \
108 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \
109 " </method>\n" \
110 " <method name=\"Subscribe\"/>\n" \
111 " <method name=\"Unsubscribe\"/>\n" \
112 " <method name=\"Dump\"/>\n" \
113 " <method name=\"CreateSnapshot\">\n" \
114 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
115 " <arg name=\"cleanup\" type=\"b\" direction=\"in\"/>\n" \
116 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
117 " </method>\n" \
118 " <method name=\"Reload\"/>\n" \
119 " <method name=\"Reexecute\"/>\n" \
120 " <method name=\"Exit\"/>\n" \
121 " <method name=\"Reboot\"/>\n" \
122 " <method name=\"PowerOff\"/>\n" \
123 " <method name=\"Halt\"/>\n" \
124 " <method name=\"KExec\"/>\n" \
125 " <method name=\"SetEnvironment\">\n" \
126 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
127 " </method>\n" \
128 " <method name=\"UnsetEnvironment\">\n" \
129 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
130 " </method>\n"
131
132 #define BUS_MANAGER_INTERFACE_SIGNALS \
133 " <signal name=\"UnitNew\">\n" \
134 " <arg name=\"id\" type=\"s\"/>\n" \
135 " <arg name=\"unit\" type=\"o\"/>\n" \
136 " </signal>\n" \
137 " <signal name=\"UnitRemoved\">\n" \
138 " <arg name=\"id\" type=\"s\"/>\n" \
139 " <arg name=\"unit\" type=\"o\"/>\n" \
140 " </signal>\n" \
141 " <signal name=\"JobNew\">\n" \
142 " <arg name=\"id\" type=\"u\"/>\n" \
143 " <arg name=\"job\" type=\"o\"/>\n" \
144 " </signal>\n" \
145 " <signal name=\"JobRemoved\">\n" \
146 " <arg name=\"id\" type=\"u\"/>\n" \
147 " <arg name=\"job\" type=\"o\"/>\n" \
148 " <arg name=\"result\" type=\"s\"/>\n" \
149 " </signal>"
150
151 #define BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
152 " <property name=\"Version\" type=\"s\" access=\"read\"/>\n" \
153 " <property name=\"Distribution\" type=\"s\" access=\"read\"/>\n" \
154 " <property name=\"Features\" type=\"s\" access=\"read\"/>\n" \
155 " <property name=\"Tainted\" type=\"s\" access=\"read\"/>\n" \
156 " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \
157 " <property name=\"InitRDTimestamp\" type=\"t\" access=\"read\"/>\n" \
158 " <property name=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \
159 " <property name=\"FinishTimestamp\" type=\"t\" access=\"read\"/>\n" \
160 " <property name=\"LogLevel\" type=\"s\" access=\"readwrite\"/>\n" \
161 " <property name=\"LogTarget\" type=\"s\" access=\"readwrite\"/>\n" \
162 " <property name=\"NNames\" type=\"u\" access=\"read\"/>\n" \
163 " <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n" \
164 " <property name=\"NInstalledJobs\" type=\"u\" access=\"read\"/>\n" \
165 " <property name=\"NFailedJobs\" type=\"u\" access=\"read\"/>\n" \
166 " <property name=\"Progress\" type=\"d\" access=\"read\"/>\n" \
167 " <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
168 " <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \
169 " <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \
170 " <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
171 " <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
172 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
173 " <property name=\"MountAuto\" type=\"b\" access=\"read\"/>\n" \
174 " <property name=\"SwapAuto\" type=\"b\" access=\"read\"/>\n" \
175 " <property name=\"DefaultControllers\" type=\"as\" access=\"read\"/>\n" \
176 " <property name=\"DefaultStandardOutput\" type=\"s\" access=\"read\"/>\n" \
177 " <property name=\"DefaultStandardError\" type=\"s\" access=\"read\"/>\n"
178
179 #ifdef HAVE_SYSV_COMPAT
180 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
181 " <property name=\"SysVConsole\" type=\"b\" access=\"read\"/>\n" \
182 " <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
183 " <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n"
184 #else
185 #define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV
186 #endif
187
188 #define BUS_MANAGER_INTERFACE_END \
189 " </interface>\n"
190
191 #define BUS_MANAGER_INTERFACE \
192 BUS_MANAGER_INTERFACE_BEGIN \
193 BUS_MANAGER_INTERFACE_METHODS \
194 BUS_MANAGER_INTERFACE_SIGNALS \
195 BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
196 BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
197 BUS_MANAGER_INTERFACE_END
198
199 #define INTROSPECTION_BEGIN \
200 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
201 "<node>\n" \
202 BUS_MANAGER_INTERFACE \
203 BUS_PROPERTIES_INTERFACE \
204 BUS_PEER_INTERFACE \
205 BUS_INTROSPECTABLE_INTERFACE
206
207 #define INTROSPECTION_END \
208 "</node>\n"
209
210 #define INTERFACES_LIST \
211 BUS_GENERIC_INTERFACES_LIST \
212 "org.freedesktop.systemd1.Manager\0"
213
214 const char bus_manager_interface[] _introspect_("Manager") = BUS_MANAGER_INTERFACE;
215
216 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
217 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_exec_output, exec_output, ExecOutput);
218
219 static int bus_manager_append_tainted(Manager *m, DBusMessageIter *i, const char *property, void *data) {
220 const char *t;
221 char buf[64] = "", *e = buf, *p = NULL;
222
223 assert(m);
224 assert(i);
225 assert(property);
226
227 if (m->taint_usr)
228 e = stpcpy(e, "usr-separate-fs ");
229
230 if (readlink_malloc("/etc/mtab", &p) < 0)
231 e = stpcpy(e, "etc-mtab-not-symlink ");
232 else
233 free(p);
234
235 if (access("/proc/cgroups", F_OK) < 0)
236 e = stpcpy(e, "cgroups-missing ");
237
238 t = strstrip(buf);
239
240 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
241 return -ENOMEM;
242
243 return 0;
244 }
245
246 static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
247 const char *t;
248
249 assert(m);
250 assert(i);
251 assert(property);
252
253 t = log_target_to_string(log_get_target());
254
255 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
256 return -ENOMEM;
257
258 return 0;
259 }
260
261 static int bus_manager_set_log_target(Manager *m, DBusMessageIter *i, const char *property) {
262 const char *t;
263
264 assert(m);
265 assert(i);
266 assert(property);
267
268 dbus_message_iter_get_basic(i, &t);
269
270 return log_set_target_from_string(t);
271 }
272
273 static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
274 const char *t;
275
276 assert(m);
277 assert(i);
278 assert(property);
279
280 t = log_level_to_string(log_get_max_level());
281
282 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
283 return -ENOMEM;
284
285 return 0;
286 }
287
288 static int bus_manager_set_log_level(Manager *m, DBusMessageIter *i, const char *property) {
289 const char *t;
290
291 assert(m);
292 assert(i);
293 assert(property);
294
295 dbus_message_iter_get_basic(i, &t);
296
297 return log_set_max_level_from_string(t);
298 }
299
300 static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
301 uint32_t u;
302
303 assert(m);
304 assert(i);
305 assert(property);
306
307 u = hashmap_size(m->units);
308
309 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
310 return -ENOMEM;
311
312 return 0;
313 }
314
315 static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char *property, void *data) {
316 uint32_t u;
317
318 assert(m);
319 assert(i);
320 assert(property);
321
322 u = hashmap_size(m->jobs);
323
324 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
325 return -ENOMEM;
326
327 return 0;
328 }
329
330 static int bus_manager_append_progress(Manager *m, DBusMessageIter *i, const char *property, void *data) {
331 double d;
332
333 assert(m);
334 assert(i);
335 assert(property);
336
337 if (dual_timestamp_is_set(&m->finish_timestamp))
338 d = 1.0;
339 else
340 d = 1.0 - ((double) hashmap_size(m->jobs) / (double) m->n_installed_jobs);
341
342 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_DOUBLE, &d))
343 return -ENOMEM;
344
345 return 0;
346 }
347
348 static const char *message_get_sender_with_fallback(DBusMessage *m) {
349 const char *s;
350
351 assert(m);
352
353 if ((s = dbus_message_get_sender(m)))
354 return s;
355
356 /* When the message came in from a direct connection the
357 * message will have no sender. We fix that here. */
358
359 return ":no-sender";
360 }
361
362 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
363 Manager *m = data;
364
365 const BusProperty properties[] = {
366 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string, "s", PACKAGE_STRING },
367 { "org.freedesktop.systemd1.Manager", "Distribution", bus_property_append_string, "s", DISTRIBUTION },
368 { "org.freedesktop.systemd1.Manager", "Features", bus_property_append_string, "s", SYSTEMD_FEATURES },
369 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as, "s", &m->running_as },
370 { "org.freedesktop.systemd1.Manager", "Tainted", bus_manager_append_tainted, "s", m },
371 { "org.freedesktop.systemd1.Manager", "InitRDTimestamp", bus_property_append_uint64, "t", &m->initrd_timestamp.realtime },
372 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t", &m->startup_timestamp.realtime },
373 { "org.freedesktop.systemd1.Manager", "FinishTimestamp", bus_property_append_uint64, "t", &m->finish_timestamp.realtime },
374 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", NULL, bus_manager_set_log_level},
375 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", NULL, bus_manager_set_log_target},
376 { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names, "u", NULL },
377 { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs, "u", NULL },
378 { "org.freedesktop.systemd1.Manager", "NInstalledJobs",bus_property_append_uint32, "u", &m->n_installed_jobs },
379 { "org.freedesktop.systemd1.Manager", "NFailedJobs", bus_property_append_uint32, "u", &m->n_failed_jobs },
380 { "org.freedesktop.systemd1.Manager", "Progress", bus_manager_append_progress, "d", NULL },
381 { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv, "as", m->environment },
382 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn", bus_property_append_bool, "b", &m->confirm_spawn },
383 { "org.freedesktop.systemd1.Manager", "ShowStatus", bus_property_append_bool, "b", &m->show_status },
384 { "org.freedesktop.systemd1.Manager", "UnitPath", bus_property_append_strv, "as", m->lookup_paths.unit_path },
385 { "org.freedesktop.systemd1.Manager", "NotifySocket", bus_property_append_string, "s", m->notify_socket },
386 { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_hierarchy },
387 { "org.freedesktop.systemd1.Manager", "MountAuto", bus_property_append_bool, "b", &m->mount_auto },
388 { "org.freedesktop.systemd1.Manager", "SwapAuto", bus_property_append_bool, "b", &m->swap_auto },
389 { "org.freedesktop.systemd1.Manager", "DefaultControllers", bus_property_append_strv, "as", m->default_controllers },
390 { "org.freedesktop.systemd1.Manager", "DefaultStandardOutput", bus_manager_append_exec_output, "s", &m->default_std_output },
391 { "org.freedesktop.systemd1.Manager", "DefaultStandardError", bus_manager_append_exec_output, "s", &m->default_std_error },
392 #ifdef HAVE_SYSV_COMPAT
393 { "org.freedesktop.systemd1.Manager", "SysVConsole", bus_property_append_bool, "b", &m->sysv_console },
394 { "org.freedesktop.systemd1.Manager", "SysVInitPath", bus_property_append_strv, "as", m->lookup_paths.sysvinit_path },
395 { "org.freedesktop.systemd1.Manager", "SysVRcndPath", bus_property_append_strv, "as", m->lookup_paths.sysvrcnd_path },
396 #endif
397 { NULL, NULL, NULL, NULL, NULL }
398 };
399
400 int r;
401 DBusError error;
402 DBusMessage *reply = NULL;
403 char * path = NULL;
404 JobType job_type = _JOB_TYPE_INVALID;
405 bool reload_if_possible = false;
406
407 assert(connection);
408 assert(message);
409 assert(m);
410
411 dbus_error_init(&error);
412
413 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
414 const char *name;
415 Unit *u;
416
417 if (!dbus_message_get_args(
418 message,
419 &error,
420 DBUS_TYPE_STRING, &name,
421 DBUS_TYPE_INVALID))
422 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
423
424 if (!(u = manager_get_unit(m, name))) {
425 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
426 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
427 }
428
429 if (!(reply = dbus_message_new_method_return(message)))
430 goto oom;
431
432 if (!(path = unit_dbus_path(u)))
433 goto oom;
434
435 if (!dbus_message_append_args(
436 reply,
437 DBUS_TYPE_OBJECT_PATH, &path,
438 DBUS_TYPE_INVALID))
439 goto oom;
440 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) {
441 Unit *u;
442 uint32_t pid;
443
444 if (!dbus_message_get_args(
445 message,
446 &error,
447 DBUS_TYPE_UINT32, &pid,
448 DBUS_TYPE_INVALID))
449 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
450
451 if (!(u = cgroup_unit_by_pid(m, (pid_t) pid))) {
452 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "No unit for PID %lu is loaded.", (unsigned long) pid);
453 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
454 }
455
456 if (!(reply = dbus_message_new_method_return(message)))
457 goto oom;
458
459 if (!(path = unit_dbus_path(u)))
460 goto oom;
461
462 if (!dbus_message_append_args(
463 reply,
464 DBUS_TYPE_OBJECT_PATH, &path,
465 DBUS_TYPE_INVALID))
466 goto oom;
467 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
468 const char *name;
469 Unit *u;
470
471 if (!dbus_message_get_args(
472 message,
473 &error,
474 DBUS_TYPE_STRING, &name,
475 DBUS_TYPE_INVALID))
476 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
477
478 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
479 return bus_send_error_reply(m, connection, message, &error, r);
480
481 if (!(reply = dbus_message_new_method_return(message)))
482 goto oom;
483
484 if (!(path = unit_dbus_path(u)))
485 goto oom;
486
487 if (!dbus_message_append_args(
488 reply,
489 DBUS_TYPE_OBJECT_PATH, &path,
490 DBUS_TYPE_INVALID))
491 goto oom;
492
493 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
494 job_type = JOB_START;
495 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
496 job_type = JOB_START;
497 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
498 job_type = JOB_STOP;
499 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
500 job_type = JOB_RELOAD;
501 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
502 job_type = JOB_RESTART;
503 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
504 job_type = JOB_TRY_RESTART;
505 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) {
506 reload_if_possible = true;
507 job_type = JOB_RESTART;
508 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) {
509 reload_if_possible = true;
510 job_type = JOB_TRY_RESTART;
511 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KillUnit")) {
512 const char *name, *swho, *smode;
513 int32_t signo;
514 Unit *u;
515 KillMode mode;
516 KillWho who;
517
518 if (!dbus_message_get_args(
519 message,
520 &error,
521 DBUS_TYPE_STRING, &name,
522 DBUS_TYPE_STRING, &swho,
523 DBUS_TYPE_STRING, &smode,
524 DBUS_TYPE_INT32, &signo,
525 DBUS_TYPE_INVALID))
526 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
527
528 if ((mode = kill_mode_from_string(smode)) < 0 ||
529 (who = kill_who_from_string(swho)) < 0 ||
530 signo <= 0 ||
531 signo >= _NSIG)
532 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
533
534 if (!(u = manager_get_unit(m, name))) {
535 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
536 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
537 }
538
539 if ((r = unit_kill(u, who, mode, signo, &error)) < 0)
540 return bus_send_error_reply(m, connection, message, &error, r);
541
542 if (!(reply = dbus_message_new_method_return(message)))
543 goto oom;
544
545 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
546 uint32_t id;
547 Job *j;
548
549 if (!dbus_message_get_args(
550 message,
551 &error,
552 DBUS_TYPE_UINT32, &id,
553 DBUS_TYPE_INVALID))
554 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
555
556 if (!(j = manager_get_job(m, id))) {
557 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
558 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
559 }
560
561 if (!(reply = dbus_message_new_method_return(message)))
562 goto oom;
563
564 if (!(path = job_dbus_path(j)))
565 goto oom;
566
567 if (!dbus_message_append_args(
568 reply,
569 DBUS_TYPE_OBJECT_PATH, &path,
570 DBUS_TYPE_INVALID))
571 goto oom;
572
573 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
574
575 manager_clear_jobs(m);
576
577 if (!(reply = dbus_message_new_method_return(message)))
578 goto oom;
579
580 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailed")) {
581
582 manager_reset_failed(m);
583
584 if (!(reply = dbus_message_new_method_return(message)))
585 goto oom;
586
587 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailedUnit")) {
588 const char *name;
589 Unit *u;
590
591 if (!dbus_message_get_args(
592 message,
593 &error,
594 DBUS_TYPE_STRING, &name,
595 DBUS_TYPE_INVALID))
596 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
597
598 if (!(u = manager_get_unit(m, name))) {
599 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
600 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
601 }
602
603 unit_reset_failed(u);
604
605 if (!(reply = dbus_message_new_method_return(message)))
606 goto oom;
607
608 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
609 DBusMessageIter iter, sub;
610 Iterator i;
611 Unit *u;
612 const char *k;
613
614 if (!(reply = dbus_message_new_method_return(message)))
615 goto oom;
616
617 dbus_message_iter_init_append(reply, &iter);
618
619 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub))
620 goto oom;
621
622 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
623 char *u_path, *j_path;
624 const char *description, *load_state, *active_state, *sub_state, *sjob_type, *following;
625 DBusMessageIter sub2;
626 uint32_t job_id;
627 Unit *f;
628
629 if (k != u->meta.id)
630 continue;
631
632 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
633 goto oom;
634
635 description = unit_description(u);
636 load_state = unit_load_state_to_string(u->meta.load_state);
637 active_state = unit_active_state_to_string(unit_active_state(u));
638 sub_state = unit_sub_state_to_string(u);
639
640 f = unit_following(u);
641 following = f ? f->meta.id : "";
642
643 if (!(u_path = unit_dbus_path(u)))
644 goto oom;
645
646 if (u->meta.job) {
647 job_id = (uint32_t) u->meta.job->id;
648
649 if (!(j_path = job_dbus_path(u->meta.job))) {
650 free(u_path);
651 goto oom;
652 }
653
654 sjob_type = job_type_to_string(u->meta.job->type);
655 } else {
656 job_id = 0;
657 j_path = u_path;
658 sjob_type = "";
659 }
660
661 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
662 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
663 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
664 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
665 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
666 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &following) ||
667 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
668 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
669 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
670 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
671 free(u_path);
672 if (u->meta.job)
673 free(j_path);
674 goto oom;
675 }
676
677 free(u_path);
678 if (u->meta.job)
679 free(j_path);
680
681 if (!dbus_message_iter_close_container(&sub, &sub2))
682 goto oom;
683 }
684
685 if (!dbus_message_iter_close_container(&iter, &sub))
686 goto oom;
687
688 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
689 DBusMessageIter iter, sub;
690 Iterator i;
691 Job *j;
692
693 if (!(reply = dbus_message_new_method_return(message)))
694 goto oom;
695
696 dbus_message_iter_init_append(reply, &iter);
697
698 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
699 goto oom;
700
701 HASHMAP_FOREACH(j, m->jobs, i) {
702 char *u_path, *j_path;
703 const char *state, *type;
704 uint32_t id;
705 DBusMessageIter sub2;
706
707 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
708 goto oom;
709
710 id = (uint32_t) j->id;
711 state = job_state_to_string(j->state);
712 type = job_type_to_string(j->type);
713
714 if (!(j_path = job_dbus_path(j)))
715 goto oom;
716
717 if (!(u_path = unit_dbus_path(j->unit))) {
718 free(j_path);
719 goto oom;
720 }
721
722 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
723 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
724 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
725 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
726 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
727 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
728 free(j_path);
729 free(u_path);
730 goto oom;
731 }
732
733 free(j_path);
734 free(u_path);
735
736 if (!dbus_message_iter_close_container(&sub, &sub2))
737 goto oom;
738 }
739
740 if (!dbus_message_iter_close_container(&iter, &sub))
741 goto oom;
742
743 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
744 char *client;
745 Set *s;
746
747 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
748 if (!(s = set_new(string_hash_func, string_compare_func)))
749 goto oom;
750
751 if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
752 set_free(s);
753 goto oom;
754 }
755 }
756
757 if (!(client = strdup(message_get_sender_with_fallback(message))))
758 goto oom;
759
760 if ((r = set_put(s, client)) < 0) {
761 free(client);
762 return bus_send_error_reply(m, connection, message, NULL, r);
763 }
764
765 if (!(reply = dbus_message_new_method_return(message)))
766 goto oom;
767
768 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
769 char *client;
770
771 if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) {
772 dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
773 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
774 }
775
776 free(client);
777
778 if (!(reply = dbus_message_new_method_return(message)))
779 goto oom;
780
781 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
782 FILE *f;
783 char *dump = NULL;
784 size_t size;
785
786 if (!(reply = dbus_message_new_method_return(message)))
787 goto oom;
788
789 if (!(f = open_memstream(&dump, &size)))
790 goto oom;
791
792 manager_dump_units(m, f, NULL);
793 manager_dump_jobs(m, f, NULL);
794
795 if (ferror(f)) {
796 fclose(f);
797 free(dump);
798 goto oom;
799 }
800
801 fclose(f);
802
803 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
804 free(dump);
805 goto oom;
806 }
807
808 free(dump);
809 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
810 const char *name;
811 dbus_bool_t cleanup;
812 Snapshot *s;
813
814 if (!dbus_message_get_args(
815 message,
816 &error,
817 DBUS_TYPE_STRING, &name,
818 DBUS_TYPE_BOOLEAN, &cleanup,
819 DBUS_TYPE_INVALID))
820 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
821
822 if (name && name[0] == 0)
823 name = NULL;
824
825 if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0)
826 return bus_send_error_reply(m, connection, message, &error, r);
827
828 if (!(reply = dbus_message_new_method_return(message)))
829 goto oom;
830
831 if (!(path = unit_dbus_path(UNIT(s))))
832 goto oom;
833
834 if (!dbus_message_append_args(
835 reply,
836 DBUS_TYPE_OBJECT_PATH, &path,
837 DBUS_TYPE_INVALID))
838 goto oom;
839
840 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
841 char *introspection = NULL;
842 FILE *f;
843 Iterator i;
844 Unit *u;
845 Job *j;
846 const char *k;
847 size_t size;
848
849 if (!(reply = dbus_message_new_method_return(message)))
850 goto oom;
851
852 /* We roll our own introspection code here, instead of
853 * relying on bus_default_message_handler() because we
854 * need to generate our introspection string
855 * dynamically. */
856
857 if (!(f = open_memstream(&introspection, &size)))
858 goto oom;
859
860 fputs(INTROSPECTION_BEGIN, f);
861
862 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
863 char *p;
864
865 if (k != u->meta.id)
866 continue;
867
868 if (!(p = bus_path_escape(k))) {
869 fclose(f);
870 free(introspection);
871 goto oom;
872 }
873
874 fprintf(f, "<node name=\"unit/%s\"/>", p);
875 free(p);
876 }
877
878 HASHMAP_FOREACH(j, m->jobs, i)
879 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
880
881 fputs(INTROSPECTION_END, f);
882
883 if (ferror(f)) {
884 fclose(f);
885 free(introspection);
886 goto oom;
887 }
888
889 fclose(f);
890
891 if (!introspection)
892 goto oom;
893
894 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
895 free(introspection);
896 goto oom;
897 }
898
899 free(introspection);
900
901 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
902
903 assert(!m->queued_message);
904
905 /* Instead of sending the reply back right away, we
906 * just remember that we need to and then send it
907 * after the reload is finished. That way the caller
908 * knows when the reload finished. */
909
910 if (!(m->queued_message = dbus_message_new_method_return(message)))
911 goto oom;
912
913 m->queued_message_connection = connection;
914 m->exit_code = MANAGER_RELOAD;
915
916 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
917
918 if (!(reply = dbus_message_new_method_return(message)))
919 goto oom;
920
921 m->exit_code = MANAGER_REEXECUTE;
922
923 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
924
925 if (m->running_as == MANAGER_SYSTEM) {
926 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for user service managers.");
927 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
928 }
929
930 if (!(reply = dbus_message_new_method_return(message)))
931 goto oom;
932
933 m->exit_code = MANAGER_EXIT;
934
935 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reboot")) {
936
937 if (m->running_as != MANAGER_SYSTEM) {
938 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Reboot is only supported for system managers.");
939 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
940 }
941
942 if (!(reply = dbus_message_new_method_return(message)))
943 goto oom;
944
945 m->exit_code = MANAGER_REBOOT;
946
947 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "PowerOff")) {
948
949 if (m->running_as != MANAGER_SYSTEM) {
950 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Powering off is only supported for system managers.");
951 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
952 }
953
954 if (!(reply = dbus_message_new_method_return(message)))
955 goto oom;
956
957 m->exit_code = MANAGER_POWEROFF;
958
959 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Halt")) {
960
961 if (m->running_as != MANAGER_SYSTEM) {
962 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Halting is only supported for system managers.");
963 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
964 }
965
966 if (!(reply = dbus_message_new_method_return(message)))
967 goto oom;
968
969 m->exit_code = MANAGER_HALT;
970
971 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "KExec")) {
972
973 if (m->running_as != MANAGER_SYSTEM) {
974 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "kexec is only supported for system managers.");
975 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
976 }
977
978 if (!(reply = dbus_message_new_method_return(message)))
979 goto oom;
980
981 m->exit_code = MANAGER_KEXEC;
982
983 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
984 char **l = NULL, **e = NULL;
985
986 if ((r = bus_parse_strv(message, &l)) < 0) {
987 if (r == -ENOMEM)
988 goto oom;
989
990 return bus_send_error_reply(m, connection, message, NULL, r);
991 }
992
993 e = strv_env_merge(2, m->environment, l);
994 strv_free(l);
995
996 if (!e)
997 goto oom;
998
999 if (!(reply = dbus_message_new_method_return(message))) {
1000 strv_free(e);
1001 goto oom;
1002 }
1003
1004 strv_free(m->environment);
1005 m->environment = e;
1006
1007 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
1008 char **l = NULL, **e = NULL;
1009
1010 if ((r = bus_parse_strv(message, &l)) < 0) {
1011 if (r == -ENOMEM)
1012 goto oom;
1013
1014 return bus_send_error_reply(m, connection, message, NULL, r);
1015 }
1016
1017 e = strv_env_delete(m->environment, 1, l);
1018 strv_free(l);
1019
1020 if (!e)
1021 goto oom;
1022
1023 if (!(reply = dbus_message_new_method_return(message))) {
1024 strv_free(e);
1025 goto oom;
1026 }
1027
1028 strv_free(m->environment);
1029 m->environment = e;
1030
1031 } else
1032 return bus_default_message_handler(m, connection, message, NULL, INTERFACES_LIST, properties);
1033
1034 if (job_type != _JOB_TYPE_INVALID) {
1035 const char *name, *smode, *old_name = NULL;
1036 JobMode mode;
1037 Job *j;
1038 Unit *u;
1039 bool b;
1040
1041 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnitReplace"))
1042 b = dbus_message_get_args(
1043 message,
1044 &error,
1045 DBUS_TYPE_STRING, &old_name,
1046 DBUS_TYPE_STRING, &name,
1047 DBUS_TYPE_STRING, &smode,
1048 DBUS_TYPE_INVALID);
1049 else
1050 b = dbus_message_get_args(
1051 message,
1052 &error,
1053 DBUS_TYPE_STRING, &name,
1054 DBUS_TYPE_STRING, &smode,
1055 DBUS_TYPE_INVALID);
1056
1057 if (!b)
1058 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
1059
1060 if (old_name)
1061 if (!(u = manager_get_unit(m, old_name)) ||
1062 !u->meta.job ||
1063 u->meta.job->type != JOB_START) {
1064 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "No job queued for unit %s", old_name);
1065 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
1066 }
1067
1068
1069 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
1070 dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
1071 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
1072 }
1073
1074 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
1075 return bus_send_error_reply(m, connection, message, &error, r);
1076
1077 if (reload_if_possible && unit_can_reload(u)) {
1078 if (job_type == JOB_RESTART)
1079 job_type = JOB_RELOAD_OR_START;
1080 else if (job_type == JOB_TRY_RESTART)
1081 job_type = JOB_RELOAD;
1082 }
1083
1084 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
1085 (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
1086 ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
1087 (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
1088 dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
1089 return bus_send_error_reply(m, connection, message, &error, -EPERM);
1090 }
1091
1092 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
1093 return bus_send_error_reply(m, connection, message, &error, r);
1094
1095 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
1096 goto oom;
1097
1098 j->bus = connection;
1099
1100 if (!(reply = dbus_message_new_method_return(message)))
1101 goto oom;
1102
1103 if (!(path = job_dbus_path(j)))
1104 goto oom;
1105
1106 if (!dbus_message_append_args(
1107 reply,
1108 DBUS_TYPE_OBJECT_PATH, &path,
1109 DBUS_TYPE_INVALID))
1110 goto oom;
1111 }
1112
1113 if (reply) {
1114 if (!dbus_connection_send(connection, reply, NULL))
1115 goto oom;
1116
1117 dbus_message_unref(reply);
1118 }
1119
1120 free(path);
1121
1122 return DBUS_HANDLER_RESULT_HANDLED;
1123
1124 oom:
1125 free(path);
1126
1127 if (reply)
1128 dbus_message_unref(reply);
1129
1130 dbus_error_free(&error);
1131
1132 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1133 }
1134
1135 const DBusObjectPathVTable bus_manager_vtable = {
1136 .message_function = bus_manager_message_handler
1137 };