chiark / gitweb /
cgroup: if we are already in our own cgroup, then reuse it
[elogind.git] / 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
28 #define INTROSPECTION_BEGIN                                             \
29         DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                       \
30         "<node>"                                                        \
31         " <interface name=\"org.freedesktop.systemd1.Manager\">"        \
32         "  <method name=\"GetUnit\">"                                   \
33         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>"           \
34         "   <arg name=\"unit\" type=\"o\" direction=\"out\"/>"          \
35         "  </method>"                                                   \
36         "  <method name=\"LoadUnit\">"                                  \
37         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>"           \
38         "   <arg name=\"unit\" type=\"o\" direction=\"out\"/>"          \
39         "  </method>"                                                   \
40         "  <method name=\"GetJob\">"                                    \
41         "   <arg name=\"id\" type=\"u\" direction=\"in\"/>"             \
42         "   <arg name=\"job\" type=\"o\" direction=\"out\"/>"           \
43         "  </method>"                                                   \
44         "  <method name=\"ClearJobs\"/>"                                \
45         "  <method name=\"ListUnits\">"                                 \
46         "   <arg name=\"units\" type=\"a(sssssouso)\" direction=\"out\"/>" \
47         "  </method>"                                                   \
48         "  <method name=\"ListJobs\">"                                  \
49         "   <arg name=\"jobs\" type=\"a(usssoo)\" direction=\"out\"/>"  \
50         "  </method>"                                                   \
51         "  <method name=\"Subscribe\"/>"                                \
52         "  <method name=\"Unsubscribe\"/>"                              \
53         "  <method name=\"Dump\"/>"                                     \
54         "  <method name=\"CreateSnapshot\">"                            \
55         "   <arg name=\"name\" type=\"s\" direction=\"in\"/>"           \
56         "   <arg nane=\"cleanup\" type=\"b\" direction=\"in\"/>"        \
57         "   <arg name=\"unit\" type=\"o\" direction=\"out\"/>"          \
58         "  </method>"                                                   \
59         "  <signal name=\"UnitNew\">"                                   \
60         "   <arg name=\"id\" type=\"s\"/>"                              \
61         "   <arg name=\"unit\" type=\"o\"/>"                            \
62         "  </signal>"                                                   \
63         "  <signal name=\"UnitRemoved\">"                               \
64         "   <arg name=\"id\" type=\"s\"/>"                              \
65         "   <arg name=\"unit\" type=\"o\"/>"                            \
66         "  </signal>"                                                   \
67         "  <signal name=\"JobNew\">"                                    \
68         "   <arg name=\"id\" type=\"u\"/>"                              \
69         "   <arg name=\"job\" type=\"o\"/>"                             \
70         "  </signal>"                                                   \
71         "  <signal name=\"JobRemoved\">"                                \
72         "   <arg name=\"id\" type=\"u\"/>"                              \
73         "   <arg name=\"job\" type=\"o\"/>"                             \
74         "  </signal>"                                                   \
75         "  <property name=\"Version\" type=\"s\" access=\"read\"/>"     \
76         "  <property name=\"RunningAs\" type=\"s\" access=\"read\"/>"   \
77         "  <property name=\"BootTimestamp\" type=\"t\" access=\"read\"/>" \
78         "  <property name=\"LogLevel\" type=\"s\" access=\"readwrite\"/>" \
79         "  <property name=\"LogTarget\" type=\"s\" access=\"readwrite\"/>" \
80         " </interface>"                                                 \
81         BUS_PROPERTIES_INTERFACE                                        \
82         BUS_INTROSPECTABLE_INTERFACE
83
84 #define INTROSPECTION_END                                               \
85         "</node>"
86
87 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_manager_append_running_as, manager_running_as, ManagerRunningAs);
88
89 static int bus_manager_append_log_target(Manager *m, DBusMessageIter *i, const char *property, void *data) {
90         const char *t;
91
92         assert(m);
93         assert(i);
94         assert(property);
95
96         t = log_target_to_string(log_get_target());
97
98         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
99                 return -ENOMEM;
100
101         return 0;
102 }
103
104 static int bus_manager_append_log_level(Manager *m, DBusMessageIter *i, const char *property, void *data) {
105         const char *t;
106
107         assert(m);
108         assert(i);
109         assert(property);
110
111         t = log_level_to_string(log_get_max_level());
112
113         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
114                 return -ENOMEM;
115
116         return 0;
117 }
118
119 static DBusHandlerResult bus_manager_message_handler(DBusConnection  *connection, DBusMessage *message, void *data) {
120         Manager *m = data;
121
122         const BusProperty properties[] = {
123                 { "org.freedesktop.systemd1.Manager", "Version",       bus_property_append_string,    "s", PACKAGE_STRING     },
124                 { "org.freedesktop.systemd1.Manager", "RunningAs",     bus_manager_append_running_as, "s", &m->running_as     },
125                 { "org.freedesktop.systemd1.Manager", "BootTimestamp", bus_property_append_uint64,    "t", &m->boot_timestamp },
126                 { "org.freedesktop.systemd1.Manager", "LogLevel",      bus_manager_append_log_level,  "s", NULL               },
127                 { "org.freedesktop.systemd1.Manager", "LogTarget",     bus_manager_append_log_target, "s", NULL               },
128                 { NULL, NULL, NULL, NULL, NULL }
129         };
130
131         int r;
132         DBusError error;
133         DBusMessage *reply = NULL;
134         char * path = NULL;
135
136         assert(connection);
137         assert(message);
138         assert(m);
139
140         dbus_error_init(&error);
141
142         log_debug("Got D-Bus request: %s.%s() on %s",
143                   dbus_message_get_interface(message),
144                   dbus_message_get_member(message),
145                   dbus_message_get_path(message));
146
147         if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "GetUnit")) {
148                 const char *name;
149                 Unit *u;
150
151                 if (!dbus_message_get_args(
152                                     message,
153                                     &error,
154                                     DBUS_TYPE_STRING, &name,
155                                     DBUS_TYPE_INVALID))
156                         return bus_send_error_reply(m, message, &error, -EINVAL);
157
158                 if (!(u = manager_get_unit(m, name)))
159                         return bus_send_error_reply(m, message, NULL, -ENOENT);
160
161                 if (!(reply = dbus_message_new_method_return(message)))
162                         goto oom;
163
164                 if (!(path = unit_dbus_path(u)))
165                         goto oom;
166
167                 if (!dbus_message_append_args(
168                                     reply,
169                                     DBUS_TYPE_OBJECT_PATH, &path,
170                                     DBUS_TYPE_INVALID))
171                         goto oom;
172
173         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "LoadUnit")) {
174                 const char *name;
175                 Unit *u;
176
177                 if (!dbus_message_get_args(
178                                     message,
179                                     &error,
180                                     DBUS_TYPE_STRING, &name,
181                                     DBUS_TYPE_INVALID))
182                         return bus_send_error_reply(m, message, &error, -EINVAL);
183
184                 if ((r = manager_load_unit(m, name, NULL, &u)) < 0)
185                         return bus_send_error_reply(m, message, NULL, r);
186
187                 if (!(reply = dbus_message_new_method_return(message)))
188                         goto oom;
189
190                 if (!(path = unit_dbus_path(u)))
191                         goto oom;
192
193                 if (!dbus_message_append_args(
194                                     reply,
195                                     DBUS_TYPE_OBJECT_PATH, &path,
196                                     DBUS_TYPE_INVALID))
197                         goto oom;
198
199         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1", "GetJob")) {
200                 uint32_t id;
201                 Job *j;
202
203                 if (!dbus_message_get_args(
204                                     message,
205                                     &error,
206                                     DBUS_TYPE_UINT32, &id,
207                                     DBUS_TYPE_INVALID))
208                         return bus_send_error_reply(m, message, &error, -EINVAL);
209
210                 if (!(j = manager_get_job(m, id)))
211                         return bus_send_error_reply(m, message, NULL, -ENOENT);
212
213                 if (!(reply = dbus_message_new_method_return(message)))
214                         goto oom;
215
216                 if (!(path = job_dbus_path(j)))
217                         goto oom;
218
219                 if (!dbus_message_append_args(
220                                     reply,
221                                     DBUS_TYPE_OBJECT_PATH, &path,
222                                     DBUS_TYPE_INVALID))
223                         goto oom;
224
225         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ClearJobs")) {
226
227                 manager_clear_jobs(m);
228
229                 if (!(reply = dbus_message_new_method_return(message)))
230                         goto oom;
231
232         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListUnits")) {
233                 DBusMessageIter iter, sub;
234                 Iterator i;
235                 Unit *u;
236                 const char *k;
237
238                 if (!(reply = dbus_message_new_method_return(message)))
239                         goto oom;
240
241                 dbus_message_iter_init_append(reply, &iter);
242
243                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(sssssouso)", &sub))
244                         goto oom;
245
246                 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
247                         char *u_path, *j_path;
248                         const char *description, *load_state, *active_state, *sub_state, *job_type;
249                         DBusMessageIter sub2;
250                         uint32_t job_id;
251
252                         if (k != u->meta.id)
253                                 continue;
254
255                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
256                                 goto oom;
257
258                         description = unit_description(u);
259                         load_state = unit_load_state_to_string(u->meta.load_state);
260                         active_state = unit_active_state_to_string(unit_active_state(u));
261                         sub_state = unit_sub_state_to_string(u);
262
263                         if (!(u_path = unit_dbus_path(u)))
264                                 goto oom;
265
266                         if (u->meta.job) {
267                                 job_id = (uint32_t) u->meta.job->id;
268
269                                 if (!(j_path = job_dbus_path(u->meta.job))) {
270                                         free(u_path);
271                                         goto oom;
272                                 }
273
274                                 job_type = job_type_to_string(u->meta.job->type);
275                         } else {
276                                 job_id = 0;
277                                 j_path = u_path;
278                                 job_type = "";
279                         }
280
281                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &u->meta.id) ||
282                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &description) ||
283                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &load_state) ||
284                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &active_state) ||
285                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &sub_state) ||
286                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path) ||
287                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &job_id) ||
288                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &job_type) ||
289                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path)) {
290                                 free(u_path);
291                                 if (u->meta.job)
292                                         free(j_path);
293                                 goto oom;
294                         }
295
296                         free(u_path);
297                         if (u->meta.job)
298                                 free(j_path);
299
300                         if (!dbus_message_iter_close_container(&sub, &sub2))
301                                 goto oom;
302                 }
303
304                 if (!dbus_message_iter_close_container(&iter, &sub))
305                         goto oom;
306
307         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "ListJobs")) {
308                 DBusMessageIter iter, sub;
309                 Iterator i;
310                 Job *j;
311
312                 if (!(reply = dbus_message_new_method_return(message)))
313                         goto oom;
314
315                 dbus_message_iter_init_append(reply, &iter);
316
317                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "(usssoo)", &sub))
318                         goto oom;
319
320                 HASHMAP_FOREACH(j, m->jobs, i) {
321                         char *u_path, *j_path;
322                         const char *state, *type;
323                         uint32_t id;
324                         DBusMessageIter sub2;
325
326                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_STRUCT, NULL, &sub2))
327                                 goto oom;
328
329                         id = (uint32_t) j->id;
330                         state = job_state_to_string(j->state);
331                         type = job_type_to_string(j->type);
332
333                         if (!(j_path = job_dbus_path(j)))
334                                 goto oom;
335
336                         if (!(u_path = unit_dbus_path(j->unit))) {
337                                 free(j_path);
338                                 goto oom;
339                         }
340
341                         if (!dbus_message_iter_append_basic(&sub2, DBUS_TYPE_UINT32, &id) ||
342                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &j->unit->meta.id) ||
343                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &type) ||
344                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &state) ||
345                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &j_path) ||
346                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_OBJECT_PATH, &u_path)) {
347                                 free(j_path);
348                                 free(u_path);
349                                 goto oom;
350                         }
351
352                         free(j_path);
353                         free(u_path);
354
355                         if (!dbus_message_iter_close_container(&sub, &sub2))
356                                 goto oom;
357                 }
358
359                 if (!dbus_message_iter_close_container(&iter, &sub))
360                         goto oom;
361
362         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Subscribe")) {
363                 char *client;
364
365                 if (!(client = strdup(dbus_message_get_sender(message))))
366                         goto oom;
367
368                 r = set_put(m->subscribed, client);
369
370                 if (r < 0)
371                         return bus_send_error_reply(m, message, NULL, r);
372
373                 if (!(reply = dbus_message_new_method_return(message)))
374                         goto oom;
375
376         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Unsubscribe")) {
377                 char *client;
378
379                 if (!(client = set_remove(m->subscribed, (char*) dbus_message_get_sender(message))))
380                         return bus_send_error_reply(m, message, NULL, -ENOENT);
381
382                 free(client);
383
384                 if (!(reply = dbus_message_new_method_return(message)))
385                         goto oom;
386
387         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "Dump")) {
388                 FILE *f;
389                 char *dump = NULL;
390                 size_t size;
391
392                 if (!(reply = dbus_message_new_method_return(message)))
393                         goto oom;
394
395                 if (!(f = open_memstream(&dump, &size)))
396                         goto oom;
397
398                 manager_dump_units(m, f, NULL);
399                 manager_dump_jobs(m, f, NULL);
400
401                 if (ferror(f)) {
402                         fclose(f);
403                         free(dump);
404                         goto oom;
405                 }
406
407                 fclose(f);
408
409                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &dump, DBUS_TYPE_INVALID)) {
410                         free(dump);
411                         goto oom;
412                 }
413
414                 free(dump);
415         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "CreateSnapshot")) {
416                 const char *name;
417                 dbus_bool_t cleanup;
418                 Snapshot *s;
419
420                 if (!dbus_message_get_args(
421                                     message,
422                                     &error,
423                                     DBUS_TYPE_STRING, &name,
424                                     DBUS_TYPE_BOOLEAN, &cleanup,
425                                     DBUS_TYPE_INVALID))
426                         return bus_send_error_reply(m, message, &error, -EINVAL);
427
428                 if (name && name[0] == 0)
429                         name = NULL;
430
431                 if ((r = snapshot_create(m, name, cleanup, &s)) < 0)
432                         return bus_send_error_reply(m, message, NULL, r);
433
434                 if (!(reply = dbus_message_new_method_return(message)))
435                         goto oom;
436
437                 if (!(path = unit_dbus_path(UNIT(s))))
438                         goto oom;
439
440                 if (!dbus_message_append_args(
441                                     reply,
442                                     DBUS_TYPE_OBJECT_PATH, &path,
443                                     DBUS_TYPE_INVALID))
444                         goto oom;
445
446         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect")) {
447                 char *introspection = NULL;
448                 FILE *f;
449                 Iterator i;
450                 Unit *u;
451                 Job *j;
452                 const char *k;
453                 size_t size;
454
455                 if (!(reply = dbus_message_new_method_return(message)))
456                         goto oom;
457
458                 /* We roll our own introspection code here, instead of
459                  * relying on bus_default_message_handler() because we
460                  * need to generate our introspection string
461                  * dynamically. */
462
463                 if (!(f = open_memstream(&introspection, &size)))
464                         goto oom;
465
466                 fputs(INTROSPECTION_BEGIN, f);
467
468                 HASHMAP_FOREACH_KEY(u, k, m->units, i) {
469                         char *p;
470
471                         if (k != u->meta.id)
472                                 continue;
473
474                         if (!(p = bus_path_escape(k))) {
475                                 fclose(f);
476                                 free(introspection);
477                                 goto oom;
478                         }
479
480                         fprintf(f, "<node name=\"unit/%s\"/>", p);
481                         free(p);
482                 }
483
484                 HASHMAP_FOREACH(j, m->jobs, i)
485                         fprintf(f, "<node name=\"job/%lu\"/>", (unsigned long) j->id);
486
487                 fputs(INTROSPECTION_END, f);
488
489                 if (ferror(f)) {
490                         fclose(f);
491                         free(introspection);
492                         goto oom;
493                 }
494
495                 fclose(f);
496
497                 if (!introspection)
498                         goto oom;
499
500                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) {
501                         free(introspection);
502                         goto oom;
503                 }
504
505                 free(introspection);
506
507         } else
508                 return bus_default_message_handler(m, message, NULL, properties);
509
510         free(path);
511
512         if (reply) {
513                 if (!dbus_connection_send(connection, reply, NULL))
514                         goto oom;
515
516                 dbus_message_unref(reply);
517         }
518
519         return DBUS_HANDLER_RESULT_HANDLED;
520
521 oom:
522         free(path);
523
524         if (reply)
525                 dbus_message_unref(reply);
526
527         dbus_error_free(&error);
528
529         return DBUS_HANDLER_RESULT_NEED_MEMORY;
530 }
531
532 const DBusObjectPathVTable bus_manager_vtable = {
533         .message_function = bus_manager_message_handler
534 };