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"
55 #define CLIENTS_MAX 1024
56 #define MATCHES_MAX 1024
58 typedef struct Match Match;
59 typedef struct Client Client;
60 typedef struct Context Context;
66 LIST_FIELDS(Match, matches);
84 static void match_free(Match *m) {
92 first = hashmap_get(m->client->matches, m->match);
93 LIST_REMOVE(matches, first, m);
95 assert_se(hashmap_replace(m->client->matches, m->match, first) >= 0);
97 hashmap_remove(m->client->matches, m->match);
99 m->client->n_matches--;
106 static int match_new(Client *c, struct bus_match_component *components, unsigned n_components, Match **_m) {
113 r = hashmap_ensure_allocated(&c->matches, string_hash_func, string_compare_func);
121 m->match = bus_match_to_string(components, n_components);
127 m->cookie = ++c->next_cookie;
129 first = hashmap_get(c->matches, m->match);
130 LIST_PREPEND(matches, first, m);
131 r = hashmap_replace(c->matches, m->match, first);
133 LIST_REMOVE(matches, first, m);
150 static int on_name_owner_changed(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error);
152 static void client_free(Client *c) {
160 sd_bus_remove_match(c->context->bus, c->watch, on_name_owner_changed, c);
162 assert_se(hashmap_remove(c->context->clients, &c->id) == c);
165 while ((m = hashmap_first(c->matches)))
168 hashmap_free(c->matches);
174 static int on_name_owner_changed(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
175 Client *c = userdata;
184 static int client_acquire(Context *context, uint64_t id, Client **_c) {
192 c = hashmap_get(context->clients, &id);
198 if (hashmap_size(context->clients) >= CLIENTS_MAX)
201 r = hashmap_ensure_allocated(&context->clients, uint64_hash_func, uint64_compare_func);
211 r = hashmap_put(context->clients, &c->id, c);
215 c->context = context;
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) {
228 r = sd_bus_add_match(context->bus, watch, on_name_owner_changed, c);
244 static int driver_add_match(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
246 struct bus_match_component *components = NULL;
247 Context *context = userdata;
248 unsigned n_components = 0;
259 r = sd_bus_message_read(message, "s", &arg0);
263 r = bus_kernel_parse_unique_name(message->sender, &id);
267 r = client_acquire(context, id, &c);
269 return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Reached limit of %u clients", CLIENTS_MAX);
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);
278 r = bus_match_parse(arg0, &components, &n_components);
280 r = sd_bus_error_setf(error, SD_BUS_ERROR_MATCH_RULE_INVALID, "Match rule \"%s\" is not valid", arg0);
284 r = match_new(c, components, n_components, &m);
288 r = bus_add_match_internal_kernel(bus, id, components, n_components, m->cookie);
292 bus_match_parse_free(components, n_components);
294 return sd_bus_reply_method_return(message, NULL);
297 bus_match_parse_free(components, n_components);
301 if (c->n_matches <= 0)
307 static int driver_remove_match(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
309 struct bus_match_component *components = NULL;
310 _cleanup_free_ char *normalized = NULL;
311 Context *context = userdata;
312 unsigned n_components = 0;
323 r = sd_bus_message_read(message, "s", &arg0);
327 r = bus_kernel_parse_unique_name(message->sender, &id);
331 c = hashmap_get(context->clients, &id);
333 return sd_bus_error_setf(error, SD_BUS_ERROR_MATCH_RULE_NOT_FOUND, "You have not registered any matches.");
335 r = bus_match_parse(arg0, &components, &n_components);
337 r = sd_bus_error_setf(error, SD_BUS_ERROR_MATCH_RULE_INVALID, "Match rule \"%s\" is not valid", arg0);
341 normalized = bus_match_to_string(components, n_components);
347 m = hashmap_get(c->matches, normalized);
349 r = sd_bus_error_setf(error, SD_BUS_ERROR_MATCH_RULE_NOT_FOUND, "Match rule \"%s\" not found.");
353 bus_remove_match_internal_kernel(bus, id, m->cookie);
356 r = sd_bus_reply_method_return(message, NULL);
359 bus_match_parse_free(components, n_components);
361 if (c->n_matches <= 0)
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;
373 r = sd_bus_message_read(m, "s", &arg0);
377 assert_return(service_name_is_valid(arg0), -EINVAL);
379 r = sd_bus_get_owner(bus, arg0, SD_BUS_CREDS_SELINUX_CONTEXT, &creds);
381 return sd_bus_error_setf(error, SD_BUS_ERROR_NAME_HAS_NO_OWNER, "Name %s is currently not owned by anyone.", arg0);
385 r = sd_bus_message_new_method_return(m, &reply);
389 r = sd_bus_message_append_array(reply, 'y', creds->label, strlen(creds->label));
393 return sd_bus_send(bus, reply, NULL);
396 static int driver_get_pid(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
397 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
401 r = sd_bus_message_read(m, "s", &arg0);
405 assert_return(service_name_is_valid(arg0), -EINVAL);
407 r = sd_bus_get_owner(bus, arg0, SD_BUS_CREDS_PID, &creds);
409 return sd_bus_error_setf(error, SD_BUS_ERROR_NAME_HAS_NO_OWNER, "Name %s is currently not owned by anyone.", arg0);
413 return sd_bus_reply_method_return(m, "u", creds->pid);
416 static int driver_get_user(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
417 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
421 r = sd_bus_message_read(m, "s", &arg0);
425 assert_return(service_name_is_valid(arg0), -EINVAL);
427 r = sd_bus_get_owner(bus, arg0, SD_BUS_CREDS_UID, &creds);
429 return sd_bus_error_setf(error, SD_BUS_ERROR_NAME_HAS_NO_OWNER, "Name %s is currently not owned by anyone.", arg0);
433 return sd_bus_reply_method_return(m, "u", creds->uid);
436 static int driver_get_id(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
437 sd_id128_t server_id;
438 char buf[SD_ID128_STRING_MAX];
441 r = sd_bus_get_server_id(bus, &server_id);
445 return sd_bus_reply_method_return(m, "s", sd_id128_to_string(server_id, buf));
448 static int driver_get_name_owner(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
449 _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
453 r = sd_bus_message_read(m, "s", &arg0);
457 assert_return(service_name_is_valid(arg0), -EINVAL);
459 r = sd_bus_get_owner(bus, arg0, SD_BUS_CREDS_UNIQUE_NAME, &creds);
461 return sd_bus_error_setf(error, SD_BUS_ERROR_NAME_HAS_NO_OWNER, "Name %s is currently not owned by anyone.", arg0);
465 return sd_bus_reply_method_return(m, "s", creds->unique_name);
468 static int driver_hello(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
469 return sd_bus_reply_method_return(m, "s", m->sender);
472 static int return_strv(sd_bus *bus, sd_bus_message *m, char **l) {
473 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
476 r = sd_bus_message_new_method_return(m, &reply);
480 r = sd_bus_message_append_strv(reply, l);
484 return sd_bus_send(bus, reply, NULL);
487 static int driver_list_names(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
488 _cleanup_strv_free_ char **names = NULL;
491 r = sd_bus_list_names(bus, &names, NULL);
495 /* Let's sort the names list to make it stable */
498 return return_strv(bus, m, names);
501 static int driver_list_activatable_names(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
502 _cleanup_strv_free_ char **names = NULL;
505 r = sd_bus_list_names(bus, NULL, &names);
509 /* Let's sort the names list to make it stable */
512 return return_strv(bus, m, names);
515 static int driver_list_queued_owners(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
516 struct kdbus_cmd_name_list cmd = {};
517 struct kdbus_name_list *name_list;
518 struct kdbus_cmd_name *name;
519 _cleanup_strv_free_ char **owners = NULL;
523 r = sd_bus_message_read(m, "s", &arg0);
527 assert_return(service_name_is_valid(arg0), -EINVAL);
529 cmd.flags = KDBUS_NAME_LIST_QUEUED;
531 r = ioctl(bus->input_fd, KDBUS_CMD_NAME_LIST, &cmd);
535 name_list = (struct kdbus_name_list *) ((uint8_t *) bus->kdbus_buffer + cmd.offset);
537 KDBUS_ITEM_FOREACH(name, name_list, names) {
540 if (name->size <= sizeof(*name))
543 if (!streq(name->name, arg0))
546 if (asprintf(&n, ":1.%llu", (unsigned long long) name->id) < 0)
549 r = strv_push(&owners, n);
556 r = ioctl(bus->input_fd, KDBUS_CMD_FREE, &cmd.offset);
560 return return_strv(bus, m, owners);
563 static int driver_name_has_owner(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
567 r = sd_bus_message_read(m, "s", &arg0);
571 assert_return(service_name_is_valid(arg0), -EINVAL);
573 r = sd_bus_get_owner(bus, arg0, 0, NULL);
574 if (r < 0 && r != -ENOENT)
577 return sd_bus_reply_method_return(m, "b", r >= 0);
580 static int driver_request_name(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
581 struct kdbus_cmd_name *n;
588 r = sd_bus_message_read(m, "su", &name, &flags);
592 assert_return(service_name_is_valid(name), -EINVAL);
593 assert_return((flags & ~(BUS_NAME_ALLOW_REPLACEMENT|BUS_NAME_REPLACE_EXISTING|BUS_NAME_DO_NOT_QUEUE)) == 0, -EINVAL);
596 size = offsetof(struct kdbus_cmd_name, name) + l + 1;
599 memcpy(n->name, name, l+1);
600 kdbus_translate_request_name_flags(flags, (uint64_t *) &n->flags);
602 /* This function is open-coded because we request the name 'on behalf'
603 * of the requesting connection */
604 r = bus_kernel_parse_unique_name(m->sender, &id);
610 r = ioctl(bus->input_fd, KDBUS_CMD_NAME_ACQUIRE, n);
613 return sd_bus_reply_method_return(m, "u", BUS_NAME_EXISTS);
614 if (errno == EALREADY)
615 return sd_bus_reply_method_return(m, "u", BUS_NAME_ALREADY_OWNER);
620 if (n->flags & KDBUS_NAME_IN_QUEUE)
621 return sd_bus_reply_method_return(m, "u", BUS_NAME_IN_QUEUE);
623 return sd_bus_reply_method_return(m, "u", BUS_NAME_PRIMARY_OWNER);
626 static int driver_release_name(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
627 struct kdbus_cmd_name *n;
633 r = sd_bus_message_read(m, "s", &name);
637 assert_return(service_name_is_valid(name), -EINVAL);
640 size = offsetof(struct kdbus_cmd_name, name) + l + 1;
643 memcpy(n->name, name, l+1);
645 /* This function is open-coded because we request the name 'on behalf'
646 * of the requesting connection */
647 r = bus_kernel_parse_unique_name(m->sender, &id);
653 r = ioctl(bus->input_fd, KDBUS_CMD_NAME_RELEASE, n);
656 return sd_bus_reply_method_return(m, "u", BUS_NAME_NON_EXISTENT);
657 if (errno == EADDRINUSE)
658 return sd_bus_reply_method_return(m, "u", BUS_NAME_NOT_OWNER);
662 return sd_bus_reply_method_return(m, "u", BUS_NAME_RELEASED);
665 static int driver_start_service_by_name(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
666 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
667 _cleanup_strv_free_ char **t = NULL;
668 _cleanup_free_ char *path = NULL;
673 r = sd_bus_message_read(m, "su", &name, &flags);
677 assert_return(service_name_is_valid(name), -EINVAL);
678 assert_return(flags == 0, -ENOTSUP);
680 r = sd_bus_get_owner(bus, name, 0, NULL);
682 return sd_bus_reply_method_return(m, "u", BUS_START_REPLY_ALREADY_RUNNING);
686 u = strappenda(name, ".busname");
688 path = unit_dbus_path_from_name(u);
692 r = sd_bus_get_property_strv(
694 "org.freedesktop.systemd1",
696 "org.freedesktop.systemd1.Unit",
706 r = sd_bus_call_method(
708 "org.freedesktop.systemd1",
709 "/org/freedesktop/systemd1",
710 "org.freedesktop.systemd1.Manager",
720 return sd_bus_reply_method_return(m, "u", BUS_START_REPLY_SUCCESS);
723 static int driver_unsupported(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bus_error *error) {
724 return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "%s() is not supported", sd_bus_message_get_member(m));
727 static const sd_bus_vtable driver_vtable[] = {
728 SD_BUS_VTABLE_START(0),
729 SD_BUS_METHOD("AddMatch", "s", NULL, driver_add_match, SD_BUS_VTABLE_UNPRIVILEGED),
730 SD_BUS_METHOD("GetConnectionSELinuxSecurityContext", "s", "ay", driver_get_security_ctx, SD_BUS_VTABLE_UNPRIVILEGED),
731 SD_BUS_METHOD("GetConnectionUnixProcessID", "s", "u", driver_get_pid, SD_BUS_VTABLE_UNPRIVILEGED),
732 SD_BUS_METHOD("GetConnectionUnixUser", "s", "u", driver_get_user, SD_BUS_VTABLE_UNPRIVILEGED),
733 SD_BUS_METHOD("GetId", NULL, "s", driver_get_id, SD_BUS_VTABLE_UNPRIVILEGED),
734 SD_BUS_METHOD("GetNameOwner", "s", "s", driver_get_name_owner, SD_BUS_VTABLE_UNPRIVILEGED),
735 SD_BUS_METHOD("Hello", NULL, "s", driver_hello, SD_BUS_VTABLE_UNPRIVILEGED),
736 SD_BUS_METHOD("ListActivatableNames", NULL, "as", driver_list_activatable_names, SD_BUS_VTABLE_UNPRIVILEGED),
737 SD_BUS_METHOD("ListNames", NULL, "as", driver_list_names, SD_BUS_VTABLE_UNPRIVILEGED),
738 SD_BUS_METHOD("ListQueuedOwners", "s", "as", driver_list_queued_owners, SD_BUS_VTABLE_UNPRIVILEGED),
739 SD_BUS_METHOD("NameHasOwner", "s", "b", driver_name_has_owner, SD_BUS_VTABLE_UNPRIVILEGED),
740 SD_BUS_METHOD("ReleaseName", "s", "u", driver_release_name, SD_BUS_VTABLE_UNPRIVILEGED),
741 SD_BUS_METHOD("ReloadConfig", NULL, NULL, driver_unsupported, SD_BUS_VTABLE_DEPRECATED),
742 SD_BUS_METHOD("RemoveMatch", "s", NULL, driver_remove_match, SD_BUS_VTABLE_UNPRIVILEGED),
743 SD_BUS_METHOD("RequestName", "su", "u", driver_request_name, SD_BUS_VTABLE_UNPRIVILEGED),
744 SD_BUS_METHOD("StartServiceByName", "su", "u", driver_start_service_by_name, SD_BUS_VTABLE_UNPRIVILEGED),
745 SD_BUS_METHOD("UpdateActivationEnvironment", "a{ss}", NULL, driver_unsupported, SD_BUS_VTABLE_DEPRECATED),
746 SD_BUS_SIGNAL("NameAcquired", "s", SD_BUS_VTABLE_DEPRECATED),
747 SD_BUS_SIGNAL("NameLost", "s", SD_BUS_VTABLE_DEPRECATED),
748 SD_BUS_SIGNAL("NameOwnerChanged", "sss", 0),
752 static int connect_bus(Context *c) {
757 r = sd_bus_default_system(&c->bus);
759 log_error("Failed to create bus: %s", strerror(-r));
763 if (!c->bus->is_kernel) {
764 log_error("Not running on kdbus");
768 r = sd_bus_add_object_vtable(c->bus, "/org/freedesktop/DBus", "org.freedesktop.DBus", driver_vtable, c);
770 log_error("Failed to add manager object vtable: %s", strerror(-r));
774 r = sd_bus_request_name(c->bus, "org.freedesktop.DBus", 0);
776 log_error("Unable to request name: %s\n", strerror(-r));
780 r = sd_bus_attach_event(c->bus, c->event, 0);
782 log_error("Error while adding bus to event loop: %s", strerror(-r));
789 static bool check_idle(void *userdata) {
790 Context *c = userdata;
793 return hashmap_isempty(c->clients);
796 int main(int argc, char *argv[]) {
797 Context context = {};
801 log_set_target(LOG_TARGET_AUTO);
802 log_parse_environment();
806 log_error("This program takes no arguments.");
811 r = sd_event_default(&context.event);
813 log_error("Failed to allocate event loop: %s", strerror(-r));
817 sd_event_set_watchdog(context.event, true);
819 r = connect_bus(&context);
823 r = bus_event_loop_with_idle(context.event, context.bus, "org.freedesktop.DBus", DEFAULT_EXIT_USEC, check_idle, &context);
825 log_error("Failed to run event loop: %s", strerror(-r));
830 while ((c = hashmap_first(context.clients)))
833 sd_bus_unref(context.bus);
834 sd_event_unref(context.event);
836 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;