Exemple #1
0
static int insmod_do_deps_list(struct kmod_list *list,
				bool stop_on_errors, struct array *recursion)
{
	struct kmod_list *l;
	struct kmod_module *m;
	int r;

	if (list == NULL)
		return 0;

	m = kmod_module_get_module(list);
	r = insmod_recursion_has_loop(m, recursion);
	kmod_module_unref(m);

	if (r)
		return -ELOOP;

	kmod_list_foreach(l, list) {
		m = kmod_module_get_module(l);
		array_append(recursion, m);
		r = insmod_do_module(m, NULL, false, recursion);
		kmod_module_unref(m);
		array_pop(recursion);

		if (r == -ELOOP)
			return r;

		if (r < 0 && stop_on_errors)
			return r;
	}
Exemple #2
0
static int show_modversions(struct kmod_ctx *ctx, const char *filename)
{
	struct kmod_list *l, *list = NULL;
	struct kmod_module *mod;
	int err = kmod_module_new_from_path(ctx, filename, &mod);
	if (err < 0) {
		LOG("Module %s not found.\n", filename);
		return err;
	}

	err = kmod_module_get_versions(mod, &list);
	if (err < 0) {
		LOG("could not get modversions of %s: %s\n",
			filename, strerror(-err));
		kmod_module_unref(mod);
		return err;
	}

	kmod_list_foreach(l, list) {
		const char *symbol = kmod_module_version_get_symbol(l);
		uint64_t crc = kmod_module_version_get_crc(l);
		printf("0x%08"PRIx64"\t%s\n", crc, symbol);
	}
	kmod_module_versions_free_list(list);
	kmod_module_unref(mod);
	return 0;
}
Exemple #3
0
static int do_lsmod(int argc, char *argv[])
{
	struct kmod_ctx *ctx;
	const char *null_config = NULL;
	struct kmod_list *list, *itr;
	int err;

	if (argc != 1) {
		fprintf(stderr, "Usage: %s\n", argv[0]);
		return EXIT_FAILURE;
	}

	ctx = kmod_new(NULL, &null_config);
	if (ctx == NULL) {
		fputs("Error: kmod_new() failed!\n", stderr);
		return EXIT_FAILURE;
	}

	err = kmod_module_new_from_loaded(ctx, &list);
	if (err < 0) {
		fprintf(stderr, "Error: could not get list of modules: %s\n",
			strerror(-err));
		kmod_unref(ctx);
		return EXIT_FAILURE;
	}

	puts("Module                  Size  Used by");

	kmod_list_foreach(itr, list) {
		struct kmod_module *mod = kmod_module_get_module(itr);
		const char *name = kmod_module_get_name(mod);
		int use_count = kmod_module_get_refcnt(mod);
		long size = kmod_module_get_size(mod);
		struct kmod_list *holders, *hitr;
		int first = 1;

		printf("%-19s %8ld  %d ", name, size, use_count);
		holders = kmod_module_get_holders(mod);
		kmod_list_foreach(hitr, holders) {
			struct kmod_module *hm = kmod_module_get_module(hitr);

			if (!first)
				putchar(',');
			else
				first = 0;

			fputs(kmod_module_get_name(hm), stdout);
			kmod_module_unref(hm);
		}
		putchar('\n');
		kmod_module_unref_list(holders);
		kmod_module_unref(mod);
	}
	kmod_module_unref_list(list);
	kmod_unref(ctx);

	return EXIT_SUCCESS;
}
Exemple #4
0
static int loaded_1(const struct test *t)
{
	struct kmod_ctx *ctx;
	const char *null_config = NULL;
	struct kmod_list *list, *itr;
	int err;

	ctx = kmod_new(NULL, &null_config);
	if (ctx == NULL)
		exit(EXIT_FAILURE);

	err = kmod_module_new_from_loaded(ctx, &list);
	if (err < 0) {
		fprintf(stderr, "%s\n", strerror(-err));
		kmod_unref(ctx);
		exit(EXIT_FAILURE);
	}

	printf("Module                  Size  Used by\n");

	kmod_list_foreach(itr, list) {
		struct kmod_module *mod = kmod_module_get_module(itr);
		const char *name = kmod_module_get_name(mod);
		int use_count = kmod_module_get_refcnt(mod);
		long size = kmod_module_get_size(mod);
		struct kmod_list *holders, *hitr;
		int first = 1;

		printf("%-19s %8ld  %d ", name, size, use_count);
		holders = kmod_module_get_holders(mod);
		kmod_list_foreach(hitr, holders) {
			struct kmod_module *hm = kmod_module_get_module(hitr);

			if (!first)
				putchar(',');
			else
				first = 0;

			fputs(kmod_module_get_name(hm), stdout);
			kmod_module_unref(hm);
		}
		putchar('\n');
		kmod_module_unref_list(holders);
		kmod_module_unref(mod);
	}
	kmod_module_unref_list(list);

	kmod_unref(ctx);

	return EXIT_SUCCESS;
}
Exemple #5
0
int check(char *kmod_name){
	int ret = 0, err;
	struct kmod_ctx *ctx;
	struct kmod_module *mod;
	struct kmod_list *list, *itr;
	const char *null_config = NULL;

	printf("Checking if %s is loaded...\n", kmod_name);

	ctx = kmod_new(NULL, &null_config);
	if(ctx == NULL){
		ret = -1;
		printf("Unexpected error...\n");
	}else{
		err = kmod_module_new_from_loaded(ctx, &list);
		if(err < 0){
			ret = -1;
			printf("Error: %s\n", strerror(-err));
		}else{
			ret = 0;
			kmod_list_foreach(itr, list){
				mod = kmod_module_get_module(itr);
				if(strcmp(kmod_module_get_name(mod), kmod_name) == 0){
					ret = 1;
				}
				kmod_module_unref(mod);
			}
			kmod_module_unref_list(list);
		}
	}
Exemple #6
0
static int from_alias(const struct test *t)
{
	static const char *modnames[] = {
		"ext4.*",
		NULL,
	};
	const char **p;
	struct kmod_ctx *ctx;
	int err;

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

	for (p = modnames; p != NULL; p++) {
		struct kmod_list *l, *list = NULL;

		err = kmod_module_new_from_lookup(ctx, *p, &list);
		if (err < 0)
			exit(0);

		kmod_list_foreach(l, list) {
			struct kmod_module *m;
			m = kmod_module_get_module(l);

			printf("modname: %s\n", kmod_module_get_name(m));
			kmod_module_unref(m);
		}
		kmod_module_unref_list(list);
	}

	kmod_unref(ctx);

	return 0;
}
Exemple #7
0
static int check_module_inuse(struct kmod_module *mod) {
	struct kmod_list *holders;

	if (kmod_module_get_initstate(mod) == -ENOENT) {
		ERR("Module %s is not currently loaded\n",
				kmod_module_get_name(mod));
		return -ENOENT;
	}

	holders = kmod_module_get_holders(mod);
	if (holders != NULL) {
		struct kmod_list *itr;

		ERR("Module %s is in use by:", kmod_module_get_name(mod));

		kmod_list_foreach(itr, holders) {
			struct kmod_module *hm = kmod_module_get_module(itr);
			fprintf(stderr, " %s", kmod_module_get_name(hm));
			kmod_module_unref(hm);
		}
		fputc('\n', stderr);

		kmod_module_unref_list(holders);
		return -EBUSY;
	}

	if (kmod_module_get_refcnt(mod) != 0) {
		ERR("Module %s is in use\n", kmod_module_get_name(mod));
		return -EBUSY;
	}

	return 0;
}
Exemple #8
0
static int do_remove(int argc, char *argv[])
{
	struct kmod_ctx *ctx;
	struct kmod_module *mod;
	const char *name;
	int err, r = EXIT_SUCCESS;

	for (;;) {
		int c, idx =0;
		c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
		if (c == -1)
			break;
		switch (c) {
		case 'h':
			help();
			return EXIT_SUCCESS;

		default:
			ERR("Unexpected getopt_long() value '%c'.\n", c);
			return EXIT_FAILURE;
		}
	}

	if (optind >= argc) {
		ERR("Missing module name\n");
		return EXIT_FAILURE;
	}

	ctx = kmod_new(NULL, NULL);
	if (!ctx) {
		ERR("kmod_new() failed!\n");
		return EXIT_FAILURE;
	}

	name = argv[optind];
	err = kmod_module_new_from_name(ctx, name, &mod);
	if (err < 0) {
		ERR("Could not remove module %s: %s\n", name, strerror(-err));
		goto end;
	}

	err = check_module_inuse(mod);
	if (err < 0)
		goto unref;

	err = kmod_module_remove_module(mod, 0);
	if (err < 0)
		goto unref;

unref:
	kmod_module_unref(mod);

end:
	kmod_unref(ctx);
	if (err < 0) {
		r = EXIT_FAILURE;
		ERR("Could not remove module %s: %s\n", name, strerror(-err));
	}
	return r;
}
Exemple #9
0
static int load_module(struct udev *udev, const char *alias) {
        struct kmod_list *list = NULL;
        struct kmod_list *l;
        int err;

        err = kmod_module_new_from_lookup(ctx, alias, &list);
        if (err < 0)
                return err;

        if (list == NULL)
                log_debug("no module matches '%s'", alias);

        kmod_list_foreach(l, list) {
                struct kmod_module *mod = kmod_module_get_module(l);

                err = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL);
                if (err == KMOD_PROBE_APPLY_BLACKLIST)
                        log_debug("module '%s' is blacklisted", kmod_module_get_name(mod));
                else if (err == 0)
                        log_debug("inserted '%s'", kmod_module_get_name(mod));
                else
                        log_debug("failed to insert '%s'", kmod_module_get_name(mod));

                kmod_module_unref(mod);
        }

        kmod_module_unref_list(list);
        return err;
}
Exemple #10
0
/** unload module
 *  
 * @param module Name of the module to unload
 * @return 0 on success, non-zero on failure
 *
 */
int usb_moded_unload_module(const char *module)
{
	int ret = 0;


#ifdef NO_KMOD
	gchar *command;

	if(!strcmp(module, MODULE_NONE))
		return 0;

	command = g_strconcat("rmmod ", module, NULL);
	ret = system(command);
	g_free(command);
#else
	struct kmod_module *mod;

	if(!strcmp(module, MODULE_NONE))
		return 0;

	kmod_module_new_from_name(ctx, module, &mod);
	ret = kmod_module_remove_module(mod, KMOD_REMOVE_NOWAIT);
	kmod_module_unref(mod);

#endif /* NO_KMOD */

	return(ret);
}
static int load_module(const char *mod_name) {
        struct kmod_ctx *ctx;
        struct kmod_list *list = NULL, *l;
        int r;

        ctx = kmod_new(NULL, NULL);
        if (!ctx) {
                kmod_unref(ctx);
                return -ENOMEM;
        }

        r = kmod_module_new_from_lookup(ctx, mod_name, &list);
        if (r < 0)
                return -1;

        kmod_list_foreach(l, list) {
                struct kmod_module *mod = kmod_module_get_module(l);

                r = kmod_module_probe_insert_module(mod, 0, NULL, NULL, NULL, NULL);
                if (r >= 0)
                        r = 0;
                else
                        r = -1;

                kmod_module_unref(mod);
        }

        kmod_module_unref_list(list);
        kmod_unref(ctx);

        return r;
}
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;
}
Exemple #13
0
static int from_name(const struct test *t)
{
	static const char *modnames[] = {
		"ext4",
		"balbalbalbbalbalbalbalbalbalbal",
		"snd-hda-intel",
		"snd-timer",
		"iTCO_wdt",
		NULL,
	};
	const char **p;
	struct kmod_ctx *ctx;
	struct kmod_module *mod;
	const char *null_config = NULL;
	int err;

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

	for (p = modnames; p != NULL; p++) {
		err = kmod_module_new_from_name(ctx, *p, &mod);
		if (err < 0)
			exit(0);

		printf("modname: %s\n", kmod_module_get_name(mod));
		kmod_module_unref(mod);
	}

	kmod_unref(ctx);

	return 0;
}
static noreturn int test_dependencies(const struct test *t)
{
    struct kmod_ctx *ctx;
    struct kmod_module *mod = NULL;
    struct kmod_list *list, *l;
    int err;
    size_t len = 0;
    int fooa = 0, foob = 0, fooc = 0;

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

    err = kmod_module_new_from_name(ctx, "mod-foo", &mod);
    if (err < 0 || mod == NULL) {
        kmod_unref(ctx);
        exit(EXIT_FAILURE);
    }

    list = kmod_module_get_dependencies(mod);

    kmod_list_foreach(l, list) {
        struct kmod_module *m = kmod_module_get_module(l);
        const char *name = kmod_module_get_name(m);

        if (streq(name, "mod_foo_a"))
            fooa = 1;
        if (streq(name, "mod_foo_b"))
            foob = 1;
        else if (streq(name, "mod_foo_c"))
            fooc = 1;

        fprintf(stderr, "name=%s", name);
        kmod_module_unref(m);
        len++;
    }

    /* fooa, foob, fooc */
    if (len != 3 || !fooa || !foob || !fooc)
        exit(EXIT_FAILURE);

    kmod_module_unref_list(list);
    kmod_module_unref(mod);
    kmod_unref(ctx);

    exit(EXIT_SUCCESS);
}
/**                                                                
 * @attention 本注释得到了"核高基"科技重大专项2012年课题
 *             “开源操作系统内核分析和安全性评估
 *            (课题编号:2012ZX01039-004)”的资助。
 *                                                                      
 * @copyright 注释添加单位:清华大学——03任务
 *            (Linux内核相关通用基础软件包分析)
 *                                                                        
 * @author 注释添加人员: 李明
 *             (电子邮件 <*****@*****.**>)
 *                                                                    
 * @date 注释添加日期: 2013-6-1
 *                                                                   
 * @note 注释详细内容:                                                
 * 
 * @brief  测试模块的 kmod_module_get_dependencies 是否工作正确
 * 其中需要调用到 libkmod 模块中的以下接口
 *	- kmod_new()
 *	- kmod_module_new_from_name()
 *	- kmod_module_get_dependencies()
 *	- kmod_list_foreach()
 *	- kmod_module_get_module()
 *	- kmod_module_get_name()
 *	- kmod_module_unref_list()
 *	- kmod_module_unref()
 *	- kmod_unref()
 */
