tools/insmod: add syslog and verbose options

Add the extra options for consistency with the rest of kmod.

Document all options in the man page.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/138
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
This commit is contained in:
Emil Velikov 2024-09-18 16:49:08 +01:00 committed by Lucas De Marchi
parent b6b04afb34
commit ca8f04e87a
2 changed files with 56 additions and 13 deletions

View File

@ -6,7 +6,7 @@ insmod - Simple program to insert a module into the Linux Kernel
# SYNOPSIS
*insmod* [_filename_] [_module options_...]
*insmod* [_OPTIONS_] [_filename_] [_module options_...]
# DESCRIPTION
@ -18,6 +18,25 @@ Only the most general of error messages are reported: as the work of trying to
link the module is now done inside the kernel, the *dmesg* usually gives more
information about errors.
# OPTIONS
*-s*
*--syslog*
Send errors to syslog instead of standard error.
*-v*
*--verbose*
Print messages about what the program is doing. Usually *insmod* prints
messages only if something goes wrong.
*-V*
*--version*
Show version of program and exit.
*-h*
*--help*
Print the help message and exit.
# COPYRIGHT
This manual page originally Copyright 2002, Rusty Russell, IBM Corporation.

View File

@ -15,11 +15,15 @@
#include "kmod.h"
static const char cmdopts_s[] = "fVh";
static const char cmdopts_s[] = "fsvVh";
static const struct option cmdopts[] = {
{"version", no_argument, 0, 'V'},
{"help", no_argument, 0, 'h'},
{NULL, 0, 0, 0},
// clang-format off
{ "syslog", no_argument, 0, 's' },
{ "verbose", no_argument, 0, 'v' },
{ "version", no_argument, 0, 'V' },
{ "help", no_argument, 0, 'h' },
{ NULL, 0, 0, 0 },
// clang-format on
};
static void help(void)
@ -27,6 +31,8 @@ static void help(void)
printf("Usage:\n"
"\t%s [options] filename [args]\n"
"Options:\n"
"\t-s, --syslog print to syslog, not stderr\n"
"\t-v, --verbose enables more messages\n"
"\t-V, --version show version\n"
"\t-h, --help show this help\n",
program_invocation_short_name);
@ -50,12 +56,14 @@ static const char *mod_strerror(int err)
static int do_insmod(int argc, char *argv[])
{
struct kmod_ctx *ctx;
struct kmod_ctx *ctx = NULL;
struct kmod_module *mod;
const char *filename;
char *opts = NULL;
size_t optslen = 0;
int i, err;
int verbose = LOG_ERR;
int use_syslog;
int i, err = 0, r = 0;
const char *null_config = NULL;
unsigned int flags = 0;
@ -69,6 +77,12 @@ static int do_insmod(int argc, char *argv[])
flags |= KMOD_PROBE_FORCE_MODVERSION;
flags |= KMOD_PROBE_FORCE_VERMAGIC;
break;
case 's':
use_syslog = 1;
break;
case 'v':
verbose++;
break;
case 'h':
help();
return EXIT_SUCCESS;
@ -84,15 +98,19 @@ static int do_insmod(int argc, char *argv[])
}
}
log_open(use_syslog);
if (optind >= argc) {
ERR("missing filename.\n");
return EXIT_FAILURE;
r = EXIT_FAILURE;
goto end;
}
filename = argv[optind];
if (streq(filename, "-")) {
ERR("this tool does not support loading from stdin!\n");
return EXIT_FAILURE;
r = EXIT_FAILURE;
goto end;
}
for (i = optind + 1; i < argc; i++) {
@ -100,8 +118,8 @@ static int do_insmod(int argc, char *argv[])
void *tmp = realloc(opts, optslen + len + 2);
if (tmp == NULL) {
ERR("out of memory\n");
free(opts);
return EXIT_FAILURE;
r = EXIT_FAILURE;
goto end;
}
opts = tmp;
if (optslen > 0) {
@ -116,14 +134,17 @@ static int do_insmod(int argc, char *argv[])
ctx = kmod_new(NULL, &null_config);
if (!ctx) {
ERR("kmod_new() failed!\n");
free(opts);
return EXIT_FAILURE;
r = EXIT_FAILURE;
goto end;
}
log_setup_kmod_log(ctx, verbose);
err = kmod_module_new_from_path(ctx, filename, &mod);
if (err < 0) {
ERR("could not load module %s: %s\n", filename,
strerror(-err));
r++;
goto end;
}
@ -131,12 +152,15 @@ static int do_insmod(int argc, char *argv[])
if (err < 0) {
ERR("could not insert module %s: %s\n", filename,
mod_strerror(-err));
r++;
}
kmod_module_unref(mod);
end:
kmod_unref(ctx);
free(opts);
log_close();
return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}