<commit_before>/*
 * kmod-modprob - manage linux kernel modules using libkmod.
 *
 * Copyright (C) 2011  ProFUSION embedded systems
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <unistd.h>
#include <syslog.h>
#include <limits.h>

#include "libkmod.h"

static int log_priority = LOG_CRIT;
static int use_syslog = 0;

#define DEFAULT_VERBOSE LOG_WARNING
static int verbose = DEFAULT_VERBOSE;
static int do_show = 0;
static int dry_run = 0;
static int ignore_loaded = 0;
static int lookup_only = 0;
static int first_time = 0;
static int ignore_commands = 0;
static int use_blacklist = 0;
static int force = 0;
static int strip_modversion = 0;
static int strip_vermagic = 0;
static int remove_dependencies = 0;
static int quiet_inuse = 0;

static const char cmdopts_s[] = "arRibft:DcnC:d:S:sqvVh";
static const struct option cmdopts[] = {
	{"all", no_argument, 0, 'a'},
	{"remove", no_argument, 0, 'r'},
	{"remove-dependencies", no_argument, 0, 5},
	{"resolve-alias", no_argument, 0, 'R'},
	{"first-time", no_argument, 0, 3},
	{"ignore-install", no_argument, 0, 'i'},
	{"ignore-remove", no_argument, 0, 'i'},
	{"use-blacklist", no_argument, 0, 'b'},
	{"force", no_argument, 0, 'f'},
	{"force-modversion", no_argument, 0, 2},
	{"force-vermagic", no_argument, 0, 1},

	{"type", required_argument, 0, 't'},
	{"show-depends", no_argument, 0, 'D'},
	{"showconfig", no_argument, 0, 'c'},
	{"show-config", no_argument, 0, 'c'},
	{"show-modversions", no_argument, 0, 4},
	{"dump-modversions", no_argument, 0, 4},

	{"dry-run", no_argument, 0, 'n'},
	{"show", no_argument, 0, 'n'},

	{"config", required_argument, 0, 'C'},
	{"dirname", required_argument, 0, 'd'},
	{"set-version", required_argument, 0, 'S'},

	{"syslog", no_argument, 0, 's'},
	{"quiet", no_argument, 0, 'q'},
	{"verbose", no_argument, 0, 'v'},
	{"version", no_argument, 0, 'V'},
	{"help", no_argument, 0, 'h'},
	{NULL, 0, 0, 0}
};

static void help(const char *progname)
{
	fprintf(stderr,
		"Usage:\n"
		"\t%s [options] [-i] [-b] modulename\n"
		"\t%s [options] -a [-i] [-b] modulename [modulename...]\n"
		"\t%s [options] -r [-i] modulename\n"
		"\t%s [options] -r -a [-i] modulename [modulename...]\n"
		"\t%s [options] -l [-t dirname] [wildcard]\n"
		"\t%s [options] -c\n"
		"\t%s [options] --dump-modversions filename\n"
		"Management Options:\n"
		"\t-a, --all                   Consider every non-argument to\n"
		"\t                            be a module name to be inserted\n"
		"\t                            or removed (-r)\n"
		"\t-r, --remove                Remove modules instead of inserting\n"
		"\t    --remove-dependencies   Also remove modules depending on it\n"
		"\t-R, --resolve-alias         Only lookup and print alias and exit\n"
		"\t    --first-time            Fail if module already inserted or removed\n"
		"\t-i, --ignore-install        Ignore install commands\n"
		"\t-i, --ignore-remove         Ignore remove commands\n"
		"\t-b, --use-blacklist         Apply blacklist to resolved alias.\n"
		"\t-f, --force                 Force module insertion or removal.\n"
		"\t                            implies --force-modversions and\n"
		"\t                            --force-vermagic\n"
		"\t    --force-modversion      Ignore module's version\n"
		"\t    --force-vermagic        Ignore module's version magic\n"
		"\n"
		"Query Options:\n"
		"\t-t, --type=DIR              Limit type used by --list\n"
		"\t-D, --show-depends          Only print module dependencies and exit\n"
		"\t-c, --showconfig            Print out known configuration and exit\n"
		"\t-c, --show-config           Same as --showconfig\n"
		"\t    --show-modversions      Dump module symbol version and exit\n"
		"\t    --dump-modversions      Same as --show-modversions\n"
		"\n"
		"General Options:\n"
		"\t-n, --dry-run               Do not execute operations, just print out\n"
		"\t-n, --show                  Same as --dry-run\n"

		"\t-C, --config=FILE           Use FILE instead of default search paths\n"
		"\t-d, --dirname=DIR           Use DIR as filesystem root for " ROOTPREFIX "/lib/modules\n"
		"\t-S, --set-version=VERSION   Use VERSION instead of `uname -r`\n"

		"\t-s, --syslog                print to syslog, not stderr\n"
		"\t-q, --quiet                 disable messages\n"
		"\t-v, --verbose               enables more messages\n"
		"\t-V, --version               show version\n"
		"\t-h, --help                  show this help\n",
		progname, progname, progname, progname, progname, progname,
		progname);
}

static inline void _show(const char *fmt, ...)
{
	va_list args;

	if (!do_show && verbose <= DEFAULT_VERBOSE)
		return;

	va_start(args, fmt);
	vfprintf(stdout, fmt, args);
	fflush(stdout);
	va_end(args);
}

static inline void _log(int prio, const char *fmt, ...)
{
	const char *prioname;
	char buf[32], *msg;
	va_list args;

	if (prio > verbose)
		return;

	va_start(args, fmt);
	if (vasprintf(&msg, fmt, args) < 0)
		msg = NULL;
	va_end(args);
	if (msg == NULL)
		return;

	switch (prio) {
	case LOG_CRIT:
		prioname = "FATAL";
		break;
	case LOG_ERR:
		prioname = "ERROR";
		break;
	case LOG_WARNING:
		prioname = "WARNING";
		break;
	case LOG_NOTICE:
		prioname = "NOTICE";
		break;
	case LOG_INFO:
		prioname = "INFO";
		break;
	case LOG_DEBUG:
		prioname = "DEBUG";
		break;
	default:
		snprintf(buf, sizeof(buf), "LOG-%03d", prio);
		prioname = buf;
	}

	if (use_syslog)
		syslog(LOG_NOTICE, "%s: %s", prioname, msg);
	else
		fprintf(stderr, "%s: %s", prioname, msg);
	free(msg);

	if (prio <= LOG_CRIT)
		exit(EXIT_FAILURE);
}
#define ERR(...) _log(LOG_ERR, __VA_ARGS__)
#define WRN(...) _log(LOG_WARNING, __VA_ARGS__)
#define INF(...) _log(LOG_INFO, __VA_ARGS__)
#define DBG(...) _log(LOG_DEBUG, __VA_ARGS__)
#define LOG(...) _log(log_priority, __VA_ARGS__)
#define SHOW(...) _show(__VA_ARGS__)

static int show_config(struct kmod_ctx *ctx)
{
	ERR("TODO - config is missing in kmod.\n");
	/*
	  needs:
	    struct kmod_list *kmod_get_config(struct kmod_ctx *ctx);
	    kmod_config_get_type() {alias,options,blacklist,install,remove,softdeps}
	    kmod_config_get_key()
	    kmod_config_get_value()
	    kmod_config_unref_list()
	*/
	return -ENOENT;
}

