chiark / gitweb /
driverd: make sure AddMatch is accessible without privileges
[elogind.git] / src / bus-driverd / bus-driverd.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2013 Daniel Mack
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <stdlib.h>
21 #include <stdbool.h>
22 #include <unistd.h>
23 #include <getopt.h>
24 #include <locale.h>
25 #include <string.h>
26 #include <poll.h>
27 #include <netdb.h>
28 #include <sys/socket.h>
29 #include <sys/un.h>
30 #include <sys/timex.h>
31 #include <sys/utsname.h>
32 #include <unistd.h>
33
34 #include "kdbus.h"
35 #include "sd-bus.h"
36 #include "bus-internal.h"
37 #include "sd-daemon.h"
38 #include "sd-event.h"
39 #include "event-util.h"
40 #include "bus-util.h"
41 #include "bus-error.h"
42 #include "bus-message.h"
43 #include "bus-kernel.h"
44 #include "socket-util.h"
45 #include "util.h"
46 #include "build.h"
47 #include "strv.h"
48 #include "sd-id128.h"
49 #include "async.h"
50 #include "hashmap.h"
51 #include "def.h"
52 #include "unit-name.h"
53 #include "bus-control.h"
54
55 #define CLIENTS_MAX 1024
56 #define MATCHES_MAX 1024
57
58 typedef struct Match Match;
59 typedef struct Client Client;
60 typedef struct Context Context;
61
62 struct Match {
63         Client *client;
64         char *match;
65         uint64_t cookie;
66         LIST_FIELDS(Match, matches);
67 };
68
69 struct Client {
70         Context *context;
71         uint64_t id;
72         uint64_t next_cookie;
73         Hashmap *matches;
74         unsigned n_matches;
75         char *watch;
76 };
77
78 struct Context {
79         sd_bus *bus;
80         sd_event *event;
81         Hashmap *clients;
82 };
83
84 static void match_free(Match *m) {
85
86         if (!m)
87                 return;
88
89         if (m->client) {
90                 Match *first;
91
92                 first = hashmap_get(m->client->matches, m->match);
93                 LIST_REMOVE(matches, first, m);
94                 if (first)
95                         assert_se(hashmap_replace(m->client->matches, m->match, first) >= 0);
96                 else
97                         hashmap_remove(m->client->matches, m->match);
98
99                 m->client->n_matches--;
100         }
101
102         free(m->match);
103         free(m);
104 }
105
106 static int match_new(Client *c, struct bus_match_component *components, unsigned n_components, Match **_m) {
107         Match *m, *first;
108         int r;
109
110         assert(c);
111         assert(_m);
112
113         r = hashmap_ensure_allocated(&c->matches, string_hash_func, string_compare_func);
114         if (r < 0)
115                 return r;
116
117         m = new0(Match, 1);
118         if (!m)
119                 return -ENOMEM;
120
121         m->match = bus_match_to_string(components, n_components);
122         if (!m->match) {
123                 r = -ENOMEM;
124                 goto fail;
125         }
126
127         m->cookie = ++c->next_cookie;
128
129         first = hashmap_get(c->matches, m->match);
130         LIST_PREPEND(matches, first, m);
131         r = hashmap_replace(c->matches, m->match, first);
132         if (r < 0) {
133                 LIST_REMOVE(matches, first, m);
134                 goto fail;
135         }
136
137         m->client = c;
138         c->n_matches++;
139
140         *_m = m;
141         m = NULL;
142
143         return 0;
144
145 fail:
146         match_free(m);
147         return r;
148 }
149
150 static int on_name_owner_changed(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error);
151
152 static void client_free(Client *c) {
153         Match *m;
154
155         if (!c)
156                 return;
157
158         if (c->context) {
159                 if (c->watch)
160                         sd_bus_remove_match(c->context->bus, c->watch, on_name_owner_changed, c);
161
162                 assert_se(hashmap_remove(c->context->clients, &c->id) == c);
163         }
164
165         while ((m = hashmap_first(c->matches)))
166                 match_free(m);
167
168         hashmap_free(c->matches);
169         free(c->watch);
170
171         free(c);
172 }
173
174 static int on_name_owner_changed(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
175         Client *c = userdata;
176
177         assert(bus);
178         assert(m);
179
180         client_free(c);
181         return 0;
182 }
183
184 static int client_acquire(Context *context, uint64_t id, Client **_c) {
185         char *watch = NULL;
186         Client *c;
187         int r;
188
189         assert(context);
190         assert(_c);
191
192         c = hashmap_get(context->clients, &id);
193         if (c) {
194                 *_c = c;
195                 return 0;
196         }
197
198         if (hashmap_size(context->clients) >= CLIENTS_MAX)
199                 return -ENOBUFS;
200
201         r = hashmap_ensure_allocated(&context->clients, uint64_hash_func, uint64_compare_func);
202         if (r < 0)
203                 return r;
204
205         c = new0(Client, 1);
206         if (!c)
207                 return -ENOMEM;
208
209         c->id = id;
210
211         r = hashmap_put(context->clients, &c->id, c);
212         if (r < 0)
213                 goto fail;
214
215         c->context = context;
216
217         if (asprintf(&watch,
218                      "type='signal',"
219                      "sender='org.freedesktop.DBus',"
220                      "path='/org/freedesktop/DBus',"
221                      "interface='org.freedesktop.DBus',"
222                      "member='NameOwnerChanged',"
223                      "arg0=':1.%llu'", (unsigned long long) id) < 0) {
224                 r = -ENOMEM;
225                 goto fail;
226         }
227
228         r = sd_bus_add_match(context->bus, watch, on_name_owner_changed, c);
229         if (r < 0) {
230                 free(watch);
231                 goto fail;
232         }
233
234         c->watch = watch;
235
236         *_c = c;
237         return 0;
238
239 fail:
240         client_free(c);
241         return r;
242 }
243
244 static int driver_add_match(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
245
246         struct bus_match_component *components = NULL;
247         Context *context = userdata;
248         unsigned n_components = 0;
249         Match *m = NULL;
250         Client *c = NULL;
251         char *arg0;
252         uint64_t id;
253         int r;
254
255         assert(bus);
256         assert(message);
257         assert(context);
258
259         r = sd_bus_message_read(message, "s", &arg0);
260         if (r < 0)
261                 return r;
262
263         r = bus_kernel_parse_unique_name(message->sender, &id);
264         if (r < 0)
265                 return r;
266
267         r = client_acquire(context, id, &c);
268         if (r == -ENOBUFS)
269                 return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Reached limit of %u clients", CLIENTS_MAX);
270         if (r < 0)
271                 return r;
272
273         if (c->n_matches >= MATCHES_MAX) {
274                 r = sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Reached limit of %u matches per client", MATCHES_MAX);
275                 goto fail;
276         }
277
278         r = bus_match_parse(arg0, &components, &n_components);
279         if (r < 0) {
280                 r = sd_bus_error_setf(error, SD_BUS_ERROR_MATCH_RULE_INVALID, "Match rule \"%s\" is not valid", arg0);
281                 goto fail;
282         }
283
284         r = match_new(c, components, n_components, &m);
285         if (r < 0)
286                 goto fail;
287
288         r = bus_add_match_internal_kernel(bus, id, components, n_components, m->cookie);
289         if (r < 0)
290                 goto fail;
291
292         bus_match_parse_free(components, n_components);
293
294         return sd_bus_reply_method_return(message, NULL);
295
296 fail:
297         bus_match_parse_free(components, n_components);
298
299         match_free(m);
300
301         if (c->n_matches <= 0)
302                 client_free(c);
303
304         return r;
305 }
306
307 static int driver_remove_match(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
308
309         struct bus_match_component *components = NULL;
310         _cleanup_free_ char *normalized = NULL;
311         Context *context = userdata;
312         unsigned n_components = 0;
313         Client *c = NULL;
314         Match *m = NULL;
315         char *arg0;
316         uint64_t id;
317         int r;
318
319         assert(bus);
320         assert(message);
321         assert(context);
322
323         r = sd_bus_message_read(message, "s", &arg0);
324         if (r < 0)
325                 return r;
326
327         r = bus_kernel_parse_unique_name(message->sender, &id);
328         if (r < 0)
329                 return r;
330
331         c = hashmap_get(context->clients, &id);
332         if (!c)
333                 return sd_bus_error_setf(error, SD_BUS_ERROR_MATCH_RULE_NOT_FOUND, "You have not registered any matches.");
334
335         r = bus_match_parse(arg0, &components, &n_components);
336         if (r < 0) {
337                 r = sd_bus_error_setf(error, SD_BUS_ERROR_MATCH_RULE_INVALID, "Match rule \"%s\" is not valid", arg0);
338                 goto finish;
339         }
340
341         normalized = bus_match_to_string(components, n_components);
342         if (!normalized) {
343                 r = -ENOMEM;
344                 goto finish;
345         }
346
347         m = hashmap_get(c->matches, normalized);
348         if (!m) {
349                 r = sd_bus_error_setf(error, SD_BUS_ERROR_MATCH_RULE_NOT_FOUND, "Match rule \"%s\" not found.");
350                 goto finish;
351         }
352
353         bus_remove_match_internal_kernel(bus, id, m->cookie);
354         match_free(m);
355
356         r = sd_bus_reply_method_return(message, NULL);
357
358 finish:
359         bus_match_parse_free(components, n_components);
360
361         if (c->n_matches <= 0)
362                 client_free(c);
363
364         return r;
365 }
366
367 static int driver_get_security_ctx(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
368         _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
369         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
370         char *arg0;
371         int r;
372
373         r = sd_bus_message_read(m, "s", &arg0);
374         if (r < 0)
375                 return r;
376
377         assert_return(service_name_is_valid(arg0), -EINVAL);
378
379         r = sd_bus_get_owner(bus, arg0, SD_BUS_CREDS_SELINUX_CONTEXT, &creds);
380         if (r < 0)
381                 return r;
382
383         r = sd_bus_message_new_method_return(m, &reply);
384         if (r < 0)
385                 return r;
386
387         r = sd_bus_message_append_array(reply, 'y', creds->label, strlen(creds->label));
388         if (r < 0)
389                 return r;
390
391         return sd_bus_send(bus, reply, NULL);
392 }
393
394 static int driver_get_pid(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
395         _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
396         char *arg0;
397         int r;
398
399         r = sd_bus_message_read(m, "s", &arg0);
400         if (r < 0)
401                 return r;
402
403         assert_return(service_name_is_valid(arg0), -EINVAL);
404
405         r = sd_bus_get_owner(bus, arg0, SD_BUS_CREDS_PID, &creds);
406         if (r < 0)
407                 return r;
408
409         return sd_bus_reply_method_return(m, "u", creds->pid);
410 }
411
412 static int driver_get_user(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
413         _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
414         char *arg0;
415         int r;
416
417         r = sd_bus_message_read(m, "s", &arg0);
418         if (r < 0)
419                 return r;
420
421         assert_return(service_name_is_valid(arg0), -EINVAL);
422
423         r = sd_bus_get_owner(bus, arg0, SD_BUS_CREDS_UID, &creds);
424         if (r < 0)
425                 return r;
426
427         return sd_bus_reply_method_return(m, "u", creds->uid);
428 }
429
430 static int driver_get_id(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
431         sd_id128_t server_id;
432         char buf[SD_ID128_STRING_MAX];
433         int r;
434
435         r = sd_bus_get_server_id(bus, &server_id);
436         if (r < 0)
437                 return r;
438
439         return sd_bus_reply_method_return(m, "s", sd_id128_to_string(server_id, buf));
440 }
441
442 static int driver_get_name_owner(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
443         _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
444         char *arg0;
445         int r;
446
447         r = sd_bus_message_read(m, "s", &arg0);
448         if (r < 0)
449                 return r;
450
451         assert_return(service_name_is_valid(arg0), -EINVAL);
452
453         r = sd_bus_get_owner(bus, arg0, SD_BUS_CREDS_UNIQUE_NAME, &creds);
454         if (r < 0)
455                 return r;
456
457         return sd_bus_reply_method_return(m, "s", creds->unique_name);
458 }
459
460 static int driver_hello(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
461         return sd_bus_reply_method_return(m, "s", m->sender);
462 }
463
464 static int return_strv(sd_bus *bus, sd_bus_message *m, char **l) {
465         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
466         int r;
467
468         r = sd_bus_message_new_method_return(m, &reply);
469         if (r < 0)
470                 return r;
471
472         r = sd_bus_message_append_strv(reply, l);
473         if (r < 0)
474                 return r;
475
476         return sd_bus_send(bus, reply, NULL);
477 }
478
479 static int driver_list_names(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
480         _cleanup_strv_free_ char **names = NULL;
481         int r;
482
483         r = sd_bus_list_names(bus, &names, NULL);
484         if (r < 0)
485                 return r;
486
487         /* Let's sort the names list to make it stable */
488         strv_sort(names);
489
490         return return_strv(bus, m, names);
491 }
492
493 static int driver_list_activatable_names(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
494         _cleanup_strv_free_ char **names = NULL;
495         int r;
496
497         r = sd_bus_list_names(bus, NULL, &names);
498         if (r < 0)
499                 return r;
500
501         /* Let's sort the names list to make it stable */
502         strv_sort(names);
503
504         return return_strv(bus, m, names);
505 }
506
507 static int driver_list_queued_owners(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
508         struct kdbus_cmd_name_list cmd = {};
509         struct kdbus_name_list *name_list;
510         struct kdbus_cmd_name *name;
511         _cleanup_strv_free_ char **owners = NULL;
512         char *arg0;
513         int r;
514
515         r = sd_bus_message_read(m, "s", &arg0);
516         if (r < 0)
517                 return r;
518
519         assert_return(service_name_is_valid(arg0), -EINVAL);
520
521         cmd.flags = KDBUS_NAME_LIST_QUEUED;
522
523         r = ioctl(bus->input_fd, KDBUS_CMD_NAME_LIST, &cmd);
524         if (r < 0)
525                 return -errno;
526
527         name_list = (struct kdbus_name_list *) ((uint8_t *) bus->kdbus_buffer + cmd.offset);
528
529         KDBUS_ITEM_FOREACH(name, name_list, names) {
530                 char *n;
531
532                 if (name->size <= sizeof(*name))
533                         continue;
534
535                 if (!streq(name->name, arg0))
536                         continue;
537
538                 if (asprintf(&n, ":1.%llu", (unsigned long long) name->id) < 0)
539                         return -ENOMEM;
540
541                 r = strv_push(&owners, n);
542                 if (r < 0) {
543                         free(n);
544                         return -ENOMEM;
545                 }
546         }
547
548         r = ioctl(bus->input_fd, KDBUS_CMD_FREE, &cmd.offset);
549         if (r < 0)
550                 return -errno;
551
552         return return_strv(bus, m, owners);
553 }
554
555 static int driver_name_has_owner(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
556         char *arg0;
557         int r;
558
559         r = sd_bus_message_read(m, "s", &arg0);
560         if (r < 0)
561                 return r;
562
563         assert_return(service_name_is_valid(arg0), -EINVAL);
564
565         r = sd_bus_get_owner(bus, arg0, 0, NULL);
566         if (r < 0 && r != -ENOENT)
567                 return r;
568
569         return sd_bus_reply_method_return(m, "b", r >= 0);
570 }
571
572 static int driver_request_name(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
573         struct kdbus_cmd_name *n;
574         uint32_t flags;
575         size_t size, l;
576         uint64_t id;
577         const char *name;
578         int r;
579
580         r = sd_bus_message_read(m, "su", &name, &flags);
581         if (r < 0)
582                 return r;
583
584         assert_return(service_name_is_valid(name), -EINVAL);
585         assert_return((flags & ~(BUS_NAME_ALLOW_REPLACEMENT|BUS_NAME_REPLACE_EXISTING|BUS_NAME_DO_NOT_QUEUE)) == 0, -EINVAL);
586
587         l = strlen(name);
588         size = offsetof(struct kdbus_cmd_name, name) + l + 1;
589         n = alloca0(size);
590         n->size = size;
591         memcpy(n->name, name, l+1);
592         kdbus_translate_request_name_flags(flags, (uint64_t *) &n->flags);
593
594         /* This function is open-coded because we request the name 'on behalf'
595          * of the requesting connection */
596         r = bus_kernel_parse_unique_name(m->sender, &id);
597         if (r < 0)
598                 return r;
599
600         n->id = id;
601
602         r = ioctl(bus->input_fd, KDBUS_CMD_NAME_ACQUIRE, n);
603         if (r < 0) {
604                 if (errno == EEXIST)
605                         return sd_bus_reply_method_return(m, "u", BUS_NAME_EXISTS);
606                 if (errno == EALREADY)
607                         return sd_bus_reply_method_return(m, "u", BUS_NAME_ALREADY_OWNER);
608
609                 return -errno;
610         }
611
612         if (n->flags & KDBUS_NAME_IN_QUEUE)
613                 return sd_bus_reply_method_return(m, "u", BUS_NAME_IN_QUEUE);
614
615         return sd_bus_reply_method_return(m, "u", BUS_NAME_PRIMARY_OWNER);
616 }
617
618 static int driver_release_name(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
619         struct kdbus_cmd_name *n;
620         const char *name;
621         size_t l, size;
622         uint64_t id;
623         int r;
624
625         r = sd_bus_message_read(m, "s", &name);
626         if (r < 0)
627                 return r;
628
629         assert_return(service_name_is_valid(name), -EINVAL);
630
631         l = strlen(name);
632         size = offsetof(struct kdbus_cmd_name, name) + l + 1;
633         n = alloca0(size);
634         n->size = size;
635         memcpy(n->name, name, l+1);
636
637         /* This function is open-coded because we request the name 'on behalf'
638          * of the requesting connection */
639         r = bus_kernel_parse_unique_name(m->sender, &id);
640         if (r < 0)
641                 return r;
642
643         n->id = id;
644
645         r = ioctl(bus->input_fd, KDBUS_CMD_NAME_RELEASE, n);
646         if (r < 0) {
647                 if (errno == ESRCH)
648                         return sd_bus_reply_method_return(m, "u", BUS_NAME_NON_EXISTENT);
649                 if (errno == EADDRINUSE)
650                         return sd_bus_reply_method_return(m, "u", BUS_NAME_NOT_OWNER);
651                 return -errno;
652         }
653
654         return sd_bus_reply_method_return(m, "u", BUS_NAME_RELEASED);
655 }
656
657 static int driver_start_service_by_name(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
658         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
659         _cleanup_strv_free_ char **t = NULL;
660         _cleanup_free_ char *path = NULL;
661         uint32_t flags;
662         char *name, *u;
663         int r;
664
665         r = sd_bus_message_read(m, "su", &name, &flags);
666         if (r < 0)
667                 return r;
668
669         assert_return(service_name_is_valid(name), -EINVAL);
670         assert_return(flags == 0, -ENOTSUP);
671
672         r = sd_bus_get_owner(bus, name, 0, NULL);
673         if (r >= 0)
674                 return sd_bus_reply_method_return(m, "u", BUS_START_REPLY_ALREADY_RUNNING);
675         if (r != -ENOENT)
676                 return r;
677
678         u = strappenda(name, ".busname");
679
680         path = unit_dbus_path_from_name(u);
681         if (!path)
682                 return -ENOMEM;
683
684         r = sd_bus_get_property_strv(
685                         bus,
686                         "org.freedesktop.systemd1",
687                         path,
688                         "org.freedesktop.systemd1.Unit",
689                         "Triggers",
690                         error,
691                         &t);
692         if (r < 0)
693                 return r;
694
695         if (!t[0] || t[1])
696                 return -EIO;
697
698         r = sd_bus_call_method(
699                         bus,
700                         "org.freedesktop.systemd1",
701                         "/org/freedesktop/systemd1",
702                         "org.freedesktop.systemd1.Manager",
703                         "StartUnit",
704                         error,
705                         &reply,
706                         "ss",
707                         t[0],
708                         "replace");
709         if (r < 0)
710                 return r;
711
712         return sd_bus_reply_method_return(m, "u", BUS_START_REPLY_SUCCESS);
713 }
714
715 static int driver_unsupported(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
716         return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "%s() is not supported", sd_bus_message_get_member(m));
717 }
718
719 static const sd_bus_vtable driver_vtable[] = {
720         SD_BUS_VTABLE_START(0),
721         SD_BUS_METHOD("AddMatch", "s", NULL, driver_add_match, SD_BUS_VTABLE_UNPRIVILEGED),
722         SD_BUS_METHOD("GetConnectionSELinuxSecurityContext", "s", "ay", driver_get_security_ctx, SD_BUS_VTABLE_UNPRIVILEGED),
723         SD_BUS_METHOD("GetConnectionUnixProcessID", "s", "u", driver_get_pid, SD_BUS_VTABLE_UNPRIVILEGED),
724         SD_BUS_METHOD("GetConnectionUnixUser", "s", "u", driver_get_user, SD_BUS_VTABLE_UNPRIVILEGED),
725         SD_BUS_METHOD("GetId", NULL, "s", driver_get_id, SD_BUS_VTABLE_UNPRIVILEGED),
726         SD_BUS_METHOD("GetNameOwner", "s", "s", driver_get_name_owner, SD_BUS_VTABLE_UNPRIVILEGED),
727         SD_BUS_METHOD("Hello", NULL, "s", driver_hello, SD_BUS_VTABLE_UNPRIVILEGED),
728         SD_BUS_METHOD("ListActivatableNames", NULL, "as", driver_list_activatable_names, SD_BUS_VTABLE_UNPRIVILEGED),
729         SD_BUS_METHOD("ListNames", NULL, "as", driver_list_names, SD_BUS_VTABLE_UNPRIVILEGED),
730         SD_BUS_METHOD("ListQueuedOwners", "s", "as", driver_list_queued_owners, SD_BUS_VTABLE_UNPRIVILEGED),
731         SD_BUS_METHOD("NameHasOwner", "s", "b", driver_name_has_owner, SD_BUS_VTABLE_UNPRIVILEGED),
732         SD_BUS_METHOD("ReleaseName", "s", "u", driver_release_name, SD_BUS_VTABLE_UNPRIVILEGED),
733         SD_BUS_METHOD("ReloadConfig", NULL, NULL, driver_unsupported, SD_BUS_VTABLE_DEPRECATED),
734         SD_BUS_METHOD("RemoveMatch", "s", NULL, driver_remove_match, SD_BUS_VTABLE_UNPRIVILEGED),
735         SD_BUS_METHOD("RequestName", "su", "u", driver_request_name, SD_BUS_VTABLE_UNPRIVILEGED),
736         SD_BUS_METHOD("StartServiceByName", "su", "u", driver_start_service_by_name, SD_BUS_VTABLE_UNPRIVILEGED),
737         SD_BUS_METHOD("UpdateActivationEnvironment", "a{ss}", NULL, driver_unsupported, SD_BUS_VTABLE_DEPRECATED),
738         SD_BUS_SIGNAL("NameAcquired", "s", SD_BUS_VTABLE_DEPRECATED),
739         SD_BUS_SIGNAL("NameLost", "s", SD_BUS_VTABLE_DEPRECATED),
740         SD_BUS_SIGNAL("NameOwnerChanged", "sss", 0),
741         SD_BUS_VTABLE_END
742 };
743
744 static int connect_bus(Context *c) {
745         int r;
746
747         assert(c);
748
749         r = sd_bus_default_system(&c->bus);
750         if (r < 0) {
751                 log_error("Failed to create bus: %s", strerror(-r));
752                 return r;
753         }
754
755         if (!c->bus->is_kernel) {
756                 log_error("Not running on kdbus");
757                 return -EPERM;
758         }
759
760         r = sd_bus_add_object_vtable(c->bus, "/org/freedesktop/DBus", "org.freedesktop.DBus", driver_vtable, c);
761         if (r < 0) {
762                 log_error("Failed to add manager object vtable: %s", strerror(-r));
763                 return r;
764         }
765
766         r = sd_bus_request_name(c->bus, "org.freedesktop.DBus", 0);
767         if (r < 0) {
768                 log_error("Unable to request name: %s\n", strerror(-r));
769                 return r;
770         }
771
772         r = sd_bus_attach_event(c->bus, c->event, 0);
773         if (r < 0) {
774                 log_error("Error while adding bus to event loop: %s", strerror(-r));
775                 return r;
776         }
777
778         return 0;
779 }
780
781 static bool check_idle(void *userdata) {
782         Context *c = userdata;
783         assert(c);
784
785         return hashmap_isempty(c->clients);
786 }
787
788 int main(int argc, char *argv[]) {
789         Context context = {};
790         Client *c;
791         int r;
792
793         log_set_target(LOG_TARGET_AUTO);
794         log_parse_environment();
795         log_open();
796
797         if (argc != 1) {
798                 log_error("This program takes no arguments.");
799                 r = -EINVAL;
800                 goto finish;
801         }
802
803         r = sd_event_default(&context.event);
804         if (r < 0) {
805                 log_error("Failed to allocate event loop: %s", strerror(-r));
806                 goto finish;
807         }
808
809         sd_event_set_watchdog(context.event, true);
810
811         r = connect_bus(&context);
812         if (r < 0)
813                 goto finish;
814
815         r = bus_event_loop_with_idle(context.event, context.bus, "org.freedesktop.DBus", DEFAULT_EXIT_USEC, check_idle, &context);
816         if (r < 0) {
817                 log_error("Failed to run event loop: %s", strerror(-r));
818                 goto finish;
819         }
820
821 finish:
822         while ((c = hashmap_first(context.clients)))
823                 client_free(c);
824
825         sd_bus_unref(context.bus);
826         sd_event_unref(context.event);
827
828         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
829
830 }