]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/dbus-manager.c
sysv: optionally disable of SysV init/rcN.d support at compile time
[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=\"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" \
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" \
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" \
81 " <method name=\"ResetFailedUnit\">\n" \
82 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
83 " </method>\n" \
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" \
89 " <method name=\"ResetFailed\"/>\n" \
90 " <method name=\"ListUnits\">\n" \
91 " <arg name=\"units\" type=\"a(ssssssouso)\" direction=\"out\"/>\n" \
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" \
101 " <arg name=\"cleanup\" type=\"b\" direction=\"in\"/>\n" \
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" \
112 " </method>\n"
113
114 #define BUS_MANAGER_INTERFACE_SIGNALS \
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" \
131 " </signal>"
132
133
134 #define BUS_MANAGER_INTERFACE_PROPERTIES_GENERAL \
135 " <property name=\"Version\" type=\"s\" access=\"read\"/>\n" \
136 " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \
137 " <property name=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \
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" \
142 " <property name=\"NInstalledJobs\" type=\"u\" access=\"read\"/>\n" \
143 " <property name=\"NFailedJobs\" type=\"u\" access=\"read\"/>\n" \
144 " <property name=\"Progress\" type=\"d\" access=\"read\"/>\n" \
145 " <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
146 " <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \
147 " <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \
148 " <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
149 " <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
150 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
151 " <property name=\"MountAuto\" type=\"b\" access=\"read\"/>\n" \
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 \
164 " </interface>\n"
165
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
174 #define INTROSPECTION_BEGIN \
175 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
176 "<node>\n" \
177 BUS_MANAGER_INTERFACE \
178 BUS_PROPERTIES_INTERFACE \
179 BUS_PEER_INTERFACE \
180 BUS_INTROSPECTABLE_INTERFACE
181
182 #define INTROSPECTION_END \
183 "</node>\n"
184
185 const char bus_manager_interface[] = BUS_MANAGER_INTERFACE;
186
187 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
188
189 static 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
204 static 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
219 static 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
234 static 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
249 static 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
267 static 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
281 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
282 Manager *m = data;
283
284 const BusProperty properties[] = {
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 },
288 { "org.freedesktop.systemd1.Manager", "FinishTimestamp", bus_property_append_uint64, "t", &m->finish_timestamp.realtime },
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 },
293 { "org.freedesktop.systemd1.Manager", "NInstalledJobs",bus_property_append_uint32, "u", &m->n_installed_jobs },
294 { "org.freedesktop.systemd1.Manager", "NFailedJobs", bus_property_append_uint32, "u", &m->n_failed_jobs },
295 { "org.freedesktop.systemd1.Manager", "Progress", bus_manager_append_progress, "d", NULL },
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 },
298 { "org.freedesktop.systemd1.Manager", "ShowStatus", bus_property_append_bool, "b", &m->show_status },
299 { "org.freedesktop.systemd1.Manager", "UnitPath", bus_property_append_strv, "as", m->lookup_paths.unit_path },
300 { "org.freedesktop.systemd1.Manager", "NotifySocket", bus_property_append_string, "s", m->notify_socket },
301 { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_hierarchy },
302 { "org.freedesktop.systemd1.Manager", "MountAuto", bus_property_append_bool, "b", &m->mount_auto },
303 { "org.freedesktop.systemd1.Manager", "SwapAuto", bus_property_append_bool, "b", &m->swap_auto },
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
309 { NULL, NULL, NULL, NULL, NULL }
310 };
311
312 int r;
313 DBusError error;
314 DBusMessage *reply = NULL;
315 char * path = NULL;
316 JobType job_type = _JOB_TYPE_INVALID;
317 bool reload_if_possible = false;
318
319 assert(connection);
320 assert(message);
321 assert(m);
322
323 dbus_error_init(&error);
324
325 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
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))
334 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
335
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 }
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;
352 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnitByPID")) {
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;
373
374 if (!dbus_message_append_args(
375 reply,
376 DBUS_TYPE_OBJECT_PATH, &path,
377 DBUS_TYPE_INVALID))
378 goto oom;
379 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
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))
388 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
389
390 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
391 return bus_send_error_reply(m, connection, message, &error, r);
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
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;
413 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
414 job_type = JOB_TRY_RESTART;
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")) {
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))
430 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
431
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 }
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
449 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
450
451 manager_clear_jobs(m);
452
453 if (!(reply = dbus_message_new_method_return(message)))
454 goto oom;
455
456 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailed")) {
457
458 manager_reset_failed(m);
459
460 if (!(reply = dbus_message_new_method_return(message)))
461 goto oom;
462
463 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetFailedUnit")) {
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
479 unit_reset_failed(u);
480
481 if (!(reply = dbus_message_new_method_return(message)))
482 goto oom;
483
484 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
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
495 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub))
496 goto oom;
497
498 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
499 char *u_path, *j_path;
500 const char *description, *load_state, *active_state, *sub_state, *sjob_type, *following;
501 DBusMessageIter sub2;
502 uint32_t job_id;
503 Unit *f;
504
505 if (k != u->meta.id)
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));
514 sub_state = unit_sub_state_to_string(u);
515
516 f = unit_following(u);
517 following = f ? f->meta.id : "";
518
519 if (!(u_path = unit_dbus_path(u)))
520 goto oom;
521
522 if (u->meta.job) {
523 job_id = (uint32_t) u->meta.job->id;
524
525 if (!(j_path = job_dbus_path(u->meta.job))) {
526 free(u_path);
527 goto oom;
528 }
529
530 sjob_type = job_type_to_string(u->meta.job->type);
531 } else {
532 job_id = 0;
533 j_path = u_path;
534 sjob_type = "";
535 }
536
537 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
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) ||
541 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
542 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &following) ||
543 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
544 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
545 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
546 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
547 free(u_path);
548 if (u->meta.job)
549 free(j_path);
550 goto oom;
551 }
552
553 free(u_path);
554 if (u->meta.job)
555 free(j_path);
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
564 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
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) {
578 char *u_path, *j_path;
579 const char *state, *type;
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;
587 state = job_state_to_string(j->state);
588 type = job_type_to_string(j->type);
589
590 if (!(j_path = job_dbus_path(j)))
591 goto oom;
592
593 if (!(u_path = unit_dbus_path(j->unit))) {
594 free(j_path);
595 goto oom;
596 }
597
598 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
599 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
600 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
601 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
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);
606 goto oom;
607 }
608
609 free(j_path);
610 free(u_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", "Subscribe")) {
620 char *client;
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 }
632
633 if (!(client = strdup(message_get_sender_with_fallback(message))))
634 goto oom;
635
636 if ((r = set_put(s, client)) < 0) {
637 free(client);
638 return bus_send_error_reply(m, connection, message, NULL, r);
639 }
640
641 if (!(reply = dbus_message_new_method_return(message)))
642 goto oom;
643
644 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
645 char *client;
646
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 }
651
652 free(client);
653
654 if (!(reply = dbus_message_new_method_return(message)))
655 goto oom;
656
657 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
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);
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))
696 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
697
698 if (name && name[0] == 0)
699 name = NULL;
700
701 if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0)
702 return bus_send_error_reply(m, connection, message, &error, r);
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;
715
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
741 if (k != u->meta.id)
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
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
789 m->queued_message_connection = connection;
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
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 }
805
806 if (!(reply = dbus_message_new_method_return(message)))
807 goto oom;
808
809 m->exit_code = MANAGER_EXIT;
810
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
818 return bus_send_error_reply(m, connection, message, NULL, r);
819 }
820
821 e = strv_env_merge(2, m->environment, l);
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
842 return bus_send_error_reply(m, connection, message, NULL, r);
843 }
844
845 e = strv_env_delete(m->environment, 1, l);
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
857 } else
858 return bus_default_message_handler(m, connection, message, NULL, properties);
859
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))
872 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
873
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 }
878
879 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
880 return bus_send_error_reply(m, connection, message, &error, r);
881
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
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.");
894 return bus_send_error_reply(m, connection, message, &error, -EPERM);
895 }
896
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);
899
900 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
901 goto oom;
902
903 j->bus = connection;
904
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
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
929 oom:
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
940 const DBusObjectPathVTable bus_manager_vtable = {
941 .message_function = bus_manager_message_handler
942 };