Ejemplo n.º 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;
	}
Ejemplo n.º 2
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;
}
Ejemplo n.º 3
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;
}
Ejemplo n.º 4
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;
}
Ejemplo n.º 5
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;
}
Ejemplo n.º 6
0
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;
}
Ejemplo n.º 7
0
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;
}
Ejemplo n.º 8
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;
}
Ejemplo n.º 9
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);
		}
	}
Ejemplo n.º 10
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;
}
Ejemplo n.º 11
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;
}
Ejemplo n.º 12
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;
}
Ejemplo n.º 13
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);
}
Ejemplo n.º 14
0
/**                                                                
 * @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;
}
Ejemplo n.º 15
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;
}
Ejemplo n.º 16
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);
  }
Ejemplo n.º 17
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;
}
Ejemplo n.º 18
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;
}
Ejemplo n.º 19
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;
}