예제 #1
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;
}
예제 #2
0
파일: lsmod.c 프로젝트: SebKuzminsky/kmod
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;
}
예제 #3
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;
}
예제 #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;
}
예제 #5
0
struct kmod_ctx* modalias_init(void) {
	if (aliasdefault.empty())
		set_default_alias_file();

	std::string dkms_file(table_name_dir + "dkms-modules.alias");

	/* We only use canned aliases as last resort. */
	const char *alias_filelist[] = {
		"/run/modprobe.d",
		"/etc/modprobe.d",
		"/lib/modprobe.d",
		"/lib/module-init-tools/ldetect-lst-modules.alias",
		aliasdefault.c_str(),
		dkms_file.c_str(),
		nullptr,
	};

	/* Init libkmod */
	struct kmod_ctx *ctx = kmod_new(dirname.c_str(), alias_filelist);
	if (!ctx) {
		fputs("Error: kmod_new() failed!\n", stderr);
		kmod_unref(ctx);
		ctx = nullptr;
	}
	kmod_load_resources(ctx);
	return ctx;
}
예제 #6
0
// test_insert.cmt
static __noreturn int test_insert(const struct test *t)
{
	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(EXIT_FAILURE);

	err = kmod_module_new_from_path(ctx, "/ext4-x86_64.ko", &mod);
	if (err != 0) {
		ERR("could not create module from path: %m\n");
		exit(EXIT_FAILURE);
	}

	err = kmod_module_insert_module(mod, 0, NULL);
	if (err != 0) {
		ERR("could not insert module: %m\n");
		exit(EXIT_FAILURE);
	}
	kmod_unref(ctx);

	exit(EXIT_SUCCESS);
}
예제 #7
0
// test_remove.cmt
static __noreturn int test_remove(const struct test *t)
{
	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(EXIT_FAILURE);

	err = kmod_module_new_from_name(ctx, "ext4", &mod);
	if (err != 0) {
		ERR("could not create module from name: %m\n");
		exit(EXIT_FAILURE);
	}

	err = kmod_module_remove_module(mod, 0);
	if (err != 0) {
		ERR("could not remove module: %m\n");
		exit(EXIT_FAILURE);
	}
	kmod_unref(ctx);

	exit(EXIT_SUCCESS);
}
예제 #8
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;
}
예제 #9
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;
}
예제 #10
0
파일: remove.c 프로젝트: lucasdemarchi/kmod
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;
}
예제 #11
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);
}
예제 #12
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;
}
예제 #13
0
// test_initlib.cmt
static __noreturn int test_initlib(const struct test *t)
{
	struct kmod_ctx *ctx;
	const char *null_config = NULL;

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

	kmod_unref(ctx);

	exit(EXIT_SUCCESS);
}
예제 #14
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;
}
예제 #15
0
파일: kmod-setup.c 프로젝트: adsr/systemd
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;
}
예제 #16
0
파일: test-init.c 프로젝트: Myxomopka/kmod
int main(int argc, char *argv[])
{
	struct kmod_ctx *ctx;
	const char *null_config = NULL;

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

	printf("libkmod version %s\n", VERSION);

	kmod_unref(ctx);

	return EXIT_SUCCESS;
}
예제 #17
0
파일: hid.cpp 프로젝트: DrakXtools/ldetect
void hid::probe(void)
{
    const std::string hidDevs("/sys/bus/hid/devices/");
    DIR *dir = opendir(hidDevs.c_str());
    if (dir == nullptr)
	return;
    struct kmod_ctx *ctx = modalias_init();

    std::ifstream f;
    for (struct dirent *dent = readdir(dir); dent != nullptr; dent = readdir(dir)) {
	if ((dent->d_type != DT_DIR && dent->d_type != DT_LNK) || !strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
	    continue;
	std::string hidDev(std::string(hidDevs).append(dent->d_name));

	f.open((hidDev + "/modalias").c_str());
	std::string modname;
	if (f.is_open()) {
	    std::string modalias;
	    getline(f, modalias);
	    std::vector<std::string> kmodules = modalias_resolve_modules(ctx, modalias);
	    if (!kmodules.empty())
		modname = kmodules.front();
	    f.close();
	}

	f.open((hidDev + "/uevent").c_str());
	std::string deviceName;
	if (f.is_open()) {
	    std::string line;
	    while (!f.eof()) {
		getline(f, line);
		if (!line.compare(0,sizeof("HID_NAME")-1, "HID_NAME")) {
		    deviceName.assign(line, sizeof("HID_NAME"), line.size()-sizeof("HID_NAME"));
		    break;
		}
	    }
	    f.close();
	} else
	    deviceName = "HID Device";

	if (!modname.empty() && !(!_entries.empty() && _entries.back().module == modname))
	    _entries.push_back(entry(modname, deviceName));
    }

    kmod_unref(ctx);
    closedir(dir);
}
예제 #18
0
파일: parent-uuid.c 프로젝트: bgbhpe/ndctl
int test_parent_uuid(int loglevel, struct ndctl_test *test)
{
	struct ndctl_ctx *ctx;
	struct kmod_module *mod;
	struct kmod_ctx *kmod_ctx;
	int err, result = EXIT_FAILURE;

	if (!ndctl_test_attempt(test, KERNEL_VERSION(4, 3, 0)))
		return 77;

	err = ndctl_new(&ctx);
	if (err < 0)
		exit(EXIT_FAILURE);

	ndctl_set_log_priority(ctx, loglevel);

	kmod_ctx = kmod_new(NULL, NULL);
	if (!kmod_ctx)
		goto err_kmod;

	err = kmod_module_new_from_name(kmod_ctx, NFIT_TEST_MODULE, &mod);
	if (err < 0)
		goto err_module;

	err = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST,
			NULL, NULL, NULL, NULL);
	if (err < 0) {
		result = 77;
		ndctl_test_skip(test);
		fprintf(stderr, "%s unavailable skipping tests\n",
				NFIT_TEST_MODULE);
		goto err_module;
	}

	err = do_test(ctx);
	if (err == 0)
		result = EXIT_SUCCESS;
	kmod_module_remove_module(mod, 0);

 err_module:
	kmod_unref(kmod_ctx);
 err_kmod:
	ndctl_unref(ctx);
	return result;
}
예제 #19
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;
}
예제 #20
0
파일: pci.cpp 프로젝트: DrakXtools/ldetect
void pci::findModules(std::string &&fpciusbtable, bool descr_lookup) {
    ldetect::findModules(fpciusbtable, descr_lookup, _entries);
    ::kmod_ctx *ctx = modalias_init();

    for (std::vector<pciEntry>::iterator it = _entries.begin();
	    it != _entries.end(); ++it) {
	pciEntry &e = *it;

	// No special case found in pcitable ? Then lookup modalias for PCI devices
	if (!e.module.empty() && (e.module != "unknown" && e.card.empty()))
	    continue;
	{
	    std::ostringstream devname(std::ostringstream::out);
	    devname << hexFmt(e.pci_domain, 4, false) << ":" <<  hexFmt(e.bus, 2, false) <<
		":" << hexFmt(e.pciusb_device, 2, false) << "." << hexFmt(e.pci_function, 0, false);

	    std::string sysDir = std::string("/sys/bus/pci/devices/").append(devname.str());
	    char buf[1024];
	    auto n = readlink(std::string(sysDir + "/driver").c_str(), buf, sizeof(buf));
	    if(n > 0) {
		buf[n] = 0;

		char* drv;
		if ((drv = strrchr(buf, '/')))
		    e.module = drv + 1;
		else
		    e.module = buf;
	    }
	    std::ifstream f(sysDir.append("/modalias").c_str());
	    if (f.is_open()) {
		std::string modalias;
		getline(f, modalias);
		e.kmodules = modalias_resolve_modules(ctx, modalias);
	    }

	}
    }

    kmod_unref(ctx);

}
예제 #21
0
파일: kmod.c 프로젝트: amituttam/examples
int main( int argc, char **argv )
{
	int r;
	struct kmod_ctx *ctx;

	ctx = kmod_new(NULL, NULL);
	if (!ctx) {
		printf("Failed to allocate memory for kmod\n");
	}

	kmod_load_resources(ctx);

	/* Load g_mass_storage module */
	r = load_module(ctx, "g_mass_storage");
	if (r == EAGAIN)
		load_module(ctx, "g_mass_storage");


	kmod_unref(ctx);
	return 0;
}
예제 #22
0
static int do_modinfo(int argc, char *argv[])
{
	struct kmod_ctx *ctx;
	char dirname_buf[PATH_MAX];
	const char *dirname = NULL;
	const char *kversion = NULL;
	const char *root = NULL;
	const char *null_config = NULL;
	int i, err;

	for (;;) {
		int c, idx = 0;
		c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
		if (c == -1)
			break;
		switch (c) {
		case 'a':
			field = "author";
			break;
		case 'd':
			field = "description";
			break;
		case 'l':
			field = "license";
			break;
		case 'p':
			field = "parm";
			break;
		case 'n':
			field = "filename";
			break;
		case '0':
			separator = '\0';
			break;
		case 'F':
			field = optarg;
			break;
		case 'k':
			kversion = optarg;
			break;
		case 'b':
			root = optarg;
			break;
		case 'h':
			help(basename(argv[0]));
			return EXIT_SUCCESS;
		case 'V':
			puts(PACKAGE " version " VERSION);
			return EXIT_SUCCESS;
		case '?':
			return EXIT_FAILURE;
		default:
			fprintf(stderr,
				"Error: unexpected getopt_long() value '%c'.\n",
				c);
			return EXIT_FAILURE;
		}
	}

	if (optind >= argc) {
		fprintf(stderr, "Error: missing module or filename.\n");
		return EXIT_FAILURE;
	}

	if (root != NULL || kversion != NULL) {
		struct utsname u;
		if (root == NULL)
			root = "";
		if (kversion == NULL) {
			if (uname(&u) < 0) {
				fprintf(stderr, "Error: uname() failed: %s\n",
					strerror(errno));
				return EXIT_FAILURE;
			}
			kversion = u.release;
		}
		snprintf(dirname_buf, sizeof(dirname_buf), "%s" ROOTPREFIX "/lib/modules/%s",
			 root, kversion);
		dirname = dirname_buf;
	}

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

	err = 0;
	for (i = optind; i < argc; i++) {
		const char *name = argv[i];
		struct stat st;
		int r;

		if (stat(name, &st) == 0 && S_ISREG(st.st_mode))
			r = modinfo_path_do(ctx, name);
		else
			r = modinfo_alias_do(ctx, name);

		if (r < 0)
			err = r;
	}

	kmod_unref(ctx);
	return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
예제 #23
0
/* called on udev shutdown and reload request */
static void builtin_kmod_exit(struct udev *udev) {
        log_debug("unload module index");
        ctx = kmod_unref(ctx);
}
예제 #24
0
/* called on udev shutdown and reload request */
static void builtin_kmod_exit(void) {
        log_debug("Unload module index");
        ctx = kmod_unref(ctx);
}
예제 #25
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;
}
예제 #26
0
// do_insmod.cmt
static int do_insmod(int argc, char *argv[])
{
	struct kmod_ctx *ctx;
	struct kmod_module *mod;
	const char *filename;
	char *opts = NULL;
	size_t optslen = 0;
	int i, err;
	const char *null_config = NULL;

	printf("begin to parse para\n");
	for (;;) {
		int c, idx = 0;
		c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
		printf("c = %d\n", c);
		if (c == -1)
			break;
		switch (c) {
		case 'p':
		case 's':
		case 'f':
			/* ignored, for compatibility only */
			break;
		case 'h':
			help();
			return EXIT_SUCCESS;
		case 'V':
			puts(PACKAGE " version " VERSION);
			return EXIT_SUCCESS;
		case '?':
			return EXIT_FAILURE;
		default:
			printf("no this para\n");
			ERR("unexpected getopt_long() value '%c'.\n",
				c);
			return EXIT_FAILURE;
		}
	}

	if (optind >= argc) {
		ERR("missing filename.\n");
		return EXIT_FAILURE;
	}

	filename = argv[optind];
	if (strcmp(filename, "-") == 0) {
		ERR("this tool does not support loading from stdin!\n");
		return EXIT_FAILURE;
	}

	for (i = optind + 1; i < argc; i++) {
		size_t len = strlen(argv[i]);
		void *tmp = realloc(opts, optslen + len + 2);
		if (tmp == NULL) {
			ERR("out of memory\n");
			free(opts);
			return EXIT_FAILURE;
		}
		opts = tmp;
		if (optslen > 0) {
			opts[optslen] = ' ';
			optslen++;
		}
		memcpy(opts + optslen, argv[i], len);
		optslen += len;
		opts[optslen] = '\0';
	}

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

	err = kmod_module_new_from_path(ctx, filename, &mod);
	if (err < 0) {
		ERR("could not load module %s: %s\n", filename,
		    strerror(-err));
		goto end;
	}

	err = kmod_module_insert_module(mod, 0, opts);
	if (err < 0) {
		ERR("could not insert module %s: %s\n", filename,
		    mod_strerror(-err));
	}
	kmod_module_unref(mod);

end:
	kmod_unref(ctx);
	free(opts);
	return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
예제 #27
0
static int do_rmmod(int argc, char**argv)
{
	struct kmod_ctx *ctx;
	const char *null_config = NULL;
	int flags = 0;
	int i, err, r = 0;

	for (;;) {
		int c, idx = 0;
		c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
		if (c == -1)
			break;
		switch (c) {
		case 'f':
			flags |= KMOD_REMOVE_FORCE;
			break;
		case 's':
			use_syslog = 1;
			break;
		case 'v':
			verbose++;
			break;
		case 'h':
			help(argc,argv);
			return EXIT_SUCCESS;
		case 'V':
			puts(PACKAGE " version " VERSION);
			return EXIT_SUCCESS;
		case '?':
			return EXIT_FAILURE;
		default:
			ERR("unexpected getopt_long() value '%c'.\n", c);
			return EXIT_FAILURE;
		}
	}

	log_open(use_syslog);

	if (optind >= argc) {
		ERR("missing module name.\n");
		r = EXIT_FAILURE;
		goto done;
	}

	ctx = kmod_new(NULL, &null_config);
	if (!ctx) {
		ERR("kmod_new() failed!\n");
		r = EXIT_FAILURE;
		goto done;
	}

	log_setup_kmod_log(ctx, verbose);

	for (i = optind; i < argc; i++) {
		struct kmod_module *mod;
		const char *arg = argv[i];
		struct stat st;
		if (stat(arg, &st) == 0)
			err = kmod_module_new_from_path(ctx, arg, &mod);
		else
			err = kmod_module_new_from_name(ctx, arg, &mod);

		if (err < 0) {
			ERR("could not use module %s: %s\n", arg,
			    strerror(-err));
			break;
		}

		if (!(flags & KMOD_REMOVE_FORCE) && check_module_inuse(mod) < 0) {
			r++;
			goto next;
		}

		err = kmod_module_remove_module(mod, flags);
		if (err < 0) {
			ERR("could not remove module %s: %s\n", arg,
			    strerror(-err));
			r++;
		}
next:
		kmod_module_unref(mod);
	}

	kmod_unref(ctx);

done:
	log_close();

	return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}