chiark / gitweb /
cgroup: kill processes, not tasks and other cgroup changes
[elogind.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=\"GetJob\">\n"                                  \
66         "   <arg name=\"id\" type=\"u\" direction=\"in\"/>\n"           \
67         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>\n"         \
68         "  </method>\n"                                                 \
69         "  <method name=\"ClearJobs\"/>\n"                              \
70         "  <method name=\"ListUnits\">\n"                               \
71         "   <arg name=\"units\" type=\"a(sssssouso)\" direction=\"out\"/>\n" \
72         "  </method>\n"                                                 \
73         "  <method name=\"ListJobs\">\n"                                \
74         "   <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>\n" \
75         "  </method>\n"                                                 \
76         "  <method name=\"Subscribe\"/>\n"                              \
77         "  <method name=\"Unsubscribe\"/>\n"                            \
78         "  <method name=\"Dump\"/>\n"                                   \
79         "  <method name=\"CreateSnapshot\">\n"                          \
80         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>\n"         \
81         "   <arg nane=\"cleanup\" type=\"b\" direction=\"in\"/>\n"      \
82         "   <arg name=\"unit\" type=\"o\" direction=\"out\"/>\n"        \
83         "  </method>\n"                                                 \
84         "  <method name=\"Reload\"/>\n"                                 \
85         "  <method name=\"Reexecute\"/>\n"                              \
86         "  <method name=\"Exit\"/>\n"                                   \
87         "  <method name=\"SetEnvironment\">\n"                          \
88         "   <arg name=\"names\" type=\"as\" direction=\"in\"/>\n"       \
89         "  </method>\n"                                                 \
90         "  <method name=\"UnsetEnvironment\">\n"                        \
91         "   <arg name=\"names\" type=\"as\" direction=\"in\"/>\n"       \
92         "  </method>\n"                                                 \
93         "  <signal name=\"UnitNew\">\n"                                 \
94         "   <arg name=\"id\" type=\"s\"/>\n"                            \
95         "   <arg name=\"unit\" type=\"o\"/>\n"                          \
96         "  </signal>\n"                                                 \
97         "  <signal name=\"UnitRemoved\">\n"                             \
98         "   <arg name=\"id\" type=\"s\"/>\n"                            \
99         "   <arg name=\"unit\" type=\"o\"/>\n"                          \
100         "  </signal>\n"                                                 \
101         "  <signal name=\"JobNew\">\n"                                  \
102         "   <arg name=\"id\" type=\"u\"/>\n"                            \
103         "   <arg name=\"job\" type=\"o\"/>\n"                           \
104         "  </signal>\n"                                                 \
105         "  <signal name=\"JobRemoved\">\n"                              \
106         "   <arg name=\"id\" type=\"u\"/>\n"                            \
107         "   <arg name=\"job\" type=\"o\"/>\n"                           \
108         "   <arg name=\"success\" type=\"b\"/>\n"                       \
109         "  </signal>"                                                   \
110         "  <property name=\"Version\" type=\"s\" access=\"read\"/>\n"   \
111         "  <property name=\"RunningAs\" type=\"s\" access=\"read\"/>\n" \
112         "  <property name=\"StartupTimestamp\" type=\"t\" access=\"read\"/>\n" \
113         "  <property name=\"LogLevel\" type=\"s\" access=\"read\"/>\n"  \
114         "  <property name=\"LogTarget\" type=\"s\" access=\"read\"/>\n" \
115         "  <property name=\"NNames\" type=\"u\" access=\"read\"/>\n"    \
116         "  <property name=\"NJobs\" type=\"u\" access=\"read\"/>\n"     \
117         "  <property name=\"Environment\" type=\"as\" access=\"read\"/>\n" \
118         "  <property name=\"ConfirmSpawn\" type=\"b\" access=\"read\"/>\n" \
119         "  <property name=\"ShowStatus\" type=\"b\" access=\"read\"/>\n" \
120         "  <property name=\"UnitPath\" type=\"as\" access=\"read\"/>\n" \
121         "  <property name=\"SysVInitPath\" type=\"as\" access=\"read\"/>\n" \
122         "  <property name=\"SysVRcndPath\" type=\"as\" access=\"read\"/>\n" \
123         "  <property name=\"NotifySocket\" type=\"s\" access=\"read\"/>\n" \
124         "  <property name=\"ControlGroupHierarchy\" type=\"s\" access=\"read\"/>\n" \
125         " </interface>\n"
126
127 #define INTROSPECTION_BEGIN                                             \
128         DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                       \
129         "<node>\n"                                                      \
130         BUS_MANAGER_INTERFACE                                           \
131         BUS_PROPERTIES_INTERFACE                                        \
132         BUS_INTROSPECTABLE_INTERFACE
133
134 #define INTROSPECTION_END                                               \
135         "</node>\n"
136
137 const char bus_manager_interface[] = BUS_MANAGER_INTERFACE;
138
139 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
140
141 static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
142         const char *t;
143
144         assert(m);
145         assert(i);
146         assert(property);
147
148         t = log_target_to_string(log_get_target());
149
150         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
151                 return -ENOMEM;
152
153         return 0;
154 }
155
156 static int bus_manager_append_log_level(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_level_to_string(log_get_max_level());
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_n_names(Manager *m, DBusMessageIter *i, const char *property, void *data) {
172         uint32_t u;
173
174         assert(m);
175         assert(i);
176         assert(property);
177
178         u = hashmap_size(m->units);
179
180         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
181                 return -ENOMEM;
182
183         return 0;
184 }
185
186 static int bus_manager_append_n_jobs(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->jobs);
194
195         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, &u))
196                 return -ENOMEM;
197
198         return 0;
199 }
200
201 static const char *message_get_sender_with_fallback(DBusMessage *m) {
202         const char *s;
203
204         assert(m);
205
206         if ((s = dbus_message_get_sender(m)))
207                 return s;
208
209         /* When the message came in from a direct connection the
210          * message will have no sender. We fix that here. */
211
212         return ":no-sender";
213 }
214
215 static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection, DBusMessage *message, void *data) {
216         Manager *m = data;
217
218         const BusProperty properties[] = {
219                 { "org.freedesktop.systemd1.Manager", "Version",       bus_property_append_string,    "s",  PACKAGE_STRING     },
220                 { "org.freedesktop.systemd1.Manager", "RunningAs",     bus_manager_append_running_as, "s",  &m->running_as     },
221                 { "org.freedesktop.systemd1.Manager", "StartupTimestamp", bus_property_append_uint64, "t",  &m->startup_timestamp.realtime },
222                 { "org.freedesktop.systemd1.Manager", "LogLevel",      bus_manager_append_log_level,  "s",  NULL               },
223                 { "org.freedesktop.systemd1.Manager", "LogTarget",     bus_manager_append_log_target, "s",  NULL               },
224                 { "org.freedesktop.systemd1.Manager", "NNames",        bus_manager_append_n_names,    "u",  NULL               },
225                 { "org.freedesktop.systemd1.Manager", "NJobs",         bus_manager_append_n_jobs,     "u",  NULL               },
226                 { "org.freedesktop.systemd1.Manager", "Environment",   bus_property_append_strv,      "as", m->environment     },
227                 { "org.freedesktop.systemd1.Manager", "ConfirmSpawn",  bus_property_append_bool,      "b",  &m->confirm_spawn  },
228                 { "org.freedesktop.systemd1.Manager", "ShowStatus",    bus_property_append_bool,      "b",  &m->show_status    },
229                 { "org.freedesktop.systemd1.Manager", "UnitPath",      bus_property_append_strv,      "as", m->lookup_paths.unit_path },
230                 { "org.freedesktop.systemd1.Manager", "SysVInitPath",  bus_property_append_strv,      "as", m->lookup_paths.sysvinit_path },
231                 { "org.freedesktop.systemd1.Manager", "SysVRcndPath",  bus_property_append_strv,      "as", m->lookup_paths.sysvrcnd_path },
232                 { "org.freedesktop.systemd1.Manager", "NotifySocket",  bus_property_append_string,    "s",  m->notify_socket   },
233                 { "org.freedesktop.systemd1.Manager", "ControlGroupHierarchy", bus_property_append_string, "s", m->cgroup_hierarchy },
234                 { NULL, NULL, NULL, NULL, NULL }
235         };
236
237         int r;
238         DBusError error;
239         DBusMessage *reply = NULL;
240         char * path = NULL;
241         JobType job_type = _JOB_TYPE_INVALID;
242
243         assert(connection);
244         assert(message);
245         assert(m);
246
247         dbus_error_init(&error);
248
249         if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
250                 const char *name;
251                 Unit *u;
252
253                 if (!dbus_message_get_args(
254                                     message,
255                                     &error,
256                                     DBUS_TYPE_STRING, &name,
257                                     DBUS_TYPE_INVALID))
258                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
259
260                 if (!(u = manager_get_unit(m, name))) {
261                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s is not loaded.", name);
262                         return bus_send_error_reply(m, connection, message, &error, -ENOENT);
263                 }
264
265                 if (!(reply = dbus_message_new_method_return(message)))
266                         goto oom;
267
268                 if (!(path = unit_dbus_path(u)))
269                         goto oom;
270
271                 if (!dbus_message_append_args(
272                                     reply,
273                                     DBUS_TYPE_OBJECT_PATH, &path,
274                                     DBUS_TYPE_INVALID))
275                         goto oom;
276
277         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
278                 const char *name;
279                 Unit *u;
280
281                 if (!dbus_message_get_args(
282                                     message,
283                                     &error,
284                                     DBUS_TYPE_STRING, &name,
285                                     DBUS_TYPE_INVALID))
286                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
287
288                 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
289                         return bus_send_error_reply(m, connection, message, &error, r);
290
291                 if (!(reply = dbus_message_new_method_return(message)))
292                         goto oom;
293
294                 if (!(path = unit_dbus_path(u)))
295                         goto oom;
296
297                 if (!dbus_message_append_args(
298                                     reply,
299                                     DBUS_TYPE_OBJECT_PATH, &path,
300                                     DBUS_TYPE_INVALID))
301                         goto oom;
302
303         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StartUnit"))
304                 job_type = JOB_START;
305         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "StopUnit"))
306                 job_type = JOB_STOP;
307         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ReloadUnit"))
308                 job_type = JOB_RELOAD;
309         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "RestartUnit"))
310                 job_type = JOB_RESTART;
311         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "TryRestartUnit"))
312                 job_type = JOB_TRY_RESTART;
313         else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetJob")) {
314                 uint32_t id;
315                 Job *j;
316
317                 if (!dbus_message_get_args(
318                                     message,
319                                     &error,
320                                     DBUS_TYPE_UINT32, &id,
321                                     DBUS_TYPE_INVALID))
322                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
323
324                 if (!(j = manager_get_job(m, id))) {
325                         dbus_set_error(&error, BUS_ERROR_NO_SUCH_JOB, "Job %u does not exist.", (unsigned) id);
326                         return bus_send_error_reply(m, connection, message, &error, -ENOENT);
327                 }
328
329                 if (!(reply = dbus_message_new_method_return(message)))
330                         goto oom;
331
332                 if (!(path = job_dbus_path(j)))
333                         goto oom;
334
335                 if (!dbus_message_append_args(
336                                     reply,
337                                     DBUS_TYPE_OBJECT_PATH, &path,
338                                     DBUS_TYPE_INVALID))
339                         goto oom;
340
341         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
342
343                 manager_clear_jobs(m);
344
345                 if (!(reply = dbus_message_new_method_return(message)))
346                         goto oom;
347
348         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
349                 DBusMessageIter iter, sub;
350                 Iterator i;
351                 Unit *u;
352                 const char *k;
353
354                 if (!(reply = dbus_message_new_method_return(message)))
355                         goto oom;
356
357                 dbus_message_iter_init_append(reply, &iter);
358
359                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(sssssouso)", &sub))
360                         goto oom;
361
362                 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
363                         char *u_path, *j_path;
364                         const char *description, *load_state, *active_state, *sub_state, *sjob_type;
365                         DBusMessageIter sub2;
366                         uint32_t job_id;
367
368                         if (k != u->meta.id)
369                                 continue;
370
371                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
372                                 goto oom;
373
374                         description = unit_description(u);
375                         load_state = unit_load_state_to_string(u->meta.load_state);
376                         active_state = unit_active_state_to_string(unit_active_state(u));
377                         sub_state = unit_sub_state_to_string(u);
378
379                         if (!(u_path = unit_dbus_path(u)))
380                                 goto oom;
381
382                         if (u->meta.job) {
383                                 job_id = (uint32_t) u->meta.job->id;
384
385                                 if (!(j_path = job_dbus_path(u->meta.job))) {
386                                         free(u_path);
387                                         goto oom;
388                                 }
389
390                                 sjob_type = job_type_to_string(u->meta.job->type);
391                         } else {
392                                 job_id = 0;
393                                 j_path = u_path;
394                                 sjob_type = "";
395                         }
396
397                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
398                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
399                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
400                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
401                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
402                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
403                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
404                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sjob_type) ||
405                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
406                                 free(u_path);
407                                 if (u->meta.job)
408                                         free(j_path);
409                                 goto oom;
410                         }
411
412                         free(u_path);
413                         if (u->meta.job)
414                                 free(j_path);
415
416                         if (!dbus_message_iter_close_container(&sub, &sub2))
417                                 goto oom;
418                 }
419
420                 if (!dbus_message_iter_close_container(&iter, &sub))
421                         goto oom;
422
423         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
424                 DBusMessageIter iter, sub;
425                 Iterator i;
426                 Job *j;
427
428                 if (!(reply = dbus_message_new_method_return(message)))
429                         goto oom;
430
431                 dbus_message_iter_init_append(reply, &iter);
432
433                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
434                         goto oom;
435
436                 HASHMAP_FOREACH(j, m->jobs, i) {
437                         char *u_path, *j_path;
438                         const char *state, *type;
439                         uint32_t id;
440                         DBusMessageIter sub2;
441
442                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
443                                 goto oom;
444
445                         id = (uint32_t) j->id;
446                         state = job_state_to_string(j->state);
447                         type = job_type_to_string(j->type);
448
449                         if (!(j_path = job_dbus_path(j)))
450                                 goto oom;
451
452                         if (!(u_path = unit_dbus_path(j->unit))) {
453                                 free(j_path);
454                                 goto oom;
455                         }
456
457                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
458                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
459                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
460                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
461                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
462                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
463                                 free(j_path);
464                                 free(u_path);
465                                 goto oom;
466                         }
467
468                         free(j_path);
469                         free(u_path);
470
471                         if (!dbus_message_iter_close_container(&sub, &sub2))
472                                 goto oom;
473                 }
474
475                 if (!dbus_message_iter_close_container(&iter, &sub))
476                         goto oom;
477
478         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
479                 char *client;
480                 Set *s;
481
482                 if (!(s = BUS_CONNECTION_SUBSCRIBED(m, connection))) {
483                         if (!(s = set_new(string_hash_func, string_compare_func)))
484                                 goto oom;
485
486                         if (!(dbus_connection_set_data(connection, m->subscribed_data_slot, s, NULL))) {
487                                 set_free(s);
488                                 goto oom;
489                         }
490                 }
491
492                 if (!(client = strdup(message_get_sender_with_fallback(message))))
493                         goto oom;
494
495                 if ((r = set_put(s, client)) < 0) {
496                         free(client);
497                         return bus_send_error_reply(m, connection, message, NULL, r);
498                 }
499
500                 if (!(reply = dbus_message_new_method_return(message)))
501                         goto oom;
502
503         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
504                 char *client;
505
506                 if (!(client = set_remove(BUS_CONNECTION_SUBSCRIBED(m, connection), (char*) message_get_sender_with_fallback(message)))) {
507                         dbus_set_error(&error, BUS_ERROR_NOT_SUBSCRIBED, "Client is not subscribed.");
508                         return bus_send_error_reply(m, connection, message, &error, -ENOENT);
509                 }
510
511                 free(client);
512
513                 if (!(reply = dbus_message_new_method_return(message)))
514                         goto oom;
515
516         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
517                 FILE *f;
518                 char *dump = NULL;
519                 size_t size;
520
521                 if (!(reply = dbus_message_new_method_return(message)))
522                         goto oom;
523
524                 if (!(f = open_memstream(&dump, &size)))
525                         goto oom;
526
527                 manager_dump_units(m, f, NULL);
528                 manager_dump_jobs(m, f, NULL);
529
530                 if (ferror(f)) {
531                         fclose(f);
532                         free(dump);
533                         goto oom;
534                 }
535
536                 fclose(f);
537
538                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
539                         free(dump);
540                         goto oom;
541                 }
542
543                 free(dump);
544         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
545                 const char *name;
546                 dbus_bool_t cleanup;
547                 Snapshot *s;
548
549                 if (!dbus_message_get_args(
550                                     message,
551                                     &error,
552                                     DBUS_TYPE_STRING, &name,
553                                     DBUS_TYPE_BOOLEAN, &cleanup,
554                                     DBUS_TYPE_INVALID))
555                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
556
557                 if (name && name[0] == 0)
558                         name = NULL;
559
560                 if ((r = snapshot_create(m, name, cleanup, &error, &s)) < 0)
561                         return bus_send_error_reply(m, connection, message, &error, r);
562
563                 if (!(reply = dbus_message_new_method_return(message)))
564                         goto oom;
565
566                 if (!(path = unit_dbus_path(UNIT(s))))
567                         goto oom;
568
569                 if (!dbus_message_append_args(
570                                     reply,
571                                     DBUS_TYPE_OBJECT_PATH, &path,
572                                     DBUS_TYPE_INVALID))
573                         goto oom;
574
575         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
576                 char *introspection = NULL;
577                 FILE *f;
578                 Iterator i;
579                 Unit *u;
580                 Job *j;
581                 const char *k;
582                 size_t size;
583
584                 if (!(reply = dbus_message_new_method_return(message)))
585                         goto oom;
586
587                 /* We roll our own introspection code here, instead of
588                  * relying on bus_default_message_handler() because we
589                  * need to generate our introspection string
590                  * dynamically. */
591
592                 if (!(f = open_memstream(&introspection, &size)))
593                         goto oom;
594
595                 fputs(INTROSPECTION_BEGIN, f);
596
597                 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
598                         char *p;
599
600                         if (k != u->meta.id)
601                                 continue;
602
603                         if (!(p = bus_path_escape(k))) {
604                                 fclose(f);
605                                 free(introspection);
606                                 goto oom;
607                         }
608
609                         fprintf(f, "<node name=\"unit/%s\"/>", p);
610                         free(p);
611                 }
612
613                 HASHMAP_FOREACH(j, m->jobs, i)
614                         fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
615
616                 fputs(INTROSPECTION_END, f);
617
618                 if (ferror(f)) {
619                         fclose(f);
620                         free(introspection);
621                         goto oom;
622                 }
623
624                 fclose(f);
625
626                 if (!introspection)
627                         goto oom;
628
629                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
630                         free(introspection);
631                         goto oom;
632                 }
633
634                 free(introspection);
635
636         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reload")) {
637
638                 assert(!m->queued_message);
639
640                 /* Instead of sending the reply back right away, we
641                  * just remember that we need to and then send it
642                  * after the reload is finished. That way the caller
643                  * knows when the reload finished. */
644
645                 if (!(m->queued_message = dbus_message_new_method_return(message)))
646                         goto oom;
647
648                 m->queued_message_connection = connection;
649                 m->exit_code = MANAGER_RELOAD;
650
651         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Reexecute")) {
652
653                 if (!(reply = dbus_message_new_method_return(message)))
654                         goto oom;
655
656                 m->exit_code = MANAGER_REEXECUTE;
657
658         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Exit")) {
659
660                 if (m->running_as == MANAGER_SYSTEM) {
661                         dbus_set_error(&error, BUS_ERROR_NOT_SUPPORTED, "Exit is only supported for session managers.");
662                         return bus_send_error_reply(m, connection, message, &error, -ENOTSUP);
663                 }
664
665                 if (!(reply = dbus_message_new_method_return(message)))
666                         goto oom;
667
668                 m->exit_code = MANAGER_EXIT;
669
670         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SetEnvironment")) {
671                 char **l = NULL, **e = NULL;
672
673                 if ((r = bus_parse_strv(message, &l)) < 0) {
674                         if (r == -ENOMEM)
675                                 goto oom;
676
677                         return bus_send_error_reply(m, connection, message, NULL, r);
678                 }
679
680                 e = strv_env_merge(2, m->environment, l);
681                 strv_free(l);
682
683                 if (!e)
684                         goto oom;
685
686                 if (!(reply = dbus_message_new_method_return(message))) {
687                         strv_free(e);
688                         goto oom;
689                 }
690
691                 strv_free(m->environment);
692                 m->environment = e;
693
694         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "UnsetEnvironment")) {
695                 char **l = NULL, **e = NULL;
696
697                 if ((r = bus_parse_strv(message, &l)) < 0) {
698                         if (r == -ENOMEM)
699                                 goto oom;
700
701                         return bus_send_error_reply(m, connection, message, NULL, r);
702                 }
703
704                 e = strv_env_delete(m->environment, 1, l);
705                 strv_free(l);
706
707                 if (!e)
708                         goto oom;
709
710                 if (!(reply = dbus_message_new_method_return(message)))
711                         goto oom;
712
713                 strv_free(m->environment);
714                 m->environment = e;
715
716         } else
717                 return bus_default_message_handler(m, connection, message, NULL, properties);
718
719
720         if (job_type != _JOB_TYPE_INVALID) {
721                 const char *name, *smode;
722                 JobMode mode;
723                 Job *j;
724                 Unit *u;
725
726                 if (!dbus_message_get_args(
727                                     message,
728                                     &error,
729                                     DBUS_TYPE_STRING, &name,
730                                     DBUS_TYPE_STRING, &smode,
731                                     DBUS_TYPE_INVALID))
732                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
733
734                 if ((mode = job_mode_from_string(smode)) == _JOB_MODE_INVALID) {
735                         dbus_set_error(&error, BUS_ERROR_INVALID_JOB_MODE, "Job mode %s is invalid.", smode);
736                         return bus_send_error_reply(m, connection, message, &error, -EINVAL);
737                 }
738
739                 if ((r = manager_load_unit(m, name, NULL, &error, &u)) < 0)
740                         return bus_send_error_reply(m, connection, message, &error, r);
741
742                 if (job_type == JOB_START && u->meta.only_by_dependency) {
743                         dbus_set_error(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Unit may be activated by dependency only.");
744                         return bus_send_error_reply(m, connection, message, &error, -EPERM);
745                 }
746
747                 if ((r = manager_add_job(m, job_type, u, mode, true, &error, &j)) < 0)
748                         return bus_send_error_reply(m, connection, message, &error, r);
749
750                 if (!(j->bus_client = strdup(message_get_sender_with_fallback(message))))
751                         goto oom;
752
753                 j->bus = connection;
754
755                 if (!(reply = dbus_message_new_method_return(message)))
756                         goto oom;
757
758                 if (!(path = job_dbus_path(j)))
759                         goto oom;
760
761                 if (!dbus_message_append_args(
762                                     reply,
763                                     DBUS_TYPE_OBJECT_PATH, &path,
764                                     DBUS_TYPE_INVALID))
765                         goto oom;
766         }
767
768         free(path);
769
770         if (reply) {
771                 if (!dbus_connection_send(connection, reply, NULL))
772                         goto oom;
773
774                 dbus_message_unref(reply);
775         }
776
777         return DBUS_HANDLER_RESULT_HANDLED;
778
779 oom:
780         free(path);
781
782         if (reply)
783                 dbus_message_unref(reply);
784
785         dbus_error_free(&error);
786
787         return DBUS_HANDLER_RESULT_NEED_MEMORY;
788 }
789
790 const DBusObjectPathVTable bus_manager_vtable = {
791         .message_function = bus_manager_message_handler
792 };