[RFC] a few kldstat(8) improvements
Alexander Best
arundel at freebsd.org
Sun Apr 24 13:20:37 UTC 2011
hi there,
i hacked up kldstat(8) a bit. just wanted to hear what people think of it. the
changes are as follows:
- make -i , -m and -n flags mutually exclusive
- when iterating through kernel files and kernel modules, don't bail out when
an error occurs, but hand down the error via "int error"
- improve handling of -q and -v flags and call usage() when used improperly
- use errx(3) and warnx(3) rather than err(3) and warn(3) in order to keep
stderr at a sensible user information level
- adjust white space handling in order to improve formating
- a few kldstat(8) man page improvements
cheers.
alex
--
a13x
-------------- next part --------------
diff --git a/sbin/kldstat/kldstat.8 b/sbin/kldstat/kldstat.8
index 6f040e2..10b8fd5 100644
--- a/sbin/kldstat/kldstat.8
+++ b/sbin/kldstat/kldstat.8
@@ -25,7 +25,7 @@
.\"
.\" $FreeBSD$
.\"
-.Dd September 23, 2005
+.Dd April 24, 2011
.Dt KLDSTAT 8
.Os
.Sh NAME
@@ -35,10 +35,12 @@
.Nm
.Op Fl v
.Op Fl i Ar id
-.Op Fl n Ar filename
.Nm
.Op Fl q
.Op Fl m Ar modname
+.Nm
+.Op Fl v
+.Op Fl n Ar filename
.Sh DESCRIPTION
The
.Nm
@@ -47,16 +49,17 @@ kernel.
.Pp
The following options are available:
.Bl -tag -width indentXX
-.It Fl v
-Be more verbose.
+
.It Fl i Ar id
Display the status of only the file with this ID.
+.It Fl m Ar modname
+Display the status of only the module with this name.
.It Fl n Ar filename
-Display the status of only the file with this filename.
+Display the status of only the file with this name.
.It Fl q
-Only check if module is loaded or compiled into the kernel.
-.It Fl m Ar modname
-Display the status of only the module with this modname.
+Only check if the module is loaded or compiled into the kernel.
+.It Fl v
+Be more verbose.
.El
.Sh EXIT STATUS
.Ex -std
diff --git a/sbin/kldstat/kldstat.c b/sbin/kldstat/kldstat.c
index 575fca8..0b73f74 100644
--- a/sbin/kldstat/kldstat.c
+++ b/sbin/kldstat/kldstat.c
@@ -39,38 +39,42 @@ __FBSDID("$FreeBSD$");
#define POINTER_WIDTH ((int)(sizeof(void *) * 2 + 2))
static void
-printmod(int modid)
+printmod(int modid, int error)
{
struct module_stat stat;
stat.version = sizeof(struct module_stat);
- if (modstat(modid, &stat) < 0)
- warn("can't stat module id %d", modid);
+ if (modstat(modid, &stat) < 0) {
+ warnx("can't stat module id %d", modid);
+ error = 1;
+ }
else
- printf("\t\t%2d %s\n", stat.id, stat.name);
+ printf(" %-4d %s\n", stat.id, stat.name);
}
static void
-printfile(int fileid, int verbose)
+printfile(int fileid, int verbose, int error)
{
struct kld_file_stat stat;
int modid;
stat.version = sizeof(struct kld_file_stat);
- if (kldstat(fileid, &stat) < 0)
- err(1, "can't stat file id %d", fileid);
+ if (kldstat(fileid, &stat) < 0) {
+ warnx("can't stat file id %d", fileid);
+ error = 1;
+ }
else
- printf("%2d %4d %p %-8zx %s",
- stat.id, stat.refs, stat.address, stat.size,
+ printf("%-3d %4d %p %-8zx %s",
+ stat.id, stat.refs, stat.address, stat.size,
stat.name);
if (verbose) {
printf(" (%s)\n", stat.pathname);
- printf("\tContains modules:\n");
- printf("\t\tId Name\n");
+ printf(" Contains modules:\n");
+ printf(" Id Name\n");
for (modid = kldfirstmod(fileid); modid > 0;
modid = modfnext(modid))
- printmod(modid);
+ printmod(modid, error);
} else
printf("\n");
}
@@ -78,21 +82,20 @@ printfile(int fileid, int verbose)
static void
usage(void)
{
- fprintf(stderr, "usage: kldstat [-v] [-i id] [-n filename]\n");
+ fprintf(stderr, "usage: kldstat [-v] [-i id]\n");
fprintf(stderr, " kldstat [-q] [-m modname]\n");
+ fprintf(stderr, " kldstat [-v] [-n filename]\n");
exit(1);
}
int
main(int argc, char** argv)
{
- int c;
- int verbose = 0;
- int fileid = 0;
- int quiet = 0;
- char* filename = NULL;
- char* modname = NULL;
- char* p;
+ int c, error, fileid, quiet, verbose;
+ char *filename, *modname, *p;
+
+ error = fileid = quiet = verbose = 0;
+ filename = modname = NULL;
while ((c = getopt(argc, argv, "i:m:n:qv")) != -1)
switch (c) {
@@ -122,13 +125,21 @@ main(int argc, char** argv)
if (argc != 0)
usage();
+ if ((*p == '\0' && modname != NULL) || (*p == '\0' && filename != NULL) ||
+ (modname != NULL && filename != NULL))
+ errx(1, "-i, -m and -n flags are mutually exclusive");
+
+ if ((*p == '\0' && quiet) || (modname != NULL && verbose) ||
+ (filename != NULL && quiet))
+ usage();
+
if (modname != NULL) {
int modid;
struct module_stat stat;
if ((modid = modfind(modname)) < 0) {
if (!quiet)
- warn("can't find module %s", modname);
+ warnx("can't find module %s", modname);
return 1;
} else if (quiet) {
return 0;
@@ -136,10 +147,10 @@ main(int argc, char** argv)
stat.version = sizeof(struct module_stat);
if (modstat(modid, &stat) < 0)
- warn("can't stat module id %d", modid);
+ errx(1, "can't stat module id %d", modid);
else {
printf("Id Refs Name\n");
- printf("%3d %4d %s\n", stat.id, stat.refs, stat.name);
+ printf("%-3d %4d %s\n", stat.id, stat.refs, stat.name);
}
return 0;
@@ -147,15 +158,15 @@ main(int argc, char** argv)
if (filename != NULL) {
if ((fileid = kldfind(filename)) < 0)
- err(1, "can't find file %s", filename);
+ errx(1, "can't find file %s", filename);
}
- printf("Id Refs Address%*c Size Name\n", POINTER_WIDTH - 7, ' ');
+ printf("Id Refs Address%*c Size Name\n", POINTER_WIDTH - 7, ' ');
if (fileid != 0)
- printfile(fileid, verbose);
+ printfile(fileid, verbose, error);
else
for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid))
- printfile(fileid, verbose);
+ printfile(fileid, verbose, error);
- return 0;
+ return error;
}
More information about the freebsd-hackers
mailing list