]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/dbus-manager.c
service: hide output of sysv scripts if quiet is passed on the kernel cmdline
[thirdparty/systemd.git] / src / dbus-manager.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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 \
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" \
35 " </method>\n" \
36 " <method name=\"LoadUnit\">\n" \
37 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
38 " <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n" \
39 " </method>\n" \
40 " <method name=\"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" \
44 " </method>\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" \
49 " </method>\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" \
54 " </method>\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" \
59 " </method>\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" \
64 " </method>\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" \
69 " </method>\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" \
74 " </method>\n" \
75 " <method name=\"ResetMaintenanceUnit\">\n" \
76 " <arg name=\"name\" type=\"s\" direction=\"in\"/>\n" \
77 " </method>\n" \
78 " <method name=\"GetJob\">\n" \
79 " <arg name=\"id\" type=\"u\" direction=\"in\"/>\n" \
80 " <arg name=\"job\" type=\"o\" direction=\"out\"/>\n" \
81 " </method>\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" \
86 " </method>\n" \
87 " <method name=\"ListJobs\">\n" \
88 " <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \
89 " </method>\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" \
97 " </method>\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" \
103 " </method>\n" \
104 " <method name=\"UnsetEnvironment\">\n" \
105 " <arg name=\"names\" type=\"as\" direction=\"in\"/>\n" \
106 " </method>\n" \
107 " <signal name=\"UnitNew\">\n" \
108 " <arg name=\"id\" type=\"s\"/>\n" \
109 " <arg name=\"unit\" type=\"o\"/>\n" \
110 " </signal>\n" \
111 " <signal name=\"UnitRemoved\">\n" \
112 " <arg name=\"id\" type=\"s\"/>\n" \
113 " <arg name=\"unit\" type=\"o\"/>\n" \
114 " </signal>\n" \
115 " <signal name=\"JobNew\">\n" \
116 " <arg name=\"id\" type=\"u\"/>\n" \
117 " <arg name=\"job\" type=\"o\"/>\n" \
118 " </signal>\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" \
123 " </signal>" \
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" \
140 " </interface>\n"
141
142 #define INTROSPECTION_BEGIN \
143 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
144 "<node>\n" \
145 BUS_MANAGER_INTERFACE \
146 BUS_PROPERTIES_INTERFACE \
147 BUS_INTROSPECTABLE_INTERFACE
148
149 #define INTROSPECTION_END \
150 "</node>\n"
151
152 const char bus_manager_interface[] = BUS_MANAGER_INTERFACE;
153
154 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
155
156 static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
157 const char *t;
158
159 assert(m);
160 assert(i);
161 assert(property);
162
163 t = log_target_to_string(log_get_target());
164
165 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
166 return -ENOMEM;
167
168 return 0;
169 }
170
171 static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
172 const char *t;
173
174 assert(m);
175 assert(i);
176 assert(property);
177
178 t = log_level_to_string(log_get_max_level());
179
180 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
181 return -ENOMEM;
182
183 return 0;
184 }
185
186 static int bus_manager_append_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
187 uint32_t u;
188
189 assert(m);
190 assert(i);
191 assert(property);
192
193 u = hashmap_size(m->units);
194
195 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
196 return -ENOMEM;
197
198 return 0;
199 }
200
201 static int bus_manager_append_n_jobs(Manager *m, DBusMessageIter *i, const char *property, void *data) {
202 uint32_t u;
203
204 assert(m);
205 assert(i);
206 assert(property);
207
208 u = hashmap_size(m->jobs);
209
210 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
211 return -ENOMEM;
212
213 return 0;
214 }
215
216 static const char *message_get_sender_with_fallback(DBusMessage *m) {
217 const char *s;
218
219 assert(m);
220
221 if ((s = dbus_message_get_sender(m)))
222 return s;
223
224 /* When the message came in from a direct connection the
225 * message will have no sender. We fix that here. */
226
227 return ":no-sender";
228 }
229
230 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
231 Manager *m = data;
232
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 }
251 };
252
253 int r;
254 DBusError error;
255 DBusMessage *reply = NULL;
256 char * path = NULL;
257 JobType job_type = _JOB_TYPE_INVALID;
258 bool reload_if_possible = false;
259
260 assert(connection);
261 assert(message);
262 assert(m);
263
264 dbus_error_init(&error);
265
266 if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
267 const char *name;
268 Unit *u;
269
270 if (!dbus_message_get_args(
271 message,
272 &error,
273 DBUS_TYPE_STRING, &name,
274 DBUS_TYPE_INVALID))
275 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
276
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);
280 }
281
282 if (!(reply = dbus_message_new_method_return(message)))
283 goto oom;
284
285 if (!(path = unit_dbus_path(u)))
286 goto oom;
287
288 if (!dbus_message_append_args(
289 reply,
290 DBUS_TYPE_OBJECT_PATH, &path,
291 DBUS_TYPE_INVALID))
292 goto oom;
293
294 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
295 const char *name;
296 Unit *u;
297
298 if (!dbus_message_get_args(
299 message,
300 &error,
301 DBUS_TYPE_STRING, &name,
302 DBUS_TYPE_INVALID))
303 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
304
305 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
306 return bus_send_error_reply(m, connection, message, &error, r);
307
308 if (!(reply = dbus_message_new_method_return(message)))
309 goto oom;
310
311 if (!(path = unit_dbus_path(u)))
312 goto oom;
313
314 if (!dbus_message_append_args(
315 reply,
316 DBUS_TYPE_OBJECT_PATH, &path,
317 DBUS_TYPE_INVALID))
318 goto oom;
319
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"))
323 job_type = JOB_STOP;
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")) {
337 uint32_t id;
338 Job *j;
339
340 if (!dbus_message_get_args(
341 message,
342 &error,
343 DBUS_TYPE_UINT32, &id,
344 DBUS_TYPE_INVALID))
345 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
346
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);
350 }
351
352 if (!(reply = dbus_message_new_method_return(message)))
353 goto oom;
354
355 if (!(path = job_dbus_path(j)))
356 goto oom;
357
358 if (!dbus_message_append_args(
359 reply,
360 DBUS_TYPE_OBJECT_PATH, &path,
361 DBUS_TYPE_INVALID))
362 goto oom;
363
364 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
365
366 manager_clear_jobs(m);
367
368 if (!(reply = dbus_message_new_method_return(message)))
369 goto oom;
370
371 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetMaintenance")) {
372
373 manager_reset_maintenance(m);
374
375 if (!(reply = dbus_message_new_method_return(message)))
376 goto oom;
377
378 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ResetMaintenanceUnit")) {
379 const char *name;
380 Unit *u;
381
382 if (!dbus_message_get_args(
383 message,
384 &error,
385 DBUS_TYPE_STRING, &name,
386 DBUS_TYPE_INVALID))
387 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
388
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);
392 }
393
394 unit_reset_maintenance(u);
395
396 if (!(reply = dbus_message_new_method_return(message)))
397 goto oom;
398
399 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
400 DBusMessageIter iter, sub;
401 Iterator i;
402 Unit *u;
403 const char *k;
404
405 if (!(reply = dbus_message_new_method_return(message)))
406 goto oom;
407
408 dbus_message_iter_init_append(reply, &iter);
409
410 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(ssssssouso)", &sub))
411 goto oom;
412
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;
417 uint32_t job_id;
418 Unit *f;
419
420 if (k != u->meta.id)
421 continue;
422
423 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
424 goto oom;
425
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);
430
431 f = unit_following(u);
432 following = f ? f->meta.id : "";
433
434 if (!(u_path = unit_dbus_path(u)))
435 goto oom;
436
437 if (u->meta.job) {
438 job_id = (uint32_t) u->meta.job->id;
439
440 if (!(j_path = job_dbus_path(u->meta.job))) {
441 free(u_path);
442 goto oom;
443 }
444
445 sjob_type = job_type_to_string(u->meta.job->type);
446 } else {
447 job_id = 0;
448 j_path = u_path;
449 sjob_type = "";
450 }
451
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)) {
462 free(u_path);
463 if (u->meta.job)
464 free(j_path);
465 goto oom;
466 }
467
468 free(u_path);
469 if (u->meta.job)
470 free(j_path);
471
472 if (!dbus_message_iter_close_container(&sub, &sub2))
473 goto oom;
474 }
475
476 if (!dbus_message_iter_close_container(&iter, &sub))
477 goto oom;
478
479 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
480 DBusMessageIter iter, sub;
481 Iterator i;
482 Job *j;
483
484 if (!(reply = dbus_message_new_method_return(message)))
485 goto oom;
486
487 dbus_message_iter_init_append(reply, &iter);
488
489 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
490 goto oom;
491
492 HASHMAP_FOREACH(j, m->jobs, i) {
493 char *u_path, *j_path;
494 const char *state, *type;
495 uint32_t id;
496 DBusMessageIter sub2;
497
498 if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
499 goto oom;
500
501 id = (uint32_t) j->id;
502 state = job_state_to_string(j->state);
503 type = job_type_to_string(j->type);
504
505 if (!(j_path = job_dbus_path(j)))
506 goto oom;
507
508 if (!(u_path = unit_dbus_path(j->unit))) {
509 free(j_path);
510 goto oom;
511 }
512
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)) {
519 free(j_path);
520 free(u_path);
521 goto oom;
522 }
523
524 free(j_path);
525 free(u_path);
526
527 if (!dbus_message_iter_close_container(&sub, &sub2))
528 goto oom;
529 }
530
531 if (!dbus_message_iter_close_container(&iter, &sub))
532 goto oom;
533
534 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
535 char *client;
536 Set *s;
537
538 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
539 if (!(s = set_new(string_hash_func, string_compare_func)))
540 goto oom;
541
542 if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
543 set_free(s);
544 goto oom;
545 }
546 }
547
548 if (!(client = strdup(message_get_sender_with_fallback(message))))
549 goto oom;
550
551 if ((r = set_put(s, client)) < 0) {
552 free(client);
553 return bus_send_error_reply(m, connection, message, NULL, r);
554 }
555
556 if (!(reply = dbus_message_new_method_return(message)))
557 goto oom;
558
559 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
560 char *client;
561
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);
565 }
566
567 free(client);
568
569 if (!(reply = dbus_message_new_method_return(message)))
570 goto oom;
571
572 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
573 FILE *f;
574 char *dump = NULL;
575 size_t size;
576
577 if (!(reply = dbus_message_new_method_return(message)))
578 goto oom;
579
580 if (!(f = open_memstream(&dump, &size)))
581 goto oom;
582
583 manager_dump_units(m, f, NULL);
584 manager_dump_jobs(m, f, NULL);
585
586 if (ferror(f)) {
587 fclose(f);
588 free(dump);
589 goto oom;
590 }
591
592 fclose(f);
593
594 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
595 free(dump);
596 goto oom;
597 }
598
599 free(dump);
600 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
601 const char *name;
602 dbus_bool_t cleanup;
603 Snapshot *s;
604
605 if (!dbus_message_get_args(
606 message,
607 &error,
608 DBUS_TYPE_STRING, &name,
609 DBUS_TYPE_BOOLEAN, &cleanup,
610 DBUS_TYPE_INVALID))
611 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
612
613 if (name && name[0] == 0)
614 name = NULL;
615
616 if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0)
617 return bus_send_error_reply(m, connection, message, &error, r);
618
619 if (!(reply = dbus_message_new_method_return(message)))
620 goto oom;
621
622 if (!(path = unit_dbus_path(UNIT(s))))
623 goto oom;
624
625 if (!dbus_message_append_args(
626 reply,
627 DBUS_TYPE_OBJECT_PATH, &path,
628 DBUS_TYPE_INVALID))
629 goto oom;
630
631 } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
632 char *introspection = NULL;
633 FILE *f;
634 Iterator i;
635 Unit *u;
636 Job *j;
637 const char *k;
638 size_t size;
639
640 if (!(reply = dbus_message_new_method_return(message)))
641 goto oom;
642
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
646 * dynamically. */
647
648 if (!(f = open_memstream(&introspection, &size)))
649 goto oom;
650
651 fputs(INTROSPECTION_BEGIN, f);
652
653 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
654 char *p;
655
656 if (k != u->meta.id)
657 continue;
658
659 if (!(p = bus_path_escape(k))) {
660 fclose(f);
661 free(introspection);
662 goto oom;
663 }
664
665 fprintf(f, "<node name=\"unit/%s\"/>", p);
666 free(p);
667 }
668
669 HASHMAP_FOREACH(j, m->jobs, i)
670 fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
671
672 fputs(INTROSPECTION_END, f);
673
674 if (ferror(f)) {
675 fclose(f);
676 free(introspection);
677 goto oom;
678 }
679
680 fclose(f);
681
682 if (!introspection)
683 goto oom;
684
685 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
686 free(introspection);
687 goto oom;
688 }
689
690 free(introspection);
691
692 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
693
694 assert(!m->queued_message);
695
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. */
700
701 if (!(m->queued_message = dbus_message_new_method_return(message)))
702 goto oom;
703
704 m->queued_message_connection = connection;
705 m->exit_code = MANAGER_RELOAD;
706
707 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
708
709 if (!(reply = dbus_message_new_method_return(message)))
710 goto oom;
711
712 m->exit_code = MANAGER_REEXECUTE;
713
714 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
715
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);
719 }
720
721 if (!(reply = dbus_message_new_method_return(message)))
722 goto oom;
723
724 m->exit_code = MANAGER_EXIT;
725
726 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
727 char **l = NULL, **e = NULL;
728
729 if ((r = bus_parse_strv(message, &l)) < 0) {
730 if (r == -ENOMEM)
731 goto oom;
732
733 return bus_send_error_reply(m, connection, message, NULL, r);
734 }
735
736 e = strv_env_merge(2, m->environment, l);
737 strv_free(l);
738
739 if (!e)
740 goto oom;
741
742 if (!(reply = dbus_message_new_method_return(message))) {
743 strv_free(e);
744 goto oom;
745 }
746
747 strv_free(m->environment);
748 m->environment = e;
749
750 } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
751 char **l = NULL, **e = NULL;
752
753 if ((r = bus_parse_strv(message, &l)) < 0) {
754 if (r == -ENOMEM)
755 goto oom;
756
757 return bus_send_error_reply(m, connection, message, NULL, r);
758 }
759
760 e = strv_env_delete(m->environment, 1, l);
761 strv_free(l);
762
763 if (!e)
764 goto oom;
765
766 if (!(reply = dbus_message_new_method_return(message)))
767 goto oom;
768
769 strv_free(m->environment);
770 m->environment = e;
771
772 } else
773 return bus_default_message_handler(m, connection, message, NULL, properties);
774
775 if (job_type != _JOB_TYPE_INVALID) {
776 const char *name, *smode;
777 JobMode mode;
778 Job *j;
779 Unit *u;
780
781 if (!dbus_message_get_args(
782 message,
783 &error,
784 DBUS_TYPE_STRING, &name,
785 DBUS_TYPE_STRING, &smode,
786 DBUS_TYPE_INVALID))
787 return bus_send_error_reply(m, connection, message, &error, -EINVAL);
788
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);
792 }
793
794 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
795 return bus_send_error_reply(m, connection, message, &error, r);
796
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;
802 }
803
804 if (job_type == JOB_START && u->meta.only_by_dependency) {
805 dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Unit may be activated by dependency only.");
806 return bus_send_error_reply(m, connection, message, &error, -EPERM);
807 }
808
809 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
810 return bus_send_error_reply(m, connection, message, &error, r);
811
812 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
813 goto oom;
814
815 j->bus = connection;
816
817 if (!(reply = dbus_message_new_method_return(message)))
818 goto oom;
819
820 if (!(path = job_dbus_path(j)))
821 goto oom;
822
823 if (!dbus_message_append_args(
824 reply,
825 DBUS_TYPE_OBJECT_PATH, &path,
826 DBUS_TYPE_INVALID))
827 goto oom;
828 }
829
830 free(path);
831
832 if (reply) {
833 if (!dbus_connection_send(connection, reply, NULL))
834 goto oom;
835
836 dbus_message_unref(reply);
837 }
838
839 return DBUS_HANDLER_RESULT_HANDLED;
840
841 oom:
842 free(path);
843
844 if (reply)
845 dbus_message_unref(reply);
846
847 dbus_error_free(&error);
848
849 return DBUS_HANDLER_RESULT_NEED_MEMORY;
850 }
851
852 const DBusObjectPathVTable bus_manager_vtable = {
853 .message_function = bus_manager_message_handler
854 };