]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/dbus-manager.c
manager: hookup execution of systemd-shutdown helper
[thirdparty/systemd.git] / src / dbus-manager.c
CommitLineData
d6c9574f 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
ea430986 2
a7334b09
LP
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
ea430986
LP
22#include <errno.h>
23
24#include "dbus.h"
25#include "log.h"
4139c1b2 26#include "dbus-manager.h"
1137a57c 27#include "strv.h"
398ef8ba 28#include "bus-errors.h"
ea430986 29
07459bb6
FF
30#define BUS_MANAGER_INTERFACE_BEGIN \
31 " <interface name=\"org.freedesktop.systemd1.Manager\">\n"
32
33#define BUS_MANAGER_INTERFACE_METHODS \
4288f619
LP
34 " <method name=\"GetUnit\">\n" \
35 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
36 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
37 " </method>\n" \
598b557b 38 " <method name=\"GetUnitByPID\">\n" \
07459bb6 39 " <arg name=\"pid\" type=\"u\" direction=\"in\"/>\n" \
598b557b
LP
40 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
41 " </method>\n" \
4288f619
LP
42 " <method name=\"LoadUnit\">\n" \
43 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
44 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
45 " </method>\n" \
c87eba54
LP
46 " <method name=\"StartUnit\">\n" \
47 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
48 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
49 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
50 " </method>\n" \
51 " <method name=\"StopUnit\">\n" \
52 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
53 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
54 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
55 " </method>\n" \
56 " <method name=\"ReloadUnit\">\n" \
57 " <arg name=\"name\" 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=\"RestartUnit\">\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" \
9a1ac7b9
LP
66 " <method name=\"TryRestartUnit\">\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" \
6f28c033
LP
71 " <method name=\"ReloadOrRestartUnit\">\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=\"ReloadOrTryRestartUnit\">\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" \
fdf20a31 81 " <method name=\"ResetFailedUnit\">\n" \
5632e374
LP
82 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
83 " </method>\n" \
4288f619
LP
84 " <method name=\"GetJob\">\n" \
85 " <arg name=\"id\" type=\"u\" direction=\"in\"/>\n" \
86 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
87 " </method>\n" \
88 " <method name=\"ClearJobs\"/>\n" \
fdf20a31 89 " <method name=\"ResetFailed\"/>\n" \
4288f619 90 " <method name=\"ListUnits\">\n" \
8fe914ec 91 " <arg name=\"units\" type=\"a(ssssssouso)\" direction=\"out\"/>\n" \
4288f619
LP
92 " </method>\n" \
93 " <method name=\"ListJobs\">\n" \
94 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \
95 " </method>\n" \
96 " <method name=\"Subscribe\"/>\n" \
97 " <method name=\"Unsubscribe\"/>\n" \
98 " <method name=\"Dump\"/>\n" \
99 " <method name=\"CreateSnapshot\">\n" \
100 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
5948ee7c 101 " <arg name=\"cleanup\" type=\"b\" direction=\"in\"/>\n" \
4288f619
LP
102 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
103 " </method>\n" \
104 " <method name=\"Reload\"/>\n" \
105 " <method name=\"Reexecute\"/>\n" \
106 " <method name=\"Exit\"/>\n" \
107 " <method name=\"SetEnvironment\">\n" \
108 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
109 " </method>\n" \
110 " <method name=\"UnsetEnvironment\">\n" \
111 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
07459bb6
FF
112 " </method>\n"
113
114#define BUS_MANAGER_INTERFACE_SIGNALS \
4288f619
LP
115 " <signal name=\"UnitNew\">\n" \
116 " <arg name=\"id\" type=\"s\"/>\n" \
117 " <arg name=\"unit\" type=\"o\"/>\n" \
118 " </signal>\n" \
119 " <signal name=\"UnitRemoved\">\n" \
120 " <arg name=\"id\" type=\"s\"/>\n" \
121 " <arg name=\"unit\" type=\"o\"/>\n" \
122 " </signal>\n" \
123 " <signal name=\"JobNew\">\n" \
124 " <arg name=\"id\" type=\"u\"/>\n" \
125 " <arg name=\"job\" type=\"o\"/>\n" \
126 " </signal>\n" \
127 " <signal name=\"JobRemoved\">\n" \
128 " <arg name=\"id\" type=\"u\"/>\n" \
129 " <arg name=\"job\" type=\"o\"/>\n" \
130 " <arg name=\"success\" type=\"b\"/>\n" \
07459bb6
FF
131 " </signal>"
132
133
134#define BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
4288f619
LP
135 " <property name=\"Version\" type=\"s\" access=\"read\"/>\n" \
136 " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \
0442c13b 137 " <property name=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \
4288f619
LP
138 " <property name=\"LogLevel\" type=\"s\" access=\"read\"/>\n" \
139 " <property name=\"LogTarget\" type=\"s\" access=\"read\"/>\n" \
140 " <property name=\"NNames\" type=\"u\" access=\"read\"/>\n" \
141 " <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n" \
e409f875 142 " <property name=\"NInstalledJobs\" type=\"u\" access=\"read\"/>\n" \
76bf48b7 143 " <property name=\"NFailedJobs\" type=\"u\" access=\"read\"/>\n" \
fa70128d 144 " <property name=\"Progress\" type=\"d\" access=\"read\"/>\n" \
4288f619 145 " <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
f295f5c0 146 " <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \
9e58ff9c 147 " <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \
f295f5c0 148 " <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
f295f5c0 149 " <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
c6c18be3 150 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
d3689161 151 " <property name=\"MountAuto\" type=\"b\" access=\"read\"/>\n" \
07459bb6
FF
152 " <property name=\"SwapAuto\" type=\"b\" access=\"read\"/>\n"
153
154#ifdef HAVE_SYSV_COMPAT
155#define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
156 " <property name=\"SysVConsole\" type=\"b\" access=\"read\"/>\n" \
157 " <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
158 " <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n"
159#else
160#define BUS_MANAGER_INTERFACE_PROPERTIES_SYSV
161#endif
162
163#define BUS_MANAGER_INTERFACE_END \
4288f619
LP
164 " </interface>\n"
165
07459bb6
FF
166#define BUS_MANAGER_INTERFACE \
167 BUS_MANAGER_INTERFACE_BEGIN \
168 BUS_MANAGER_INTERFACE_METHODS \
169 BUS_MANAGER_INTERFACE_SIGNALS \
170 BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
171 BUS_MANAGER_INTERFACE_PROPERTIES_SYSV \
172 BUS_MANAGER_INTERFACE_END
173
ea430986
LP
174#define INTROSPECTION_BEGIN \
175 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
4288f619
LP
176 "<node>\n" \
177 BUS_MANAGER_INTERFACE \
ea430986 178 BUS_PROPERTIES_INTERFACE \
c4e2ceae 179 BUS_PEER_INTERFACE \
ea430986
LP
180 BUS_INTROSPECTABLE_INTERFACE
181
182#define INTROSPECTION_END \
4288f619
LP
183 "</node>\n"
184
185const char bus_manager_interface[] = BUS_MANAGER_INTERFACE;
ea430986 186
4139c1b2 187static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
1adf1049
LP
188
189static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
190 const char *t;
191
192 assert(m);
193 assert(i);
194 assert(property);
195
196 t = log_target_to_string(log_get_target());
197
198 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
199 return -ENOMEM;
200
201 return 0;
202}
203
204static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
205 const char *t;
206
207 assert(m);
208 assert(i);
209 assert(property);
210
211 t = log_level_to_string(log_get_max_level());
212
213 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
214 return -ENOMEM;
215
216 return 0;
217}
218
4f0f902f
LP
219static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
220 uint32_t u;
221
222 assert(m);
223 assert(i);
224 assert(property);
225
226 u = hashmap_size(m->units);
227
228 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
229 return -ENOMEM;
230
231 return 0;
232}
233
234static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char *property, void *data) {
235 uint32_t u;
236
237 assert(m);
238 assert(i);
239 assert(property);
240
241 u = hashmap_size(m->jobs);
242
243 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
244 return -ENOMEM;
245
246 return 0;
247}
248
05d6a3b6
LP
249static int bus_manager_append_progress(Manager *m, DBusMessageIter *i, const char *property, void *data) {
250 double d;
251
252 assert(m);
253 assert(i);
254 assert(property);
255
256 if (dual_timestamp_is_set(&m->finish_timestamp))
257 d = 1.0;
258 else
259 d = 1.0 - ((double) hashmap_size(m->jobs) / (double) m->n_installed_jobs);
260
261 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_DOUBLE, &d))
262 return -ENOMEM;
263
264 return 0;
265}
266
894ba510
LP
267static const char *message_get_sender_with_fallback(DBusMessage *m) {
268 const char *s;
269
270 assert(m);
271
272 if ((s = dbus_message_get_sender(m)))
273 return s;
274
275 /* When the message came in from a direct connection the
276 * message will have no sender. We fix that here. */
277
278 return ":no-sender";
279}
280
5e8d1c9a 281static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
ea430986 282 Manager *m = data;
1adf1049
LP
283
284 const BusProperty properties[] = {
f295f5c0
LP
285 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string, "s", PACKAGE_STRING },
286 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as, "s", &m->running_as },
287 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t", &m->startup_timestamp.realtime },
b0c918b9 288 { "org.freedesktop.systemd1.Manager", "FinishTimestamp", bus_property_append_uint64, "t", &m->finish_timestamp.realtime },
f295f5c0
LP
289 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", NULL },
290 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", NULL },
291 { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names, "u", NULL },
292 { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs, "u", NULL },
e409f875 293 { "org.freedesktop.systemd1.Manager", "NInstalledJobs",bus_property_append_uint32, "u", &m->n_installed_jobs },
76bf48b7 294 { "org.freedesktop.systemd1.Manager", "NFailedJobs", bus_property_append_uint32, "u", &m->n_failed_jobs },
05d6a3b6 295 { "org.freedesktop.systemd1.Manager", "Progress", bus_manager_append_progress, "d", NULL },
f295f5c0
LP
296 { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv, "as", m->environment },
297 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn", bus_property_append_bool, "b", &m->confirm_spawn },
9e58ff9c 298 { "org.freedesktop.systemd1.Manager", "ShowStatus", bus_property_append_bool, "b", &m->show_status },
f295f5c0 299 { "org.freedesktop.systemd1.Manager", "UnitPath", bus_property_append_strv, "as", m->lookup_paths.unit_path },
f295f5c0 300 { "org.freedesktop.systemd1.Manager", "NotifySocket", bus_property_append_string, "s", m->notify_socket },
c6c18be3 301 { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_hierarchy },
d3689161 302 { "org.freedesktop.systemd1.Manager", "MountAuto", bus_property_append_bool, "b", &m->mount_auto },
173a8d04 303 { "org.freedesktop.systemd1.Manager", "SwapAuto", bus_property_append_bool, "b", &m->swap_auto },
07459bb6
FF
304#ifdef HAVE_SYSV_COMPAT
305 { "org.freedesktop.systemd1.Manager", "SysVConsole", bus_property_append_bool, "b", &m->sysv_console },
306 { "org.freedesktop.systemd1.Manager", "SysVInitPath", bus_property_append_strv, "as", m->lookup_paths.sysvinit_path },
307 { "org.freedesktop.systemd1.Manager", "SysVRcndPath", bus_property_append_strv, "as", m->lookup_paths.sysvrcnd_path },
308#endif
1adf1049
LP
309 { NULL, NULL, NULL, NULL, NULL }
310 };
311
312 int r;
ea430986
LP
313 DBusError error;
314 DBusMessage *reply = NULL;
315 char * path = NULL;
c87eba54 316 JobType job_type = _JOB_TYPE_INVALID;
6f28c033 317 bool reload_if_possible = false;
ea430986
LP
318
319 assert(connection);
320 assert(message);
321 assert(m);
322
323 dbus_error_init(&error);
324
4139c1b2 325 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
ea430986
LP
326 const char *name;
327 Unit *u;
328
329 if (!dbus_message_get_args(
330 message,
331 &error,
332 DBUS_TYPE_STRING, &name,
333 DBUS_TYPE_INVALID))
5e8d1c9a 334 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
ea430986 335
398ef8ba
LP
336 if (!(u = manager_get_unit(m, name))) {
337 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
338 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
339 }
ea430986
LP
340
341 if (!(reply = dbus_message_new_method_return(message)))
342 goto oom;
343
344 if (!(path = unit_dbus_path(u)))
345 goto oom;
346
347 if (!dbus_message_append_args(
348 reply,
349 DBUS_TYPE_OBJECT_PATH, &path,
350 DBUS_TYPE_INVALID))
351 goto oom;
598b557b 352 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) {
598b557b
LP
353 Unit *u;
354 uint32_t pid;
355
356 if (!dbus_message_get_args(
357 message,
358 &error,
359 DBUS_TYPE_UINT32, &pid,
360 DBUS_TYPE_INVALID))
361 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
362
363 if (!(u = cgroup_unit_by_pid(m, (pid_t) pid))) {
364 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "No unit for PID %lu is loaded.", (unsigned long) pid);
365 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
366 }
367
368 if (!(reply = dbus_message_new_method_return(message)))
369 goto oom;
370
371 if (!(path = unit_dbus_path(u)))
372 goto oom;
ea430986 373
598b557b
LP
374 if (!dbus_message_append_args(
375 reply,
376 DBUS_TYPE_OBJECT_PATH, &path,
377 DBUS_TYPE_INVALID))
378 goto oom;
4139c1b2 379 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
ea430986
LP
380 const char *name;
381 Unit *u;
382
383 if (!dbus_message_get_args(
384 message,
385 &error,
386 DBUS_TYPE_STRING, &name,
387 DBUS_TYPE_INVALID))
5e8d1c9a 388 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
ea430986 389
398ef8ba
LP
390 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
391 return bus_send_error_reply(m, connection, message, &error, r);
ea430986
LP
392
393 if (!(reply = dbus_message_new_method_return(message)))
394 goto oom;
395
396 if (!(path = unit_dbus_path(u)))
397 goto oom;
398
399 if (!dbus_message_append_args(
400 reply,
401 DBUS_TYPE_OBJECT_PATH, &path,
402 DBUS_TYPE_INVALID))
403 goto oom;
404
c87eba54
LP
405 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
406 job_type = JOB_START;
407 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
408 job_type = JOB_STOP;
409 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
410 job_type = JOB_RELOAD;
411 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
412 job_type = JOB_RESTART;
9a1ac7b9
LP
413 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
414 job_type = JOB_TRY_RESTART;
6f28c033
LP
415 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) {
416 reload_if_possible = true;
417 job_type = JOB_RESTART;
418 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) {
419 reload_if_possible = true;
420 job_type = JOB_TRY_RESTART;
421 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
ea430986
LP
422 uint32_t id;
423 Job *j;
424
425 if (!dbus_message_get_args(
426 message,
427 &error,
428 DBUS_TYPE_UINT32, &id,
429 DBUS_TYPE_INVALID))
5e8d1c9a 430 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
ea430986 431
398ef8ba
LP
432 if (!(j = manager_get_job(m, id))) {
433 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
434 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
435 }
ea430986
LP
436
437 if (!(reply = dbus_message_new_method_return(message)))
438 goto oom;
439
440 if (!(path = job_dbus_path(j)))
441 goto oom;
442
443 if (!dbus_message_append_args(
444 reply,
445 DBUS_TYPE_OBJECT_PATH, &path,
446 DBUS_TYPE_INVALID))
447 goto oom;
448
4139c1b2 449 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
ea430986
LP
450
451 manager_clear_jobs(m);
452
453 if (!(reply = dbus_message_new_method_return(message)))
454 goto oom;
455
fdf20a31 456 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailed")) {
5632e374 457
fdf20a31 458 manager_reset_failed(m);
5632e374
LP
459
460 if (!(reply = dbus_message_new_method_return(message)))
461 goto oom;
462
fdf20a31 463 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailedUnit")) {
5632e374
LP
464 const char *name;
465 Unit *u;
466
467 if (!dbus_message_get_args(
468 message,
469 &error,
470 DBUS_TYPE_STRING, &name,
471 DBUS_TYPE_INVALID))
472 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
473
474 if (!(u = manager_get_unit(m, name))) {
475 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
476 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
477 }
478
fdf20a31 479 unit_reset_failed(u);
5632e374
LP
480
481 if (!(reply = dbus_message_new_method_return(message)))
482 goto oom;
483
4139c1b2 484 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
ea430986
LP
485 DBusMessageIter iter, sub;
486 Iterator i;
487 Unit *u;
488 const char *k;
489
490 if (!(reply = dbus_message_new_method_return(message)))
491 goto oom;
492
493 dbus_message_iter_init_append(reply, &iter);
494
8fe914ec 495 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub))
ea430986
LP
496 goto oom;
497
498 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
47be870b 499 char *u_path, *j_path;
8fe914ec 500 const char *description, *load_state, *active_state, *sub_state, *sjob_type, *following;
ea430986
LP
501 DBusMessageIter sub2;
502 uint32_t job_id;
a7f241db 503 Unit *f;
ea430986 504
9e2f7c11 505 if (k != u->meta.id)
ea430986
LP
506 continue;
507
508 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
509 goto oom;
510
511 description = unit_description(u);
512 load_state = unit_load_state_to_string(u->meta.load_state);
513 active_state = unit_active_state_to_string(unit_active_state(u));
10a94420 514 sub_state = unit_sub_state_to_string(u);
a7f241db
LP
515
516 f = unit_following(u);
517 following = f ? f->meta.id : "";
ea430986 518
47be870b 519 if (!(u_path = unit_dbus_path(u)))
ea430986
LP
520 goto oom;
521
522 if (u->meta.job) {
523 job_id = (uint32_t) u->meta.job->id;
524
47be870b
LP
525 if (!(j_path = job_dbus_path(u->meta.job))) {
526 free(u_path);
ea430986
LP
527 goto oom;
528 }
529
c87eba54 530 sjob_type = job_type_to_string(u->meta.job->type);
ea430986
LP
531 } else {
532 job_id = 0;
47be870b 533 j_path = u_path;
c87eba54 534 sjob_type = "";
ea430986
LP
535 }
536
9e2f7c11 537 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
ea430986
LP
538 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
539 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
540 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
10a94420 541 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
8fe914ec 542 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &following) ||
47be870b 543 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
ea430986 544 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
c87eba54 545 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
47be870b
LP
546 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
547 free(u_path);
ea430986 548 if (u->meta.job)
47be870b 549 free(j_path);
ea430986
LP
550 goto oom;
551 }
552
47be870b 553 free(u_path);
ea430986 554 if (u->meta.job)
47be870b 555 free(j_path);
ea430986
LP
556
557 if (!dbus_message_iter_close_container(&sub, &sub2))
558 goto oom;
559 }
560
561 if (!dbus_message_iter_close_container(&iter, &sub))
562 goto oom;
563
4139c1b2 564 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
ea430986
LP
565 DBusMessageIter iter, sub;
566 Iterator i;
567 Job *j;
568
569 if (!(reply = dbus_message_new_method_return(message)))
570 goto oom;
571
572 dbus_message_iter_init_append(reply, &iter);
573
574 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
575 goto oom;
576
577 HASHMAP_FOREACH(j, m->jobs, i) {
47be870b 578 char *u_path, *j_path;
9e2f7c11 579 const char *state, *type;
ea430986
LP
580 uint32_t id;
581 DBusMessageIter sub2;
582
583 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
584 goto oom;
585
586 id = (uint32_t) j->id;
ea430986 587 state = job_state_to_string(j->state);
2b53c70b 588 type = job_type_to_string(j->type);
ea430986 589
47be870b 590 if (!(j_path = job_dbus_path(j)))
ea430986
LP
591 goto oom;
592
47be870b
LP
593 if (!(u_path = unit_dbus_path(j->unit))) {
594 free(j_path);
ea430986
LP
595 goto oom;
596 }
597
598 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
9e2f7c11 599 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
ea430986
LP
600 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
601 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
47be870b
LP
602 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
603 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
604 free(j_path);
605 free(u_path);
ea430986
LP
606 goto oom;
607 }
608
47be870b
LP
609 free(j_path);
610 free(u_path);
ea430986
LP
611
612 if (!dbus_message_iter_close_container(&sub, &sub2))
613 goto oom;
614 }
615
616 if (!dbus_message_iter_close_container(&iter, &sub))
617 goto oom;
618
4139c1b2 619 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
c1e1601e 620 char *client;
a567261a
LP
621 Set *s;
622
623 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
624 if (!(s = set_new(string_hash_func, string_compare_func)))
625 goto oom;
626
627 if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
628 set_free(s);
629 goto oom;
630 }
631 }
c1e1601e 632
894ba510 633 if (!(client = strdup(message_get_sender_with_fallback(message))))
c1e1601e
LP
634 goto oom;
635
a567261a
LP
636 if ((r = set_put(s, client)) < 0) {
637 free(client);
5e8d1c9a 638 return bus_send_error_reply(m, connection, message, NULL, r);
a567261a 639 }
c1e1601e
LP
640
641 if (!(reply = dbus_message_new_method_return(message)))
642 goto oom;
643
4139c1b2 644 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
c1e1601e
LP
645 char *client;
646
398ef8ba
LP
647 if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) {
648 dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
649 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
650 }
c1e1601e
LP
651
652 free(client);
653
654 if (!(reply = dbus_message_new_method_return(message)))
655 goto oom;
656
4139c1b2 657 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
b152adec
LP
658 FILE *f;
659 char *dump = NULL;
660 size_t size;
661
662 if (!(reply = dbus_message_new_method_return(message)))
663 goto oom;
664
665 if (!(f = open_memstream(&dump, &size)))
666 goto oom;
667
668 manager_dump_units(m, f, NULL);
669 manager_dump_jobs(m, f, NULL);
670
671 if (ferror(f)) {
672 fclose(f);
673 free(dump);
674 goto oom;
675 }
676
677 fclose(f);
678
679 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
680 free(dump);
681 goto oom;
682 }
683
684 free(dump);
4139c1b2
LP
685 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
686 const char *name;
687 dbus_bool_t cleanup;
688 Snapshot *s;
689
690 if (!dbus_message_get_args(
691 message,
692 &error,
693 DBUS_TYPE_STRING, &name,
694 DBUS_TYPE_BOOLEAN, &cleanup,
695 DBUS_TYPE_INVALID))
5e8d1c9a 696 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
4139c1b2
LP
697
698 if (name && name[0] == 0)
699 name = NULL;
700
398ef8ba
LP
701 if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0)
702 return bus_send_error_reply(m, connection, message, &error, r);
4139c1b2
LP
703
704 if (!(reply = dbus_message_new_method_return(message)))
705 goto oom;
706
707 if (!(path = unit_dbus_path(UNIT(s))))
708 goto oom;
709
710 if (!dbus_message_append_args(
711 reply,
712 DBUS_TYPE_OBJECT_PATH, &path,
713 DBUS_TYPE_INVALID))
714 goto oom;
b152adec 715
ea430986
LP
716 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
717 char *introspection = NULL;
718 FILE *f;
719 Iterator i;
720 Unit *u;
721 Job *j;
722 const char *k;
723 size_t size;
724
725 if (!(reply = dbus_message_new_method_return(message)))
726 goto oom;
727
728 /* We roll our own introspection code here, instead of
729 * relying on bus_default_message_handler() because we
730 * need to generate our introspection string
731 * dynamically. */
732
733 if (!(f = open_memstream(&introspection, &size)))
734 goto oom;
735
736 fputs(INTROSPECTION_BEGIN, f);
737
738 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
739 char *p;
740
9e2f7c11 741 if (k != u->meta.id)
ea430986
LP
742 continue;
743
744 if (!(p = bus_path_escape(k))) {
745 fclose(f);
746 free(introspection);
747 goto oom;
748 }
749
750 fprintf(f, "<node name=\"unit/%s\"/>", p);
751 free(p);
752 }
753
754 HASHMAP_FOREACH(j, m->jobs, i)
755 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
756
757 fputs(INTROSPECTION_END, f);
758
759 if (ferror(f)) {
760 fclose(f);
761 free(introspection);
762 goto oom;
763 }
764
765 fclose(f);
766
767 if (!introspection)
768 goto oom;
769
770 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
771 free(introspection);
772 goto oom;
773 }
774
775 free(introspection);
776
a16e1123
LP
777 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
778
779 assert(!m->queued_message);
780
781 /* Instead of sending the reply back right away, we
782 * just remember that we need to and then send it
783 * after the reload is finished. That way the caller
784 * knows when the reload finished. */
785
786 if (!(m->queued_message = dbus_message_new_method_return(message)))
787 goto oom;
788
7b97f477 789 m->queued_message_connection = connection;
a16e1123
LP
790 m->exit_code = MANAGER_RELOAD;
791
792 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
793
794 if (!(reply = dbus_message_new_method_return(message)))
795 goto oom;
796
797 m->exit_code = MANAGER_REEXECUTE;
798
799 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
800
398ef8ba
LP
801 if (m->running_as == MANAGER_SYSTEM) {
802 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for session managers.");
803 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
804 }
a16e1123
LP
805
806 if (!(reply = dbus_message_new_method_return(message)))
807 goto oom;
808
809 m->exit_code = MANAGER_EXIT;
810
1137a57c
LP
811 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
812 char **l = NULL, **e = NULL;
813
814 if ((r = bus_parse_strv(message, &l)) < 0) {
815 if (r == -ENOMEM)
816 goto oom;
817
5e8d1c9a 818 return bus_send_error_reply(m, connection, message, NULL, r);
1137a57c
LP
819 }
820
5b6319dc 821 e = strv_env_merge(2, m->environment, l);
1137a57c
LP
822 strv_free(l);
823
824 if (!e)
825 goto oom;
826
827 if (!(reply = dbus_message_new_method_return(message))) {
828 strv_free(e);
829 goto oom;
830 }
831
832 strv_free(m->environment);
833 m->environment = e;
834
835 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
836 char **l = NULL, **e = NULL;
837
838 if ((r = bus_parse_strv(message, &l)) < 0) {
839 if (r == -ENOMEM)
840 goto oom;
841
5e8d1c9a 842 return bus_send_error_reply(m, connection, message, NULL, r);
1137a57c
LP
843 }
844
5b6319dc 845 e = strv_env_delete(m->environment, 1, l);
1137a57c
LP
846 strv_free(l);
847
848 if (!e)
849 goto oom;
850
851 if (!(reply = dbus_message_new_method_return(message)))
852 goto oom;
853
854 strv_free(m->environment);
855 m->environment = e;
856
ea430986 857 } else
5e8d1c9a 858 return bus_default_message_handler(m, connection, message, NULL, properties);
ea430986 859
c87eba54
LP
860 if (job_type != _JOB_TYPE_INVALID) {
861 const char *name, *smode;
862 JobMode mode;
863 Job *j;
864 Unit *u;
865
866 if (!dbus_message_get_args(
867 message,
868 &error,
869 DBUS_TYPE_STRING, &name,
870 DBUS_TYPE_STRING, &smode,
871 DBUS_TYPE_INVALID))
5e8d1c9a 872 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
c87eba54 873
398ef8ba
LP
874 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
875 dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
876 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
877 }
c87eba54 878
398ef8ba
LP
879 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
880 return bus_send_error_reply(m, connection, message, &error, r);
c87eba54 881
6f28c033
LP
882 if (reload_if_possible && unit_can_reload(u)) {
883 if (job_type == JOB_RESTART)
884 job_type = JOB_RELOAD_OR_START;
885 else if (job_type == JOB_TRY_RESTART)
886 job_type = JOB_RELOAD;
887 }
888
b5e9dba8
LP
889 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
890 (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
891 ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
892 (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
893 dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
398ef8ba
LP
894 return bus_send_error_reply(m, connection, message, &error, -EPERM);
895 }
c87eba54 896
398ef8ba
LP
897 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
898 return bus_send_error_reply(m, connection, message, &error, r);
c87eba54 899
894ba510 900 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
a567261a
LP
901 goto oom;
902
903 j->bus = connection;
904
c87eba54
LP
905 if (!(reply = dbus_message_new_method_return(message)))
906 goto oom;
907
908 if (!(path = job_dbus_path(j)))
909 goto oom;
910
911 if (!dbus_message_append_args(
912 reply,
913 DBUS_TYPE_OBJECT_PATH, &path,
914 DBUS_TYPE_INVALID))
915 goto oom;
916 }
917
ea430986
LP
918 free(path);
919
920 if (reply) {
921 if (!dbus_connection_send(connection, reply, NULL))
922 goto oom;
923
924 dbus_message_unref(reply);
925 }
926
927 return DBUS_HANDLER_RESULT_HANDLED;
928
929oom:
930 free(path);
931
932 if (reply)
933 dbus_message_unref(reply);
934
935 dbus_error_free(&error);
936
937 return DBUS_HANDLER_RESULT_NEED_MEMORY;
938}
939
940const DBusObjectPathVTable bus_manager_vtable = {
941 .message_function = bus_manager_message_handler
942};