static int show_modversions(struct kmod_ctx *ctx, const char *filename)
{
	struct kmod_list *l, *list = NULL;
	struct kmod_module *mod;
	int err = kmod_module_new_from_path(ctx, filename, &mod);
	if (err < 0) {
		LOG("Module %s not found.\n", filename);
		return err;
	}

	err = kmod_module_get_versions(mod, &list);
	if (err < 0) {
		LOG("could not get modversions of %s: %s\n",
			filename, strerror(-err));
		kmod_module_unref(mod);
		return err;
	}

	kmod_list_foreach(l, list) {
		const char *symbol = kmod_module_version_get_symbol(l);
		uint64_t crc = kmod_module_version_get_crc(l);
		printf("0x%08"PRIx64"\t%s\n", crc, symbol);
	}
	kmod_module_versions_free_list(list);
	kmod_module_unref(mod);
	return 0;
}

static int command_do(struct kmod_module *module, const char *type, const char *command, const char *cmdline_opts)
{
	const char *modname = kmod_module_get_name(module);
	char *p, *cmd = NULL;
	size_t cmdlen, cmdline_opts_len, varlen;
	int ret = 0;

	if (cmdline_opts == NULL)
		cmdline_opts = "";
	cmdline_opts_len = strlen(cmdline_opts);

	cmd = strdup(command);
	if (cmd == NULL)
		return -ENOMEM;
	cmdlen = strlen(cmd);
	varlen = sizeof("$CMDLINE_OPTS") - 1;
	while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
		size_t prefixlen = p - cmd;
		size_t suffixlen = cmdlen - prefixlen - varlen;
		size_t slen = cmdlen - varlen + cmdline_opts_len;
		char *suffix = p + varlen;
		char *s = malloc(slen + 1);
		if (s == NULL) {
			free(cmd);
			return -ENOMEM;
		}
		memcpy(s, cmd, p - cmd);
		memcpy(s + prefixlen, cmdline_opts, cmdline_opts_len);
		memcpy(s + prefixlen + cmdline_opts_len, suffix, suffixlen);
		s[slen] = '\0';

		free(cmd);
		cmd = s;
		cmdlen = slen;
	}

	SHOW("%s %s\n", type, cmd);
	if (dry_run)
		goto end;

	setenv("MODPROBE_MODULE", modname, 1);
	ret = system(cmd);
	unsetenv("MODPROBE_MODULE");
	if (ret == -1 || WEXITSTATUS(ret)) {
		LOG("Error running %s command for %s\n", type, modname);
		if (ret != -1)
			ret = -WEXITSTATUS(ret);
	}

end:
	free(cmd);
	return ret;
}

static int rmmod_do_dependencies(struct kmod_module *parent);
static int rmmod_do_soft_dependencies(struct kmod_module *mod, struct kmod_list *deps);

