HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/core/dbus.c

45,897 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: f1bd5a49dc45d336397a209179b00739e43fca2a

Lines 1001–1308 of 1,308

 
        if (m->private_listen_fd >= 0)
                return 0;
 
        if (MANAGER_IS_SYSTEM(m)) {
 
                /* We want the private bus only when running as init */
                if (getpid_cached() != 1)
                        return 0;
 
                r = sockaddr_un_set_path(&sa.un, "/run/systemd/private");
        } else {
                _cleanup_free_ char *p = NULL;
 
                p = path_join(m->prefix[EXEC_DIRECTORY_RUNTIME], "systemd/private");
                if (!p)
                        return log_oom();
 
                r = sockaddr_un_set_path(&sa.un, p);
        }
        if (r < 0)
                return log_error_errno(r, "Failed to set socket path for private bus: %m");
        sa_len = r;
 
        (void) sockaddr_un_unlink(&sa.un);
 
        fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
        if (fd < 0)
                return log_error_errno(errno, "Failed to allocate private socket: %m");
 
        WITH_UMASK(0077)
                r = bind(fd, &sa.sa, sa_len);
        if (r < 0)
                return log_error_errno(errno, "Failed to bind private socket: %m");
 
        r = listen(fd, SOMAXCONN_DELUXE);
        if (r < 0)
                return log_error_errno(errno, "Failed to make private socket listening: %m");
 
        /* Generate an inotify event in case somebody waits for this socket to appear using inotify() */
        (void) touch(sa.un.sun_path);
 
        r = sd_event_add_io(m->event, &s, fd, EPOLLIN, bus_on_connection, m);
        if (r < 0)
                return log_error_errno(r, "Failed to allocate event source: %m");
 
        (void) sd_event_source_set_description(s, "bus-connection");
 
        m->private_listen_fd = TAKE_FD(fd);
        m->private_listen_event_source = s;
 
        log_debug("Successfully created private D-Bus server.");
 
        return 0;
}
 
static void destroy_bus(Manager *m, sd_bus **bus) {
        Unit *u;
        Job *j;
 
        assert(m);
        assert(bus);
 
        if (!*bus)
                return;
 
        /* Make sure all bus slots watching names are released. */
        HASHMAP_FOREACH(u, m->watch_bus) {
                if (u->match_bus_slot && sd_bus_slot_get_bus(u->match_bus_slot) == *bus)
                        u->match_bus_slot = sd_bus_slot_unref(u->match_bus_slot);
                if (u->get_name_owner_slot && sd_bus_slot_get_bus(u->get_name_owner_slot) == *bus)
                        u->get_name_owner_slot = sd_bus_slot_unref(u->get_name_owner_slot);
        }
 
        /* Get rid of tracked clients on this bus */
        if (m->subscribed && sd_bus_track_get_bus(m->subscribed) == *bus) {
                _cleanup_strv_free_ char **subscribed = NULL;
                int r;
 
                r = bus_track_to_strv(m->subscribed, &subscribed);
                if (r < 0)
                        log_warning_errno(r, "Failed to serialize api subscribers, ignoring: %m");
                strv_free_and_replace(m->subscribed_as_strv, subscribed);
 
                m->deserialized_bus_id = m->bus_id;
                m->bus_id = SD_ID128_NULL;
 
                m->subscribed = sd_bus_track_unref(m->subscribed);
        }
 
        HASHMAP_FOREACH(j, m->jobs)
                if (j->bus_track && sd_bus_track_get_bus(j->bus_track) == *bus)
                        j->bus_track = sd_bus_track_unref(j->bus_track);
 
        HASHMAP_FOREACH(u, m->units) {
                if (u->bus_track && sd_bus_track_get_bus(u->bus_track) == *bus)
                        u->bus_track = sd_bus_track_unref(u->bus_track);
 
                /* Get rid of pending freezer messages on this bus */
                if (u->pending_freezer_invocation && sd_bus_message_get_bus(u->pending_freezer_invocation) == *bus)
                        u->pending_freezer_invocation = sd_bus_message_unref(u->pending_freezer_invocation);
        }
 
        /* Get rid of queued message on this bus */
        if (m->pending_reload_message_dbus && sd_bus_message_get_bus(m->pending_reload_message_dbus) == *bus)
                m->pending_reload_message_dbus = sd_bus_message_unref(m->pending_reload_message_dbus);
 
        /* Possibly flush unwritten data, but only if we are unprivileged, since we don't want to sync
         * here, and only if the connection is currently RUNNING: sd_bus_flush() first drives the
         * connection to completion via bus_ensure_running(), which blocks us synchronously for up to
         * BUS_AUTH_TIMEOUT if the peer accepted the connection but never answers authentication (e.g. a
         * socket-activated D-Bus service that is hung or already gone during session teardown).
         * sd_bus_flush() only touches the write queue once that step succeeds, so anything queued on a
         * connection that does not reach RUNNING is discarded either way - not worth blocking for. */
        if (!MANAGER_IS_SYSTEM(m) && sd_bus_is_ready(*bus) > 0)
                sd_bus_flush(*bus);
 
        /* And destroy the object */
        *bus = sd_bus_close_unref(*bus);
}
 
void bus_done_api(Manager *m) {
        destroy_bus(m, &m->api_bus);
 
        m->api_bus_ready = false;
}
 
void bus_done_system(Manager *m) {
        destroy_bus(m, &m->system_bus);
}
 
void bus_done_private(Manager *m) {
        sd_bus *b;
 
        assert(m);
 
        while ((b = set_steal_first(m->private_buses)))
                destroy_bus(m, &b);
 
        m->private_buses = set_free(m->private_buses);
 
        m->private_listen_event_source = sd_event_source_disable_unref(m->private_listen_event_source);
        m->private_listen_fd = safe_close(m->private_listen_fd);
}
 
