X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=src%2Fbin%2Flttng-sessiond%2Fmodprobe.c;h=90fec711b8120c740d0ac4d48239b68c7db097c4;hb=866c17cef5ab4305d0087cf2823e92f0057904c0;hp=81ce8105a220be9568618ac07ceb032811432127;hpb=51c62b8c142906933436ec987ddd451430983b79;p=lttng-tools.git diff --git a/src/bin/lttng-sessiond/modprobe.c b/src/bin/lttng-sessiond/modprobe.c index 81ce8105a..90fec711b 100644 --- a/src/bin/lttng-sessiond/modprobe.c +++ b/src/bin/lttng-sessiond/modprobe.c @@ -90,83 +90,9 @@ static struct kern_modules_param *probes; static int nr_probes; static int probes_capacity; -static void modprobe_remove_lttng(const struct kern_modules_param *modules, - int entries, int required) -{ - int ret = 0, i; - char modprobe[256]; - - for (i = entries - 1; i >= 0; i--) { - ret = snprintf(modprobe, sizeof(modprobe), - "/sbin/modprobe -r -q %s", - modules[i].name); - if (ret < 0) { - PERROR("snprintf modprobe -r"); - return; - } - modprobe[sizeof(modprobe) - 1] = '\0'; - ret = system(modprobe); - if (ret == -1) { - ERR("Unable to launch modprobe -r for module %s", - modules[i].name); - } else if (required && WEXITSTATUS(ret) != 0) { - ERR("Unable to remove module %s", - modules[i].name); - } else { - DBG("Modprobe removal successful %s", - modules[i].name); - } - } -} - -/* - * Remove control kernel module(s) in reverse load order. - */ -void modprobe_remove_lttng_control(void) -{ - modprobe_remove_lttng(kern_modules_control_core, - ARRAY_SIZE(kern_modules_control_core), - LTTNG_MOD_REQUIRED); -} - -static void free_probes(void) -{ - int i; - - if (!probes) { - return; - } - for (i = 0; i < nr_probes; ++i) { - free(probes[i].name); - } - free(probes); - probes = NULL; - nr_probes = 0; -} - -/* - * Remove data kernel modules in reverse load order. - */ -void modprobe_remove_lttng_data(void) -{ - if (!probes) { - return; - } - modprobe_remove_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL); - free_probes(); -} - -/* - * Remove all kernel modules in reverse order. - */ -void modprobe_remove_lttng_all(void) -{ - modprobe_remove_lttng_data(); - modprobe_remove_lttng_control(); -} - #if HAVE_KMOD #include + static void log_kmod(void *data, int priority, const char *file, int line, const char *fn, const char *format, va_list args) { @@ -179,21 +105,39 @@ static void log_kmod(void *data, int priority, const char *file, int line, DBG("libkmod: %s", str); free(str); } -static int modprobe_lttng(struct kern_modules_param *modules, - int entries, int required) + +static int setup_kmod_ctx(struct kmod_ctx **ctx) { - int ret = 0, i; - struct kmod_ctx *ctx; + int ret = 0; - ctx = kmod_new(NULL, NULL); + *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); + kmod_set_log_fn(*ctx, log_kmod, NULL); + ret = kmod_load_resources(*ctx); + if (ret < 0) { + ERR("Failed to load kmod library resources"); + goto error; + } + +error: + return ret; +} + +static int modprobe_lttng(struct kern_modules_param *modules, + int entries, int required) +{ + int ret = 0, i; + struct kmod_ctx *ctx; + + ret = setup_kmod_ctx(&ctx); + if (ret < 0) { + goto error; + } for (i = 0; i < entries; i++) { struct kmod_module *mod = NULL; @@ -230,6 +174,73 @@ error: return ret; } +static int rmmod_recurse(struct kmod_module *mod) { + int ret = 0; + struct kmod_list *deps, *itr; + + if (kmod_module_get_initstate(mod) == KMOD_MODULE_BUILTIN) { + DBG("Module %s is builtin", kmod_module_get_name(mod)); + return ret; + } + + ret = kmod_module_remove_module(mod, 0); + + deps = kmod_module_get_dependencies(mod); + if (deps != NULL) { + kmod_list_foreach(itr, deps) { + struct kmod_module *dep = kmod_module_get_module(itr); + if (kmod_module_get_refcnt(dep) == 0) { + DBG("Recursive remove module %s", + kmod_module_get_name(dep)); + rmmod_recurse(dep); + } + kmod_module_unref(dep); + } + kmod_module_unref_list(deps); + } + + return ret; +} + +static void modprobe_remove_lttng(const struct kern_modules_param *modules, + int entries, int required) +{ + int ret = 0, i; + struct kmod_ctx *ctx; + + ret = setup_kmod_ctx(&ctx); + if (ret < 0) { + goto error; + } + + for (i = entries - 1; i >= 0; 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 = rmmod_recurse(mod); + if (ret == -EEXIST) { + DBG("Module %s is not in kernel.", modules[i].name); + } else if (required && ret < 0) { + ERR("Unable to remove module %s", modules[i].name); + } else { + DBG("Modprobe removal successful %s", + modules[i].name); + } + + kmod_module_unref(mod); + } + +error: + if (ctx) { + kmod_unref(ctx); + } +} + #else /* HAVE_KMOD */ static int modprobe_lttng(struct kern_modules_param *modules, @@ -278,8 +289,83 @@ error: return ret; } +static void modprobe_remove_lttng(const struct kern_modules_param *modules, + int entries, int required) +{ + int ret = 0, i; + char modprobe[256]; + + for (i = entries - 1; i >= 0; i--) { + ret = snprintf(modprobe, sizeof(modprobe), + "/sbin/modprobe -r -q %s", + modules[i].name); + if (ret < 0) { + PERROR("snprintf modprobe -r"); + return; + } + modprobe[sizeof(modprobe) - 1] = '\0'; + ret = system(modprobe); + if (ret == -1) { + ERR("Unable to launch modprobe -r for module %s", + modules[i].name); + } else if (required && WEXITSTATUS(ret) != 0) { + ERR("Unable to remove module %s", + modules[i].name); + } else { + DBG("Modprobe removal successful %s", + modules[i].name); + } + } +} + #endif /* HAVE_KMOD */ +/* + * Remove control kernel module(s) in reverse load order. + */ +void modprobe_remove_lttng_control(void) +{ + modprobe_remove_lttng(kern_modules_control_core, + ARRAY_SIZE(kern_modules_control_core), + LTTNG_MOD_REQUIRED); +} + +static void free_probes(void) +{ + int i; + + if (!probes) { + return; + } + for (i = 0; i < nr_probes; ++i) { + free(probes[i].name); + } + free(probes); + probes = NULL; + nr_probes = 0; +} + +/* + * Remove data kernel modules in reverse load order. + */ +void modprobe_remove_lttng_data(void) +{ + if (!probes) { + return; + } + modprobe_remove_lttng(probes, nr_probes, LTTNG_MOD_OPTIONAL); + free_probes(); +} + +/* + * Remove all kernel modules in reverse order. + */ +void modprobe_remove_lttng_all(void) +{ + modprobe_remove_lttng_data(); + modprobe_remove_lttng_control(); +} + /* * Load control kernel module(s). */