]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
Manually handle unrecognised getopt arguments master
authorEmil Velikov <emil.l.velikov@gmail.com>
Sun, 2 Aug 2026 14:19:30 +0000 (15:19 +0100)
committerLucas De Marchi <ldemarchi@kernel.org>
Wed, 5 Aug 2026 15:49:54 +0000 (10:49 -0500)
Was meaning to add a negative test or two to modinfo - where a invalid
option is passed into the program - only to notice that the default will
print the path, alongside the program name.

While fixing that, I've decided to pimp-up the reporting to also show
the help screen.

Eg. from this:

/usr/bin/insmod: unrecognized option '--versoin'

... to this

insmod: ERROR: unrecognised option '--versoin'

Usage:
insmod [options] filename [module options]
Options:
-f, --force              DANGEROUS: forces a module load, may cause
                         data corruption and crash your machine.
                         implies --force-modversion and
                         --force-vermagic
    --force-modversion   Ignore module's version
    --force-vermagic     Ignore module's version magic
-s, --syslog             print to syslog, not stderr
-v, --verbose            enables more messages
-V, --version            show version
-h, --help               show this help

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/453
Signed-off-by: Lucas De Marchi <ldemarchi@kernel.org>
testsuite/testsuite.c
tools/depmod.c
tools/insmod.c
tools/kmod.c
tools/lsmod.c
tools/modinfo.c
tools/modprobe.c
tools/rmmod.c
tools/static-nodes.c

index 8837ecbe3b7c7e1879a7219fbd1947b38026622e..e15f8977aed149af9e5a27d89858150e362d2803 100644 (file)
@@ -90,6 +90,7 @@ int test_init(const struct test *start, const struct test *stop, int argc,
                return -EINVAL;
        }
 
