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