Skip to content

Commit 2bee7f4

Browse files
committed
Manually handle unrecognised getopt arguments
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. So instead I've decided to pimp-up the reporting while also showing 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>
1 parent 71ca0dd commit 2bee7f4

9 files changed

Lines changed: 27 additions & 0 deletions

File tree

testsuite/testsuite.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ int test_init(const struct test *start, const struct test *stop, int argc,
9090
return -EINVAL;
9191
}
9292

93+
opterr = 0;
9394
while ((c = getopt_long(argc, argv, options_short, options, NULL)) != -1) {
9495
switch (c) {
9596
case 'l':
@@ -102,6 +103,8 @@ int test_init(const struct test *start, const struct test *stop, int argc,
102103
oneshot = 1;
103104
break;
104105
case '?':
106+
TS_ERR("unrecognised option \'%s\'\n\n", argv[optind-1]);
107+
help();
105108
return -1;
106109
default:
107110
TS_ERR("unexpected getopt_long() value %c\n", c);

tools/depmod.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2896,6 +2896,7 @@ static int do_depmod(int argc, char *argv[])
28962896
memset(&cfg, 0, sizeof(cfg));
28972897
memset(&depmod, 0, sizeof(depmod));
28982898

2899+
opterr = 0;
28992900
while ((c = getopt_long(argc, argv, cmdopts_s, cmdopts, NULL)) != -1) {
29002901
switch (c) {
29012902
case 'a':
@@ -2971,6 +2972,8 @@ static int do_depmod(int argc, char *argv[])
29712972
kmod_version();
29722973
return EXIT_SUCCESS;
29732974
case '?':
2975+
ERR("unrecognised option \'%s\'\n\n", argv[optind-1]);
2976+
help();
29742977
goto cmdline_failed;
29752978
default:
29762979
ERR("unexpected getopt_long() value '%c'.\n", c);

tools/insmod.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ static int do_insmod(int argc, char *argv[])
7474
const char *null_config = NULL;
7575
unsigned int flags = 0;
7676

77+
opterr = 0;
7778
while ((c = getopt_long(argc, argv, cmdopts_s, cmdopts, NULL)) != -1) {
7879
switch (c) {
7980
case 'f':
@@ -99,6 +100,8 @@ static int do_insmod(int argc, char *argv[])
99100
kmod_version();
100101
return EXIT_SUCCESS;
101102
case '?':
103+
ERR("unrecognised option \'%s\'\n\n", argv[optind-1]);
104+
help();
102105
return EXIT_FAILURE;
103106
default:
104107
ERR("unexpected getopt_long() value '%c'.\n", c);

tools/kmod.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ static int handle_kmod_commands(int argc, char *argv[])
8585
int err = 0, c;
8686
size_t i;
8787

88+
opterr = 0;
8889
while ((c = getopt_long(argc, argv, options_s, options, NULL)) != -1) {
8990
switch (c) {
9091
case 'h':
@@ -94,6 +95,8 @@ static int handle_kmod_commands(int argc, char *argv[])
9495
kmod_version();
9596
return EXIT_SUCCESS;
9697
case '?':
98+
ERR("unrecognised option \'%s\'\n\n", argv[optind-1]);
99+
kmod_help(argc, argv);
97100
return EXIT_FAILURE;
98101
default:
99102
fprintf(stderr, "Error: unexpected getopt_long() value '%c'.\n",

tools/lsmod.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ static int do_lsmod(int argc, char *argv[])
4545
bool use_syslog = false;
4646
int err, c, r = 0;
4747

48+
opterr = 0;
4849
while ((c = getopt_long(argc, argv, cmdopts_s, cmdopts, NULL)) != -1) {
4950
switch (c) {
5051
case 's':
@@ -60,6 +61,8 @@ static int do_lsmod(int argc, char *argv[])
6061
kmod_version();
6162
return EXIT_SUCCESS;
6263
case '?':
64+
ERR("unrecognised option \'%s\'\n\n", argv[optind-1]);
65+
help();
6366
return EXIT_FAILURE;
6467
default:
6568
ERR("unexpected getopt_long() value '%c'.\n", c);

tools/modinfo.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ static int do_modinfo(int argc, char *argv[])
367367
bool arg_is_modname = false;
368368
int i, err, c;
369369

370+
opterr = 0;
370371
while ((c = getopt_long(argc, argv, cmdopts_s, cmdopts, NULL)) != -1) {
371372
switch (c) {
372373
case 'a':
@@ -406,6 +407,8 @@ static int do_modinfo(int argc, char *argv[])
406407
kmod_version();
407408
return EXIT_SUCCESS;
408409
case '?':
410+
ERR("unrecognised option \'%s\'\n\n", argv[optind-1]);
411+
help();
409412
return EXIT_FAILURE;
410413
default:
411414
ERR("unexpected getopt_long() value '%c'.\n", c);

tools/modprobe.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,7 @@ static int do_modprobe(int argc, char **orig_argv)
813813
return EXIT_FAILURE;
814814
}
815815

816+
opterr = 0;
816817
while ((c = getopt_long(argc, argv, cmdopts_s, cmdopts, NULL)) != -1) {
817818
switch (c) {
818819
case 'a':
@@ -935,6 +936,8 @@ static int do_modprobe(int argc, char **orig_argv)
935936
err = 0;
936937
goto done;
937938
case '?':
939+
ERR("unrecognised option \'%s\'\n\n", argv[optind-1]);
940+
help();
938941
err = -1;
939942
goto done;
940943
default:

tools/rmmod.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ static int do_rmmod(int argc, char *argv[])
101101
int flags = 0;
102102
int i, c, r = 0;
103103

104+
opterr = 0;
104105
while ((c = getopt_long(argc, argv, cmdopts_s, cmdopts, NULL)) != -1) {
105106
switch (c) {
106107
case 'f':
@@ -119,6 +120,8 @@ static int do_rmmod(int argc, char *argv[])
119120
kmod_version();
120121
return EXIT_SUCCESS;
121122
case '?':
123+
ERR("unrecognised option \'%s\'\n\n", argv[optind-1]);
124+
help();
122125
return EXIT_FAILURE;
123126
default:
124127
ERR("unexpected getopt_long() value '%c'.\n", c);

tools/static-nodes.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ static int do_static_nodes(int argc, char *argv[])
151151
int r, ret = EXIT_SUCCESS;
152152
int c, valid;
153153

154+
opterr = 0;
154155
while ((c = getopt_long(argc, argv, cmdopts_s, cmdopts, NULL)) != -1) {
155156
switch (c) {
156157
case 'o':
@@ -177,6 +178,8 @@ static int do_static_nodes(int argc, char *argv[])
177178
help();
178179
goto finish;
179180
case '?':
181+
fprintf(stderr, "unrecognised option \'%s\'\n\n", argv[optind-1]);
182+
help();
180183
ret = EXIT_FAILURE;
181184
goto finish;
182185
default:

0 commit comments

Comments
 (0)