static int rmmod_do_deps_list(struct kmod_module *parent, struct kmod_list *deps, unsigned stop_on_errors)
{
	struct kmod_list *d;
	int err = 0;

	kmod_list_foreach_reverse(d, deps) {
		struct kmod_module *dm = kmod_module_get_module(d);
		struct kmod_list *pre = NULL, *post = NULL;
		const char *cmd, *dmname = kmod_module_get_name(dm);
		int r;

		r = rmmod_do_dependencies(dm);
		if (r < 0) {
			WRN("could not remove dependencies of '%s': %s\n",
			    dmname, strerror(-r));
			goto dep_error;
		}

		r = kmod_module_get_softdeps(dm, &pre, &post);
		if (r < 0) {
			WRN("could not get softdeps of '%s': %s\n",
			    dmname, strerror(-r));
			goto dep_done;
		}

		r = rmmod_do_soft_dependencies(dm, post);
		if (r < 0) {
			WRN("could not remove post soft softdeps of '%s': %s\n",
			    dmname, strerror(-r));
			goto dep_error;
		}

		if (!ignore_loaded) {
			int state = kmod_module_get_initstate(dm);
			if (state != KMOD_MODULE_LIVE &&
					state != KMOD_MODULE_COMING)
				goto dep_done;
		}

		cmd = kmod_module_get_remove_commands(dm);
		if (cmd) {
			r = command_do(dm, "remove", cmd, NULL);
			if (r < 0) {
				WRN("failed to execute remove command of '%s': "
				    "%s\n", dmname, strerror(-r));
				goto dep_error;
			} else
				goto dep_done;
		}

		r = kmod_module_get_refcnt(dm);
		if (r < 0) {
			WRN("could not get module '%s' refcnt: %s\n",
			    dmname, strerror(-r));
			goto dep_error;
		} else if (r > 0 && !ignore_loaded) {
			LOG("Module %s is in use.\n", dmname);
			r = -EBUSY;
			goto dep_error;
		}

		SHOW("rmmod %s\n", dmname);

		if (!dry_run) {
			r = kmod_module_remove_module(dm, 0);
			if (r < 0) {
				WRN("could not remove '%s': %s\n",
				    dmname, strerror(-r));
				goto dep_error;
			}
		}

	dep_done:
		r = rmmod_do_soft_dependencies(dm, pre);
		if (r < 0) {
			WRN("could not remove pre softdeps of '%s': %s\n",
			    dmname, strerror(-r));
			goto dep_error;
		}
		kmod_module_unref_list(pre);
		kmod_module_unref_list(post);
		kmod_module_unref(dm);
		continue;

	dep_error:
		err = r;
		kmod_module_unref_list(pre);
		kmod_module_unref_list(post);
		kmod_module_unref(dm);
		if (stop_on_errors)
			break;
		else
			continue;
	}

	return err;
}

static int rmmod_do_soft_dependencies(struct kmod_module *mod, struct kmod_list *deps)
{
	return rmmod_do_deps_list(mod, deps, 0);
}

static int rmmod_do_dependencies(struct kmod_module *parent)
{
	struct kmod_list *deps = kmod_module_get_holders(parent);
	int err = rmmod_do_deps_list(parent, deps, 1);
	kmod_module_unref_list(deps);
	return err;
}

static int rmmod_do(struct kmod_module *mod)
{
	const char *modname = kmod_module_get_name(mod);
	struct kmod_list *pre = NULL, *post = NULL, *deps;
	int err;

	if (!ignore_commands) {
		const char *cmd;

		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;
		}

		err = rmmod_do_soft_dependencies(mod, post);
		if (err < 0) {
			WRN("could not remove post softdeps of '%s': %s\n",
			    modname, strerror(-err));
			goto error;
		}

		cmd = kmod_module_get_remove_commands(mod);
		if (cmd != NULL) {
			err = command_do(mod, "remove", cmd, NULL);
			goto done;
		}
	}

	if (!ignore_loaded) {
		int state = kmod_module_get_initstate(mod);

		if (state < 0) {
			LOG ("Module %s not found.\n", modname);
			return -ENOENT;
		} else if (state == KMOD_MODULE_BUILTIN) {
			LOG("Module %s is builtin.\n", modname);
			return -ENOENT;
		} else if (state != KMOD_MODULE_LIVE) {
			if (first_time) {
				LOG("Module %s is not in kernel.\n", modname);
				return -ENOENT;
			} else
				return 0;
		}
	}

	/* not in original modprobe -r, but helpful */
	if (remove_dependencies) {
		err = rmmod_do_dependencies(mod);
		if (err < 0)
			return err;
	}

	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 done_deps;
		}
	}

	SHOW("rmmod %s\n", modname);

	if (dry_run)
		err = 0;
	else {
		int flags = 0;

		if (force)
			flags |= KMOD_REMOVE_FORCE;

		err = kmod_module_remove_module(mod, flags);
		if (err == -EEXIST) {
			if (!first_time)
				err = 0;
			else
				LOG("Module %s is not in kernel.\n", modname);
		}
	}

done:
	if (!ignore_commands) {
		err = rmmod_do_soft_dependencies(mod, pre);
		if (err < 0) {
			WRN("could not remove pre softdeps of '%s': %s\n",
			    modname, strerror(-err));
			goto error;
		}
	}

