From 5658c6d3f789b6f34eb03033247579a51c78fcff Mon Sep 17 00:00:00 2001 From: "Wojciech A. Koszek" Date: Mon, 27 Feb 2006 22:20:57 +0000 Subject: Extend kldunload(8) functionality and fix minor problems: o multiple modules can be unloaded at once (specified either by id or be module name) o exit with EX_USAGE after usage() is called. o remove unused variables, since we keep command line flags as bitmask, in 'opt'. o 'kldload -n ...' does nothing. Add comment to this options. Additionally: o Update manual page to conform new functionality. o Increace WARNS to 6. Because we can. Approved by: cognet (mentor) MFC after: 1 week --- sbin/kldunload/Makefile | 2 +- sbin/kldunload/kldunload.8 | 4 +-- sbin/kldunload/kldunload.c | 84 ++++++++++++++++++++++++++-------------------- 3 files changed, 50 insertions(+), 40 deletions(-) (limited to 'sbin') diff --git a/sbin/kldunload/Makefile b/sbin/kldunload/Makefile index 1f71a8f8313e..48e5c035ccdc 100644 --- a/sbin/kldunload/Makefile +++ b/sbin/kldunload/Makefile @@ -28,6 +28,6 @@ PROG= kldunload MAN= kldunload.8 -WARNS?= 5 +WARNS?= 6 .include diff --git a/sbin/kldunload/kldunload.8 b/sbin/kldunload/kldunload.8 index 3cd8de71df5c..41df595d82b0 100644 --- a/sbin/kldunload/kldunload.8 +++ b/sbin/kldunload/kldunload.8 @@ -34,11 +34,11 @@ .Sh SYNOPSIS .Nm .Op Fl fv -.Fl i Ar id +.Fl i Ar id ... .Nm .Op Fl fv .Op Fl n -.Ar name +.Ar name ... .Sh DESCRIPTION The .Nm diff --git a/sbin/kldunload/kldunload.c b/sbin/kldunload/kldunload.c index cafc7ea4dee4..8a3ea6176f66 100644 --- a/sbin/kldunload/kldunload.c +++ b/sbin/kldunload/kldunload.c @@ -33,41 +33,48 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include static void usage(void) { - fprintf(stderr, "usage: kldunload [-fv] -i id\n"); - fprintf(stderr, " kldunload [-fv] [-n] name\n"); - exit(1); + fprintf(stderr, "usage: kldunload [-fv] -i id ...\n"); + fprintf(stderr, " kldunload [-fv] [-n] name ...\n"); + exit(EX_USAGE); } +#define OPT_NULL 0x00 +#define OPT_ID 0x01 +#define OPT_VERBOSE 0x02 +#define OPT_FORCE 0x04 + int main(int argc, char** argv) { struct kld_file_stat stat; - int c; - int verbose = 0; - int fileid = 0; - int force = LINKER_UNLOAD_NORMAL; - char *filename = NULL; + int c, fileid, force, opt; + char *filename; + + filename = NULL; + opt = OPT_NULL; - while ((c = getopt(argc, argv, "fi:n:v")) != -1) { + while ((c = getopt(argc, argv, "finv")) != -1) { switch (c) { case 'f': - force = LINKER_UNLOAD_FORCE; + opt |= OPT_FORCE; break; case 'i': - fileid = atoi(optarg); - if (!fileid) - errx(1, "Invalid ID %s", optarg); + opt |= OPT_ID; break; case 'n': - filename = optarg; + /* + * XXX: For backward compatibility. Currently does + * nothing + */ break; case 'v': - verbose = 1; + opt |= OPT_VERBOSE; break; default: usage(); @@ -77,31 +84,34 @@ main(int argc, char** argv) argc -= optind; argv += optind; - if (fileid == 0 && filename == NULL && (argc == 1)) { - filename = *argv; - argc--; - } - - if (argc != 0 || (fileid != 0 && filename != NULL)) - usage(); - - if (fileid == 0 && filename == NULL) + if (argc == 0) usage(); - if (filename != NULL) { - if ((fileid = kldfind(filename)) < 0) - err(1, "can't find file %s", filename); - } + while ((filename = *argv++) != NULL) { + if (opt & OPT_ID) { + fileid = atoi(filename); + if (fileid < 0) + errx(EXIT_FAILURE, "Invalid ID %s", optarg); + } else { + if ((fileid = kldfind(filename)) < 0) + errx(EXIT_FAILURE, "can't find file %s", + filename); + } + if (opt & OPT_VERBOSE) { + stat.version = sizeof(stat); + if (kldstat(fileid, &stat) < 0) + err(EXIT_FAILURE, "can't stat file"); + (void) printf("Unloading %s, id=%d\n", stat.name, + fileid); + } + if (opt & OPT_FORCE) + force = LINKER_UNLOAD_FORCE; + else + force = LINKER_UNLOAD_NORMAL; - if (verbose) { - stat.version = sizeof stat; - if (kldstat(fileid, &stat) < 0) - err(1, "can't stat file"); - printf("Unloading %s, id=%d\n", stat.name, fileid); + if (kldunloadf(fileid, force) < 0) + err(EXIT_FAILURE, "can't unload file"); } - if (kldunloadf(fileid, force) < 0) - err(1, "can't unload file"); - - return 0; + return (EXIT_SUCCESS); } -- cgit v1.2.3