]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
tools/insmod: add syslog and verbose options
authorEmil Velikov <emil.l.velikov@gmail.com>
Wed, 18 Sep 2024 15:49:08 +0000 (16:49 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Sat, 21 Sep 2024 15:53:12 +0000 (10:53 -0500)
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>
man/insmod.8.scd
tools/insmod.c

index 137830fe99b1b42dd5f8454dfe72584d309dab6d..4c6d1f7a95e95e2448128234f8261cc138061294 100644 (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.
index 825cd7f2f2661945203f1a98967a9610a80c11f3..7c8b385eb5c5dae4a98804d020da13a2ffd249e1 100644 (file)
 
 #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;
 }