done_deps:
	deps = kmod_module_get_dependencies(mod);
	if (deps != NULL) {
		struct kmod_list *itr;

		first_time = 0;
		ignore_commands = 0;
		quiet_inuse = 1;

		kmod_list_foreach(itr, deps) {
			struct kmod_module *dep = kmod_module_get_module(itr);

			if (kmod_module_get_refcnt(dep) == 0)
				rmmod_do(dep);

			kmod_module_unref(dep);
		}
		kmod_module_unref_list(deps);
	}

error:
	kmod_module_unref_list(pre);
	kmod_module_unref_list(post);
	return err;
}

static int rmmod_path(struct kmod_ctx *ctx, const char *path)
{
	struct kmod_module *mod;
	int err;

	err = kmod_module_new_from_path(ctx, path, &mod);
	if (err < 0) {
		LOG("Module %s not found.\n", path);
		return err;
	}
	err = rmmod_do(mod);
	kmod_module_unref(mod);
	return err;
}

static int rmmod_alias(struct kmod_ctx *ctx, const char *alias)
{
	struct kmod_list *l, *list = NULL;
	int err;

	err = kmod_module_new_from_lookup(ctx, alias, &list);
	if (err < 0)
		return err;

	if (list == NULL)
		LOG("Module %s not found.\n", alias);

	kmod_list_foreach(l, list) {
		struct kmod_module *mod = kmod_module_get_module(l);
		err = rmmod_do(mod);
		kmod_module_unref(mod);
		if (err < 0)
			break;
	}

	kmod_module_unref_list(list);
	return err;
}

static int rmmod(struct kmod_ctx *ctx, const char *name)
{
	if (access(name, F_OK) == 0)
		return rmmod_path(ctx, name);
	else
		return rmmod_alias(ctx, name);
}

static int rmmod_all(struct kmod_ctx *ctx, char **args, int nargs)
{
	int i, err = 0;

	for (i = 0; i < nargs; i++) {
		int r = rmmod(ctx, args[i]);
		if (r < 0)
			err = r;
	}

	return err;
}

static int insmod_do_dependencies(struct kmod_module *parent);
static int insmod_do_soft_dependencies(struct kmod_module *mod, struct kmod_list *deps);

