static void test_hashmap_foreach(void) {
        Hashmap *m;
        Iterator i;
        bool value_found[] = { false, false, false, false };
        char *val1, *val2, *val3, *val4, *s;
        unsigned count;

        val1 = strdup("my val1");
        assert_se(val1);
        val2 = strdup("my val2");
        assert_se(val2);
        val3 = strdup("my val3");
        assert_se(val3);
        val4 = strdup("my val4");
        assert_se(val4);

        m = NULL;

        count = 0;
        HASHMAP_FOREACH(s, m, i)
                count++;
        assert_se(count == 0);

        m = hashmap_new(&string_hash_ops);

        count = 0;
        HASHMAP_FOREACH(s, m, i)
                count++;
        assert_se(count == 0);

        hashmap_put(m, "Key 1", val1);
        hashmap_put(m, "Key 2", val2);
        hashmap_put(m, "Key 3", val3);
        hashmap_put(m, "Key 4", val4);

        HASHMAP_FOREACH(s, m, i) {
                if (!value_found[0] && streq(s, val1))
                        value_found[0] = true;
                else if (!value_found[1] && streq(s, val2))
                        value_found[1] = true;
                else if (!value_found[2] && streq(s, val3))
                        value_found[2] = true;
                else if (!value_found[3] && streq(s, val4))
                        value_found[3] = true;
        }

        assert_se(m);
        assert_se(value_found[0] && value_found[1] && value_found[2] && value_found[3]);

        hashmap_free_free(m);
}
Beispiel #2
0
static int test_unit_file_get_set(void) {
        int r;
        Hashmap *h;
        Iterator i;
        UnitFileList *p;

        h = hashmap_new(&string_hash_ops);
        assert_se(h);

        r = unit_file_get_list(UNIT_FILE_SYSTEM, NULL, h, NULL, NULL);

        if (IN_SET(r, -EPERM, -EACCES)) {
                log_notice_errno(r, "Skipping test: unit_file_get_list: %m");
                return EXIT_TEST_SKIP;
        }

        log_full_errno(r == 0 ? LOG_INFO : LOG_ERR, r,
                       "unit_file_get_list: %m");
        if (r < 0)
                return EXIT_FAILURE;

        HASHMAP_FOREACH(p, h, i)
                printf("%s = %s\n", p->path, unit_file_state_to_string(p->state));

        unit_file_list_free(h);

        return 0;
}
Beispiel #3
0
void bus_match_dump(struct bus_match_node *node, unsigned level) {
        struct bus_match_node *c;
        _cleanup_free_ char *pfx = NULL;
        char buf[32];

        if (!node)
                return;

        pfx = strrep("  ", level);
        printf("%s[%s]", strempty(pfx), bus_match_node_type_to_string(node->type, buf, sizeof(buf)));

        if (node->type == BUS_MATCH_VALUE) {
                if (node->parent->type == BUS_MATCH_MESSAGE_TYPE)
                        printf(" <%u>\n", node->value.u8);
                else
                        printf(" <%s>\n", node->value.str);
        } else if (node->type == BUS_MATCH_ROOT)
                puts(" root");
        else if (node->type == BUS_MATCH_LEAF)
                printf(" %p/%p\n", node->leaf.callback, node->leaf.userdata);
        else
                putchar('\n');

        if (BUS_MATCH_CAN_HASH(node->type)) {
                Iterator i;

                HASHMAP_FOREACH(c, node->compare.children, i)
                        bus_match_dump(c, level + 1);
        }

        for (c = node->child; c; c = c->next)
                bus_match_dump(c, level + 1);
}
Beispiel #4
0
_public_ int sd_lldp_get_neighbors(sd_lldp *lldp, sd_lldp_neighbor ***ret) {
        sd_lldp_neighbor **l = NULL, *n;
        Iterator i;
        int k = 0, r;

        assert_return(lldp, -EINVAL);
        assert_return(ret, -EINVAL);

        if (hashmap_isempty(lldp->neighbor_by_id)) { /* Special shortcut */
                *ret = NULL;
                return 0;
        }

        l = new0(sd_lldp_neighbor*, hashmap_size(lldp->neighbor_by_id));
        if (!l)
                return -ENOMEM;

        r = lldp_start_timer(lldp, NULL);
        if (r < 0) {
                free(l);
                return r;
        }

        HASHMAP_FOREACH(n, lldp->neighbor_by_id, i)
                l[k++] = sd_lldp_neighbor_ref(n);

        assert((size_t) k == hashmap_size(lldp->neighbor_by_id));

        /* Return things in a stable order */
        qsort(l, k, sizeof(sd_lldp_neighbor*), neighbor_compare_func);
        *ret = l;

        return k;
}
Beispiel #5
0
int manager_startup(Manager *m) {
        int r;
        Machine *machine;
        Iterator i;

        assert(m);
        assert(m->epoll_fd <= 0);

        m->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
        if (m->epoll_fd < 0)
                return -errno;

        /* Connect to the bus */
        r = manager_connect_bus(m);
        if (r < 0)
                return r;

        /* Deserialize state */
        manager_enumerate_machines(m);

        /* Remove stale objects before we start them */
        manager_gc(m, false);

        /* And start everything */
        HASHMAP_FOREACH(machine, m->machines, i)
                machine_start(machine, NULL);

        return 0;
}
static int kbdctx_refresh_compose_table(kbdctx *kc, const char *lang) {
        kbdtbl *kt;
        idev_session *s;
        idev_device *d;
        Iterator i, j;
        int r;

        if (!lang)
                lang = "C";

        if (streq_ptr(kc->locale_lang, lang))
                return 0;

        r = free_and_strdup(&kc->locale_lang, lang);
        if (r < 0)
                return r;

        log_debug("idev-keyboard: new default compose table: [ %s ]", lang);

        r = kbdtbl_new_from_locale(&kt, kc, lang);
        if (r < 0) {
                /* TODO: We need to catch the case where no compose-file is
                 * available. xkb doesn't tell us so far.. so we must not treat
                 * it as a hard-failure but just continue. Preferably, we want
                 * xkb to tell us exactly whether compilation failed or whether
                 * there is no compose file available for this locale. */
                log_debug_errno(r, "idev-keyboard: cannot load compose-table for '%s': %m",
                                lang);
                r = 0;
                kt = NULL;
        }

        kbdtbl_unref(kc->kbdtbl);
        kc->kbdtbl = kt;

        HASHMAP_FOREACH(s, kc->context->session_map, i)
                HASHMAP_FOREACH(d, s->device_map, j)
                        if (idev_is_keyboard(d))
                                keyboard_update_kbdtbl(keyboard_from_device(d));

        return 0;
}
static int kbdctx_refresh_keymap(kbdctx *kc) {
        idev_session *s;
        idev_device *d;
        Iterator i, j;
        kbdmap *km;
        int r;

        if (kc->kbdmap &&
            streq_ptr(kc->locale_x11_model, kc->last_x11_model) &&
            streq_ptr(kc->locale_x11_layout, kc->last_x11_layout) &&
            streq_ptr(kc->locale_x11_variant, kc->last_x11_variant) &&
            streq_ptr(kc->locale_x11_options, kc->last_x11_options))
                return 0 ;

        move_str(&kc->last_x11_model, &kc->locale_x11_model);
        move_str(&kc->last_x11_layout, &kc->locale_x11_layout);
        move_str(&kc->last_x11_variant, &kc->locale_x11_variant);
        move_str(&kc->last_x11_options, &kc->locale_x11_options);

        log_debug("idev-keyboard: new default keymap: [%s / %s / %s / %s]",
                  kc->last_x11_model, kc->last_x11_layout, kc->last_x11_variant, kc->last_x11_options);

        /* TODO: add a fallback keymap that's compiled-in */
        r = kbdmap_new_from_names(&km, kc, kc->last_x11_model, kc->last_x11_layout,
                                  kc->last_x11_variant, kc->last_x11_options);
        if (r < 0)
                return log_debug_errno(r, "idev-keyboard: cannot create keymap from locale1: %m");

        kbdmap_unref(kc->kbdmap);
        kc->kbdmap = km;

        HASHMAP_FOREACH(s, kc->context->session_map, i)
                HASHMAP_FOREACH(d, s->device_map, j)
                        if (idev_is_keyboard(d))
                                keyboard_update_kbdmap(keyboard_from_device(d));

        return 0;
}
Beispiel #8
0
void bus_match_free(struct bus_match_node *node) {
        struct bus_match_node *c;

        if (!node)
                return;

        if (BUS_MATCH_CAN_HASH(node->type)) {
                Iterator i;

                HASHMAP_FOREACH(c, node->compare.children, i)
                        bus_match_free(c);

                assert(hashmap_isempty(node->compare.children));
        }

        while ((c = node->child))
                bus_match_free(c);

        if (node->type != BUS_MATCH_ROOT)
                bus_match_node_free(node);
}
static int test_unit_file_get_set(void) {
        int r;
        Hashmap *h;
        Iterator i;
        UnitFileList *p;

        h = hashmap_new(string_hash_func, string_compare_func);
        assert(h);

        r = unit_file_get_list(UNIT_FILE_SYSTEM, NULL, h);
        log_full(r == 0 ? LOG_INFO : LOG_ERR,
                 "unit_file_get_list: %s", strerror(-r));
        if (r < 0)
                return EXIT_FAILURE;

        HASHMAP_FOREACH(p, h, i)
                printf("%s = %s\n", p->path, unit_file_state_to_string(p->state));

        unit_file_list_free(h);

        return 0;
}
Beispiel #10
0
int manager_startup(Manager *m) {
        Machine *machine;
        Iterator i;
        int r;

        assert(m);

        /* Connect to the bus */
        r = manager_connect_bus(m);
        if (r < 0)
                return r;

        /* Deserialize state */
        manager_enumerate_machines(m);

        /* Remove stale objects before we start them */
        manager_gc(m, false);

        /* And start everything */
        HASHMAP_FOREACH(machine, m->machines, i)
                machine_start(machine, NULL, NULL);

        return 0;
}