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