static int insmod_do_deps_list(struct kmod_module *parent, struct kmod_list *deps, unsigned stop_on_errors)
{
	struct kmod_list *d;
	int err = 0;

	kmod_list_foreach(d, deps) {
		struct kmod_module *dm = kmod_module_get_module(d);
		struct kmod_list *pre = NULL, *post = NULL;
		const char *cmd, *opts, *dmname = kmod_module_get_name(dm);
		int r;

		r = insmod_do_dependencies(dm);
		if (r < 0) {
			WRN("could not insert dependencies of '%s': %s\n",
			    dmname, strerror(-r));
			goto dep_error;
		}

		r = kmod_module_get_softdeps(dm, &pre, &post);
		if (r < 0) {
			WRN("could not get softdeps of '%s': %s\n",
			    dmname, strerror(-r));
			goto dep_done;
		}

		r = insmod_do_soft_dependencies(dm, pre);
		if (r < 0) {
			WRN("could not insert pre softdeps of '%s': %s\n",
			    dmname, strerror(-r));
			goto dep_error;
		}

		if (!ignore_loaded) {
			int state = kmod_module_get_initstate(dm);
			if (state == KMOD_MODULE_LIVE ||
					state == KMOD_MODULE_COMING ||
					state == KMOD_MODULE_BUILTIN)
				goto dep_done;
		}

		cmd = kmod_module_get_install_commands(dm);
		if (cmd) {
			r = command_do(dm, "install", cmd, NULL);
			if (r < 0) {
				WRN("failed to execute install command of '%s':"
				    " %s\n", dmname, strerror(-r));
				goto dep_error;
			} else
				goto dep_done;
		}

		opts = kmod_module_get_options(dm);
		SHOW("insmod %s %s\n",
			kmod_module_get_path(dm), opts ? opts : "");

		if (!dry_run) {
			int flags = 0;

			if (strip_modversion || force)
				flags |= KMOD_INSERT_FORCE_MODVERSION;
			if (strip_vermagic || force)
				flags |= KMOD_INSERT_FORCE_VERMAGIC;

			r = kmod_module_insert_module(dm, flags, opts);
			if (r < 0) {
				WRN("could not insert '%s': %s\n",
				    dmname, strerror(-r));
				goto dep_error;
			}
		}

	dep_done:
		r = insmod_do_soft_dependencies(dm, post);
		if (r < 0) {
			WRN("could not insert post softdeps of '%s': %s\n",
			    dmname, strerror(-r));
			goto dep_error;
		}

		kmod_module_unref_list(pre);
		kmod_module_unref_list(post);
		kmod_module_unref(dm);
		continue;

	dep_error:
		err = r;
		kmod_module_unref_list(pre);
		kmod_module_unref_list(post);
		kmod_module_unref(dm);
		if (stop_on_errors)
			break;<commit_msg>modprobe: fix error path when loading dependencies<commit_after>/*
 * kmod-modprob - manage linux kernel modules using libkmod.
 *
 * Copyright (C) 2011  ProFUSION embedded systems
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <unistd.h>
#include <syslog.h>
#include <limits.h>

#include "libkmod.h"

static int log_priority = LOG_CRIT;
static int use_syslog = 0;

#define DEFAULT_VERBOSE LOG_WARNING
static int verbose = DEFAULT_VERBOSE;
static int do_show = 0;
static int dry_run = 0;
static int ignore_loaded = 0;
static int lookup_only = 0;
static int first_time = 0;
static int ignore_commands = 0;
static int use_blacklist = 0;
static int force = 0;
static int strip_modversion = 0;
static int strip_vermagic = 0;
static int remove_dependencies = 0;
static int quiet_inuse = 0;

static const char cmdopts_s[] = "arRibft:DcnC:d:S:sqvVh";
static const struct option cmdopts[] = {
	{"all", no_argument, 0, 'a'},
	{"remove", no_argument, 0, 'r'},
	{"remove-dependencies", no_argument, 0, 5},
	{"resolve-alias", no_argument, 0, 'R'},
	{"first-time", no_argument, 0, 3},
	{"ignore-install", no_argument, 0, 'i'},
	{"ignore-remove", no_argument, 0, 'i'},
	{"use-blacklist", no_argument, 0, 'b'},
	{"force", no_argument, 0, 'f'},
	{"force-modversion", no_argument, 0, 2},
	{"force-vermagic", no_argument, 0, 1},

	{"type", required_argument, 0, 't'},
	{"show-depends", no_argument, 0, 'D'},
	{"showconfig", no_argument, 0, 'c'},
	{"show-config", no_argument, 0, 'c'},
	{"show-modversions", no_argument, 0, 4},
	{"dump-modversions", no_argument, 0, 4},

	{"dry-run", no_argument, 0, 'n'},
	{"show", no_argument, 0, 'n'},

	{"config", required_argument, 0, 'C'},
	{"dirname", required_argument, 0, 'd'},
	{"set-version", required_argument, 0, 'S'},

	{"syslog", no_argument, 0, 's'},
	{"quiet", no_argument, 0, 'q'},
	{"verbose", no_argument, 0, 'v'},
	{"version", no_argument, 0, 'V'},
	{"help", no_argument, 0, 'h'},
	{NULL, 0, 0, 0}
};

static void help(const char *progname)
{
	fprintf(stderr,
		"Usage:\n"
		"\t%s [options] [-i] [-b] modulename\n"
		"\t%s [options] -a [-i] [-b] modulename [modulename...]\n"
		"\t%s [options] -r [-i] modulename\n"
		"\t%s [options] -r -a [-i] modulename [modulename...]\n"
		"\t%s [options] -l [-t dirname] [wildcard]\n"
		"\t%s [options] -c\n"
		"\t%s [options] --dump-modversions filename\n"
		"Management Options:\n"
		"\t-a, --all                   Consider every non-argument to\n"
		"\t                            be a module name to be inserted\n"
		"\t                            or removed (-r)\n"
		"\t-r, --remove                Remove modules instead of inserting\n"
		"\t    --remove-dependencies   Also remove modules depending on it\n"
		"\t-R, --resolve-alias         Only lookup and print alias and exit\n"
		"\t    --first-time            Fail if module already inserted or removed\n"
		"\t-i, --ignore-install        Ignore install commands\n"
		"\t-i, --ignore-remove         Ignore remove commands\n"
		"\t-b, --use-blacklist         Apply blacklist to resolved alias.\n"
		"\t-f, --force                 Force module insertion or removal.\n"
		"\t                            implies --force-modversions and\n"
		"\t                            --force-vermagic\n"
		"\t    --force-modversion      Ignore module's version\n"
		"\t    --force-vermagic        Ignore module's version magic\n"
		"\n"
		"Query Options:\n"
		"\t-t, --type=DIR              Limit type used by --list\n"
		"\t-D, --show-depends          Only print module dependencies and exit\n"
		"\t-c, --showconfig            Print out known configuration and exit\n"
		"\t-c, --show-config           Same as --showconfig\n"
		"\t    --show-modversions      Dump module symbol version and exit\n"
		"\t    --dump-modversions      Same as --show-modversions\n"
		"\n"
		"General Options:\n"
		"\t-n, --dry-run               Do not execute operations, just print out\n"
		"\t-n, --show                  Same as --dry-run\n"

		"\t-C, --config=FILE           Use FILE instead of default search paths\n"
		"\t-d, --dirname=DIR           Use DIR as filesystem root for " ROOTPREFIX "/lib/modules\n"
		"\t-S, --set-version=VERSION   Use VERSION instead of `uname -r`\n"

		"\t-s, --syslog                print to syslog, not stderr\n"
		"\t-q, --quiet                 disable messages\n"
		"\t-v, --verbose               enables more messages\n"
		"\t-V, --version               show version\n"
		"\t-h, --help                  show this help\n",
		progname, progname, progname, progname, progname, progname,
		progname);
}

static inline void _show(const char *fmt, ...)
{
	va_list args;

	if (!do_show && verbose <= DEFAULT_VERBOSE)
		return;

	va_start(args, fmt);
	vfprintf(stdout, fmt, args);
	fflush(stdout);
	va_end(args);
}

static inline void _log(int prio, const char *fmt, ...)
{
	const char *prioname;
	char buf[32], *msg;
	va_list args;

	if (prio > verbose)
		return;

	va_start(args, fmt);
	if (vasprintf(&msg, fmt, args) < 0)
		msg = NULL;
	va_end(args);
	if (msg == NULL)
		return;

	switch (prio) {
	case LOG_CRIT:
		prioname = "FATAL";
		break;
	case LOG_ERR:
		prioname = "ERROR";
		break;
	case LOG_WARNING:
		prioname = "WARNING";
		break;
	case LOG_NOTICE:
		prioname = "NOTICE";
		break;
	case LOG_INFO:
		prioname = "INFO";
		break;
	case LOG_DEBUG:
		prioname = "DEBUG";
		break;
	default:
		snprintf(buf, sizeof(buf), "LOG-%03d", prio);
		prioname = buf;
	}

	if (use_syslog)
		syslog(LOG_NOTICE, "%s: %s", prioname, msg);
	else
		fprintf(stderr, "%s: %s", prioname, msg);
	free(msg);

	if (prio <= LOG_CRIT)
		exit(EXIT_FAILURE);
}
#define ERR(...) _log(LOG_ERR, __VA_ARGS__)
#define WRN(...) _log(LOG_WARNING, __VA_ARGS__)
#define INF(...) _log(LOG_INFO, __VA_ARGS__)
#define DBG(...) _log(LOG_DEBUG, __VA_ARGS__)
#define LOG(...) _log(log_priority, __VA_ARGS__)
#define SHOW(...) _show(__VA_ARGS__)

static int show_config(struct kmod_ctx *ctx)
{
	ERR("TODO - config is missing in kmod.\n");
	/*
	  needs:
	    struct kmod_list *kmod_get_config(struct kmod_ctx *ctx);
	    kmod_config_get_type() {alias,options,blacklist,install,remove,softdeps}
	    kmod_config_get_key()
	    kmod_config_get_value()
	    kmod_config_unref_list()
	*/
	return -ENOENT;
}

static int show_modversions(struct kmod_ctx *ctx, const char *filename)
{
	struct kmod_list *l, *list = NULL;
	struct kmod_module *mod;
	int err = kmod_module_new_from_path(ctx, filename, &mod);
	if (err < 0) {
		LOG("Module %s not found.\n", filename);
		return err;
	}

	err = kmod_module_get_versions(mod, &list);
	if (err < 0) {
		LOG("could not get modversions of %s: %s\n",
			filename, strerror(-err));
		kmod_module_unref(mod);
		return err;
	}

	kmod_list_foreach(l, list) {
		const char *symbol = kmod_module_version_get_symbol(l);
		uint64_t crc = kmod_module_version_get_crc(l);
		printf("0x%08"PRIx64"\t%s\n", crc, symbol);
	}
	kmod_module_versions_free_list(list);
	kmod_module_unref(mod);
	return 0;
}

static int command_do(struct kmod_module *module, const char *type, const char *command, const char *cmdline_opts)
{
	const char *modname = kmod_module_get_name(module);
	char *p, *cmd = NULL;
	size_t cmdlen, cmdline_opts_len, varlen;
	int ret = 0;

	if (cmdline_opts == NULL)
		cmdline_opts = "";
	cmdline_opts_len = strlen(cmdline_opts);

	cmd = strdup(command);
	if (cmd == NULL)
		return -ENOMEM;
	cmdlen = strlen(cmd);
	varlen = sizeof("$CMDLINE_OPTS") - 1;
	while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
		size_t prefixlen = p - cmd;
		size_t suffixlen = cmdlen - prefixlen - varlen;
		size_t slen = cmdlen - varlen + cmdline_opts_len;
		char *suffix = p + varlen;
		char *s = malloc(slen + 1);
		if (s == NULL) {
			free(cmd);
			return -ENOMEM;
		}
		memcpy(s, cmd, p - cmd);
		memcpy(s + prefixlen, cmdline_opts, cmdline_opts_len);
		memcpy(s + prefixlen + cmdline_opts_len, suffix, suffixlen);
		s[slen] = '\0';

		free(cmd);
		cmd = s;
		cmdlen = slen;
	}

	SHOW("%s %s\n", type, cmd);
	if (dry_run)
		goto end;

	setenv("MODPROBE_MODULE", modname, 1);
	ret = system(cmd);
	unsetenv("MODPROBE_MODULE");
	if (ret == -1 || WEXITSTATUS(ret)) {
		LOG("Error running %s command for %s\n", type, modname);
		if (ret != -1)
			ret = -WEXITSTATUS(ret);
	}

end:
	free(cmd);
	return ret;
}

