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; }
/** 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 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; }
// 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); }
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; }
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; }
/** 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); }
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; }
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; }
/** * 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; }
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; }
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; }
/** load module * * @param module Name of the module to load * @return 0 on success, non-zero on failure * */ int usb_moded_load_module(const char *module) { int ret = 0; #ifdef NO_KMOD gchar *command; if(!strcmp(module, MODULE_NONE)) return 0; command = g_strconcat("modprobe ", module, NULL); ret = system(command); if(!strcmp(module, MODULE_MASS_STORAGE) && (ret != 0)) { command = g_strconcat("modprobe ", MODULE_FILE_STORAGE, NULL); ret = system(command); } if(!strcmp(module, MODULE_CHARGING) && (ret != 0)) { command = g_strconcat("modprobe ", MODULE_CHARGE_FALLBACK, NULL); ret = system(command); } g_free(command); #else const int probe_flags = KMOD_PROBE_APPLY_BLACKLIST; struct kmod_module *mod; char *charging_args = NULL; char *load = NULL; if(!strcmp(module, MODULE_NONE)) return 0; /* copy module to load as it might be modified if we're trying charging mode */ load = strdup(module); if(!strcmp(module, MODULE_CHARGING) || !strcmp(module, MODULE_CHARGE_FALLBACK)) { /* split the string in module name and argument, they are the same for MODULE_CHARGE_FALLBACK so no need to handle them separately */ gchar **strings; /* since the mass_storage module is the newer one and we check against it to avoid loading failures we use it here, as we fall back to g_file_storage if g_mass_storage fails to load */ strings = g_strsplit(MODULE_CHARGE_FALLBACK, " ", 2); //log_debug("module args = %s, module = %s\n", strings[1], strings[0]); charging_args = strdup(strings[1]); /* load was already assigned. Free it to re-assign */ free(load); load = strdup(strings[0]); g_strfreev(strings); } ret = kmod_module_new_from_name(ctx, load, &mod); /* since kmod_module_new_from_name does not check if the module exists we test it's path in case we deal with the mass-storage one */ if(!strcmp(module, MODULE_MASS_STORAGE) && (kmod_module_get_path(mod) == NULL)) { log_debug("Fallback on older g_file_storage\n"); ret = kmod_module_new_from_name(ctx, MODULE_FILE_STORAGE, &mod); } if(!charging_args) ret = kmod_module_probe_insert_module(mod, probe_flags, NULL, NULL, NULL, NULL); else { ret = kmod_module_probe_insert_module(mod, probe_flags, charging_args, NULL, NULL, NULL); free(charging_args); } kmod_module_unref(mod); free(load); #endif /* NO_KMOD */ if( ret == 0) log_info("Module %s loaded successfully\n", module); else log_info("Module %s failed to load\n", module); return(ret); }
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; }
int test_pmem_namespaces(int log_level, struct ndctl_test *test) { struct ndctl_region *region, *pmem_region = NULL; struct kmod_ctx *kmod_ctx = NULL; struct kmod_module *mod = NULL; struct ndctl_namespace *ndns; struct ndctl_dimm *dimm; struct ndctl_ctx *ctx; struct ndctl_bus *bus; char bdev[50]; int rc; if (!ndctl_test_attempt(test, KERNEL_VERSION(4, 2, 0))) return 77; rc = ndctl_new(&ctx); if (rc < 0) return rc; ndctl_set_log_priority(ctx, log_level); bus = ndctl_bus_get_by_provider(ctx, "ACPI.NFIT"); if (!bus) { fprintf(stderr, "ACPI.NFIT unavailable falling back to nfit_test\n"); kmod_ctx = kmod_new(NULL, NULL); if (!kmod_ctx) goto err_kmod; kmod_set_log_priority(kmod_ctx, log_level); rc = kmod_module_new_from_name(kmod_ctx, "nfit_test", &mod); if (rc < 0) goto err_module; rc = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL); ndctl_invalidate(ctx); bus = ndctl_bus_get_by_provider(ctx, "nfit_test.0"); if (rc < 0 || !bus) { rc = 77; ndctl_test_skip(test); fprintf(stderr, "nfit_test unavailable skipping tests\n"); goto err_module; } } fprintf(stderr, "%s: found provider: %s\n", comm, ndctl_bus_get_provider(bus)); /* get the system to a clean state */ ndctl_region_foreach(bus, region) ndctl_region_disable_invalidate(region); ndctl_dimm_foreach(bus, dimm) { rc = ndctl_dimm_zero_labels(dimm); if (rc < 0) { fprintf(stderr, "failed to zero %s\n", ndctl_dimm_get_devname(dimm)); goto err; } }
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; }