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; }
static int rmmod_do_module(struct kmod_module *mod, bool do_dependencies) { const char *modname = kmod_module_get_name(mod); struct kmod_list *pre = NULL, *post = NULL; const char *cmd = NULL; int err; if (!ignore_commands) { err = kmod_module_get_softdeps(mod, &pre, &post); if (err < 0) { WRN("could not get softdeps of '%s': %s\n", modname, strerror(-err)); return err; } cmd = kmod_module_get_remove_commands(mod); } if (cmd == NULL && !ignore_loaded) { int state = kmod_module_get_initstate(mod); if (state < 0) { LOG ("Module %s not found.\n", modname); err = -ENOENT; goto error; } else if (state == KMOD_MODULE_BUILTIN) { LOG("Module %s is builtin.\n", modname); err = -ENOENT; goto error; } else if (state != KMOD_MODULE_LIVE) { if (first_time) { LOG("Module %s is not in kernel.\n", modname); err = -ENOENT; goto error; } else { err = 0; goto error; } } } rmmod_do_deps_list(post, false); if (do_dependencies && remove_dependencies) { struct kmod_list *deps = kmod_module_get_dependencies(mod); err = rmmod_do_deps_list(deps, true); if (err < 0) goto error; } if (!ignore_loaded) { int usage = kmod_module_get_refcnt(mod); if (usage > 0) { if (!quiet_inuse) LOG("Module %s is in use.\n", modname); err = -EBUSY; goto error; } } if (cmd == NULL) err = rmmod_do_remove_module(mod); else err = command_do(mod, "remove", cmd, NULL); if (err < 0) goto error; rmmod_do_deps_list(pre, false); error: kmod_module_unref_list(pre); kmod_module_unref_list(post); return err; }