static int rmmod_do_dependencies(struct kmod_module *parent);
static int rmmod_do_soft_dependencies(struct kmod_module *mod, struct kmod_list *deps);

static int rmmod_do_deps_list(struct kmod_module *parent, struct kmod_list *deps, unsigned stop_on_errors)
{
	struct kmod_list *d;
	int err = 0;

	kmod_list_foreach_reverse(d, deps) {
		struct kmod_module *dm = kmod_module_get_module(d);
		struct kmod_list *pre = NULL, *post = NULL;
		const char *cmd, *dmname = kmod_module_get_name(dm);
		int r;

		r = rmmod_do_dependencies(dm);
		if (r < 0) {
			WRN("could not remove dependencies of '%s': %s\n",
			    dmname, strerror(-r));
			goto dep_error;
		}

		r = kmod_module_get_softdeps(dm, &pre, &post);
		if (r < 0) {
			WRN("could not get softdeps of '%s': %s\n",
			    dmname, strerror(-r));
			goto dep_done;
		}

		r = rmmod_do_soft_dependencies(dm, post);
		if (r < 0) {
			WRN("could not remove post soft softdeps of '%s': %s\n",
			    dmname, strerror(-r));
			goto dep_error;
		}

		if (!ignore_loaded) {
			int state = kmod_module_get_initstate(dm);
			if (state != KMOD_MODULE_LIVE &&
					state != KMOD_MODULE_COMING)
				goto dep_done;
		}

		cmd = kmod_module_get_remove_commands(dm);
		if (cmd) {
			r = command_do(dm, "remove", cmd, NULL);
			if (r < 0) {
				WRN("failed to execute remove command of '%s': "
				    "%s\n", dmname, strerror(-r));
				goto dep_error;
			} else
				goto dep_done;
		}

		r = kmod_module_get_refcnt(dm);
		if (r < 0) {
			WRN("could not get module '%s' refcnt: %s\n",
			    dmname, strerror(-r));
			goto dep_error;
		} else if (r > 0 && !ignore_loaded) {
			LOG("Module %s is in use.\n", dmname);
			r = -EBUSY;
			goto dep_error;
		}

		SHOW("rmmod %s\n", dmname);

		if (!dry_run) {
			r = kmod_module_remove_module(dm, 0);
			if (r < 0) {
				WRN("could not remove '%s': %s\n",
				    dmname, strerror(-r));
				goto dep_error;
			}
		}

	dep_done:
		r = rmmod_do_soft_dependencies(dm, pre);
		if (r < 0) {
			WRN("could not remove pre softdeps of '%s': %s\n",
			    dmname, strerror(-r));
			goto dep_error;
		}
		kmod_module_unref_list(pre);
		kmod_module_unref_list(post);
		kmod_module_unref(dm);
		continue;

	dep_error:
		err = r;
		kmod_module_unref_list(pre);
		kmod_module_unref_list(post);
		kmod_module_unref(dm);
		if (stop_on_errors)
			break;
		else
			continue;
	}

	return err;
}

