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; }
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; }
/** 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); }
/** * 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 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; }
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; }
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; }