chiark / gitweb /
systemadm: fix signed comparison issue
[elogind.git] / dbus.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 <dbus/dbus.h>
23
24 #include <sys/epoll.h>
25 #include <sys/timerfd.h>
26 #include <errno.h>
27 #include <unistd.h>
28
29 #include "dbus.h"
30 #include "log.h"
31 #include "strv.h"
32
33 static void bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data)  {
34         Manager *m = data;
35
36         assert(bus);
37         assert(m);
38
39         m->request_bus_dispatch = status != DBUS_DISPATCH_COMPLETE;
40 }
41
42 static uint32_t bus_flags_to_events(DBusWatch *bus_watch) {
43         unsigned flags;
44         uint32_t events = 0;
45
46         assert(bus_watch);
47
48         /* no watch flags for disabled watches */
49         if (!dbus_watch_get_enabled(bus_watch))
50                 return 0;
51
52         flags = dbus_watch_get_flags(bus_watch);
53
54         if (flags & DBUS_WATCH_READABLE)
55                 events |= EPOLLIN;
56         if (flags & DBUS_WATCH_WRITABLE)
57                 events |= EPOLLOUT;
58
59         return events | EPOLLHUP | EPOLLERR;
60 }
61
62 static unsigned events_to_bus_flags(uint32_t events) {
63         unsigned flags = 0;
64
65         if (events & EPOLLIN)
66                 flags |= DBUS_WATCH_READABLE;
67         if (events & EPOLLOUT)
68                 flags |= DBUS_WATCH_WRITABLE;
69         if (events & EPOLLHUP)
70                 flags |= DBUS_WATCH_HANGUP;
71         if (events & EPOLLERR)
72                 flags |= DBUS_WATCH_ERROR;
73
74         return flags;
75 }
76
77 void bus_watch_event(Manager *m, Watch *w, int events) {
78         assert(m);
79         assert(w);
80
81         /* This is called by the event loop whenever there is
82          * something happening on D-Bus' file handles. */
83
84         if (!(dbus_watch_get_enabled(w->data.bus_watch)))
85                 return;
86
87         dbus_watch_handle(w->data.bus_watch, events_to_bus_flags(events));
88 }
89
90 static dbus_bool_t bus_add_watch(DBusWatch *bus_watch, void *data) {
91         Manager *m = data;
92         Watch *w;
93         struct epoll_event ev;
94
95         assert(bus_watch);
96         assert(m);
97
98         if (!(w = new0(Watch, 1)))
99                 return FALSE;
100
101         w->fd = dbus_watch_get_unix_fd(bus_watch);
102         w->type = WATCH_DBUS_WATCH;
103         w->data.bus_watch = bus_watch;
104
105         zero(ev);
106         ev.events = bus_flags_to_events(bus_watch);
107         ev.data.ptr = w;
108
109         if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) {
110
111                 if (errno != EEXIST) {
112                         free(w);
113                         return FALSE;
114                 }
115
116                 /* Hmm, bloody D-Bus creates multiple watches on the
117                  * same fd. epoll() does not like that. As a dirty
118                  * hack we simply dup() the fd and hence get a second
119                  * one we can safely add to the epoll(). */
120
121                 if ((w->fd = dup(w->fd)) < 0) {
122                         free(w);
123                         return FALSE;
124                 }
125
126                 if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) {
127                         free(w);
128                         close_nointr_nofail(w->fd);
129                         return FALSE;
130                 }
131
132                 w->fd_is_dupped = true;
133         }
134
135         dbus_watch_set_data(bus_watch, w, NULL);
136
137         return TRUE;
138 }
139
140 static void bus_remove_watch(DBusWatch *bus_watch, void *data) {
141         Manager *m = data;
142         Watch *w;
143
144         assert(bus_watch);
145         assert(m);
146
147         if (!(w = dbus_watch_get_data(bus_watch)))
148                 return;
149
150         assert(w->type == WATCH_DBUS_WATCH);
151         assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
152
153         if (w->fd_is_dupped)
154                 close_nointr_nofail(w->fd);
155
156         free(w);
157 }
158
159 static void bus_toggle_watch(DBusWatch *bus_watch, void *data) {
160         Manager *m = data;
161         Watch *w;
162         struct epoll_event ev;
163
164         assert(bus_watch);
165         assert(m);
166
167         assert_se(w = dbus_watch_get_data(bus_watch));
168         assert(w->type == WATCH_DBUS_WATCH);
169
170         zero(ev);
171         ev.events = bus_flags_to_events(bus_watch);
172         ev.data.ptr = w;
173
174         assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_MOD, w->fd, &ev) == 0);
175 }
176
177 static int bus_timeout_arm(Manager *m, Watch *w) {
178         struct itimerspec its;
179
180         assert(m);
181         assert(w);
182
183         zero(its);
184
185         if (dbus_timeout_get_enabled(w->data.bus_timeout)) {
186                 timespec_store(&its.it_value, dbus_timeout_get_interval(w->data.bus_timeout) * USEC_PER_MSEC);
187                 its.it_interval = its.it_interval;
188         }
189
190         if (timerfd_settime(w->fd, 0, &its, NULL) < 0)
191                 return -errno;
192
193         return 0;
194 }
195
196 void bus_timeout_event(Manager *m, Watch *w, int events) {
197         assert(m);
198         assert(w);
199
200         /* This is called by the event loop whenever there is
201          * something happening on D-Bus' file handles. */
202
203         if (!(dbus_timeout_get_enabled(w->data.bus_timeout)))
204                 return;
205
206         dbus_timeout_handle(w->data.bus_timeout);
207 }
208
209 static dbus_bool_t bus_add_timeout(DBusTimeout *timeout, void *data) {
210         Manager *m = data;
211         Watch *w;
212         struct epoll_event ev;
213
214         assert(timeout);
215         assert(m);
216
217         if (!(w = new0(Watch, 1)))
218                 return FALSE;
219
220         if (!(w->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
221                 goto fail;
222
223         w->type = WATCH_DBUS_TIMEOUT;
224         w->data.bus_timeout = timeout;
225
226         if (bus_timeout_arm(m, w) < 0)
227                 goto fail;
228
229         zero(ev);
230         ev.events = EPOLLIN;
231         ev.data.ptr = w;
232
233         if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0)
234                 goto fail;
235
236         dbus_timeout_set_data(timeout, w, NULL);
237
238         return TRUE;
239
240 fail:
241         if (w->fd >= 0)
242                 close_nointr_nofail(w->fd);
243
244         free(w);
245         return FALSE;
246 }
247
248 static void bus_remove_timeout(DBusTimeout *timeout, void *data) {
249         Manager *m = data;
250         Watch *w;
251
252         assert(timeout);
253         assert(m);
254
255         if (!(w = dbus_timeout_get_data(timeout)))
256                 return;
257
258         assert(w->type == WATCH_DBUS_TIMEOUT);
259         assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
260         close_nointr_nofail(w->fd);
261         free(w);
262 }
263
264 static void bus_toggle_timeout(DBusTimeout *timeout, void *data) {
265         Manager *m = data;
266         Watch *w;
267         int r;
268
269         assert(timeout);
270         assert(m);
271
272         assert_se(w = dbus_timeout_get_data(timeout));
273         assert(w->type == WATCH_DBUS_TIMEOUT);
274
275         if ((r = bus_timeout_arm(m, w)) < 0)
276                 log_error("Failed to rearm timer: %s", strerror(-r));
277 }
278
279 void bus_dispatch(Manager *m) {
280         assert(m);
281
282         if (dbus_connection_dispatch(m->bus) == DBUS_DISPATCH_COMPLETE)
283                 m->request_bus_dispatch = false;
284 }
285
286 static int request_name(Manager *m) {
287         DBusMessage *message;
288         const char *name = "org.freedesktop.systemd1";
289         uint32_t flags = 0;
290
291         if (!(message = dbus_message_new_method_call(
292                               DBUS_SERVICE_DBUS,
293                               DBUS_PATH_DBUS,
294                               DBUS_INTERFACE_DBUS,
295                               "RequestName")))
296                 return -ENOMEM;
297
298         if (!dbus_message_append_args(
299                             message,
300                             DBUS_TYPE_STRING, &name,
301                             DBUS_TYPE_UINT32, &flags,
302                             DBUS_TYPE_INVALID)) {
303                 dbus_message_unref(message);
304                 return -ENOMEM;
305         }
306
307         if (!dbus_connection_send(m->bus, message, NULL)) {
308                 dbus_message_unref(message);
309                 return -ENOMEM;
310         }
311
312         /* We simple ask for the name and don't wait for it. Sooner or
313          * later we'll have it, and we wouldn't know what to do on
314          * error anyway. */
315
316         dbus_message_unref(message);
317
318         return 0;
319 }
320
321 int bus_init(Manager *m) {
322         DBusError error;
323         char *id;
324         int r;
325
326         assert(m);
327
328         if (m->bus)
329                 return 0;
330
331         dbus_connection_set_change_sigpipe(FALSE);
332
333         dbus_error_init(&error);
334         if (!(m->bus = dbus_bus_get_private(m->is_init ? DBUS_BUS_SYSTEM : DBUS_BUS_SESSION, &error))) {
335                 log_error("Failed to get D-Bus connection: %s", error.message);
336                 dbus_error_free(&error);
337                 return -ECONNREFUSED;
338         }
339
340         dbus_connection_set_exit_on_disconnect(m->bus, FALSE);
341         dbus_connection_set_dispatch_status_function(m->bus, bus_dispatch_status, m, NULL);
342         if (!dbus_connection_set_watch_functions(m->bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) ||
343             !dbus_connection_set_timeout_functions(m->bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL) ||
344             !dbus_connection_register_object_path(m->bus, "/org/freedesktop/systemd1", &bus_manager_vtable, m) ||
345             !dbus_connection_register_fallback(m->bus, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) ||
346             !dbus_connection_register_fallback(m->bus, "/org/freedesktop/systemd1/job", &bus_job_vtable, m)) {
347                 bus_done(m);
348                 return -ENOMEM;
349         }
350
351         if ((r = request_name(m)) < 0) {
352                 bus_done(m);
353                 return r;
354         }
355
356         log_debug("Successfully connected to D-Bus bus %s as %s",
357                   strnull((id = dbus_connection_get_server_id(m->bus))),
358                   strnull(dbus_bus_get_unique_name(m->bus)));
359         dbus_free(id);
360
361         m->request_bus_dispatch = true;
362
363         return 0;
364 }
365
366 void bus_done(Manager *m) {
367         assert(m);
368
369         if (m->bus) {
370                 dbus_connection_close(m->bus);
371                 dbus_connection_unref(m->bus);
372                 m->bus = NULL;
373         }
374 }
375
376 DBusHandlerResult bus_default_message_handler(Manager *m, DBusMessage *message, const char*introspection, const BusProperty *properties) {
377         DBusError error;
378         DBusMessage *reply = NULL;
379         int r;
380
381         assert(m);
382         assert(message);
383
384         dbus_error_init(&error);
385
386         if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) {
387
388                 if (!(reply = dbus_message_new_method_return(message)))
389                         goto oom;
390
391                 if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID))
392                         goto oom;
393
394         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) {
395                 const char *interface, *property;
396                 const BusProperty *p;
397
398                 if (!dbus_message_get_args(
399                             message,
400                             &error,
401                             DBUS_TYPE_STRING, &interface,
402                             DBUS_TYPE_STRING, &property,
403                             DBUS_TYPE_INVALID))
404                         return bus_send_error_reply(m, message, &error, -EINVAL);
405
406                 for (p = properties; p->property; p++)
407                         if (streq(p->interface, interface) && streq(p->property, property))
408                                 break;
409
410                 if (p->property) {
411                         DBusMessageIter iter, sub;
412
413                         if (!(reply = dbus_message_new_method_return(message)))
414                                 goto oom;
415
416                         dbus_message_iter_init_append(reply, &iter);
417
418                         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub))
419                                 goto oom;
420
421                         if ((r = p->append(m, &sub, property, p->data)) < 0) {
422
423                                 if (r == -ENOMEM)
424                                         goto oom;
425
426                                 dbus_message_unref(reply);
427                                 return bus_send_error_reply(m, message, NULL, r);
428                         }
429
430                         if (!dbus_message_iter_close_container(&iter, &sub))
431                                 goto oom;
432                 }
433         } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) {
434                 const char *interface;
435                 const BusProperty *p;
436                 DBusMessageIter iter, sub, sub2, sub3;
437                 bool any = false;
438
439                 if (!dbus_message_get_args(
440                             message,
441                             &error,
442                             DBUS_TYPE_STRING, &interface,
443                             DBUS_TYPE_INVALID))
444                         return bus_send_error_reply(m, message, &error, -EINVAL);
445
446                 if (!(reply = dbus_message_new_method_return(message)))
447                         goto oom;
448
449                 dbus_message_iter_init_append(reply, &iter);
450
451                 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub))
452                         goto oom;
453
454                 for (p = properties; p->property; p++) {
455                         if (!streq(p->interface, interface))
456                                 continue;
457
458                         if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) ||
459                             !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) ||
460                             !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3))
461                                 goto oom;
462
463                         if ((r = p->append(m, &sub3, p->property, p->data)) < 0) {
464
465                                 if (r == -ENOMEM)
466                                         goto oom;
467
468                                 dbus_message_unref(reply);
469                                 return bus_send_error_reply(m, message, NULL, r);
470                         }
471
472                         if (!dbus_message_iter_close_container(&sub2, &sub3) ||
473                             !dbus_message_iter_close_container(&sub, &sub2))
474                                 goto oom;
475
476                         any = true;
477                 }
478
479                 if (!dbus_message_iter_close_container(&iter, &sub))
480                         goto oom;
481         }
482
483         if (reply) {
484                 if (!dbus_connection_send(m->bus, reply, NULL))
485                         goto oom;
486
487                 dbus_message_unref(reply);
488                 return DBUS_HANDLER_RESULT_HANDLED;
489         }
490
491         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
492
493 oom:
494         if (reply)
495                 dbus_message_unref(reply);
496
497         dbus_error_free(&error);
498
499         return DBUS_HANDLER_RESULT_NEED_MEMORY;
500 }
501
502
503
504 static const char *error_to_dbus(int error) {
505
506         switch(error) {
507
508         case -EINVAL:
509                 return DBUS_ERROR_INVALID_ARGS;
510
511         case -ENOMEM:
512                 return DBUS_ERROR_NO_MEMORY;
513
514         case -EPERM:
515         case -EACCES:
516                 return DBUS_ERROR_ACCESS_DENIED;
517
518         case -ESRCH:
519                 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN;
520
521         case -ENOENT:
522                 return DBUS_ERROR_FILE_NOT_FOUND;
523
524         case -EEXIST:
525                 return DBUS_ERROR_FILE_EXISTS;
526
527         case -ETIMEDOUT:
528                 return DBUS_ERROR_TIMEOUT;
529
530         case -EIO:
531                 return DBUS_ERROR_IO_ERROR;
532
533         case -ENETRESET:
534         case -ECONNABORTED:
535         case -ECONNRESET:
536                 return DBUS_ERROR_DISCONNECTED;
537         }
538
539         return DBUS_ERROR_FAILED;
540 }
541
542 DBusHandlerResult bus_send_error_reply(Manager *m, DBusMessage *message, DBusError *bus_error, int error) {
543         DBusMessage *reply = NULL;
544         const char *name, *text;
545
546         if (bus_error && dbus_error_is_set(bus_error)) {
547                 name = bus_error->name;
548                 text = bus_error->message;
549         } else {
550                 name = error_to_dbus(error);
551                 text = strerror(-error);
552         }
553
554         if (!(reply = dbus_message_new_error(message, name, text)))
555                 goto oom;
556
557         if (!dbus_connection_send(m->bus, reply, NULL))
558                 goto oom;
559
560         dbus_message_unref(reply);
561
562         if (bus_error)
563                 dbus_error_free(bus_error);
564
565         return DBUS_HANDLER_RESULT_HANDLED;
566
567 oom:
568         if (reply)
569                 dbus_message_unref(reply);
570
571         if (bus_error)
572                 dbus_error_free(bus_error);
573
574         return DBUS_HANDLER_RESULT_NEED_MEMORY;
575 }
576
577 int bus_property_append_string(Manager *m, DBusMessageIter *i, const char *property, void *data) {
578         const char *t = data;
579
580         assert(m);
581         assert(i);
582         assert(property);
583
584         if (!t)
585                 t = "";
586
587         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t))
588                 return -ENOMEM;
589
590         return 0;
591 }
592
593 int bus_property_append_strv(Manager *m, DBusMessageIter *i, const char *property, void *data) {
594         DBusMessageIter sub;
595         char **t = data;
596
597         assert(m);
598         assert(i);
599         assert(property);
600
601         if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub))
602                 return -ENOMEM;
603
604         STRV_FOREACH(t, t)
605                 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t))
606                         return -ENOMEM;
607
608         if (!dbus_message_iter_close_container(i, &sub))
609                 return -ENOMEM;
610
611         return 0;
612 }
613
614 int bus_property_append_bool(Manager *m, DBusMessageIter *i, const char *property, void *data) {
615         bool *b = data;
616         dbus_bool_t db;
617
618         assert(m);
619         assert(i);
620         assert(property);
621         assert(b);
622
623         db = *b;
624
625         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
626                 return -ENOMEM;
627
628         return 0;
629 }
630
631 int bus_property_append_uint64(Manager *m, DBusMessageIter *i, const char *property, void *data) {
632         assert(m);
633         assert(i);
634         assert(property);
635         assert(data);
636
637         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data))
638                 return -ENOMEM;
639
640         return 0;
641 }
642
643 int bus_property_append_uint32(Manager *m, DBusMessageIter *i, const char *property, void *data) {
644         assert(m);
645         assert(i);
646         assert(property);
647         assert(data);
648
649         if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data))
650                 return -ENOMEM;
651
652         return 0;
653 }