static int rmmod_do_soft_dependencies(struct kmod_module *mod, struct kmod_list *deps)
{
	return rmmod_do_deps_list(mod, deps, 0);
}

static int rmmod_do_dependencies(struct kmod_module *parent)
{
	struct kmod_list *deps = kmod_module_get_holders(parent);
	int err = rmmod_do_deps_list(parent, deps, 1);
	kmod_module_unref_list(deps);
	return err;
}

static int rmmod_do(struct kmod_module *mod)
{
	const char *modname = kmod_module_get_name(mod);
	struct kmod_list *pre = NULL, *post = NULL, *deps;
	int err;

	if (!ignore_commands) {
		const char *cmd;

		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;
		}

		err = rmmod_do_soft_dependencies(mod, post);
		if (err < 0) {
			WRN("could not remove post softdeps of '%s': %s\n",
			    modname, strerror(-err));
			goto error;
		}

		cmd = kmod_module_get_remove_commands(mod);
		if (cmd != NULL) {
			err = command_do(mod, "remove", cmd, NULL);
			goto done;
		}
	}

	if (!ignore_loaded) {
		int state = kmod_module_get_initstate(mod);

		if (state < 0) {
			LOG ("Module %s not found.\n", modname);
			return -ENOENT;
		} else if (state == KMOD_MODULE_BUILTIN) {
			LOG("Module %s is builtin.\n", modname);
			return -ENOENT;
		} else if (state != KMOD_MODULE_LIVE) {
			if (first_time) {
				LOG("Module %s is not in kernel.\n", modname);
				return -ENOENT;
			} else
				return 0;
		}
	}

	/* not in original modprobe -r, but helpful */
	if (remove_dependencies) {
		err = rmmod_do_dependencies(mod);
		if (err < 0)
			return err;
	}

	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 done_deps;
		}
	}

	SHOW("rmmod %s\n", modname);

	if (dry_run)
		err = 0;
	else {
		int flags = 0;

		if (force)
			flags |= KMOD_REMOVE_FORCE;

		err = kmod_module_remove_module(mod, flags);
		if (err == -EEXIST) {
			if (!first_time)
				err = 0;
			else
				LOG("Module %s is not in kernel.\n", modname);
		}
	}

done:
	if (!ignore_commands) {
		err = rmmod_do_soft_dependencies(mod, pre);
		if (err < 0) {
			WRN("could not remove pre softdeps of '%s': %s\n",
			    modname, strerror(-err));
			goto error;
		}
	}

done_deps:
	deps = kmod_module_get_dependencies(mod);
	if (deps != NULL) {
		struct kmod_list *itr;

		first_time = 0;
		ignore_commands = 0;
		quiet_inuse = 1;

		kmod_list_foreach(itr, deps) {
			struct kmod_module *dep = kmod_module_get_module(itr);

			if (kmod_module_get_refcnt(dep) == 0)
				rmmod_do(dep);

			kmod_module_unref(dep);
		}
		kmod_module_unref_list(deps);
	}

error:
	kmod_module_unref_list(pre);
	kmod_module_unref_list(post);
	return err;
}

static int rmmod_path(struct kmod_ctx *ctx, const char *path)
{
	struct kmod_module *mod;
	int err;

	err = kmod_module_new_from_path(ctx, path, &mod);
	if (err < 0) {
		LOG("Module %s not found.\n", path);
		return err;
	}
	err = rmmod_do(mod);
	kmod_module_unref(mod);
	return err;
}

static int rmmod_alias(struct kmod_ctx *ctx, const char *alias)
{
	struct kmod_list *l, *list = NULL;
	int err;

	err = kmod_module_new_from_lookup(ctx, alias, &list);
	if (err < 0)
		return err;

	if (list == NULL)
		LOG("Module %s not found.\n", alias);

	kmod_list_foreach(l, list) {
		struct kmod_module *mod = kmod_module_get_module(l);
		err = rmmod_do(mod);
		kmod_module_unref(mod);
		if (err < 0)
			break;
	}

	kmod_module_unref_list(list);
	return err;
}

static int rmmod(struct kmod_ctx *ctx, const char *name)
{
	if (access(name, F_OK) == 0)
		return rmmod_path(ctx, name);
	else
		return rmmod_alias(ctx, name);
}

static int rmmod_all(struct kmod_ctx *ctx, char **args, int nargs)
{
	int i, err = 0;

	for (i = 0; i < nargs; i++) {
		int r = rmmod(ctx, args[i]);
		if (r < 0)
			err = r;
	}

	return err;
}

static int insmod_do_dependencies(struct kmod_module *parent);
static int insmod_do_soft_dependencies(struct kmod_module *mod, struct kmod_list *deps);

static int insmod_do_deps_list(struct kmod_module *parent, struct kmod_list *deps, unsigned stop_on_errors)
{
	struct kmod_list *d;
	int err = 0;

	kmod_list_foreach(d, deps) {
		struct kmod_module *dm = kmod_module_get_module(d);
		struct kmod_list *pre = NULL, *post = NULL;
		const char *cmd, *opts, *dmname = kmod_module_get_name(dm);
		int r;

		r = insmod_do_dependencies(dm);
		if (r < 0) {
			WRN("could not insert dependencies of '%s': %s\n",
			    dmname, strerror(-r));
			goto dep_error;
		}

		r = kmod_module_get_softdeps(dm, &pre, &post);
		if (r < 0) {
			WRN("could not get softdeps of '%s': %s\n",
			    dmname, strerror(-r));
			goto dep_done;
		}

		r = insmod_do_soft_dependencies(dm, pre);
		if (r < 0) {
			WRN("could not insert pre softdeps of '%s': %s\n",
			    dmname, strerror(-r));
			goto dep_error;
		}

		if (!ignore_loaded) {
			int state = kmod_module_get_initstate(dm);
			if (state == KMOD_MODULE_LIVE ||
					state == KMOD_MODULE_COMING ||
					state == KMOD_MODULE_BUILTIN)
				goto dep_done;
		}

		cmd = kmod_module_get_install_commands(dm);
		if (cmd) {
			r = command_do(dm, "install", cmd, NULL);
			if (r < 0) {
				WRN("failed to execute install command of '%s':"
				    " %s\n", dmname, strerror(-r));
				goto dep_error;
			} else
				goto dep_done;
		}

		opts = kmod_module_get_options(dm);
		SHOW("insmod %s %s\n",
			kmod_module_get_path(dm), opts ? opts : "");

		if (!dry_run) {
			int flags = 0;

			if (strip_modversion || force)
				flags |= KMOD_INSERT_FORCE_MODVERSION;
			if (strip_vermagic || force)
				flags |= KMOD_INSERT_FORCE_VERMAGIC;

			r = kmod_module_insert_module(dm, flags, opts);
			if (r == -EEXIST && !first_time)
				r = 0;
			if (r < 0) {
				WRN("could not insert '%s': %s\n",
						dmname, strerror(-r));
				goto dep_error;
			}
		}

	dep_done:
		r = insmod_do_soft_dependencies(dm, post);
		if (r < 0) {
			WRN("could not insert post softdeps of '%s': %s\n",
			    dmname, strerror(-r));
			goto dep_error;
		}

		kmod_module_unref_list(pre);
		kmod_module_unref_list(post);
		kmod_module_unref(dm);
		continue;

	dep_error:
		err = r;
		kmod_module_unref_list(pre);
		kmod_module_unref_list(post);
		kmod_module_unref(dm);
		if (stop_on_errors)
			break;
{'fraction_non_alphanumeric': 0.14658897100625357, 'fraction_numerical': 0.006679931779420125, 'mean_word_length': 3.3040127232689014, 'pattern_counts': {'":': 0, '<': 83, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6921571', 'n_tokens_mistral': 15041, 'n_tokens_neox': 13587, 'n_words': 3559}