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