std::vector<std::string> modalias_resolve_modules(struct kmod_ctx *ctx, const std::string &modalias) {

	struct kmod_list *l = nullptr, *list = nullptr, *filtered = nullptr;
	std::vector<std::string> modules;
	int err = kmod_module_new_from_lookup(ctx, modalias.c_str(), &list);
	if (err < 0)
		goto exit;

	// No module found...
	if (list == nullptr)
		goto exit;

	// filter through blacklist
	err =  kmod_module_apply_filter(ctx, KMOD_FILTER_BLACKLIST, list, &filtered);
	kmod_module_unref_list(list);
	if (err <0)
		goto exit;
	list = filtered;

	kmod_list_foreach(l, list) {
		struct kmod_module *mod = kmod_module_get_module(l);
		const char *str = kmod_module_get_name(mod);
		if (modules.empty() || modules.back() != str)
		    modules.push_back(str);
		kmod_module_unref(mod);
		if (err < 0)
			break;
	}

	kmod_module_unref_list(list);

exit:
	return modules;
}
Example #2
0
static int blacklist_1(const struct test *t)
{
    struct kmod_ctx *ctx;
    struct kmod_list *list = NULL, *l, *filtered;
    struct kmod_module *mod;
    int err;
    size_t len = 0;

    const char *names[] = { "pcspkr", "pcspkr2", "floppy", "ext4", NULL };
    const char **name;

    ctx = kmod_new(NULL, NULL);
    if (ctx == NULL)
        exit(1);

    for(name = names; *name; name++) {
        err = kmod_module_new_from_name(ctx, *name, &mod);
        if (err < 0)
            goto fail_lookup;
        list = kmod_list_append(list, mod);
    }

    err = kmod_module_apply_filter(ctx, KMOD_FILTER_BLACKLIST, list,
                                   &filtered);
    if (err < 0) {
        ERR("Could not filter: %s\n", strerror(-err));
        goto fail;
    }
    if (filtered == NULL) {
        ERR("All modules were filtered out!\n");
        goto fail;
    }

    kmod_list_foreach(l, filtered) {
        const char *modname;
        mod = kmod_module_get_module(l);
        modname = kmod_module_get_name(mod);
        if (strcmp("pcspkr", modname) == 0 || strcmp("floppy", modname) == 0)
            goto fail;
        len++;
        kmod_module_unref(mod);
    }

    if (len != 2)
        goto fail;

    kmod_module_unref_list(filtered);
    kmod_module_unref_list(list);
    kmod_unref(ctx);

    return 0;

fail:
    kmod_module_unref_list(list);
fail_lookup:
    kmod_unref(ctx);
    return 1;
}