1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
26 #include "dbus-manager.h"
28 #include "bus-errors.h"
30 #define BUS_MANAGER_INTERFACE \
31 " <interface name=\"org.freedesktop.systemd1.Manager\">\n" \
32 " <method name=\"GetUnit\">\n" \
33 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
34 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
36 " <method name=\"LoadUnit\">\n" \
37 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
38 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
40 " <method name=\"StartUnit\">\n" \
41 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
42 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
43 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
45 " <method name=\"StopUnit\">\n" \
46 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
47 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
48 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
50 " <method name=\"ReloadUnit\">\n" \
51 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
52 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
53 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
55 " <method name=\"RestartUnit\">\n" \
56 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
57 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
58 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
60 " <method name=\"TryRestartUnit\">\n" \
61 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
62 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
63 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
65 " <method name=\"ReloadOrRestartUnit\">\n" \
66 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
67 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
68 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
70 " <method name=\"ReloadOrTryRestartUnit\">\n" \
71 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
72 " <arg name=\"mode\" type=\"s\" direction=\"in\"/>\n" \
73 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
75 " <method name=\"ResetMaintenanceUnit\">\n" \
76 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
78 " <method name=\"GetJob\">\n" \
79 " <arg name=\"id\" type=\"u\" direction=\"in\"/>\n" \
80 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
82 " <method name=\"ClearJobs\"/>\n" \
83 " <method name=\"ResetMaintenance\"/>\n" \
84 " <method name=\"ListUnits\">\n" \
85 " <arg name=\"units\" type=\"a(ssssssouso)\" direction=\"out\"/>\n" \
87 " <method name=\"ListJobs\">\n" \
88 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \
90 " <method name=\"Subscribe\"/>\n" \
91 " <method name=\"Unsubscribe\"/>\n" \
92 " <method name=\"Dump\"/>\n" \
93 " <method name=\"CreateSnapshot\">\n" \
94 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
95 " <arg nane=\"cleanup\" type=\"b\" direction=\"in\"/>\n" \
96 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
98 " <method name=\"Reload\"/>\n" \
99 " <method name=\"Reexecute\"/>\n" \
100 " <method name=\"Exit\"/>\n" \
101 " <method name=\"SetEnvironment\">\n" \
102 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
104 " <method name=\"UnsetEnvironment\">\n" \
105 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
107 " <signal name=\"UnitNew\">\n" \
108 " <arg name=\"id\" type=\"s\"/>\n" \
109 " <arg name=\"unit\" type=\"o\"/>\n" \
111 " <signal name=\"UnitRemoved\">\n" \
112 " <arg name=\"id\" type=\"s\"/>\n" \
113 " <arg name=\"unit\" type=\"o\"/>\n" \
115 " <signal name=\"JobNew\">\n" \
116 " <arg name=\"id\" type=\"u\"/>\n" \
117 " <arg name=\"job\" type=\"o\"/>\n" \
119 " <signal name=\"JobRemoved\">\n" \
120 " <arg name=\"id\" type=\"u\"/>\n" \
121 " <arg name=\"job\" type=\"o\"/>\n" \
122 " <arg name=\"success\" type=\"b\"/>\n" \
124 " <property name=\"Version\" type=\"s\" access=\"read\"/>\n" \
125 " <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \
126 " <property name=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \
127 " <property name=\"LogLevel\" type=\"s\" access=\"read\"/>\n" \
128 " <property name=\"LogTarget\" type=\"s\" access=\"read\"/>\n" \
129 " <property name=\"NNames\" type=\"u\" access=\"read\"/>\n" \
130 " <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n" \
131 " <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
132 " <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \
133 " <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \
134 " <property name=\"SysVConsole\" type=\"b\" access=\"read\"/>\n" \
135 " <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
136 " <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
137 " <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n" \
138 " <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
139 " <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
142 #define INTROSPECTION_BEGIN \
143 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
145 BUS_MANAGER_INTERFACE \
146 BUS_PROPERTIES_INTERFACE \
147 BUS_INTROSPECTABLE_INTERFACE
149 #define INTROSPECTION_END \
152 const char bus_manager_interface[] = BUS_MANAGER_INTERFACE;
154 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
156 static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
163 t = log_target_to_string(log_get_target());
165 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
171 static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
178 t = log_level_to_string(log_get_max_level());
180 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
186 static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
193 u = hashmap_size(m->units);
195 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
201 static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char *property, void *data) {
208 u = hashmap_size(m->jobs);
210 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
216 static const char *message_get_sender_with_fallback(DBusMessage *m) {
221 if ((s = dbus_message_get_sender(m)))
224 /* When the message came in from a direct connection the
225 * message will have no sender. We fix that here. */
230 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
233 const BusProperty properties[] = {
234 { "org.freedesktop.systemd1.Manager", "Version", bus_property_append_string, "s", PACKAGE_STRING },
235 { "org.freedesktop.systemd1.Manager", "RunningAs", bus_manager_append_running_as, "s", &m->running_as },
236 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t", &m->startup_timestamp.realtime },
237 { "org.freedesktop.systemd1.Manager", "LogLevel", bus_manager_append_log_level, "s", NULL },
238 { "org.freedesktop.systemd1.Manager", "LogTarget", bus_manager_append_log_target, "s", NULL },
239 { "org.freedesktop.systemd1.Manager", "NNames", bus_manager_append_n_names, "u", NULL },
240 { "org.freedesktop.systemd1.Manager", "NJobs", bus_manager_append_n_jobs, "u", NULL },
241 { "org.freedesktop.systemd1.Manager", "Environment", bus_property_append_strv, "as", m->environment },
242 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn", bus_property_append_bool, "b", &m->confirm_spawn },
243 { "org.freedesktop.systemd1.Manager", "ShowStatus", bus_property_append_bool, "b", &m->show_status },
244 { "org.freedesktop.systemd1.Manager", "SysVConsole", bus_property_append_bool, "b", &m->sysv_console },
245 { "org.freedesktop.systemd1.Manager", "UnitPath", bus_property_append_strv, "as", m->lookup_paths.unit_path },
246 { "org.freedesktop.systemd1.Manager", "SysVInitPath", bus_property_append_strv, "as", m->lookup_paths.sysvinit_path },
247 { "org.freedesktop.systemd1.Manager", "SysVRcndPath", bus_property_append_strv, "as", m->lookup_paths.sysvrcnd_path },
248 { "org.freedesktop.systemd1.Manager", "NotifySocket", bus_property_append_string, "s", m->notify_socket },
249 { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_hierarchy },
250 { NULL, NULL, NULL, NULL, NULL }
255 DBusMessage *reply = NULL;
257 JobType job_type = _JOB_TYPE_INVALID;
258 bool reload_if_possible = false;
264 dbus_error_init(&error);
266 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
270 if (!dbus_message_get_args(
273 DBUS_TYPE_STRING, &name,
275 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
277 if (!(u = manager_get_unit(m, name))) {
278 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
279 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
282 if (!(reply = dbus_message_new_method_return(message)))
285 if (!(path = unit_dbus_path(u)))
288 if (!dbus_message_append_args(
290 DBUS_TYPE_OBJECT_PATH, &path,
294 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
298 if (!dbus_message_get_args(
301 DBUS_TYPE_STRING, &name,
303 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
305 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
306 return bus_send_error_reply(m, connection, message, &error, r);
308 if (!(reply = dbus_message_new_method_return(message)))
311 if (!(path = unit_dbus_path(u)))
314 if (!dbus_message_append_args(
316 DBUS_TYPE_OBJECT_PATH, &path,
320 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
321 job_type = JOB_START;
322 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
324 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
325 job_type = JOB_RELOAD;
326 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
327 job_type = JOB_RESTART;
328 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
329 job_type = JOB_TRY_RESTART;
330 else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrRestartUnit")) {
331 reload_if_possible = true;
332 job_type = JOB_RESTART;
333 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadOrTryRestartUnit")) {
334 reload_if_possible = true;
335 job_type = JOB_TRY_RESTART;
336 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
340 if (!dbus_message_get_args(
343 DBUS_TYPE_UINT32, &id,
345 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
347 if (!(j = manager_get_job(m, id))) {
348 dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
349 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
352 if (!(reply = dbus_message_new_method_return(message)))
355 if (!(path = job_dbus_path(j)))
358 if (!dbus_message_append_args(
360 DBUS_TYPE_OBJECT_PATH, &path,
364 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
366 manager_clear_jobs(m);
368 if (!(reply = dbus_message_new_method_return(message)))
371 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetMaintenance")) {
373 manager_reset_maintenance(m);
375 if (!(reply = dbus_message_new_method_return(message)))
378 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetMaintenanceUnit")) {
382 if (!dbus_message_get_args(
385 DBUS_TYPE_STRING, &name,
387 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
389 if (!(u = manager_get_unit(m, name))) {
390 dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
391 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
394 unit_reset_maintenance(u);
396 if (!(reply = dbus_message_new_method_return(message)))
399 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
400 DBusMessageIter iter, sub;
405 if (!(reply = dbus_message_new_method_return(message)))
408 dbus_message_iter_init_append(reply, &iter);
410 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub))
413 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
414 char *u_path, *j_path;
415 const char *description, *load_state, *active_state, *sub_state, *sjob_type, *following;
416 DBusMessageIter sub2;
423 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
426 description = unit_description(u);
427 load_state = unit_load_state_to_string(u->meta.load_state);
428 active_state = unit_active_state_to_string(unit_active_state(u));
429 sub_state = unit_sub_state_to_string(u);
431 f = unit_following(u);
432 following = f ? f->meta.id : "";
434 if (!(u_path = unit_dbus_path(u)))
438 job_id = (uint32_t) u->meta.job->id;
440 if (!(j_path = job_dbus_path(u->meta.job))) {
445 sjob_type = job_type_to_string(u->meta.job->type);
452 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
453 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
454 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
455 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
456 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
457 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &following) ||
458 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
459 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
460 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
461 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
472 if (!dbus_message_iter_close_container(&sub, &sub2))
476 if (!dbus_message_iter_close_container(&iter, &sub))
479 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
480 DBusMessageIter iter, sub;
484 if (!(reply = dbus_message_new_method_return(message)))
487 dbus_message_iter_init_append(reply, &iter);
489 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
492 HASHMAP_FOREACH(j, m->jobs, i) {
493 char *u_path, *j_path;
494 const char *state, *type;
496 DBusMessageIter sub2;
498 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
501 id = (uint32_t) j->id;
502 state = job_state_to_string(j->state);
503 type = job_type_to_string(j->type);
505 if (!(j_path = job_dbus_path(j)))
508 if (!(u_path = unit_dbus_path(j->unit))) {
513 if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
514 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
515 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
516 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
517 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
518 !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
527 if (!dbus_message_iter_close_container(&sub, &sub2))
531 if (!dbus_message_iter_close_container(&iter, &sub))
534 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
538 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
539 if (!(s = set_new(string_hash_func, string_compare_func)))
542 if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
548 if (!(client = strdup(message_get_sender_with_fallback(message))))
551 if ((r = set_put(s, client)) < 0) {
553 return bus_send_error_reply(m, connection, message, NULL, r);
556 if (!(reply = dbus_message_new_method_return(message)))
559 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
562 if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) {
563 dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
564 return bus_send_error_reply(m, connection, message, &error, -ENOENT);
569 if (!(reply = dbus_message_new_method_return(message)))
572 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
577 if (!(reply = dbus_message_new_method_return(message)))
580 if (!(f = open_memstream(&dump, &size)))
583 manager_dump_units(m, f, NULL);
584 manager_dump_jobs(m, f, NULL);
594 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
600 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
605 if (!dbus_message_get_args(
608 DBUS_TYPE_STRING, &name,
609 DBUS_TYPE_BOOLEAN, &cleanup,
611 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
613 if (name && name[0] == 0)
616 if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0)
617 return bus_send_error_reply(m, connection, message, &error, r);
619 if (!(reply = dbus_message_new_method_return(message)))
622 if (!(path = unit_dbus_path(UNIT(s))))
625 if (!dbus_message_append_args(
627 DBUS_TYPE_OBJECT_PATH, &path,
631 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
632 char *introspection = NULL;
640 if (!(reply = dbus_message_new_method_return(message)))
643 /* We roll our own introspection code here, instead of
644 * relying on bus_default_message_handler() because we
645 * need to generate our introspection string
648 if (!(f = open_memstream(&introspection, &size)))
651 fputs(INTROSPECTION_BEGIN, f);
653 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
659 if (!(p = bus_path_escape(k))) {
665 fprintf(f, "<node name=\"unit/%s\"/>", p);
669 HASHMAP_FOREACH(j, m->jobs, i)
670 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
672 fputs(INTROSPECTION_END, f);
685 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
692 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
694 assert(!m->queued_message);
696 /* Instead of sending the reply back right away, we
697 * just remember that we need to and then send it
698 * after the reload is finished. That way the caller
699 * knows when the reload finished. */
701 if (!(m->queued_message = dbus_message_new_method_return(message)))
704 m->queued_message_connection = connection;
705 m->exit_code = MANAGER_RELOAD;
707 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
709 if (!(reply = dbus_message_new_method_return(message)))
712 m->exit_code = MANAGER_REEXECUTE;
714 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
716 if (m->running_as == MANAGER_SYSTEM) {
717 dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for session managers.");
718 return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
721 if (!(reply = dbus_message_new_method_return(message)))
724 m->exit_code = MANAGER_EXIT;
726 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
727 char **l = NULL, **e = NULL;
729 if ((r = bus_parse_strv(message, &l)) < 0) {
733 return bus_send_error_reply(m, connection, message, NULL, r);
736 e = strv_env_merge(2, m->environment, l);
742 if (!(reply = dbus_message_new_method_return(message))) {
747 strv_free(m->environment);
750 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
751 char **l = NULL, **e = NULL;
753 if ((r = bus_parse_strv(message, &l)) < 0) {
757 return bus_send_error_reply(m, connection, message, NULL, r);
760 e = strv_env_delete(m->environment, 1, l);
766 if (!(reply = dbus_message_new_method_return(message)))
769 strv_free(m->environment);
773 return bus_default_message_handler(m, connection, message, NULL, properties);
775 if (job_type != _JOB_TYPE_INVALID) {
776 const char *name, *smode;
781 if (!dbus_message_get_args(
784 DBUS_TYPE_STRING, &name,
785 DBUS_TYPE_STRING, &smode,
787 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
789 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
790 dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
791 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
794 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
795 return bus_send_error_reply(m, connection, message, &error, r);
797 if (reload_if_possible && unit_can_reload(u)) {
798 if (job_type == JOB_RESTART)
799 job_type = JOB_RELOAD_OR_START;
800 else if (job_type == JOB_TRY_RESTART)
801 job_type = JOB_RELOAD;
804 if ((job_type == JOB_START && u->meta.refuse_manual_start) ||
805 (job_type == JOB_STOP && u->meta.refuse_manual_stop) ||
806 ((job_type == JOB_RESTART || job_type == JOB_TRY_RESTART) &&
807 (u->meta.refuse_manual_start || u->meta.refuse_manual_stop))) {
808 dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, may be requested by dependency only.");
809 return bus_send_error_reply(m, connection, message, &error, -EPERM);
812 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
813 return bus_send_error_reply(m, connection, message, &error, r);
815 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
820 if (!(reply = dbus_message_new_method_return(message)))
823 if (!(path = job_dbus_path(j)))
826 if (!dbus_message_append_args(
828 DBUS_TYPE_OBJECT_PATH, &path,
836 if (!dbus_connection_send(connection, reply, NULL))
839 dbus_message_unref(reply);
842 return DBUS_HANDLER_RESULT_HANDLED;
848 dbus_message_unref(reply);
850 dbus_error_free(&error);
852 return DBUS_HANDLER_RESULT_NEED_MEMORY;
855 const DBusObjectPathVTable bus_manager_vtable = {
856 .message_function = bus_manager_message_handler