static int test_dependencies(const struct test *t)
{
	struct kmod_ctx *ctx;
	struct kmod_module *mod;
	struct kmod_list *list, *l;
	int err;
	size_t len = 0;
	int crc16 = 0, mbcache = 0, jbd2 = 0;

	ctx = kmod_new(NULL, NULL);
	if (ctx == NULL)
		return EXIT_FAILURE;

	err = kmod_module_new_from_name(ctx, "ext4", &mod);
	if (err < 0) {
		kmod_unref(ctx);
		return EXIT_FAILURE;
	}

	list = kmod_module_get_dependencies(mod);

	kmod_list_foreach(l, list) {
		struct kmod_module *m = kmod_module_get_module(l);
		const char *name = kmod_module_get_name(m);

		if (strcmp(name, "crc16") == 0)
			crc16 = 1;
		if (strcmp(name, "mbcache") == 0)
			mbcache = 1;
		else if (strcmp(name, "jbd2") == 0)
			jbd2 = 1;

		kmod_module_unref(m);
		len++;
	}

	/* crc16, mbcache, jbd2 */
	if (len != 3 || !crc16 || !mbcache || !jbd2)
		return EXIT_FAILURE;

	kmod_module_unref_list(list);
	kmod_module_unref(mod);
	kmod_unref(ctx);

	return EXIT_SUCCESS;
}
Exemple #16
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;
}
Exemple #17
0
static int load_module(struct kmod_ctx *ctx, const char *m) {
        const int probe_flags = KMOD_PROBE_APPLY_BLACKLIST;
        struct kmod_list *itr, *modlist = NULL;
        int r = 0;

        log_debug("load: %s", m);

        r = kmod_module_new_from_lookup(ctx, m, &modlist);
        if (r < 0) {
                log_error("Failed to lookup alias '%s': %s", m, strerror(-r));
                return r;
        }

        if (!modlist) {
                log_error("Failed to find module '%s'", m);
                return -ENOENT;
        }

        kmod_list_foreach(itr, modlist) {
                struct kmod_module *mod;
                int state, err;

                mod = kmod_module_get_module(itr);
                state = kmod_module_get_initstate(mod);

                switch (state) {
                case KMOD_MODULE_BUILTIN:
                        log_info("Module '%s' is builtin", kmod_module_get_name(mod));
                        break;

                case KMOD_MODULE_LIVE:
                        log_debug("Module '%s' is already loaded", kmod_module_get_name(mod));
                        break;

                default:
                        err = kmod_module_probe_insert_module(mod, probe_flags,
                                                              NULL, NULL, NULL, NULL);

                        if (err == 0)
                                log_info("Inserted module '%s'", kmod_module_get_name(mod));
                        else if (err == KMOD_PROBE_APPLY_BLACKLIST)
                                log_info("Module '%s' is blacklisted", kmod_module_get_name(mod));
                        else {
                                log_error("Failed to insert '%s': %s", kmod_module_get_name(mod),
                                          strerror(-err));
                                r = err;
                        }
                }

                kmod_module_unref(mod);
        }

        kmod_module_unref_list(modlist);

        return r;
}
Exemple #18
0
static int modinfo_path_do(struct kmod_ctx *ctx, const char *path)
{
	struct kmod_module *mod;
	int err = kmod_module_new_from_path(ctx, path, &mod);
	if (err < 0) {
		LOG("Module file %s not found.\n", path);
		return err;
	}
	err = modinfo_do(mod);
	kmod_module_unref(mod);
	return err;
}
Exemple #19
0
/** Check which state a module is in
 *
 * @return 1 if loaded, 0 when not loaded
 */