+       opterr = 0;
        while ((c = getopt_long(argc, argv, options_short, options, NULL)) != -1) {
                switch (c) {
                case 'l':
@@ -102,6 +103,8 @@ int test_init(const struct test *start, const struct test *stop, int argc,
                        oneshot = 1;
                        break;
                case '?':
+                       TS_ERR("unrecognised option \'%s\'\n\n", argv[optind - 1]);
+                       help();
                        return -1;
                default:
                        TS_ERR("unexpected getopt_long() value %c\n", c);
index 1349bef677f182bb4e9a9ee16a74e3568533034c..0fb6fbcb5e0da08c7c9a06303cf66bf1b99ef3d5 100644 (file)
@@ -2896,6 +2896,7 @@ static int do_depmod(int argc, char *argv[])
        memset(&cfg, 0, sizeof(cfg));
        memset(&depmod, 0, sizeof(depmod));
 
+       opterr = 0;
        while ((c = getopt_long(argc, argv, cmdopts_s, cmdopts, NULL)) != -1) {
                switch (c) {
                case 'a':
@@ -2971,6 +2972,8 @@ static int do_depmod(int argc, char *argv[])
                        kmod_version();
                        return EXIT_SUCCESS;
                case '?':
+                       ERR("unrecognised option \'%s\'\n\n", argv[optind - 1]);
+                       help();
                        goto cmdline_failed;
                default:
                        ERR("unexpected getopt_long() value '%c'.\n", c);
index 17d3fc80cb24dfbcde3efd57ee2eeb87c4a73f27..e1accdd7b1431409041353005b8a716057f9c956 100644 (file)
@@ -74,6 +74,7 @@ static int do_insmod(int argc, char *argv[])
        const char *null_config = NULL;
        unsigned int flags = 0;
 
+       opterr = 0;
        while ((c = getopt_long(argc, argv, cmdopts_s, cmdopts, NULL)) != -1) {
                switch (c) {
                case 'f':
@@ -99,6 +100,8 @@ static int do_insmod(int argc, char *argv[])
                        kmod_version();
                        return EXIT_SUCCESS;
                case '?':
+                       ERR("unrecognised option \'%s\'\n\n", argv[optind - 1]);
+                       help();
                        return EXIT_FAILURE;
                default:
                        ERR("unexpected getopt_long() value '%c'.\n", c);
index eda91e62ea328bb2a5d385404f6922077ca14f74..293a686292419c7afe5d2a01dc6d9f61ad6768d9 100644 (file)
@@ -85,6 +85,7 @@ static int handle_kmod_commands(int argc, char *argv[])
        int err = 0, c;
        size_t i;
 
+       opterr = 0;
        while ((c = getopt_long(argc, argv, options_s, options, NULL)) != -1) {
                switch (c) {
                case 'h':
@@ -94,6 +95,8 @@ static int handle_kmod_commands(int argc, char *argv[])
                        kmod_version();
                        return EXIT_SUCCESS;
                case '?':
+                       ERR("unrecognised option \'%s\'\n\n", argv[optind - 1]);
+                       kmod_help(argc, argv);
                        return EXIT_FAILURE;
                default:
                        fprintf(stderr, "Error: unexpected getopt_long() value '%c'.\n",
index a55097f51302240a86ca8357e8589c643f0d4b4c..e786f2588b68edaa99c23224043554e1716ee512 100644 (file)
@@ -45,6 +45,7 @@ static int do_lsmod(int argc, char *argv[])
        bool use_syslog = false;
        int err, c, r = 0;
 
+       opterr = 0;
        while ((c = getopt_long(argc, argv, cmdopts_s, cmdopts, NULL)) != -1) {
                switch (c) {
                case 's':
@@ -60,6 +61,8 @@ static int do_lsmod(int argc, char *argv[])
                        kmod_version();
                        return EXIT_SUCCESS;
                case '?':
+                       ERR("unrecognised option \'%s\'\n\n", argv[optind - 1]);
+                       help();
                        return EXIT_FAILURE;
                default:
                        ERR("unexpected getopt_long() value '%c'.\n", c);
index f5cb30745e73d2ed343765ef1ccd67fcdb86e849..8ec24a978fed52a68fb5283d6868532de38cb111 100644 (file)
@@ -367,6 +367,7 @@ static int do_modinfo(int argc, char *argv[])
        bool arg_is_modname = false;
        int i, err, c;
 
+       opterr = 0;
        while ((c = getopt_long(argc, argv, cmdopts_s, cmdopts, NULL)) != -1) {
                switch (c) {
                case 'a':
@@ -406,6 +407,8 @@ static int do_modinfo(int argc, char *argv[])
                        kmod_version();
                        return EXIT_SUCCESS;
                case '?':
+                       ERR("unrecognised option \'%s\'\n\n", argv[optind - 1]);
+                       help();
                        return EXIT_FAILURE;
                default:
                        ERR("unexpected getopt_long() value '%c'.\n", c);
index 6eb06cd4dea6c75dd8eeb692e710decf97272abc..c4b5021137a01529d75d9979e262308ce371bbae 100644 (file)
@@ -813,6 +813,7 @@ static int do_modprobe(int argc, char **orig_argv)
                return EXIT_FAILURE;
        }
 
+       opterr = 0;
        while ((c = getopt_long(argc, argv, cmdopts_s, cmdopts, NULL)) != -1) {
                switch (c) {
                case 'a':
@@ -935,6 +936,8 @@ static int do_modprobe(int argc, char **orig_argv)
                        err = 0;
                        goto done;
                case '?':
+                       ERR("unrecognised option \'%s\'\n\n", argv[optind - 1]);
+                       help();
                        err = -1;
                        goto done;
                default:
index d8504ea69a989670a714d9bf259daf0cf2d85cff..92f9f9c82474217d95c33425ac215c3c14830ef1 100644 (file)
@@ -101,6 +101,7 @@ static int do_rmmod(int argc, char *argv[])
        int flags = 0;
        int i, c, r = 0;
 
+       opterr = 0;
        while ((c = getopt_long(argc, argv, cmdopts_s, cmdopts, NULL)) != -1) {
                switch (c) {
                case 'f':
@@ -119,6 +120,8 @@ static int do_rmmod(int argc, char *argv[])
                        kmod_version();
                        return EXIT_SUCCESS;
                case '?':
+                       ERR("unrecognised option \'%s\'\n\n", argv[optind - 1]);
+                       help();
                        return EXIT_FAILURE;
                default:
                        ERR("unexpected getopt_long() value '%c'.\n", c);
index e64cd1b0a7de37ef7bfce7ef3550a138c6566738..61e239aef422f37759c35bf928346491c069d5de 100644 (file)
@@ -151,6 +151,7 @@ static int do_static_nodes(int argc, char *argv[])
        int r, ret = EXIT_SUCCESS;
        int c, valid;
 
+       opterr = 0;
        while ((c = getopt_long(argc, argv, cmdopts_s, cmdopts, NULL)) != -1) {
                switch (c) {
                case 'o':
@@ -177,6 +178,9 @@ static int do_static_nodes(int argc, char *argv[])
                        help();
                        goto finish;
                case '?':
+                       fprintf(stderr, "unrecognised option \'%s\'\n\n",
+                               argv[optind - 1]);
+                       help();
                        ret = EXIT_FAILURE;
                        goto finish;
                default: