chiark / gitweb /
b97206600b461a92c925e58a4a9f9c36a4d78a57
[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 #include "cgroup-util.h"
55
56 #define CLIENTS_MAX 1024
57 #define MATCHES_MAX 1024
58
59 typedef struct Match Match;
60 typedef struct Client Client;
61 typedef struct Context Context;
62
63 struct Match {
64         Client *client;
65         char *match;
66         uint64_t cookie;
67         LIST_FIELDS(Match, matches);
68 };
69
70 struct Client {
71         Context *context;
72         uint64_t id;
73         uint64_t next_cookie;
74         Hashmap *matches;
75         unsigned n_matches;
76         char *watch;
77 };
78
79 struct Context {
80         sd_bus *bus;
81         sd_event *event;
82         Hashmap *clients;
83 };
84
85 static void match_free(Match *m) {
86
87         if (!m)
88                 return;
89
90         if (m->client) {
91                 Match *first;
92
93                 first = hashmap_get(m->client->matches, m->match);
94                 LIST_REMOVE(matches, first, m);
95                 if (first)
96                         assert_se(hashmap_replace(m->client->matches, first->match, first) >= 0);
97                 else
98                         hashmap_remove(m->client->matches, m->match);
99
100                 m->client->n_matches--;
101         }
102
103         free(m->match);
104         free(m);
105 }
106
107 static int match_new(Client *c, struct bus_match_component *components, unsigned n_components, Match **_m) {
108         Match *m, *first;
109         int r;
110
111         assert(c);
112         assert(_m);
113
114         r = hashmap_ensure_allocated(&c->matches, string_hash_func, string_compare_func);
115         if (r < 0)
116                 return r;
117
118         m = new0(Match, 1);
119         if (!m)
120                 return -ENOMEM;
121
122         m->match = bus_match_to_string(components, n_components);
123         if (!m->match) {
124                 r = -ENOMEM;
125                 goto fail;
126         }
127
128         m->cookie = ++c->next_cookie;
129
130         first = hashmap_get(c->matches, m->match);
131         LIST_PREPEND(matches, first, m);
132         r = hashmap_replace(c->matches, m->match, first);
133         if (r < 0) {
134                 LIST_REMOVE(matches, first, m);
135                 goto fail;
136         }
137
138         m->client = c;
139         c->n_matches++;
140
141         *_m = m;
142         m = NULL;
143
144         return 0;
145
146 fail:
147         match_free(m);
148         return r;
149 }
150
151 static int on_name_owner_changed(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error);
152
153 static void client_free(Client *c) {
154         Match *m;
155
156         if (!c)
157                 return;
158
159         if (c->context) {
160                 if (c->watch)
161                         sd_bus_remove_match(c->context->bus, c->watch, on_name_owner_changed, c);
162
163                 assert_se(hashmap_remove(c->context->clients, &c->id) == c);
164         }
165
166         while ((m = hashmap_first(c->matches)))
167                 match_free(m);
168
169         hashmap_free(c->matches);
170         free(c->watch);
171
172         free(c);
173 }
174
175 static int on_name_owner_changed(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
176         Client *c = userdata;
177
178         assert(bus);
179         assert(m);
180
181         client_free(c);
182         return 0;
183 }
184
185 static int client_acquire(Context *context, uint64_t id, Client **_c) {
186         char *watch = NULL;
187         Client *c;
188         int r;
189
190         assert(context);
191         assert(_c);
192
193         c = hashmap_get(context->clients, &id);
194         if (c) {
195                 *_c = c;
196                 return 0;
197         }
198
199         if (hashmap_size(context->clients) >= CLIENTS_MAX)
200                 return -ENOBUFS;
201
202         r = hashmap_ensure_allocated(&context->clients, uint64_hash_func, uint64_compare_func);
203         if (r < 0)
204                 return r;
205
206         c = new0(Client, 1);
207         if (!c)
208                 return -ENOMEM;
209
210         c->id = id;
211
212         r = hashmap_put(context->clients, &c->id, c);
213         if (r < 0)
214                 goto fail;
215
216         c->context = context;
217
218         if (asprintf(&watch,
219                      "type='signal',"
220                      "sender='org.freedesktop.DBus',"
221                      "path='/org/freedesktop/DBus',"
222                      "interface='org.freedesktop.DBus',"
223                      "member='NameOwnerChanged',"
224                      "arg0=':1.%llu'", (unsigned long long) id) < 0) {
225                 r = -ENOMEM;
226                 goto fail;
227         }
228
229         r = sd_bus_add_match(context->bus, watch, on_name_owner_changed, c);
230         if (r < 0) {
231                 free(watch);
232                 goto fail;
233         }
234
235         c->watch = watch;
236
237         *_c = c;
238         return 0;
239
240 fail:
241         client_free(c);
242         return r;
243 }
244
245 static int driver_add_match(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
246
247         struct bus_match_component *components = NULL;
248         Context *context = userdata;
249         unsigned n_components = 0;
250         Match *m = NULL;
251         Client *c = NULL;
252         char *arg0;
253         uint64_t id;
254         int r;
255
256         assert(bus);
257         assert(message);
258         assert(context);
259
260         r = sd_bus_message_read(message, "s", &arg0);
261         if (r < 0)
262                 return r;
263
264         r = bus_kernel_parse_unique_name(message->sender, &id);
265         if (r < 0)
266                 return r;
267
268         r = client_acquire(context, id, &c);
269         if (r == -ENOBUFS)
270                 return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Reached limit of %u clients", CLIENTS_MAX);
271         if (r < 0)
272                 return r;
273
274         if (c->n_matches >= MATCHES_MAX) {
275                 r = sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Reached limit of %u matches per client", MATCHES_MAX);
276                 goto fail;
277         }
278
279         r = bus_match_parse(arg0, &components, &n_components);
280         if (r < 0) {
281                 r = sd_bus_error_setf(error, SD_BUS_ERROR_MATCH_RULE_INVALID, "Match rule \"%s\" is not valid", arg0);
282                 goto fail;
283         }
284
285         r = match_new(c, components, n_components, &m);
286         if (r < 0)
287                 goto fail;
288
289         r = bus_add_match_internal_kernel(bus, id, components, n_components, m->cookie);
290         if (r < 0)
291                 goto fail;
292
293         bus_match_parse_free(components, n_components);
294
295         return sd_bus_reply_method_return(message, NULL);
296
297 fail:
298         bus_match_parse_free(components, n_components);
299
300         match_free(m);
301
302         if (c->n_matches <= 0)
303                 client_free(c);
304
305         return r;
306 }
307
308 static int driver_remove_match(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
309
310         struct bus_match_component *components = NULL;
311         _cleanup_free_ char *normalized = NULL;
312         Context *context = userdata;
313         unsigned n_components = 0;
314         Client *c = NULL;
315         Match *m = NULL;
316         char *arg0;
317         uint64_t id;
318         int r;
319
320         assert(bus);
321         assert(message);
322         assert(context);
323
324         r = sd_bus_message_read(message, "s", &arg0);
325         if (r < 0)
326                 return r;
327
328         r = bus_kernel_parse_unique_name(message->sender, &id);
329         if (r < 0)
330                 return r;
331
332         c = hashmap_get(context->clients, &id);
333         if (!c)
334                 return sd_bus_error_setf(error, SD_BUS_ERROR_MATCH_RULE_NOT_FOUND, "You have not registered any matches.");
335
336         r = bus_match_parse(arg0, &components, &n_components);
337         if (r < 0) {
338                 r = sd_bus_error_setf(error, SD_BUS_ERROR_MATCH_RULE_INVALID, "Match rule \"%s\" is not valid", arg0);
339                 goto finish;
340         }
341
342         normalized = bus_match_to_string(components, n_components);
343         if (!normalized) {
344                 r = -ENOMEM;
345                 goto finish;
346         }
347
348         m = hashmap_get(c->matches, normalized);
349         if (!m) {
350                 r = sd_bus_error_setf(error, SD_BUS_ERROR_MATCH_RULE_NOT_FOUND, "Match rule \"%s\" not found.", normalized);
351                 goto finish;
352         }
353
354         bus_remove_match_internal_kernel(bus, id, m->cookie);
355         match_free(m);
356
357         r = sd_bus_reply_method_return(message, NULL);
358
359 finish:
360         bus_match_parse_free(components, n_components);
361
362         if (c->n_matches <= 0)
363                 client_free(c);
364
365         return r;
366 }
367
368 static int get_creds_by_name(sd_bus *bus, const char *name, uint64_t mask, sd_bus_creds **_creds, sd_bus_error *error) {
369         _cleanup_bus_creds_unref_ sd_bus_creds *c = NULL;
370         int r;
371
372         assert(bus);
373         assert(name);
374         assert(_creds);
375
376         assert_return(service_name_is_valid(name), -EINVAL);
377
378         r = sd_bus_get_owner(bus, name, mask, &c);
379         if (r == -ENOENT || r == -ENXIO)
380                 return sd_bus_error_setf(error, SD_BUS_ERROR_NAME_HAS_NO_OWNER, "Name %s is currently not owned by anyone.", name);
381         if (r < 0)
382                 return r;
383
384         if ((c->mask & mask) != mask)
385                 return -ENOTSUP;
386
387         *_creds = c;
388         c = NULL;
389
390         return 0;
391 }
392
393
394 static int get_creds_by_message(sd_bus *bus, sd_bus_message *m, uint64_t mask, sd_bus_creds **_creds, sd_bus_error *error) {
395         const char *name;
396         int r;
397
398         assert(bus);
399         assert(m);
400         assert(_creds);
401
402         r = sd_bus_message_read(m, "s", &name);
403         if (r < 0)
404                 return r;
405
406         return get_creds_by_name(bus, name, mask, _creds, error);
407 }
408
409 static int driver_get_security_context(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
410         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
411         _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
412         int r;
413
414         r = get_creds_by_message(bus, m, SD_BUS_CREDS_SELINUX_CONTEXT, &creds, error);
415         if (r < 0)
416                 return r;
417
418         r = sd_bus_message_new_method_return(m, &reply);
419         if (r < 0)
420                 return r;
421
422         r = sd_bus_message_append_array(reply, 'y', creds->label, strlen(creds->label));
423         if (r < 0)
424                 return r;
425
426         return sd_bus_send(bus, reply, NULL);
427 }
428
429 static int driver_get_pid(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
430         _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
431         int r;
432
433         r = get_creds_by_message(bus, m, SD_BUS_CREDS_PID, &creds, error);
434         if (r < 0)
435                 return r;
436
437         return sd_bus_reply_method_return(m, "u", (uint32_t) creds->pid);
438 }
439
440 static int driver_get_user(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
441         _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
442         int r;
443
444         r = get_creds_by_message(bus, m, SD_BUS_CREDS_UID, &creds, error);
445         if (r < 0)
446                 return r;
447
448         return sd_bus_reply_method_return(m, "u", (uint32_t) creds->uid);
449 }
450
451 static int driver_get_name_owner(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
452         _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
453         const char *name;
454         int r;
455
456         r = sd_bus_message_read(m, "s", &name);
457         if (r < 0)
458                 return r;
459
460         /* Here's a special exception for compatibility with dbus1:
461          * the bus name of the driver is owned by itself, not by a
462          * unique ID. */
463         if (streq(name, "org.freedesktop.DBus"))
464                 return sd_bus_reply_method_return(m, "s", "org.freedesktop.DBus");
465
466         r = get_creds_by_name(bus, name, SD_BUS_CREDS_UNIQUE_NAME, &creds, error);
467         if (r < 0)
468                 return r;
469
470         return sd_bus_reply_method_return(m, "s", creds->unique_name);
471 }
472
473 static int driver_get_id(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
474         sd_id128_t server_id;
475         char buf[SD_ID128_STRING_MAX];
476         int r;
477
478         r = sd_bus_get_server_id(bus, &server_id);
479         if (r < 0)
480                 return r;
481
482         return sd_bus_reply_method_return(m, "s", sd_id128_to_string(server_id, buf));
483 }
484
485 static int driver_hello(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
486         return sd_bus_reply_method_return(m, "s", m->sender);
487 }
488
489 static int return_strv(sd_bus *bus, sd_bus_message *m, char **l) {
490         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
491         int r;
492
493         r = sd_bus_message_new_method_return(m, &reply);
494         if (r < 0)
495                 return r;
496
497         r = sd_bus_message_append_strv(reply, l);
498         if (r < 0)
499                 return r;
500
501         return sd_bus_send(bus, reply, NULL);
502 }
503
504 static int driver_list_names(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
505         _cleanup_strv_free_ char **names = NULL;
506         int r;
507
508         r = sd_bus_list_names(bus, &names, NULL);
509         if (r < 0)
510                 return r;
511
512         /* Let's sort the names list to make it stable */
513         strv_sort(names);
514
515         return return_strv(bus, m, names);
516 }
517
518 static int driver_list_activatable_names(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
519         _cleanup_strv_free_ char **names = NULL;
520         int r;
521
522         r = sd_bus_list_names(bus, NULL, &names);
523         if (r < 0)
524                 return r;
525
526         /* Let's sort the names list to make it stable */
527         strv_sort(names);
528
529         return return_strv(bus, m, names);
530 }
531
532 static int driver_list_queued_owners(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
533         struct kdbus_cmd_name_list cmd = {};
534         struct kdbus_name_list *name_list;
535         struct kdbus_cmd_name *name;
536         _cleanup_strv_free_ char **owners = NULL;
537         char *arg0;
538         int r;
539
540         r = sd_bus_message_read(m, "s", &arg0);
541         if (r < 0)
542                 return r;
543
544         assert_return(service_name_is_valid(arg0), -EINVAL);
545
546         cmd.flags = KDBUS_NAME_LIST_QUEUED;
547
548         r = ioctl(bus->input_fd, KDBUS_CMD_NAME_LIST, &cmd);
549         if (r < 0)
550                 return -errno;
551
552         name_list = (struct kdbus_name_list *) ((uint8_t *) bus->kdbus_buffer + cmd.offset);
553
554         KDBUS_ITEM_FOREACH(name, name_list, names) {
555                 char *n;
556
557                 if (name->size <= sizeof(*name))
558                         continue;
559
560                 if (!streq(name->name, arg0))
561                         continue;
562
563                 if (asprintf(&n, ":1.%llu", (unsigned long long) name->owner_id) < 0)
564                         return -ENOMEM;
565
566                 r = strv_push(&owners, n);
567                 if (r < 0) {
568                         free(n);
569                         return -ENOMEM;
570                 }
571         }
572
573         r = ioctl(bus->input_fd, KDBUS_CMD_FREE, &cmd.offset);
574         if (r < 0)
575                 return -errno;
576
577         return return_strv(bus, m, owners);
578 }
579
580 static int driver_name_has_owner(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
581         const char *name;
582         int r;
583
584         r = sd_bus_message_read(m, "s", &name);
585         if (r < 0)
586                 return r;
587
588         assert_return(service_name_is_valid(name), -EINVAL);
589
590         r = sd_bus_get_owner(bus, name, 0, NULL);
591         if (r < 0 && r != -ENOENT && r != -ENXIO)
592                 return r;
593
594         return sd_bus_reply_method_return(m, "b", r >= 0);
595 }
596
597 static int driver_request_name(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
598         struct kdbus_cmd_name *n;
599         uint32_t flags;
600         size_t size, l;
601         uint64_t id;
602         const char *name;
603         int r;
604
605         r = sd_bus_message_read(m, "su", &name, &flags);
606         if (r < 0)
607                 return r;
608
609         assert_return(service_name_is_valid(name), -EINVAL);
610         assert_return((flags & ~(BUS_NAME_ALLOW_REPLACEMENT|BUS_NAME_REPLACE_EXISTING|BUS_NAME_DO_NOT_QUEUE)) == 0, -EINVAL);
611
612         l = strlen(name);
613         size = offsetof(struct kdbus_cmd_name, name) + l + 1;
614         n = alloca0(size);
615         n->size = size;
616         memcpy(n->name, name, l+1);
617         kdbus_translate_request_name_flags(flags, (uint64_t *) &n->flags);
618
619         /* This function is open-coded because we request the name 'on behalf'
620          * of the requesting connection */
621         r = bus_kernel_parse_unique_name(m->sender, &id);
622         if (r < 0)
623                 return r;
624
625         n->owner_id = id;
626
627         r = ioctl(bus->input_fd, KDBUS_CMD_NAME_ACQUIRE, n);
628         if (r < 0) {
629                 if (errno == EEXIST)
630                         return sd_bus_reply_method_return(m, "u", BUS_NAME_EXISTS);
631                 if (errno == EALREADY)
632                         return sd_bus_reply_method_return(m, "u", BUS_NAME_ALREADY_OWNER);
633
634                 return -errno;
635         }
636
637         if (n->flags & KDBUS_NAME_IN_QUEUE)
638                 return sd_bus_reply_method_return(m, "u", BUS_NAME_IN_QUEUE);
639
640         return sd_bus_reply_method_return(m, "u", BUS_NAME_PRIMARY_OWNER);
641 }
642
643 static int driver_release_name(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
644         struct kdbus_cmd_name *n;
645         const char *name;
646         size_t l, size;
647         uint64_t id;
648         int r;
649
650         r = sd_bus_message_read(m, "s", &name);
651         if (r < 0)
652                 return r;
653
654         assert_return(service_name_is_valid(name), -EINVAL);
655
656         l = strlen(name);
657         size = offsetof(struct kdbus_cmd_name, name) + l + 1;
658         n = alloca0(size);
659         n->size = size;
660         memcpy(n->name, name, l+1);
661
662         /* This function is open-coded because we request the name 'on behalf'
663          * of the requesting connection */
664         r = bus_kernel_parse_unique_name(m->sender, &id);
665         if (r < 0)
666                 return r;
667
668         n->owner_id = id;
669
670         r = ioctl(bus->input_fd, KDBUS_CMD_NAME_RELEASE, n);
671         if (r < 0) {
672                 if (errno == ESRCH)
673                         return sd_bus_reply_method_return(m, "u", BUS_NAME_NON_EXISTENT);
674                 if (errno == EADDRINUSE)
675                         return sd_bus_reply_method_return(m, "u", BUS_NAME_NOT_OWNER);
676                 return -errno;
677         }
678
679         return sd_bus_reply_method_return(m, "u", BUS_NAME_RELEASED);
680 }
681
682 static int driver_start_service_by_name(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
683         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
684         _cleanup_strv_free_ char **t = NULL;
685         _cleanup_free_ char *path = NULL;
686         uint32_t flags;
687         char *name, *u;
688         int r;
689
690         r = sd_bus_message_read(m, "su", &name, &flags);
691         if (r < 0)
692                 return r;
693
694         assert_return(service_name_is_valid(name), -EINVAL);
695         assert_return(flags == 0, -ENOTSUP);
696
697         r = sd_bus_get_owner(bus, name, 0, NULL);
698         if (r >= 0)
699                 return sd_bus_reply_method_return(m, "u", BUS_START_REPLY_ALREADY_RUNNING);
700         if (r != -ENOENT)
701                 return r;
702
703         u = strappenda(name, ".busname");
704
705         path = unit_dbus_path_from_name(u);
706         if (!path)
707                 return -ENOMEM;
708
709         r = sd_bus_get_property_strv(
710                         bus,
711                         "org.freedesktop.systemd1",
712                         path,
713                         "org.freedesktop.systemd1.Unit",
714                         "Triggers",
715                         error,
716                         &t);
717         if (r < 0)
718                 return r;
719
720         if (!t || !t[0] || t[1])
721                 return sd_bus_error_setf(error, SD_BUS_ERROR_SERVICE_UNKNOWN, "Bus name %s not found.", name);
722
723         r = sd_bus_call_method(
724                         bus,
725                         "org.freedesktop.systemd1",
726                         "/org/freedesktop/systemd1",
727                         "org.freedesktop.systemd1.Manager",
728                         "StartUnit",
729                         error,
730                         &reply,
731                         "ss",
732                         t[0],
733                         "replace");
734         if (r < 0)
735                 return r;
736
737         return sd_bus_reply_method_return(m, "u", BUS_START_REPLY_SUCCESS);
738 }
739
740 static int driver_update_environment(sd_bus*bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
741         _cleanup_bus_message_unref_ sd_bus_message *msg = NULL;
742         _cleanup_strv_free_ char **args = NULL;
743         int r;
744
745         r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{ss}");
746         if (r < 0)
747                 return r;
748
749        while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "ss")) > 0) {
750                 _cleanup_free_ char *s = NULL;
751                 const char *key;
752                 const char *value;
753
754                 r = sd_bus_message_read(m, "ss", &key, &value);
755                 if (r < 0)
756                         return r;
757
758                 s = strjoin(key, "=", value, NULL);
759                 if (!s)
760                         return ENOMEM;
761
762                 r  = strv_extend(&args, s);
763                 if (r < 0)
764                         return r;
765
766                 r = sd_bus_message_exit_container(m);
767                 if (r < 0)
768                         return r;
769         }
770
771         r = sd_bus_message_exit_container(m);
772         if (r < 0)
773                 return r;
774
775         if (!args)
776                 return -EINVAL;
777
778         r = sd_bus_message_new_method_call(
779                         bus,
780                         "org.freedesktop.systemd1",
781                         "/org/freedesktop/systemd1",
782                         "org.freedesktop.systemd1.Manager",
783                         "SetEnvironment",
784                         &msg);
785         if (r < 0)
786                 return r;
787
788         r = sd_bus_message_append_strv(msg, args);
789         if (r < 0)
790                 return r;
791
792         r = sd_bus_call(bus, msg, 0, NULL, NULL);
793         if (r < 0)
794                 return r;
795
796         return sd_bus_reply_method_return(m, NULL);
797 }
798
799 static int driver_unsupported(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
800         return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "%s() is not supported", sd_bus_message_get_member(m));
801 }
802
803 static const sd_bus_vtable driver_vtable[] = {
804         SD_BUS_VTABLE_START(0),
805         SD_BUS_METHOD("AddMatch", "s", NULL, driver_add_match, SD_BUS_VTABLE_UNPRIVILEGED),
806         SD_BUS_METHOD("GetConnectionSELinuxSecurityContext", "s", "ay", driver_get_security_context, SD_BUS_VTABLE_UNPRIVILEGED),
807         SD_BUS_METHOD("GetConnectionUnixProcessID", "s", "u", driver_get_pid, SD_BUS_VTABLE_UNPRIVILEGED),
808         SD_BUS_METHOD("GetConnectionUnixUser", "s", "u", driver_get_user, SD_BUS_VTABLE_UNPRIVILEGED),
809         SD_BUS_METHOD("GetId", NULL, "s", driver_get_id, SD_BUS_VTABLE_UNPRIVILEGED),
810         SD_BUS_METHOD("GetNameOwner", "s", "s", driver_get_name_owner, SD_BUS_VTABLE_UNPRIVILEGED),
811         SD_BUS_METHOD("Hello", NULL, "s", driver_hello, SD_BUS_VTABLE_UNPRIVILEGED),
812         SD_BUS_METHOD("ListActivatableNames", NULL, "as", driver_list_activatable_names, SD_BUS_VTABLE_UNPRIVILEGED),
813         SD_BUS_METHOD("ListNames", NULL, "as", driver_list_names, SD_BUS_VTABLE_UNPRIVILEGED),
814         SD_BUS_METHOD("ListQueuedOwners", "s", "as", driver_list_queued_owners, SD_BUS_VTABLE_UNPRIVILEGED),
815         SD_BUS_METHOD("NameHasOwner", "s", "b", driver_name_has_owner, SD_BUS_VTABLE_UNPRIVILEGED),
816         SD_BUS_METHOD("ReleaseName", "s", "u", driver_release_name, SD_BUS_VTABLE_UNPRIVILEGED),
817         SD_BUS_METHOD("ReloadConfig", NULL, NULL, driver_unsupported, SD_BUS_VTABLE_DEPRECATED),
818         SD_BUS_METHOD("RemoveMatch", "s", NULL, driver_remove_match, SD_BUS_VTABLE_UNPRIVILEGED),
819         SD_BUS_METHOD("RequestName", "su", "u", driver_request_name, SD_BUS_VTABLE_UNPRIVILEGED),
820         SD_BUS_METHOD("StartServiceByName", "su", "u", driver_start_service_by_name, SD_BUS_VTABLE_UNPRIVILEGED),
821         SD_BUS_METHOD("UpdateActivationEnvironment", "a{ss}", NULL, driver_update_environment, 0),
822         SD_BUS_SIGNAL("NameAcquired", "s", SD_BUS_VTABLE_DEPRECATED),
823         SD_BUS_SIGNAL("NameLost", "s", SD_BUS_VTABLE_DEPRECATED),
824         SD_BUS_SIGNAL("NameOwnerChanged", "sss", 0),
825         SD_BUS_VTABLE_END
826 };
827
828 static int find_object(
829                 sd_bus *bus,
830                 const char *path,
831                 const char *interface,
832                 void *userdata,
833                 void **ret_found,
834                 sd_bus_error *ret_error) {
835
836         /* We support the driver interface on exactly two different
837          * paths: the root and the entry point object. This is a bit
838          * different from the original dbus-daemon which supported it
839          * on any path. */
840
841         if (streq_ptr(path, "/"))
842                 return 1;
843
844         if (streq_ptr(path, "/org/freedesktop/DBus"))
845                 return 1;
846
847         return 0;
848 }
849
850 static int node_enumerator(
851                 sd_bus *bus,
852                 const char *path,
853                 void *userdata,
854                 char ***ret_nodes,
855                 sd_bus_error *ret_error) {
856
857         char **l;
858
859         l = strv_new("/", "/org/freedesktop/DBus", NULL);
860         if (!l)
861                 return -ENOMEM;
862
863         *ret_nodes = l;
864         return 0;
865 }
866
867 static int connect_bus(Context *c) {
868         int r;
869
870         assert(c);
871
872         r = sd_bus_default(&c->bus);
873         if (r < 0) {
874                 log_error("Failed to create bus: %s", strerror(-r));
875                 return r;
876         }
877
878         if (!c->bus->is_kernel) {
879                 log_error("Not running on kdbus");
880                 return -EPERM;
881         }
882
883         r = sd_bus_add_fallback_vtable(c->bus, "/", "org.freedesktop.DBus", driver_vtable, find_object, c);
884         if (r < 0) {
885                 log_error("Failed to add manager object vtable: %s", strerror(-r));
886                 return r;
887         }
888
889         r = sd_bus_add_node_enumerator(c->bus, "/", node_enumerator, c);
890         if (r < 0) {
891                 log_error("Failed to add node enumerator: %s", strerror(-r));
892                 return r;
893         }
894
895         r = sd_bus_request_name(c->bus, "org.freedesktop.DBus", 0);
896         if (r < 0) {
897                 log_error("Unable to request name: %s", strerror(-r));
898                 return r;
899         }
900
901         r = sd_bus_attach_event(c->bus, c->event, 0);
902         if (r < 0) {
903                 log_error("Error while adding bus to event loop: %s", strerror(-r));
904                 return r;
905         }
906
907         return 0;
908 }
909
910 static bool check_idle(void *userdata) {
911         Context *c = userdata;
912         assert(c);
913
914         return hashmap_isempty(c->clients);
915 }
916
917 int main(int argc, char *argv[]) {
918         Context context = {};
919         Client *c;
920         int r;
921
922         log_set_target(LOG_TARGET_AUTO);
923         log_parse_environment();
924         log_open();
925
926         if (argc != 1) {
927                 log_error("This program takes no arguments.");
928                 r = -EINVAL;
929                 goto finish;
930         }
931
932         r = sd_event_default(&context.event);
933         if (r < 0) {
934                 log_error("Failed to allocate event loop: %s", strerror(-r));
935                 goto finish;
936         }
937
938         sd_event_set_watchdog(context.event, true);
939
940         r = connect_bus(&context);
941         if (r < 0)
942                 goto finish;
943
944         r = bus_event_loop_with_idle(context.event, context.bus, "org.freedesktop.DBus", DEFAULT_EXIT_USEC, check_idle, &context);
945         if (r < 0) {
946                 log_error("Failed to run event loop: %s", strerror(-r));
947                 goto finish;
948         }
949
950 finish:
951         while ((c = hashmap_first(context.clients)))
952                 client_free(c);
953
954         sd_bus_unref(context.bus);
955         sd_event_unref(context.event);
956
957         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
958 }