2 This file is part of systemd.
4 Copyright 2013 Daniel Mack
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.
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.
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/>.
28 #include <sys/socket.h>
30 #include <sys/timex.h>
31 #include <sys/utsname.h>
36 #include "bus-internal.h"
37 #include "sd-daemon.h"
39 #include "event-util.h"
41 #include "bus-error.h"
42 #include "bus-message.h"
43 #include "bus-kernel.h"
44 #include "socket-util.h"
52 #include "unit-name.h"
53 #include "bus-control.h"
54 #include "cgroup-util.h"
56 #define CLIENTS_MAX 1024
57 #define MATCHES_MAX 1024
59 typedef struct Match Match;
60 typedef struct Client Client;
61 typedef struct Context Context;
67 LIST_FIELDS(Match, matches);
85 static void match_free(Match *m) {
93 first = hashmap_get(m->client->matches, m->match);
94 LIST_REMOVE(matches, first, m);
96 assert_se(hashmap_replace(m->client->matches, first->match, first) >= 0);
98 hashmap_remove(m->client->matches, m->match);
100 m->client->n_matches--;
107 static int match_new(Client *c, struct bus_match_component *components, unsigned n_components, Match **_m) {
114 r = hashmap_ensure_allocated(&c->matches, string_hash_func, string_compare_func);
122 m->match = bus_match_to_string(components, n_components);
128 m->cookie = ++c->next_cookie;
130 first = hashmap_get(c->matches, m->match);
131 LIST_PREPEND(matches, first, m);
132 r = hashmap_replace(c->matches, m->match, first);
134 LIST_REMOVE(matches, first, m);
151 static int on_name_owner_changed(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error);
153 static void client_free(Client *c) {
161 sd_bus_remove_match(c->context->bus, c->watch, on_name_owner_changed, c);
163 assert_se(hashmap_remove(c->context->clients, &c->id) == c);
166 while ((m = hashmap_first(c->matches)))
169 hashmap_free(c->matches);
175 static int on_name_owner_changed(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
176 Client *c = userdata;
185 static int client_acquire(Context *context, uint64_t id, Client **_c) {
193 c = hashmap_get(context->clients, &id);
199 if (hashmap_size(context->clients) >= CLIENTS_MAX)
202 r = hashmap_ensure_allocated(&context->clients, uint64_hash_func, uint64_compare_func);
212 r = hashmap_put(context->clients, &c->id, c);
216 c->context = context;
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) {
229 r = sd_bus_add_match(context->bus, watch, on_name_owner_changed, c);
245 static int driver_add_match(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
247 struct bus_match_component *components = NULL;
248 Context *context = userdata;
249 unsigned n_components = 0;
260 r = sd_bus_message_read(message, "s", &arg0);
264 r = bus_kernel_parse_unique_name(message->sender, &id);
268 r = client_acquire(context, id, &c);
270 return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Reached limit of %u clients", CLIENTS_MAX);
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);
279 r = bus_match_parse(arg0, &components, &n_components);
281 r = sd_bus_error_setf(error, SD_BUS_ERROR_MATCH_RULE_INVALID, "Match rule \"%s\" is not valid", arg0);
285 r = match_new(c, components, n_components, &m);
289 r = bus_add_match_internal_kernel(bus, id, components, n_components, m->cookie);
293 bus_match_parse_free(components, n_components);
295 return sd_bus_reply_method_return(message, NULL);
298 bus_match_parse_free(components, n_components);
302 if (c->n_matches <= 0)
308 static int driver_remove_match(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
310 struct bus_match_component *components = NULL;
311 _cleanup_free_ char *normalized = NULL;
312 Context *context = userdata;
313 unsigned n_components = 0;
324 r = sd_bus_message_read(message, "s", &arg0);
328 r = bus_kernel_parse_unique_name(message->sender, &id);
332 c = hashmap_get(context->clients, &id);
334 return sd_bus_error_setf(error, SD_BUS_ERROR_MATCH_RULE_NOT_FOUND, "You have not registered any matches.");
336 r = bus_match_parse(arg0, &components, &n_components);
338 r = sd_bus_error_setf(error, SD_BUS_ERROR_MATCH_RULE_INVALID, "Match rule \"%s\" is not valid", arg0);
342 normalized = bus_match_to_string(components, n_components);
348 m = hashmap_get(c->matches, normalized);
350 r = sd_bus_error_setf(error, SD_BUS_ERROR_MATCH_RULE_NOT_FOUND, "Match rule \"%s\" not found.", normalized);
354 bus_remove_match_internal_kernel(bus, id, m->cookie);
357 r = sd_bus_reply_method_return(message, NULL);
360 bus_match_parse_free(components, n_components);
362 if (c->n_matches <= 0)
368 static int get_creds(sd_bus *bus, sd_bus_message *m, uint64_t mask, sd_bus_creds **_creds, sd_bus_error *error) {
369 _cleanup_bus_creds_unref_ sd_bus_creds *c = NULL;
377 r = sd_bus_message_read(m, "s", &name);
381 assert_return(service_name_is_valid(name), -EINVAL);
383 r = sd_bus_get_owner(bus, name, mask, &c);
384 if (r == -ENOENT || r == -ENXIO)
385 return sd_bus_error_setf(error, SD_BUS_ERROR_NAME_HAS_NO_OWNER, "Name %s is currently not owned by anyone.", name);
389 if ((c->mask & mask) != mask)
398 static int driver_get_security_context(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
399 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
400 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
403 r = get_creds(bus, m, SD_BUS_CREDS_SELINUX_CONTEXT, &creds, error);
407 r = sd_bus_message_new_method_return(m, &reply);
411 r = sd_bus_message_append_array(reply, 'y', creds->label, strlen(creds->label));
415 return sd_bus_send(bus, reply, NULL);
418 static int driver_get_pid(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
419 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
422 r = get_creds(bus, m, SD_BUS_CREDS_PID, &creds, error);
426 return sd_bus_reply_method_return(m, "u", (uint32_t) creds->pid);
429 static int driver_get_user(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
430 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
433 r = get_creds(bus, m, SD_BUS_CREDS_UID, &creds, error);
437 return sd_bus_reply_method_return(m, "u", (uint32_t) creds->uid);
440 static int driver_get_name_owner(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
441 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
444 r = get_creds(bus, m, SD_BUS_CREDS_UNIQUE_NAME, &creds, error);
448 return sd_bus_reply_method_return(m, "s", creds->unique_name);
451 static int driver_get_id(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
452 sd_id128_t server_id;
453 char buf[SD_ID128_STRING_MAX];
456 r = sd_bus_get_server_id(bus, &server_id);
460 return sd_bus_reply_method_return(m, "s", sd_id128_to_string(server_id, buf));
463 static int driver_hello(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
464 return sd_bus_reply_method_return(m, "s", m->sender);
467 static int return_strv(sd_bus *bus, sd_bus_message *m, char **l) {
468 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
471 r = sd_bus_message_new_method_return(m, &reply);
475 r = sd_bus_message_append_strv(reply, l);
479 return sd_bus_send(bus, reply, NULL);
482 static int driver_list_names(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
483 _cleanup_strv_free_ char **names = NULL;
486 r = sd_bus_list_names(bus, &names, NULL);
490 /* Let's sort the names list to make it stable */
493 return return_strv(bus, m, names);
496 static int driver_list_activatable_names(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
497 _cleanup_strv_free_ char **names = NULL;
500 r = sd_bus_list_names(bus, NULL, &names);
504 /* Let's sort the names list to make it stable */
507 return return_strv(bus, m, names);
510 static int driver_list_queued_owners(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
511 struct kdbus_cmd_name_list cmd = {};
512 struct kdbus_name_list *name_list;
513 struct kdbus_cmd_name *name;
514 _cleanup_strv_free_ char **owners = NULL;
518 r = sd_bus_message_read(m, "s", &arg0);
522 assert_return(service_name_is_valid(arg0), -EINVAL);
524 cmd.flags = KDBUS_NAME_LIST_QUEUED;
526 r = ioctl(bus->input_fd, KDBUS_CMD_NAME_LIST, &cmd);
530 name_list = (struct kdbus_name_list *) ((uint8_t *) bus->kdbus_buffer + cmd.offset);
532 KDBUS_ITEM_FOREACH(name, name_list, names) {
535 if (name->size <= sizeof(*name))
538 if (!streq(name->name, arg0))
541 if (asprintf(&n, ":1.%llu", (unsigned long long) name->owner_id) < 0)
544 r = strv_push(&owners, n);
551 r = ioctl(bus->input_fd, KDBUS_CMD_FREE, &cmd.offset);
555 return return_strv(bus, m, owners);
558 static int driver_name_has_owner(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
562 r = sd_bus_message_read(m, "s", &name);
566 assert_return(service_name_is_valid(name), -EINVAL);
568 r = sd_bus_get_owner(bus, name, 0, NULL);
569 if (r < 0 && r != -ENOENT && r != -ENXIO)
572 return sd_bus_reply_method_return(m, "b", r >= 0);
575 static int driver_request_name(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
576 struct kdbus_cmd_name *n;
583 r = sd_bus_message_read(m, "su", &name, &flags);
587 assert_return(service_name_is_valid(name), -EINVAL);
588 assert_return((flags & ~(BUS_NAME_ALLOW_REPLACEMENT|BUS_NAME_REPLACE_EXISTING|BUS_NAME_DO_NOT_QUEUE)) == 0, -EINVAL);
591 size = offsetof(struct kdbus_cmd_name, name) + l + 1;
594 memcpy(n->name, name, l+1);
595 kdbus_translate_request_name_flags(flags, (uint64_t *) &n->flags);
597 /* This function is open-coded because we request the name 'on behalf'
598 * of the requesting connection */
599 r = bus_kernel_parse_unique_name(m->sender, &id);
605 r = ioctl(bus->input_fd, KDBUS_CMD_NAME_ACQUIRE, n);
608 return sd_bus_reply_method_return(m, "u", BUS_NAME_EXISTS);
609 if (errno == EALREADY)
610 return sd_bus_reply_method_return(m, "u", BUS_NAME_ALREADY_OWNER);
615 if (n->flags & KDBUS_NAME_IN_QUEUE)
616 return sd_bus_reply_method_return(m, "u", BUS_NAME_IN_QUEUE);
618 return sd_bus_reply_method_return(m, "u", BUS_NAME_PRIMARY_OWNER);
621 static int driver_release_name(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
622 struct kdbus_cmd_name *n;
628 r = sd_bus_message_read(m, "s", &name);
632 assert_return(service_name_is_valid(name), -EINVAL);
635 size = offsetof(struct kdbus_cmd_name, name) + l + 1;
638 memcpy(n->name, name, l+1);
640 /* This function is open-coded because we request the name 'on behalf'
641 * of the requesting connection */
642 r = bus_kernel_parse_unique_name(m->sender, &id);
648 r = ioctl(bus->input_fd, KDBUS_CMD_NAME_RELEASE, n);
651 return sd_bus_reply_method_return(m, "u", BUS_NAME_NON_EXISTENT);
652 if (errno == EADDRINUSE)
653 return sd_bus_reply_method_return(m, "u", BUS_NAME_NOT_OWNER);
657 return sd_bus_reply_method_return(m, "u", BUS_NAME_RELEASED);
660 static int driver_start_service_by_name(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
661 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
662 _cleanup_strv_free_ char **t = NULL;
663 _cleanup_free_ char *path = NULL;
668 r = sd_bus_message_read(m, "su", &name, &flags);
672 assert_return(service_name_is_valid(name), -EINVAL);
673 assert_return(flags == 0, -ENOTSUP);
675 r = sd_bus_get_owner(bus, name, 0, NULL);
677 return sd_bus_reply_method_return(m, "u", BUS_START_REPLY_ALREADY_RUNNING);
681 u = strappenda(name, ".busname");
683 path = unit_dbus_path_from_name(u);
687 r = sd_bus_get_property_strv(
689 "org.freedesktop.systemd1",
691 "org.freedesktop.systemd1.Unit",
698 if (!t || !t[0] || t[1])
699 return sd_bus_error_setf(error, SD_BUS_ERROR_SERVICE_UNKNOWN, "Bus name %s not found.", name);
701 r = sd_bus_call_method(
703 "org.freedesktop.systemd1",
704 "/org/freedesktop/systemd1",
705 "org.freedesktop.systemd1.Manager",
715 return sd_bus_reply_method_return(m, "u", BUS_START_REPLY_SUCCESS);
718 static int driver_update_environment(sd_bus*bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
719 _cleanup_bus_message_unref_ sd_bus_message *msg = NULL;
720 _cleanup_strv_free_ char **args = NULL;
723 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{ss}");
727 while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "ss")) > 0) {
728 _cleanup_free_ char *s = NULL;
732 r = sd_bus_message_read(m, "ss", &key, &value);
736 s = strjoin(key, "=", value, NULL);
740 r = strv_extend(&args, s);
744 r = sd_bus_message_exit_container(m);
749 r = sd_bus_message_exit_container(m);
756 r = sd_bus_message_new_method_call(
758 "org.freedesktop.systemd1",
759 "/org/freedesktop/systemd1",
760 "org.freedesktop.systemd1.Manager",
766 r = sd_bus_message_append_strv(msg, args);
770 r = sd_bus_call(bus, msg, 0, NULL, NULL);
774 return sd_bus_reply_method_return(m, NULL);
777 static int driver_unsupported(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
778 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "%s() is not supported", sd_bus_message_get_member(m));
781 static const sd_bus_vtable driver_vtable[] = {
782 SD_BUS_VTABLE_START(0),
783 SD_BUS_METHOD("AddMatch", "s", NULL, driver_add_match, SD_BUS_VTABLE_UNPRIVILEGED),
784 SD_BUS_METHOD("GetConnectionSELinuxSecurityContext", "s", "ay", driver_get_security_context, SD_BUS_VTABLE_UNPRIVILEGED),
785 SD_BUS_METHOD("GetConnectionUnixProcessID", "s", "u", driver_get_pid, SD_BUS_VTABLE_UNPRIVILEGED),
786 SD_BUS_METHOD("GetConnectionUnixUser", "s", "u", driver_get_user, SD_BUS_VTABLE_UNPRIVILEGED),
787 SD_BUS_METHOD("GetId", NULL, "s", driver_get_id, SD_BUS_VTABLE_UNPRIVILEGED),
788 SD_BUS_METHOD("GetNameOwner", "s", "s", driver_get_name_owner, SD_BUS_VTABLE_UNPRIVILEGED),
789 SD_BUS_METHOD("Hello", NULL, "s", driver_hello, SD_BUS_VTABLE_UNPRIVILEGED),
790 SD_BUS_METHOD("ListActivatableNames", NULL, "as", driver_list_activatable_names, SD_BUS_VTABLE_UNPRIVILEGED),
791 SD_BUS_METHOD("ListNames", NULL, "as", driver_list_names, SD_BUS_VTABLE_UNPRIVILEGED),
792 SD_BUS_METHOD("ListQueuedOwners", "s", "as", driver_list_queued_owners, SD_BUS_VTABLE_UNPRIVILEGED),
793 SD_BUS_METHOD("NameHasOwner", "s", "b", driver_name_has_owner, SD_BUS_VTABLE_UNPRIVILEGED),
794 SD_BUS_METHOD("ReleaseName", "s", "u", driver_release_name, SD_BUS_VTABLE_UNPRIVILEGED),
795 SD_BUS_METHOD("ReloadConfig", NULL, NULL, driver_unsupported, SD_BUS_VTABLE_DEPRECATED),
796 SD_BUS_METHOD("RemoveMatch", "s", NULL, driver_remove_match, SD_BUS_VTABLE_UNPRIVILEGED),
797 SD_BUS_METHOD("RequestName", "su", "u", driver_request_name, SD_BUS_VTABLE_UNPRIVILEGED),
798 SD_BUS_METHOD("StartServiceByName", "su", "u", driver_start_service_by_name, SD_BUS_VTABLE_UNPRIVILEGED),
799 SD_BUS_METHOD("UpdateActivationEnvironment", "a{ss}", NULL, driver_update_environment, 0),
800 SD_BUS_SIGNAL("NameAcquired", "s", SD_BUS_VTABLE_DEPRECATED),
801 SD_BUS_SIGNAL("NameLost", "s", SD_BUS_VTABLE_DEPRECATED),
802 SD_BUS_SIGNAL("NameOwnerChanged", "sss", 0),
806 static int find_object(
809 const char *interface,
812 sd_bus_error *ret_error) {
814 /* We support the driver interface on exactly two different
815 * paths: the root and the entry point object. This is a bit
816 * different from the original dbus-daemon which supported it
819 if (streq_ptr(path, "/"))
822 if (streq_ptr(path, "/org/freedesktop/DBus"))
828 static int node_enumerator(
833 sd_bus_error *ret_error) {
837 l = strv_new("/", "/org/freedesktop/DBus", NULL);
845 static int connect_bus(Context *c) {
850 r = sd_bus_default(&c->bus);
852 log_error("Failed to create bus: %s", strerror(-r));
856 if (!c->bus->is_kernel) {
857 log_error("Not running on kdbus");
861 r = sd_bus_add_fallback_vtable(c->bus, "/", "org.freedesktop.DBus", driver_vtable, find_object, c);
863 log_error("Failed to add manager object vtable: %s", strerror(-r));
867 r = sd_bus_add_node_enumerator(c->bus, "/", node_enumerator, c);
869 log_error("Failed to add node enumerator: %s", strerror(-r));
873 r = sd_bus_request_name(c->bus, "org.freedesktop.DBus", 0);
875 log_error("Unable to request name: %s", strerror(-r));
879 r = sd_bus_attach_event(c->bus, c->event, 0);
881 log_error("Error while adding bus to event loop: %s", strerror(-r));
888 static bool check_idle(void *userdata) {
889 Context *c = userdata;
892 return hashmap_isempty(c->clients);
895 int main(int argc, char *argv[]) {
896 Context context = {};
900 log_set_target(LOG_TARGET_AUTO);
901 log_parse_environment();
905 log_error("This program takes no arguments.");
910 r = sd_event_default(&context.event);
912 log_error("Failed to allocate event loop: %s", strerror(-r));
916 sd_event_set_watchdog(context.event, true);
918 r = connect_bus(&context);
922 r = bus_event_loop_with_idle(context.event, context.bus, "org.freedesktop.DBus", DEFAULT_EXIT_USEC, check_idle, &context);
924 log_error("Failed to run event loop: %s", strerror(-r));
929 while ((c = hashmap_first(context.clients)))
932 sd_bus_unref(context.bus);
933 sd_event_unref(context.event);
935 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;