void bus_done(Manager *m) {
        assert(m);
 
        bus_done_api(m);
        bus_done_system(m);
        bus_done_private(m);
 
        assert(!m->subscribed);
 
        m->polkit_registry = hashmap_free(m->polkit_registry);
}
 
int bus_fdset_add_all(Manager *m, FDSet *fds) {
        sd_bus *b;
        int fd;
 
        assert(m);
        assert(fds);
 
        /* When we are about to reexecute we add all D-Bus fds to the
         * set to pass over to the newly executed systemd. They won't
         * be used there however, except that they are closed at the
         * very end of deserialization, those making it possible for
         * clients to synchronously wait for systemd to reexec by
         * simply waiting for disconnection */
 
        if (m->api_bus) {
                fd = sd_bus_get_fd(m->api_bus);
                if (fd >= 0) {
                        fd = fdset_put_dup(fds, fd);
                        if (fd < 0)
                                return fd;
                }
        }
 
        SET_FOREACH(b, m->private_buses) {
                fd = sd_bus_get_fd(b);
                if (fd >= 0) {
                        fd = fdset_put_dup(fds, fd);
                        if (fd < 0)
                                return fd;
                }
        }
 
        /* We don't offer any APIs on the system bus (well, unless it
         * is the same as the API bus) hence we don't bother with it
         * here */
 
        return 0;
}
 
int bus_foreach_bus(
                Manager *m,
                sd_bus_track *subscribed2,
                int (*send_message)(sd_bus *bus, void *userdata),
                void *userdata) {
 
        int r = 0;
 
        assert(m);
        assert(send_message);
 
        /* Send to all direct buses, unconditionally */
        sd_bus *b;
        SET_FOREACH(b, m->private_buses) {
 
                /* Don't bother with enqueuing these messages to clients that haven't started yet */
                if (sd_bus_is_ready(b) <= 0)
                        continue;
 
                RET_GATHER(r, send_message(b, userdata));
        }
 
        /* Send to API bus, but only if somebody is subscribed */
        if (m->api_bus &&
            (sd_bus_track_count(m->subscribed) > 0 ||
             sd_bus_track_count(subscribed2) > 0))
                RET_GATHER(r, send_message(m->api_bus, userdata));
 
        return r;
}
 
void bus_track_serialize(sd_bus_track *t, FILE *f, const char *prefix) {
        const char *n;
 
        assert(f);
        assert(prefix);
 
        for (n = sd_bus_track_first(t); n; n = sd_bus_track_next(t)) {
                int c, j;
 
                c = sd_bus_track_count_name(t, n);
                for (j = 0; j < c; j++)
                        (void) serialize_item(f, prefix, n);
        }
}
 
uint64_t manager_bus_n_queued_write(Manager *m) {
        uint64_t c = 0;
        sd_bus *b;
        int r;
 
        /* Returns the total number of messages queued for writing on all our direct and API buses. */
 
        SET_FOREACH(b, m->private_buses) {
                uint64_t k;
 
                r = sd_bus_get_n_queued_write(b, &k);
                if (r < 0)
                        log_debug_errno(r, "Failed to query queued messages for private bus: %m");
                else
                        c += k;
        }
 
        if (m->api_bus) {
                uint64_t k;
 
                r = sd_bus_get_n_queued_write(m->api_bus, &k);
                if (r < 0)
                        log_debug_errno(r, "Failed to query queued messages for API bus: %m");
                else
                        c += k;
        }
 
        return c;
}
 
static void vtable_dump_bus_properties(FILE *f, const sd_bus_vtable *table) {
        const sd_bus_vtable *i;
 
        for (i = table; i->type != _SD_BUS_VTABLE_END; i++) {
                if (!IN_SET(i->type, _SD_BUS_VTABLE_PROPERTY, _SD_BUS_VTABLE_WRITABLE_PROPERTY) ||
                    (i->flags & (SD_BUS_VTABLE_DEPRECATED | SD_BUS_VTABLE_HIDDEN)) != 0)
                        continue;
 
                fprintf(f, "%s\n", i->x.property.member);
        }
}
 
void dump_bus_properties(FILE *f) {
        assert(f);
 
        vtable_dump_bus_properties(f, bus_automount_vtable);
        vtable_dump_bus_properties(f, bus_cgroup_vtable);
        vtable_dump_bus_properties(f, bus_device_vtable);
        vtable_dump_bus_properties(f, bus_exec_vtable);
        vtable_dump_bus_properties(f, bus_unit_exec_vtable);
        vtable_dump_bus_properties(f, bus_job_vtable);
        vtable_dump_bus_properties(f, bus_kill_vtable);
        vtable_dump_bus_properties(f, bus_manager_vtable);
        vtable_dump_bus_properties(f, bus_mount_vtable);
        vtable_dump_bus_properties(f, bus_path_vtable);
        vtable_dump_bus_properties(f, bus_scope_vtable);
        vtable_dump_bus_properties(f, bus_service_vtable);
        vtable_dump_bus_properties(f, bus_slice_vtable);
        vtable_dump_bus_properties(f, bus_socket_vtable);
        vtable_dump_bus_properties(f, bus_swap_vtable);
        vtable_dump_bus_properties(f, bus_target_vtable);
        vtable_dump_bus_properties(f, bus_timer_vtable);
        vtable_dump_bus_properties(f, bus_unit_vtable);
        vtable_dump_bus_properties(f, bus_unit_cgroup_vtable);
}