static int module_state_check(const char *module)
{
  int ret = 0;
  struct kmod_module *mod;

  kmod_module_new_from_name(ctx, module, &mod);
  ret = kmod_module_get_initstate(mod);
  kmod_module_unref(mod);
  if( ret == KMOD_MODULE_LIVE)
	return(1);
  else
	return(0);
}
Exemple #20
0
static int modprobe_lttng(struct kern_modules_param *modules,
		int entries, int required)
{
	int ret = 0, i;
	struct kmod_ctx *ctx;

	ctx = kmod_new(NULL, NULL);
	if (!ctx) {
		PERROR("Unable to create kmod library context");
		ret = -ENOMEM;
		goto error;
	}

	kmod_set_log_fn(ctx, log_kmod, NULL);
	kmod_load_resources(ctx);

	for (i = 0; i < entries; i++) {
		struct kmod_module *mod = NULL;

		ret = kmod_module_new_from_name(ctx, modules[i].name, &mod);
		if (ret < 0) {
			PERROR("Failed to create kmod module for %s", modules[i].name);
			goto error;
		}

		ret = kmod_module_probe_insert_module(mod, KMOD_PROBE_IGNORE_LOADED,
				NULL, NULL, NULL, NULL);
		if (ret < 0) {
			if (required) {
				ERR("Unable to load required module %s",
						modules[i].name);
				goto error;
			} else {
				DBG("Unable to load optional module %s; continuing",
						modules[i].name);
				ret = 0;
			}
		} else {
			DBG("Modprobe successfully %s", modules[i].name);
		}

		kmod_module_unref(mod);
	}

error:
	if (ctx) {
		kmod_unref(ctx);
	}
	return ret;
}
Exemple #21
0
int kmod_setup(void) {
        unsigned i;
        struct kmod_ctx *ctx = NULL;
        struct kmod_module *mod;
        int err;

        for (i = 0; i < ELEMENTSOF(kmod_table); i += 2) {

                if (access(kmod_table[i+1], F_OK) >= 0)
                        continue;

                log_debug("Your kernel apparently lacks built-in %s support. Might be a good idea to compile it in. "
                          "We'll now try to work around this by loading the module...",
                          kmod_table[i]);

                if (!ctx) {
                        ctx = kmod_new(NULL, NULL);
                        if (!ctx) {
                                log_error("Failed to allocate memory for kmod");
                                return -ENOMEM;
                        }

                        kmod_set_log_fn(ctx, systemd_kmod_log, NULL);

                        kmod_load_resources(ctx);
                }

                err = kmod_module_new_from_name(ctx, kmod_table[i], &mod);
                if (err < 0) {
                        log_error("Failed to load module '%s'", kmod_table[i]);
                        continue;
                }

                err = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL);
                if (err == 0)
                        log_info("Inserted module '%s'", kmod_module_get_name(mod));
                else if (err == KMOD_PROBE_APPLY_BLACKLIST)
                        log_info("Module '%s' is blacklisted", kmod_module_get_name(mod));
                else
                        log_error("Failed to insert '%s'", kmod_module_get_name(mod));

                kmod_module_unref(mod);
        }

        if (ctx)
                kmod_unref(ctx);

        return 0;
}
Exemple #22
0
static int rmmod_do_deps_list(struct kmod_list *list, bool stop_on_errors)
{
	struct kmod_list *l;

	kmod_list_foreach_reverse(l, list) {
		struct kmod_module *m = kmod_module_get_module(l);
		int r = rmmod_do_module(m, false);
		kmod_module_unref(m);

		if (r < 0 && stop_on_errors)
			return r;
	}

	return 0;
}
Exemple #23
0
/**
 * Checks whether a kernel module is loaded
 *
 * @param driver The name of the driver (not a filename)
 * @return 1 if the module is loaded, 0 otherwise
 */
