]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
tools/lsmod: add basic opts like rmmod
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)
This allows for more consistent experience across tools, allows to see
the version, observe/debug if "Used by" reports -1, etc.

Also don't forget to update 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/lsmod.8.scd
tools/lsmod.c

index d81cce2f30ccf4a1d1b2c4f17e533f026645afde..72d2375cbe81cc4a8d42aa3bb175858e5882493f 100644 (file)
@@ -5,13 +5,33 @@ LSMOD(8) "kmod" "lsmod"
 lsmod - Show the status of modules in the Linux Kernel
 
 # SYNOPSIS
-*lsmod*
+
+*lsmod* [_OPTIONS_]
 
 # DESCRIPTION
 
 *lsmod* is a trivial program which nicely formats the contents of the
 /proc/modules, showing what kernel modules are currently loaded.
 
+# OPTIONS
+
+*-s*
+*--syslog*
+       Send errors to syslog instead of standard error.
+
+*-v*
+*--verbose*
+       Print messages about what the program is doing. Usually *lsmod* 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 323aad69eed6f12d3bdd3723dada00c4d3276032..6cbe70c76070fdfebe75aa38a4677f676890c0df 100644 (file)
@@ -4,6 +4,7 @@
  */
 
 #include <errno.h>
+#include <getopt.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
 
 #include "kmod.h"
 
+static const char cmdopts_s[] = "svVh";
+static const struct option cmdopts[] = {
+       // 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]\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);
+}
+
 static int do_lsmod(int argc, char *argv[])
 {
-       struct kmod_ctx *ctx;
+       struct kmod_ctx *ctx = NULL;
        const char *null_config = NULL;
        struct kmod_list *list, *itr;
-       int err;
+       int verbose = LOG_ERR;
+       int use_syslog;
+       int err, r = 0;
 
-       if (argc != 1) {
-               fprintf(stderr, "Usage: %s\n", argv[0]);
-               return EXIT_FAILURE;
+       for (;;) {
+               int c, idx = 0;
+               c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
+               if (c == -1)
+                       break;
+               switch (c) {
+               case 's':
+                       use_syslog = 1;
+                       break;
+               case 'v':
+                       verbose++;
+                       break;
+               case 'h':
+                       help();
+                       return EXIT_SUCCESS;
+               case 'V':
+                       kmod_version();
+                       return EXIT_SUCCESS;
+               case '?':
+                       return EXIT_FAILURE;
+               default:
+                       ERR("unexpected getopt_long() value '%c'.\n", c);
+                       return EXIT_FAILURE;
+               }
+       }
+
+       log_open(use_syslog);
+
+       if (optind < argc) {
+               ERR("too many arguments provided.\n");
+               r = EXIT_FAILURE;
+               goto done;
        }
 
        ctx = kmod_new(NULL, &null_config);
-       if (ctx == NULL) {
-               fputs("Error: kmod_new() failed!\n", stderr);
-               return EXIT_FAILURE;
+       if (!ctx) {
+               ERR("kmod_new() failed!\n");
+               r = EXIT_FAILURE;
+               goto done;
        }
 
+       log_setup_kmod_log(ctx, verbose);
+
        err = kmod_module_new_from_loaded(ctx, &list);
        if (err < 0) {
-               fprintf(stderr, "Error: could not get list of modules: %s\n",
-                       strerror(-err));
-               kmod_unref(ctx);
-               return EXIT_FAILURE;
+               ERR("could not get list of modules: %s\n", strerror(-err));
+               r = EXIT_FAILURE;
+               goto done;
        }
 
        puts("Module                  Size  Used by");
@@ -70,9 +127,12 @@ static int do_lsmod(int argc, char *argv[])
                kmod_module_unref(mod);
        }
        kmod_module_unref_list(list);
+
+done:
        kmod_unref(ctx);
+       log_close();
 
-       return EXIT_SUCCESS;
+       return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }
 
 const struct kmod_cmd kmod_cmd_compat_lsmod = {