chiark / gitweb /
3284d625340dc8897082d78b73cfe53d991dffcc
[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
33 #include "kdbus.h"
34 #include "sd-bus.h"
35 #include "bus-internal.h"
36 #include "sd-daemon.h"
37 #include "sd-event.h"
38 #include "event-util.h"
39 #include "bus-util.h"
40 #include "bus-error.h"
41 #include "bus-message.h"
42 #include "bus-kernel.h"
43 #include "socket-util.h"
44 #include "util.h"
45 #include "build.h"
46 #include "strv.h"
47 #include "sd-id128.h"
48 #include "async.h"
49 #include "hashmap.h"
50 #include "def.h"
51 #include "unit-name.h"
52 #include "bus-control.h"
53 #include "cgroup-util.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, first->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.", normalized);
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 get_creds_by_name(sd_bus *bus, const char *name, uint64_t mask, sd_bus_creds **_creds, sd_bus_error *error) {
368         _cleanup_bus_creds_unref_ sd_bus_creds *c = NULL;
369         int r;
370
371         assert(bus);
372         assert(name);
373         assert(_creds);
374
375         assert_return(service_name_is_valid(name), -EINVAL);
376
377         r = sd_bus_get_owner(bus, name, mask, &c);
378         if (r == -ENOENT || r == -ENXIO)
379                 return sd_bus_error_setf(error, SD_BUS_ERROR_NAME_HAS_NO_OWNER, "Name %s is currently not owned by anyone.", name);
380         if (r < 0)
381                 return r;
382
383         if ((c->mask & mask) != mask)
384                 return -ENOTSUP;
385
386         *_creds = c;
387         c = NULL;
388
389         return 0;
390 }
391
392
393 static int get_creds_by_message(sd_bus *bus, sd_bus_message *m, uint64_t mask, sd_bus_creds **_creds, sd_bus_error *error) {
394         const char *name;
395         int r;
396
397         assert(bus);
398         assert(m);
399         assert(_creds);
400
401         r = sd_bus_message_read(m, "s", &name);
402         if (r < 0)
403                 return r;
404
405         return get_creds_by_name(bus, name, mask, _creds, error);
406 }
407
408 static int driver_get_security_context(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
409         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
410         _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
411         int r;
412
413         r = get_creds_by_message(bus, m, SD_BUS_CREDS_SELINUX_CONTEXT, &creds, error);
414         if (r < 0)
415                 return r;
416
417         r = sd_bus_message_new_method_return(m, &reply);
418         if (r < 0)
419                 return r;
420
421         r = sd_bus_message_append_array(reply, 'y', creds->label, strlen(creds->label));
422         if (r < 0)
423                 return r;
424
425         return sd_bus_send(bus, reply, NULL);
426 }
427
428 static int driver_get_pid(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
429         _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
430         int r;
431
432         r = get_creds_by_message(bus, m, SD_BUS_CREDS_PID, &creds, error);
433         if (r < 0)
434                 return r;
435
436         return sd_bus_reply_method_return(m, "u", (uint32_t) creds->pid);
437 }
438
439 static int driver_get_user(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
440         _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
441         int r;
442
443         r = get_creds_by_message(bus, m, SD_BUS_CREDS_UID, &creds, error);
444         if (r < 0)
445                 return r;
446
447         return sd_bus_reply_method_return(m, "u", (uint32_t) creds->uid);
448 }
449
450 static int driver_get_name_owner(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
451         _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
452         const char *name;
453         int r;
454
455         r = sd_bus_message_read(m, "s", &name);
456         if (r < 0)
457                 return r;
458
459         /* Here's a special exception for compatibility with dbus1:
460          * the bus name of the driver is owned by itself, not by a
461          * unique ID. */
462         if (streq(name, "org.freedesktop.DBus"))
463                 return sd_bus_reply_method_return(m, "s", "org.freedesktop.DBus");
464
465         r = get_creds_by_name(bus, name, SD_BUS_CREDS_UNIQUE_NAME, &creds, error);
466         if (r < 0)
467                 return r;
468
469         return sd_bus_reply_method_return(m, "s", creds->unique_name);
470 }
471
472 static int driver_get_id(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
473         sd_id128_t server_id;
474         char buf[SD_ID128_STRING_MAX];
475         int r;
476
477         r = sd_bus_get_server_id(bus, &server_id);
478         if (r < 0)
479                 return r;
480
481         return sd_bus_reply_method_return(m, "s", sd_id128_to_string(server_id, buf));
482 }
483
484 static int driver_hello(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
485         return sd_bus_reply_method_return(m, "s", m->sender);
486 }
487
488 static int return_strv(sd_bus *bus, sd_bus_message *m, char **l) {
489         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
490         int r;
491
492         r = sd_bus_message_new_method_return(m, &reply);
493         if (r < 0)
494                 return r;
495
496         r = sd_bus_message_append_strv(reply, l);
497         if (r < 0)
498                 return r;
499
500         return sd_bus_send(bus, reply, NULL);
501 }
502
503 static int driver_list_names(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
504         _cleanup_strv_free_ char **names = NULL;
505         int r;
506
507         r = sd_bus_list_names(bus, &names, NULL);
508         if (r < 0)
509                 return r;
510
511         /* Let's sort the names list to make it stable */
512         strv_sort(names);
513
514         return return_strv(bus, m, names);
515 }
516
517 static int driver_list_activatable_names(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
518         _cleanup_strv_free_ char **names = NULL;
519         int r;
520
521         r = sd_bus_list_names(bus, NULL, &names);
522         if (r < 0)
523                 return r;
524
525         /* Let's sort the names list to make it stable */
526         strv_sort(names);
527
528         return return_strv(bus, m, names);
529 }
530
531 static int driver_list_queued_owners(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
532         struct kdbus_cmd_name_list cmd = {};
533         struct kdbus_name_list *name_list;
534         struct kdbus_cmd_name *name;
535         _cleanup_strv_free_ char **owners = NULL;
536         char *arg0;
537         int r;
538
539         r = sd_bus_message_read(m, "s", &arg0);
540         if (r < 0)
541                 return r;
542
543         assert_return(service_name_is_valid(arg0), -EINVAL);
544
545         cmd.flags = KDBUS_NAME_LIST_QUEUED;
546
547         r = ioctl(bus->input_fd, KDBUS_CMD_NAME_LIST, &cmd);
548         if (r < 0)
549                 return -errno;
550
551         name_list = (struct kdbus_name_list *) ((uint8_t *) bus->kdbus_buffer + cmd.offset);
552
553         KDBUS_ITEM_FOREACH(name, name_list, names) {
554                 char *n;
555
556                 if (name->size <= sizeof(*name))
557                         continue;
558
559                 if (!streq(name->name, arg0))
560                         continue;
561
562                 if (asprintf(&n, ":1.%llu", (unsigned long long) name->owner_id) < 0)
563                         return -ENOMEM;
564
565                 r = strv_consume(&owners, n);
566                 if (r < 0)
567                         return r;
568         }
569
570         r = ioctl(bus->input_fd, KDBUS_CMD_FREE, &cmd.offset);
571         if (r < 0)
572                 return -errno;
573
574         return return_strv(bus, m, owners);
575 }
576
577 static int driver_name_has_owner(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
578         const char *name;
579         int r;
580
581         r = sd_bus_message_read(m, "s", &name);
582         if (r < 0)
583                 return r;
584
585         assert_return(service_name_is_valid(name), -EINVAL);
586
587         r = sd_bus_get_owner(bus, name, 0, NULL);
588         if (r < 0 && r != -ENOENT && r != -ENXIO)
589                 return r;
590
591         return sd_bus_reply_method_return(m, "b", r >= 0);
592 }
593
594 static int driver_request_name(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
595         struct kdbus_cmd_name *n;
596         uint32_t flags;
597         size_t size, l;
598         uint64_t id;
599         const char *name;
600         int r;
601
602         r = sd_bus_message_read(m, "su", &name, &flags);
603         if (r < 0)
604                 return r;
605
606         assert_return(service_name_is_valid(name), -EINVAL);
607         assert_return((flags & ~(BUS_NAME_ALLOW_REPLACEMENT|BUS_NAME_REPLACE_EXISTING|BUS_NAME_DO_NOT_QUEUE)) == 0, -EINVAL);
608
609         l = strlen(name);
610         size = offsetof(struct kdbus_cmd_name, name) + l + 1;
611         n = alloca0(size);
612         n->size = size;
613         memcpy(n->name, name, l+1);
614         kdbus_translate_request_name_flags(flags, (uint64_t *) &n->flags);
615
616         /* This function is open-coded because we request the name 'on behalf'
617          * of the requesting connection */
618         r = bus_kernel_parse_unique_name(m->sender, &id);
619         if (r < 0)
620                 return r;
621
622         n->owner_id = id;
623
624         r = ioctl(bus->input_fd, KDBUS_CMD_NAME_ACQUIRE, n);
625         if (r < 0) {
626                 if (errno == EEXIST)
627                         return sd_bus_reply_method_return(m, "u", BUS_NAME_EXISTS);
628                 if (errno == EALREADY)
629                         return sd_bus_reply_method_return(m, "u", BUS_NAME_ALREADY_OWNER);
630
631                 return -errno;
632         }
633
634         if (n->flags & KDBUS_NAME_IN_QUEUE)
635                 return sd_bus_reply_method_return(m, "u", BUS_NAME_IN_QUEUE);
636
637         return sd_bus_reply_method_return(m, "u", BUS_NAME_PRIMARY_OWNER);
638 }
639
640 static int driver_release_name(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
641         struct kdbus_cmd_name *n;
642         const char *name;
643         size_t l, size;
644         uint64_t id;
645         int r;
646
647         r = sd_bus_message_read(m, "s", &name);
648         if (r < 0)
649                 return r;
650
651         assert_return(service_name_is_valid(name), -EINVAL);
652
653         l = strlen(name);
654         size = offsetof(struct kdbus_cmd_name, name) + l + 1;
655         n = alloca0(size);
656         n->size = size;
657         memcpy(n->name, name, l+1);
658
659         /* This function is open-coded because we request the name 'on behalf'
660          * of the requesting connection */
661         r = bus_kernel_parse_unique_name(m->sender, &id);
662         if (r < 0)
663                 return r;
664
665         n->owner_id = id;
666
667         r = ioctl(bus->input_fd, KDBUS_CMD_NAME_RELEASE, n);
668         if (r < 0) {
669                 if (errno == ESRCH)
670                         return sd_bus_reply_method_return(m, "u", BUS_NAME_NON_EXISTENT);
671                 if (errno == EADDRINUSE)
672                         return sd_bus_reply_method_return(m, "u", BUS_NAME_NOT_OWNER);
673                 return -errno;
674         }
675
676         return sd_bus_reply_method_return(m, "u", BUS_NAME_RELEASED);
677 }
678
679 static int driver_start_service_by_name(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
680         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
681         _cleanup_strv_free_ char **t = NULL;
682         _cleanup_free_ char *path = NULL;
683         uint32_t flags;
684         char *name, *u;
685         int r;
686
687         r = sd_bus_message_read(m, "su", &name, &flags);
688         if (r < 0)
689                 return r;
690
691         assert_return(service_name_is_valid(name), -EINVAL);
692         assert_return(flags == 0, -ENOTSUP);
693
694         r = sd_bus_get_owner(bus, name, 0, NULL);
695         if (r >= 0)
696                 return sd_bus_reply_method_return(m, "u", BUS_START_REPLY_ALREADY_RUNNING);
697         if (r != -ENOENT)
698                 return r;
699
700         u = strappenda(name, ".busname");
701
702         path = unit_dbus_path_from_name(u);
703         if (!path)
704                 return -ENOMEM;
705
706         r = sd_bus_get_property_strv(
707                         bus,
708                         "org.freedesktop.systemd1",
709                         path,
710                         "org.freedesktop.systemd1.Unit",
711                         "Triggers",
712                         error,
713                         &t);
714         if (r < 0)
715                 return r;
716
717         if (!t || !t[0] || t[1])
718                 return sd_bus_error_setf(error, SD_BUS_ERROR_SERVICE_UNKNOWN, "Bus name %s not found.", name);
719
720         r = sd_bus_call_method(
721                         bus,
722                         "org.freedesktop.systemd1",
723                         "/org/freedesktop/systemd1",
724                         "org.freedesktop.systemd1.Manager",
725                         "StartUnit",
726                         error,
727                         &reply,
728                         "ss",
729                         t[0],
730                         "replace");
731         if (r < 0)
732                 return r;
733
734         return sd_bus_reply_method_return(m, "u", BUS_START_REPLY_SUCCESS);
735 }
736
737 static int driver_update_environment(sd_bus*bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
738         _cleanup_bus_message_unref_ sd_bus_message *msg = NULL;
739         _cleanup_strv_free_ char **args = NULL;
740         int r;
741
742         r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{ss}");
743         if (r < 0)
744                 return r;
745
746        while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "ss")) > 0) {
747                 _cleanup_free_ char *s = NULL;
748                 const char *key;
749                 const char *value;
750
751                 r = sd_bus_message_read(m, "ss", &key, &value);
752                 if (r < 0)
753                         return r;
754
755                 s = strjoin(key, "=", value, NULL);
756                 if (!s)
757                         return ENOMEM;
758
759                 r  = strv_extend(&args, s);
760                 if (r < 0)
761                         return r;
762
763                 r = sd_bus_message_exit_container(m);
764                 if (r < 0)
765                         return r;
766         }
767
768         r = sd_bus_message_exit_container(m);
769         if (r < 0)
770                 return r;
771
772         if (!args)
773                 return -EINVAL;
774
775         r = sd_bus_message_new_method_call(
776                         bus,
777                         &msg,
778                         "org.freedesktop.systemd1",
779                         "/org/freedesktop/systemd1",
780                         "org.freedesktop.systemd1.Manager",
781                         "SetEnvironment");
782         if (r < 0)
783                 return r;
784
785         r = sd_bus_message_append_strv(msg, args);
786         if (r < 0)
787                 return r;
788
789         r = sd_bus_call(bus, msg, 0, NULL, NULL);
790         if (r < 0)
791                 return r;
792
793         return sd_bus_reply_method_return(m, NULL);
794 }
795
796 static int driver_unsupported(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
797         return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "%s() is not supported", sd_bus_message_get_member(m));
798 }
799
800 static const sd_bus_vtable driver_vtable[] = {
801         SD_BUS_VTABLE_START(0),
802         SD_BUS_METHOD("AddMatch", "s", NULL, driver_add_match, SD_BUS_VTABLE_UNPRIVILEGED),
803         SD_BUS_METHOD("GetConnectionSELinuxSecurityContext", "s", "ay", driver_get_security_context, SD_BUS_VTABLE_UNPRIVILEGED),
804         SD_BUS_METHOD("GetConnectionUnixProcessID", "s", "u", driver_get_pid, SD_BUS_VTABLE_UNPRIVILEGED),
805         SD_BUS_METHOD("GetConnectionUnixUser", "s", "u", driver_get_user, SD_BUS_VTABLE_UNPRIVILEGED),
806         SD_BUS_METHOD("GetId", NULL, "s", driver_get_id, SD_BUS_VTABLE_UNPRIVILEGED),
807         SD_BUS_METHOD("GetNameOwner", "s", "s", driver_get_name_owner, SD_BUS_VTABLE_UNPRIVILEGED),
808         SD_BUS_METHOD("Hello", NULL, "s", driver_hello, SD_BUS_VTABLE_UNPRIVILEGED),
809         SD_BUS_METHOD("ListActivatableNames", NULL, "as", driver_list_activatable_names, SD_BUS_VTABLE_UNPRIVILEGED),
810         SD_BUS_METHOD("ListNames", NULL, "as", driver_list_names, SD_BUS_VTABLE_UNPRIVILEGED),
811         SD_BUS_METHOD("ListQueuedOwners", "s", "as", driver_list_queued_owners, SD_BUS_VTABLE_UNPRIVILEGED),
812         SD_BUS_METHOD("NameHasOwner", "s", "b", driver_name_has_owner, SD_BUS_VTABLE_UNPRIVILEGED),
813         SD_BUS_METHOD("ReleaseName", "s", "u", driver_release_name, SD_BUS_VTABLE_UNPRIVILEGED),
814         SD_BUS_METHOD("ReloadConfig", NULL, NULL, driver_unsupported, SD_BUS_VTABLE_DEPRECATED),
815         SD_BUS_METHOD("RemoveMatch", "s", NULL, driver_remove_match, SD_BUS_VTABLE_UNPRIVILEGED),
816         SD_BUS_METHOD("RequestName", "su", "u", driver_request_name, SD_BUS_VTABLE_UNPRIVILEGED),
817         SD_BUS_METHOD("StartServiceByName", "su", "u", driver_start_service_by_name, SD_BUS_VTABLE_UNPRIVILEGED),
818         SD_BUS_METHOD("UpdateActivationEnvironment", "a{ss}", NULL, driver_update_environment, 0),
819         SD_BUS_SIGNAL("NameAcquired", "s", SD_BUS_VTABLE_DEPRECATED),
820         SD_BUS_SIGNAL("NameLost", "s", SD_BUS_VTABLE_DEPRECATED),
821         SD_BUS_SIGNAL("NameOwnerChanged", "sss", 0),
822         SD_BUS_VTABLE_END
823 };
824
825 static int find_object(
826                 sd_bus *bus,
827                 const char *path,
828                 const char *interface,
829                 void *userdata,
830                 void **ret_found,
831                 sd_bus_error *ret_error) {
832
833         /* We support the driver interface on exactly two different
834          * paths: the root and the entry point object. This is a bit
835          * different from the original dbus-daemon which supported it
836          * on any path. */
837
838         if (streq_ptr(path, "/"))
839                 return 1;
840
841         if (streq_ptr(path, "/org/freedesktop/DBus"))
842                 return 1;
843
844         return 0;
845 }
846
847 static int node_enumerator(
848                 sd_bus *bus,
849                 const char *path,
850                 void *userdata,
851                 char ***ret_nodes,
852                 sd_bus_error *ret_error) {
853
854         char **l;
855
856         l = strv_new("/", "/org/freedesktop/DBus", NULL);
857         if (!l)
858                 return -ENOMEM;
859
860         *ret_nodes = l;
861         return 0;
862 }
863
864 static int connect_bus(Context *c) {
865         int r;
866
867         assert(c);
868
869         r = sd_bus_default(&c->bus);
870         if (r < 0) {
871                 log_error("Failed to create bus: %s", strerror(-r));
872                 return r;
873         }
874
875         if (!c->bus->is_kernel) {
876                 log_error("Not running on kdbus");
877                 return -EPERM;
878         }
879
880         r = sd_bus_add_fallback_vtable(c->bus, "/", "org.freedesktop.DBus", driver_vtable, find_object, c);
881         if (r < 0) {
882                 log_error("Failed to add manager object vtable: %s", strerror(-r));
883                 return r;
884         }
885
886         r = sd_bus_add_node_enumerator(c->bus, "/", node_enumerator, c);
887         if (r < 0) {
888                 log_error("Failed to add node enumerator: %s", strerror(-r));
889                 return r;
890         }
891
892         r = sd_bus_request_name(c->bus, "org.freedesktop.DBus", 0);
893         if (r < 0) {
894                 log_error("Unable to request name: %s", strerror(-r));
895                 return r;
896         }
897
898         r = sd_bus_attach_event(c->bus, c->event, 0);
899         if (r < 0) {
900                 log_error("Error while adding bus to event loop: %s", strerror(-r));
901                 return r;
902         }
903
904         return 0;
905 }
906
907 static bool check_idle(void *userdata) {
908         Context *c = userdata;
909         assert(c);
910
911         return hashmap_isempty(c->clients);
912 }
913
914 int main(int argc, char *argv[]) {
915         Context context = {};
916         Client *c;
917         int r;
918
919         log_set_target(LOG_TARGET_AUTO);
920         log_parse_environment();
921         log_open();
922
923         if (argc != 1) {
924                 log_error("This program takes no arguments.");
925                 r = -EINVAL;
926                 goto finish;
927         }
928
929         r = sd_event_default(&context.event);
930         if (r < 0) {
931                 log_error("Failed to allocate event loop: %s", strerror(-r));
932                 goto finish;
933         }
934
935         sd_event_set_watchdog(context.event, true);
936
937         r = connect_bus(&context);
938         if (r < 0)
939                 goto finish;
940
941         r = bus_event_loop_with_idle(context.event, context.bus, "org.freedesktop.DBus", DEFAULT_EXIT_USEC, check_idle, &context);
942         if (r < 0) {
943                 log_error("Failed to run event loop: %s", strerror(-r));
944                 goto finish;
945         }
946
947 finish:
948         while ((c = hashmap_first(context.clients)))
949                 client_free(c);
950
951         sd_bus_unref(context.bus);
952         sd_event_unref(context.event);
953
954         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
955 }