int module_is_loaded(char *driver) {
  int err, state;
  struct kmod_module *mod;

  err = kmod_module_new_from_name(bb_status.kmod_ctx, driver, &mod);
  if (err < 0) {
    bb_log(LOG_DEBUG, "kmod_module_new_from_name(%s) failed (err: %d).\n",
      driver, err);
    return 0;
  }

  state = kmod_module_get_initstate(mod);
  kmod_module_unref(mod);

  return state == KMOD_MODULE_LIVE;
}
Exemple #24
0
int transport_load_kmod(char *transport_name)
{
	struct kmod_ctx *ctx;
	struct kmod_module *mod;
	int rc;

	ctx = kmod_new(NULL, NULL);
	if (!ctx) {
		log_error("Could not load transport module %s. Out of "
			  "memory.", transport_name);
		return ISCSI_ERR_NOMEM;
	}

	kmod_load_resources(ctx);

	/*
	 * dumb dumb dumb - named iscsi_tcp and ib_iser differently from
	 * transport name
	 */
	if (!strcmp(transport_name, "tcp"))
		rc = kmod_module_new_from_name(ctx, "iscsi_tcp", &mod);
	else if (!strcmp(transport_name, "iser"))
		rc = kmod_module_new_from_name(ctx, "ib_iser", &mod);
	else
		rc = kmod_module_new_from_name(ctx, transport_name, &mod);
	if (rc) {
		log_error("Failed to load module %s.", transport_name);
		rc = ISCSI_ERR_TRANS_NOT_FOUND;
		goto unref_mod;
	}

	rc = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST,
					     NULL, NULL, NULL, NULL);
	if (rc) {
		log_error("Could not insert module %s. Kmod error %d",
			  transport_name, rc);
		rc = ISCSI_ERR_TRANS_NOT_FOUND;
	}
	kmod_module_unref(mod);

