Skip to content

Commit

Permalink
tools/insmod: add syslog and verbose options
Browse files Browse the repository at this point in the history
Add the extra options for consistency with the rest of kmod.

Document all options in the man page.

Signed-off-by: Emil Velikov <[email protected]>
Link: #138
Signed-off-by: Lucas De Marchi <[email protected]>
  • Loading branch information
evelikov authored and lucasdemarchi committed Sep 21, 2024
1 parent b6b04af commit ca8f04e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 13 deletions.
21 changes: 20 additions & 1 deletion man/insmod.8.scd
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down
48 changes: 36 additions & 12 deletions tools/insmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@

#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)
{
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);
Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -84,24 +98,28 @@ 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++) {
size_t len = strlen(argv[i]);
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) {
Expand All @@ -116,27 +134,33 @@ 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;
}

err = kmod_module_insert_module(mod, flags, opts);
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;
}

Expand Down

0 comments on commit ca8f04e

Please sign in to comment.