unref_mod:
	kmod_unref(ctx);
	return rc;
}
Exemple #25
0
/**
 * Attempts to load a module.
 *
 * @param module_name The filename of the module to be loaded
 * @param driver The name of the driver to be loaded
 * @return 1 if the driver is succesfully loaded, 0 otherwise
 */
int module_load(char *module_name, char *driver) {
  int err = 0;
  int flags = KMOD_PROBE_IGNORE_LOADED;
  struct kmod_list *l, *list = NULL;

  if (module_is_loaded(driver) == 0) {
    /* the module has not loaded yet, try to load it */

    bb_log(LOG_INFO, "Loading driver '%s' (module '%s')\n", driver, module_name);
    err = kmod_module_new_from_lookup(bb_status.kmod_ctx, module_name, &list);

    if (err < 0) {
      bb_log(LOG_DEBUG, "kmod_module_new_from_lookup(%s) failed (err: %d).\n",
        module_name, err);
      return 0;
    }

    if (list == NULL) {
      bb_log(LOG_ERR, "Module '%s' not found.\n", module_name);
      return 0;
    }

    kmod_list_foreach(l, list) {
      struct kmod_module *mod = kmod_module_get_module(l);

      bb_log(LOG_DEBUG, "Loading module '%s'.\n", kmod_module_get_name(mod));
      err = kmod_module_probe_insert_module(mod, flags, NULL, NULL, NULL, 0);

      if (err < 0) {
        bb_log(LOG_DEBUG, "kmod_module_probe_insert_module(%s) failed (err: %d).\n",
          kmod_module_get_name(mod), err);
      }

      kmod_module_unref(mod);

      if (err < 0) {
        break;
      }
    }

    kmod_module_unref_list(list);
  }
Exemple #26
0
static int check_module_inuse(struct kmod_module *mod) {
	struct kmod_list *holders;
	int state, ret;

	state = kmod_module_get_initstate(mod);

	if (state == KMOD_MODULE_BUILTIN) {
		ERR("Module %s is builtin.\n", kmod_module_get_name(mod));
		return -ENOENT;
	} else if (state < 0) {
		ERR("Module %s is not currently loaded\n",
				kmod_module_get_name(mod));
		return -ENOENT;
	}

	holders = kmod_module_get_holders(mod);
	if (holders != NULL) {
		struct kmod_list *itr;

		ERR("Module %s is in use by:", kmod_module_get_name(mod));

		kmod_list_foreach(itr, holders) {
			struct kmod_module *hm = kmod_module_get_module(itr);
			fprintf(stderr, " %s", kmod_module_get_name(hm));
			kmod_module_unref(hm);
		}
		fputc('\n', stderr);

		kmod_module_unref_list(holders);
		return -EBUSY;
	}

	ret = kmod_module_get_refcnt(mod);
	if (ret > 0) {
		ERR("Module %s is in use\n", kmod_module_get_name(mod));
		return -EBUSY;
	} else if (ret == -ENOENT) {
		ERR("Module unloading is not supported\n");
	}

	return ret;
}
Exemple #27
0
static int rmmod(struct kmod_ctx *ctx, const char *alias)
{
	struct kmod_list *l, *list = NULL;
	int err;

	err = kmod_module_new_from_lookup(ctx, alias, &list);
	if (err < 0)
		return err;

	if (list == NULL)
		LOG("Module %s not found.\n", alias);

	kmod_list_foreach(l, list) {
		struct kmod_module *mod = kmod_module_get_module(l);
		err = rmmod_do_module(mod, true);
		kmod_module_unref(mod);
		if (err < 0)
			break;
	}

	kmod_module_unref_list(list);
	return err;
}
Exemple #28
0
static int modinfo_alias_do(struct kmod_ctx *ctx, const char *alias)
{
	struct kmod_list *l, *list = NULL;
	int err = kmod_module_new_from_lookup(ctx, alias, &list);
	if (err < 0) {
		LOG("Module alias %s not found.\n", alias);
		return err;
	}

	if (list == NULL) {
		LOG("Module %s not found.\n", alias);
		return -ENOENT;
	}

	kmod_list_foreach(l, list) {
		struct kmod_module *mod = kmod_module_get_module(l);
		int r = modinfo_do(mod);
		kmod_module_unref(mod);
		if (r < 0)
			err = r;
	}
	kmod_module_unref_list(list);
	return err;
}
Exemple #29
0
static int load_module(struct kmod_ctx *ctx, const char *m) {
	struct kmod_list *itr, *modlist = NULL;
	const int probe_flags = KMOD_PROBE_APPLY_BLACKLIST;
	const char *options = "luns=8 iSerialNumber=1234000 ro=1";
	int r = 0;

	r = kmod_module_new_from_lookup(ctx, m, &modlist);
	if (r < 0) {
		printf("ERROR: Failed to lookup alias '%s': %s\n", m, strerror(-r));
	}

	if (!modlist) {
		printf("ERROR: Failed to find module '%s'\n", m);
		return -ENOENT;
	}

	 kmod_list_foreach(itr, modlist) {
		struct kmod_module *mod;
		int state, err;

		mod = kmod_module_get_module(itr);
		state = kmod_module_get_initstate(mod);

		switch (state) {
			case KMOD_MODULE_BUILTIN:
				printf("Module '%s' is builtin\n", kmod_module_get_name(mod));
				break;
			case KMOD_MODULE_LIVE:
				printf("Module '%s' is already loaded\n", kmod_module_get_name(mod));
				//printf("Unloading module '%s'...\n", kmod_module_get_name(mod));

				//err = kmod_module_remove_module(mod, KMOD_REMOVE_FORCE);

				//if (err == 0)
				//{
				//	printf("Successfully removed module '%s'\n", kmod_module_get_name(mod));
				//	r = EAGAIN;
				//}
				//else
				//{
				//	printf("ERROR: Module '%s' can't be removed\n", kmod_module_get_name(mod));
				//	r = err;
				//}

				break;
			default:
				err = kmod_module_probe_insert_module(mod, probe_flags, options, NULL, NULL, NULL);

				if (err == 0)
					printf("Inserted module '%s'\n", kmod_module_get_name(mod));
				else if (err == KMOD_PROBE_APPLY_BLACKLIST)
					printf("Module '%s' is blacklisted\n", kmod_module_get_name(mod));
				else {
					printf("ERROR: Failed to insert '%s': %s\n", kmod_module_get_name(mod), strerror(-err));
					r = err;
				}
		}

		kmod_module_unref(mod);
	 }

	 kmod_module_unref_list(modlist);

	 return r;
}
Exemple #30
0
int kmod_setup(void) {
#ifdef HAVE_KMOD

        static const struct {
                const char *module;
                const char *path;
                bool warn_if_unavailable:1;
                bool warn_if_module:1;
                bool (*condition_fn)(void);
        } kmod_table[] = {
                /* auto-loading on use doesn't work before udev is up */
                { "autofs4",   "/sys/class/misc/autofs",    true,   false,   NULL      },

                /* early configure of ::1 on the loopback device */
                { "ipv6",      "/sys/module/ipv6",          false,  true,    NULL      },

                /* this should never be a module */
                { "unix",      "/proc/net/unix",            true,   true,    NULL      },

                /* IPC is needed before we bring up any other services */
                { "kdbus",     "/sys/fs/kdbus",             false,  false,   is_kdbus_wanted },

#ifdef HAVE_LIBIPTC
                /* netfilter is needed by networkd, nspawn among others, and cannot be autoloaded */
                { "ip_tables", "/proc/net/ip_tables_names", false,  false,   NULL      },
#endif
        };
        struct kmod_ctx *ctx = NULL;
        unsigned int i;
        int r;

        if (have_effective_cap(CAP_SYS_MODULE) == 0)
                return 0;

        for (i = 0; i < ELEMENTSOF(kmod_table); i++) {
                struct kmod_module *mod;

                if (kmod_table[i].path && access(kmod_table[i].path, F_OK) >= 0)
                        continue;

                if (kmod_table[i].condition_fn && !kmod_table[i].condition_fn())
                        continue;

                if (kmod_table[i].warn_if_module)
                        log_debug("Your kernel apparently lacks built-in %s support. Might be "
                                  "a good idea to compile it in. We'll now try to work around "
                                  "this by loading the module...", kmod_table[i].module);

                if (!ctx) {
                        ctx = kmod_new(NULL, NULL);
                        if (!ctx)
                                return log_oom();

                        kmod_set_log_fn(ctx, systemd_kmod_log, NULL);
                        kmod_load_resources(ctx);
                }

                r = kmod_module_new_from_name(ctx, kmod_table[i].module, &mod);
                if (r < 0) {
                        log_error("Failed to lookup module '%s'", kmod_table[i].module);
                        continue;
                }

                r = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL);
                if (r == 0)
                        log_debug("Inserted module '%s'", kmod_module_get_name(mod));
                else if (r == KMOD_PROBE_APPLY_BLACKLIST)
                        log_info("Module '%s' is blacklisted", kmod_module_get_name(mod));
                else {
                        bool print_warning = kmod_table[i].warn_if_unavailable || (r < 0 && r != -ENOENT);

                        log_full_errno(print_warning ? LOG_WARNING : LOG_DEBUG, r,
                                       "Failed to insert module '%s': %m", kmod_module_get_name(mod));
                }

                kmod_module_unref(mod);
        }

        if (ctx)
                kmod_unref(ctx);

#endif
        return 0;
}